home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / mpeg_stat-2.2 / decoders.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-10  |  32.5 KB  |  977 lines

  1. /* MPEGSTAT - analyzing tool for MPEG-I video streams
  2.  * 
  3.  *
  4.  *  Copyright (c) 1995 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * Technical University of Berlin, Germany, Dept. of Computer Science
  8.  * Tom Pfeifer - Multimedia systems project - pfeifer@fokus.gmd.de
  9.  *
  10.  * Jens Brettin, Harald Masche, Alexander Schulze, Dirk Schubert
  11.  *
  12.  * This program uses parts of the source code of the Berkeley MPEG player
  13.  *
  14.  * ---------------------------
  15.  *
  16.  * Copyright (c) 1993 Technical University of Berlin, Germany
  17.  *
  18.  * for the parts of the Berkeley player used:
  19.  *
  20.  * Copyright (c) 1992 The Regents of the University of California.
  21.  * All rights reserved.
  22.  *
  23.  * ---------------------------
  24.  *
  25.  * Permission to use, copy, modify, and distribute this software and its
  26.  * documentation for any purpose, without fee, and without written agreement is
  27.  * hereby granted, provided that the above copyright notices and the following
  28.  * two paragraphs appear in all copies of this software.
  29.  * 
  30.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA 
  31.  * or the Technical University of Berlin BE LIABLE TO ANY PARTY FOR
  32.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  33.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  34.  * CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
  35.  * POSSIBILITY OF SUCH DAMAGE.
  36.  * 
  37.  * THE UNIVERSITY OF CALIFORNIA and the Technical University of Berlin 
  38.  * SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  39.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40.  * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
  41.  * UNIVERSITY OF CALIFORNIA and the Technical University of Berlin HAVE NO 
  42.  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
  43.  * OR MODIFICATIONS.
  44.  */
  45. /*
  46.  * decoders.c
  47.  *
  48.  * This file contains all the routines for Huffman decoding required in 
  49.  * MPEG
  50.  *
  51.  */
  52.  
  53. #include <stdio.h>
  54. #include <assert.h>
  55. #include "decoders.h"
  56. #include "util.h" 
  57. #include "video.h"
  58. #include "proto.h"
  59. #include "dither.h"
  60.  
  61.  
  62. /* Error handling code */
  63. extern char *errorLocation, *errorSpecifics;
  64.  
  65. #define myassert(expression)\
  66.   if (!(expression)) {\
  67.             sprintf (errorSpecifics,"\nBad DCT-decode value (%d) at line %d\n", expression, __LINE__);\
  68.               return SKIP_TO_START_CODE;}
  69.  
  70. /* Decoding table for macroblock_address_increment */
  71. mb_addr_inc_entry     mb_addr_inc[2048];
  72.  
  73. /* Decoding table for macroblock_type in predictive-coded pictures */
  74. mb_type_entry         mb_type_P[64];
  75.  
  76. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  77. mb_type_entry         mb_type_B[64];
  78.  
  79. /* Decoding table for motion vectors */
  80. motion_vectors_entry  motion_vectors[2048];
  81.  
  82. /* Decoding table for coded_block_pattern */
  83.  
  84. coded_block_pattern_entry coded_block_pattern[512] = 
  85. { {(unsigned int)ERROR, 0}, {(unsigned int)ERROR, 0}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
  86.   {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
  87.   {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
  88.   {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
  89.   {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
  90.   {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
  91.   {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
  92.   {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
  93.   {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
  94.   {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7}, 
  95.   {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, 
  96.   {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7}, 
  97.   {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, 
  98.   {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, 
  99.   {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, 
  100.   {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, 
  101.   {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
  102.   {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
  103.   {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
  104.   {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
  105.   {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
  106.   {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
  107.   {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
  108.   {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
  109.   {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
  110.   {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
  111.   {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
  112.   {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
  113.   {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
  114.   {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
  115.   {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
  116.   {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
  117.   {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
  118.   {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
  119.   {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
  120.   {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
  121.   {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
  122.   {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
  123.   {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
  124.   {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
  125.   {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  126.   {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  127.   {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  128.   {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  129.   {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  130.   {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  131.   {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  132.   {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  133.   {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  134.   {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  135.   {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  136.   {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
  137.   {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
  138.   {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  139.   {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  140.   {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
  141.   {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  142.   {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  143.   {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  144.   {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  145.   {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  146.   {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  147.   {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  148.   {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}
  149. };
  150.  
  151. /* Decoding table for dct_dc_size_luminance */
  152. dct_dc_size_entry dct_dc_size_luminance[128] =
  153. {   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  154.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  155.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  156.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  157.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  158.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  159.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  160.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  161.     {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, 
  162.     {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, 
  163.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  164.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  165.     {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, 
  166.     {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, 
  167.     {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, 
  168.     {6, 5}, {6, 5}, {6, 5}, {6, 5}, {7, 6}, {7, 6}, {8, 7}, {(unsigned int)ERROR, 0}
  169.   };
  170.  
  171. /* Decoding table for dct_dc_size_chrominance */
  172. dct_dc_size_entry dct_dc_size_chrominance[256] =
  173. { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  174.   {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  175.   {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  176.   {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  177.   {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  178.   {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  179.   {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  180.   {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  181.   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  182.   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  183.   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  184.   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  185.   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  186.   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  187.   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  188.   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  189.   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  190.   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  191.   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  192.   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  193.   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  194.   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  195.   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  196.   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  197.   {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  198.   {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  199.   {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  200.   {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  201.   {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  202.   {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  203.   {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, 
  204.   {6, 6}, {6, 6}, {6, 6}, {6, 6}, {7, 7}, {7, 7}, {8, 8}, {(unsigned int)ERROR, 0}
  205. };
  206.  
  207. /* DCT coeff tables. */
  208.  
  209. unsigned short int dct_coeff_tbl_0[256] =
  210. {
  211.   0xffff, 0xffff, 0xffff, 0xffff, 
  212.   0xffff, 0xffff, 0xffff, 0xffff, 
  213.   0xffff, 0xffff, 0xffff, 0xffff, 
  214.   0xffff, 0xffff, 0xffff, 0xffff, 
  215.   0x052f, 0x051f, 0x050f, 0x04ff, 
  216.   0x183f, 0x402f, 0x3c2f, 0x382f, 
  217.   0x342f, 0x302f, 0x2c2f, 0x7c1f, 
  218.   0x781f, 0x741f, 0x701f, 0x6c1f, 
  219.   0x028e, 0x028e, 0x027e, 0x027e, 
  220.   0x026e, 0x026e, 0x025e, 0x025e, 
  221.   0x024e, 0x024e, 0x023e, 0x023e, 
  222.   0x022e, 0x022e, 0x021e, 0x021e, 
  223.   0x020e, 0x020e, 0x04ee, 0x04ee, 
  224.   0x04de, 0x04de, 0x04ce, 0x04ce, 
  225.   0x04be, 0x04be, 0x04ae, 0x04ae, 
  226.   0x049e, 0x049e, 0x048e, 0x048e, 
  227.   0x01fd, 0x01fd, 0x01fd, 0x01fd, 
  228.   0x01ed, 0x01ed, 0x01ed, 0x01ed, 
  229.   0x01dd, 0x01dd, 0x01dd, 0x01dd, 
  230.   0x01cd, 0x01cd, 0x01cd, 0x01cd, 
  231.   0x01bd, 0x01bd, 0x01bd, 0x01bd, 
  232.   0x01ad, 0x01ad, 0x01ad, 0x01ad, 
  233.   0x019d, 0x019d, 0x019d, 0x019d, 
  234.   0x018d, 0x018d, 0x018d, 0x018d, 
  235.   0x017d, 0x017d, 0x017d, 0x017d, 
  236.   0x016d, 0x016d, 0x016d, 0x016d, 
  237.   0x015d, 0x015d, 0x015d, 0x015d, 
  238.   0x014d, 0x014d, 0x014d, 0x014d, 
  239.   0x013d, 0x013d, 0x013d, 0x013d, 
  240.   0x012d, 0x012d, 0x012d, 0x012d, 
  241.   0x011d, 0x011d, 0x011d, 0x011d, 
  242.   0x010d, 0x010d, 0x010d, 0x010d, 
  243.   0x282c, 0x282c, 0x282c, 0x282c, 
  244.   0x282c, 0x282c, 0x282c, 0x282c, 
  245.   0x242c, 0x242c, 0x242c, 0x242c, 
  246.   0x242c, 0x242c, 0x242c, 0x242c, 
  247.   0x143c, 0x143c, 0x143c, 0x143c, 
  248.   0x143c, 0x143c, 0x143c, 0x143c, 
  249.   0x0c4c, 0x0c4c, 0x0c4c, 0x0c4c, 
  250.   0x0c4c, 0x0c4c, 0x0c4c, 0x0c4c, 
  251.   0x085c, 0x085c, 0x085c, 0x085c, 
  252.   0x085c, 0x085c, 0x085c, 0x085c, 
  253.   0x047c, 0x047c, 0x047c, 0x047c, 
  254.   0x047c, 0x047c, 0x047c, 0x047c, 
  255.   0x046c, 0x046c, 0x046c, 0x046c, 
  256.   0x046c, 0x046c, 0x046c, 0x046c, 
  257.   0x00fc, 0x00fc, 0x00fc, 0x00fc, 
  258.   0x00fc, 0x00fc, 0x00fc, 0x00fc, 
  259.   0x00ec, 0x00ec, 0x00ec, 0x00ec, 
  260.   0x00ec, 0x00ec, 0x00ec, 0x00ec, 
  261.   0x00dc, 0x00dc, 0x00dc, 0x00dc, 
  262.   0x00dc, 0x00dc, 0x00dc, 0x00dc, 
  263.   0x00cc, 0x00cc, 0x00cc, 0x00cc, 
  264.   0x00cc, 0x00cc, 0x00cc, 0x00cc, 
  265.   0x681c, 0x681c, 0x681c, 0x681c, 
  266.   0x681c, 0x681c, 0x681c, 0x681c, 
  267.   0x641c, 0x641c, 0x641c, 0x641c, 
  268.   0x641c, 0x641c, 0x641c, 0x641c, 
  269.   0x601c, 0x601c, 0x601c, 0x601c, 
  270.   0x601c, 0x601c, 0x601c, 0x601c, 
  271.   0x5c1c, 0x5c1c, 0x5c1c, 0x5c1c, 
  272.   0x5c1c, 0x5c1c, 0x5c1c, 0x5c1c, 
  273.   0x581c, 0x581c, 0x581c, 0x581c, 
  274.   0x581c, 0x581c, 0x581c, 0x581c, 
  275. };
  276.  
  277. unsigned short int dct_coeff_tbl_1[16] = 
  278. {
  279.   0x00bb, 0x202b, 0x103b, 0x00ab, 
  280.   0x084b, 0x1c2b, 0x541b, 0x501b, 
  281.   0x009b, 0x4c1b, 0x481b, 0x045b, 
  282.   0x0c3b, 0x008b, 0x182b, 0x441b, 
  283. };
  284.  
  285. unsigned short int dct_coeff_tbl_2[4] =
  286. {
  287.   0x4019, 0x1429, 0x0079, 0x0839, 
  288. };
  289.  
  290. unsigned short int dct_coeff_tbl_3[4] = 
  291. {
  292.   0x0449, 0x3c19, 0x3819, 0x1029, 
  293. };
  294.  
  295. unsigned short int dct_coeff_next[256] = 
  296. {
  297.   0xffff, 0xffff, 0xffff, 0xffff, 
  298.   0xf7d5, 0xf7d5, 0xf7d5, 0xf7d5, 
  299.   0x0826, 0x0826, 0x2416, 0x2416, 
  300.   0x0046, 0x0046, 0x2016, 0x2016, 
  301.   0x1c15, 0x1c15, 0x1c15, 0x1c15, 
  302.   0x1815, 0x1815, 0x1815, 0x1815, 
  303.   0x0425, 0x0425, 0x0425, 0x0425, 
  304.   0x1415, 0x1415, 0x1415, 0x1415, 
  305.   0x3417, 0x0067, 0x3017, 0x2c17, 
  306.   0x0c27, 0x0437, 0x0057, 0x2817, 
  307.   0x0034, 0x0034, 0x0034, 0x0034, 
  308.   0x0034, 0x0034, 0x0034, 0x0034, 
  309.   0x1014, 0x1014, 0x1014, 0x1014, 
  310.   0x1014, 0x1014, 0x1014, 0x1014, 
  311.   0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  312.   0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  313.   0x0023, 0x0023, 0x0023, 0x0023, 
  314.   0x0023, 0x0023, 0x0023, 0x0023, 
  315.   0x0023, 0x0023, 0x0023, 0x0023, 
  316.   0x0023, 0x0023, 0x0023, 0x0023, 
  317.   0x0813, 0x0813, 0x0813, 0x0813, 
  318.   0x0813, 0x0813, 0x0813, 0x0813, 
  319.   0x0813, 0x0813, 0x0813, 0x0813, 
  320.   0x0813, 0x0813, 0x0813, 0x0813, 
  321.   0x0412, 0x0412, 0x0412, 0x0412, 
  322.   0x0412, 0x0412, 0x0412, 0x0412, 
  323.   0x0412, 0x0412, 0x0412, 0x0412, 
  324.   0x0412, 0x0412, 0x0412, 0x0412, 
  325.   0x0412, 0x0412, 0x0412, 0x0412, 
  326.   0x0412, 0x0412, 0x0412, 0x0412, 
  327.   0x0412, 0x0412, 0x0412, 0x0412, 
  328.   0x0412, 0x0412, 0x0412, 0x0412, 
  329.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  330.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  331.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  332.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  333.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  334.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  335.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  336.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  337.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  338.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  339.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  340.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  341.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  342.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  343.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  344.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  345.   0x0011, 0x0011, 0x0011, 0x0011, 
  346.   0x0011, 0x0011, 0x0011, 0x0011, 
  347.   0x0011, 0x0011, 0x0011, 0x0011, 
  348.   0x0011, 0x0011, 0x0011, 0x0011, 
  349.   0x0011, 0x0011, 0x0011, 0x0011, 
  350.   0x0011, 0x0011, 0x0011, 0x0011, 
  351.   0x0011, 0x0011, 0x0011, 0x0011, 
  352.   0x0011, 0x0011, 0x0011, 0x0011, 
  353.   0x0011, 0x0011, 0x0011, 0x0011, 
  354.   0x0011, 0x0011, 0x0011, 0x0011, 
  355.   0x0011, 0x0011, 0x0011, 0x0011, 
  356.   0x0011, 0x0011, 0x0011, 0x0011, 
  357.   0x0011, 0x0011, 0x0011, 0x0011, 
  358.   0x0011, 0x0011, 0x0011, 0x0011, 
  359.   0x0011, 0x0011, 0x0011, 0x0011, 
  360.   0x0011, 0x0011, 0x0011, 0x0011, 
  361. };
  362.  
  363. unsigned short int dct_coeff_first[256] = 
  364. {
  365.   0xffff, 0xffff, 0xffff, 0xffff, 
  366.   0xf7d5, 0xf7d5, 0xf7d5, 0xf7d5, 
  367.   0x0826, 0x0826, 0x2416, 0x2416, 
  368.   0x0046, 0x0046, 0x2016, 0x2016, 
  369.   0x1c15, 0x1c15, 0x1c15, 0x1c15, 
  370.   0x1815, 0x1815, 0x1815, 0x1815, 
  371.   0x0425, 0x0425, 0x0425, 0x0425, 
  372.   0x1415, 0x1415, 0x1415, 0x1415, 
  373.   0x3417, 0x0067, 0x3017, 0x2c17, 
  374.   0x0c27, 0x0437, 0x0057, 0x2817, 
  375.   0x0034, 0x0034, 0x0034, 0x0034, 
  376.   0x0034, 0x0034, 0x0034, 0x0034, 
  377.   0x1014, 0x1014, 0x1014, 0x1014, 
  378.   0x1014, 0x1014, 0x1014, 0x1014, 
  379.   0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  380.   0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  381.   0x0023, 0x0023, 0x0023, 0x0023, 
  382.   0x0023, 0x0023, 0x0023, 0x0023, 
  383.   0x0023, 0x0023, 0x0023, 0x0023, 
  384.   0x0023, 0x0023, 0x0023, 0x0023, 
  385.   0x0813, 0x0813, 0x0813, 0x0813, 
  386.   0x0813, 0x0813, 0x0813, 0x0813, 
  387.   0x0813, 0x0813, 0x0813, 0x0813, 
  388.   0x0813, 0x0813, 0x0813, 0x0813, 
  389.   0x0412, 0x0412, 0x0412, 0x0412, 
  390.   0x0412, 0x0412, 0x0412, 0x0412, 
  391.   0x0412, 0x0412, 0x0412, 0x0412, 
  392.   0x0412, 0x0412, 0x0412, 0x0412, 
  393.   0x0412, 0x0412, 0x0412, 0x0412, 
  394.   0x0412, 0x0412, 0x0412, 0x0412, 
  395.   0x0412, 0x0412, 0x0412, 0x0412, 
  396.   0x0412, 0x0412, 0x0412, 0x0412, 
  397.   0x0010, 0x0010, 0x0010, 0x0010, 
  398.   0x0010, 0x0010, 0x0010, 0x0010, 
  399.   0x0010, 0x0010, 0x0010, 0x0010, 
  400.   0x0010, 0x0010, 0x0010, 0x0010, 
  401.   0x0010, 0x0010, 0x0010, 0x0010, 
  402.   0x0010, 0x0010, 0x0010, 0x0010, 
  403.   0x0010, 0x0010, 0x0010, 0x0010, 
  404.   0x0010, 0x0010, 0x0010, 0x0010, 
  405.   0x0010, 0x0010, 0x0010, 0x0010, 
  406.   0x0010, 0x0010, 0x0010, 0x0010, 
  407.   0x0010, 0x0010, 0x0010, 0x0010, 
  408.   0x0010, 0x0010, 0x0010, 0x0010, 
  409.   0x0010, 0x0010, 0x0010, 0x0010, 
  410.   0x0010, 0x0010, 0x0010, 0x0010, 
  411.   0x0010, 0x0010, 0x0010, 0x0010, 
  412.   0x0010, 0x0010, 0x0010, 0x0010, 
  413.   0x0010, 0x0010, 0x0010, 0x0010, 
  414.   0x0010, 0x0010, 0x0010, 0x0010, 
  415.   0x0010, 0x0010, 0x0010, 0x0010, 
  416.   0x0010, 0x0010, 0x0010, 0x0010, 
  417.   0x0010, 0x0010, 0x0010, 0x0010, 
  418.   0x0010, 0x0010, 0x0010, 0x0010, 
  419.   0x0010, 0x0010, 0x0010, 0x0010, 
  420.   0x0010, 0x0010, 0x0010, 0x0010, 
  421.   0x0010, 0x0010, 0x0010, 0x0010, 
  422.   0x0010, 0x0010, 0x0010, 0x0010, 
  423.   0x0010, 0x0010, 0x0010, 0x0010, 
  424.   0x0010, 0x0010, 0x0010, 0x0010, 
  425.   0x0010, 0x0010, 0x0010, 0x0010, 
  426.   0x0010, 0x0010, 0x0010, 0x0010, 
  427.   0x0010, 0x0010, 0x0010, 0x0010, 
  428.   0x0010, 0x0010, 0x0010, 0x0010, 
  429. };
  430.  
  431. /* Macro for filling up the decoding table for mb_addr_inc */
  432. #define ASSIGN1(start, end, step, val, num) \
  433.   for (i = start; i < end; i+= step) { \
  434.                      for (j = 0; j < step; j++) { \
  435.                                     mb_addr_inc[i+j].value = val; \
  436.                                       mb_addr_inc[i+j].num_bits = num; \
  437.                                       } \
  438.                                         val--; \
  439.                                         }
  440.  
  441.  
  442.  
  443. /*
  444.  *--------------------------------------------------------------
  445.  *
  446.  * init_mb_addr_inc --
  447.  *
  448.  *    Initialize the VLC decoding table for macro_block_address_increment
  449.  *
  450.  * Results:
  451.  *    The decoding table for macro_block_address_increment will
  452.  *      be filled; illegal values will be filled as ERROR.
  453.  *
  454.  * Side effects:
  455.  *    The global array mb_addr_inc will be filled.
  456.  *
  457.  *--------------------------------------------------------------
  458.  */
  459. static void
  460.   init_mb_addr_inc()
  461. {
  462.   int i, j, val;
  463.  
  464.   for (i = 0; i < 8; i++) {
  465.     mb_addr_inc[i].value = ERROR;
  466.     mb_addr_inc[i].num_bits = 0;
  467.   }
  468.  
  469.   mb_addr_inc[8].value = MACRO_BLOCK_ESCAPE;
  470.   mb_addr_inc[8].num_bits = 11;
  471.  
  472.   for (i = 9; i < 15; i++) {
  473.     mb_addr_inc[i].value = ERROR;
  474.     mb_addr_inc[i].num_bits = 0;
  475.   }
  476.  
  477.   mb_addr_inc[15].value = MACRO_BLOCK_STUFFING;
  478.   mb_addr_inc[15].num_bits = 11;
  479.  
  480.   for (i = 16; i < 24; i++) {
  481.     mb_addr_inc[i].value = ERROR;
  482.     mb_addr_inc[i].num_bits = 0;
  483.   }
  484.  
  485.   val = 33;
  486.  
  487.   ASSIGN1(24, 36, 1, val, 11);
  488.   ASSIGN1(36, 48, 2, val, 10);
  489.   ASSIGN1(48, 96, 8, val, 8);
  490.   ASSIGN1(96, 128, 16, val, 7);
  491.   ASSIGN1(128, 256, 64, val, 5);
  492.   ASSIGN1(256, 512, 128, val, 4);
  493.   ASSIGN1(512, 1024, 256, val, 3);
  494.   ASSIGN1(1024, 2048, 1024, val, 1);
  495. }
  496.  
  497.  
  498. /* Macro for filling up the decoding table for mb_type */
  499. #define ASSIGN2(start, end, quant, motion_forward, motion_backward, pattern, intra, num, mb_type) \
  500.   for (i = start; i < end; i ++) { \
  501.       mb_type[i].mb_quant = quant; \
  502.       mb_type[i].mb_motion_forward = motion_forward; \
  503.       mb_type[i].mb_motion_backward = motion_backward; \
  504.       mb_type[i].mb_pattern = pattern; \
  505.       mb_type[i].mb_intra = intra; \
  506.       mb_type[i].num_bits = num; \
  507.       }
  508.  
  509.  
  510.  
  511. /*
  512.  *--------------------------------------------------------------
  513.  *
  514.  * init_mb_type_P --
  515.  *
  516.  *    Initialize the VLC decoding table for macro_block_type in
  517.  *      predictive-coded pictures.
  518.  *
  519.  * Results:
  520.  *    The decoding table for macro_block_type in predictive-coded
  521.  *      pictures will be filled; illegal values will be filled as ERROR.
  522.  *
  523.  * Side effects:
  524.  *    The global array mb_type_P will be filled.
  525.  *
  526.  *--------------------------------------------------------------
  527.  */
  528. static void
  529.   init_mb_type_P()
  530. {
  531.   int i;
  532.  
  533.   mb_type_P[0].mb_quant = mb_type_P[0].mb_motion_forward 
  534.     = mb_type_P[0].mb_motion_backward = mb_type_P[0].mb_pattern 
  535.       = mb_type_P[0].mb_intra = ERROR;
  536.   mb_type_P[0].num_bits = 0;
  537.  
  538.   ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_P)
  539.     ASSIGN2(2, 4, 1, 0, 0, 1, 0, 5, mb_type_P)
  540.       ASSIGN2(4, 6, 1, 1, 0, 1, 0, 5, mb_type_P);
  541.   ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_P);
  542.   ASSIGN2(8, 16, 0, 1, 0, 0, 0, 3, mb_type_P);
  543.   ASSIGN2(16, 32, 0, 0, 0, 1, 0, 2, mb_type_P);
  544.   ASSIGN2(32, 64, 0, 1, 0, 1, 0, 1, mb_type_P);
  545. }
  546.  
  547.  
  548.  
  549.  
  550. /*
  551.  *--------------------------------------------------------------
  552.  *
  553.  * init_mb_type_B --
  554.  *
  555.  *    Initialize the VLC decoding table for macro_block_type in
  556.  *      bidirectionally-coded pictures.
  557.  *
  558.  * Results:
  559.  *    The decoding table for macro_block_type in bidirectionally-coded
  560.  *      pictures will be filled; illegal values will be filled as ERROR.
  561.  *
  562.  * Side effects:
  563.  *    The global array mb_type_B will be filled.
  564.  *
  565.  *--------------------------------------------------------------
  566.  */
  567. static void
  568.   init_mb_type_B()
  569. {
  570.   int i;
  571.  
  572.   mb_type_B[0].mb_quant = mb_type_B[0].mb_motion_forward 
  573.     = mb_type_B[0].mb_motion_backward = mb_type_B[0].mb_pattern 
  574.       = mb_type_B[0].mb_intra = ERROR;
  575.   mb_type_B[0].num_bits = 0;
  576.  
  577.   ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_B);
  578.   ASSIGN2(2, 3, 1, 0, 1, 1, 0, 6, mb_type_B);
  579.   ASSIGN2(3, 4, 1, 1, 0, 1, 0, 6, mb_type_B);
  580.   ASSIGN2(4, 6, 1, 1, 1, 1, 0, 5, mb_type_B);
  581.   ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_B);
  582.   ASSIGN2(8, 12, 0, 1, 0, 0, 0, 4, mb_type_B);
  583.   ASSIGN2(12, 16, 0, 1, 0, 1, 0, 4, mb_type_B);
  584.   ASSIGN2(16, 24, 0, 0, 1, 0, 0, 3, mb_type_B);
  585.   ASSIGN2(24, 32, 0, 0, 1, 1, 0, 3, mb_type_B);
  586.   ASSIGN2(32, 48, 0, 1, 1, 0, 0, 2, mb_type_B);
  587.   ASSIGN2(48, 64, 0, 1, 1, 1, 0, 2, mb_type_B);
  588. }
  589.  
  590.  
  591. /* Macro for filling up the decoding tables for motion_vectors */
  592. #define ASSIGN3(start, end, step, val, num) \
  593.   for (i = start; i < end; i+= step) { \
  594.        for (j = 0; j < step / 2; j++) { \
  595.          motion_vectors[i+j].code = val; \
  596.          motion_vectors[i+j].num_bits = num; \
  597.          } \
  598.        for (j = step / 2; j < step; j++) { \
  599.      motion_vectors[i+j].code = -val; \
  600.      motion_vectors[i+j].num_bits = num; \
  601.          } \
  602.        val--; \
  603.        }
  604.  
  605.  
  606.  
  607. /*
  608.  *--------------------------------------------------------------
  609.  *
  610.  * init_motion_vectors --
  611.  *
  612.  *    Initialize the VLC decoding table for the various motion
  613.  *      vectors, including motion_horizontal_forward_code, 
  614.  *      motion_vertical_forward_code, motion_horizontal_backward_code,
  615.  *      and motion_vertical_backward_code.
  616.  *
  617.  * Results:
  618.  *    The decoding table for the motion vectors will be filled;
  619.  *      illegal values will be filled as ERROR.
  620.  *
  621.  * Side effects:
  622.  *    The global array motion_vector will be filled.
  623.  *
  624.  *--------------------------------------------------------------
  625.  */
  626. static void
  627.   init_motion_vectors()
  628. {
  629.   int i, j, val = 16;
  630.  
  631.   for (i = 0; i < 24; i++) {
  632.     motion_vectors[i].code = ERROR;
  633.     motion_vectors[i].num_bits = 0;
  634.   }
  635.  
  636.   ASSIGN3(24, 36, 2, val, 11);
  637.   ASSIGN3(36, 48, 4, val, 10);
  638.   ASSIGN3(48, 96, 16, val, 8);
  639.   ASSIGN3(96, 128, 32, val, 7);
  640.   ASSIGN3(128, 256, 128, val, 5);
  641.   ASSIGN3(256, 512, 256, val, 4);
  642.   ASSIGN3(512, 1024, 512, val, 3);
  643.   ASSIGN3(1024, 2048, 1024, val, 1);
  644. }
  645.  
  646.  
  647.  
  648.  
  649. /*
  650.  *--------------------------------------------------------------
  651.  *
  652.  * init_tables --
  653.  *
  654.  *    Initialize all the tables for VLC decoding; this must be
  655.  *      called when the system is set up before any decoding can
  656.  *      take place.
  657.  *
  658.  * Results:
  659.  *    All the decoding tables will be filled accordingly.
  660.  *
  661.  * Side effects:
  662.  *    The corresponding global array for each decoding table 
  663.  *      will be filled.
  664.  *
  665.  *--------------------------------------------------------------
  666.  */    
  667. void
  668.   init_tables()
  669. {
  670.   extern void init_pre_idct();
  671.  
  672.   init_mb_addr_inc();
  673.   init_mb_type_P();
  674.   init_mb_type_B();
  675.   init_motion_vectors();
  676.   init_pre_idct();
  677.  
  678.   {
  679.     init_stats();
  680.   }
  681. }
  682.  
  683. /*
  684.  *--------------------------------------------------------------
  685.  *
  686.  * DecodeDCTDCSizeLum --
  687.  *
  688.  *    Huffman Decoder for dct_dc_size_luminance; location where
  689.  *      the result of decoding will be placed is passed as argument.
  690.  *      The decoded values are obtained by doing a table lookup on
  691.  *      dct_dc_size_luminance.
  692.  *
  693.  * Results:
  694.  *    The decoded value for dct_dc_size_luminance or ERROR for 
  695.  *      unbound values will be placed in the location specified.
  696.  *
  697.  * Side effects:
  698.  *    Bit stream is irreversibly parsed.
  699.  *
  700.  *--------------------------------------------------------------
  701.  */        
  702. void
  703.   decodeDCTDCSizeLum(value)
  704. unsigned int *value;
  705. {
  706.   unsigned int index;
  707.  
  708.   show_bits7(index);
  709.   
  710.   *value = dct_dc_size_luminance[index].value;
  711.  
  712.   flush_bits(dct_dc_size_luminance[index].num_bits);
  713. }
  714.  
  715.  
  716.  
  717.  
  718. /*
  719.  *--------------------------------------------------------------
  720.  *
  721.  * DecodeDCTDCSizeChrom --
  722.  *
  723.  *    Huffman Decoder for dct_dc_size_chrominance; location where
  724.  *      the result of decoding will be placed is passed as argument.
  725.  *      The decoded values are obtained by doing a table lookup on
  726.  *      dct_dc_size_chrominance.
  727.  *
  728.  * Results:
  729.  *    The decoded value for dct_dc_size_chrominance or ERROR for
  730.  *      unbound values will be placed in the location specified.
  731.  *
  732.  * Side effects:
  733.  *    Bit stream is irreversibly parsed.
  734.  *
  735.  *--------------------------------------------------------------
  736.  */    
  737. void    
  738.   decodeDCTDCSizeChrom(value)
  739. unsigned int *value;
  740. {
  741.   unsigned int index;
  742.  
  743.   show_bits8(index);
  744.   
  745.   *value = dct_dc_size_chrominance[index].value;
  746.  
  747.   flush_bits(dct_dc_size_chrominance[index].num_bits);
  748. }
  749.  
  750.  
  751.  
  752. /*
  753.  *--------------------------------------------------------------
  754.  *
  755.  * decodeDCTCoeff --
  756.  *
  757.  *    Huffman Decoder for dct_coeff_first and dct_coeff_next;
  758.  *      locations where the results of decoding: run and level, are to
  759.  *      be placed and also the type of DCT coefficients, either
  760.  *      dct_coeff_first or dct_coeff_next, are being passed as argument.
  761.  *      
  762.  *      The decoder first examines the next 8 bits in the input stream,
  763.  *      and perform according to the following cases:
  764.  *      
  765.  *      '0000 0000' - examine 8 more bits (i.e. 16 bits total) and
  766.  *                    perform a table lookup on dct_coeff_tbl_0.
  767.  *                    One more bit is then examined to determine the sign
  768.  *                    of level.
  769.  *
  770.  *      '0000 0001' - examine 4 more bits (i.e. 12 bits total) and 
  771.  *                    perform a table lookup on dct_coeff_tbl_1.
  772.  *                    One more bit is then examined to determine the sign
  773.  *                    of level.
  774.  *      
  775.  *      '0000 0010' - examine 2 more bits (i.e. 10 bits total) and
  776.  *                    perform a table lookup on dct_coeff_tbl_2.
  777.  *                    One more bit is then examined to determine the sign
  778.  *                    of level.
  779.  *
  780.  *      '0000 0011' - examine 2 more bits (i.e. 10 bits total) and 
  781.  *                    perform a table lookup on dct_coeff_tbl_3.
  782.  *                    One more bit is then examined to determine the sign
  783.  *                    of level.
  784.  *
  785.  *      otherwise   - perform a table lookup on dct_coeff_tbl. If the
  786.  *                    value of run is not ESCAPE, extract one more bit
  787.  *                    to determine the sign of level; otherwise 6 more
  788.  *                    bits will be extracted to obtain the actual value 
  789.  *                    of run , and then 8 or 16 bits to get the value of level.
  790.  *                    
  791.  *      
  792.  *
  793.  * Results:
  794.  *    The decoded values of run and level or ERROR for unbound values
  795.  *      are placed in the locations specified.
  796.  *
  797.  * Side effects:
  798.  *    Bit stream is irreversibly parsed.
  799.  *
  800.  *--------------------------------------------------------------
  801.  */
  802. static int
  803.   decodeDCTCoeff(dct_coeff_tbl, run, level)
  804. unsigned short int *dct_coeff_tbl;                                       
  805. unsigned int *run;
  806. int *level;
  807. {
  808.   unsigned int temp, index, num_bits;
  809.   unsigned int value, next32bits, flushed;
  810.  
  811.   /*
  812.    * Grab the next 32 bits and use it to improve performance of
  813.    * getting the bits to parse. Thus, calls are translated as:
  814.    *
  815.    *    show_bitsX  <-->   next32bits >> (32-X)
  816.    *    get_bitsX   <-->   val = next32bits >> (32-flushed-X);
  817.    *               flushed += X;
  818.    *               next32bits &= bitMask[flushed];
  819.    *    flush_bitsX <-->   flushed += X;
  820.    *               next32bits &= bitMask[flushed];
  821.    *
  822.    */
  823.   show_bits32(next32bits);
  824.   flushed = 0;
  825.  
  826.   /* show_bits8(index); */
  827.   index = next32bits >> 24;
  828.  
  829.   if (index > 3) {
  830.     value = dct_coeff_tbl[index];
  831.     *run = (value & RUN_MASK) >> RUN_SHIFT;
  832.     if (*run == END_OF_BLOCK) {
  833.       *level = END_OF_BLOCK;
  834.     }
  835.     else {
  836.       /* num_bits = (value & NUM_MASK) + 1; */
  837.       /* flush_bits(num_bits); */
  838.       flushed = (value & NUM_MASK) + 1;
  839.       next32bits &= bitMask[flushed];
  840.       if (*run != ESCAPE) {
  841.     *level = (value & LEVEL_MASK) >> LEVEL_SHIFT;
  842.     /* get_bits1(value); */
  843.     /* if (value) *level = -*level; */
  844.     if (next32bits >> (31-flushed)) *level = -*level;
  845.     flushed++;
  846.     /* next32bits &= bitMask[flushed];  last op before update */
  847.       }
  848.       else {            /* *run == ESCAPE */
  849.     /* get_bits14(temp); */
  850.     temp = next32bits >> (18-flushed);
  851.     flushed += 14;
  852.     next32bits &= bitMask[flushed];
  853.     *run = temp >> 8;
  854.     temp &= 0xff;
  855.     if (temp == 0) {
  856.       /* get_bits8(*level); */
  857.       *level = next32bits >> (24-flushed);
  858.       flushed += 8;
  859.       /* next32bits &= bitMask[flushed];  last op before update */
  860.       myassert(*level >= 128);
  861.     } else if (temp != 128) {
  862.       /* Grab sign bit */
  863.       *level = ((int) (temp << 24)) >> 24;
  864.     } else {
  865.       /* get_bits8(*level); */
  866.       *level = next32bits >> (24-flushed);
  867.       flushed += 8;
  868.       /* next32bits &= bitMask[flushed];  last op before update */
  869.       *level = *level - 256;
  870.       myassert(*level <= -128 && *level >= -255);
  871.     }
  872.       }
  873.       /* Update bitstream... */
  874.       flush_bits(flushed);
  875.     }
  876.   }
  877.   else {
  878.     if (index == 2) { 
  879.       /* show_bits10(index); */
  880.       index = next32bits >> 22;
  881.       value = dct_coeff_tbl_2[index & 3];
  882.     }
  883.     else if (index == 3) { 
  884.       /* show_bits10(index); */
  885.       index = next32bits >> 22;
  886.       value = dct_coeff_tbl_3[index & 3];
  887.     }
  888.     else if (index) {        /* index == 1 */
  889.       /* show_bits12(index); */
  890.       index = next32bits >> 20;
  891.       value = dct_coeff_tbl_1[index & 15];
  892.     }
  893.     else {            /* index == 0 */
  894.       /* show_bits16(index); */
  895.       index = next32bits >> 16;
  896.       value = dct_coeff_tbl_0[index & 255];
  897.     }
  898.     *run = (value & RUN_MASK) >> RUN_SHIFT;
  899.     *level = (value & LEVEL_MASK) >> LEVEL_SHIFT;
  900.  
  901.     /*
  902.      * Fold these operations together to make it fast...
  903.      */
  904.     /* num_bits = (value & NUM_MASK) + 1; */
  905.     /* flush_bits(num_bits); */
  906.     /* get_bits1(value); */
  907.     /* if (value) *level = -*level; */
  908.  
  909.     flushed = (value & NUM_MASK) + 2;
  910.     if ((next32bits >> (32-flushed)) & 0x1) *level = -*level;
  911.  
  912.     /* Update bitstream ... */
  913.     flush_bits(flushed);
  914.   }
  915.   return PARSE_OK;
  916. }
  917.  
  918. #ifdef not_in_use
  919.  
  920. /*
  921.  *--------------------------------------------------------------
  922.  *
  923.  * decodeDCTCoeffFirst --
  924.  *
  925.  *    Huffman Decoder for dct_coeff_first. Locations for the
  926.  *      decoded results: run and level, are being passed as
  927.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  928.  *      with the table dct_coeff_first.
  929.  *
  930.  * Results:
  931.  *    The decoded values of run and level for dct_coeff_first or
  932.  *      ERROR for unbound values are placed in the locations given.
  933.  *
  934.  * Side effects:
  935.  *    Bit stream is irreversibly parsed.
  936.  *
  937.  *--------------------------------------------------------------
  938.  */        
  939. int
  940.   decodeDCTCoeffFirst(run, level)
  941. unsigned int *run;
  942. int *level;
  943. {
  944.   return decodeDCTCoeff(dct_coeff_first, run, level);
  945. }
  946.  
  947.  
  948.  
  949.  
  950. /*
  951.  *--------------------------------------------------------------
  952.  *
  953.  * decodeDCTCoeffNext --
  954.  *
  955.  *    Huffman Decoder for dct_coeff_first. Locations for the
  956.  *      decoded results: run and level, are being passed as
  957.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  958.  *      with the table dct_coeff_next.
  959.  *
  960.  * Results:
  961.  *    The decoded values of run and level for dct_coeff_next or
  962.  *      ERROR for unbound values are placed in the locations given.
  963.  *
  964.  * Side effects:
  965.  *    Bit stream is irreversibly parsed.
  966.  *
  967.  *--------------------------------------------------------------
  968.  */ 
  969. int
  970.   decodeDCTCoeffNext(run, level)
  971. unsigned int *run;
  972. int *level;
  973. {
  974.   return decodeDCTCoeff(dct_coeff_next, run, level);
  975. }
  976. #endif
  977.