home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / gfx / misc / mpeg_stat_2_2a.lha / mpeg_stat / src / decoders.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-21  |  32.5 KB  |  976 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.  
  60.  
  61. /* Error handling code */
  62. extern char *errorLocation, *errorSpecifics;
  63.  
  64. #define myassert(expression)\
  65.   if (!(expression)) {\
  66.             sprintf (errorSpecifics,"\nBad DCT-decode value (%d) at line %d\n", expression, __LINE__);\
  67.               return SKIP_TO_START_CODE;}
  68.  
  69. /* Decoding table for macroblock_address_increment */
  70. mb_addr_inc_entry     mb_addr_inc[2048];
  71.  
  72. /* Decoding table for macroblock_type in predictive-coded pictures */
  73. mb_type_entry         mb_type_P[64];
  74.  
  75. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  76. mb_type_entry         mb_type_B[64];
  77.  
  78. /* Decoding table for motion vectors */
  79. motion_vectors_entry  motion_vectors[2048];
  80.  
  81. /* Decoding table for coded_block_pattern */
  82.  
  83. coded_block_pattern_entry coded_block_pattern[512] = 
  84. { {(unsigned int)ERROR, 0}, {(unsigned int)ERROR, 0}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
  85.   {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
  86.   {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
  87.   {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
  88.   {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
  89.   {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
  90.   {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
  91.   {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
  92.   {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
  93.   {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7}, 
  94.   {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, 
  95.   {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7}, 
  96.   {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, 
  97.   {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, 
  98.   {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, 
  99.   {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, 
  100.   {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
  101.   {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
  102.   {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
  103.   {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
  104.   {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
  105.   {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
  106.   {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
  107.   {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
  108.   {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
  109.   {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
  110.   {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
  111.   {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
  112.   {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
  113.   {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
  114.   {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
  115.   {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
  116.   {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
  117.   {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
  118.   {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
  119.   {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
  120.   {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
  121.   {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
  122.   {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
  123.   {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
  124.   {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  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.   {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 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.   {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 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.   {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 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.   {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  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. };
  149.  
  150. /* Decoding table for dct_dc_size_luminance */
  151. dct_dc_size_entry dct_dc_size_luminance[128] =
  152. {   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  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.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 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.     {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, 
  161.     {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, 
  162.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  163.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  164.     {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, 
  165.     {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, 
  166.     {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, 
  167.     {6, 5}, {6, 5}, {6, 5}, {6, 5}, {7, 6}, {7, 6}, {8, 7}, {(unsigned int)ERROR, 0}
  168.   };
  169.  
  170. /* Decoding table for dct_dc_size_chrominance */
  171. dct_dc_size_entry dct_dc_size_chrominance[256] =
  172. { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  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.   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 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.   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 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.   {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  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.   {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  201.   {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  202.   {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, 
  203.   {6, 6}, {6, 6}, {6, 6}, {6, 6}, {7, 7}, {7, 7}, {8, 8}, {(unsigned int)ERROR, 0}
  204. };
  205.  
  206. /* DCT coeff tables. */
  207.  
  208. unsigned short int dct_coeff_tbl_0[256] =
  209. {
  210.   0xffff, 0xffff, 0xffff, 0xffff, 
  211.   0xffff, 0xffff, 0xffff, 0xffff, 
  212.   0xffff, 0xffff, 0xffff, 0xffff, 
  213.   0xffff, 0xffff, 0xffff, 0xffff, 
  214.   0x052f, 0x051f, 0x050f, 0x04ff, 
  215.   0x183f, 0x402f, 0x3c2f, 0x382f, 
  216.   0x342f, 0x302f, 0x2c2f, 0x7c1f, 
  217.   0x781f, 0x741f, 0x701f, 0x6c1f, 
  218.   0x028e, 0x028e, 0x027e, 0x027e, 
  219.   0x026e, 0x026e, 0x025e, 0x025e, 
  220.   0x024e, 0x024e, 0x023e, 0x023e, 
  221.   0x022e, 0x022e, 0x021e, 0x021e, 
  222.   0x020e, 0x020e, 0x04ee, 0x04ee, 
  223.   0x04de, 0x04de, 0x04ce, 0x04ce, 
  224.   0x04be, 0x04be, 0x04ae, 0x04ae, 
  225.   0x049e, 0x049e, 0x048e, 0x048e, 
  226.   0x01fd, 0x01fd, 0x01fd, 0x01fd, 
  227.   0x01ed, 0x01ed, 0x01ed, 0x01ed, 
  228.   0x01dd, 0x01dd, 0x01dd, 0x01dd, 
  229.   0x01cd, 0x01cd, 0x01cd, 0x01cd, 
  230.   0x01bd, 0x01bd, 0x01bd, 0x01bd, 
  231.   0x01ad, 0x01ad, 0x01ad, 0x01ad, 
  232.   0x019d, 0x019d, 0x019d, 0x019d, 
  233.   0x018d, 0x018d, 0x018d, 0x018d, 
  234.   0x017d, 0x017d, 0x017d, 0x017d, 
  235.   0x016d, 0x016d, 0x016d, 0x016d, 
  236.   0x015d, 0x015d, 0x015d, 0x015d, 
  237.   0x014d, 0x014d, 0x014d, 0x014d, 
  238.   0x013d, 0x013d, 0x013d, 0x013d, 
  239.   0x012d, 0x012d, 0x012d, 0x012d, 
  240.   0x011d, 0x011d, 0x011d, 0x011d, 
  241.   0x010d, 0x010d, 0x010d, 0x010d, 
  242.   0x282c, 0x282c, 0x282c, 0x282c, 
  243.   0x282c, 0x282c, 0x282c, 0x282c, 
  244.   0x242c, 0x242c, 0x242c, 0x242c, 
  245.   0x242c, 0x242c, 0x242c, 0x242c, 
  246.   0x143c, 0x143c, 0x143c, 0x143c, 
  247.   0x143c, 0x143c, 0x143c, 0x143c, 
  248.   0x0c4c, 0x0c4c, 0x0c4c, 0x0c4c, 
  249.   0x0c4c, 0x0c4c, 0x0c4c, 0x0c4c, 
  250.   0x085c, 0x085c, 0x085c, 0x085c, 
  251.   0x085c, 0x085c, 0x085c, 0x085c, 
  252.   0x047c, 0x047c, 0x047c, 0x047c, 
  253.   0x047c, 0x047c, 0x047c, 0x047c, 
  254.   0x046c, 0x046c, 0x046c, 0x046c, 
  255.   0x046c, 0x046c, 0x046c, 0x046c, 
  256.   0x00fc, 0x00fc, 0x00fc, 0x00fc, 
  257.   0x00fc, 0x00fc, 0x00fc, 0x00fc, 
  258.   0x00ec, 0x00ec, 0x00ec, 0x00ec, 
  259.   0x00ec, 0x00ec, 0x00ec, 0x00ec, 
  260.   0x00dc, 0x00dc, 0x00dc, 0x00dc, 
  261.   0x00dc, 0x00dc, 0x00dc, 0x00dc, 
  262.   0x00cc, 0x00cc, 0x00cc, 0x00cc, 
  263.   0x00cc, 0x00cc, 0x00cc, 0x00cc, 
  264.   0x681c, 0x681c, 0x681c, 0x681c, 
  265.   0x681c, 0x681c, 0x681c, 0x681c, 
  266.   0x641c, 0x641c, 0x641c, 0x641c, 
  267.   0x641c, 0x641c, 0x641c, 0x641c, 
  268.   0x601c, 0x601c, 0x601c, 0x601c, 
  269.   0x601c, 0x601c, 0x601c, 0x601c, 
  270.   0x5c1c, 0x5c1c, 0x5c1c, 0x5c1c, 
  271.   0x5c1c, 0x5c1c, 0x5c1c, 0x5c1c, 
  272.   0x581c, 0x581c, 0x581c, 0x581c, 
  273.   0x581c, 0x581c, 0x581c, 0x581c, 
  274. };
  275.  
  276. unsigned short int dct_coeff_tbl_1[16] = 
  277. {
  278.   0x00bb, 0x202b, 0x103b, 0x00ab, 
  279.   0x084b, 0x1c2b, 0x541b, 0x501b, 
  280.   0x009b, 0x4c1b, 0x481b, 0x045b, 
  281.   0x0c3b, 0x008b, 0x182b, 0x441b, 
  282. };
  283.  
  284. unsigned short int dct_coeff_tbl_2[4] =
  285. {
  286.   0x4019, 0x1429, 0x0079, 0x0839, 
  287. };
  288.  
  289. unsigned short int dct_coeff_tbl_3[4] = 
  290. {
  291.   0x0449, 0x3c19, 0x3819, 0x1029, 
  292. };
  293.  
  294. unsigned short int dct_coeff_next[256] = 
  295. {
  296.   0xffff, 0xffff, 0xffff, 0xffff, 
  297.   0xf7d5, 0xf7d5, 0xf7d5, 0xf7d5, 
  298.   0x0826, 0x0826, 0x2416, 0x2416, 
  299.   0x0046, 0x0046, 0x2016, 0x2016, 
  300.   0x1c15, 0x1c15, 0x1c15, 0x1c15, 
  301.   0x1815, 0x1815, 0x1815, 0x1815, 
  302.   0x0425, 0x0425, 0x0425, 0x0425, 
  303.   0x1415, 0x1415, 0x1415, 0x1415, 
  304.   0x3417, 0x0067, 0x3017, 0x2c17, 
  305.   0x0c27, 0x0437, 0x0057, 0x2817, 
  306.   0x0034, 0x0034, 0x0034, 0x0034, 
  307.   0x0034, 0x0034, 0x0034, 0x0034, 
  308.   0x1014, 0x1014, 0x1014, 0x1014, 
  309.   0x1014, 0x1014, 0x1014, 0x1014, 
  310.   0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  311.   0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  312.   0x0023, 0x0023, 0x0023, 0x0023, 
  313.   0x0023, 0x0023, 0x0023, 0x0023, 
  314.   0x0023, 0x0023, 0x0023, 0x0023, 
  315.   0x0023, 0x0023, 0x0023, 0x0023, 
  316.   0x0813, 0x0813, 0x0813, 0x0813, 
  317.   0x0813, 0x0813, 0x0813, 0x0813, 
  318.   0x0813, 0x0813, 0x0813, 0x0813, 
  319.   0x0813, 0x0813, 0x0813, 0x0813, 
  320.   0x0412, 0x0412, 0x0412, 0x0412, 
  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.   0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  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.   0x0011, 0x0011, 0x0011, 0x0011, 
  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. };
  361.  
  362. unsigned short int dct_coeff_first[256] = 
  363. {
  364.   0xffff, 0xffff, 0xffff, 0xffff, 
  365.   0xf7d5, 0xf7d5, 0xf7d5, 0xf7d5, 
  366.   0x0826, 0x0826, 0x2416, 0x2416, 
  367.   0x0046, 0x0046, 0x2016, 0x2016, 
  368.   0x1c15, 0x1c15, 0x1c15, 0x1c15, 
  369.   0x1815, 0x1815, 0x1815, 0x1815, 
  370.   0x0425, 0x0425, 0x0425, 0x0425, 
  371.   0x1415, 0x1415, 0x1415, 0x1415, 
  372.   0x3417, 0x0067, 0x3017, 0x2c17, 
  373.   0x0c27, 0x0437, 0x0057, 0x2817, 
  374.   0x0034, 0x0034, 0x0034, 0x0034, 
  375.   0x0034, 0x0034, 0x0034, 0x0034, 
  376.   0x1014, 0x1014, 0x1014, 0x1014, 
  377.   0x1014, 0x1014, 0x1014, 0x1014, 
  378.   0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  379.   0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  380.   0x0023, 0x0023, 0x0023, 0x0023, 
  381.   0x0023, 0x0023, 0x0023, 0x0023, 
  382.   0x0023, 0x0023, 0x0023, 0x0023, 
  383.   0x0023, 0x0023, 0x0023, 0x0023, 
  384.   0x0813, 0x0813, 0x0813, 0x0813, 
  385.   0x0813, 0x0813, 0x0813, 0x0813, 
  386.   0x0813, 0x0813, 0x0813, 0x0813, 
  387.   0x0813, 0x0813, 0x0813, 0x0813, 
  388.   0x0412, 0x0412, 0x0412, 0x0412, 
  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.   0x0010, 0x0010, 0x0010, 0x0010, 
  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. };
  429.  
  430. /* Macro for filling up the decoding table for mb_addr_inc */
  431. #define ASSIGN1(start, end, step, val, num) \
  432.   for (i = start; i < end; i+= step) { \
  433.    for (j = 0; j < step; j++) { \
  434.     mb_addr_inc[i+j].value = val; \
  435.         mb_addr_inc[i+j].num_bits = num; \
  436.       } \
  437.     val--; \
  438.     }
  439.  
  440.  
  441.  
  442. /*
  443.  *--------------------------------------------------------------
  444.  *
  445.  * init_mb_addr_inc --
  446.  *
  447.  *    Initialize the VLC decoding table for macro_block_address_increment
  448.  *
  449.  * Results:
  450.  *    The decoding table for macro_block_address_increment will
  451.  *      be filled; illegal values will be filled as ERROR.
  452.  *
  453.  * Side effects:
  454.  *    The global array mb_addr_inc will be filled.
  455.  *
  456.  *--------------------------------------------------------------
  457.  */
  458. static void
  459.   init_mb_addr_inc()
  460. {
  461.   int i, j, val;
  462.  
  463.   for (i = 0; i < 8; i++) {
  464.     mb_addr_inc[i].value = ERROR;
  465.     mb_addr_inc[i].num_bits = 0;
  466.   }
  467.  
  468.   mb_addr_inc[8].value = MACRO_BLOCK_ESCAPE;
  469.   mb_addr_inc[8].num_bits = 11;
  470.  
  471.   for (i = 9; i < 15; i++) {
  472.     mb_addr_inc[i].value = ERROR;
  473.     mb_addr_inc[i].num_bits = 0;
  474.   }
  475.  
  476.   mb_addr_inc[15].value = MACRO_BLOCK_STUFFING;
  477.   mb_addr_inc[15].num_bits = 11;
  478.  
  479.   for (i = 16; i < 24; i++) {
  480.     mb_addr_inc[i].value = ERROR;
  481.     mb_addr_inc[i].num_bits = 0;
  482.   }
  483.  
  484.   val = 33;
  485.  
  486.   ASSIGN1(24, 36, 1, val, 11);
  487.   ASSIGN1(36, 48, 2, val, 10);
  488.   ASSIGN1(48, 96, 8, val, 8);
  489.   ASSIGN1(96, 128, 16, val, 7);
  490.   ASSIGN1(128, 256, 64, val, 5);
  491.   ASSIGN1(256, 512, 128, val, 4);
  492.   ASSIGN1(512, 1024, 256, val, 3);
  493.   ASSIGN1(1024, 2048, 1024, val, 1);
  494. }
  495.  
  496.  
  497. /* Macro for filling up the decoding table for mb_type */
  498. #define ASSIGN2(start, end, quant, motion_forward, motion_backward, pattern, intra, num, mb_type) \
  499.   for (i = start; i < end; i ++) { \
  500.       mb_type[i].mb_quant = quant; \
  501.       mb_type[i].mb_motion_forward = motion_forward; \
  502.       mb_type[i].mb_motion_backward = motion_backward; \
  503.       mb_type[i].mb_pattern = pattern; \
  504.       mb_type[i].mb_intra = intra; \
  505.       mb_type[i].num_bits = num; \
  506.       }
  507.  
  508.  
  509.  
  510. /*
  511.  *--------------------------------------------------------------
  512.  *
  513.  * init_mb_type_P --
  514.  *
  515.  *    Initialize the VLC decoding table for macro_block_type in
  516.  *      predictive-coded pictures.
  517.  *
  518.  * Results:
  519.  *    The decoding table for macro_block_type in predictive-coded
  520.  *      pictures will be filled; illegal values will be filled as ERROR.
  521.  *
  522.  * Side effects:
  523.  *    The global array mb_type_P will be filled.
  524.  *
  525.  *--------------------------------------------------------------
  526.  */
  527. static void
  528.   init_mb_type_P()
  529. {
  530.   int i;
  531.  
  532.   mb_type_P[0].mb_quant = mb_type_P[0].mb_motion_forward 
  533.     = mb_type_P[0].mb_motion_backward = mb_type_P[0].mb_pattern 
  534.       = mb_type_P[0].mb_intra = ERROR;
  535.   mb_type_P[0].num_bits = 0;
  536.  
  537.   ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_P)
  538.     ASSIGN2(2, 4, 1, 0, 0, 1, 0, 5, mb_type_P)
  539.       ASSIGN2(4, 6, 1, 1, 0, 1, 0, 5, mb_type_P);
  540.   ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_P);
  541.   ASSIGN2(8, 16, 0, 1, 0, 0, 0, 3, mb_type_P);
  542.   ASSIGN2(16, 32, 0, 0, 0, 1, 0, 2, mb_type_P);
  543.   ASSIGN2(32, 64, 0, 1, 0, 1, 0, 1, mb_type_P);
  544. }
  545.  
  546.  
  547.  
  548.  
  549. /*
  550.  *--------------------------------------------------------------
  551.  *
  552.  * init_mb_type_B --
  553.  *
  554.  *    Initialize the VLC decoding table for macro_block_type in
  555.  *      bidirectionally-coded pictures.
  556.  *
  557.  * Results:
  558.  *    The decoding table for macro_block_type in bidirectionally-coded
  559.  *      pictures will be filled; illegal values will be filled as ERROR.
  560.  *
  561.  * Side effects:
  562.  *    The global array mb_type_B will be filled.
  563.  *
  564.  *--------------------------------------------------------------
  565.  */
  566. static void
  567.   init_mb_type_B()
  568. {
  569.   int i;
  570.  
  571.   mb_type_B[0].mb_quant = mb_type_B[0].mb_motion_forward 
  572.     = mb_type_B[0].mb_motion_backward = mb_type_B[0].mb_pattern 
  573.       = mb_type_B[0].mb_intra = ERROR;
  574.   mb_type_B[0].num_bits = 0;
  575.  
  576.   ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_B);
  577.   ASSIGN2(2, 3, 1, 0, 1, 1, 0, 6, mb_type_B);
  578.   ASSIGN2(3, 4, 1, 1, 0, 1, 0, 6, mb_type_B);
  579.   ASSIGN2(4, 6, 1, 1, 1, 1, 0, 5, mb_type_B);
  580.   ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_B);
  581.   ASSIGN2(8, 12, 0, 1, 0, 0, 0, 4, mb_type_B);
  582.   ASSIGN2(12, 16, 0, 1, 0, 1, 0, 4, mb_type_B);
  583.   ASSIGN2(16, 24, 0, 0, 1, 0, 0, 3, mb_type_B);
  584.   ASSIGN2(24, 32, 0, 0, 1, 1, 0, 3, mb_type_B);
  585.   ASSIGN2(32, 48, 0, 1, 1, 0, 0, 2, mb_type_B);
  586.   ASSIGN2(48, 64, 0, 1, 1, 1, 0, 2, mb_type_B);
  587. }
  588.  
  589.  
  590. /* Macro for filling up the decoding tables for motion_vectors */
  591. #define ASSIGN3(start, end, step, val, num) \
  592.   for (i = start; i < end; i+= step) { \
  593.        for (j = 0; j < step / 2; j++) { \
  594.          motion_vectors[i+j].code = val; \
  595.          motion_vectors[i+j].num_bits = num; \
  596.          } \
  597.        for (j = step / 2; j < step; j++) { \
  598.      motion_vectors[i+j].code = -val; \
  599.      motion_vectors[i+j].num_bits = num; \
  600.          } \
  601.        val--; \
  602.        }
  603.  
  604.  
  605.  
  606. /*
  607.  *--------------------------------------------------------------
  608.  *
  609.  * init_motion_vectors --
  610.  *
  611.  *    Initialize the VLC decoding table for the various motion
  612.  *      vectors, including motion_horizontal_forward_code, 
  613.  *      motion_vertical_forward_code, motion_horizontal_backward_code,
  614.  *      and motion_vertical_backward_code.
  615.  *
  616.  * Results:
  617.  *    The decoding table for the motion vectors will be filled;
  618.  *      illegal values will be filled as ERROR.
  619.  *
  620.  * Side effects:
  621.  *    The global array motion_vector will be filled.
  622.  *
  623.  *--------------------------------------------------------------
  624.  */
  625. static void
  626.   init_motion_vectors()
  627. {
  628.   int i, j, val = 16;
  629.  
  630.   for (i = 0; i < 24; i++) {
  631.     motion_vectors[i].code = ERROR;
  632.     motion_vectors[i].num_bits = 0;
  633.   }
  634.  
  635.   ASSIGN3(24, 36, 2, val, 11);
  636.   ASSIGN3(36, 48, 4, val, 10);
  637.   ASSIGN3(48, 96, 16, val, 8);
  638.   ASSIGN3(96, 128, 32, val, 7);
  639.   ASSIGN3(128, 256, 128, val, 5);
  640.   ASSIGN3(256, 512, 256, val, 4);
  641.   ASSIGN3(512, 1024, 512, val, 3);
  642.   ASSIGN3(1024, 2048, 1024, val, 1);
  643. }
  644.  
  645.  
  646.  
  647.  
  648. /*
  649.  *--------------------------------------------------------------
  650.  *
  651.  * init_tables --
  652.  *
  653.  *    Initialize all the tables for VLC decoding; this must be
  654.  *      called when the system is set up before any decoding can
  655.  *      take place.
  656.  *
  657.  * Results:
  658.  *    All the decoding tables will be filled accordingly.
  659.  *
  660.  * Side effects:
  661.  *    The corresponding global array for each decoding table 
  662.  *      will be filled.
  663.  *
  664.  *--------------------------------------------------------------
  665.  */    
  666. void
  667.   init_tables()
  668. {
  669.   extern void init_pre_idct();
  670.  
  671.   init_mb_addr_inc();
  672.   init_mb_type_P();
  673.   init_mb_type_B();
  674.   init_motion_vectors();
  675.   init_pre_idct();
  676.  
  677.   {
  678.     init_stats();
  679.   }
  680. }
  681.  
  682. /*
  683.  *--------------------------------------------------------------
  684.  *
  685.  * DecodeDCTDCSizeLum --
  686.  *
  687.  *    Huffman Decoder for dct_dc_size_luminance; location where
  688.  *      the result of decoding will be placed is passed as argument.
  689.  *      The decoded values are obtained by doing a table lookup on
  690.  *      dct_dc_size_luminance.
  691.  *
  692.  * Results:
  693.  *    The decoded value for dct_dc_size_luminance or ERROR for 
  694.  *      unbound values will be placed in the location specified.
  695.  *
  696.  * Side effects:
  697.  *    Bit stream is irreversibly parsed.
  698.  *
  699.  *--------------------------------------------------------------
  700.  */        
  701. void
  702.   decodeDCTDCSizeLum(value)
  703. unsigned int *value;
  704. {
  705.   unsigned int index;
  706.  
  707.   show_bits7(index);
  708.   
  709.   *value = dct_dc_size_luminance[index].value;
  710.  
  711.   flush_bits(dct_dc_size_luminance[index].num_bits);
  712. }
  713.  
  714.  
  715.  
  716.  
  717. /*
  718.  *--------------------------------------------------------------
  719.  *
  720.  * DecodeDCTDCSizeChrom --
  721.  *
  722.  *    Huffman Decoder for dct_dc_size_chrominance; location where
  723.  *      the result of decoding will be placed is passed as argument.
  724.  *      The decoded values are obtained by doing a table lookup on
  725.  *      dct_dc_size_chrominance.
  726.  *
  727.  * Results:
  728.  *    The decoded value for dct_dc_size_chrominance or ERROR for
  729.  *      unbound values will be placed in the location specified.
  730.  *
  731.  * Side effects:
  732.  *    Bit stream is irreversibly parsed.
  733.  *
  734.  *--------------------------------------------------------------
  735.  */    
  736. void    
  737.   decodeDCTDCSizeChrom(value)
  738. unsigned int *value;
  739. {
  740.   unsigned int index;
  741.  
  742.   show_bits8(index);
  743.   
  744.   *value = dct_dc_size_chrominance[index].value;
  745.  
  746.   flush_bits(dct_dc_size_chrominance[index].num_bits);
  747. }
  748.  
  749.  
  750.  
  751. /*
  752.  *--------------------------------------------------------------
  753.  *
  754.  * decodeDCTCoeff --
  755.  *
  756.  *    Huffman Decoder for dct_coeff_first and dct_coeff_next;
  757.  *      locations where the results of decoding: run and level, are to
  758.  *      be placed and also the type of DCT coefficients, either
  759.  *      dct_coeff_first or dct_coeff_next, are being passed as argument.
  760.  *      
  761.  *      The decoder first examines the next 8 bits in the input stream,
  762.  *      and perform according to the following cases:
  763.  *      
  764.  *      '0000 0000' - examine 8 more bits (i.e. 16 bits total) and
  765.  *                    perform a table lookup on dct_coeff_tbl_0.
  766.  *                    One more bit is then examined to determine the sign
  767.  *                    of level.
  768.  *
  769.  *      '0000 0001' - examine 4 more bits (i.e. 12 bits total) and 
  770.  *                    perform a table lookup on dct_coeff_tbl_1.
  771.  *                    One more bit is then examined to determine the sign
  772.  *                    of level.
  773.  *      
  774.  *      '0000 0010' - examine 2 more bits (i.e. 10 bits total) and
  775.  *                    perform a table lookup on dct_coeff_tbl_2.
  776.  *                    One more bit is then examined to determine the sign
  777.  *                    of level.
  778.  *
  779.  *      '0000 0011' - examine 2 more bits (i.e. 10 bits total) and 
  780.  *                    perform a table lookup on dct_coeff_tbl_3.
  781.  *                    One more bit is then examined to determine the sign
  782.  *                    of level.
  783.  *
  784.  *      otherwise   - perform a table lookup on dct_coeff_tbl. If the
  785.  *                    value of run is not ESCAPE, extract one more bit
  786.  *                    to determine the sign of level; otherwise 6 more
  787.  *                    bits will be extracted to obtain the actual value 
  788.  *                    of run , and then 8 or 16 bits to get the value of level.
  789.  *                    
  790.  *      
  791.  *
  792.  * Results:
  793.  *    The decoded values of run and level or ERROR for unbound values
  794.  *      are placed in the locations specified.
  795.  *
  796.  * Side effects:
  797.  *    Bit stream is irreversibly parsed.
  798.  *
  799.  *--------------------------------------------------------------
  800.  */
  801. static int
  802.   decodeDCTCoeff(dct_coeff_tbl, run, level)
  803. unsigned short int *dct_coeff_tbl;                                       
  804. unsigned int *run;
  805. int *level;
  806. {
  807.   unsigned int temp, index, num_bits;
  808.   unsigned int value, next32bits, flushed;
  809.  
  810.   /*
  811.    * Grab the next 32 bits and use it to improve performance of
  812.    * getting the bits to parse. Thus, calls are translated as:
  813.    *
  814.    *    show_bitsX  <-->   next32bits >> (32-X)
  815.    *    get_bitsX   <-->   val = next32bits >> (32-flushed-X);
  816.    *               flushed += X;
  817.    *               next32bits &= bitMask[flushed];
  818.    *    flush_bitsX <-->   flushed += X;
  819.    *               next32bits &= bitMask[flushed];
  820.    *
  821.    */
  822.   show_bits32(next32bits);
  823.   flushed = 0;
  824.  
  825.   /* show_bits8(index); */
  826.   index = next32bits >> 24;
  827.  
  828.   if (index > 3) {
  829.     value = dct_coeff_tbl[index];
  830.     *run = (value & RUN_MASK) >> RUN_SHIFT;
  831.     if (*run == END_OF_BLOCK) {
  832.       *level = END_OF_BLOCK;
  833.     }
  834.     else {
  835.       /* num_bits = (value & NUM_MASK) + 1; */
  836.       /* flush_bits(num_bits); */
  837.       flushed = (value & NUM_MASK) + 1;
  838.       next32bits &= bitMask[flushed];
  839.       if (*run != ESCAPE) {
  840.     *level = (value & LEVEL_MASK) >> LEVEL_SHIFT;
  841.     /* get_bits1(value); */
  842.     /* if (value) *level = -*level; */
  843.     if (next32bits >> (31-flushed)) *level = -*level;
  844.     flushed++;
  845.     /* next32bits &= bitMask[flushed];  last op before update */
  846.       }
  847.       else {            /* *run == ESCAPE */
  848.     /* get_bits14(temp); */
  849.     temp = next32bits >> (18-flushed);
  850.     flushed += 14;
  851.     next32bits &= bitMask[flushed];
  852.     *run = temp >> 8;
  853.     temp &= 0xff;
  854.     if (temp == 0) {
  855.       /* get_bits8(*level); */
  856.       *level = next32bits >> (24-flushed);
  857.       flushed += 8;
  858.       /* next32bits &= bitMask[flushed];  last op before update */
  859.       myassert(*level >= 128);
  860.     } else if (temp != 128) {
  861.       /* Grab sign bit */
  862.       *level = ((int) (temp << 24)) >> 24;
  863.     } else {
  864.       /* get_bits8(*level); */
  865.       *level = next32bits >> (24-flushed);
  866.       flushed += 8;
  867.       /* next32bits &= bitMask[flushed];  last op before update */
  868.       *level = *level - 256;
  869.       myassert(*level <= -128 && *level >= -255);
  870.     }
  871.       }
  872.       /* Update bitstream... */
  873.       flush_bits(flushed);
  874.     }
  875.   }
  876.   else {
  877.     if (index == 2) { 
  878.       /* show_bits10(index); */
  879.       index = next32bits >> 22;
  880.       value = dct_coeff_tbl_2[index & 3];
  881.     }
  882.     else if (index == 3) { 
  883.       /* show_bits10(index); */
  884.       index = next32bits >> 22;
  885.       value = dct_coeff_tbl_3[index & 3];
  886.     }
  887.     else if (index) {        /* index == 1 */
  888.       /* show_bits12(index); */
  889.       index = next32bits >> 20;
  890.       value = dct_coeff_tbl_1[index & 15];
  891.     }
  892.     else {            /* index == 0 */
  893.       /* show_bits16(index); */
  894.       index = next32bits >> 16;
  895.       value = dct_coeff_tbl_0[index & 255];
  896.     }
  897.     *run = (value & RUN_MASK) >> RUN_SHIFT;
  898.     *level = (value & LEVEL_MASK) >> LEVEL_SHIFT;
  899.  
  900.     /*
  901.      * Fold these operations together to make it fast...
  902.      */
  903.     /* num_bits = (value & NUM_MASK) + 1; */
  904.     /* flush_bits(num_bits); */
  905.     /* get_bits1(value); */
  906.     /* if (value) *level = -*level; */
  907.  
  908.     flushed = (value & NUM_MASK) + 2;
  909.     if ((next32bits >> (32-flushed)) & 0x1) *level = -*level;
  910.  
  911.     /* Update bitstream ... */
  912.     flush_bits(flushed);
  913.   }
  914.   return PARSE_OK;
  915. }
  916.  
  917. #ifdef not_in_use
  918.  
  919. /*
  920.  *--------------------------------------------------------------
  921.  *
  922.  * decodeDCTCoeffFirst --
  923.  *
  924.  *    Huffman Decoder for dct_coeff_first. Locations for the
  925.  *      decoded results: run and level, are being passed as
  926.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  927.  *      with the table dct_coeff_first.
  928.  *
  929.  * Results:
  930.  *    The decoded values of run and level for dct_coeff_first or
  931.  *      ERROR for unbound values are placed in the locations given.
  932.  *
  933.  * Side effects:
  934.  *    Bit stream is irreversibly parsed.
  935.  *
  936.  *--------------------------------------------------------------
  937.  */        
  938. int
  939.   decodeDCTCoeffFirst(run, level)
  940. unsigned int *run;
  941. int *level;
  942. {
  943.   return decodeDCTCoeff(dct_coeff_first, run, level);
  944. }
  945.  
  946.  
  947.  
  948.  
  949. /*
  950.  *--------------------------------------------------------------
  951.  *
  952.  * decodeDCTCoeffNext --
  953.  *
  954.  *    Huffman Decoder for dct_coeff_first. Locations for the
  955.  *      decoded results: run and level, are being passed as
  956.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  957.  *      with the table dct_coeff_next.
  958.  *
  959.  * Results:
  960.  *    The decoded values of run and level for dct_coeff_next or
  961.  *      ERROR for unbound values are placed in the locations given.
  962.  *
  963.  * Side effects:
  964.  *    Bit stream is irreversibly parsed.
  965.  *
  966.  *--------------------------------------------------------------
  967.  */ 
  968. int
  969.   decodeDCTCoeffNext(run, level)
  970. unsigned int *run;
  971. int *level;
  972. {
  973.   return decodeDCTCoeff(dct_coeff_next, run, level);
  974. }
  975. #endif
  976.