1 /*
2  * slice.c
3  * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5  *
6  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
7  * See http://libmpeg2.sourceforge.net/ for updates.
8  *
9  * mpeg2dec is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * mpeg2dec is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 
24 #include "config.h"
25 
26 #include <inttypes.h>
27 
28 #include "mpeg2.h"
29 #include "mpeg2_internal.h"
30 #include "attributes.h"
31 
32 extern mpeg2_mc_t mpeg2_mc;
33 extern void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride);
34 extern void (* mpeg2_idct_add) (int last, int16_t * block,
35 				uint8_t * dest, int stride);
36 extern void (* mpeg2_cpu_state_save) (cpu_state_t * state);
37 extern void (* mpeg2_cpu_state_restore) (cpu_state_t * state);
38 
39 #include "vlc.h"
40 
41 static int non_linear_quantizer_scale [] = {
42      0,  1,  2,  3,  4,  5,   6,   7,
43      8, 10, 12, 14, 16, 18,  20,  22,
44     24, 28, 32, 36, 40, 44,  48,  52,
45     56, 64, 72, 80, 88, 96, 104, 112
46 };
47 
get_macroblock_modes(decoder_t * const decoder)48 static inline int get_macroblock_modes (decoder_t * const decoder)
49 {
50 #define bit_buf (decoder->bitstream_buf)
51 #define bits (decoder->bitstream_bits)
52 #define bit_ptr (decoder->bitstream_ptr)
53     int macroblock_modes;
54     const MBtab * tab;
55 
56     switch (decoder->coding_type) {
57     case I_TYPE:
58 
59 	tab = MB_I + UBITS (bit_buf, 1);
60 	DUMPBITS (bit_buf, bits, tab->len);
61 	macroblock_modes = tab->modes;
62 
63 	if ((! (decoder->frame_pred_frame_dct)) &&
64 	    (decoder->picture_structure == FRAME_PICTURE)) {
65 	    macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
66 	    DUMPBITS (bit_buf, bits, 1);
67 	}
68 
69 	return macroblock_modes;
70 
71     case P_TYPE:
72 
73 	tab = MB_P + UBITS (bit_buf, 5);
74 	DUMPBITS (bit_buf, bits, tab->len);
75 	macroblock_modes = tab->modes;
76 
77 	if (decoder->picture_structure != FRAME_PICTURE) {
78 	    if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
79 		macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
80 		DUMPBITS (bit_buf, bits, 2);
81 	    }
82 	    return macroblock_modes;
83 	} else if (decoder->frame_pred_frame_dct) {
84 	    if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
85 		macroblock_modes |= MC_FRAME;
86 	    return macroblock_modes;
87 	} else {
88 	    if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
89 		macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
90 		DUMPBITS (bit_buf, bits, 2);
91 	    }
92 	    if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
93 		macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
94 		DUMPBITS (bit_buf, bits, 1);
95 	    }
96 	    return macroblock_modes;
97 	}
98 
99     case B_TYPE:
100 
101 	tab = MB_B + UBITS (bit_buf, 6);
102 	DUMPBITS (bit_buf, bits, tab->len);
103 	macroblock_modes = tab->modes;
104 
105 	if (decoder->picture_structure != FRAME_PICTURE) {
106 	    if (! (macroblock_modes & MACROBLOCK_INTRA)) {
107 		macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
108 		DUMPBITS (bit_buf, bits, 2);
109 	    }
110 	    return macroblock_modes;
111 	} else if (decoder->frame_pred_frame_dct) {
112 	    /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
113 	    macroblock_modes |= MC_FRAME;
114 	    return macroblock_modes;
115 	} else {
116 	    if (macroblock_modes & MACROBLOCK_INTRA)
117 		goto intra;
118 	    macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
119 	    DUMPBITS (bit_buf, bits, 2);
120 	    if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
121 	    intra:
122 		macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
123 		DUMPBITS (bit_buf, bits, 1);
124 	    }
125 	    return macroblock_modes;
126 	}
127 
128     case D_TYPE:
129 
130 	DUMPBITS (bit_buf, bits, 1);
131 	return MACROBLOCK_INTRA;
132 
133     default:
134 	return 0;
135     }
136 #undef bit_buf
137 #undef bits
138 #undef bit_ptr
139 }
140 
get_quantizer_scale(decoder_t * const decoder)141 static inline int get_quantizer_scale (decoder_t * const decoder)
142 {
143 #define bit_buf (decoder->bitstream_buf)
144 #define bits (decoder->bitstream_bits)
145 #define bit_ptr (decoder->bitstream_ptr)
146 
147     int quantizer_scale_code;
148 
149     quantizer_scale_code = UBITS (bit_buf, 5);
150     DUMPBITS (bit_buf, bits, 5);
151 
152     if (decoder->q_scale_type)
153 	return non_linear_quantizer_scale [quantizer_scale_code];
154     else
155 	return quantizer_scale_code << 1;
156 #undef bit_buf
157 #undef bits
158 #undef bit_ptr
159 }
160 
get_motion_delta(decoder_t * const decoder,const int f_code)161 static inline int get_motion_delta (decoder_t * const decoder,
162 				    const int f_code)
163 {
164 #define bit_buf (decoder->bitstream_buf)
165 #define bits (decoder->bitstream_bits)
166 #define bit_ptr (decoder->bitstream_ptr)
167 
168     int delta;
169     int sign;
170     const MVtab * tab;
171 
172     if (bit_buf & 0x80000000) {
173 	DUMPBITS (bit_buf, bits, 1);
174 	return 0;
175     } else if (bit_buf >= 0x0c000000) {
176 
177 	tab = MV_4 + UBITS (bit_buf, 4);
178 	delta = (tab->delta << f_code) + 1;
179 	bits += tab->len + f_code + 1;
180 	bit_buf <<= tab->len;
181 
182 	sign = SBITS (bit_buf, 1);
183 	bit_buf <<= 1;
184 
185 	if (f_code)
186 	    delta += UBITS (bit_buf, f_code);
187 	bit_buf <<= f_code;
188 
189 	return (delta ^ sign) - sign;
190 
191     } else {
192 
193 	tab = MV_10 + UBITS (bit_buf, 10);
194 	delta = (tab->delta << f_code) + 1;
195 	bits += tab->len + 1;
196 	bit_buf <<= tab->len;
197 
198 	sign = SBITS (bit_buf, 1);
199 	bit_buf <<= 1;
200 
201 	if (f_code) {
202 	    NEEDBITS (bit_buf, bits, bit_ptr);
203 	    delta += UBITS (bit_buf, f_code);
204 	    DUMPBITS (bit_buf, bits, f_code);
205 	}
206 
207 	return (delta ^ sign) - sign;
208 
209     }
210 #undef bit_buf
211 #undef bits
212 #undef bit_ptr
213 }
214 
bound_motion_vector(const int vector,const int f_code)215 static inline int bound_motion_vector (const int vector, const int f_code)
216 {
217 #if 0
218     unsigned int limit;
219     int sign;
220 
221     limit = 16 << f_code;
222 
223     if ((unsigned int)(vector + limit) < 2 * limit)
224 	return vector;
225     else {
226 	sign = ((int32_t)vector) >> 31;
227 	return vector - ((2 * limit) ^ sign) + sign;
228     }
229 #else
230     return ((int32_t)vector << (27 - f_code)) >> (27 - f_code);
231 #endif
232 }
233 
get_dmv(decoder_t * const decoder)234 static inline int get_dmv (decoder_t * const decoder)
235 {
236 #define bit_buf (decoder->bitstream_buf)
237 #define bits (decoder->bitstream_bits)
238 #define bit_ptr (decoder->bitstream_ptr)
239 
240     const DMVtab * tab;
241 
242     tab = DMV_2 + UBITS (bit_buf, 2);
243     DUMPBITS (bit_buf, bits, tab->len);
244     return tab->dmv;
245 #undef bit_buf
246 #undef bits
247 #undef bit_ptr
248 }
249 
get_coded_block_pattern(decoder_t * const decoder)250 static inline int get_coded_block_pattern (decoder_t * const decoder)
251 {
252 #define bit_buf (decoder->bitstream_buf)
253 #define bits (decoder->bitstream_bits)
254 #define bit_ptr (decoder->bitstream_ptr)
255 
256     const CBPtab * tab;
257 
258     NEEDBITS (bit_buf, bits, bit_ptr);
259 
260     if (bit_buf >= 0x20000000) {
261 
262 	tab = CBP_7 + (UBITS (bit_buf, 7) - 16);
263 	DUMPBITS (bit_buf, bits, tab->len);
264 	return tab->cbp;
265 
266     } else {
267 
268 	tab = CBP_9 + UBITS (bit_buf, 9);
269 	DUMPBITS (bit_buf, bits, tab->len);
270 	return tab->cbp;
271     }
272 
273 #undef bit_buf
274 #undef bits
275 #undef bit_ptr
276 }
277 
get_luma_dc_dct_diff(decoder_t * const decoder)278 static inline int get_luma_dc_dct_diff (decoder_t * const decoder)
279 {
280 #define bit_buf (decoder->bitstream_buf)
281 #define bits (decoder->bitstream_bits)
282 #define bit_ptr (decoder->bitstream_ptr)
283     const DCtab * tab;
284     int size;
285     int dc_diff;
286 
287     if (bit_buf < 0xf8000000) {
288 	tab = DC_lum_5 + UBITS (bit_buf, 5);
289 	size = tab->size;
290 	if (size) {
291 	    bits += tab->len + size;
292 	    bit_buf <<= tab->len;
293 	    dc_diff =
294 		UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
295 	    bit_buf <<= size;
296 	    return dc_diff;
297 	} else {
298 	    DUMPBITS (bit_buf, bits, 3);
299 	    return 0;
300 	}
301     } else {
302 	tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0);
303 	size = tab->size;
304 	DUMPBITS (bit_buf, bits, tab->len);
305 	NEEDBITS (bit_buf, bits, bit_ptr);
306 	dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
307 	DUMPBITS (bit_buf, bits, size);
308 	return dc_diff;
309     }
310 #undef bit_buf
311 #undef bits
312 #undef bit_ptr
313 }
314 
get_chroma_dc_dct_diff(decoder_t * const decoder)315 static inline int get_chroma_dc_dct_diff (decoder_t * const decoder)
316 {
317 #define bit_buf (decoder->bitstream_buf)
318 #define bits (decoder->bitstream_bits)
319 #define bit_ptr (decoder->bitstream_ptr)
320     const DCtab * tab;
321     int size;
322     int dc_diff;
323 
324     if (bit_buf < 0xf8000000) {
325 	tab = DC_chrom_5 + UBITS (bit_buf, 5);
326 	size = tab->size;
327 	if (size) {
328 	    bits += tab->len + size;
329 	    bit_buf <<= tab->len;
330 	    dc_diff =
331 		UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
332 	    bit_buf <<= size;
333 	    return dc_diff;
334 	} else {
335 	    DUMPBITS (bit_buf, bits, 2);
336 	    return 0;
337 	}
338     } else {
339 	tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
340 	size = tab->size;
341 	DUMPBITS (bit_buf, bits, tab->len + 1);
342 	NEEDBITS (bit_buf, bits, bit_ptr);
343 	dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
344 	DUMPBITS (bit_buf, bits, size);
345 	return dc_diff;
346     }
347 #undef bit_buf
348 #undef bits
349 #undef bit_ptr
350 }
351 
352 #define SATURATE(val)					\
353 do {							\
354     if (unlikely ((uint32_t)(val + 2048) > 4095))	\
355 	val = SBITS (val, 1) ^ 2047;			\
356 } while (0)
357 
get_intra_block_B14(decoder_t * const decoder)358 static void get_intra_block_B14 (decoder_t * const decoder)
359 {
360     int i;
361     int j;
362     int val;
363     const uint8_t * scan = decoder->scan;
364     const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
365     int quantizer_scale = decoder->quantizer_scale;
366     int mismatch;
367     const DCTtab * tab;
368     uint32_t bit_buf;
369     int bits;
370     const uint8_t * bit_ptr;
371     int16_t * dest;
372 
373     dest = decoder->DCTblock;
374     i = 0;
375     mismatch = ~dest[0];
376 
377     bit_buf = decoder->bitstream_buf;
378     bits = decoder->bitstream_bits;
379     bit_ptr = decoder->bitstream_ptr;
380 
381     NEEDBITS (bit_buf, bits, bit_ptr);
382 
383     while (1) {
384 	if (bit_buf >= 0x28000000) {
385 
386 	    tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
387 
388 	    i += tab->run;
389 	    if (i >= 64)
390 		break;	/* end of block */
391 
392 	normal_code:
393 	    j = scan[i];
394 	    bit_buf <<= tab->len;
395 	    bits += tab->len + 1;
396 	    val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
397 
398 	    /* if (bitstream_get (1)) val = -val; */
399 	    val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
400 
401 	    SATURATE (val);
402 	    dest[j] = val;
403 	    mismatch ^= val;
404 
405 	    bit_buf <<= 1;
406 	    NEEDBITS (bit_buf, bits, bit_ptr);
407 
408 	    continue;
409 
410 	} else if (bit_buf >= 0x04000000) {
411 
412 	    tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
413 
414 	    i += tab->run;
415 	    if (i < 64)
416 		goto normal_code;
417 
418 	    /* escape code */
419 
420 	    i += UBITS (bit_buf << 6, 6) - 64;
421 	    if (i >= 64)
422 		break;	/* illegal, check needed to avoid buffer overflow */
423 
424 	    j = scan[i];
425 
426 	    DUMPBITS (bit_buf, bits, 12);
427 	    NEEDBITS (bit_buf, bits, bit_ptr);
428 	    val = (SBITS (bit_buf, 12) *
429 		   quantizer_scale * quant_matrix[j]) / 16;
430 
431 	    SATURATE (val);
432 	    dest[j] = val;
433 	    mismatch ^= val;
434 
435 	    DUMPBITS (bit_buf, bits, 12);
436 	    NEEDBITS (bit_buf, bits, bit_ptr);
437 
438 	    continue;
439 
440 	} else if (bit_buf >= 0x02000000) {
441 	    tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
442 	    i += tab->run;
443 	    if (i < 64)
444 		goto normal_code;
445 	} else if (bit_buf >= 0x00800000) {
446 	    tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
447 	    i += tab->run;
448 	    if (i < 64)
449 		goto normal_code;
450 	} else if (bit_buf >= 0x00200000) {
451 	    tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
452 	    i += tab->run;
453 	    if (i < 64)
454 		goto normal_code;
455 	} else {
456 	    tab = DCT_16 + UBITS (bit_buf, 16);
457 	    bit_buf <<= 16;
458 	    GETWORD (bit_buf, bits + 16, bit_ptr);
459 	    i += tab->run;
460 	    if (i < 64)
461 		goto normal_code;
462 	}
463 	break;	/* illegal, check needed to avoid buffer overflow */
464     }
465     dest[63] ^= mismatch & 1;
466     DUMPBITS (bit_buf, bits, 2);	/* dump end of block code */
467     decoder->bitstream_buf = bit_buf;
468     decoder->bitstream_bits = bits;
469     decoder->bitstream_ptr = bit_ptr;
470 }
471 
get_intra_block_B15(decoder_t * const decoder)472 static void get_intra_block_B15 (decoder_t * const decoder)
473 {
474     int i;
475     int j;
476     int val;
477     const uint8_t * scan = decoder->scan;
478     const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
479     int quantizer_scale = decoder->quantizer_scale;
480     int mismatch;
481     const DCTtab * tab;
482     uint32_t bit_buf;
483     int bits;
484     const uint8_t * bit_ptr;
485     int16_t * dest;
486 
487     dest = decoder->DCTblock;
488     i = 0;
489     mismatch = ~dest[0];
490 
491     bit_buf = decoder->bitstream_buf;
492     bits = decoder->bitstream_bits;
493     bit_ptr = decoder->bitstream_ptr;
494 
495     NEEDBITS (bit_buf, bits, bit_ptr);
496 
497     while (1) {
498 	if (bit_buf >= 0x04000000) {
499 
500 	    tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);
501 
502 	    i += tab->run;
503 	    if (i < 64) {
504 
505 	    normal_code:
506 		j = scan[i];
507 		bit_buf <<= tab->len;
508 		bits += tab->len + 1;
509 		val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
510 
511 		/* if (bitstream_get (1)) val = -val; */
512 		val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
513 
514 		SATURATE (val);
515 		dest[j] = val;
516 		mismatch ^= val;
517 
518 		bit_buf <<= 1;
519 		NEEDBITS (bit_buf, bits, bit_ptr);
520 
521 		continue;
522 
523 	    } else {
524 
525 		/* end of block. I commented out this code because if we */
526 		/* dont exit here we will still exit at the later test :) */
527 
528 		/* if (i >= 128) break;	*/	/* end of block */
529 
530 		/* escape code */
531 
532 		i += UBITS (bit_buf << 6, 6) - 64;
533 		if (i >= 64)
534 		    break;	/* illegal, check against buffer overflow */
535 
536 		j = scan[i];
537 
538 		DUMPBITS (bit_buf, bits, 12);
539 		NEEDBITS (bit_buf, bits, bit_ptr);
540 		val = (SBITS (bit_buf, 12) *
541 		       quantizer_scale * quant_matrix[j]) / 16;
542 
543 		SATURATE (val);
544 		dest[j] = val;
545 		mismatch ^= val;
546 
547 		DUMPBITS (bit_buf, bits, 12);
548 		NEEDBITS (bit_buf, bits, bit_ptr);
549 
550 		continue;
551 
552 	    }
553 	} else if (bit_buf >= 0x02000000) {
554 	    tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
555 	    i += tab->run;
556 	    if (i < 64)
557 		goto normal_code;
558 	} else if (bit_buf >= 0x00800000) {
559 	    tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
560 	    i += tab->run;
561 	    if (i < 64)
562 		goto normal_code;
563 	} else if (bit_buf >= 0x00200000) {
564 	    tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
565 	    i += tab->run;
566 	    if (i < 64)
567 		goto normal_code;
568 	} else {
569 	    tab = DCT_16 + UBITS (bit_buf, 16);
570 	    bit_buf <<= 16;
571 	    GETWORD (bit_buf, bits + 16, bit_ptr);
572 	    i += tab->run;
573 	    if (i < 64)
574 		goto normal_code;
575 	}
576 	break;	/* illegal, check needed to avoid buffer overflow */
577     }
578     dest[63] ^= mismatch & 1;
579     DUMPBITS (bit_buf, bits, 4);	/* dump end of block code */
580     decoder->bitstream_buf = bit_buf;
581     decoder->bitstream_bits = bits;
582     decoder->bitstream_ptr = bit_ptr;
583 }
584 
get_non_intra_block(decoder_t * const decoder)585 static int get_non_intra_block (decoder_t * const decoder)
586 {
587     int i;
588     int j;
589     int val;
590     const uint8_t * scan = decoder->scan;
591     const uint8_t * quant_matrix = decoder->non_intra_quantizer_matrix;
592     int quantizer_scale = decoder->quantizer_scale;
593     int mismatch;
594     const DCTtab * tab;
595     uint32_t bit_buf;
596     int bits;
597     const uint8_t * bit_ptr;
598     int16_t * dest;
599 
600     i = -1;
601     mismatch = 1;
602     dest = decoder->DCTblock;
603 
604     bit_buf = decoder->bitstream_buf;
605     bits = decoder->bitstream_bits;
606     bit_ptr = decoder->bitstream_ptr;
607 
608     NEEDBITS (bit_buf, bits, bit_ptr);
609     if (bit_buf >= 0x28000000) {
610 	tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
611 	goto entry_1;
612     } else
613 	goto entry_2;
614 
615     while (1) {
616 	if (bit_buf >= 0x28000000) {
617 
618 	    tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
619 
620 	entry_1:
621 	    i += tab->run;
622 	    if (i >= 64)
623 		break;	/* end of block */
624 
625 	normal_code:
626 	    j = scan[i];
627 	    bit_buf <<= tab->len;
628 	    bits += tab->len + 1;
629 	    val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
630 
631 	    /* if (bitstream_get (1)) val = -val; */
632 	    val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
633 
634 	    SATURATE (val);
635 	    dest[j] = val;
636 	    mismatch ^= val;
637 
638 	    bit_buf <<= 1;
639 	    NEEDBITS (bit_buf, bits, bit_ptr);
640 
641 	    continue;
642 
643 	}
644 
645     entry_2:
646 	if (bit_buf >= 0x04000000) {
647 
648 	    tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
649 
650 	    i += tab->run;
651 	    if (i < 64)
652 		goto normal_code;
653 
654 	    /* escape code */
655 
656 	    i += UBITS (bit_buf << 6, 6) - 64;
657 	    if (i >= 64)
658 		break;	/* illegal, check needed to avoid buffer overflow */
659 
660 	    j = scan[i];
661 
662 	    DUMPBITS (bit_buf, bits, 12);
663 	    NEEDBITS (bit_buf, bits, bit_ptr);
664 	    val = 2 * (SBITS (bit_buf, 12) + SBITS (bit_buf, 1)) + 1;
665 	    val = (val * quantizer_scale * quant_matrix[j]) / 32;
666 
667 	    SATURATE (val);
668 	    dest[j] = val;
669 	    mismatch ^= val;
670 
671 	    DUMPBITS (bit_buf, bits, 12);
672 	    NEEDBITS (bit_buf, bits, bit_ptr);
673 
674 	    continue;
675 
676 	} else if (bit_buf >= 0x02000000) {
677 	    tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
678 	    i += tab->run;
679 	    if (i < 64)
680 		goto normal_code;
681 	} else if (bit_buf >= 0x00800000) {
682 	    tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
683 	    i += tab->run;
684 	    if (i < 64)
685 		goto normal_code;
686 	} else if (bit_buf >= 0x00200000) {
687 	    tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
688 	    i += tab->run;
689 	    if (i < 64)
690 		goto normal_code;
691 	} else {
692 	    tab = DCT_16 + UBITS (bit_buf, 16);
693 	    bit_buf <<= 16;
694 	    GETWORD (bit_buf, bits + 16, bit_ptr);
695 	    i += tab->run;
696 	    if (i < 64)
697 		goto normal_code;
698 	}
699 	break;	/* illegal, check needed to avoid buffer overflow */
700     }
701     dest[63] ^= mismatch & 1;
702     DUMPBITS (bit_buf, bits, 2);	/* dump end of block code */
703     decoder->bitstream_buf = bit_buf;
704     decoder->bitstream_bits = bits;
705     decoder->bitstream_ptr = bit_ptr;
706     return i;
707 }
708 
get_mpeg1_intra_block(decoder_t * const decoder)709 static void get_mpeg1_intra_block (decoder_t * const decoder)
710 {
711     int i;
712     int j;
713     int val;
714     const uint8_t * scan = decoder->scan;
715     const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
716     int quantizer_scale = decoder->quantizer_scale;
717     const DCTtab * tab;
718     uint32_t bit_buf;
719     int bits;
720     const uint8_t * bit_ptr;
721     int16_t * dest;
722 
723     i = 0;
724     dest = decoder->DCTblock;
725 
726     bit_buf = decoder->bitstream_buf;
727     bits = decoder->bitstream_bits;
728     bit_ptr = decoder->bitstream_ptr;
729 
730     NEEDBITS (bit_buf, bits, bit_ptr);
731 
732     while (1) {
733 	if (bit_buf >= 0x28000000) {
734 
735 	    tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
736 
737 	    i += tab->run;
738 	    if (i >= 64)
739 		break;	/* end of block */
740 
741 	normal_code:
742 	    j = scan[i];
743 	    bit_buf <<= tab->len;
744 	    bits += tab->len + 1;
745 	    val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
746 
747 	    /* oddification */
748 	    val = (val - 1) | 1;
749 
750 	    /* if (bitstream_get (1)) val = -val; */
751 	    val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
752 
753 	    SATURATE (val);
754 	    dest[j] = val;
755 
756 	    bit_buf <<= 1;
757 	    NEEDBITS (bit_buf, bits, bit_ptr);
758 
759 	    continue;
760 
761 	} else if (bit_buf >= 0x04000000) {
762 
763 	    tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
764 
765 	    i += tab->run;
766 	    if (i < 64)
767 		goto normal_code;
768 
769 	    /* escape code */
770 
771 	    i += UBITS (bit_buf << 6, 6) - 64;
772 	    if (i >= 64)
773 		break;	/* illegal, check needed to avoid buffer overflow */
774 
775 	    j = scan[i];
776 
777 	    DUMPBITS (bit_buf, bits, 12);
778 	    NEEDBITS (bit_buf, bits, bit_ptr);
779 	    val = SBITS (bit_buf, 8);
780 	    if (! (val & 0x7f)) {
781 		DUMPBITS (bit_buf, bits, 8);
782 		val = UBITS (bit_buf, 8) + 2 * val;
783 	    }
784 	    val = (val * quantizer_scale * quant_matrix[j]) / 16;
785 
786 	    /* oddification */
787 	    val = (val + ~SBITS (val, 1)) | 1;
788 
789 	    SATURATE (val);
790 	    dest[j] = val;
791 
792 	    DUMPBITS (bit_buf, bits, 8);
793 	    NEEDBITS (bit_buf, bits, bit_ptr);
794 
795 	    continue;
796 
797 	} else if (bit_buf >= 0x02000000) {
798 	    tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
799 	    i += tab->run;
800 	    if (i < 64)
801 		goto normal_code;
802 	} else if (bit_buf >= 0x00800000) {
803 	    tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
804 	    i += tab->run;
805 	    if (i < 64)
806 		goto normal_code;
807 	} else if (bit_buf >= 0x00200000) {
808 	    tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
809 	    i += tab->run;
810 	    if (i < 64)
811 		goto normal_code;
812 	} else {
813 	    tab = DCT_16 + UBITS (bit_buf, 16);
814 	    bit_buf <<= 16;
815 	    GETWORD (bit_buf, bits + 16, bit_ptr);
816 	    i += tab->run;
817 	    if (i < 64)
818 		goto normal_code;
819 	}
820 	break;	/* illegal, check needed to avoid buffer overflow */
821     }
822     DUMPBITS (bit_buf, bits, 2);	/* dump end of block code */
823     decoder->bitstream_buf = bit_buf;
824     decoder->bitstream_bits = bits;
825     decoder->bitstream_ptr = bit_ptr;
826 }
827 
get_mpeg1_non_intra_block(decoder_t * const decoder)828 static int get_mpeg1_non_intra_block (decoder_t * const decoder)
829 {
830     int i;
831     int j;
832     int val;
833     const uint8_t * scan = decoder->scan;
834     const uint8_t * quant_matrix = decoder->non_intra_quantizer_matrix;
835     int quantizer_scale = decoder->quantizer_scale;
836     const DCTtab * tab;
837     uint32_t bit_buf;
838     int bits;
839     const uint8_t * bit_ptr;
840     int16_t * dest;
841 
842     i = -1;
843     dest = decoder->DCTblock;
844 
845     bit_buf = decoder->bitstream_buf;
846     bits = decoder->bitstream_bits;
847     bit_ptr = decoder->bitstream_ptr;
848 
849     NEEDBITS (bit_buf, bits, bit_ptr);
850     if (bit_buf >= 0x28000000) {
851 	tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
852 	goto entry_1;
853     } else
854 	goto entry_2;
855 
856     while (1) {
857 	if (bit_buf >= 0x28000000) {
858 
859 	    tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
860 
861 	entry_1:
862 	    i += tab->run;
863 	    if (i >= 64)
864 		break;	/* end of block */
865 
866 	normal_code:
867 	    j = scan[i];
868 	    bit_buf <<= tab->len;
869 	    bits += tab->len + 1;
870 	    val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
871 
872 	    /* oddification */
873 	    val = (val - 1) | 1;
874 
875 	    /* if (bitstream_get (1)) val = -val; */
876 	    val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
877 
878 	    SATURATE (val);
879 	    dest[j] = val;
880 
881 	    bit_buf <<= 1;
882 	    NEEDBITS (bit_buf, bits, bit_ptr);
883 
884 	    continue;
885 
886 	}
887 
888     entry_2:
889 	if (bit_buf >= 0x04000000) {
890 
891 	    tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
892 
893 	    i += tab->run;
894 	    if (i < 64)
895 		goto normal_code;
896 
897 	    /* escape code */
898 
899 	    i += UBITS (bit_buf << 6, 6) - 64;
900 	    if (i >= 64)
901 		break;	/* illegal, check needed to avoid buffer overflow */
902 
903 	    j = scan[i];
904 
905 	    DUMPBITS (bit_buf, bits, 12);
906 	    NEEDBITS (bit_buf, bits, bit_ptr);
907 	    val = SBITS (bit_buf, 8);
908 	    if (! (val & 0x7f)) {
909 		DUMPBITS (bit_buf, bits, 8);
910 		val = UBITS (bit_buf, 8) + 2 * val;
911 	    }
912 	    val = 2 * (val + SBITS (val, 1)) + 1;
913 	    val = (val * quantizer_scale * quant_matrix[j]) / 32;
914 
915 	    /* oddification */
916 	    val = (val + ~SBITS (val, 1)) | 1;
917 
918 	    SATURATE (val);
919 	    dest[j] = val;
920 
921 	    DUMPBITS (bit_buf, bits, 8);
922 	    NEEDBITS (bit_buf, bits, bit_ptr);
923 
924 	    continue;
925 
926 	} else if (bit_buf >= 0x02000000) {
927 	    tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
928 	    i += tab->run;
929 	    if (i < 64)
930 		goto normal_code;
931 	} else if (bit_buf >= 0x00800000) {
932 	    tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
933 	    i += tab->run;
934 	    if (i < 64)
935 		goto normal_code;
936 	} else if (bit_buf >= 0x00200000) {
937 	    tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
938 	    i += tab->run;
939 	    if (i < 64)
940 		goto normal_code;
941 	} else {
942 	    tab = DCT_16 + UBITS (bit_buf, 16);
943 	    bit_buf <<= 16;
944 	    GETWORD (bit_buf, bits + 16, bit_ptr);
945 	    i += tab->run;
946 	    if (i < 64)
947 		goto normal_code;
948 	}
949 	break;	/* illegal, check needed to avoid buffer overflow */
950     }
951     DUMPBITS (bit_buf, bits, 2);	/* dump end of block code */
952     decoder->bitstream_buf = bit_buf;
953     decoder->bitstream_bits = bits;
954     decoder->bitstream_ptr = bit_ptr;
955     return i;
956 }
957 
slice_intra_DCT(decoder_t * const decoder,const int cc,uint8_t * const dest,const int stride)958 static inline void slice_intra_DCT (decoder_t * const decoder, const int cc,
959 				    uint8_t * const dest, const int stride)
960 {
961 #define bit_buf (decoder->bitstream_buf)
962 #define bits (decoder->bitstream_bits)
963 #define bit_ptr (decoder->bitstream_ptr)
964     NEEDBITS (bit_buf, bits, bit_ptr);
965     /* Get the intra DC coefficient and inverse quantize it */
966     if (cc == 0)
967 	decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder);
968     else
969 	decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff (decoder);
970     decoder->DCTblock[0] =
971 	decoder->dc_dct_pred[cc] << (3 - decoder->intra_dc_precision);
972 
973     if (decoder->mpeg1) {
974 	if (decoder->coding_type != D_TYPE)
975 	    get_mpeg1_intra_block (decoder);
976     } else if (decoder->intra_vlc_format)
977 	get_intra_block_B15 (decoder);
978     else
979 	get_intra_block_B14 (decoder);
980     mpeg2_idct_copy (decoder->DCTblock, dest, stride);
981 #undef bit_buf
982 #undef bits
983 #undef bit_ptr
984 }
985 
slice_non_intra_DCT(decoder_t * const decoder,uint8_t * const dest,const int stride)986 static inline void slice_non_intra_DCT (decoder_t * const decoder,
987 					uint8_t * const dest, const int stride)
988 {
989     int last;
990 
991     if (decoder->mpeg1)
992 	last = get_mpeg1_non_intra_block (decoder);
993     else
994 	last = get_non_intra_block (decoder);
995     mpeg2_idct_add (last, decoder->DCTblock, dest, stride);
996 }
997 
998 #define MOTION(table,ref,motion_x,motion_y,size,y)			      \
999     pos_x = 2 * decoder->offset + motion_x;				      \
1000     pos_y = 2 * decoder->v_offset + motion_y + 2 * y;			      \
1001     if ((pos_x > decoder->limit_x) || (pos_y > decoder->limit_y_ ## size))    \
1002 	return;								      \
1003     xy_half = ((pos_y & 1) << 1) | (pos_x & 1);				      \
1004     table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1005 		    ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride,   \
1006 		    decoder->stride, size);				      \
1007     motion_x /= 2;	motion_y /= 2;					      \
1008     xy_half = ((motion_y & 1) << 1) | (motion_x & 1);			      \
1009     offset = (((decoder->offset + motion_x) >> 1) +			      \
1010 	      ((((decoder->v_offset + motion_y) >> 1) + y/2) *		      \
1011 	       decoder->uv_stride));					      \
1012     table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride +	      \
1013 		      (decoder->offset >> 1), ref[1] + offset,		      \
1014 		      decoder->uv_stride, size/2);			      \
1015     table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride +	      \
1016 		      (decoder->offset >> 1), ref[2] + offset,		      \
1017 		      decoder->uv_stride, size/2)
1018 
1019 #define MOTION_FIELD(table,ref,motion_x,motion_y,dest_field,op,src_field)     \
1020     pos_x = 2 * decoder->offset + motion_x;				      \
1021     pos_y = decoder->v_offset + motion_y;				      \
1022     if ((pos_x > decoder->limit_x) || (pos_y > decoder->limit_y))	      \
1023 	return;								      \
1024     xy_half = ((pos_y & 1) << 1) | (pos_x & 1);				      \
1025     table[xy_half] (decoder->dest[0] + dest_field * decoder->stride +	      \
1026 		    decoder->offset,					      \
1027 		    (ref[0] + (pos_x >> 1) +				      \
1028 		     ((pos_y op) + src_field) * decoder->stride),	      \
1029 		    2 * decoder->stride, 8);				      \
1030     motion_x /= 2;	motion_y /= 2;					      \
1031     xy_half = ((motion_y & 1) << 1) | (motion_x & 1);			      \
1032     offset = (((decoder->offset + motion_x) >> 1) +			      \
1033 	      (((decoder->v_offset >> 1) + (motion_y op) + src_field) *	      \
1034 	       decoder->uv_stride));					      \
1035     table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride +    \
1036 		      (decoder->offset >> 1), ref[1] + offset,		      \
1037 		      2 * decoder->uv_stride, 4);			      \
1038     table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride +    \
1039 		      (decoder->offset >> 1), ref[2] + offset,		      \
1040 		      2 * decoder->uv_stride, 4)
1041 
motion_mp1(decoder_t * const decoder,motion_t * const motion,mpeg2_mc_fct * const * const table)1042 static void motion_mp1 (decoder_t * const decoder, motion_t * const motion,
1043 			mpeg2_mc_fct * const * const table)
1044 {
1045 #define bit_buf (decoder->bitstream_buf)
1046 #define bits (decoder->bitstream_bits)
1047 #define bit_ptr (decoder->bitstream_ptr)
1048     int motion_x, motion_y;
1049     unsigned int pos_x, pos_y, xy_half, offset;
1050 
1051     NEEDBITS (bit_buf, bits, bit_ptr);
1052     motion_x = (motion->pmv[0][0] +
1053 		(get_motion_delta (decoder,
1054 				   motion->f_code[0]) << motion->f_code[1]));
1055     motion_x = bound_motion_vector (motion_x,
1056 				    motion->f_code[0] + motion->f_code[1]);
1057     motion->pmv[0][0] = motion_x;
1058 
1059     NEEDBITS (bit_buf, bits, bit_ptr);
1060     motion_y = (motion->pmv[0][1] +
1061 		(get_motion_delta (decoder,
1062 				   motion->f_code[0]) << motion->f_code[1]));
1063     motion_y = bound_motion_vector (motion_y,
1064 				    motion->f_code[0] + motion->f_code[1]);
1065     motion->pmv[0][1] = motion_y;
1066 
1067     MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
1068 #undef bit_buf
1069 #undef bits
1070 #undef bit_ptr
1071 }
1072 
motion_fr_frame(decoder_t * const decoder,motion_t * const motion,mpeg2_mc_fct * const * const table)1073 static void motion_fr_frame (decoder_t * const decoder,
1074 			     motion_t * const motion,
1075 			     mpeg2_mc_fct * const * const table)
1076 {
1077 #define bit_buf (decoder->bitstream_buf)
1078 #define bits (decoder->bitstream_bits)
1079 #define bit_ptr (decoder->bitstream_ptr)
1080     int motion_x, motion_y;
1081     unsigned int pos_x, pos_y, xy_half, offset;
1082 
1083     NEEDBITS (bit_buf, bits, bit_ptr);
1084     motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1085 						     motion->f_code[0]);
1086     motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1087     motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
1088 
1089     NEEDBITS (bit_buf, bits, bit_ptr);
1090     motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
1091 						     motion->f_code[1]);
1092     motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
1093     motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
1094 
1095     MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
1096 #undef bit_buf
1097 #undef bits
1098 #undef bit_ptr
1099 }
1100 
motion_fr_field(decoder_t * const decoder,motion_t * const motion,mpeg2_mc_fct * const * const table)1101 static void motion_fr_field (decoder_t * const decoder,
1102 			     motion_t * const motion,
1103 			     mpeg2_mc_fct * const * const table)
1104 {
1105 #define bit_buf (decoder->bitstream_buf)
1106 #define bits (decoder->bitstream_bits)
1107 #define bit_ptr (decoder->bitstream_ptr)
1108     int motion_x, motion_y, field;
1109     unsigned int pos_x, pos_y, xy_half, offset;
1110 
1111     NEEDBITS (bit_buf, bits, bit_ptr);
1112     field = UBITS (bit_buf, 1);
1113     DUMPBITS (bit_buf, bits, 1);
1114 
1115     motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1116 						     motion->f_code[0]);
1117     motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1118     motion->pmv[0][0] = motion_x;
1119 
1120     NEEDBITS (bit_buf, bits, bit_ptr);
1121     motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta (decoder,
1122 							    motion->f_code[1]);
1123     /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */
1124     motion->pmv[0][1] = motion_y << 1;
1125 
1126     MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field);
1127 
1128     NEEDBITS (bit_buf, bits, bit_ptr);
1129     field = UBITS (bit_buf, 1);
1130     DUMPBITS (bit_buf, bits, 1);
1131 
1132     motion_x = motion->pmv[1][0] + get_motion_delta (decoder,
1133 						     motion->f_code[0]);
1134     motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1135     motion->pmv[1][0] = motion_x;
1136 
1137     NEEDBITS (bit_buf, bits, bit_ptr);
1138     motion_y = (motion->pmv[1][1] >> 1) + get_motion_delta (decoder,
1139 							    motion->f_code[1]);
1140     /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */
1141     motion->pmv[1][1] = motion_y << 1;
1142 
1143     MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field);
1144 #undef bit_buf
1145 #undef bits
1146 #undef bit_ptr
1147 }
1148 
motion_fr_dmv(decoder_t * const decoder,motion_t * const motion,mpeg2_mc_fct * const * const table)1149 static void motion_fr_dmv (decoder_t * const decoder, motion_t * const motion,
1150 			   mpeg2_mc_fct * const * const table)
1151 {
1152 #define bit_buf (decoder->bitstream_buf)
1153 #define bits (decoder->bitstream_bits)
1154 #define bit_ptr (decoder->bitstream_ptr)
1155     int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y;
1156     unsigned int pos_x, pos_y, xy_half, offset;
1157 
1158     NEEDBITS (bit_buf, bits, bit_ptr);
1159     motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1160 						     motion->f_code[0]);
1161     motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1162     motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
1163     NEEDBITS (bit_buf, bits, bit_ptr);
1164     dmv_x = get_dmv (decoder);
1165 
1166     motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta (decoder,
1167 							    motion->f_code[1]);
1168     /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */
1169     motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1;
1170     dmv_y = get_dmv (decoder);
1171 
1172     m = decoder->top_field_first ? 1 : 3;
1173     other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x;
1174     other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1;
1175     MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0);
1176 
1177     m = decoder->top_field_first ? 3 : 1;
1178     other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x;
1179     other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1;
1180     MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);
1181 
1182     xy_half = ((motion_y & 1) << 1) | (motion_x & 1);
1183     offset = (decoder->offset + (motion_x >> 1) +
1184 	      (decoder->v_offset + (motion_y & ~1)) * decoder->stride);
1185     mpeg2_mc.avg[xy_half]
1186 	(decoder->dest[0] + decoder->offset,
1187 	 motion->ref[0][0] + offset, 2 * decoder->stride, 8);
1188     mpeg2_mc.avg[xy_half]
1189 	(decoder->dest[0] + decoder->stride + decoder->offset,
1190 	 motion->ref[0][0] + decoder->stride + offset, 2 * decoder->stride, 8);
1191     motion_x /= 2;	motion_y /= 2;
1192     xy_half = ((motion_y & 1) << 1) | (motion_x & 1);
1193     offset = (((decoder->offset + motion_x) >> 1) +
1194 	      (((decoder->v_offset >> 1) + (motion_y & ~1)) *
1195 	       decoder->uv_stride));
1196     mpeg2_mc.avg[4+xy_half]
1197 	(decoder->dest[1] + (decoder->offset >> 1),
1198 	 motion->ref[0][1] + offset, 2 * decoder->uv_stride, 4);
1199     mpeg2_mc.avg[4+xy_half]
1200 	(decoder->dest[1] + decoder->uv_stride + (decoder->offset >> 1),
1201 	 motion->ref[0][1] + decoder->uv_stride + offset,
1202 	 2 * decoder->uv_stride, 4);
1203     mpeg2_mc.avg[4+xy_half]
1204 	(decoder->dest[2] + (decoder->offset >> 1),
1205 	 motion->ref[0][2] + offset, 2 * decoder->uv_stride, 4);
1206     mpeg2_mc.avg[4+xy_half]
1207 	(decoder->dest[2] + decoder->uv_stride + (decoder->offset >> 1),
1208 	 motion->ref[0][2] + decoder->uv_stride + offset,
1209 	 2 * decoder->uv_stride, 4);
1210 #undef bit_buf
1211 #undef bits
1212 #undef bit_ptr
1213 }
1214 
motion_reuse(const decoder_t * const decoder,const motion_t * const motion,mpeg2_mc_fct * const * const table)1215 static inline void motion_reuse (const decoder_t * const decoder,
1216 				 const motion_t * const motion,
1217 				 mpeg2_mc_fct * const * const table)
1218 {
1219     int motion_x, motion_y;
1220     unsigned int pos_x, pos_y, xy_half, offset;
1221 
1222     motion_x = motion->pmv[0][0];
1223     motion_y = motion->pmv[0][1];
1224 
1225     MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
1226 }
1227 
motion_zero(const decoder_t * const decoder,const motion_t * const motion,mpeg2_mc_fct * const * const table)1228 static inline void motion_zero (const decoder_t * const decoder,
1229 				const motion_t * const motion,
1230 				mpeg2_mc_fct * const * const table)
1231 {
1232     unsigned int offset;
1233 
1234     table[0] (decoder->dest[0] + decoder->offset,
1235 	      (motion->ref[0][0] + decoder->offset +
1236 	       decoder->v_offset * decoder->stride),
1237 	      decoder->stride, 16);
1238 
1239     offset = ((decoder->offset >> 1) +
1240 	      (decoder->v_offset >> 1) * decoder->uv_stride);
1241     table[4] (decoder->dest[1] + (decoder->offset >> 1),
1242 	      motion->ref[0][1] + offset, decoder->uv_stride, 8);
1243     table[4] (decoder->dest[2] + (decoder->offset >> 1),
1244 	      motion->ref[0][2] + offset, decoder->uv_stride, 8);
1245 }
1246 
1247 /* like motion_frame, but parsing without actual motion compensation */
motion_fr_conceal(decoder_t * const decoder)1248 static void motion_fr_conceal (decoder_t * const decoder)
1249 {
1250 #define bit_buf (decoder->bitstream_buf)
1251 #define bits (decoder->bitstream_bits)
1252 #define bit_ptr (decoder->bitstream_ptr)
1253     int tmp;
1254 
1255     NEEDBITS (bit_buf, bits, bit_ptr);
1256     tmp = (decoder->f_motion.pmv[0][0] +
1257 	   get_motion_delta (decoder, decoder->f_motion.f_code[0]));
1258     tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
1259     decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
1260 
1261     NEEDBITS (bit_buf, bits, bit_ptr);
1262     tmp = (decoder->f_motion.pmv[0][1] +
1263 	   get_motion_delta (decoder, decoder->f_motion.f_code[1]));
1264     tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
1265     decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
1266 
1267     DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
1268 #undef bit_buf
1269 #undef bits
1270 #undef bit_ptr
1271 }
1272 
motion_fi_field(decoder_t * const decoder,motion_t * const motion,mpeg2_mc_fct * const * const table)1273 static void motion_fi_field (decoder_t * const decoder,
1274 			     motion_t * const motion,
1275 			     mpeg2_mc_fct * const * const table)
1276 {
1277 #define bit_buf (decoder->bitstream_buf)
1278 #define bits (decoder->bitstream_bits)
1279 #define bit_ptr (decoder->bitstream_ptr)
1280     int motion_x, motion_y;
1281     uint8_t ** ref_field;
1282     unsigned int pos_x, pos_y, xy_half, offset;
1283 
1284     NEEDBITS (bit_buf, bits, bit_ptr);
1285     ref_field = motion->ref2[UBITS (bit_buf, 1)];
1286     DUMPBITS (bit_buf, bits, 1);
1287 
1288     motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1289 						     motion->f_code[0]);
1290     motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1291     motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
1292 
1293     NEEDBITS (bit_buf, bits, bit_ptr);
1294     motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
1295 						     motion->f_code[1]);
1296     motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
1297     motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
1298 
1299     MOTION (table, ref_field, motion_x, motion_y, 16, 0);
1300 #undef bit_buf
1301 #undef bits
1302 #undef bit_ptr
1303 }
1304 
motion_fi_16x8(decoder_t * const decoder,motion_t * const motion,mpeg2_mc_fct * const * const table)1305 static void motion_fi_16x8 (decoder_t * const decoder, motion_t * const motion,
1306 			    mpeg2_mc_fct * const * const table)
1307 {
1308 #define bit_buf (decoder->bitstream_buf)
1309 #define bits (decoder->bitstream_bits)
1310 #define bit_ptr (decoder->bitstream_ptr)
1311     int motion_x, motion_y;
1312     uint8_t ** ref_field;
1313     unsigned int pos_x, pos_y, xy_half, offset;
1314 
1315     NEEDBITS (bit_buf, bits, bit_ptr);
1316     ref_field = motion->ref2[UBITS (bit_buf, 1)];
1317     DUMPBITS (bit_buf, bits, 1);
1318 
1319     motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1320 						     motion->f_code[0]);
1321     motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1322     motion->pmv[0][0] = motion_x;
1323 
1324     NEEDBITS (bit_buf, bits, bit_ptr);
1325     motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
1326 						     motion->f_code[1]);
1327     motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
1328     motion->pmv[0][1] = motion_y;
1329 
1330     MOTION (table, ref_field, motion_x, motion_y, 8, 0);
1331 
1332     NEEDBITS (bit_buf, bits, bit_ptr);
1333     ref_field = motion->ref2[UBITS (bit_buf, 1)];
1334     DUMPBITS (bit_buf, bits, 1);
1335 
1336     motion_x = motion->pmv[1][0] + get_motion_delta (decoder,
1337 						     motion->f_code[0]);
1338     motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1339     motion->pmv[1][0] = motion_x;
1340 
1341     NEEDBITS (bit_buf, bits, bit_ptr);
1342     motion_y = motion->pmv[1][1] + get_motion_delta (decoder,
1343 						     motion->f_code[1]);
1344     motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
1345     motion->pmv[1][1] = motion_y;
1346 
1347     MOTION (table, ref_field, motion_x, motion_y, 8, 8);
1348 #undef bit_buf
1349 #undef bits
1350 #undef bit_ptr
1351 }
1352 
motion_fi_dmv(decoder_t * const decoder,motion_t * const motion,mpeg2_mc_fct * const * const table)1353 static void motion_fi_dmv (decoder_t * const decoder, motion_t * const motion,
1354 			   mpeg2_mc_fct * const * const table)
1355 {
1356 #define bit_buf (decoder->bitstream_buf)
1357 #define bits (decoder->bitstream_bits)
1358 #define bit_ptr (decoder->bitstream_ptr)
1359     int motion_x, motion_y, other_x, other_y;
1360     unsigned int pos_x, pos_y, xy_half, offset;
1361 
1362     NEEDBITS (bit_buf, bits, bit_ptr);
1363     motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1364 						     motion->f_code[0]);
1365     motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1366     motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
1367     NEEDBITS (bit_buf, bits, bit_ptr);
1368     other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder);
1369 
1370     motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
1371 						     motion->f_code[1]);
1372     motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
1373     motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
1374     other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) +
1375 	       decoder->dmv_offset);
1376 
1377     MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0);
1378     MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0);
1379 #undef bit_buf
1380 #undef bits
1381 #undef bit_ptr
1382 }
1383 
motion_fi_conceal(decoder_t * const decoder)1384 static void motion_fi_conceal (decoder_t * const decoder)
1385 {
1386 #define bit_buf (decoder->bitstream_buf)
1387 #define bits (decoder->bitstream_bits)
1388 #define bit_ptr (decoder->bitstream_ptr)
1389     int tmp;
1390 
1391     NEEDBITS (bit_buf, bits, bit_ptr);
1392     DUMPBITS (bit_buf, bits, 1); /* remove field_select */
1393 
1394     tmp = (decoder->f_motion.pmv[0][0] +
1395 	   get_motion_delta (decoder, decoder->f_motion.f_code[0]));
1396     tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
1397     decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
1398 
1399     NEEDBITS (bit_buf, bits, bit_ptr);
1400     tmp = (decoder->f_motion.pmv[0][1] +
1401 	   get_motion_delta (decoder, decoder->f_motion.f_code[1]));
1402     tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
1403     decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
1404 
1405     DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
1406 #undef bit_buf
1407 #undef bits
1408 #undef bit_ptr
1409 }
1410 
1411 #define MOTION_CALL(routine,direction)				\
1412 do {								\
1413     if ((direction) & MACROBLOCK_MOTION_FORWARD)		\
1414 	routine (decoder, &(decoder->f_motion), mpeg2_mc.put);	\
1415     if ((direction) & MACROBLOCK_MOTION_BACKWARD)		\
1416 	routine (decoder, &(decoder->b_motion),			\
1417 		 ((direction) & MACROBLOCK_MOTION_FORWARD ?	\
1418 		  mpeg2_mc.avg : mpeg2_mc.put));		\
1419 } while (0)
1420 
1421 #define NEXT_MACROBLOCK							\
1422 do {									\
1423     decoder->offset += 16;						\
1424     if (decoder->offset == decoder->width) {				\
1425 	do { /* just so we can use the break statement */		\
1426 	    if (decoder->convert) {					\
1427 		decoder->convert (decoder->fbuf_id, decoder->dest,	\
1428 				  decoder->v_offset);			\
1429 		if (decoder->coding_type == B_TYPE)			\
1430 		    break;						\
1431 	    }								\
1432 	    decoder->dest[0] += 16 * decoder->stride;			\
1433 	    decoder->dest[1] += 4 * decoder->stride;			\
1434 	    decoder->dest[2] += 4 * decoder->stride;			\
1435 	} while (0);							\
1436 	decoder->v_offset += 16;					\
1437 	if (decoder->v_offset > decoder->limit_y) {			\
1438 	    if (mpeg2_cpu_state_restore)				\
1439 		mpeg2_cpu_state_restore (&cpu_state);			\
1440 	    return;							\
1441 	}								\
1442 	decoder->offset = 0;						\
1443     }									\
1444 } while (0)
1445 
mpeg2_init_fbuf(decoder_t * decoder,uint8_t * current_fbuf[3],uint8_t * forward_fbuf[3],uint8_t * backward_fbuf[3])1446 void mpeg2_init_fbuf (decoder_t * decoder, uint8_t * current_fbuf[3],
1447 		      uint8_t * forward_fbuf[3], uint8_t * backward_fbuf[3])
1448 {
1449     int offset, stride, height, bottom_field;
1450 
1451     stride = decoder->width;
1452     bottom_field = (decoder->picture_structure == BOTTOM_FIELD);
1453     offset = bottom_field ? stride : 0;
1454     height = decoder->height;
1455 
1456     decoder->picture_dest[0] = current_fbuf[0] + offset;
1457     decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1);
1458     decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1);
1459 
1460     decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset;
1461     decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1);
1462     decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1);
1463 
1464     decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset;
1465     decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1);
1466     decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1);
1467 
1468     if (decoder->picture_structure != FRAME_PICTURE) {
1469 	decoder->dmv_offset = bottom_field ? 1 : -1;
1470 	decoder->f_motion.ref2[0] = decoder->f_motion.ref[bottom_field];
1471 	decoder->f_motion.ref2[1] = decoder->f_motion.ref[!bottom_field];
1472 	decoder->b_motion.ref2[0] = decoder->b_motion.ref[bottom_field];
1473 	decoder->b_motion.ref2[1] = decoder->b_motion.ref[!bottom_field];
1474 	offset = stride - offset;
1475 
1476 	if (decoder->second_field && (decoder->coding_type != B_TYPE))
1477 	    forward_fbuf = current_fbuf;
1478 
1479 	decoder->f_motion.ref[1][0] = forward_fbuf[0] + offset;
1480 	decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1);
1481 	decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1);
1482 
1483 	decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset;
1484 	decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1);
1485 	decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1);
1486 
1487 	stride <<= 1;
1488 	height >>= 1;
1489     }
1490 
1491     decoder->stride = stride;
1492     decoder->uv_stride = stride >> 1;
1493     decoder->limit_x = 2 * decoder->width - 32;
1494     decoder->limit_y_16 = 2 * height - 32;
1495     decoder->limit_y_8 = 2 * height - 16;
1496     decoder->limit_y = height - 16;
1497 }
1498 
slice_init(decoder_t * const decoder,int code)1499 static inline int slice_init (decoder_t * const decoder, int code)
1500 {
1501 #define bit_buf (decoder->bitstream_buf)
1502 #define bits (decoder->bitstream_bits)
1503 #define bit_ptr (decoder->bitstream_ptr)
1504     int offset;
1505     const MBAtab * mba;
1506 
1507     decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
1508 	decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
1509 
1510     decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
1511     decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
1512     decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
1513     decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
1514 
1515     if (decoder->vertical_position_extension) {
1516 	code += UBITS (bit_buf, 3) << 7;
1517 	DUMPBITS (bit_buf, bits, 3);
1518     }
1519     decoder->v_offset = (code - 1) * 16;
1520     offset = 0;
1521     if (!(decoder->convert) || decoder->coding_type != B_TYPE)
1522 	offset = (code - 1) * decoder->stride * 4;
1523 
1524     decoder->dest[0] = decoder->picture_dest[0] + offset * 4;
1525     decoder->dest[1] = decoder->picture_dest[1] + offset;
1526     decoder->dest[2] = decoder->picture_dest[2] + offset;
1527 
1528     decoder->quantizer_scale = get_quantizer_scale (decoder);
1529 
1530     /* ignore intra_slice and all the extra data */
1531     while (bit_buf & 0x80000000) {
1532 	DUMPBITS (bit_buf, bits, 9);
1533 	NEEDBITS (bit_buf, bits, bit_ptr);
1534     }
1535 
1536     /* decode initial macroblock address increment */
1537     offset = 0;
1538     while (1) {
1539 	if (bit_buf >= 0x08000000) {
1540 	    mba = MBA_5 + (UBITS (bit_buf, 6) - 2);
1541 	    break;
1542 	} else if (bit_buf >= 0x01800000) {
1543 	    mba = MBA_11 + (UBITS (bit_buf, 12) - 24);
1544 	    break;
1545 	} else switch (UBITS (bit_buf, 12)) {
1546 	case 8:		/* macroblock_escape */
1547 	    offset += 33;
1548 	    DUMPBITS (bit_buf, bits, 11);
1549 	    NEEDBITS (bit_buf, bits, bit_ptr);
1550 	    continue;
1551 	case 15:	/* macroblock_stuffing (MPEG1 only) */
1552 	    bit_buf &= 0xfffff;
1553 	    DUMPBITS (bit_buf, bits, 11);
1554 	    NEEDBITS (bit_buf, bits, bit_ptr);
1555 	    continue;
1556 	default:	/* error */
1557 	    return 1;
1558 	}
1559     }
1560     DUMPBITS (bit_buf, bits, mba->len + 1);
1561     decoder->offset = (offset + mba->mba) << 4;
1562 
1563     while (decoder->offset - decoder->width >= 0) {
1564 	decoder->offset -= decoder->width;
1565 	if (!(decoder->convert) || decoder->coding_type != B_TYPE) {
1566 	    decoder->dest[0] += 16 * decoder->stride;
1567 	    decoder->dest[1] += 4 * decoder->stride;
1568 	    decoder->dest[2] += 4 * decoder->stride;
1569 	}
1570 	decoder->v_offset += 16;
1571     }
1572     if (decoder->v_offset > decoder->limit_y)
1573 	return 1;
1574 
1575     return 0;
1576 #undef bit_buf
1577 #undef bits
1578 #undef bit_ptr
1579 }
1580 
mpeg2_slice(decoder_t * const decoder,const int code,const uint8_t * const buffer)1581 void mpeg2_slice (decoder_t * const decoder, const int code,
1582 		  const uint8_t * const buffer)
1583 {
1584 #define bit_buf (decoder->bitstream_buf)
1585 #define bits (decoder->bitstream_bits)
1586 #define bit_ptr (decoder->bitstream_ptr)
1587     cpu_state_t cpu_state;
1588 
1589     bitstream_init (decoder, buffer);
1590 
1591     if (slice_init (decoder, code))
1592 	return;
1593 
1594     if (mpeg2_cpu_state_save)
1595 	mpeg2_cpu_state_save (&cpu_state);
1596 
1597     while (1) {
1598 	int macroblock_modes;
1599 	int mba_inc;
1600 	const MBAtab * mba;
1601 
1602 	NEEDBITS (bit_buf, bits, bit_ptr);
1603 
1604 	macroblock_modes = get_macroblock_modes (decoder);
1605 
1606 	/* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1607 	if (macroblock_modes & MACROBLOCK_QUANT)
1608 	    decoder->quantizer_scale = get_quantizer_scale (decoder);
1609 
1610 	if (macroblock_modes & MACROBLOCK_INTRA) {
1611 
1612 	    int DCT_offset, DCT_stride;
1613 	    int offset;
1614 	    uint8_t * dest_y;
1615 
1616 	    if (decoder->concealment_motion_vectors) {
1617 		if (decoder->picture_structure == FRAME_PICTURE)
1618 		    motion_fr_conceal (decoder);
1619 		else
1620 		    motion_fi_conceal (decoder);
1621 	    } else {
1622 		decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
1623 		decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
1624 		decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
1625 		decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
1626 	    }
1627 
1628 	    if (macroblock_modes & DCT_TYPE_INTERLACED) {
1629 		DCT_offset = decoder->stride;
1630 		DCT_stride = decoder->stride * 2;
1631 	    } else {
1632 		DCT_offset = decoder->stride * 8;
1633 		DCT_stride = decoder->stride;
1634 	    }
1635 
1636 	    offset = decoder->offset;
1637 	    dest_y = decoder->dest[0] + offset;
1638 	    slice_intra_DCT (decoder, 0, dest_y, DCT_stride);
1639 	    slice_intra_DCT (decoder, 0, dest_y + 8, DCT_stride);
1640 	    slice_intra_DCT (decoder, 0, dest_y + DCT_offset, DCT_stride);
1641 	    slice_intra_DCT (decoder, 0, dest_y + DCT_offset + 8, DCT_stride);
1642 	    slice_intra_DCT (decoder, 1, decoder->dest[1] + (offset >> 1),
1643 			     decoder->uv_stride);
1644 	    slice_intra_DCT (decoder, 2, decoder->dest[2] + (offset >> 1),
1645 			     decoder->uv_stride);
1646 
1647 	    if (decoder->coding_type == D_TYPE) {
1648 		NEEDBITS (bit_buf, bits, bit_ptr);
1649 		DUMPBITS (bit_buf, bits, 1);
1650 	    }
1651 	} else {
1652 
1653 	    if (decoder->picture_structure == FRAME_PICTURE)
1654 		switch (macroblock_modes & MOTION_TYPE_MASK) {
1655 		case MC_FRAME:
1656 		    if (decoder->mpeg1)
1657 			MOTION_CALL (motion_mp1, macroblock_modes);
1658 		    else
1659 			MOTION_CALL (motion_fr_frame, macroblock_modes);
1660 		    break;
1661 
1662 		case MC_FIELD:
1663 		    MOTION_CALL (motion_fr_field, macroblock_modes);
1664 		    break;
1665 
1666 		case MC_DMV:
1667 		    MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD);
1668 		    break;
1669 
1670 		case 0:
1671 		    /* non-intra mb without forward mv in a P picture */
1672 		    decoder->f_motion.pmv[0][0] = 0;
1673 		    decoder->f_motion.pmv[0][1] = 0;
1674 		    decoder->f_motion.pmv[1][0] = 0;
1675 		    decoder->f_motion.pmv[1][1] = 0;
1676 		    MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD);
1677 		    break;
1678 		}
1679 	    else
1680 		switch (macroblock_modes & MOTION_TYPE_MASK) {
1681 		case MC_FIELD:
1682 		    MOTION_CALL (motion_fi_field, macroblock_modes);
1683 		    break;
1684 
1685 		case MC_16X8:
1686 		    MOTION_CALL (motion_fi_16x8, macroblock_modes);
1687 		    break;
1688 
1689 		case MC_DMV:
1690 		    MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD);
1691 		    break;
1692 
1693 		case 0:
1694 		    /* non-intra mb without forward mv in a P picture */
1695 		    decoder->f_motion.pmv[0][0] = 0;
1696 		    decoder->f_motion.pmv[0][1] = 0;
1697 		    decoder->f_motion.pmv[1][0] = 0;
1698 		    decoder->f_motion.pmv[1][1] = 0;
1699 		    MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD);
1700 		    break;
1701 		}
1702 
1703 	    if (macroblock_modes & MACROBLOCK_PATTERN) {
1704 		int coded_block_pattern;
1705 		int DCT_offset, DCT_stride;
1706 		int offset;
1707 		uint8_t * dest_y;
1708 
1709 		if (macroblock_modes & DCT_TYPE_INTERLACED) {
1710 		    DCT_offset = decoder->stride;
1711 		    DCT_stride = decoder->stride * 2;
1712 		} else {
1713 		    DCT_offset = decoder->stride * 8;
1714 		    DCT_stride = decoder->stride;
1715 		}
1716 
1717 		coded_block_pattern = get_coded_block_pattern (decoder);
1718 
1719 		offset = decoder->offset;
1720 		dest_y = decoder->dest[0] + offset;
1721 		if (coded_block_pattern & 0x20)
1722 		    slice_non_intra_DCT (decoder, dest_y, DCT_stride);
1723 		if (coded_block_pattern & 0x10)
1724 		    slice_non_intra_DCT (decoder, dest_y + 8, DCT_stride);
1725 		if (coded_block_pattern & 0x08)
1726 		    slice_non_intra_DCT (decoder, dest_y + DCT_offset,
1727 					 DCT_stride);
1728 		if (coded_block_pattern & 0x04)
1729 		    slice_non_intra_DCT (decoder, dest_y + DCT_offset + 8,
1730 					 DCT_stride);
1731 		if (coded_block_pattern & 0x2)
1732 		    slice_non_intra_DCT (decoder,
1733 					 decoder->dest[1] + (offset >> 1),
1734 					 decoder->uv_stride);
1735 		if (coded_block_pattern & 0x1)
1736 		    slice_non_intra_DCT (decoder,
1737 					 decoder->dest[2] + (offset >> 1),
1738 					 decoder->uv_stride);
1739 	    }
1740 
1741 	    decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
1742 		decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
1743 	}
1744 
1745 	NEXT_MACROBLOCK;
1746 
1747 	NEEDBITS (bit_buf, bits, bit_ptr);
1748 	mba_inc = 0;
1749 	while (1) {
1750 	    if (bit_buf >= 0x10000000) {
1751 		mba = MBA_5 + (UBITS (bit_buf, 5) - 2);
1752 		break;
1753 	    } else if (bit_buf >= 0x03000000) {
1754 		mba = MBA_11 + (UBITS (bit_buf, 11) - 24);
1755 		break;
1756 	    } else switch (UBITS (bit_buf, 11)) {
1757 	    case 8:		/* macroblock_escape */
1758 		mba_inc += 33;
1759 		/* pass through */
1760 	    case 15:	/* macroblock_stuffing (MPEG1 only) */
1761 		DUMPBITS (bit_buf, bits, 11);
1762 		NEEDBITS (bit_buf, bits, bit_ptr);
1763 		continue;
1764 	    default:	/* end of slice, or error */
1765 		if (mpeg2_cpu_state_restore)
1766 		    mpeg2_cpu_state_restore (&cpu_state);
1767 		return;
1768 	    }
1769 	}
1770 	DUMPBITS (bit_buf, bits, mba->len);
1771 	mba_inc += mba->mba;
1772 
1773 	if (mba_inc) {
1774 	    decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
1775 		decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
1776 
1777 	    if (decoder->coding_type == P_TYPE) {
1778 		decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
1779 		decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
1780 
1781 		do {
1782 		    MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD);
1783 		    NEXT_MACROBLOCK;
1784 		} while (--mba_inc);
1785 	    } else {
1786 		do {
1787 		    MOTION_CALL (motion_reuse, macroblock_modes);
1788 		    NEXT_MACROBLOCK;
1789 		} while (--mba_inc);
1790 	    }
1791 	}
1792     }
1793 #undef bit_buf
1794 #undef bits
1795 #undef bit_ptr
1796 }
1797