comparison libmpeg2/slice.c @ 1:3b5f5d1c5041

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