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