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