{"id":219,"date":"2006-03-12T14:56:05","date_gmt":"2006-03-12T22:56:05","guid":{"rendered":"http:\/\/multimedia.cx\/eggs\/?p=219"},"modified":"2006-03-12T15:07:44","modified_gmt":"2006-03-12T23:07:44","slug":"aac-puzzles","status":"publish","type":"post","link":"https:\/\/multimedia.cx\/eggs\/aac-puzzles\/","title":{"rendered":"AAC Puzzles"},"content":{"rendered":"<p>You thought those <a href=\"http:\/\/multimedia.cx\/eggs\/category\/pickover-puzzles\/\">Pickover puzzles<\/a> were inane? Well, you were right. But it does help to keep a programmer sharp on pure computer science skills. One reason I have been having such a good time reverse engineering the <a href=\"http:\/\/audiocoding.com\/\">FAAD2<\/a> source is because it represents a bunch of little puzzles. Only they&#8217;re considerably more straightforward and grounded in reality than the Pickover puzzles. If these puzzles were on a Pickover calendar page, they would be worded along the lines of, <\/p>\n<blockquote><p>&#8220;There are 8,192 numbers stored in a table called iq_table. 0, 1, 2.5198420997897464, 4.3267487109222245, all the way up to 165113.4940829452. What is the common property that connects all of these numbers?&#8221;\n<\/p><\/blockquote>\n<p>The answer (thanks to Jindrich Makovicka for pointing this out on the ffmpeg-devel list) is that each number is its index (0..8191) raised to an exponent of 4\/3: iq_table[index] = index<sup>4\/3<\/sup>. Here are some more puzzles:<\/p>\n<p><!--more--><\/p>\n<p>Split an 8-bit quantity into an exponent (top 6 bits) and fraction (bottom 2 bits). Multiply <em>number<\/em> by quantities from 2 tables indexed by the fraction and the exponent:<\/p>\n<pre>\r\n  number *= pow2sf_tab[exponent];\r\n  number *= pow2_table[fraction];\r\n<\/pre>\n<p>pow2sf_tab is defined as:<\/p>\n<pre>\r\n  foreach i in -25..38\r\n    pow2sf_tab[i] = 2<sup>i<\/sup>\r\n<\/pre>\n<p>while pow2_table is defined as:<\/p>\n<pre>\r\n  foreach i in 0..3\r\n    pow2_table[i] = 2<sup>i\/4<\/sup>\r\n<\/pre>\n<p>Question: What is the distilled mathematical operation that is being performed on <em>number<\/em>?<\/p>\n<p>As <a href=\"http:\/\/multimedia.cx\/eggs\/new-aac-spec\/\">mentioned previously<\/a>, I am writing a new <a href=\"http:\/\/wiki.multimedia.cx\/index.php?title=Understanding_AAC\">AAC spec<\/a>. Part of that process is gathering up all of the Huffman tables and formatting them in such a way that they can be easily imported into <a href=\"http:\/\/www.ffmpeg.org\/\">FFmpeg<\/a>. There are a 12 different hardcoded Huffman tables in use in AAC decoding. Tables 1..10 are used for coding quads or pairs of spectral coefficients. I&#8217;m not entirely sure what table 11 is for yet. Then there is a scalefactor Huffman table. Based on the specific table, FAAD2 uses 2 different methods for decoding. For 5 of the tables (3, 5, 7, 9, and scalefactors), FAAD2 uses a textbook tree-based Huffman decoder. The remainder of the spectral coefficient tables use a more efficient table-based approach.<\/p>\n<p>I have written 2 different programs (1 for scalefactors and 1 for tables 3, 5, 7, and 9) to convert the binary tree tables to tables that FFmpeg can more easily use (converted tables are <a href=\"http:\/\/wiki.multimedia.cx\/index.php?title=AAC_Huffman_Tables\">available in the MultimediaWiki<\/a>). I decided to make this AAC documentation more of a community effort. That, and I&#8217;m too lazy to finish solving this particular problem so I am throwing it out there to see if someone else wants to do it while I work on the new spec some more. The challenge is to convert Huffman tables 1, 2, 4, 6, 8, and 10 to the <em>{ codeword, code length, data }<\/em> format that FFmpeg will be able to use.<\/p>\n<p>The <a href=\"http:\/\/multimedia.cx\/aac\/\">codebooks are here<\/a>. For example, Huffman table 1 is in hcb_1.h. It has 2 tables: hcb1_1[] and hcb1_2[]. The step 1 table contains data sets such as:<\/p>\n<pre>\r\n  ...\r\n    { \/*       *\/ 0, 0 },\r\n    { \/* 10000 *\/ 1, 0 },\r\n  ...\r\n <\/pre>\n<p>The first number is offset while the second number denotes extrabits. The step 2 table contains data such as:<\/p>\n<pre>\r\n  ...\r\n    { 5,  1,  0,  0,  0 },\r\n    { 5, -1,  0,  0,  0 },\r\n  ...\r\n<\/pre>\n<p>The first number represents the total number of bits in the codeword and the next 4 numbers are the components of the spectral coefficient quad. The method for decoding based on these tables is:<\/p>\n<ul>\n<li>&#8220;show&#8221; (read but don&#8217;t extract) the next 5 bits from the stream (6 in the case of Huffman table 10)<\/li>\n<li>use those 5-6 bits to index into the step 1 table and obtain offset and extrabits<\/li>\n<li>if extrabits is non-zero<\/li>\n<ol>\n<li>flush 5-6 bits from stream, depending on Huffman table<\/li>\n<li>read (extrabits) bits from the stream, add them to the offset quantity decoded from the step 1 table<\/li>\n<li>use offset to index into the step 2 table and obtain the total bits for the Huffman codeword<\/li>\n<li>subtract either 5-6 bits from the total bits (since they were already flushed) and flush the remaining bits from the stream<\/li>\n<\/ol>\n<li>else<\/li>\n<ol>\n<li>use offset to index into the step 2 table and obtain the total bits for the Huffman codeword<\/li>\n<li>flush the codeword bits from the stream<\/li>\n<\/ol>\n<li>use offset to index into the step 2 table and obtain the data quad or pair represented by the Huffman code<\/li>\n<\/ul>\n<p>For the curious, this is the code I wrote to convert the scalefactor Huffman table (hcb_sf.h):<\/p>\n<pre>\r\n#include <font COLOR=BLUE SIZE=+1>&lt;<\/font>stdio<font COLOR=BLUE SIZE=+1><b>.<\/b><\/font>h<font COLOR=BLUE SIZE=+1>&gt;<\/font>\r\n#include <font COLOR=BLUE SIZE=+1>&lt;<\/font>inttypes<font COLOR=BLUE SIZE=+1><b>.<\/b><\/font>h<font COLOR=BLUE SIZE=+1>&gt;<\/font>\r\n#include <font COLOR=PURPLE>\"hcb_sf.h\"<\/font>\r\n\r\n<font COLOR=RED><b>static<\/b><\/font> <font COLOR=RED><b>void<\/b><\/font> traverse_tree_node<font COLOR=BLUE SIZE=+1><b>(<\/b><\/font><font COLOR=RED><b>int<\/b><\/font> node<font COLOR=BLUE SIZE=+1><b>,<\/b><\/font>\r\n    unsigned <font COLOR=RED><b>int<\/b><\/font> current_codeword<font COLOR=BLUE SIZE=+1><b>,<\/b><\/font> <font COLOR=RED><b>int<\/b><\/font> current_size<font COLOR=BLUE SIZE=+1><b>)<\/b><\/font>\r\n<font COLOR=BLUE SIZE=+1><b>{<\/b><\/font>\r\n    <font COLOR=RED><b>if<\/b><\/font> <font COLOR=BLUE SIZE=+1><b>(<\/b><\/font>hcb_sf<font COLOR=BLUE SIZE=+1><b>[<\/b><\/font>node<font COLOR=BLUE SIZE=+1><b>]<\/b><\/font><font COLOR=BLUE SIZE=+1><b>[<\/b><\/font><font COLOR=BROWN>1<\/font><font COLOR=BLUE SIZE=+1><b>]<\/b><\/font> <font COLOR=BLUE SIZE=+1>=<\/font><font COLOR=BLUE SIZE=+1>=<\/font> <font COLOR=BROWN>0<\/font><font COLOR=BLUE SIZE=+1><b>)<\/b><\/font> <font COLOR=BLUE SIZE=+1><b>{<\/b><\/font>\r\n        printf<font COLOR=BLUE SIZE=+1><b>(<\/b><\/font><font COLOR=PURPLE>\"    { 0x%X, %d, %d },\\n\"<\/font><font COLOR=BLUE SIZE=+1><b>,<\/b><\/font> current_codeword<font COLOR=BLUE SIZE=+1><b>,<\/b><\/font> current_size<font COLOR=BLUE SIZE=+1><b>,<\/b><\/font>\r\n            hcb_sf<font COLOR=BLUE SIZE=+1><b>[<\/b><\/font>node<font COLOR=BLUE SIZE=+1><b>]<\/b><\/font><font COLOR=BLUE SIZE=+1><b>[<\/b><\/font><font COLOR=BROWN>0<\/font><font COLOR=BLUE SIZE=+1><b>]<\/b><\/font><font COLOR=BLUE SIZE=+1><b>)<\/b><\/font><font COLOR=BLUE SIZE=+1><b>;<\/b><\/font>\r\n    <font COLOR=BLUE SIZE=+1><b>}<\/b><\/font> <font COLOR=RED><b>else<\/b><\/font> <font COLOR=BLUE SIZE=+1><b>{<\/b><\/font>\r\n        traverse_tree_node<font COLOR=BLUE SIZE=+1><b>(<\/b><\/font>hcb_sf<font COLOR=BLUE SIZE=+1><b>[<\/b><\/font>node<font COLOR=BLUE SIZE=+1><b>]<\/b><\/font><font COLOR=BLUE SIZE=+1><b>[<\/b><\/font><font COLOR=BROWN>0<\/font><font COLOR=BLUE SIZE=+1><b>]<\/b><\/font> <font COLOR=BLUE SIZE=+1>+<\/font> node<font COLOR=BLUE SIZE=+1><b>,<\/b><\/font>\r\n            current_codeword <font COLOR=BLUE SIZE=+1>&lt;<\/font><font COLOR=BLUE SIZE=+1>&lt;<\/font> <font COLOR=BROWN>1<\/font><font COLOR=BLUE SIZE=+1><b>,<\/b><\/font> current_size <font COLOR=BLUE SIZE=+1>+<\/font> <font COLOR=BROWN>1<\/font><font COLOR=BLUE SIZE=+1><b>)<\/b><\/font><font COLOR=BLUE SIZE=+1><b>;<\/b><\/font>\r\n        traverse_tree_node<font COLOR=BLUE SIZE=+1><b>(<\/b><\/font>hcb_sf<font COLOR=BLUE SIZE=+1><b>[<\/b><\/font>node<font COLOR=BLUE SIZE=+1><b>]<\/b><\/font><font COLOR=BLUE SIZE=+1><b>[<\/b><\/font><font COLOR=BROWN>1<\/font><font COLOR=BLUE SIZE=+1><b>]<\/b><\/font> <font COLOR=BLUE SIZE=+1>+<\/font> node<font COLOR=BLUE SIZE=+1><b>,<\/b><\/font>\r\n            <font COLOR=BLUE SIZE=+1><b>(<\/b><\/font>current_codeword <font COLOR=BLUE SIZE=+1>&lt;<\/font><font COLOR=BLUE SIZE=+1>&lt;<\/font> <font COLOR=BROWN>1<\/font><font COLOR=BLUE SIZE=+1><b>)<\/b><\/font> <font COLOR=BLUE SIZE=+1>|<\/font> <font COLOR=BROWN>1<\/font><font COLOR=BLUE SIZE=+1><b>,<\/b><\/font> current_size <font COLOR=BLUE SIZE=+1>+<\/font> <font COLOR=BROWN>1<\/font><font COLOR=BLUE SIZE=+1><b>)<\/b><\/font><font COLOR=BLUE SIZE=+1><b>;<\/b><\/font>\r\n    <font COLOR=BLUE SIZE=+1><b>}<\/b><\/font>\r\n<font COLOR=BLUE SIZE=+1><b>}<\/b><\/font>\r\n\r\n<font COLOR=RED><b>int<\/b><\/font> main<font COLOR=BLUE SIZE=+1><b>(<\/b><\/font><font COLOR=RED><b>int<\/b><\/font> argc<font COLOR=BLUE SIZE=+1><b>,<\/b><\/font> <font COLOR=RED><b>char<\/b><\/font> <font COLOR=BLUE SIZE=+1>*<\/font>argv<font COLOR=BLUE SIZE=+1><b>[<\/b><\/font><font COLOR=BLUE SIZE=+1><b>]<\/b><\/font><font COLOR=BLUE SIZE=+1><b>)<\/b><\/font>\r\n<font COLOR=BLUE SIZE=+1><b>{<\/b><\/font>\r\n    printf<font COLOR=BLUE SIZE=+1><b>(<\/b><\/font><font COLOR=PURPLE>\"static const unsigned int aac_scalefactor_huffman_table[][3] = {\\n\"<\/font><font COLOR=BLUE SIZE=+1><b>);<\/b><\/font>\r\n    printf<font COLOR=BLUE SIZE=+1><b>(<\/b><\/font><font COLOR=PURPLE>\"    \/* codeword, code length, scalefactor *\/\\n\"<\/font><font COLOR=BLUE SIZE=+1><b>)<\/b><\/font><font COLOR=BLUE SIZE=+1><b>;<\/b><\/font>\r\n    traverse_tree_node<font COLOR=BLUE SIZE=+1><b>(<\/b><\/font><font COLOR=BROWN>0<\/font><font COLOR=BLUE SIZE=+1><b>,<\/b><\/font> <font COLOR=BROWN>0<\/font><font COLOR=BLUE SIZE=+1><b>,<\/b><\/font> <font COLOR=BROWN>0<\/font><font COLOR=BLUE SIZE=+1><b>)<\/b><\/font><font COLOR=BLUE SIZE=+1><b>;<\/b><\/font>\r\n    printf<font COLOR=BLUE SIZE=+1><b>(<\/b><\/font><font COLOR=PURPLE>\"};\\n\"<\/font><font COLOR=BLUE SIZE=+1><b>)<\/b><\/font><font COLOR=BLUE SIZE=+1><b>;<\/b><\/font>\r\n\r\n    <font COLOR=RED><b>return<\/b><\/font> <font COLOR=BROWN>0<\/font><font COLOR=BLUE SIZE=+1><b>;<\/b><\/font>\r\n<font COLOR=BLUE SIZE=+1><b>}<\/b><\/font>\r\n<\/pre>\n<p><em>Code colorized by the <a href=\"http:\/\/www.chami.com\/colorizer\/\">CodeColorizer<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now&#8217;s your chance to solve some real puzzles&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,27],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-open-source-multimedia","category-pickover-puzzles"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":0,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"wp:attachment":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}