1 /*
2  * Copyright (c) 1995 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.h
23  *
24  * This file contains the declarations of structures required for Huffman
25  * decoding
26  *
27  */
28 
29 /* Include util.h for bit i/o parsing macros. */
30 
31 #include "util.h"
32 
33 /* Code for unbound values in decoding tables */
34 #define ERROR (-1)
35 #define DCT_ERROR 63
36 
37 #define MACRO_BLOCK_STUFFING 34
38 #define MACRO_BLOCK_ESCAPE 35
39 
40 /* Two types of DCT Coefficients */
41 #define DCT_COEFF_FIRST 0
42 #define DCT_COEFF_NEXT 1
43 
44 /* Special values for DCT Coefficients */
45 #define END_OF_BLOCK 62
46 #define ESCAPE 61
47 
48 /* Structure for an entry in the decoding table of
49  * macroblock_address_increment */
50 typedef struct {
51   int value;       /* value for macroblock_address_increment */
52   int num_bits;             /* length of the Huffman code */
53 } mb_addr_inc_entry;
54 
55 /* Decoding table for macroblock_address_increment */
56 extern mb_addr_inc_entry mb_addr_inc[2048];
57 
58 
59 /* Structure for an entry in the decoding table of macroblock_type */
60 typedef struct {
61   unsigned int mb_quant;              /* macroblock_quant */
62   unsigned int mb_motion_forward;     /* macroblock_motion_forward */
63   unsigned int mb_motion_backward;    /* macroblock_motion_backward */
64   unsigned int mb_pattern;            /* macroblock_pattern */
65   unsigned int mb_intra;              /* macroblock_intra */
66   int num_bits;                       /* length of the Huffman code */
67 } mb_type_entry;
68 
69 /* Decoding table for macroblock_type in predictive-coded pictures */
70 extern mb_type_entry mb_type_P[64];
71 
72 /* Decoding table for macroblock_type in bidirectionally-coded pictures */
73 extern mb_type_entry mb_type_B[64];
74 
75 
76 /* Structures for an entry in the decoding table of coded_block_pattern */
77 typedef struct {
78   unsigned int cbp;            /* coded_block_pattern */
79   int num_bits;                /* length of the Huffman code */
80 } coded_block_pattern_entry;
81 
82 /* External declaration of coded block pattern table. */
83 
84 extern coded_block_pattern_entry coded_block_pattern[512];
85 
86 
87 
88 /* Structure for an entry in the decoding table of motion vectors */
89 typedef struct {
90   int code;              /* value for motion_horizontal_forward_code,
91 			  * motion_vertical_forward_code,
92 			  * motion_horizontal_backward_code, or
93 			  * motion_vertical_backward_code.
94 			  */
95   int num_bits;          /* length of the Huffman code */
96 } motion_vectors_entry;
97 
98 
99 /* Decoding table for motion vectors */
100 extern motion_vectors_entry motion_vectors[2048];
101 
102 
103 /* Structure for an entry in the decoding table of dct_dc_size */
104 typedef struct {
105   unsigned int value;    /* value of dct_dc_size (luminance or chrominance) */
106   int num_bits;          /* length of the Huffman code */
107 } dct_dc_size_entry;
108 
109 /* External declaration of dct dc size lumiance table. */
110 
111 extern dct_dc_size_entry dct_dc_size_luminance[32];
112 extern dct_dc_size_entry dct_dc_size_luminance1[16];
113 
114 /* External declaration of dct dc size chrom table. */
115 
116 extern dct_dc_size_entry dct_dc_size_chrominance[32];
117 extern dct_dc_size_entry dct_dc_size_chrominance1[32];
118 
119 
120 /* DCT coeff tables. */
121 
122 #define RUN_MASK 0xfc00
123 #define LEVEL_MASK 0x03f0
124 #define NUM_MASK 0x000f
125 #define RUN_SHIFT 10
126 #define LEVEL_SHIFT 4
127 
128 /* External declaration of dct coeff tables. */
129 
130 extern unsigned short int dct_coeff_tbl_0[256];
131 extern unsigned short int dct_coeff_tbl_1[16];
132 extern unsigned short int dct_coeff_tbl_2[4];
133 extern unsigned short int dct_coeff_tbl_3[4];
134 extern unsigned short int dct_coeff_next[256];
135 extern unsigned short int dct_coeff_first[256];
136 
137 #define DecodeDCTDCSizeLum(macro_val)                    \
138 {                                                    \
139   unsigned int index;	\
140 	\
141   show_bits5(index);	\
142   	\
143   if (index < 31) {	\
144   	macro_val = dct_dc_size_luminance[index].value;	\
145   	flush_bits(dct_dc_size_luminance[index].num_bits);	\
146   }	\
147   else {	\
148 	show_bits9(index);	\
149 	index -= 0x1f0;	\
150 	macro_val = dct_dc_size_luminance1[index].value;	\
151 	flush_bits(dct_dc_size_luminance1[index].num_bits);	\
152   }	\
153 }
154 
155 #define DecodeDCTDCSizeChrom(macro_val)                      \
156 {                                                        \
157   unsigned int index;	\
158 	\
159   show_bits5(index);	\
160   	\
161   if (index < 31) {	\
162   	macro_val = dct_dc_size_chrominance[index].value;	\
163   	flush_bits(dct_dc_size_chrominance[index].num_bits);	\
164   }	\
165   else {	\
166 	show_bits10(index);	\
167 	index -= 0x3e0;	\
168 	macro_val = dct_dc_size_chrominance1[index].value;	\
169 	flush_bits(dct_dc_size_chrominance1[index].num_bits);	\
170   }	\
171 }
172 
173 #define DecodeDCTCoeff(dct_coeff_tbl, run, level)			\
174 {									\
175   unsigned int temp, index;						\
176   unsigned int value, next32bits, flushed;				\
177 									\
178   /*									\
179    * Grab the next 32 bits and use it to improve performance of		\
180    * getting the bits to parse. Thus, calls are translated as:		\
181    *									\
182    *	show_bitsX  <-->   next32bits >> (32-X)				\
183    *	get_bitsX   <-->   val = next32bits >> (32-flushed-X);		\
184    *			   flushed += X;				\
185    *			   next32bits &= bitMask[flushed];		\
186    *	flush_bitsX <-->   flushed += X;				\
187    *			   next32bits &= bitMask[flushed];		\
188    *									\
189    * I've streamlined the code a lot, so that we don't have to mask	\
190    * out the low order bits and a few of the extra adds are removed.	\
191    */									\
192   show_bits32(next32bits);						\
193 									\
194   /* show_bits8(index); */						\
195   index = next32bits >> 24;						\
196 									\
197   if (index > 3) {							\
198     value = dct_coeff_tbl[index];					\
199     run = value >> RUN_SHIFT;						\
200     if (run != END_OF_BLOCK) {						\
201       /* num_bits = (value & NUM_MASK) + 1; */				\
202       /* flush_bits(num_bits); */					\
203       if (run != ESCAPE) {						\
204 	 /* get_bits1(value); */					\
205 	 /* if (value) level = -level; */				\
206 	 flushed = (value & NUM_MASK) + 2;				\
207          level = (value & LEVEL_MASK) >> LEVEL_SHIFT;			\
208 	 value = next32bits >> (32-flushed);				\
209 	 value &= 0x1;							\
210 	 if (value) level = -level;					\
211 	 /* next32bits &= ((~0) >> flushed);  last op before update */	\
212        }								\
213        else {    /* run == ESCAPE */					\
214 	 /* Get the next six into run, and next 8 into temp */		\
215          /* get_bits14(temp); */					\
216 	 flushed = (value & NUM_MASK) + 1;				\
217 	 temp = next32bits >> (18-flushed);				\
218 	 /* Normally, we'd ad 14 to flushed, but I've saved a few	\
219 	  * instr by moving the add below */				\
220 	 temp &= 0x3fff;						\
221 	 run = temp >> 8;						\
222 	 temp &= 0xff;							\
223 	 if (temp == 0) {						\
224             /* get_bits8(level); */					\
225 	    level = next32bits >> (10-flushed);				\
226 	    level &= 0xff;						\
227 	    flushed += 22;						\
228  	    assert(level >= 128);					\
229 	 } else if (temp != 128) {					\
230 	    /* Grab sign bit */						\
231 	    flushed += 14;						\
232 	    level = ((int) (temp << 24)) >> 24;				\
233 	 } else {							\
234             /* get_bits8(level); */					\
235 	    level = next32bits >> (10-flushed);				\
236 	    level &= 0xff;						\
237 	    flushed += 22;						\
238 	    level = level - 256;					\
239 	    assert(level <= -128 && level >= -255);			\
240 	 }								\
241        }								\
242        /* Update bitstream... */					\
243        flush_bits(flushed);						\
244        assert (flushed <= 32);						\
245     }									\
246   }									\
247   else {								\
248     switch (index) {                                                    \
249     case 2: {   							\
250       /* show_bits10(index); */						\
251       index = next32bits >> 22;						\
252       value = dct_coeff_tbl_2[index & 3];				\
253       break;                                                            \
254     }									\
255     case 3: { 						                \
256       /* show_bits10(index); */						\
257       index = next32bits >> 22;						\
258       value = dct_coeff_tbl_3[index & 3];				\
259       break;                                                            \
260     }									\
261     case 1: {                                             		\
262       /* show_bits12(index); */						\
263       index = next32bits >> 20;						\
264       value = dct_coeff_tbl_1[index & 15];				\
265       break;                                                            \
266     }									\
267     default: { /* index == 0 */						\
268       /* show_bits16(index); */						\
269       index = next32bits >> 16;						\
270       value = dct_coeff_tbl_0[index & 255];				\
271     }}									\
272     run = value >> RUN_SHIFT;						\
273     level = (value & LEVEL_MASK) >> LEVEL_SHIFT;			\
274 									\
275     /*									\
276      * Fold these operations together to make it fast...		\
277      */									\
278     /* num_bits = (value & NUM_MASK) + 1; */				\
279     /* flush_bits(num_bits); */						\
280     /* get_bits1(value); */						\
281     /* if (value) level = -level; */					\
282 									\
283     flushed = (value & NUM_MASK) + 2;					\
284     value = next32bits >> (32-flushed);					\
285     value &= 0x1;							\
286     if (value) level = -level;						\
287 									\
288     /* Update bitstream ... */						\
289     flush_bits(flushed);						\
290     assert (flushed <= 32);						\
291   }									\
292 }
293 
294 #define DecodeDCTCoeffFirst(runval, levelval)         \
295 {                                                     \
296   DecodeDCTCoeff(dct_coeff_first, runval, levelval);  \
297 }
298 
299 #define DecodeDCTCoeffNext(runval, levelval)          \
300 {                                                     \
301   DecodeDCTCoeff(dct_coeff_next, runval, levelval);   \
302 }
303 
304 /*
305  *--------------------------------------------------------------
306  *
307  * DecodeMBAddrInc --
308  *
309  *      Huffman Decoder for macro_block_address_increment; the location
310  *      in which the result will be placed is being passed as argument.
311  *      The decoded value is obtained by doing a table lookup on
312  *      mb_addr_inc.
313  *
314  * Results:
315  *      The decoded value for macro_block_address_increment or ERROR
316  *      for unbound values will be placed in the location specified.
317  *
318  * Side effects:
319  *      Bit stream is irreversibly parsed.
320  *
321  *--------------------------------------------------------------
322  */
323 #define DecodeMBAddrInc(val)				\
324 {							\
325     unsigned int index;					\
326     show_bits11(index);					\
327     val = mb_addr_inc[index].value;			\
328     flush_bits(mb_addr_inc[index].num_bits);		\
329 }
330 
331 /*
332  *--------------------------------------------------------------
333  *
334  * DecodeMotionVectors --
335  *
336  *      Huffman Decoder for the various motion vectors, including
337  *      motion_horizontal_forward_code, motion_vertical_forward_code,
338  *      motion_horizontal_backward_code, motion_vertical_backward_code.
339  *      Location where the decoded result will be placed is being passed
340  *      as argument. The decoded values are obtained by doing a table
341  *      lookup on motion_vectors.
342  *
343  * Results:
344  *      The decoded value for the motion vector or ERROR for unbound
345  *      values will be placed in the location specified.
346  *
347  * Side effects:
348  *      Bit stream is irreversibly parsed.
349  *
350  *--------------------------------------------------------------
351  */
352 
353 #define DecodeMotionVectors(value)			\
354 {							\
355   unsigned int index;					\
356   show_bits11(index);					\
357   value = motion_vectors[index].code;			\
358   flush_bits(motion_vectors[index].num_bits);		\
359 }
360 /*
361  *--------------------------------------------------------------
362  *
363  * DecodeMBTypeB --
364  *
365  *      Huffman Decoder for macro_block_type in bidirectionally-coded
366  *      pictures;locations in which the decoded results: macroblock_quant,
367  *      macroblock_motion_forward, macro_block_motion_backward,
368  *      macroblock_pattern, macro_block_intra, will be placed are
369  *      being passed as argument. The decoded values are obtained by
370  *      doing a table lookup on mb_type_B.
371  *
372  * Results:
373  *      The various decoded values for macro_block_type in
374  *      bidirectionally-coded pictures or ERROR for unbound values will
375  *      be placed in the locations specified.
376  *
377  * Side effects:
378  *      Bit stream is irreversibly parsed.
379  *
380  *--------------------------------------------------------------
381  */
382 #define DecodeMBTypeB(quant, motion_fwd, motion_bwd, pat, intra)	\
383 {									\
384   unsigned int index;							\
385 									\
386   show_bits6(index);							\
387 									\
388   quant = mb_type_B[index].mb_quant;					\
389   motion_fwd = mb_type_B[index].mb_motion_forward;			\
390   motion_bwd = mb_type_B[index].mb_motion_backward;			\
391   pat = mb_type_B[index].mb_pattern;					\
392   intra = mb_type_B[index].mb_intra;					\
393   flush_bits(mb_type_B[index].num_bits);				\
394 }
395 /*
396  *--------------------------------------------------------------
397  *
398  * DecodeMBTypeI --
399  *
400  *      Huffman Decoder for macro_block_type in intra-coded pictures;
401  *      locations in which the decoded results: macroblock_quant,
402  *      macroblock_motion_forward, macro_block_motion_backward,
403  *      macroblock_pattern, macro_block_intra, will be placed are
404  *      being passed as argument.
405  *
406  * Results:
407  *      The various decoded values for macro_block_type in intra-coded
408  *      pictures or ERROR for unbound values will be placed in the
409  *      locations specified.
410  *
411  * Side effects:
412  *      Bit stream is irreversibly parsed.
413  *
414  *--------------------------------------------------------------
415  */
416 #define DecodeMBTypeI(quant, motion_fwd, motion_bwd, pat, intra)	\
417 {									\
418   unsigned int index;							\
419   static int quantTbl[4] = {ERROR, 1, 0, 0};				\
420 									\
421   show_bits2(index);							\
422 									\
423   motion_fwd = 0;							\
424   motion_bwd = 0;							\
425   pat = 0;								\
426   intra = 1;								\
427   quant = quantTbl[index];						\
428   if (index) {								\
429     flush_bits (1 + quant);						\
430   }									\
431 }
432 /*
433  *--------------------------------------------------------------
434  *
435  * DecodeMBTypeP --
436  *
437  *      Huffman Decoder for macro_block_type in predictive-coded pictures;
438  *      locations in which the decoded results: macroblock_quant,
439  *      macroblock_motion_forward, macro_block_motion_backward,
440  *      macroblock_pattern, macro_block_intra, will be placed are
441  *      being passed as argument. The decoded values are obtained by
442  *      doing a table lookup on mb_type_P.
443  *
444  * Results:
445  *      The various decoded values for macro_block_type in
446  *      predictive-coded pictures or ERROR for unbound values will be
447  *      placed in the locations specified.
448  *
449  * Side effects:
450  *      Bit stream is irreversibly parsed.
451  *
452  *--------------------------------------------------------------
453  */
454 #define DecodeMBTypeP(quant, motion_fwd, motion_bwd, pat, intra)	\
455 {									\
456   unsigned int index;							\
457 									\
458   show_bits6(index);							\
459 									\
460   quant = mb_type_P[index].mb_quant;					\
461   motion_fwd = mb_type_P[index].mb_motion_forward;			\
462   motion_bwd = mb_type_P[index].mb_motion_backward;			\
463   pat = mb_type_P[index].mb_pattern;					\
464   intra = mb_type_P[index].mb_intra;					\
465 									\
466   flush_bits(mb_type_P[index].num_bits);				\
467 }
468 /*
469  *--------------------------------------------------------------
470  *
471  * DecodeCBP --
472  *
473  *      Huffman Decoder for coded_block_pattern; location in which the
474  *      decoded result will be placed is being passed as argument. The
475  *      decoded values are obtained by doing a table lookup on
476  *      coded_block_pattern.
477  *
478  * Results:
479  *      The decoded value for coded_block_pattern or ERROR for unbound
480  *      values will be placed in the location specified.
481  *
482  * Side effects:
483  *      Bit stream is irreversibly parsed.
484  *
485  *--------------------------------------------------------------
486  */
487 #define DecodeCBP(coded_bp)						\
488 {									\
489   unsigned int index;							\
490 									\
491   show_bits9(index);							\
492   coded_bp = coded_block_pattern[index].cbp;				\
493   flush_bits(coded_block_pattern[index].num_bits);			\
494 }
495