"Fossies" - the Fresh Open Source Software Archive

Member "faac-1_30/libfaac/huff2.c" (16 Oct 2019, 15194 Bytes) of package /linux/misc/faac-1_30.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "huff2.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.29.9_vs_1.29.9.2.

    1 /****************************************************************************
    2     Huffman coding
    3 
    4     Copyright (C) 2017 Krzysztof Nikiel
    5 
    6     This program is free software: you can redistribute it and/or modify
    7     it under the terms of the GNU General Public License as published by
    8     the Free Software Foundation, either version 3 of the License, or
    9     (at your option) any later version.
   10 
   11     This program is distributed in the hope that it will be useful,
   12     but WITHOUT ANY WARRANTY; without even the implied warranty of
   13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14     GNU General Public License for more details.
   15 
   16     You should have received a copy of the GNU General Public License
   17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18 ****************************************************************************/
   19 
   20 #include <stdio.h>
   21 #include <stdlib.h>
   22 #include "coder.h"
   23 #include "huffdata.h"
   24 #include "huff2.h"
   25 #include "bitstream.h"
   26 
   27 #ifdef DRM
   28 static int vcb11;
   29 #endif
   30 
   31 static int escape(int x, int *code)
   32 {
   33     int preflen = 0;
   34     int base = 32;
   35 
   36     if (x >= 8192)
   37     {
   38         fprintf(stderr, "%s(%d): x_quant >= 8192\n", __FILE__, __LINE__);
   39         return 0;
   40     }
   41 
   42     *code = 0;
   43     while (base <= x)
   44     {
   45         base <<= 1;
   46         *code <<= 1;
   47         *code |= 1;
   48         preflen++;
   49     }
   50     base >>= 1;
   51 
   52     // separator
   53     *code <<= 1;
   54 
   55     *code <<= (preflen + 4);
   56     *code |= (x - base);
   57 
   58     return (preflen << 1) + 5;
   59 }
   60 
   61 #ifdef DRM
   62 # define DRMDATA if(coder){coder->num_data_cw[coder->cur_cw++]=1;\
   63     coder->iLenReordSpData+=blen;if(coder->iLenLongestCW<blen)\
   64     coder->iLenLongestCW=blen;}
   65 #else
   66 # define DRMDATA
   67 #endif
   68 
   69 #define arrlen(array) (sizeof(array) / sizeof(*array))
   70 
   71 static int huffcode(int *qs /* quantized spectrum */,
   72                     int len,
   73                     int bnum,
   74                     CoderInfo *coder)
   75 {
   76     static hcode16_t * const hmap[12] = {0, book01, book02, book03, book04,
   77       book05, book06, book07, book08, book09, book10, book11};
   78     hcode16_t *book;
   79     int cnt;
   80     int bits = 0, blen;
   81     int ofs, *qp;
   82     int data;
   83     int idx;
   84     int datacnt;
   85 #ifdef DRM
   86     int drmbits = 0;
   87     int maxesc = 0;
   88 #endif
   89 
   90     if (coder)
   91         datacnt = coder->datacnt;
   92     else
   93         datacnt = 0;
   94 
   95     book = hmap[bnum];
   96     switch (bnum)
   97     {
   98 #ifdef DRM
   99     case HCB_ZERO:
  100     case HCB_INTENSITY:
  101     case HCB_INTENSITY2:
  102         for(ofs = 0; ofs < len; ofs += 4)
  103         {
  104             coder->s[datacnt].data = 0;
  105             coder->s[datacnt++].len = 0;
  106             coder->num_data_cw[coder->cur_cw++] = 1;
  107         }
  108         break;
  109 #endif
  110     case 1:
  111     case 2:
  112         for(ofs = 0; ofs < len; ofs += 4)
  113         {
  114             qp = qs+ofs;
  115             idx = 27 * qp[0] + 9 * qp[1] + 3 * qp[2] + qp[3] + 40;
  116             if (idx < 0 || idx >= arrlen(book01))
  117             {
  118                 return -1;
  119             }
  120             blen = book[idx].len;
  121             if (coder)
  122             {
  123                 data = book[idx].data;
  124                 coder->s[datacnt].data = data;
  125                 coder->s[datacnt++].len = blen;
  126                 DRMDATA;
  127             }
  128             bits += blen;
  129         }
  130         break;
  131     case 3:
  132     case 4:
  133         for(ofs = 0; ofs < len; ofs += 4)
  134         {
  135             qp = qs+ofs;
  136             idx = 27 * abs(qp[0]) + 9 * abs(qp[1]) + 3 * abs(qp[2]) + abs(qp[3]);
  137             if (idx < 0 || idx >= arrlen(book03))
  138             {
  139                 return -1;
  140             }
  141             blen = book[idx].len;
  142             if (!coder)
  143             {
  144                 // add sign bits
  145                 for(cnt = 0; cnt < 4; cnt++)
  146                     if(qp[cnt])
  147                         blen++;
  148             }
  149             else
  150             {
  151                 data = book[idx].data;
  152                 // add sign bits
  153                 for(cnt = 0; cnt < 4; cnt++)
  154                 {
  155                     if(qp[cnt])
  156                     {
  157                         blen++;
  158                         data <<= 1;
  159                         if (qp[cnt] < 0)
  160                             data |= 1;
  161                     }
  162                 }
  163                 coder->s[datacnt].data = data;
  164                 coder->s[datacnt++].len = blen;
  165                 DRMDATA;
  166             }
  167             bits += blen;
  168         }
  169         break;
  170     case 5:
  171     case 6:
  172         for(ofs = 0; ofs < len; ofs += 2)
  173         {
  174             qp = qs+ofs;
  175             idx = 9 * qp[0] + qp[1] + 40;
  176             if (idx < 0 || idx >= arrlen(book05))
  177             {
  178                 return -1;
  179             }
  180             blen = book[idx].len;
  181             if (coder)
  182             {
  183                 data = book[idx].data;
  184                 coder->s[datacnt].data = data;
  185                 coder->s[datacnt++].len = blen;
  186                 DRMDATA;
  187             }
  188             bits += blen;
  189         }
  190         break;
  191     case 7:
  192     case 8:
  193         for(ofs = 0; ofs < len; ofs += 2)
  194         {
  195             qp = qs+ofs;
  196             idx = 8 * abs(qp[0]) + abs(qp[1]);
  197             if (idx < 0 || idx >= arrlen(book07))
  198             {
  199                 return -1;
  200             }
  201             blen = book[idx].len;
  202             if (!coder)
  203             {
  204                 for(cnt = 0; cnt < 2; cnt++)
  205                     if(qp[cnt])
  206                         blen++;
  207             }
  208             else
  209             {
  210                 data = book[idx].data;
  211                 for(cnt = 0; cnt < 2; cnt++)
  212                 {
  213                     if(qp[cnt])
  214                     {
  215                         blen++;
  216                         data <<= 1;
  217                         if (qp[cnt] < 0)
  218                             data |= 1;
  219                     }
  220                 }
  221                 coder->s[datacnt].data = data;
  222                 coder->s[datacnt++].len = blen;
  223                 DRMDATA;
  224             }
  225             bits += blen;
  226         }
  227         break;
  228     case 9:
  229     case 10:
  230         for(ofs = 0; ofs < len; ofs += 2)
  231         {
  232             qp = qs+ofs;
  233             idx = 13 * abs(qp[0]) + abs(qp[1]);
  234             if (idx < 0 || idx >= arrlen(book09))
  235             {
  236                 return -1;
  237             }
  238             blen = book[idx].len;
  239             if (!coder)
  240             {
  241                 for(cnt = 0; cnt < 2; cnt++)
  242                     if(qp[cnt])
  243                         blen++;
  244             }
  245             else
  246             {
  247                 data = book[idx].data;
  248                 for(cnt = 0; cnt < 2; cnt++)
  249                 {
  250                     if(qp[cnt])
  251                     {
  252                         blen++;
  253                         data <<= 1;
  254                         if (qp[cnt] < 0)
  255                             data |= 1;
  256                     }
  257                 }
  258                 coder->s[datacnt].data = data;
  259                 coder->s[datacnt++].len = blen;
  260                 DRMDATA;
  261             }
  262             bits += blen;
  263         }
  264         break;
  265     case HCB_ESC:
  266         for(ofs = 0; ofs < len; ofs += 2)
  267         {
  268             int x0, x1;
  269 
  270             qp = qs+ofs;
  271 
  272             x0 = abs(qp[0]);
  273             x1 = abs(qp[1]);
  274             if (x0 > 16)
  275                 x0 = 16;
  276             if (x1 > 16)
  277                 x1 = 16;
  278             idx = 17 * x0 + x1;
  279             if (idx < 0 || idx >= arrlen(book11))
  280             {
  281                 return -1;
  282             }
  283 
  284             blen = book[idx].len;
  285             if (!coder)
  286             {
  287                 for(cnt = 0; cnt < 2; cnt++)
  288                     if(qp[cnt])
  289                         blen++;
  290             }
  291             else
  292             {
  293                 data = book[idx].data;
  294                 for(cnt = 0; cnt < 2; cnt++)
  295                 {
  296                     if(qp[cnt])
  297                     {
  298                         blen++;
  299                         data <<= 1;
  300                         if (qp[cnt] < 0)
  301                             data |= 1;
  302                     }
  303                 }
  304                 coder->s[datacnt].data = data;
  305                 coder->s[datacnt++].len = blen;
  306 #ifdef DRM
  307                 coder->num_data_cw[coder->cur_cw] = 1;
  308                 drmbits = blen;
  309 #endif
  310             }
  311             bits += blen;
  312 
  313             if (x0 >= 16)
  314             {
  315                 blen = escape(abs(qp[0]), &data);
  316                 if (coder)
  317                 {
  318                     coder->s[datacnt].data = data;
  319                     coder->s[datacnt++].len = blen;
  320 #ifdef DRM
  321                     coder->num_data_cw[coder->cur_cw]++;
  322                     drmbits += blen;
  323 
  324                     if (maxesc < data)
  325                         maxesc = data;
  326 #endif
  327                 }
  328                 bits += blen;
  329             }
  330 
  331             if (x1 >= 16)
  332             {
  333                 blen = escape(abs(qp[1]), &data);
  334                 if (coder)
  335                 {
  336                     coder->s[datacnt].data = data;
  337                     coder->s[datacnt++].len = blen;
  338 #ifdef DRM
  339                     coder->num_data_cw[coder->cur_cw]++;
  340                     drmbits += blen;
  341 
  342                     if (maxesc < data)
  343                         maxesc = data;
  344 #endif
  345                 }
  346                 bits += blen;
  347             }
  348 #ifdef DRM
  349             coder->iLenReordSpData += drmbits;
  350             if (coder->iLenLongestCW < drmbits)
  351                 coder->iLenLongestCW = drmbits;
  352 
  353             coder->cur_cw++;
  354 #endif
  355         }
  356 #ifdef DRM
  357         /* VCB11: check which codebook should be used using max escape sequence */
  358         /* 8.5.3.1.3, table 157 */
  359         if (maxesc <= 15)
  360             vcb11 = 16;
  361         else if (maxesc <= 31)
  362             vcb11 = 17;
  363         else if (maxesc <= 47)
  364             vcb11 = 18;
  365         else if (maxesc <= 63)
  366             vcb11 = 19;
  367         else if (maxesc <= 95)
  368             vcb11 = 20;
  369         else if (maxesc <= 127)
  370             vcb11 = 21;
  371         else if (maxesc <= 159)
  372             vcb11 = 22;
  373         else if (maxesc <= 191)
  374             vcb11 = 23;
  375         else if (maxesc <= 223)
  376             vcb11 = 24;
  377         else if (maxesc <= 255)
  378             vcb11 = 25;
  379         else if (maxesc <= 319)
  380             vcb11 = 26;
  381         else if (maxesc <= 383)
  382             vcb11 = 27;
  383         else if (maxesc <= 511)
  384             vcb11 = 28;
  385         else if (maxesc <= 767)
  386             vcb11 = 29;
  387         else if (maxesc <= 1023)
  388             vcb11 = 30;
  389         else if (maxesc <= 2047)
  390             vcb11 = 31;
  391         /* else: codebook 11 -> it is already 11 */
  392 #endif
  393         break;
  394     default:
  395         fprintf(stderr, "%s(%d) book %d out of range\n", __FILE__, __LINE__, bnum);
  396         return -1;
  397     }
  398 
  399     if (coder)
  400         coder->datacnt = datacnt;
  401 
  402     return bits;
  403 }
  404 
  405 
  406 int huffbook(CoderInfo *coder,
  407              int *qs /* quantized spectrum */,
  408              int len)
  409 {
  410     int cnt;
  411     int maxq = 0;
  412     int bookmin, lenmin;
  413 
  414     for (cnt = 0; cnt < len; cnt++)
  415     {
  416         int q = abs(qs[cnt]);
  417         if (maxq < q)
  418             maxq = q;
  419     }
  420 
  421 #define BOOKMIN(n)bookmin=n;lenmin=huffcode(qs,len,bookmin,0);if(huffcode(qs,len,bookmin+1,0)<lenmin)bookmin++;
  422 
  423     if (maxq < 1)
  424     {
  425         bookmin = HCB_ZERO;
  426         lenmin = 0;
  427     }
  428     else if (maxq < 2)
  429     {
  430         BOOKMIN(1);
  431     }
  432     else if (maxq < 3)
  433     {
  434         BOOKMIN(3);
  435     }
  436     else if (maxq < 5)
  437     {
  438         BOOKMIN(5);
  439     }
  440     else if (maxq < 8)
  441     {
  442         BOOKMIN(7);
  443     }
  444     else if (maxq < 13)
  445     {
  446         BOOKMIN(9);
  447     }
  448     else
  449     {
  450         bookmin = HCB_ESC;
  451     }
  452 
  453 #ifdef DRM
  454     vcb11 = 0;
  455     huffcode(qs, len, bookmin, coder);
  456     if (vcb11)
  457         bookmin = vcb11;
  458 #else
  459     if (bookmin > HCB_ZERO)
  460         huffcode(qs, len, bookmin, coder);
  461 #endif
  462     coder->book[coder->bandcnt] = bookmin;
  463 
  464     return 0;
  465 }
  466 
  467 int writebooks(CoderInfo *coder, BitStream *stream, int write)
  468 {
  469     int bits = 0;
  470     int maxcnt, cntbits;
  471     int group;
  472     int bookbits = 4;
  473 
  474 #ifdef DRM
  475     bookbits = 5; /* 5 bits in case of VCB11 */
  476 #endif
  477 
  478     if (coder->block_type == ONLY_SHORT_WINDOW){
  479         maxcnt = 7;
  480         cntbits = 3;
  481     } else {
  482         maxcnt = 31;
  483         cntbits = 5;
  484     }
  485 
  486     for (group = 0; group < coder->groups.n; group++)
  487     {
  488         int band = group * coder->sfbn;
  489         int maxband = band + coder->sfbn;
  490 
  491         while (band < maxband)
  492         {
  493             int book = coder->book[band++];
  494             int bookcnt = 1;
  495             if (write) {
  496                 PutBit(stream, book, bookbits);
  497             }
  498             bits += bookbits;
  499 
  500 #ifdef DRM
  501             /* sect_len is not transmitted in case the codebook for a */
  502             /* section is 11 or in the range of 16 and 31 */
  503             if ((book == 11) || ((book >= 16) && (book <= 32)))
  504                 continue;
  505 #endif
  506 
  507             if (band < maxband)
  508             {
  509                 while (book == coder->book[band])
  510                 {
  511                     band++;
  512                     bookcnt++;
  513                     if (band >= maxband)
  514                         break;
  515                 }
  516             }
  517 
  518             while (bookcnt >= maxcnt)
  519             {
  520                 if (write)
  521                     PutBit(stream, maxcnt, cntbits);
  522                 bits += cntbits;
  523                 bookcnt -= maxcnt;
  524             }
  525             if (write)
  526                 PutBit(stream, bookcnt, cntbits);
  527             bits += cntbits;
  528         }
  529     }
  530 
  531     return bits;
  532 }
  533 
  534 int writesf(CoderInfo *coder, BitStream *stream, int write)
  535 {
  536     int cnt;
  537     int bits = 0;
  538     int diff, length;
  539     int lastsf;
  540     int lastis;
  541     int lastpns;
  542     int initpns = 1;
  543 
  544     lastsf = coder->global_gain;
  545     lastis = 0;
  546     lastpns = coder->global_gain - 90;
  547 
  548     // fixme: move range check to quantizer
  549     for (cnt = 0; cnt < coder->bandcnt; cnt++)
  550     {
  551         int book = coder->book[cnt];
  552 
  553         if ((book == HCB_INTENSITY) || (book== HCB_INTENSITY2))
  554         {
  555             diff = coder->sf[cnt] - lastis;
  556             if (diff > 60)
  557                 diff = 60;
  558             if (diff < -60)
  559                 diff = -60;
  560             length = book12[60 + diff].len;
  561 
  562             bits += length;
  563 
  564             lastis += diff;
  565 
  566             if (write)
  567                 PutBit(stream, book12[60 + diff].data, length);
  568         }
  569         else if (book == HCB_PNS)
  570         {
  571             diff = coder->sf[cnt] - lastpns;
  572 
  573             if (initpns)
  574             {
  575                 initpns = 0;
  576 
  577                 length = 9;
  578                 bits += length;
  579                 lastpns += diff;
  580 
  581                 if (write)
  582                     PutBit(stream, diff + 256, length);
  583                 continue;
  584             }
  585 
  586             if (diff > 60)
  587                 diff = 60;
  588             if (diff < -60)
  589                 diff = -60;
  590 
  591             length = book12[60 + diff].len;
  592             bits += length;
  593             lastpns += diff;
  594 
  595             if (write)
  596                 PutBit(stream, book12[60 + diff].data, length);
  597         }
  598         else if (book)
  599         {
  600             diff = coder->sf[cnt] - lastsf;
  601             if (diff > 60)
  602                 diff = 60;
  603             if (diff < -60)
  604                 diff = -60;
  605             length = book12[60 + diff].len;
  606 
  607             bits += length;
  608             lastsf += diff;
  609 
  610             if (write)
  611                 PutBit(stream, book12[60 + diff].data, length);
  612         }
  613 
  614     }
  615     return bits;
  616 }