home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1997 June / PC_Shareware-1997-06.iso / manga / mp2win95 / _setup.1 / HUFFMAN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-30  |  7.8 KB  |  307 lines

  1. /*
  2. Adopted from "huffman.c" from the ISO MPEG Audio Subgroup Software Simulation
  3. Group's public c source for its MPEG audio decoder. Optimizations and
  4. conversion of stdio routines to stream routines by Jeff Tsay
  5. (ctsay@pasteur.eecs.berkeley.edu). Also most error checks were removed.
  6.  
  7. Last modified : 01/28/97 */
  8.  
  9. /**********************************************************************
  10. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  11. huffman.c
  12. **********************************************************************/
  13. /**********************************************************************
  14.  * MPEG/audio coding/decoding software, work in progress              *
  15.  *   NOT for public distribution until verified and approved by the   *
  16.  *   MPEG/audio committee.  For further information, please contact   *
  17.  *   Davis Pan, 508-493-2241, e-mail: pan@gauss.enet.dec.com          *
  18.  *                                                                    *
  19.  * VERSION 2.10                                                       *
  20.  *   changes made since last update:                                  *
  21.  *   date   programmers                comment                        *
  22.  *27.2.92   F.O.Witte                  (ITT Intermetall)              *
  23.  *                       email: otto.witte@itt-sc.de    *
  24.  *                       tel:   ++49 (761)517-125          *
  25.  *                       fax:   ++49 (761)517-880          *
  26.  *12.6.92   J. Pineda                  Added sign bit to decoder.     *
  27.  * 08/24/93 M. Iwadare                 Changed for 1 pass decoding.   *
  28.  *--------------------------------------------------------------------*
  29.  *  7/14/94 Juergen Koller      Bug fixes in Layer III code           *
  30.  *********************************************************************/
  31.  
  32. #define STRICT
  33. #define WIN32_LEAN_AND_MEAN
  34. #define NOMCX
  35. #define NOIME
  36. #define NOGDI
  37. #define NOUSER
  38. #define NOSOUND
  39. #define NOCOMM
  40. #define NODRIVERS
  41. #define OEMRESOURCE
  42. #define NONLS
  43. #define NOSERVICE
  44. #define NOKANJI
  45. #define NOMINMAX
  46. #define NOLOGERROR
  47. #define NOPROFILER
  48. #define NOMEMMGR
  49. #define NOLFILEIO
  50. #define NOOPENFILE
  51. #define NORESOURCE
  52. #define NOATOM
  53. #define NOLANGUAGE
  54. // #define NOLSTRING
  55. #define NODBCS
  56. #define NOKEYBOARDINFO
  57. #define NOGDICAPMASKS
  58. #define NOCOLOR
  59. #define NOGDIOBJ
  60. #define NODRAWTEXT
  61. #define NOTEXTMETRIC
  62. #define NOSCALABLEFONT
  63. #define NOBITMAP
  64. #define NORASTEROPS
  65. #define NOMETAFILE
  66. #define NOSYSMETRICS
  67. #define NOSYSTEMPARAMSINFO
  68. #define NOMSG
  69. #define NOWINSTYLES
  70. #define NOWINOFFSETS
  71. #define NOSHOWWINDOW
  72. #define NODEFERWINDOWPOS
  73. #define NOVIRTUALKEYCODES
  74. #define NOKEYSTATES
  75. #define NOWH
  76. #define NOMENUS
  77. #define NOSCROLL
  78. #define NOCLIPBOARD
  79. #define NOICONS
  80. #define NOMB
  81. #define NOSYSCOMMANDS
  82. #define NOMDI
  83. #define NOCTLMGR
  84. #define NOWINMESSAGES
  85. #define NOHELP
  86. #define _WINUSER_
  87. #define __oleidl_h__
  88. #define _OLE2_H_
  89. #include <windows.h>
  90.  
  91. #include <fstream.h>
  92. #include <strstrea.h>
  93. #include "all.h"
  94. #include "bit_res.h"
  95. #include "huffman.h"
  96.  
  97. HUFFBITS dmask = 1 << (sizeof(HUFFBITS)*8-1);
  98. uint32 hs      = sizeof(HUFFBITS)*8;
  99.  
  100. struct huffcodetab ht[HTN];
  101.              /* array of all huffcodtable headers    */
  102.                 /* 0..31 Huffman code table 0..31    */
  103.                 /* 32,33 count1-tables            */
  104.  
  105. int32 hexstr_to_int(char *hexstr)
  106. {
  107.     int32 result = 0;
  108.     int32 shifts = 0;
  109.     int32 i;
  110.    char c;
  111.  
  112.     for (i = (lstrlen(hexstr) - 1); i>=0; i--) {
  113.  
  114.        if ((c = hexstr[i])  <= '9')
  115.             result+= ((int32) (c - '0')) << shifts;
  116.       else
  117.           result+= ((int32) (c - 'a') + 10) << shifts;
  118.  
  119.         shifts+=4;
  120.     }
  121.  
  122.     return (result);
  123. }
  124.  
  125. /* read the huffman decoder table */
  126.  
  127. int32 read_decoder_table(char *filename)
  128. {
  129.   int32 n,i, nn, t;
  130.   char v0str[4];
  131.   char v1str[4];
  132.   char command[100],line[100];
  133.  
  134.   ifstream fi((const char *) filename);
  135.   istrstream *line_stream = NULL;
  136.  
  137.   for (n=0;n<HTN;n++) {
  138.      // .table number treelen xlen ylen linbits
  139.      do {
  140.         fi.getline(line, 99, '\n');
  141.      } while ((line[0] == '#') || (line[0] < ' '));
  142.  
  143.      delete line_stream;
  144.      line_stream = new istrstream(line);
  145.  
  146.      *line_stream >> command >> ht[n].tablename >> ht[n].treelen
  147.                       >> ht[n].xlen >> ht[n].ylen >> ht[n].linbits;
  148.  
  149.      if (lstrcmp(command,".end")==0)
  150.         return(n);
  151.      else if (lstrcmp(command,".table")!=0) {
  152. //        fprintf(stderr,"huffman table %u data corrupted\n",n);
  153.         return -1;
  154.      }
  155.  
  156.      ht[n].linmax = (1<<ht[n].linbits)-1;
  157.  
  158. /*     sscanf(ht[n].tablename,"%u",&nn);
  159.      if (nn != n) {
  160.         fprintf(stderr,"wrong table number %u\n",n);
  161.         return(-2);
  162.      } */
  163.      delete line_stream;
  164.      line_stream = new istrstream(ht[n].tablename);
  165.  
  166.      *line_stream >> nn;
  167.  
  168.      do {
  169.         fi.getline(line, 99, '\n');
  170.      } while ((line[0] == '#') || (line[0] < ' '));
  171.  
  172.      delete line_stream;
  173.      line_stream = new istrstream(line);
  174.      *line_stream >> command >> t;
  175.  
  176.      if (lstrcmp(command,".reference")==0) {
  177.         ht[n].ref   = t;
  178.         ht[n].val   = ht[t].val;
  179.         ht[n].treelen  = ht[t].treelen;
  180. /*        if ( (ht[n].xlen != ht[t].xlen) ||
  181.               (ht[n].ylen != ht[t].ylen)  ) {
  182.           fprintf(stderr,"wrong table %u reference\n",n);
  183.           return (-3);
  184.         }; */
  185.  
  186.         while ((line[0] == '#') || (line[0] < ' ') ) {
  187.           fi.getline(line, 99, '\n');
  188.         }
  189.      }
  190.      else if (lstrcmp(command,".treedata")==0) {
  191.         ht[n].ref  = -1;
  192.         ht[n].val= new unsigned char[ht[n].treelen] [2];
  193.  
  194. /*        if (ht[n].val == NULL) {
  195.         fprintf(stderr, "heaperror at table %d\n",n);
  196.         exit (-10);
  197.         } */
  198.  
  199.         for (i=0;i<ht[n].treelen; i++) {
  200.           fi >> v0str >> v1str;
  201.           ht[n].val[i][0]=(unsigned char) hexstr_to_int(v0str);
  202.           ht[n].val[i][1]=(unsigned char) hexstr_to_int(v1str);
  203.         }
  204.  
  205.         fi.getline(line, 99, '\n');
  206.      }
  207. /*     else {
  208.         fprintf(stderr,"huffman decodertable error at table %d\n",n);
  209.      } */
  210.   }
  211.  
  212.   return n;
  213. }
  214.  
  215. /* do the huffman-decoding                         */
  216. /* note! for counta,countb -the 4 bit value is returned in y, discard x */
  217. int32 huffman_decoder(struct huffcodetab *h, int32 *x, int32 *y, int32 *v,
  218.                              int32 *w, Bit_Reserve *br)
  219. {
  220.   HUFFBITS level;
  221.   int point = 0;
  222.   int error = 1;
  223.   level     = dmask;
  224.  
  225.   if (h->val == NULL) return(2);
  226.  
  227.   /* table 0 needs no bits */
  228.   if ( h->treelen == 0)
  229.   {  *x = *y = 0;
  230.       return(0);
  231.   }
  232.  
  233.   /* Lookup in Huffman table. */
  234.  
  235.   do {
  236.      if (h->val[point][0]==0) {   /*end of tree*/
  237.         *x = h->val[point][1] >> 4;
  238.         *y = h->val[point][1] & 0xf;
  239.  
  240.         error = 0;
  241.         break;
  242.      }
  243.      if (br->hgetbits(1)) {
  244.         while (h->val[point][1] >= MXOFF) point += h->val[point][1];
  245.         point += h->val[point][1];
  246.      }
  247.      else {
  248.         while (h->val[point][0] >= MXOFF) point += h->val[point][0];
  249.         point += h->val[point][0];
  250.      }
  251.      level >>= 1;
  252.   } while (level  || (point < ht->treelen) );
  253.  
  254.   // Check for error.
  255.  
  256. /*  if (error) { // set x and y to a medium value as a simple concealment
  257.      printf("Illegal Huffman code in data.\n");
  258.      *x = (h->xlen-1 << 1);
  259.      *y = (h->ylen-1 << 1);
  260.   } */
  261.  
  262.   /* Process sign encodings for quadruples tables. */
  263.  
  264.   if (h->tablename[0] == '3'
  265.         && (h->tablename[1] == '2' || h->tablename[1] == '3')) {
  266.       *v = (*y>>3) & 1;
  267.       *w = (*y>>2) & 1;
  268.       *x = (*y>>1) & 1;
  269.       *y = *y & 1;
  270.  
  271.       /* v, w, x and y are reversed in the bitstream.
  272.           switch them around to make test bistream work. */
  273.  
  274.       if (*v)
  275.           if (br->hgetbits(1)) *v = -*v;
  276.       if (*w)
  277.           if (br->hgetbits(1)) *w = -*w;
  278.       if (*x)
  279.           if (br->hgetbits(1)) *x = -*x;
  280.       if (*y)
  281.           if (br->hgetbits(1)) *y = -*y;
  282.       }
  283.  
  284.   /* Process sign and escape encodings for dual tables. */
  285.  
  286.   else {
  287.  
  288.         /* x and y are reversed in the test bitstream.
  289.             Reverse x and y here to make test bitstream work. */
  290.  
  291.       if (h->linbits)
  292.          if ((h->xlen-1) == *x)
  293.             *x += br->hgetbits(h->linbits);
  294.       if (*x)
  295.           if (br->hgetbits(1)) *x = -*x;
  296.       if (h->linbits)
  297.          if ((h->ylen-1) == *y)
  298.             *y += br->hgetbits(h->linbits);
  299.       if (*y)
  300.           if (br->hgetbits(1)) *y = -*y;
  301.       }
  302.  
  303.   return(error);
  304. }
  305.  
  306.  
  307.