Generating Huffman codes from unsorted code lengths.

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












As an exercise for myself, I am writing an Ogg Vorbis decoder. Part of the decoding process involves generating Huffman codes from a series of code lengths that is later used when decoding audio packets.



The thing is, I am familiar with Huffman coding from having written a PNG decoder as well. The difference here is that the Huffman tables in the zlib format rely on the code lengths being sorted, whereas the Vorbis specification does not.



Here is an example given in the Vorbis spec (https://xiph.org/vorbis/doc/Vorbis_I_spec.html, section 3.2.1):



There are 8 entries, each with a code length:



  • entry 0: length 2

  • entry 1: length 4

  • entry 2: length 4

  • entry 3: length 4

  • entry 4: length 4

  • entry 5: length 2

  • entry 6: length 3

  • entry 7: length 3

Assigning codes in order (lowest possible value of the appropriate length to highest) results in the following:



  • entry 0: length 2, code 00

  • entry 1: length 4, code 0100

  • entry 2: length 4, code 0101

  • entry 3: length 4, code 0110

  • entry 4: length 4, code 0111

  • entry 5: length 2, code 10

  • entry 6: length 3, code 110

  • entry 7: length 3, code 111

I'm having difficulty seeing how to generate those codes from the given lengths. As can be seen above, not all codes of the same length increase monotonically. All the codes of length 4 do because they follow one after the other, but entry 5 (with code 10, or 2 in decimal) does not monotonically follow entry 0 (with code 00).



How would I go about generating these codes?







share|cite|improve this question
























    up vote
    0
    down vote

    favorite












    As an exercise for myself, I am writing an Ogg Vorbis decoder. Part of the decoding process involves generating Huffman codes from a series of code lengths that is later used when decoding audio packets.



    The thing is, I am familiar with Huffman coding from having written a PNG decoder as well. The difference here is that the Huffman tables in the zlib format rely on the code lengths being sorted, whereas the Vorbis specification does not.



    Here is an example given in the Vorbis spec (https://xiph.org/vorbis/doc/Vorbis_I_spec.html, section 3.2.1):



    There are 8 entries, each with a code length:



    • entry 0: length 2

    • entry 1: length 4

    • entry 2: length 4

    • entry 3: length 4

    • entry 4: length 4

    • entry 5: length 2

    • entry 6: length 3

    • entry 7: length 3

    Assigning codes in order (lowest possible value of the appropriate length to highest) results in the following:



    • entry 0: length 2, code 00

    • entry 1: length 4, code 0100

    • entry 2: length 4, code 0101

    • entry 3: length 4, code 0110

    • entry 4: length 4, code 0111

    • entry 5: length 2, code 10

    • entry 6: length 3, code 110

    • entry 7: length 3, code 111

    I'm having difficulty seeing how to generate those codes from the given lengths. As can be seen above, not all codes of the same length increase monotonically. All the codes of length 4 do because they follow one after the other, but entry 5 (with code 10, or 2 in decimal) does not monotonically follow entry 0 (with code 00).



    How would I go about generating these codes?







    share|cite|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      As an exercise for myself, I am writing an Ogg Vorbis decoder. Part of the decoding process involves generating Huffman codes from a series of code lengths that is later used when decoding audio packets.



      The thing is, I am familiar with Huffman coding from having written a PNG decoder as well. The difference here is that the Huffman tables in the zlib format rely on the code lengths being sorted, whereas the Vorbis specification does not.



      Here is an example given in the Vorbis spec (https://xiph.org/vorbis/doc/Vorbis_I_spec.html, section 3.2.1):



      There are 8 entries, each with a code length:



      • entry 0: length 2

      • entry 1: length 4

      • entry 2: length 4

      • entry 3: length 4

      • entry 4: length 4

      • entry 5: length 2

      • entry 6: length 3

      • entry 7: length 3

      Assigning codes in order (lowest possible value of the appropriate length to highest) results in the following:



      • entry 0: length 2, code 00

      • entry 1: length 4, code 0100

      • entry 2: length 4, code 0101

      • entry 3: length 4, code 0110

      • entry 4: length 4, code 0111

      • entry 5: length 2, code 10

      • entry 6: length 3, code 110

      • entry 7: length 3, code 111

      I'm having difficulty seeing how to generate those codes from the given lengths. As can be seen above, not all codes of the same length increase monotonically. All the codes of length 4 do because they follow one after the other, but entry 5 (with code 10, or 2 in decimal) does not monotonically follow entry 0 (with code 00).



      How would I go about generating these codes?







      share|cite|improve this question












      As an exercise for myself, I am writing an Ogg Vorbis decoder. Part of the decoding process involves generating Huffman codes from a series of code lengths that is later used when decoding audio packets.



      The thing is, I am familiar with Huffman coding from having written a PNG decoder as well. The difference here is that the Huffman tables in the zlib format rely on the code lengths being sorted, whereas the Vorbis specification does not.



      Here is an example given in the Vorbis spec (https://xiph.org/vorbis/doc/Vorbis_I_spec.html, section 3.2.1):



      There are 8 entries, each with a code length:



      • entry 0: length 2

      • entry 1: length 4

      • entry 2: length 4

      • entry 3: length 4

      • entry 4: length 4

      • entry 5: length 2

      • entry 6: length 3

      • entry 7: length 3

      Assigning codes in order (lowest possible value of the appropriate length to highest) results in the following:



      • entry 0: length 2, code 00

      • entry 1: length 4, code 0100

      • entry 2: length 4, code 0101

      • entry 3: length 4, code 0110

      • entry 4: length 4, code 0111

      • entry 5: length 2, code 10

      • entry 6: length 3, code 110

      • entry 7: length 3, code 111

      I'm having difficulty seeing how to generate those codes from the given lengths. As can be seen above, not all codes of the same length increase monotonically. All the codes of length 4 do because they follow one after the other, but entry 5 (with code 10, or 2 in decimal) does not monotonically follow entry 0 (with code 00).



      How would I go about generating these codes?









      share|cite|improve this question











      share|cite|improve this question




      share|cite|improve this question










      asked Aug 19 at 4:16









      Chris30

      11




      11

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "69"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2887345%2fgenerating-huffman-codes-from-unsorted-code-lengths%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2887345%2fgenerating-huffman-codes-from-unsorted-code-lengths%23new-answer', 'question_page');

          );

          Post as a guest













































































          這個網誌中的熱門文章

          How to combine Bézier curves to a surface?

          Mutual Information Always Non-negative

          Why am i infinitely getting the same tweet with the Twitter Search API?