00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "config.h"
00025
00026 #include <inttypes.h>
00027
00028 #include "mpeg2.h"
00029 #include "mpeg2_internal.h"
00030 #include "attributes.h"
00031
00032 extern mpeg2_mc_t mpeg2_mc;
00033 extern void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride);
00034 extern void (* mpeg2_idct_add) (int last, int16_t * block,
00035 uint8_t * dest, int stride);
00036 extern void (* mpeg2_cpu_state_save) (cpu_state_t * state);
00037 extern void (* mpeg2_cpu_state_restore) (cpu_state_t * state);
00038
00039 #include "vlc.h"
00040
00041 static int non_linear_quantizer_scale [] = {
00042 0, 1, 2, 3, 4, 5, 6, 7,
00043 8, 10, 12, 14, 16, 18, 20, 22,
00044 24, 28, 32, 36, 40, 44, 48, 52,
00045 56, 64, 72, 80, 88, 96, 104, 112
00046 };
00047
00048 static inline int get_macroblock_modes (decoder_t * const decoder)
00049 {
00050 #define bit_buf (decoder->bitstream_buf)
00051 #define bits (decoder->bitstream_bits)
00052 #define bit_ptr (decoder->bitstream_ptr)
00053 int macroblock_modes;
00054 const MBtab * tab;
00055
00056 switch (decoder->coding_type) {
00057 case I_TYPE:
00058
00059 tab = MB_I + UBITS (bit_buf, 1);
00060 DUMPBITS (bit_buf, bits, tab->len);
00061 macroblock_modes = tab->modes;
00062
00063 if ((! (decoder->frame_pred_frame_dct)) &&
00064 (decoder->picture_structure == FRAME_PICTURE)) {
00065 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
00066 DUMPBITS (bit_buf, bits, 1);
00067 }
00068
00069 return macroblock_modes;
00070
00071 case P_TYPE:
00072
00073 tab = MB_P + UBITS (bit_buf, 5);
00074 DUMPBITS (bit_buf, bits, tab->len);
00075 macroblock_modes = tab->modes;
00076
00077 if (decoder->picture_structure != FRAME_PICTURE) {
00078 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
00079 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
00080 DUMPBITS (bit_buf, bits, 2);
00081 }
00082 return macroblock_modes;
00083 } else if (decoder->frame_pred_frame_dct) {
00084 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
00085 macroblock_modes |= MC_FRAME;
00086 return macroblock_modes;
00087 } else {
00088 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
00089 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
00090 DUMPBITS (bit_buf, bits, 2);
00091 }
00092 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
00093 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
00094 DUMPBITS (bit_buf, bits, 1);
00095 }
00096 return macroblock_modes;
00097 }
00098
00099 case B_TYPE:
00100
00101 tab = MB_B + UBITS (bit_buf, 6);
00102 DUMPBITS (bit_buf, bits, tab->len);
00103 macroblock_modes = tab->modes;
00104
00105 if (decoder->picture_structure != FRAME_PICTURE) {
00106 if (! (macroblock_modes & MACROBLOCK_INTRA)) {
00107 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
00108 DUMPBITS (bit_buf, bits, 2);
00109 }
00110 return macroblock_modes;
00111 } else if (decoder->frame_pred_frame_dct) {
00112
00113 macroblock_modes |= MC_FRAME;
00114 return macroblock_modes;
00115 } else {
00116 if (macroblock_modes & MACROBLOCK_INTRA)
00117 goto intra;
00118 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
00119 DUMPBITS (bit_buf, bits, 2);
00120 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
00121 intra:
00122 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
00123 DUMPBITS (bit_buf, bits, 1);
00124 }
00125 return macroblock_modes;
00126 }
00127
00128 case D_TYPE:
00129
00130 DUMPBITS (bit_buf, bits, 1);
00131 return MACROBLOCK_INTRA;
00132
00133 default:
00134 return 0;
00135 }
00136 #undef bit_buf
00137 #undef bits
00138 #undef bit_ptr
00139 }
00140
00141 static inline int get_quantizer_scale (decoder_t * const decoder)
00142 {
00143 #define bit_buf (decoder->bitstream_buf)
00144 #define bits (decoder->bitstream_bits)
00145 #define bit_ptr (decoder->bitstream_ptr)
00146
00147 int quantizer_scale_code;
00148
00149 quantizer_scale_code = UBITS (bit_buf, 5);
00150 DUMPBITS (bit_buf, bits, 5);
00151
00152 if (decoder->q_scale_type)
00153 return non_linear_quantizer_scale [quantizer_scale_code];
00154 else
00155 return quantizer_scale_code << 1;
00156 #undef bit_buf
00157 #undef bits
00158 #undef bit_ptr
00159 }
00160
00161 static inline int get_motion_delta (decoder_t * const decoder,
00162 const int f_code)
00163 {
00164 #define bit_buf (decoder->bitstream_buf)
00165 #define bits (decoder->bitstream_bits)
00166 #define bit_ptr (decoder->bitstream_ptr)
00167
00168 int delta;
00169 int sign;
00170 const MVtab * tab;
00171
00172 if (bit_buf & 0x80000000) {
00173 DUMPBITS (bit_buf, bits, 1);
00174 return 0;
00175 } else if (bit_buf >= 0x0c000000) {
00176
00177 tab = MV_4 + UBITS (bit_buf, 4);
00178 delta = (tab->delta << f_code) + 1;
00179 bits += tab->len + f_code + 1;
00180 bit_buf <<= tab->len;
00181
00182 sign = SBITS (bit_buf, 1);
00183 bit_buf <<= 1;
00184
00185 if (f_code)
00186 delta += UBITS (bit_buf, f_code);
00187 bit_buf <<= f_code;
00188
00189 return (delta ^ sign) - sign;
00190
00191 } else {
00192
00193 tab = MV_10 + UBITS (bit_buf, 10);
00194 delta = (tab->delta << f_code) + 1;
00195 bits += tab->len + 1;
00196 bit_buf <<= tab->len;
00197
00198 sign = SBITS (bit_buf, 1);
00199 bit_buf <<= 1;
00200
00201 if (f_code) {
00202 NEEDBITS (bit_buf, bits, bit_ptr);
00203 delta += UBITS (bit_buf, f_code);
00204 DUMPBITS (bit_buf, bits, f_code);
00205 }
00206
00207 return (delta ^ sign) - sign;
00208
00209 }
00210 #undef bit_buf
00211 #undef bits
00212 #undef bit_ptr
00213 }
00214
00215 static inline int bound_motion_vector (const int vector, const int f_code)
00216 {
00217 #if 0
00218 unsigned int limit;
00219 int sign;
00220
00221 limit = 16 << f_code;
00222
00223 if ((unsigned int)(vector + limit) < 2 * limit)
00224 return vector;
00225 else {
00226 sign = ((int32_t)vector) >> 31;
00227 return vector - ((2 * limit) ^ sign) + sign;
00228 }
00229 #else
00230 return ((int32_t)vector << (27 - f_code)) >> (27 - f_code);
00231 #endif
00232 }
00233
00234 static inline int get_dmv (decoder_t * const decoder)
00235 {
00236 #define bit_buf (decoder->bitstream_buf)
00237 #define bits (decoder->bitstream_bits)
00238 #define bit_ptr (decoder->bitstream_ptr)
00239
00240 const DMVtab * tab;
00241
00242 tab = DMV_2 + UBITS (bit_buf, 2);
00243 DUMPBITS (bit_buf, bits, tab->len);
00244 return tab->dmv;
00245 #undef bit_buf
00246 #undef bits
00247 #undef bit_ptr
00248 }
00249
00250 static inline int get_coded_block_pattern (decoder_t * const decoder)
00251 {
00252 #define bit_buf (decoder->bitstream_buf)
00253 #define bits (decoder->bitstream_bits)
00254 #define bit_ptr (decoder->bitstream_ptr)
00255
00256 const CBPtab * tab;
00257
00258 NEEDBITS (bit_buf, bits, bit_ptr);
00259
00260 if (bit_buf >= 0x20000000) {
00261
00262 tab = CBP_7 + (UBITS (bit_buf, 7) - 16);
00263 DUMPBITS (bit_buf, bits, tab->len);
00264 return tab->cbp;
00265
00266 } else {
00267
00268 tab = CBP_9 + UBITS (bit_buf, 9);
00269 DUMPBITS (bit_buf, bits, tab->len);
00270 return tab->cbp;
00271 }
00272
00273 #undef bit_buf
00274 #undef bits
00275 #undef bit_ptr
00276 }
00277
00278 static inline int get_luma_dc_dct_diff (decoder_t * const decoder)
00279 {
00280 #define bit_buf (decoder->bitstream_buf)
00281 #define bits (decoder->bitstream_bits)
00282 #define bit_ptr (decoder->bitstream_ptr)
00283 const DCtab * tab;
00284 int size;
00285 int dc_diff;
00286
00287 if (bit_buf < 0xf8000000) {
00288 tab = DC_lum_5 + UBITS (bit_buf, 5);
00289 size = tab->size;
00290 if (size) {
00291 bits += tab->len + size;
00292 bit_buf <<= tab->len;
00293 dc_diff =
00294 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
00295 bit_buf <<= size;
00296 return dc_diff;
00297 } else {
00298 DUMPBITS (bit_buf, bits, 3);
00299 return 0;
00300 }
00301 } else {
00302 tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0);
00303 size = tab->size;
00304 DUMPBITS (bit_buf, bits, tab->len);
00305 NEEDBITS (bit_buf, bits, bit_ptr);
00306 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
00307 DUMPBITS (bit_buf, bits, size);
00308 return dc_diff;
00309 }
00310 #undef bit_buf
00311 #undef bits
00312 #undef bit_ptr
00313 }
00314
00315 static inline int get_chroma_dc_dct_diff (decoder_t * const decoder)
00316 {
00317 #define bit_buf (decoder->bitstream_buf)
00318 #define bits (decoder->bitstream_bits)
00319 #define bit_ptr (decoder->bitstream_ptr)
00320 const DCtab * tab;
00321 int size;
00322 int dc_diff;
00323
00324 if (bit_buf < 0xf8000000) {
00325 tab = DC_chrom_5 + UBITS (bit_buf, 5);
00326 size = tab->size;
00327 if (size) {
00328 bits += tab->len + size;
00329 bit_buf <<= tab->len;
00330 dc_diff =
00331 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
00332 bit_buf <<= size;
00333 return dc_diff;
00334 } else {
00335 DUMPBITS (bit_buf, bits, 2);
00336 return 0;
00337 }
00338 } else {
00339 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
00340 size = tab->size;
00341 DUMPBITS (bit_buf, bits, tab->len + 1);
00342 NEEDBITS (bit_buf, bits, bit_ptr);
00343 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
00344 DUMPBITS (bit_buf, bits, size);
00345 return dc_diff;
00346 }
00347 #undef bit_buf
00348 #undef bits
00349 #undef bit_ptr
00350 }
00351
00352 #define SATURATE(val) \
00353 do { \
00354 if (unlikely ((uint32_t)(val + 2048) > 4095)) \
00355 val = SBITS (val, 1) ^ 2047; \
00356 } while (0)
00357
00358 static void get_intra_block_B14 (decoder_t * const decoder)
00359 {
00360 int i;
00361 int j;
00362 int val;
00363 const uint8_t * scan = decoder->scan;
00364 const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
00365 int quantizer_scale = decoder->quantizer_scale;
00366 int mismatch;
00367 const DCTtab * tab;
00368 uint32_t bit_buf;
00369 int bits;
00370 const uint8_t * bit_ptr;
00371 int16_t * dest;
00372
00373 dest = decoder->DCTblock;
00374 i = 0;
00375 mismatch = ~dest[0];
00376
00377 bit_buf = decoder->bitstream_buf;
00378 bits = decoder->bitstream_bits;
00379 bit_ptr = decoder->bitstream_ptr;
00380
00381 NEEDBITS (bit_buf, bits, bit_ptr);
00382
00383 while (1) {
00384 if (bit_buf >= 0x28000000) {
00385
00386 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
00387
00388 i += tab->run;
00389 if (i >= 64)
00390 break;
00391
00392 normal_code:
00393 j = scan[i];
00394 bit_buf <<= tab->len;
00395 bits += tab->len + 1;
00396 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
00397
00398
00399 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
00400
00401 SATURATE (val);
00402 dest[j] = val;
00403 mismatch ^= val;
00404
00405 bit_buf <<= 1;
00406 NEEDBITS (bit_buf, bits, bit_ptr);
00407
00408 continue;
00409
00410 } else if (bit_buf >= 0x04000000) {
00411
00412 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
00413
00414 i += tab->run;
00415 if (i < 64)
00416 goto normal_code;
00417
00418
00419
00420 i += UBITS (bit_buf << 6, 6) - 64;
00421 if (i >= 64)
00422 break;
00423
00424 j = scan[i];
00425
00426 DUMPBITS (bit_buf, bits, 12);
00427 NEEDBITS (bit_buf, bits, bit_ptr);
00428 val = (SBITS (bit_buf, 12) *
00429 quantizer_scale * quant_matrix[j]) / 16;
00430
00431 SATURATE (val);
00432 dest[j] = val;
00433 mismatch ^= val;
00434
00435 DUMPBITS (bit_buf, bits, 12);
00436 NEEDBITS (bit_buf, bits, bit_ptr);
00437
00438 continue;
00439
00440 } else if (bit_buf >= 0x02000000) {
00441 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
00442 i += tab->run;
00443 if (i < 64)
00444 goto normal_code;
00445 } else if (bit_buf >= 0x00800000) {
00446 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
00447 i += tab->run;
00448 if (i < 64)
00449 goto normal_code;
00450 } else if (bit_buf >= 0x00200000) {
00451 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
00452 i += tab->run;
00453 if (i < 64)
00454 goto normal_code;
00455 } else {
00456 tab = DCT_16 + UBITS (bit_buf, 16);
00457 bit_buf <<= 16;
00458 GETWORD (bit_buf, bits + 16, bit_ptr);
00459 i += tab->run;
00460 if (i < 64)
00461 goto normal_code;
00462 }
00463 break;
00464 }
00465 dest[63] ^= mismatch & 1;
00466 DUMPBITS (bit_buf, bits, 2);
00467 decoder->bitstream_buf = bit_buf;
00468 decoder->bitstream_bits = bits;
00469 decoder->bitstream_ptr = bit_ptr;
00470 }
00471
00472 static void get_intra_block_B15 (decoder_t * const decoder)
00473 {
00474 int i;
00475 int j;
00476 int val;
00477 const uint8_t * scan = decoder->scan;
00478 const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
00479 int quantizer_scale = decoder->quantizer_scale;
00480 int mismatch;
00481 const DCTtab * tab;
00482 uint32_t bit_buf;
00483 int bits;
00484 const uint8_t * bit_ptr;
00485 int16_t * dest;
00486
00487 dest = decoder->DCTblock;
00488 i = 0;
00489 mismatch = ~dest[0];
00490
00491 bit_buf = decoder->bitstream_buf;
00492 bits = decoder->bitstream_bits;
00493 bit_ptr = decoder->bitstream_ptr;
00494
00495 NEEDBITS (bit_buf, bits, bit_ptr);
00496
00497 while (1) {
00498 if (bit_buf >= 0x04000000) {
00499
00500 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);
00501
00502 i += tab->run;
00503 if (i < 64) {
00504
00505 normal_code:
00506 j = scan[i];
00507 bit_buf <<= tab->len;
00508 bits += tab->len + 1;
00509 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
00510
00511
00512 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
00513
00514 SATURATE (val);
00515 dest[j] = val;
00516 mismatch ^= val;
00517
00518 bit_buf <<= 1;
00519 NEEDBITS (bit_buf, bits, bit_ptr);
00520
00521 continue;
00522
00523 } else {
00524
00525
00526
00527
00528
00529
00530
00531
00532 i += UBITS (bit_buf << 6, 6) - 64;
00533 if (i >= 64)
00534 break;
00535
00536 j = scan[i];
00537
00538 DUMPBITS (bit_buf, bits, 12);
00539 NEEDBITS (bit_buf, bits, bit_ptr);
00540 val = (SBITS (bit_buf, 12) *
00541 quantizer_scale * quant_matrix[j]) / 16;
00542
00543 SATURATE (val);
00544 dest[j] = val;
00545 mismatch ^= val;
00546
00547 DUMPBITS (bit_buf, bits, 12);
00548 NEEDBITS (bit_buf, bits, bit_ptr);
00549
00550 continue;
00551
00552 }
00553 } else if (bit_buf >= 0x02000000) {
00554 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
00555 i += tab->run;
00556 if (i < 64)
00557 goto normal_code;
00558 } else if (bit_buf >= 0x00800000) {
00559 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
00560 i += tab->run;
00561 if (i < 64)
00562 goto normal_code;
00563 } else if (bit_buf >= 0x00200000) {
00564 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
00565 i += tab->run;
00566 if (i < 64)
00567 goto normal_code;
00568 } else {
00569 tab = DCT_16 + UBITS (bit_buf, 16);
00570 bit_buf <<= 16;
00571 GETWORD (bit_buf, bits + 16, bit_ptr);
00572 i += tab->run;
00573 if (i < 64)
00574 goto normal_code;
00575 }
00576 break;
00577 }
00578 dest[63] ^= mismatch & 1;
00579 DUMPBITS (bit_buf, bits, 4);
00580 decoder->bitstream_buf = bit_buf;
00581 decoder->bitstream_bits = bits;
00582 decoder->bitstream_ptr = bit_ptr;
00583 }
00584
00585 static int get_non_intra_block (decoder_t * const decoder)
00586 {
00587 int i;
00588 int j;
00589 int val;
00590 const uint8_t * scan = decoder->scan;
00591 const uint8_t * quant_matrix = decoder->non_intra_quantizer_matrix;
00592 int quantizer_scale = decoder->quantizer_scale;
00593 int mismatch;
00594 const DCTtab * tab;
00595 uint32_t bit_buf;
00596 int bits;
00597 const uint8_t * bit_ptr;
00598 int16_t * dest;
00599
00600 i = -1;
00601 mismatch = 1;
00602 dest = decoder->DCTblock;
00603
00604 bit_buf = decoder->bitstream_buf;
00605 bits = decoder->bitstream_bits;
00606 bit_ptr = decoder->bitstream_ptr;
00607
00608 NEEDBITS (bit_buf, bits, bit_ptr);
00609 if (bit_buf >= 0x28000000) {
00610 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
00611 goto entry_1;
00612 } else
00613 goto entry_2;
00614
00615 while (1) {
00616 if (bit_buf >= 0x28000000) {
00617
00618 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
00619
00620 entry_1:
00621 i += tab->run;
00622 if (i >= 64)
00623 break;
00624
00625 normal_code:
00626 j = scan[i];
00627 bit_buf <<= tab->len;
00628 bits += tab->len + 1;
00629 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
00630
00631
00632 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
00633
00634 SATURATE (val);
00635 dest[j] = val;
00636 mismatch ^= val;
00637
00638 bit_buf <<= 1;
00639 NEEDBITS (bit_buf, bits, bit_ptr);
00640
00641 continue;
00642
00643 }
00644
00645 entry_2:
00646 if (bit_buf >= 0x04000000) {
00647
00648 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
00649
00650 i += tab->run;
00651 if (i < 64)
00652 goto normal_code;
00653
00654
00655
00656 i += UBITS (bit_buf << 6, 6) - 64;
00657 if (i >= 64)
00658 break;
00659
00660 j = scan[i];
00661
00662 DUMPBITS (bit_buf, bits, 12);
00663 NEEDBITS (bit_buf, bits, bit_ptr);
00664 val = 2 * (SBITS (bit_buf, 12) + SBITS (bit_buf, 1)) + 1;
00665 val = (val * quantizer_scale * quant_matrix[j]) / 32;
00666
00667 SATURATE (val);
00668 dest[j] = val;
00669 mismatch ^= val;
00670
00671 DUMPBITS (bit_buf, bits, 12);
00672 NEEDBITS (bit_buf, bits, bit_ptr);
00673
00674 continue;
00675
00676 } else if (bit_buf >= 0x02000000) {
00677 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
00678 i += tab->run;
00679 if (i < 64)
00680 goto normal_code;
00681 } else if (bit_buf >= 0x00800000) {
00682 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
00683 i += tab->run;
00684 if (i < 64)
00685 goto normal_code;
00686 } else if (bit_buf >= 0x00200000) {
00687 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
00688 i += tab->run;
00689 if (i < 64)
00690 goto normal_code;
00691 } else {
00692 tab = DCT_16 + UBITS (bit_buf, 16);
00693 bit_buf <<= 16;
00694 GETWORD (bit_buf, bits + 16, bit_ptr);
00695 i += tab->run;
00696 if (i < 64)
00697 goto normal_code;
00698 }
00699 break;
00700 }
00701 dest[63] ^= mismatch & 1;
00702 DUMPBITS (bit_buf, bits, 2);
00703 decoder->bitstream_buf = bit_buf;
00704 decoder->bitstream_bits = bits;
00705 decoder->bitstream_ptr = bit_ptr;
00706 return i;
00707 }
00708
00709 static void get_mpeg1_intra_block (decoder_t * const decoder)
00710 {
00711 int i;
00712 int j;
00713 int val;
00714 const uint8_t * scan = decoder->scan;
00715 const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
00716 int quantizer_scale = decoder->quantizer_scale;
00717 const DCTtab * tab;
00718 uint32_t bit_buf;
00719 int bits;
00720 const uint8_t * bit_ptr;
00721 int16_t * dest;
00722
00723 i = 0;
00724 dest = decoder->DCTblock;
00725
00726 bit_buf = decoder->bitstream_buf;
00727 bits = decoder->bitstream_bits;
00728 bit_ptr = decoder->bitstream_ptr;
00729
00730 NEEDBITS (bit_buf, bits, bit_ptr);
00731
00732 while (1) {
00733 if (bit_buf >= 0x28000000) {
00734
00735 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
00736
00737 i += tab->run;
00738 if (i >= 64)
00739 break;
00740
00741 normal_code:
00742 j = scan[i];
00743 bit_buf <<= tab->len;
00744 bits += tab->len + 1;
00745 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
00746
00747
00748 val = (val - 1) | 1;
00749
00750
00751 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
00752
00753 SATURATE (val);
00754 dest[j] = val;
00755
00756 bit_buf <<= 1;
00757 NEEDBITS (bit_buf, bits, bit_ptr);
00758
00759 continue;
00760
00761 } else if (bit_buf >= 0x04000000) {
00762
00763 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
00764
00765 i += tab->run;
00766 if (i < 64)
00767 goto normal_code;
00768
00769
00770
00771 i += UBITS (bit_buf << 6, 6) - 64;
00772 if (i >= 64)
00773 break;
00774
00775 j = scan[i];
00776
00777 DUMPBITS (bit_buf, bits, 12);
00778 NEEDBITS (bit_buf, bits, bit_ptr);
00779 val = SBITS (bit_buf, 8);
00780 if (! (val & 0x7f)) {
00781 DUMPBITS (bit_buf, bits, 8);
00782 val = UBITS (bit_buf, 8) + 2 * val;
00783 }
00784 val = (val * quantizer_scale * quant_matrix[j]) / 16;
00785
00786
00787 val = (val + ~SBITS (val, 1)) | 1;
00788
00789 SATURATE (val);
00790 dest[j] = val;
00791
00792 DUMPBITS (bit_buf, bits, 8);
00793 NEEDBITS (bit_buf, bits, bit_ptr);
00794
00795 continue;
00796
00797 } else if (bit_buf >= 0x02000000) {
00798 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
00799 i += tab->run;
00800 if (i < 64)
00801 goto normal_code;
00802 } else if (bit_buf >= 0x00800000) {
00803 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
00804 i += tab->run;
00805 if (i < 64)
00806 goto normal_code;
00807 } else if (bit_buf >= 0x00200000) {
00808 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
00809 i += tab->run;
00810 if (i < 64)
00811 goto normal_code;
00812 } else {
00813 tab = DCT_16 + UBITS (bit_buf, 16);
00814 bit_buf <<= 16;
00815 GETWORD (bit_buf, bits + 16, bit_ptr);
00816 i += tab->run;
00817 if (i < 64)
00818 goto normal_code;
00819 }
00820 break;
00821 }
00822 DUMPBITS (bit_buf, bits, 2);
00823 decoder->bitstream_buf = bit_buf;
00824 decoder->bitstream_bits = bits;
00825 decoder->bitstream_ptr = bit_ptr;
00826 }
00827
00828 static int get_mpeg1_non_intra_block (decoder_t * const decoder)
00829 {
00830 int i;
00831 int j;
00832 int val;
00833 const uint8_t * scan = decoder->scan;
00834 const uint8_t * quant_matrix = decoder->non_intra_quantizer_matrix;
00835 int quantizer_scale = decoder->quantizer_scale;
00836 const DCTtab * tab;
00837 uint32_t bit_buf;
00838 int bits;
00839 const uint8_t * bit_ptr;
00840 int16_t * dest;
00841
00842 i = -1;
00843 dest = decoder->DCTblock;
00844
00845 bit_buf = decoder->bitstream_buf;
00846 bits = decoder->bitstream_bits;
00847 bit_ptr = decoder->bitstream_ptr;
00848
00849 NEEDBITS (bit_buf, bits, bit_ptr);
00850 if (bit_buf >= 0x28000000) {
00851 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
00852 goto entry_1;
00853 } else
00854 goto entry_2;
00855
00856 while (1) {
00857 if (bit_buf >= 0x28000000) {
00858
00859 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
00860
00861 entry_1:
00862 i += tab->run;
00863 if (i >= 64)
00864 break;
00865
00866 normal_code:
00867 j = scan[i];
00868 bit_buf <<= tab->len;
00869 bits += tab->len + 1;
00870 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
00871
00872
00873 val = (val - 1) | 1;
00874
00875
00876 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
00877
00878 SATURATE (val);
00879 dest[j] = val;
00880
00881 bit_buf <<= 1;
00882 NEEDBITS (bit_buf, bits, bit_ptr);
00883
00884 continue;
00885
00886 }
00887
00888 entry_2:
00889 if (bit_buf >= 0x04000000) {
00890
00891 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
00892
00893 i += tab->run;
00894 if (i < 64)
00895 goto normal_code;
00896
00897
00898
00899 i += UBITS (bit_buf << 6, 6) - 64;
00900 if (i >= 64)
00901 break;
00902
00903 j = scan[i];
00904
00905 DUMPBITS (bit_buf, bits, 12);
00906 NEEDBITS (bit_buf, bits, bit_ptr);
00907 val = SBITS (bit_buf, 8);
00908 if (! (val & 0x7f)) {
00909 DUMPBITS (bit_buf, bits, 8);
00910 val = UBITS (bit_buf, 8) + 2 * val;
00911 }
00912 val = 2 * (val + SBITS (val, 1)) + 1;
00913 val = (val * quantizer_scale * quant_matrix[j]) / 32;
00914
00915
00916 val = (val + ~SBITS (val, 1)) | 1;
00917
00918 SATURATE (val);
00919 dest[j] = val;
00920
00921 DUMPBITS (bit_buf, bits, 8);
00922 NEEDBITS (bit_buf, bits, bit_ptr);
00923
00924 continue;
00925
00926 } else if (bit_buf >= 0x02000000) {
00927 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
00928 i += tab->run;
00929 if (i < 64)
00930 goto normal_code;
00931 } else if (bit_buf >= 0x00800000) {
00932 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
00933 i += tab->run;
00934 if (i < 64)
00935 goto normal_code;
00936 } else if (bit_buf >= 0x00200000) {
00937 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
00938 i += tab->run;
00939 if (i < 64)
00940 goto normal_code;
00941 } else {
00942 tab = DCT_16 + UBITS (bit_buf, 16);
00943 bit_buf <<= 16;
00944 GETWORD (bit_buf, bits + 16, bit_ptr);
00945 i += tab->run;
00946 if (i < 64)
00947 goto normal_code;
00948 }
00949 break;
00950 }
00951 DUMPBITS (bit_buf, bits, 2);
00952 decoder->bitstream_buf = bit_buf;
00953 decoder->bitstream_bits = bits;
00954 decoder->bitstream_ptr = bit_ptr;
00955 return i;
00956 }
00957
00958 static inline void slice_intra_DCT (decoder_t * const decoder, const int cc,
00959 uint8_t * const dest, const int stride)
00960 {
00961 #define bit_buf (decoder->bitstream_buf)
00962 #define bits (decoder->bitstream_bits)
00963 #define bit_ptr (decoder->bitstream_ptr)
00964 NEEDBITS (bit_buf, bits, bit_ptr);
00965
00966 if (cc == 0)
00967 decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder);
00968 else
00969 decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff (decoder);
00970 decoder->DCTblock[0] =
00971 decoder->dc_dct_pred[cc] << (3 - decoder->intra_dc_precision);
00972
00973 if (decoder->mpeg1) {
00974 if (decoder->coding_type != D_TYPE)
00975 get_mpeg1_intra_block (decoder);
00976 } else if (decoder->intra_vlc_format)
00977 get_intra_block_B15 (decoder);
00978 else
00979 get_intra_block_B14 (decoder);
00980 mpeg2_idct_copy (decoder->DCTblock, dest, stride);
00981 #undef bit_buf
00982 #undef bits
00983 #undef bit_ptr
00984 }
00985
00986 static inline void slice_non_intra_DCT (decoder_t * const decoder,
00987 uint8_t * const dest, const int stride)
00988 {
00989 int last;
00990
00991 if (decoder->mpeg1)
00992 last = get_mpeg1_non_intra_block (decoder);
00993 else
00994 last = get_non_intra_block (decoder);
00995 mpeg2_idct_add (last, decoder->DCTblock, dest, stride);
00996 }
00997
00998 #define MOTION(table,ref,motion_x,motion_y,size,y) \
00999 pos_x = 2 * decoder->offset + motion_x; \
01000 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
01001 if ((pos_x > decoder->limit_x) || (pos_y > decoder->limit_y_ ## size)) \
01002 return; \
01003 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
01004 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
01005 ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride, \
01006 decoder->stride, size); \
01007 motion_x /= 2; motion_y /= 2; \
01008 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
01009 offset = (((decoder->offset + motion_x) >> 1) + \
01010 ((((decoder->v_offset + motion_y) >> 1) + y/2) * \
01011 decoder->uv_stride)); \
01012 table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride + \
01013 (decoder->offset >> 1), ref[1] + offset, \
01014 decoder->uv_stride, size/2); \
01015 table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride + \
01016 (decoder->offset >> 1), ref[2] + offset, \
01017 decoder->uv_stride, size/2)
01018
01019 #define MOTION_FIELD(table,ref,motion_x,motion_y,dest_field,op,src_field) \
01020 pos_x = 2 * decoder->offset + motion_x; \
01021 pos_y = decoder->v_offset + motion_y; \
01022 if ((pos_x > decoder->limit_x) || (pos_y > decoder->limit_y)) \
01023 return; \
01024 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
01025 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
01026 decoder->offset, \
01027 (ref[0] + (pos_x >> 1) + \
01028 ((pos_y op) + src_field) * decoder->stride), \
01029 2 * decoder->stride, 8); \
01030 motion_x /= 2; motion_y /= 2; \
01031 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
01032 offset = (((decoder->offset + motion_x) >> 1) + \
01033 (((decoder->v_offset >> 1) + (motion_y op) + src_field) * \
01034 decoder->uv_stride)); \
01035 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
01036 (decoder->offset >> 1), ref[1] + offset, \
01037 2 * decoder->uv_stride, 4); \
01038 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
01039 (decoder->offset >> 1), ref[2] + offset, \
01040 2 * decoder->uv_stride, 4)
01041
01042 static void motion_mp1 (decoder_t * const decoder, motion_t * const motion,
01043 mpeg2_mc_fct * const * const table)
01044 {
01045 #define bit_buf (decoder->bitstream_buf)
01046 #define bits (decoder->bitstream_bits)
01047 #define bit_ptr (decoder->bitstream_ptr)
01048 int motion_x, motion_y;
01049 unsigned int pos_x, pos_y, xy_half, offset;
01050
01051 NEEDBITS (bit_buf, bits, bit_ptr);
01052 motion_x = (motion->pmv[0][0] +
01053 (get_motion_delta (decoder,
01054 motion->f_code[0]) << motion->f_code[1]));
01055 motion_x = bound_motion_vector (motion_x,
01056 motion->f_code[0] + motion->f_code[1]);
01057 motion->pmv[0][0] = motion_x;
01058
01059 NEEDBITS (bit_buf, bits, bit_ptr);
01060 motion_y = (motion->pmv[0][1] +
01061 (get_motion_delta (decoder,
01062 motion->f_code[0]) << motion->f_code[1]));
01063 motion_y = bound_motion_vector (motion_y,
01064 motion->f_code[0] + motion->f_code[1]);
01065 motion->pmv[0][1] = motion_y;
01066
01067 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
01068 #undef bit_buf
01069 #undef bits
01070 #undef bit_ptr
01071 }
01072
01073 static void motion_fr_frame (decoder_t * const decoder,
01074 motion_t * const motion,
01075 mpeg2_mc_fct * const * const table)
01076 {
01077 #define bit_buf (decoder->bitstream_buf)
01078 #define bits (decoder->bitstream_bits)
01079 #define bit_ptr (decoder->bitstream_ptr)
01080 int motion_x, motion_y;
01081 unsigned int pos_x, pos_y, xy_half, offset;
01082
01083 NEEDBITS (bit_buf, bits, bit_ptr);
01084 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
01085 motion->f_code[0]);
01086 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
01087 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
01088
01089 NEEDBITS (bit_buf, bits, bit_ptr);
01090 motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
01091 motion->f_code[1]);
01092 motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
01093 motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
01094
01095 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
01096 #undef bit_buf
01097 #undef bits
01098 #undef bit_ptr
01099 }
01100
01101 static void motion_fr_field (decoder_t * const decoder,
01102 motion_t * const motion,
01103 mpeg2_mc_fct * const * const table)
01104 {
01105 #define bit_buf (decoder->bitstream_buf)
01106 #define bits (decoder->bitstream_bits)
01107 #define bit_ptr (decoder->bitstream_ptr)
01108 int motion_x, motion_y, field;
01109 unsigned int pos_x, pos_y, xy_half, offset;
01110
01111 NEEDBITS (bit_buf, bits, bit_ptr);
01112 field = UBITS (bit_buf, 1);
01113 DUMPBITS (bit_buf, bits, 1);
01114
01115 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
01116 motion->f_code[0]);
01117 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
01118 motion->pmv[0][0] = motion_x;
01119
01120 NEEDBITS (bit_buf, bits, bit_ptr);
01121 motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta (decoder,
01122 motion->f_code[1]);
01123
01124 motion->pmv[0][1] = motion_y << 1;
01125
01126 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field);
01127
01128 NEEDBITS (bit_buf, bits, bit_ptr);
01129 field = UBITS (bit_buf, 1);
01130 DUMPBITS (bit_buf, bits, 1);
01131
01132 motion_x = motion->pmv[1][0] + get_motion_delta (decoder,
01133 motion->f_code[0]);
01134 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
01135 motion->pmv[1][0] = motion_x;
01136
01137 NEEDBITS (bit_buf, bits, bit_ptr);
01138 motion_y = (motion->pmv[1][1] >> 1) + get_motion_delta (decoder,
01139 motion->f_code[1]);
01140
01141 motion->pmv[1][1] = motion_y << 1;
01142
01143 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field);
01144 #undef bit_buf
01145 #undef bits
01146 #undef bit_ptr
01147 }
01148
01149 static void motion_fr_dmv (decoder_t * const decoder, motion_t * const motion,
01150 mpeg2_mc_fct * const * const table)
01151 {
01152 #define bit_buf (decoder->bitstream_buf)
01153 #define bits (decoder->bitstream_bits)
01154 #define bit_ptr (decoder->bitstream_ptr)
01155 int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y;
01156 unsigned int pos_x, pos_y, xy_half, offset;
01157
01158 NEEDBITS (bit_buf, bits, bit_ptr);
01159 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
01160 motion->f_code[0]);
01161 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
01162 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
01163 NEEDBITS (bit_buf, bits, bit_ptr);
01164 dmv_x = get_dmv (decoder);
01165
01166 motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta (decoder,
01167 motion->f_code[1]);
01168
01169 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1;
01170 dmv_y = get_dmv (decoder);
01171
01172 m = decoder->top_field_first ? 1 : 3;
01173 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x;
01174 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1;
01175 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0);
01176
01177 m = decoder->top_field_first ? 3 : 1;
01178 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x;
01179 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1;
01180 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);
01181
01182 xy_half = ((motion_y & 1) << 1) | (motion_x & 1);
01183 offset = (decoder->offset + (motion_x >> 1) +
01184 (decoder->v_offset + (motion_y & ~1)) * decoder->stride);
01185 mpeg2_mc.avg[xy_half]
01186 (decoder->dest[0] + decoder->offset,
01187 motion->ref[0][0] + offset, 2 * decoder->stride, 8);
01188 mpeg2_mc.avg[xy_half]
01189 (decoder->dest[0] + decoder->stride + decoder->offset,
01190 motion->ref[0][0] + decoder->stride + offset, 2 * decoder->stride, 8);
01191 motion_x /= 2; motion_y /= 2;
01192 xy_half = ((motion_y & 1) << 1) | (motion_x & 1);
01193 offset = (((decoder->offset + motion_x) >> 1) +
01194 (((decoder->v_offset >> 1) + (motion_y & ~1)) *
01195 decoder->uv_stride));
01196 mpeg2_mc.avg[4+xy_half]
01197 (decoder->dest[1] + (decoder->offset >> 1),
01198 motion->ref[0][1] + offset, 2 * decoder->uv_stride, 4);
01199 mpeg2_mc.avg[4+xy_half]
01200 (decoder->dest[1] + decoder->uv_stride + (decoder->offset >> 1),
01201 motion->ref[0][1] + decoder->uv_stride + offset,
01202 2 * decoder->uv_stride, 4);
01203 mpeg2_mc.avg[4+xy_half]
01204 (decoder->dest[2] + (decoder->offset >> 1),
01205 motion->ref[0][2] + offset, 2 * decoder->uv_stride, 4);
01206 mpeg2_mc.avg[4+xy_half]
01207 (decoder->dest[2] + decoder->uv_stride + (decoder->offset >> 1),
01208 motion->ref[0][2] + decoder->uv_stride + offset,
01209 2 * decoder->uv_stride, 4);
01210 #undef bit_buf
01211 #undef bits
01212 #undef bit_ptr
01213 }
01214
01215 static inline void motion_reuse (const decoder_t * const decoder,
01216 const motion_t * const motion,
01217 mpeg2_mc_fct * const * const table)
01218 {
01219 int motion_x, motion_y;
01220 unsigned int pos_x, pos_y, xy_half, offset;
01221
01222 motion_x = motion->pmv[0][0];
01223 motion_y = motion->pmv[0][1];
01224
01225 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
01226 }
01227
01228 static inline void motion_zero (const decoder_t * const decoder,
01229 const motion_t * const motion,
01230 mpeg2_mc_fct * const * const table)
01231 {
01232 unsigned int offset;
01233
01234 table[0] (decoder->dest[0] + decoder->offset,
01235 (motion->ref[0][0] + decoder->offset +
01236 decoder->v_offset * decoder->stride),
01237 decoder->stride, 16);
01238
01239 offset = ((decoder->offset >> 1) +
01240 (decoder->v_offset >> 1) * decoder->uv_stride);
01241 table[4] (decoder->dest[1] + (decoder->offset >> 1),
01242 motion->ref[0][1] + offset, decoder->uv_stride, 8);
01243 table[4] (decoder->dest[2] + (decoder->offset >> 1),
01244 motion->ref[0][2] + offset, decoder->uv_stride, 8);
01245 }
01246
01247
01248 static void motion_fr_conceal (decoder_t * const decoder)
01249 {
01250 #define bit_buf (decoder->bitstream_buf)
01251 #define bits (decoder->bitstream_bits)
01252 #define bit_ptr (decoder->bitstream_ptr)
01253 int tmp;
01254
01255 NEEDBITS (bit_buf, bits, bit_ptr);
01256 tmp = (decoder->f_motion.pmv[0][0] +
01257 get_motion_delta (decoder, decoder->f_motion.f_code[0]));
01258 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
01259 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
01260
01261 NEEDBITS (bit_buf, bits, bit_ptr);
01262 tmp = (decoder->f_motion.pmv[0][1] +
01263 get_motion_delta (decoder, decoder->f_motion.f_code[1]));
01264 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
01265 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
01266
01267 DUMPBITS (bit_buf, bits, 1);
01268 #undef bit_buf
01269 #undef bits
01270 #undef bit_ptr
01271 }
01272
01273 static void motion_fi_field (decoder_t * const decoder,
01274 motion_t * const motion,
01275 mpeg2_mc_fct * const * const table)
01276 {
01277 #define bit_buf (decoder->bitstream_buf)
01278 #define bits (decoder->bitstream_bits)
01279 #define bit_ptr (decoder->bitstream_ptr)
01280 int motion_x, motion_y;
01281 uint8_t ** ref_field;
01282 unsigned int pos_x, pos_y, xy_half, offset;
01283
01284 NEEDBITS (bit_buf, bits, bit_ptr);
01285 ref_field = motion->ref2[UBITS (bit_buf, 1)];
01286 DUMPBITS (bit_buf, bits, 1);
01287
01288 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
01289 motion->f_code[0]);
01290 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
01291 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
01292
01293 NEEDBITS (bit_buf, bits, bit_ptr);
01294 motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
01295 motion->f_code[1]);
01296 motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
01297 motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
01298
01299 MOTION (table, ref_field, motion_x, motion_y, 16, 0);
01300 #undef bit_buf
01301 #undef bits
01302 #undef bit_ptr
01303 }
01304
01305 static void motion_fi_16x8 (decoder_t * const decoder, motion_t * const motion,
01306 mpeg2_mc_fct * const * const table)
01307 {
01308 #define bit_buf (decoder->bitstream_buf)
01309 #define bits (decoder->bitstream_bits)
01310 #define bit_ptr (decoder->bitstream_ptr)
01311 int motion_x, motion_y;
01312 uint8_t ** ref_field;
01313 unsigned int pos_x, pos_y, xy_half, offset;
01314
01315 NEEDBITS (bit_buf, bits, bit_ptr);
01316 ref_field = motion->ref2[UBITS (bit_buf, 1)];
01317 DUMPBITS (bit_buf, bits, 1);
01318
01319 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
01320 motion->f_code[0]);
01321 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
01322 motion->pmv[0][0] = motion_x;
01323
01324 NEEDBITS (bit_buf, bits, bit_ptr);
01325 motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
01326 motion->f_code[1]);
01327 motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
01328 motion->pmv[0][1] = motion_y;
01329
01330 MOTION (table, ref_field, motion_x, motion_y, 8, 0);
01331
01332 NEEDBITS (bit_buf, bits, bit_ptr);
01333 ref_field = motion->ref2[UBITS (bit_buf, 1)];
01334 DUMPBITS (bit_buf, bits, 1);
01335
01336 motion_x = motion->pmv[1][0] + get_motion_delta (decoder,
01337 motion->f_code[0]);
01338 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
01339 motion->pmv[1][0] = motion_x;
01340
01341 NEEDBITS (bit_buf, bits, bit_ptr);
01342 motion_y = motion->pmv[1][1] + get_motion_delta (decoder,
01343 motion->f_code[1]);
01344 motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
01345 motion->pmv[1][1] = motion_y;
01346
01347 MOTION (table, ref_field, motion_x, motion_y, 8, 8);
01348 #undef bit_buf
01349 #undef bits
01350 #undef bit_ptr
01351 }
01352
01353 static void motion_fi_dmv (decoder_t * const decoder, motion_t * const motion,
01354 mpeg2_mc_fct * const * const table)
01355 {
01356 #define bit_buf (decoder->bitstream_buf)
01357 #define bits (decoder->bitstream_bits)
01358 #define bit_ptr (decoder->bitstream_ptr)
01359 int motion_x, motion_y, other_x, other_y;
01360 unsigned int pos_x, pos_y, xy_half, offset;
01361
01362 NEEDBITS (bit_buf, bits, bit_ptr);
01363 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
01364 motion->f_code[0]);
01365 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
01366 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
01367 NEEDBITS (bit_buf, bits, bit_ptr);
01368 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder);
01369
01370 motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
01371 motion->f_code[1]);
01372 motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
01373 motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
01374 other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) +
01375 decoder->dmv_offset);
01376
01377 MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0);
01378 MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0);
01379 #undef bit_buf
01380 #undef bits
01381 #undef bit_ptr
01382 }
01383
01384 static void motion_fi_conceal (decoder_t * const decoder)
01385 {
01386 #define bit_buf (decoder->bitstream_buf)
01387 #define bits (decoder->bitstream_bits)
01388 #define bit_ptr (decoder->bitstream_ptr)
01389 int tmp;
01390
01391 NEEDBITS (bit_buf, bits, bit_ptr);
01392 DUMPBITS (bit_buf, bits, 1);
01393
01394 tmp = (decoder->f_motion.pmv[0][0] +
01395 get_motion_delta (decoder, decoder->f_motion.f_code[0]));
01396 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
01397 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
01398
01399 NEEDBITS (bit_buf, bits, bit_ptr);
01400 tmp = (decoder->f_motion.pmv[0][1] +
01401 get_motion_delta (decoder, decoder->f_motion.f_code[1]));
01402 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
01403 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
01404
01405 DUMPBITS (bit_buf, bits, 1);
01406 #undef bit_buf
01407 #undef bits
01408 #undef bit_ptr
01409 }
01410
01411 #define MOTION_CALL(routine,direction) \
01412 do { \
01413 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
01414 routine (decoder, &(decoder->f_motion), mpeg2_mc.put); \
01415 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
01416 routine (decoder, &(decoder->b_motion), \
01417 ((direction) & MACROBLOCK_MOTION_FORWARD ? \
01418 mpeg2_mc.avg : mpeg2_mc.put)); \
01419 } while (0)
01420
01421 #define NEXT_MACROBLOCK \
01422 do { \
01423 decoder->offset += 16; \
01424 if (decoder->offset == decoder->width) { \
01425 do { \
01426 if (decoder->convert) { \
01427 decoder->convert (decoder->fbuf_id, decoder->dest, \
01428 decoder->v_offset); \
01429 if (decoder->coding_type == B_TYPE) \
01430 break; \
01431 } \
01432 decoder->dest[0] += 16 * decoder->stride; \
01433 decoder->dest[1] += 4 * decoder->stride; \
01434 decoder->dest[2] += 4 * decoder->stride; \
01435 } while (0); \
01436 decoder->v_offset += 16; \
01437 if (decoder->v_offset > decoder->limit_y) { \
01438 if (mpeg2_cpu_state_restore) \
01439 mpeg2_cpu_state_restore (&cpu_state); \
01440 return; \
01441 } \
01442 decoder->offset = 0; \
01443 } \
01444 } while (0)
01445
01446 void mpeg2_init_fbuf (decoder_t * decoder, uint8_t * current_fbuf[3],
01447 uint8_t * forward_fbuf[3], uint8_t * backward_fbuf[3])
01448 {
01449 int offset, stride, height, bottom_field;
01450
01451 stride = decoder->width;
01452 bottom_field = (decoder->picture_structure == BOTTOM_FIELD);
01453 offset = bottom_field ? stride : 0;
01454 height = decoder->height;
01455
01456 decoder->picture_dest[0] = current_fbuf[0] + offset;
01457 decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1);
01458 decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1);
01459
01460 decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset;
01461 decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1);
01462 decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1);
01463
01464 decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset;
01465 decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1);
01466 decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1);
01467
01468 if (decoder->picture_structure != FRAME_PICTURE) {
01469 decoder->dmv_offset = bottom_field ? 1 : -1;
01470 decoder->f_motion.ref2[0] = decoder->f_motion.ref[bottom_field];
01471 decoder->f_motion.ref2[1] = decoder->f_motion.ref[!bottom_field];
01472 decoder->b_motion.ref2[0] = decoder->b_motion.ref[bottom_field];
01473 decoder->b_motion.ref2[1] = decoder->b_motion.ref[!bottom_field];
01474 offset = stride - offset;
01475
01476 if (decoder->second_field && (decoder->coding_type != B_TYPE))
01477 forward_fbuf = current_fbuf;
01478
01479 decoder->f_motion.ref[1][0] = forward_fbuf[0] + offset;
01480 decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1);
01481 decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1);
01482
01483 decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset;
01484 decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1);
01485 decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1);
01486
01487 stride <<= 1;
01488 height >>= 1;
01489 }
01490
01491 decoder->stride = stride;
01492 decoder->uv_stride = stride >> 1;
01493 decoder->limit_x = 2 * decoder->width - 32;
01494 decoder->limit_y_16 = 2 * height - 32;
01495 decoder->limit_y_8 = 2 * height - 16;
01496 decoder->limit_y = height - 16;
01497 }
01498
01499 static inline int slice_init (decoder_t * const decoder, int code)
01500 {
01501 #define bit_buf (decoder->bitstream_buf)
01502 #define bits (decoder->bitstream_bits)
01503 #define bit_ptr (decoder->bitstream_ptr)
01504 int offset;
01505 const MBAtab * mba;
01506
01507 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
01508 decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
01509
01510 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
01511 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
01512 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
01513 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
01514
01515 if (decoder->vertical_position_extension) {
01516 code += UBITS (bit_buf, 3) << 7;
01517 DUMPBITS (bit_buf, bits, 3);
01518 }
01519 decoder->v_offset = (code - 1) * 16;
01520 offset = 0;
01521 if (!(decoder->convert) || decoder->coding_type != B_TYPE)
01522 offset = (code - 1) * decoder->stride * 4;
01523
01524 decoder->dest[0] = decoder->picture_dest[0] + offset * 4;
01525 decoder->dest[1] = decoder->picture_dest[1] + offset;
01526 decoder->dest[2] = decoder->picture_dest[2] + offset;
01527
01528 decoder->quantizer_scale = get_quantizer_scale (decoder);
01529
01530
01531 while (bit_buf & 0x80000000) {
01532 DUMPBITS (bit_buf, bits, 9);
01533 NEEDBITS (bit_buf, bits, bit_ptr);
01534 }
01535
01536
01537 offset = 0;
01538 while (1) {
01539 if (bit_buf >= 0x08000000) {
01540 mba = MBA_5 + (UBITS (bit_buf, 6) - 2);
01541 break;
01542 } else if (bit_buf >= 0x01800000) {
01543 mba = MBA_11 + (UBITS (bit_buf, 12) - 24);
01544 break;
01545 } else switch (UBITS (bit_buf, 12)) {
01546 case 8:
01547 offset += 33;
01548 DUMPBITS (bit_buf, bits, 11);
01549 NEEDBITS (bit_buf, bits, bit_ptr);
01550 continue;
01551 case 15:
01552 bit_buf &= 0xfffff;
01553 DUMPBITS (bit_buf, bits, 11);
01554 NEEDBITS (bit_buf, bits, bit_ptr);
01555 continue;
01556 default:
01557 return 1;
01558 }
01559 }
01560 DUMPBITS (bit_buf, bits, mba->len + 1);
01561 decoder->offset = (offset + mba->mba) << 4;
01562
01563 while (decoder->offset - decoder->width >= 0) {
01564 decoder->offset -= decoder->width;
01565 if (!(decoder->convert) || decoder->coding_type != B_TYPE) {
01566 decoder->dest[0] += 16 * decoder->stride;
01567 decoder->dest[1] += 4 * decoder->stride;
01568 decoder->dest[2] += 4 * decoder->stride;
01569 }
01570 decoder->v_offset += 16;
01571 }
01572 if (decoder->v_offset > decoder->limit_y)
01573 return 1;
01574
01575 return 0;
01576 #undef bit_buf
01577 #undef bits
01578 #undef bit_ptr
01579 }
01580
01581 void mpeg2_slice (decoder_t * const decoder, const int code,
01582 const uint8_t * const buffer)
01583 {
01584 #define bit_buf (decoder->bitstream_buf)
01585 #define bits (decoder->bitstream_bits)
01586 #define bit_ptr (decoder->bitstream_ptr)
01587 cpu_state_t cpu_state;
01588
01589 bitstream_init (decoder, buffer);
01590
01591 if (slice_init (decoder, code))
01592 return;
01593
01594 if (mpeg2_cpu_state_save)
01595 mpeg2_cpu_state_save (&cpu_state);
01596
01597 while (1) {
01598 int macroblock_modes;
01599 int mba_inc;
01600 const MBAtab * mba;
01601
01602 NEEDBITS (bit_buf, bits, bit_ptr);
01603
01604 macroblock_modes = get_macroblock_modes (decoder);
01605
01606
01607 if (macroblock_modes & MACROBLOCK_QUANT)
01608 decoder->quantizer_scale = get_quantizer_scale (decoder);
01609
01610 if (macroblock_modes & MACROBLOCK_INTRA) {
01611
01612 int DCT_offset, DCT_stride;
01613 int offset;
01614 uint8_t * dest_y;
01615
01616 if (decoder->concealment_motion_vectors) {
01617 if (decoder->picture_structure == FRAME_PICTURE)
01618 motion_fr_conceal (decoder);
01619 else
01620 motion_fi_conceal (decoder);
01621 } else {
01622 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
01623 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
01624 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
01625 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
01626 }
01627
01628 if (macroblock_modes & DCT_TYPE_INTERLACED) {
01629 DCT_offset = decoder->stride;
01630 DCT_stride = decoder->stride * 2;
01631 } else {
01632 DCT_offset = decoder->stride * 8;
01633 DCT_stride = decoder->stride;
01634 }
01635
01636 offset = decoder->offset;
01637 dest_y = decoder->dest[0] + offset;
01638 slice_intra_DCT (decoder, 0, dest_y, DCT_stride);
01639 slice_intra_DCT (decoder, 0, dest_y + 8, DCT_stride);
01640 slice_intra_DCT (decoder, 0, dest_y + DCT_offset, DCT_stride);
01641 slice_intra_DCT (decoder, 0, dest_y + DCT_offset + 8, DCT_stride);
01642 slice_intra_DCT (decoder, 1, decoder->dest[1] + (offset >> 1),
01643 decoder->uv_stride);
01644 slice_intra_DCT (decoder, 2, decoder->dest[2] + (offset >> 1),
01645 decoder->uv_stride);
01646
01647 if (decoder->coding_type == D_TYPE) {
01648 NEEDBITS (bit_buf, bits, bit_ptr);
01649 DUMPBITS (bit_buf, bits, 1);
01650 }
01651 } else {
01652
01653 if (decoder->picture_structure == FRAME_PICTURE)
01654 switch (macroblock_modes & MOTION_TYPE_MASK) {
01655 case MC_FRAME:
01656 if (decoder->mpeg1)
01657 MOTION_CALL (motion_mp1, macroblock_modes);
01658 else
01659 MOTION_CALL (motion_fr_frame, macroblock_modes);
01660 break;
01661
01662 case MC_FIELD:
01663 MOTION_CALL (motion_fr_field, macroblock_modes);
01664 break;
01665
01666 case MC_DMV:
01667 MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD);
01668 break;
01669
01670 case 0:
01671
01672 decoder->f_motion.pmv[0][0] = 0;
01673 decoder->f_motion.pmv[0][1] = 0;
01674 decoder->f_motion.pmv[1][0] = 0;
01675 decoder->f_motion.pmv[1][1] = 0;
01676 MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD);
01677 break;
01678 }
01679 else
01680 switch (macroblock_modes & MOTION_TYPE_MASK) {
01681 case MC_FIELD:
01682 MOTION_CALL (motion_fi_field, macroblock_modes);
01683 break;
01684
01685 case MC_16X8:
01686 MOTION_CALL (motion_fi_16x8, macroblock_modes);
01687 break;
01688
01689 case MC_DMV:
01690 MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD);
01691 break;
01692
01693 case 0:
01694
01695 decoder->f_motion.pmv[0][0] = 0;
01696 decoder->f_motion.pmv[0][1] = 0;
01697 decoder->f_motion.pmv[1][0] = 0;
01698 decoder->f_motion.pmv[1][1] = 0;
01699 MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD);
01700 break;
01701 }
01702
01703 if (macroblock_modes & MACROBLOCK_PATTERN) {
01704 int coded_block_pattern;
01705 int DCT_offset, DCT_stride;
01706 int offset;
01707 uint8_t * dest_y;
01708
01709 if (macroblock_modes & DCT_TYPE_INTERLACED) {
01710 DCT_offset = decoder->stride;
01711 DCT_stride = decoder->stride * 2;
01712 } else {
01713 DCT_offset = decoder->stride * 8;
01714 DCT_stride = decoder->stride;
01715 }
01716
01717 coded_block_pattern = get_coded_block_pattern (decoder);
01718
01719 offset = decoder->offset;
01720 dest_y = decoder->dest[0] + offset;
01721 if (coded_block_pattern & 0x20)
01722 slice_non_intra_DCT (decoder, dest_y, DCT_stride);
01723 if (coded_block_pattern & 0x10)
01724 slice_non_intra_DCT (decoder, dest_y + 8, DCT_stride);
01725 if (coded_block_pattern & 0x08)
01726 slice_non_intra_DCT (decoder, dest_y + DCT_offset,
01727 DCT_stride);
01728 if (coded_block_pattern & 0x04)
01729 slice_non_intra_DCT (decoder, dest_y + DCT_offset + 8,
01730 DCT_stride);
01731 if (coded_block_pattern & 0x2)
01732 slice_non_intra_DCT (decoder,
01733 decoder->dest[1] + (offset >> 1),
01734 decoder->uv_stride);
01735 if (coded_block_pattern & 0x1)
01736 slice_non_intra_DCT (decoder,
01737 decoder->dest[2] + (offset >> 1),
01738 decoder->uv_stride);
01739 }
01740
01741 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
01742 decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
01743 }
01744
01745 NEXT_MACROBLOCK;
01746
01747 NEEDBITS (bit_buf, bits, bit_ptr);
01748 mba_inc = 0;
01749 while (1) {
01750 if (bit_buf >= 0x10000000) {
01751 mba = MBA_5 + (UBITS (bit_buf, 5) - 2);
01752 break;
01753 } else if (bit_buf >= 0x03000000) {
01754 mba = MBA_11 + (UBITS (bit_buf, 11) - 24);
01755 break;
01756 } else switch (UBITS (bit_buf, 11)) {
01757 case 8:
01758 mba_inc += 33;
01759
01760 case 15:
01761 DUMPBITS (bit_buf, bits, 11);
01762 NEEDBITS (bit_buf, bits, bit_ptr);
01763 continue;
01764 default:
01765 if (mpeg2_cpu_state_restore)
01766 mpeg2_cpu_state_restore (&cpu_state);
01767 return;
01768 }
01769 }
01770 DUMPBITS (bit_buf, bits, mba->len);
01771 mba_inc += mba->mba;
01772
01773 if (mba_inc) {
01774 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
01775 decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
01776
01777 if (decoder->coding_type == P_TYPE) {
01778 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
01779 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
01780
01781 do {
01782 MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD);
01783 NEXT_MACROBLOCK;
01784 } while (--mba_inc);
01785 } else {
01786 do {
01787 MOTION_CALL (motion_reuse, macroblock_modes);
01788 NEXT_MACROBLOCK;
01789 } while (--mba_inc);
01790 }
01791 }
01792 }
01793 #undef bit_buf
01794 #undef bits
01795 #undef bit_ptr
01796 }