6 ** $VER: mpegdecoders.h 1.7 (17.1.97)
7 ** mpegvideo.datatype 1.7
9 ** This file contains the declarations of structures required for
12 ** Written 1996/1997 by Roland 'Gizzy' Mainz
17 #include <exec/types.h>
19 /* Include util.h for bit i/o parsing macros. */
22 /* Code for unbound values in decoding tables */
24 #define DCT_ERROR (63)
26 #define MACRO_BLOCK_STUFFING (34)
27 #define MACRO_BLOCK_ESCAPE (35)
29 /* Two types of DCT Coefficients */
30 #define DCT_COEFF_FIRST (0)
31 #define DCT_COEFF_NEXT (1)
33 /* Special values for DCT Coefficients */
34 #define END_OF_BLOCK (62)
37 /* Structure for an entry in the decoding table of
38 * macroblock_address_increment */
41 BYTE value
; /* value for macroblock_address_increment */
42 BYTE num_bits
; /* length of the Huffman code */
45 /* Decoding table for macroblock_address_increment */
46 extern mb_addr_inc_entry mb_addr_inc
[ 2048 ];
49 /* Structure for an entry in the decoding table of macroblock_type */
52 BOOL mb_quant
; /* macroblock_quant */
53 BOOL mb_motion_forward
; /* macroblock_motion_forward */
54 BOOL mb_motion_backward
; /* macroblock_motion_backward */
55 BOOL mb_pattern
; /* macroblock_pattern */
56 BOOL mb_intra
; /* macroblock_intra */
57 BYTE num_bits
; /* length of the Huffman code */
60 /* Decoding table for macroblock_type in predictive-coded pictures */
61 extern mb_type_entry mb_type_P
[ 64 ];
63 /* Decoding table for macroblock_type in bidirectionally-coded pictures */
64 extern mb_type_entry mb_type_B
[ 64 ];
67 /* Structures for an entry in the decoding table of coded_block_pattern */
70 unsigned int cbp
; /* coded_block_pattern */
71 int num_bits
; /* length of the Huffman code */
72 } coded_block_pattern_entry
;
74 /* External declaration of coded block pattern table. */
75 extern const coded_block_pattern_entry coded_block_pattern
[ 512 ];
78 /* Structure for an entry in the decoding table of motion vectors */
81 int code
; /* value for motion_horizontal_forward_code,
82 * motion_vertical_forward_code,
83 * motion_horizontal_backward_code, or
84 * motion_vertical_backward_code.
86 int num_bits
; /* length of the Huffman code */
87 } motion_vectors_entry
;
90 /* Decoding table for motion vectors */
91 extern motion_vectors_entry motion_vectors
[ 2048 ];
94 /* Structure for an entry in the decoding table of dct_dc_size */
97 unsigned int value
; /* value of dct_dc_size (luminance or chrominance) */
98 int num_bits
; /* length of the Huffman code */
101 /* External declaration of dct dc size lumiance table. */
102 extern const dct_dc_size_entry dct_dc_size_luminance
[ 128 ];
104 /* External declaration of dct dc size chrom table. */
105 extern const dct_dc_size_entry dct_dc_size_chrominance
[ 256 ];
108 /* DCT coeff tables. */
109 #define RUN_MASK (0xfc00)
110 #define LEVEL_MASK (0x03f0)
111 #define NUM_MASK (0x000f)
112 #define RUN_SHIFT (10)
113 #define LEVEL_SHIFT (4)
115 /* External declaration of dct coeff tables. */
116 extern const UWORD dct_coeff_tbl_0
[ 256 ];
117 extern const UWORD dct_coeff_tbl_1
[ 16 ];
118 extern const UWORD dct_coeff_tbl_2
[ 4 ];
119 extern const UWORD dct_coeff_tbl_3
[ 4 ];
120 extern const UWORD dct_coeff_next
[ 256 ];
121 extern const UWORD dct_coeff_first
[ 256 ];
123 #define DecodeDCTDCSizeLum( macro_val ) \
127 show_bits7( index ); \
129 macro_val = dct_dc_size_luminance[ index ] . value; \
131 flush_bits( (dct_dc_size_luminance[ index ] . num_bits) ); \
134 #define DecodeDCTDCSizeChrom( macro_val ) \
138 show_bits8( index ); \
140 macro_val = dct_dc_size_chrominance[ index ] . value; \
142 flush_bits( (dct_dc_size_chrominance[ index ] . num_bits) ); \
146 #define DecodeDCTCoeff( dct_coeff_tbl, run, level ) \
150 unsigned int value, \
155 * Grab the next 32 bits and use it to improve performance of \
156 * getting the bits to parse. Thus, calls are translated as: \
158 * show_bitsX <--> next32bits >> (32-X) \
159 * get_bitsX <--> val = next32bits >> (32-flushed-X); \
161 * next32bits &= bitMask[flushed]; \
162 * flush_bitsX <--> flushed += X; \
163 * next32bits &= bitMask[flushed]; \
166 show_bits32( next32bits ); \
168 /* show_bits8( index ); */ \
169 index = next32bits >> 24; \
173 value = dct_coeff_tbl[ index ]; \
174 run = (value & RUN_MASK) >> RUN_SHIFT; \
176 if( run == END_OF_BLOCK ) \
178 level = END_OF_BLOCK; \
182 /* num_bits = (value & NUM_MASK) + 1; */ \
183 /* flush_bits(num_bits); */ \
184 flushed = (value & NUM_MASK) + 1; \
185 next32bits &= bitMask[ flushed ]; \
187 if( run != ESCAPE ) \
189 level = (value & LEVEL_MASK) >> LEVEL_SHIFT; \
191 /* get_bits1(value); */ \
192 /* if( value ) level = -*level; */ \
193 if( next32bits >> (31 - flushed ) ) \
199 /* next32bits &= bitMask[ flushed ]; last op before update */ \
201 else /* run == ESCAPE */ \
203 /* get_bits14( temp ); */ \
204 temp = next32bits >> (18 - flushed); \
206 next32bits &= bitMask[ flushed ]; \
212 /* get_bits8( level ); */ \
213 level = next32bits >> (24 - flushed); \
215 /* next32bits &= bitMask[ flushed ]; last op before update */ \
216 assert( level >= 128 ); \
222 /* Grab sign bit */ \
223 level = ((int)(temp << 24)) >> 24; \
227 /* get_bits8( level ); */ \
228 level = next32bits >> (24 - flushed); \
230 /* next32bits &= bitMask[ flushed ]; last op before update */ \
231 level = level - 256; \
232 assert( (level <= -128) && (level >= -255) ); \
237 /* Update bitstream... */ \
238 flush_bits( flushed ); \
245 /* show_bits10( index ); */ \
246 index = next32bits >> 22; \
247 value = dct_coeff_tbl_2[ index & 3 ]; \
253 /* show_bits10( index ); */ \
254 index = next32bits >> 22; \
255 value = dct_coeff_tbl_3[index & 3]; \
259 if( index ) /* index == 1 */ \
261 /* show_bits12(index); */ \
262 index = next32bits >> 20; \
263 value = dct_coeff_tbl_1[index & 15]; \
265 else /* index == 0 */ \
267 /* show_bits16( index ); */ \
268 index = next32bits >> 16; \
269 value = dct_coeff_tbl_0[ index & 255 ]; \
274 run = (value & RUN_MASK) >> RUN_SHIFT; \
275 level = (value & LEVEL_MASK) >> LEVEL_SHIFT; \
278 * Fold these operations together to make it fast... \
280 /* num_bits = (value & NUM_MASK) + 1; */ \
281 /* flush_bits(num_bits); */ \
282 /* get_bits1(value); */ \
283 /* if (value) level = -level; */ \
285 flushed = (value & NUM_MASK) + 2; \
286 if( (next32bits >> (32-flushed)) & 0x1 ) \
289 /* Update bitstream ... */ \
290 flush_bits( flushed ); \
295 #define DecodeDCTCoeffFirst( runval, levelval ) \
297 DecodeDCTCoeff( (dct_coeff_first), (runval), (levelval) ); \
301 #define DecodeDCTCoeffNext( runval, levelval ) \
303 DecodeDCTCoeff( (dct_coeff_next), (runval), (levelval) ); \
308 *--------------------------------------------------------------
312 * Huffman Decoder for macro_block_address_increment; the location
313 * in which the result will be placed is being passed as argument.
314 * The decoded value is obtained by doing a table lookup on
318 * The decoded value for macro_block_address_increment or ERROR
319 * for unbound values will be placed in the location specified.
322 * Bit stream is irreversibly parsed.
324 *--------------------------------------------------------------
326 #define DecodeMBAddrInc( val ) \
329 show_bits11( index ); \
330 val = mb_addr_inc[ index ] . value; \
331 flush_bits( (mb_addr_inc[ index ] . num_bits) ); \
335 *--------------------------------------------------------------
337 * DecodeMotionVectors --
339 * Huffman Decoder for the various motion vectors, including
340 * motion_horizontal_forward_code, motion_vertical_forward_code,
341 * motion_horizontal_backward_code, motion_vertical_backward_code.
342 * Location where the decoded result will be placed is being passed
343 * as argument. The decoded values are obtained by doing a table
344 * lookup on motion_vectors.
347 * The decoded value for the motion vector or ERROR for unbound
348 * values will be placed in the location specified.
351 * Bit stream is irreversibly parsed.
353 *--------------------------------------------------------------
357 #define DecodeMotionVectors( value ) \
360 show_bits11( index ); \
361 value = motion_vectors[ index ] . code; \
362 flush_bits( (motion_vectors[ index ] . num_bits) ); \
367 *--------------------------------------------------------------
371 * Huffman Decoder for macro_block_type in bidirectionally-coded
372 * pictures;locations in which the decoded results: macroblock_quant,
373 * macroblock_motion_forward, macro_block_motion_backward,
374 * macroblock_pattern, macro_block_intra, will be placed are
375 * being passed as argument. The decoded values are obtained by
376 * doing a table lookup on mb_type_B.
379 * The various decoded values for macro_block_type in
380 * bidirectionally-coded pictures or ERROR for unbound values will
381 * be placed in the locations specified.
384 * Bit stream is irreversibly parsed.
386 *--------------------------------------------------------------
390 #define DecodeMBTypeB( quant, motion_fwd, motion_bwd, pat, intra ) \
394 show_bits6( index ); \
396 quant = mb_type_B[ index ] . mb_quant; \
397 motion_fwd = mb_type_B[ index ] . mb_motion_forward; \
398 motion_bwd = mb_type_B[ index ] . mb_motion_backward; \
399 pat = mb_type_B[ index ] . mb_pattern; \
400 intra = mb_type_B[ index ] . mb_intra; \
401 flush_bits( mb_type_B[ index ] . num_bits ); \
406 *--------------------------------------------------------------
410 * Huffman Decoder for macro_block_type in intra-coded pictures;
411 * locations in which the decoded results: macroblock_quant,
412 * macroblock_motion_forward, macro_block_motion_backward,
413 * macroblock_pattern, macro_block_intra, will be placed are
414 * being passed as argument.
417 * The various decoded values for macro_block_type in intra-coded
418 * pictures or ERROR for unbound values will be placed in the
419 * locations specified.
422 * Bit stream is irreversibly parsed.
424 *--------------------------------------------------------------
428 #define DecodeMBTypeI( quant, motion_fwd, motion_bwd, pat, intra ) \
431 const BOOL quantTbl[ 4 ] = { ERROR, 1, 0, 0 }; \
433 show_bits2( index ); \
439 quant = quantTbl[ index ]; \
443 flush_bits( (1 + quant) ); \
449 *--------------------------------------------------------------
453 * Huffman Decoder for macro_block_type in predictive-coded pictures;
454 * locations in which the decoded results: macroblock_quant,
455 * macroblock_motion_forward, macro_block_motion_backward,
456 * macroblock_pattern, macro_block_intra, will be placed are
457 * being passed as argument. The decoded values are obtained by
458 * doing a table lookup on mb_type_P.
461 * The various decoded values for macro_block_type in
462 * predictive-coded pictures or ERROR for unbound values will be
463 * placed in the locations specified.
466 * Bit stream is irreversibly parsed.
468 *--------------------------------------------------------------
472 #define DecodeMBTypeP( quant, motion_fwd, motion_bwd, pat, intra ) \
476 show_bits6( index ); \
478 quant = mb_type_P[ index ] . mb_quant; \
479 motion_fwd = mb_type_P[ index ] . mb_motion_forward; \
480 motion_bwd = mb_type_P[ index ] . mb_motion_backward; \
481 pat = mb_type_P[ index ] . mb_pattern; \
482 intra = mb_type_P[ index ] . mb_intra; \
484 flush_bits( mb_type_P[ index ] . num_bits ); \
489 *--------------------------------------------------------------
493 * Huffman Decoder for coded_block_pattern; location in which the
494 * decoded result will be placed is being passed as argument. The
495 * decoded values are obtained by doing a table lookup on
496 * coded_block_pattern.
499 * The decoded value for coded_block_pattern or ERROR for unbound
500 * values will be placed in the location specified.
503 * Bit stream is irreversibly parsed.
505 *--------------------------------------------------------------
509 #define DecodeCBP( coded_bp ) \
513 show_bits9( index ); \
514 coded_bp = coded_block_pattern[ index ] . cbp; \
515 flush_bits( coded_block_pattern[ index ] . num_bits ); \
519 #endif /* !MPEGDECODERS_H */