home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / amiga / mpeg / mpgplyr1.lha / src / decoders.c < prev    next >
C/C++ Source or Header  |  1992-12-08  |  38KB  |  1,074 lines

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