|
MythTV
0.26-pre
|
00001 /* 00002 * slice.c 00003 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org> 00004 * Copyright (C) 2003 Peter Gubanov <peter@elecard.net.ru> 00005 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> 00006 * 00007 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. 00008 * See http://libmpeg2.sourceforge.net/ for updates. 00009 * 00010 * mpeg2dec is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * mpeg2dec is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU General Public License 00021 * along with this program; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 */ 00024 00025 #include "mpeg2config.h" 00026 00027 #include <inttypes.h> 00028 00029 #include "mpeg2.h" 00030 #include "attributes.h" 00031 #include "mpeg2_internal.h" 00032 00033 extern mpeg2_mc_t mpeg2_mc; 00034 extern void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride); 00035 extern void (* mpeg2_idct_add) (int last, int16_t * block, 00036 uint8_t * dest, int stride); 00037 extern void (* mpeg2_cpu_state_save) (cpu_state_t * state); 00038 extern void (* mpeg2_cpu_state_restore) (cpu_state_t * state); 00039 00040 #include "vlc.h" 00041 00042 static inline int get_macroblock_modes (mpeg2_decoder_t * const decoder) 00043 { 00044 #define bit_buf (decoder->bitstream_buf) 00045 #define bits (decoder->bitstream_bits) 00046 #define bit_ptr (decoder->bitstream_ptr) 00047 int macroblock_modes; 00048 const MBtab * tab; 00049 00050 switch (decoder->coding_type) { 00051 case I_TYPE: 00052 00053 tab = MB_I + UBITS (bit_buf, 1); 00054 DUMPBITS (bit_buf, bits, tab->len); 00055 macroblock_modes = tab->modes; 00056 00057 if ((! (decoder->frame_pred_frame_dct)) && 00058 (decoder->picture_structure == FRAME_PICTURE)) { 00059 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; 00060 DUMPBITS (bit_buf, bits, 1); 00061 } 00062 00063 return macroblock_modes; 00064 00065 case P_TYPE: 00066 00067 tab = MB_P + UBITS (bit_buf, 5); 00068 DUMPBITS (bit_buf, bits, tab->len); 00069 macroblock_modes = tab->modes; 00070 00071 if (decoder->picture_structure != FRAME_PICTURE) { 00072 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) { 00073 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT; 00074 DUMPBITS (bit_buf, bits, 2); 00075 } 00076 return macroblock_modes | MACROBLOCK_MOTION_FORWARD; 00077 } else if (decoder->frame_pred_frame_dct) { 00078 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) 00079 macroblock_modes |= MC_FRAME << MOTION_TYPE_SHIFT; 00080 return macroblock_modes | MACROBLOCK_MOTION_FORWARD; 00081 } else { 00082 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) { 00083 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT; 00084 DUMPBITS (bit_buf, bits, 2); 00085 } 00086 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) { 00087 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; 00088 DUMPBITS (bit_buf, bits, 1); 00089 } 00090 return macroblock_modes | MACROBLOCK_MOTION_FORWARD; 00091 } 00092 00093 case B_TYPE: 00094 00095 tab = MB_B + UBITS (bit_buf, 6); 00096 DUMPBITS (bit_buf, bits, tab->len); 00097 macroblock_modes = tab->modes; 00098 00099 if (decoder->picture_structure != FRAME_PICTURE) { 00100 if (! (macroblock_modes & MACROBLOCK_INTRA)) { 00101 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT; 00102 DUMPBITS (bit_buf, bits, 2); 00103 } 00104 return macroblock_modes; 00105 } else if (decoder->frame_pred_frame_dct) { 00106 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */ 00107 macroblock_modes |= MC_FRAME << MOTION_TYPE_SHIFT; 00108 return macroblock_modes; 00109 } else { 00110 if (macroblock_modes & MACROBLOCK_INTRA) 00111 goto intra; 00112 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT; 00113 DUMPBITS (bit_buf, bits, 2); 00114 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) { 00115 intra: 00116 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; 00117 DUMPBITS (bit_buf, bits, 1); 00118 } 00119 return macroblock_modes; 00120 } 00121 00122 case D_TYPE: 00123 00124 DUMPBITS (bit_buf, bits, 1); 00125 return MACROBLOCK_INTRA; 00126 00127 default: 00128 return 0; 00129 } 00130 #undef bit_buf 00131 #undef bits 00132 #undef bit_ptr 00133 } 00134 00135 static inline void get_quantizer_scale (mpeg2_decoder_t * const decoder) 00136 { 00137 #define bit_buf (decoder->bitstream_buf) 00138 #define bits (decoder->bitstream_bits) 00139 #define bit_ptr (decoder->bitstream_ptr) 00140 00141 int quantizer_scale_code; 00142 00143 quantizer_scale_code = UBITS (bit_buf, 5); 00144 DUMPBITS (bit_buf, bits, 5); 00145 00146 decoder->quantizer_matrix[0] = 00147 decoder->quantizer_prescale[0][quantizer_scale_code]; 00148 decoder->quantizer_matrix[1] = 00149 decoder->quantizer_prescale[1][quantizer_scale_code]; 00150 decoder->quantizer_matrix[2] = 00151 decoder->chroma_quantizer[0][quantizer_scale_code]; 00152 decoder->quantizer_matrix[3] = 00153 decoder->chroma_quantizer[1][quantizer_scale_code]; 00154 #undef bit_buf 00155 #undef bits 00156 #undef bit_ptr 00157 } 00158 00159 static inline int get_motion_delta (mpeg2_decoder_t * const decoder, 00160 const int f_code) 00161 { 00162 #define bit_buf (decoder->bitstream_buf) 00163 #define bits (decoder->bitstream_bits) 00164 #define bit_ptr (decoder->bitstream_ptr) 00165 00166 int delta; 00167 int sign; 00168 const MVtab * tab; 00169 00170 if (bit_buf & 0x80000000) { 00171 DUMPBITS (bit_buf, bits, 1); 00172 return 0; 00173 } else if (bit_buf >= 0x0c000000) { 00174 00175 tab = MV_4 + UBITS (bit_buf, 4); 00176 delta = (tab->delta << f_code) + 1; 00177 bits += tab->len + f_code + 1; 00178 bit_buf <<= tab->len; 00179 00180 sign = SBITS (bit_buf, 1); 00181 bit_buf <<= 1; 00182 00183 if (f_code) 00184 delta += UBITS (bit_buf, f_code); 00185 bit_buf <<= f_code; 00186 00187 return (delta ^ sign) - sign; 00188 00189 } else { 00190 00191 tab = MV_10 + UBITS (bit_buf, 10); 00192 delta = (tab->delta << f_code) + 1; 00193 bits += tab->len + 1; 00194 bit_buf <<= tab->len; 00195 00196 sign = SBITS (bit_buf, 1); 00197 bit_buf <<= 1; 00198 00199 if (f_code) { 00200 NEEDBITS (bit_buf, bits, bit_ptr); 00201 delta += UBITS (bit_buf, f_code); 00202 DUMPBITS (bit_buf, bits, f_code); 00203 } 00204 00205 return (delta ^ sign) - sign; 00206 00207 } 00208 #undef bit_buf 00209 #undef bits 00210 #undef bit_ptr 00211 } 00212 00213 static inline int bound_motion_vector (const int vector, const int f_code) 00214 { 00215 return ((int32_t)vector << (27 - f_code)) >> (27 - f_code); 00216 } 00217 00218 static inline int get_dmv (mpeg2_decoder_t * const decoder) 00219 { 00220 #define bit_buf (decoder->bitstream_buf) 00221 #define bits (decoder->bitstream_bits) 00222 #define bit_ptr (decoder->bitstream_ptr) 00223 00224 const DMVtab * tab; 00225 00226 tab = DMV_2 + UBITS (bit_buf, 2); 00227 DUMPBITS (bit_buf, bits, tab->len); 00228 return tab->dmv; 00229 #undef bit_buf 00230 #undef bits 00231 #undef bit_ptr 00232 } 00233 00234 static inline int get_coded_block_pattern (mpeg2_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 CBPtab * tab; 00241 00242 NEEDBITS (bit_buf, bits, bit_ptr); 00243 00244 if (bit_buf >= 0x20000000) { 00245 00246 tab = CBP_7 + (UBITS (bit_buf, 7) - 16); 00247 DUMPBITS (bit_buf, bits, tab->len); 00248 return tab->cbp; 00249 00250 } else { 00251 00252 tab = CBP_9 + UBITS (bit_buf, 9); 00253 DUMPBITS (bit_buf, bits, tab->len); 00254 return tab->cbp; 00255 } 00256 00257 #undef bit_buf 00258 #undef bits 00259 #undef bit_ptr 00260 } 00261 00262 static inline int get_luma_dc_dct_diff (mpeg2_decoder_t * const decoder) 00263 { 00264 #define bit_buf (decoder->bitstream_buf) 00265 #define bits (decoder->bitstream_bits) 00266 #define bit_ptr (decoder->bitstream_ptr) 00267 const DCtab * tab; 00268 int size; 00269 int dc_diff; 00270 00271 if (bit_buf < 0xf8000000) { 00272 tab = DC_lum_5 + UBITS (bit_buf, 5); 00273 size = tab->size; 00274 if (size) { 00275 bits += tab->len + size; 00276 bit_buf <<= tab->len; 00277 dc_diff = 00278 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); 00279 bit_buf <<= size; 00280 return dc_diff << decoder->intra_dc_precision; 00281 } else { 00282 DUMPBITS (bit_buf, bits, 3); 00283 return 0; 00284 } 00285 } else { 00286 tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0); 00287 size = tab->size; 00288 DUMPBITS (bit_buf, bits, tab->len); 00289 NEEDBITS (bit_buf, bits, bit_ptr); 00290 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); 00291 DUMPBITS (bit_buf, bits, size); 00292 return dc_diff << decoder->intra_dc_precision; 00293 } 00294 #undef bit_buf 00295 #undef bits 00296 #undef bit_ptr 00297 } 00298 00299 static inline int get_chroma_dc_dct_diff (mpeg2_decoder_t * const decoder) 00300 { 00301 #define bit_buf (decoder->bitstream_buf) 00302 #define bits (decoder->bitstream_bits) 00303 #define bit_ptr (decoder->bitstream_ptr) 00304 const DCtab * tab; 00305 int size; 00306 int dc_diff; 00307 00308 if (bit_buf < 0xf8000000) { 00309 tab = DC_chrom_5 + UBITS (bit_buf, 5); 00310 size = tab->size; 00311 if (size) { 00312 bits += tab->len + size; 00313 bit_buf <<= tab->len; 00314 dc_diff = 00315 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); 00316 bit_buf <<= size; 00317 return dc_diff << decoder->intra_dc_precision; 00318 } else { 00319 DUMPBITS (bit_buf, bits, 2); 00320 return 0; 00321 } 00322 } else { 00323 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0); 00324 size = tab->size; 00325 DUMPBITS (bit_buf, bits, tab->len + 1); 00326 NEEDBITS (bit_buf, bits, bit_ptr); 00327 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); 00328 DUMPBITS (bit_buf, bits, size); 00329 return dc_diff << decoder->intra_dc_precision; 00330 } 00331 #undef bit_buf 00332 #undef bits 00333 #undef bit_ptr 00334 } 00335 00336 #define SATURATE(val) \ 00337 do { \ 00338 val <<= 4; \ 00339 if (unlikely (val != (int16_t) val)) \ 00340 val = (SBITS (val, 1) ^ 2047) << 4; \ 00341 } while (0) 00342 00343 static void get_intra_block_B14 (mpeg2_decoder_t * const decoder, 00344 const uint16_t * const quant_matrix) 00345 { 00346 int i; 00347 int j; 00348 int val; 00349 const uint8_t * const scan = decoder->scan; 00350 int mismatch; 00351 const DCTtab * tab; 00352 uint32_t bit_buf; 00353 int bits; 00354 const uint8_t * bit_ptr; 00355 int16_t * const dest = decoder->DCTblock; 00356 00357 i = 0; 00358 mismatch = ~dest[0]; 00359 00360 bit_buf = decoder->bitstream_buf; 00361 bits = decoder->bitstream_bits; 00362 bit_ptr = decoder->bitstream_ptr; 00363 00364 NEEDBITS (bit_buf, bits, bit_ptr); 00365 00366 while (1) { 00367 if (bit_buf >= 0x28000000) { 00368 00369 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 00370 00371 i += tab->run; 00372 if (i >= 64) 00373 break; /* end of block */ 00374 00375 normal_code: 00376 j = scan[i]; 00377 bit_buf <<= tab->len; 00378 bits += tab->len + 1; 00379 val = (tab->level * quant_matrix[j]) >> 4; 00380 00381 /* if (bitstream_get (1)) val = -val; */ 00382 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); 00383 00384 SATURATE (val); 00385 dest[j] = val; 00386 mismatch ^= val; 00387 00388 bit_buf <<= 1; 00389 NEEDBITS (bit_buf, bits, bit_ptr); 00390 00391 continue; 00392 00393 } else if (bit_buf >= 0x04000000) { 00394 00395 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 00396 00397 i += tab->run; 00398 if (i < 64) 00399 goto normal_code; 00400 00401 /* escape code */ 00402 00403 i += UBITS (bit_buf << 6, 6) - 64; 00404 if (i >= 64) 00405 break; /* illegal, check needed to avoid buffer overflow */ 00406 00407 j = scan[i]; 00408 00409 DUMPBITS (bit_buf, bits, 12); 00410 NEEDBITS (bit_buf, bits, bit_ptr); 00411 val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16; 00412 00413 SATURATE (val); 00414 dest[j] = val; 00415 mismatch ^= val; 00416 00417 DUMPBITS (bit_buf, bits, 12); 00418 NEEDBITS (bit_buf, bits, bit_ptr); 00419 00420 continue; 00421 00422 } else if (bit_buf >= 0x02000000) { 00423 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 00424 i += tab->run; 00425 if (i < 64) 00426 goto normal_code; 00427 } else if (bit_buf >= 0x00800000) { 00428 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 00429 i += tab->run; 00430 if (i < 64) 00431 goto normal_code; 00432 } else if (bit_buf >= 0x00200000) { 00433 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 00434 i += tab->run; 00435 if (i < 64) 00436 goto normal_code; 00437 } else { 00438 tab = DCT_16 + UBITS (bit_buf, 16); 00439 bit_buf <<= 16; 00440 GETWORD (bit_buf, bits + 16, bit_ptr); 00441 i += tab->run; 00442 if (i < 64) 00443 goto normal_code; 00444 } 00445 break; /* illegal, check needed to avoid buffer overflow */ 00446 } 00447 dest[63] ^= mismatch & 16; 00448 DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */ 00449 decoder->bitstream_buf = bit_buf; 00450 decoder->bitstream_bits = bits; 00451 decoder->bitstream_ptr = bit_ptr; 00452 } 00453 00454 static void get_intra_block_B15 (mpeg2_decoder_t * const decoder, 00455 const uint16_t * const quant_matrix) 00456 { 00457 int i; 00458 int j; 00459 int val; 00460 const uint8_t * const scan = decoder->scan; 00461 int mismatch; 00462 const DCTtab * tab; 00463 uint32_t bit_buf; 00464 int bits; 00465 const uint8_t * bit_ptr; 00466 int16_t * const dest = decoder->DCTblock; 00467 00468 i = 0; 00469 mismatch = ~dest[0]; 00470 00471 bit_buf = decoder->bitstream_buf; 00472 bits = decoder->bitstream_bits; 00473 bit_ptr = decoder->bitstream_ptr; 00474 00475 NEEDBITS (bit_buf, bits, bit_ptr); 00476 00477 while (1) { 00478 if (bit_buf >= 0x04000000) { 00479 00480 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4); 00481 00482 i += tab->run; 00483 if (i < 64) { 00484 00485 normal_code: 00486 j = scan[i]; 00487 bit_buf <<= tab->len; 00488 bits += tab->len + 1; 00489 val = (tab->level * quant_matrix[j]) >> 4; 00490 00491 /* if (bitstream_get (1)) val = -val; */ 00492 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); 00493 00494 SATURATE (val); 00495 dest[j] = val; 00496 mismatch ^= val; 00497 00498 bit_buf <<= 1; 00499 NEEDBITS (bit_buf, bits, bit_ptr); 00500 00501 continue; 00502 00503 } else { 00504 00505 /* end of block. I commented out this code because if we */ 00506 /* dont exit here we will still exit at the later test :) */ 00507 00508 /* if (i >= 128) break; */ /* end of block */ 00509 00510 /* escape code */ 00511 00512 i += UBITS (bit_buf << 6, 6) - 64; 00513 if (i >= 64) 00514 break; /* illegal, check against buffer overflow */ 00515 00516 j = scan[i]; 00517 00518 DUMPBITS (bit_buf, bits, 12); 00519 NEEDBITS (bit_buf, bits, bit_ptr); 00520 val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16; 00521 00522 SATURATE (val); 00523 dest[j] = val; 00524 mismatch ^= val; 00525 00526 DUMPBITS (bit_buf, bits, 12); 00527 NEEDBITS (bit_buf, bits, bit_ptr); 00528 00529 continue; 00530 00531 } 00532 } else if (bit_buf >= 0x02000000) { 00533 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8); 00534 i += tab->run; 00535 if (i < 64) 00536 goto normal_code; 00537 } else if (bit_buf >= 0x00800000) { 00538 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 00539 i += tab->run; 00540 if (i < 64) 00541 goto normal_code; 00542 } else if (bit_buf >= 0x00200000) { 00543 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 00544 i += tab->run; 00545 if (i < 64) 00546 goto normal_code; 00547 } else { 00548 tab = DCT_16 + UBITS (bit_buf, 16); 00549 bit_buf <<= 16; 00550 GETWORD (bit_buf, bits + 16, bit_ptr); 00551 i += tab->run; 00552 if (i < 64) 00553 goto normal_code; 00554 } 00555 break; /* illegal, check needed to avoid buffer overflow */ 00556 } 00557 dest[63] ^= mismatch & 16; 00558 DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */ 00559 decoder->bitstream_buf = bit_buf; 00560 decoder->bitstream_bits = bits; 00561 decoder->bitstream_ptr = bit_ptr; 00562 } 00563 00564 static int get_non_intra_block (mpeg2_decoder_t * const decoder, 00565 const uint16_t * const quant_matrix) 00566 { 00567 int i; 00568 int j; 00569 int val; 00570 const uint8_t * const scan = decoder->scan; 00571 int mismatch; 00572 const DCTtab * tab; 00573 uint32_t bit_buf; 00574 int bits; 00575 const uint8_t * bit_ptr; 00576 int16_t * const dest = decoder->DCTblock; 00577 00578 i = -1; 00579 mismatch = -1; 00580 00581 bit_buf = decoder->bitstream_buf; 00582 bits = decoder->bitstream_bits; 00583 bit_ptr = decoder->bitstream_ptr; 00584 00585 NEEDBITS (bit_buf, bits, bit_ptr); 00586 if (bit_buf >= 0x28000000) { 00587 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5); 00588 goto entry_1; 00589 } else 00590 goto entry_2; 00591 00592 while (1) { 00593 if (bit_buf >= 0x28000000) { 00594 00595 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 00596 00597 entry_1: 00598 i += tab->run; 00599 if (i >= 64) 00600 break; /* end of block */ 00601 00602 normal_code: 00603 j = scan[i]; 00604 bit_buf <<= tab->len; 00605 bits += tab->len + 1; 00606 val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5; 00607 00608 /* if (bitstream_get (1)) val = -val; */ 00609 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); 00610 00611 SATURATE (val); 00612 dest[j] = val; 00613 mismatch ^= val; 00614 00615 bit_buf <<= 1; 00616 NEEDBITS (bit_buf, bits, bit_ptr); 00617 00618 continue; 00619 00620 } 00621 00622 entry_2: 00623 if (bit_buf >= 0x04000000) { 00624 00625 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 00626 00627 i += tab->run; 00628 if (i < 64) 00629 goto normal_code; 00630 00631 /* escape code */ 00632 00633 i += UBITS (bit_buf << 6, 6) - 64; 00634 if (i >= 64) 00635 break; /* illegal, check needed to avoid buffer overflow */ 00636 00637 j = scan[i]; 00638 00639 DUMPBITS (bit_buf, bits, 12); 00640 NEEDBITS (bit_buf, bits, bit_ptr); 00641 val = 2 * (SBITS (bit_buf, 12) + SBITS (bit_buf, 1)) + 1; 00642 val = (val * quant_matrix[j]) / 32; 00643 00644 SATURATE (val); 00645 dest[j] = val; 00646 mismatch ^= val; 00647 00648 DUMPBITS (bit_buf, bits, 12); 00649 NEEDBITS (bit_buf, bits, bit_ptr); 00650 00651 continue; 00652 00653 } else if (bit_buf >= 0x02000000) { 00654 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 00655 i += tab->run; 00656 if (i < 64) 00657 goto normal_code; 00658 } else if (bit_buf >= 0x00800000) { 00659 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 00660 i += tab->run; 00661 if (i < 64) 00662 goto normal_code; 00663 } else if (bit_buf >= 0x00200000) { 00664 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 00665 i += tab->run; 00666 if (i < 64) 00667 goto normal_code; 00668 } else { 00669 tab = DCT_16 + UBITS (bit_buf, 16); 00670 bit_buf <<= 16; 00671 GETWORD (bit_buf, bits + 16, bit_ptr); 00672 i += tab->run; 00673 if (i < 64) 00674 goto normal_code; 00675 } 00676 break; /* illegal, check needed to avoid buffer overflow */ 00677 } 00678 dest[63] ^= mismatch & 16; 00679 DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */ 00680 decoder->bitstream_buf = bit_buf; 00681 decoder->bitstream_bits = bits; 00682 decoder->bitstream_ptr = bit_ptr; 00683 return i; 00684 } 00685 00686 static void get_mpeg1_intra_block (mpeg2_decoder_t * const decoder) 00687 { 00688 int i; 00689 int j; 00690 int val; 00691 const uint8_t * const scan = decoder->scan; 00692 const uint16_t * const quant_matrix = decoder->quantizer_matrix[0]; 00693 const DCTtab * tab; 00694 uint32_t bit_buf; 00695 int bits; 00696 const uint8_t * bit_ptr; 00697 int16_t * const dest = decoder->DCTblock; 00698 00699 i = 0; 00700 00701 bit_buf = decoder->bitstream_buf; 00702 bits = decoder->bitstream_bits; 00703 bit_ptr = decoder->bitstream_ptr; 00704 00705 NEEDBITS (bit_buf, bits, bit_ptr); 00706 00707 while (1) { 00708 if (bit_buf >= 0x28000000) { 00709 00710 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 00711 00712 i += tab->run; 00713 if (i >= 64) 00714 break; /* end of block */ 00715 00716 normal_code: 00717 j = scan[i]; 00718 bit_buf <<= tab->len; 00719 bits += tab->len + 1; 00720 val = (tab->level * quant_matrix[j]) >> 4; 00721 00722 /* oddification */ 00723 val = (val - 1) | 1; 00724 00725 /* if (bitstream_get (1)) val = -val; */ 00726 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); 00727 00728 SATURATE (val); 00729 dest[j] = val; 00730 00731 bit_buf <<= 1; 00732 NEEDBITS (bit_buf, bits, bit_ptr); 00733 00734 continue; 00735 00736 } else if (bit_buf >= 0x04000000) { 00737 00738 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 00739 00740 i += tab->run; 00741 if (i < 64) 00742 goto normal_code; 00743 00744 /* escape code */ 00745 00746 i += UBITS (bit_buf << 6, 6) - 64; 00747 if (i >= 64) 00748 break; /* illegal, check needed to avoid buffer overflow */ 00749 00750 j = scan[i]; 00751 00752 DUMPBITS (bit_buf, bits, 12); 00753 NEEDBITS (bit_buf, bits, bit_ptr); 00754 val = SBITS (bit_buf, 8); 00755 if (! (val & 0x7f)) { 00756 DUMPBITS (bit_buf, bits, 8); 00757 val = UBITS (bit_buf, 8) + 2 * val; 00758 } 00759 val = (val * quant_matrix[j]) / 16; 00760 00761 /* oddification */ 00762 val = (val + ~SBITS (val, 1)) | 1; 00763 00764 SATURATE (val); 00765 dest[j] = val; 00766 00767 DUMPBITS (bit_buf, bits, 8); 00768 NEEDBITS (bit_buf, bits, bit_ptr); 00769 00770 continue; 00771 00772 } else if (bit_buf >= 0x02000000) { 00773 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 00774 i += tab->run; 00775 if (i < 64) 00776 goto normal_code; 00777 } else if (bit_buf >= 0x00800000) { 00778 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 00779 i += tab->run; 00780 if (i < 64) 00781 goto normal_code; 00782 } else if (bit_buf >= 0x00200000) { 00783 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 00784 i += tab->run; 00785 if (i < 64) 00786 goto normal_code; 00787 } else { 00788 tab = DCT_16 + UBITS (bit_buf, 16); 00789 bit_buf <<= 16; 00790 GETWORD (bit_buf, bits + 16, bit_ptr); 00791 i += tab->run; 00792 if (i < 64) 00793 goto normal_code; 00794 } 00795 break; /* illegal, check needed to avoid buffer overflow */ 00796 } 00797 DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */ 00798 decoder->bitstream_buf = bit_buf; 00799 decoder->bitstream_bits = bits; 00800 decoder->bitstream_ptr = bit_ptr; 00801 } 00802 00803 static int get_mpeg1_non_intra_block (mpeg2_decoder_t * const decoder) 00804 { 00805 int i; 00806 int j; 00807 int val; 00808 const uint8_t * const scan = decoder->scan; 00809 const uint16_t * const quant_matrix = decoder->quantizer_matrix[1]; 00810 const DCTtab * tab; 00811 uint32_t bit_buf; 00812 int bits; 00813 const uint8_t * bit_ptr; 00814 int16_t * const dest = decoder->DCTblock; 00815 00816 i = -1; 00817 00818 bit_buf = decoder->bitstream_buf; 00819 bits = decoder->bitstream_bits; 00820 bit_ptr = decoder->bitstream_ptr; 00821 00822 NEEDBITS (bit_buf, bits, bit_ptr); 00823 if (bit_buf >= 0x28000000) { 00824 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5); 00825 goto entry_1; 00826 } else 00827 goto entry_2; 00828 00829 while (1) { 00830 if (bit_buf >= 0x28000000) { 00831 00832 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 00833 00834 entry_1: 00835 i += tab->run; 00836 if (i >= 64) 00837 break; /* end of block */ 00838 00839 normal_code: 00840 j = scan[i]; 00841 bit_buf <<= tab->len; 00842 bits += tab->len + 1; 00843 val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5; 00844 00845 /* oddification */ 00846 val = (val - 1) | 1; 00847 00848 /* if (bitstream_get (1)) val = -val; */ 00849 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); 00850 00851 SATURATE (val); 00852 dest[j] = val; 00853 00854 bit_buf <<= 1; 00855 NEEDBITS (bit_buf, bits, bit_ptr); 00856 00857 continue; 00858 00859 } 00860 00861 entry_2: 00862 if (bit_buf >= 0x04000000) { 00863 00864 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 00865 00866 i += tab->run; 00867 if (i < 64) 00868 goto normal_code; 00869 00870 /* escape code */ 00871 00872 i += UBITS (bit_buf << 6, 6) - 64; 00873 if (i >= 64) 00874 break; /* illegal, check needed to avoid buffer overflow */ 00875 00876 j = scan[i]; 00877 00878 DUMPBITS (bit_buf, bits, 12); 00879 NEEDBITS (bit_buf, bits, bit_ptr); 00880 val = SBITS (bit_buf, 8); 00881 if (! (val & 0x7f)) { 00882 DUMPBITS (bit_buf, bits, 8); 00883 val = UBITS (bit_buf, 8) + 2 * val; 00884 } 00885 val = 2 * (val + SBITS (val, 1)) + 1; 00886 val = (val * quant_matrix[j]) / 32; 00887 00888 /* oddification */ 00889 val = (val + ~SBITS (val, 1)) | 1; 00890 00891 SATURATE (val); 00892 dest[j] = val; 00893 00894 DUMPBITS (bit_buf, bits, 8); 00895 NEEDBITS (bit_buf, bits, bit_ptr); 00896 00897 continue; 00898 00899 } else if (bit_buf >= 0x02000000) { 00900 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 00901 i += tab->run; 00902 if (i < 64) 00903 goto normal_code; 00904 } else if (bit_buf >= 0x00800000) { 00905 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 00906 i += tab->run; 00907 if (i < 64) 00908 goto normal_code; 00909 } else if (bit_buf >= 0x00200000) { 00910 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 00911 i += tab->run; 00912 if (i < 64) 00913 goto normal_code; 00914 } else { 00915 tab = DCT_16 + UBITS (bit_buf, 16); 00916 bit_buf <<= 16; 00917 GETWORD (bit_buf, bits + 16, bit_ptr); 00918 i += tab->run; 00919 if (i < 64) 00920 goto normal_code; 00921 } 00922 break; /* illegal, check needed to avoid buffer overflow */ 00923 } 00924 DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */ 00925 decoder->bitstream_buf = bit_buf; 00926 decoder->bitstream_bits = bits; 00927 decoder->bitstream_ptr = bit_ptr; 00928 return i; 00929 } 00930 00931 static inline void slice_intra_DCT (mpeg2_decoder_t * const decoder, 00932 const int cc, 00933 uint8_t * const dest, const int stride) 00934 { 00935 #define bit_buf (decoder->bitstream_buf) 00936 #define bits (decoder->bitstream_bits) 00937 #define bit_ptr (decoder->bitstream_ptr) 00938 NEEDBITS (bit_buf, bits, bit_ptr); 00939 /* Get the intra DC coefficient and inverse quantize it */ 00940 if (cc == 0) 00941 decoder->DCTblock[0] = 00942 decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder); 00943 else 00944 decoder->DCTblock[0] = 00945 decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff (decoder); 00946 00947 if (decoder->mpeg1) { 00948 if (decoder->coding_type != D_TYPE) 00949 get_mpeg1_intra_block (decoder); 00950 } else if (decoder->intra_vlc_format) 00951 get_intra_block_B15 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]); 00952 else 00953 get_intra_block_B14 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]); 00954 mpeg2_idct_copy (decoder->DCTblock, dest, stride); 00955 #undef bit_buf 00956 #undef bits 00957 #undef bit_ptr 00958 } 00959 00960 static inline void slice_non_intra_DCT (mpeg2_decoder_t * const decoder, 00961 const int cc, 00962 uint8_t * const dest, const int stride) 00963 { 00964 int last; 00965 00966 if (decoder->mpeg1) 00967 last = get_mpeg1_non_intra_block (decoder); 00968 else 00969 last = get_non_intra_block (decoder, 00970 decoder->quantizer_matrix[cc ? 3 : 1]); 00971 mpeg2_idct_add (last, decoder->DCTblock, dest, stride); 00972 } 00973 00974 #define MOTION_420(table,ref,motion_x,motion_y,size,y) \ 00975 pos_x = 2 * decoder->offset + motion_x; \ 00976 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \ 00977 if (unlikely (pos_x > decoder->limit_x)) { \ 00978 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 00979 motion_x = pos_x - 2 * decoder->offset; \ 00980 } \ 00981 if (unlikely (pos_y > decoder->limit_y_ ## size)) { \ 00982 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \ 00983 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \ 00984 } \ 00985 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 00986 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \ 00987 ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride, \ 00988 decoder->stride, size); \ 00989 motion_x /= 2; motion_y /= 2; \ 00990 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ 00991 offset = (((decoder->offset + motion_x) >> 1) + \ 00992 ((((decoder->v_offset + motion_y) >> 1) + y/2) * \ 00993 decoder->uv_stride)); \ 00994 table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride + \ 00995 (decoder->offset >> 1), ref[1] + offset, \ 00996 decoder->uv_stride, size/2); \ 00997 table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride + \ 00998 (decoder->offset >> 1), ref[2] + offset, \ 00999 decoder->uv_stride, size/2) 01000 01001 #define MOTION_FIELD_420(table,ref,motion_x,motion_y,dest_field,op,src_field) \ 01002 pos_x = 2 * decoder->offset + motion_x; \ 01003 pos_y = decoder->v_offset + motion_y; \ 01004 if (unlikely (pos_x > decoder->limit_x)) { \ 01005 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 01006 motion_x = pos_x - 2 * decoder->offset; \ 01007 } \ 01008 if (unlikely (pos_y > decoder->limit_y)) { \ 01009 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 01010 motion_y = pos_y - decoder->v_offset; \ 01011 } \ 01012 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 01013 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \ 01014 decoder->offset, \ 01015 (ref[0] + (pos_x >> 1) + \ 01016 ((pos_y op) + src_field) * decoder->stride), \ 01017 2 * decoder->stride, 8); \ 01018 motion_x /= 2; motion_y /= 2; \ 01019 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ 01020 offset = (((decoder->offset + motion_x) >> 1) + \ 01021 (((decoder->v_offset >> 1) + (motion_y op) + src_field) * \ 01022 decoder->uv_stride)); \ 01023 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \ 01024 (decoder->offset >> 1), ref[1] + offset, \ 01025 2 * decoder->uv_stride, 4); \ 01026 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \ 01027 (decoder->offset >> 1), ref[2] + offset, \ 01028 2 * decoder->uv_stride, 4) 01029 01030 #define MOTION_DMV_420(table,ref,motion_x,motion_y) \ 01031 pos_x = 2 * decoder->offset + motion_x; \ 01032 pos_y = decoder->v_offset + motion_y; \ 01033 if (unlikely (pos_x > decoder->limit_x)) { \ 01034 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 01035 motion_x = pos_x - 2 * decoder->offset; \ 01036 } \ 01037 if (unlikely (pos_y > decoder->limit_y)) { \ 01038 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 01039 motion_y = pos_y - decoder->v_offset; \ 01040 } \ 01041 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 01042 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \ 01043 table[xy_half] (decoder->dest[0] + decoder->offset, \ 01044 ref[0] + offset, 2 * decoder->stride, 8); \ 01045 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \ 01046 ref[0] + decoder->stride + offset, \ 01047 2 * decoder->stride, 8); \ 01048 motion_x /= 2; motion_y /= 2; \ 01049 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ 01050 offset = (((decoder->offset + motion_x) >> 1) + \ 01051 (((decoder->v_offset >> 1) + (motion_y & ~1)) * \ 01052 decoder->uv_stride)); \ 01053 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \ 01054 ref[1] + offset, 2 * decoder->uv_stride, 4); \ 01055 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \ 01056 (decoder->offset >> 1), \ 01057 ref[1] + decoder->uv_stride + offset, \ 01058 2 * decoder->uv_stride, 4); \ 01059 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \ 01060 ref[2] + offset, 2 * decoder->uv_stride, 4); \ 01061 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \ 01062 (decoder->offset >> 1), \ 01063 ref[2] + decoder->uv_stride + offset, \ 01064 2 * decoder->uv_stride, 4) 01065 01066 #define MOTION_ZERO_420(table,ref) \ 01067 table[0] (decoder->dest[0] + decoder->offset, \ 01068 (ref[0] + decoder->offset + \ 01069 decoder->v_offset * decoder->stride), decoder->stride, 16); \ 01070 offset = ((decoder->offset >> 1) + \ 01071 (decoder->v_offset >> 1) * decoder->uv_stride); \ 01072 table[4] (decoder->dest[1] + (decoder->offset >> 1), \ 01073 ref[1] + offset, decoder->uv_stride, 8); \ 01074 table[4] (decoder->dest[2] + (decoder->offset >> 1), \ 01075 ref[2] + offset, decoder->uv_stride, 8) 01076 01077 #define MOTION_422(table,ref,motion_x,motion_y,size,y) \ 01078 pos_x = 2 * decoder->offset + motion_x; \ 01079 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \ 01080 if (unlikely (pos_x > decoder->limit_x)) { \ 01081 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 01082 motion_x = pos_x - 2 * decoder->offset; \ 01083 } \ 01084 if (unlikely (pos_y > decoder->limit_y_ ## size)) { \ 01085 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \ 01086 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \ 01087 } \ 01088 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 01089 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \ 01090 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \ 01091 ref[0] + offset, decoder->stride, size); \ 01092 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \ 01093 motion_x /= 2; \ 01094 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \ 01095 table[4+xy_half] (decoder->dest[1] + y * decoder->uv_stride + \ 01096 (decoder->offset >> 1), ref[1] + offset, \ 01097 decoder->uv_stride, size); \ 01098 table[4+xy_half] (decoder->dest[2] + y * decoder->uv_stride + \ 01099 (decoder->offset >> 1), ref[2] + offset, \ 01100 decoder->uv_stride, size) 01101 01102 #define MOTION_FIELD_422(table,ref,motion_x,motion_y,dest_field,op,src_field) \ 01103 pos_x = 2 * decoder->offset + motion_x; \ 01104 pos_y = decoder->v_offset + motion_y; \ 01105 if (unlikely (pos_x > decoder->limit_x)) { \ 01106 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 01107 motion_x = pos_x - 2 * decoder->offset; \ 01108 } \ 01109 if (unlikely (pos_y > decoder->limit_y)) { \ 01110 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 01111 motion_y = pos_y - decoder->v_offset; \ 01112 } \ 01113 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 01114 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \ 01115 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \ 01116 decoder->offset, ref[0] + offset, \ 01117 2 * decoder->stride, 8); \ 01118 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \ 01119 motion_x /= 2; \ 01120 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \ 01121 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \ 01122 (decoder->offset >> 1), ref[1] + offset, \ 01123 2 * decoder->uv_stride, 8); \ 01124 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \ 01125 (decoder->offset >> 1), ref[2] + offset, \ 01126 2 * decoder->uv_stride, 8) 01127 01128 #define MOTION_DMV_422(table,ref,motion_x,motion_y) \ 01129 pos_x = 2 * decoder->offset + motion_x; \ 01130 pos_y = decoder->v_offset + motion_y; \ 01131 if (unlikely (pos_x > decoder->limit_x)) { \ 01132 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 01133 motion_x = pos_x - 2 * decoder->offset; \ 01134 } \ 01135 if (unlikely (pos_y > decoder->limit_y)) { \ 01136 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 01137 motion_y = pos_y - decoder->v_offset; \ 01138 } \ 01139 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 01140 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \ 01141 table[xy_half] (decoder->dest[0] + decoder->offset, \ 01142 ref[0] + offset, 2 * decoder->stride, 8); \ 01143 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \ 01144 ref[0] + decoder->stride + offset, \ 01145 2 * decoder->stride, 8); \ 01146 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \ 01147 motion_x /= 2; \ 01148 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \ 01149 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \ 01150 ref[1] + offset, 2 * decoder->uv_stride, 8); \ 01151 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \ 01152 (decoder->offset >> 1), \ 01153 ref[1] + decoder->uv_stride + offset, \ 01154 2 * decoder->uv_stride, 8); \ 01155 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \ 01156 ref[2] + offset, 2 * decoder->uv_stride, 8); \ 01157 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \ 01158 (decoder->offset >> 1), \ 01159 ref[2] + decoder->uv_stride + offset, \ 01160 2 * decoder->uv_stride, 8) 01161 01162 #define MOTION_ZERO_422(table,ref) \ 01163 offset = decoder->offset + decoder->v_offset * decoder->stride; \ 01164 table[0] (decoder->dest[0] + decoder->offset, \ 01165 ref[0] + offset, decoder->stride, 16); \ 01166 offset >>= 1; \ 01167 table[4] (decoder->dest[1] + (decoder->offset >> 1), \ 01168 ref[1] + offset, decoder->uv_stride, 16); \ 01169 table[4] (decoder->dest[2] + (decoder->offset >> 1), \ 01170 ref[2] + offset, decoder->uv_stride, 16) 01171 01172 #define MOTION_444(table,ref,motion_x,motion_y,size,y) \ 01173 pos_x = 2 * decoder->offset + motion_x; \ 01174 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \ 01175 if (unlikely (pos_x > decoder->limit_x)) { \ 01176 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 01177 motion_x = pos_x - 2 * decoder->offset; \ 01178 } \ 01179 if (unlikely (pos_y > decoder->limit_y_ ## size)) { \ 01180 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \ 01181 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \ 01182 } \ 01183 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 01184 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \ 01185 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \ 01186 ref[0] + offset, decoder->stride, size); \ 01187 table[xy_half] (decoder->dest[1] + y * decoder->stride + decoder->offset, \ 01188 ref[1] + offset, decoder->stride, size); \ 01189 table[xy_half] (decoder->dest[2] + y * decoder->stride + decoder->offset, \ 01190 ref[2] + offset, decoder->stride, size) 01191 01192 #define MOTION_FIELD_444(table,ref,motion_x,motion_y,dest_field,op,src_field) \ 01193 pos_x = 2 * decoder->offset + motion_x; \ 01194 pos_y = decoder->v_offset + motion_y; \ 01195 if (unlikely (pos_x > decoder->limit_x)) { \ 01196 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 01197 motion_x = pos_x - 2 * decoder->offset; \ 01198 } \ 01199 if (unlikely (pos_y > decoder->limit_y)) { \ 01200 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 01201 motion_y = pos_y - decoder->v_offset; \ 01202 } \ 01203 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 01204 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \ 01205 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \ 01206 decoder->offset, ref[0] + offset, \ 01207 2 * decoder->stride, 8); \ 01208 table[xy_half] (decoder->dest[1] + dest_field * decoder->stride + \ 01209 decoder->offset, ref[1] + offset, \ 01210 2 * decoder->stride, 8); \ 01211 table[xy_half] (decoder->dest[2] + dest_field * decoder->stride + \ 01212 decoder->offset, ref[2] + offset, \ 01213 2 * decoder->stride, 8) 01214 01215 #define MOTION_DMV_444(table,ref,motion_x,motion_y) \ 01216 pos_x = 2 * decoder->offset + motion_x; \ 01217 pos_y = decoder->v_offset + motion_y; \ 01218 if (unlikely (pos_x > decoder->limit_x)) { \ 01219 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 01220 motion_x = pos_x - 2 * decoder->offset; \ 01221 } \ 01222 if (unlikely (pos_y > decoder->limit_y)) { \ 01223 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 01224 motion_y = pos_y - decoder->v_offset; \ 01225 } \ 01226 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 01227 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \ 01228 table[xy_half] (decoder->dest[0] + decoder->offset, \ 01229 ref[0] + offset, 2 * decoder->stride, 8); \ 01230 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \ 01231 ref[0] + decoder->stride + offset, \ 01232 2 * decoder->stride, 8); \ 01233 table[xy_half] (decoder->dest[1] + decoder->offset, \ 01234 ref[1] + offset, 2 * decoder->stride, 8); \ 01235 table[xy_half] (decoder->dest[1] + decoder->stride + decoder->offset, \ 01236 ref[1] + decoder->stride + offset, \ 01237 2 * decoder->stride, 8); \ 01238 table[xy_half] (decoder->dest[2] + decoder->offset, \ 01239 ref[2] + offset, 2 * decoder->stride, 8); \ 01240 table[xy_half] (decoder->dest[2] + decoder->stride + decoder->offset, \ 01241 ref[2] + decoder->stride + offset, \ 01242 2 * decoder->stride, 8) 01243 01244 #define MOTION_ZERO_444(table,ref) \ 01245 offset = decoder->offset + decoder->v_offset * decoder->stride; \ 01246 table[0] (decoder->dest[0] + decoder->offset, \ 01247 ref[0] + offset, decoder->stride, 16); \ 01248 table[4] (decoder->dest[1] + decoder->offset, \ 01249 ref[1] + offset, decoder->stride, 16); \ 01250 table[4] (decoder->dest[2] + decoder->offset, \ 01251 ref[2] + offset, decoder->stride, 16) 01252 01253 #define bit_buf (decoder->bitstream_buf) 01254 #define bits (decoder->bitstream_bits) 01255 #define bit_ptr (decoder->bitstream_ptr) 01256 01257 static void motion_mp1 (mpeg2_decoder_t * const decoder, 01258 motion_t * const motion, 01259 mpeg2_mc_fct * const * const table) 01260 { 01261 int motion_x, motion_y; 01262 unsigned int pos_x, pos_y, xy_half, offset; 01263 01264 NEEDBITS (bit_buf, bits, bit_ptr); 01265 motion_x = (motion->pmv[0][0] + 01266 (get_motion_delta (decoder, 01267 motion->f_code[0]) << motion->f_code[1])); 01268 motion_x = bound_motion_vector (motion_x, 01269 motion->f_code[0] + motion->f_code[1]); 01270 motion->pmv[0][0] = motion_x; 01271 01272 NEEDBITS (bit_buf, bits, bit_ptr); 01273 motion_y = (motion->pmv[0][1] + 01274 (get_motion_delta (decoder, 01275 motion->f_code[0]) << motion->f_code[1])); 01276 motion_y = bound_motion_vector (motion_y, 01277 motion->f_code[0] + motion->f_code[1]); 01278 motion->pmv[0][1] = motion_y; 01279 01280 MOTION_420 (table, motion->ref[0], motion_x, motion_y, 16, 0); 01281 } 01282 01283 #define MOTION_FUNCTIONS(FORMAT,MOTION,MOTION_FIELD,MOTION_DMV,MOTION_ZERO) \ 01284 \ 01285 static void motion_fr_frame_##FORMAT (mpeg2_decoder_t * const decoder, \ 01286 motion_t * const motion, \ 01287 mpeg2_mc_fct * const * const table) \ 01288 { \ 01289 int motion_x, motion_y; \ 01290 unsigned int pos_x, pos_y, xy_half, offset; \ 01291 \ 01292 NEEDBITS (bit_buf, bits, bit_ptr); \ 01293 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 01294 motion->f_code[0]); \ 01295 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 01296 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \ 01297 \ 01298 NEEDBITS (bit_buf, bits, bit_ptr); \ 01299 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \ 01300 motion->f_code[1]); \ 01301 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \ 01302 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \ 01303 \ 01304 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \ 01305 } \ 01306 \ 01307 static void motion_fr_field_##FORMAT (mpeg2_decoder_t * const decoder, \ 01308 motion_t * const motion, \ 01309 mpeg2_mc_fct * const * const table) \ 01310 { \ 01311 int motion_x, motion_y, field; \ 01312 unsigned int pos_x, pos_y, xy_half, offset; \ 01313 \ 01314 NEEDBITS (bit_buf, bits, bit_ptr); \ 01315 field = UBITS (bit_buf, 1); \ 01316 DUMPBITS (bit_buf, bits, 1); \ 01317 \ 01318 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 01319 motion->f_code[0]); \ 01320 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 01321 motion->pmv[0][0] = motion_x; \ 01322 \ 01323 NEEDBITS (bit_buf, bits, bit_ptr); \ 01324 motion_y = ((motion->pmv[0][1] >> 1) + \ 01325 get_motion_delta (decoder, motion->f_code[1])); \ 01326 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \ 01327 motion->pmv[0][1] = motion_y << 1; \ 01328 \ 01329 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field); \ 01330 \ 01331 NEEDBITS (bit_buf, bits, bit_ptr); \ 01332 field = UBITS (bit_buf, 1); \ 01333 DUMPBITS (bit_buf, bits, 1); \ 01334 \ 01335 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \ 01336 motion->f_code[0]); \ 01337 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 01338 motion->pmv[1][0] = motion_x; \ 01339 \ 01340 NEEDBITS (bit_buf, bits, bit_ptr); \ 01341 motion_y = ((motion->pmv[1][1] >> 1) + \ 01342 get_motion_delta (decoder, motion->f_code[1])); \ 01343 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \ 01344 motion->pmv[1][1] = motion_y << 1; \ 01345 \ 01346 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field); \ 01347 } \ 01348 \ 01349 static void motion_fr_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \ 01350 motion_t * const motion, \ 01351 mpeg2_mc_fct * const * const table) \ 01352 { \ 01353 int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y; \ 01354 unsigned int pos_x, pos_y, xy_half, offset; \ 01355 \ 01356 NEEDBITS (bit_buf, bits, bit_ptr); \ 01357 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 01358 motion->f_code[0]); \ 01359 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 01360 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \ 01361 NEEDBITS (bit_buf, bits, bit_ptr); \ 01362 dmv_x = get_dmv (decoder); \ 01363 \ 01364 motion_y = ((motion->pmv[0][1] >> 1) + \ 01365 get_motion_delta (decoder, motion->f_code[1])); \ 01366 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \ 01367 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1; \ 01368 dmv_y = get_dmv (decoder); \ 01369 \ 01370 m = decoder->top_field_first ? 1 : 3; \ 01371 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \ 01372 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1; \ 01373 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0); \ 01374 \ 01375 m = decoder->top_field_first ? 3 : 1; \ 01376 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \ 01377 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1; \ 01378 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);\ 01379 \ 01380 MOTION_DMV (mpeg2_mc.avg, motion->ref[0], motion_x, motion_y); \ 01381 } \ 01382 \ 01383 static void motion_reuse_##FORMAT (mpeg2_decoder_t * const decoder, \ 01384 motion_t * const motion, \ 01385 mpeg2_mc_fct * const * const table) \ 01386 { \ 01387 int motion_x, motion_y; \ 01388 unsigned int pos_x, pos_y, xy_half, offset; \ 01389 \ 01390 motion_x = motion->pmv[0][0]; \ 01391 motion_y = motion->pmv[0][1]; \ 01392 \ 01393 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \ 01394 } \ 01395 \ 01396 static void motion_zero_##FORMAT (mpeg2_decoder_t * const decoder, \ 01397 motion_t * const motion, \ 01398 mpeg2_mc_fct * const * const table) \ 01399 { \ 01400 unsigned int offset; \ 01401 \ 01402 motion->pmv[0][0] = motion->pmv[0][1] = 0; \ 01403 motion->pmv[1][0] = motion->pmv[1][1] = 0; \ 01404 \ 01405 MOTION_ZERO (table, motion->ref[0]); \ 01406 } \ 01407 \ 01408 static void motion_fi_field_##FORMAT (mpeg2_decoder_t * const decoder, \ 01409 motion_t * const motion, \ 01410 mpeg2_mc_fct * const * const table) \ 01411 { \ 01412 int motion_x, motion_y; \ 01413 uint8_t ** ref_field; \ 01414 unsigned int pos_x, pos_y, xy_half, offset; \ 01415 \ 01416 NEEDBITS (bit_buf, bits, bit_ptr); \ 01417 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \ 01418 DUMPBITS (bit_buf, bits, 1); \ 01419 \ 01420 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 01421 motion->f_code[0]); \ 01422 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 01423 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \ 01424 \ 01425 NEEDBITS (bit_buf, bits, bit_ptr); \ 01426 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \ 01427 motion->f_code[1]); \ 01428 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \ 01429 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \ 01430 \ 01431 MOTION (table, ref_field, motion_x, motion_y, 16, 0); \ 01432 } \ 01433 \ 01434 static void motion_fi_16x8_##FORMAT (mpeg2_decoder_t * const decoder, \ 01435 motion_t * const motion, \ 01436 mpeg2_mc_fct * const * const table) \ 01437 { \ 01438 int motion_x, motion_y; \ 01439 uint8_t ** ref_field; \ 01440 unsigned int pos_x, pos_y, xy_half, offset; \ 01441 \ 01442 NEEDBITS (bit_buf, bits, bit_ptr); \ 01443 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \ 01444 DUMPBITS (bit_buf, bits, 1); \ 01445 \ 01446 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 01447 motion->f_code[0]); \ 01448 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 01449 motion->pmv[0][0] = motion_x; \ 01450 \ 01451 NEEDBITS (bit_buf, bits, bit_ptr); \ 01452 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \ 01453 motion->f_code[1]); \ 01454 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \ 01455 motion->pmv[0][1] = motion_y; \ 01456 \ 01457 MOTION (table, ref_field, motion_x, motion_y, 8, 0); \ 01458 \ 01459 NEEDBITS (bit_buf, bits, bit_ptr); \ 01460 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \ 01461 DUMPBITS (bit_buf, bits, 1); \ 01462 \ 01463 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \ 01464 motion->f_code[0]); \ 01465 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 01466 motion->pmv[1][0] = motion_x; \ 01467 \ 01468 NEEDBITS (bit_buf, bits, bit_ptr); \ 01469 motion_y = motion->pmv[1][1] + get_motion_delta (decoder, \ 01470 motion->f_code[1]); \ 01471 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \ 01472 motion->pmv[1][1] = motion_y; \ 01473 \ 01474 MOTION (table, ref_field, motion_x, motion_y, 8, 8); \ 01475 } \ 01476 \ 01477 static void motion_fi_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \ 01478 motion_t * const motion, \ 01479 mpeg2_mc_fct * const * const table) \ 01480 { \ 01481 int motion_x, motion_y, other_x, other_y; \ 01482 unsigned int pos_x, pos_y, xy_half, offset; \ 01483 \ 01484 NEEDBITS (bit_buf, bits, bit_ptr); \ 01485 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 01486 motion->f_code[0]); \ 01487 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 01488 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \ 01489 NEEDBITS (bit_buf, bits, bit_ptr); \ 01490 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder); \ 01491 \ 01492 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \ 01493 motion->f_code[1]); \ 01494 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \ 01495 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \ 01496 other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) + \ 01497 decoder->dmv_offset); \ 01498 \ 01499 MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0); \ 01500 MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0); \ 01501 } \ 01502 01503 MOTION_FUNCTIONS (420, MOTION_420, MOTION_FIELD_420, MOTION_DMV_420, 01504 MOTION_ZERO_420) 01505 MOTION_FUNCTIONS (422, MOTION_422, MOTION_FIELD_422, MOTION_DMV_422, 01506 MOTION_ZERO_422) 01507 MOTION_FUNCTIONS (444, MOTION_444, MOTION_FIELD_444, MOTION_DMV_444, 01508 MOTION_ZERO_444) 01509 01510 /* like motion_frame, but parsing without actual motion compensation */ 01511 static void motion_fr_conceal (mpeg2_decoder_t * const decoder) 01512 { 01513 int tmp; 01514 01515 NEEDBITS (bit_buf, bits, bit_ptr); 01516 tmp = (decoder->f_motion.pmv[0][0] + 01517 get_motion_delta (decoder, decoder->f_motion.f_code[0])); 01518 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]); 01519 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp; 01520 01521 NEEDBITS (bit_buf, bits, bit_ptr); 01522 tmp = (decoder->f_motion.pmv[0][1] + 01523 get_motion_delta (decoder, decoder->f_motion.f_code[1])); 01524 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]); 01525 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp; 01526 01527 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */ 01528 } 01529 01530 static void motion_fi_conceal (mpeg2_decoder_t * const decoder) 01531 { 01532 int tmp; 01533 01534 NEEDBITS (bit_buf, bits, bit_ptr); 01535 DUMPBITS (bit_buf, bits, 1); /* remove field_select */ 01536 01537 tmp = (decoder->f_motion.pmv[0][0] + 01538 get_motion_delta (decoder, decoder->f_motion.f_code[0])); 01539 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]); 01540 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp; 01541 01542 NEEDBITS (bit_buf, bits, bit_ptr); 01543 tmp = (decoder->f_motion.pmv[0][1] + 01544 get_motion_delta (decoder, decoder->f_motion.f_code[1])); 01545 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]); 01546 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp; 01547 01548 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */ 01549 } 01550 01551 #undef bit_buf 01552 #undef bits 01553 #undef bit_ptr 01554 01555 #define MOTION_CALL(routine,direction) \ 01556 do { \ 01557 if ((direction) & MACROBLOCK_MOTION_FORWARD) \ 01558 routine (decoder, &(decoder->f_motion), mpeg2_mc.put); \ 01559 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \ 01560 routine (decoder, &(decoder->b_motion), \ 01561 ((direction) & MACROBLOCK_MOTION_FORWARD ? \ 01562 mpeg2_mc.avg : mpeg2_mc.put)); \ 01563 } while (0) 01564 01565 #define NEXT_MACROBLOCK \ 01566 do { \ 01567 decoder->offset += 16; \ 01568 if (decoder->offset == decoder->width) { \ 01569 do { /* just so we can use the break statement */ \ 01570 if (decoder->convert) { \ 01571 decoder->convert (decoder->convert_id, decoder->dest, \ 01572 decoder->v_offset); \ 01573 if (decoder->coding_type == B_TYPE) \ 01574 break; \ 01575 } \ 01576 decoder->dest[0] += decoder->slice_stride; \ 01577 decoder->dest[1] += decoder->slice_uv_stride; \ 01578 decoder->dest[2] += decoder->slice_uv_stride; \ 01579 } while (0); \ 01580 decoder->v_offset += 16; \ 01581 if (decoder->v_offset > decoder->limit_y) { \ 01582 if (mpeg2_cpu_state_restore) \ 01583 mpeg2_cpu_state_restore (&cpu_state); \ 01584 return; \ 01585 } \ 01586 decoder->offset = 0; \ 01587 } \ 01588 } while (0) 01589 01590 void mpeg2_init_fbuf (mpeg2_decoder_t * decoder, uint8_t * current_fbuf[3], 01591 uint8_t * forward_fbuf[3], uint8_t * backward_fbuf[3]) 01592 { 01593 int offset, stride, height, bottom_field; 01594 01595 stride = decoder->stride_frame; 01596 bottom_field = (decoder->picture_structure == BOTTOM_FIELD); 01597 offset = bottom_field ? stride : 0; 01598 height = decoder->height; 01599 01600 decoder->picture_dest[0] = current_fbuf[0] + offset; 01601 decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1); 01602 decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1); 01603 01604 decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset; 01605 decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1); 01606 decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1); 01607 01608 decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset; 01609 decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1); 01610 decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1); 01611 01612 if (decoder->picture_structure != FRAME_PICTURE) { 01613 decoder->dmv_offset = bottom_field ? 1 : -1; 01614 decoder->f_motion.ref2[0] = decoder->f_motion.ref[bottom_field]; 01615 decoder->f_motion.ref2[1] = decoder->f_motion.ref[!bottom_field]; 01616 decoder->b_motion.ref2[0] = decoder->b_motion.ref[bottom_field]; 01617 decoder->b_motion.ref2[1] = decoder->b_motion.ref[!bottom_field]; 01618 offset = stride - offset; 01619 01620 if (decoder->second_field && (decoder->coding_type != B_TYPE)) 01621 forward_fbuf = current_fbuf; 01622 01623 decoder->f_motion.ref[1][0] = forward_fbuf[0] + offset; 01624 decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1); 01625 decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1); 01626 01627 decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset; 01628 decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1); 01629 decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1); 01630 01631 stride <<= 1; 01632 height >>= 1; 01633 } 01634 01635 decoder->stride = stride; 01636 decoder->uv_stride = stride >> 1; 01637 decoder->slice_stride = 16 * stride; 01638 decoder->slice_uv_stride = 01639 decoder->slice_stride >> (2 - decoder->chroma_format); 01640 decoder->limit_x = 2 * decoder->width - 32; 01641 decoder->limit_y_16 = 2 * height - 32; 01642 decoder->limit_y_8 = 2 * height - 16; 01643 decoder->limit_y = height - 16; 01644 01645 if (decoder->mpeg1) { 01646 decoder->motion_parser[0] = motion_zero_420; 01647 decoder->motion_parser[MC_FRAME] = motion_mp1; 01648 decoder->motion_parser[4] = motion_reuse_420; 01649 } else if (decoder->picture_structure == FRAME_PICTURE) { 01650 if (decoder->chroma_format == 0) { 01651 decoder->motion_parser[0] = motion_zero_420; 01652 decoder->motion_parser[MC_FIELD] = motion_fr_field_420; 01653 decoder->motion_parser[MC_FRAME] = motion_fr_frame_420; 01654 decoder->motion_parser[MC_DMV] = motion_fr_dmv_420; 01655 decoder->motion_parser[4] = motion_reuse_420; 01656 } else if (decoder->chroma_format == 1) { 01657 decoder->motion_parser[0] = motion_zero_422; 01658 decoder->motion_parser[MC_FIELD] = motion_fr_field_422; 01659 decoder->motion_parser[MC_FRAME] = motion_fr_frame_422; 01660 decoder->motion_parser[MC_DMV] = motion_fr_dmv_422; 01661 decoder->motion_parser[4] = motion_reuse_422; 01662 } else { 01663 decoder->motion_parser[0] = motion_zero_444; 01664 decoder->motion_parser[MC_FIELD] = motion_fr_field_444; 01665 decoder->motion_parser[MC_FRAME] = motion_fr_frame_444; 01666 decoder->motion_parser[MC_DMV] = motion_fr_dmv_444; 01667 decoder->motion_parser[4] = motion_reuse_444; 01668 } 01669 } else { 01670 if (decoder->chroma_format == 0) { 01671 decoder->motion_parser[0] = motion_zero_420; 01672 decoder->motion_parser[MC_FIELD] = motion_fi_field_420; 01673 decoder->motion_parser[MC_16X8] = motion_fi_16x8_420; 01674 decoder->motion_parser[MC_DMV] = motion_fi_dmv_420; 01675 decoder->motion_parser[4] = motion_reuse_420; 01676 } else if (decoder->chroma_format == 1) { 01677 decoder->motion_parser[0] = motion_zero_422; 01678 decoder->motion_parser[MC_FIELD] = motion_fi_field_422; 01679 decoder->motion_parser[MC_16X8] = motion_fi_16x8_422; 01680 decoder->motion_parser[MC_DMV] = motion_fi_dmv_422; 01681 decoder->motion_parser[4] = motion_reuse_422; 01682 } else { 01683 decoder->motion_parser[0] = motion_zero_444; 01684 decoder->motion_parser[MC_FIELD] = motion_fi_field_444; 01685 decoder->motion_parser[MC_16X8] = motion_fi_16x8_444; 01686 decoder->motion_parser[MC_DMV] = motion_fi_dmv_444; 01687 decoder->motion_parser[4] = motion_reuse_444; 01688 } 01689 } 01690 } 01691 01692 static inline int slice_init (mpeg2_decoder_t * const decoder, int code) 01693 { 01694 #define bit_buf (decoder->bitstream_buf) 01695 #define bits (decoder->bitstream_bits) 01696 #define bit_ptr (decoder->bitstream_ptr) 01697 int offset; 01698 const MBAtab * mba; 01699 01700 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] = 01701 decoder->dc_dct_pred[2] = 16384; 01702 01703 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0; 01704 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0; 01705 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0; 01706 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0; 01707 01708 if (decoder->vertical_position_extension) { 01709 code += UBITS (bit_buf, 3) << 7; 01710 DUMPBITS (bit_buf, bits, 3); 01711 } 01712 decoder->v_offset = (code - 1) * 16; 01713 offset = 0; 01714 if (!(decoder->convert) || decoder->coding_type != B_TYPE) 01715 offset = (code - 1) * decoder->slice_stride; 01716 01717 decoder->dest[0] = decoder->picture_dest[0] + offset; 01718 offset >>= (2 - decoder->chroma_format); 01719 decoder->dest[1] = decoder->picture_dest[1] + offset; 01720 decoder->dest[2] = decoder->picture_dest[2] + offset; 01721 01722 get_quantizer_scale (decoder); 01723 01724 /* ignore intra_slice and all the extra data */ 01725 while (bit_buf & 0x80000000) { 01726 DUMPBITS (bit_buf, bits, 9); 01727 NEEDBITS (bit_buf, bits, bit_ptr); 01728 } 01729 01730 /* decode initial macroblock address increment */ 01731 offset = 0; 01732 while (1) { 01733 if (bit_buf >= 0x08000000) { 01734 mba = MBA_5 + (UBITS (bit_buf, 6) - 2); 01735 break; 01736 } else if (bit_buf >= 0x01800000) { 01737 mba = MBA_11 + (UBITS (bit_buf, 12) - 24); 01738 break; 01739 } else switch (UBITS (bit_buf, 12)) { 01740 case 8: /* macroblock_escape */ 01741 offset += 33; 01742 DUMPBITS (bit_buf, bits, 11); 01743 NEEDBITS (bit_buf, bits, bit_ptr); 01744 continue; 01745 case 15: /* macroblock_stuffing (MPEG1 only) */ 01746 bit_buf &= 0xfffff; 01747 DUMPBITS (bit_buf, bits, 11); 01748 NEEDBITS (bit_buf, bits, bit_ptr); 01749 continue; 01750 default: /* error */ 01751 return 1; 01752 } 01753 } 01754 DUMPBITS (bit_buf, bits, mba->len + 1); 01755 decoder->offset = (offset + mba->mba) << 4; 01756 01757 while (decoder->offset - decoder->width >= 0) { 01758 decoder->offset -= decoder->width; 01759 if (!(decoder->convert) || decoder->coding_type != B_TYPE) { 01760 decoder->dest[0] += decoder->slice_stride; 01761 decoder->dest[1] += decoder->slice_uv_stride; 01762 decoder->dest[2] += decoder->slice_uv_stride; 01763 } 01764 decoder->v_offset += 16; 01765 } 01766 if (decoder->v_offset > decoder->limit_y) 01767 return 1; 01768 01769 return 0; 01770 #undef bit_buf 01771 #undef bits 01772 #undef bit_ptr 01773 } 01774 01775 void mpeg2_slice (mpeg2_decoder_t * const decoder, const int code, 01776 const uint8_t * const buffer) 01777 { 01778 #define bit_buf (decoder->bitstream_buf) 01779 #define bits (decoder->bitstream_bits) 01780 #define bit_ptr (decoder->bitstream_ptr) 01781 cpu_state_t cpu_state; 01782 01783 bitstream_init (decoder, buffer); 01784 01785 if (slice_init (decoder, code)) 01786 return; 01787 01788 if (mpeg2_cpu_state_save) 01789 mpeg2_cpu_state_save (&cpu_state); 01790 01791 while (1) { 01792 int macroblock_modes; 01793 int mba_inc; 01794 const MBAtab * mba; 01795 01796 NEEDBITS (bit_buf, bits, bit_ptr); 01797 01798 macroblock_modes = get_macroblock_modes (decoder); 01799 01800 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */ 01801 if (macroblock_modes & MACROBLOCK_QUANT) 01802 get_quantizer_scale (decoder); 01803 01804 if (macroblock_modes & MACROBLOCK_INTRA) { 01805 01806 int DCT_offset, DCT_stride; 01807 int offset; 01808 uint8_t * dest_y; 01809 01810 if (decoder->concealment_motion_vectors) { 01811 if (decoder->picture_structure == FRAME_PICTURE) 01812 motion_fr_conceal (decoder); 01813 else 01814 motion_fi_conceal (decoder); 01815 } else { 01816 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0; 01817 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0; 01818 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0; 01819 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0; 01820 } 01821 01822 if (macroblock_modes & DCT_TYPE_INTERLACED) { 01823 DCT_offset = decoder->stride; 01824 DCT_stride = decoder->stride * 2; 01825 } else { 01826 DCT_offset = decoder->stride * 8; 01827 DCT_stride = decoder->stride; 01828 } 01829 01830 offset = decoder->offset; 01831 dest_y = decoder->dest[0] + offset; 01832 slice_intra_DCT (decoder, 0, dest_y, DCT_stride); 01833 slice_intra_DCT (decoder, 0, dest_y + 8, DCT_stride); 01834 slice_intra_DCT (decoder, 0, dest_y + DCT_offset, DCT_stride); 01835 slice_intra_DCT (decoder, 0, dest_y + DCT_offset + 8, DCT_stride); 01836 if (likely (decoder->chroma_format == 0)) { 01837 slice_intra_DCT (decoder, 1, decoder->dest[1] + (offset >> 1), 01838 decoder->uv_stride); 01839 slice_intra_DCT (decoder, 2, decoder->dest[2] + (offset >> 1), 01840 decoder->uv_stride); 01841 if (decoder->coding_type == D_TYPE) { 01842 NEEDBITS (bit_buf, bits, bit_ptr); 01843 DUMPBITS (bit_buf, bits, 1); 01844 } 01845 } else if (likely (decoder->chroma_format == 1)) { 01846 uint8_t * dest_u = decoder->dest[1] + (offset >> 1); 01847 uint8_t * dest_v = decoder->dest[2] + (offset >> 1); 01848 DCT_stride >>= 1; 01849 DCT_offset >>= 1; 01850 slice_intra_DCT (decoder, 1, dest_u, DCT_stride); 01851 slice_intra_DCT (decoder, 2, dest_v, DCT_stride); 01852 slice_intra_DCT (decoder, 1, dest_u + DCT_offset, DCT_stride); 01853 slice_intra_DCT (decoder, 2, dest_v + DCT_offset, DCT_stride); 01854 } else { 01855 uint8_t * dest_u = decoder->dest[1] + offset; 01856 uint8_t * dest_v = decoder->dest[2] + offset; 01857 slice_intra_DCT (decoder, 1, dest_u, DCT_stride); 01858 slice_intra_DCT (decoder, 2, dest_v, DCT_stride); 01859 slice_intra_DCT (decoder, 1, dest_u + DCT_offset, DCT_stride); 01860 slice_intra_DCT (decoder, 2, dest_v + DCT_offset, DCT_stride); 01861 slice_intra_DCT (decoder, 1, dest_u + 8, DCT_stride); 01862 slice_intra_DCT (decoder, 2, dest_v + 8, DCT_stride); 01863 slice_intra_DCT (decoder, 1, dest_u + DCT_offset + 8, 01864 DCT_stride); 01865 slice_intra_DCT (decoder, 2, dest_v + DCT_offset + 8, 01866 DCT_stride); 01867 } 01868 } else { 01869 01870 motion_parser_t * parser; 01871 01872 parser = 01873 decoder->motion_parser[macroblock_modes >> MOTION_TYPE_SHIFT]; 01874 MOTION_CALL (parser, macroblock_modes); 01875 01876 if (macroblock_modes & MACROBLOCK_PATTERN) { 01877 int coded_block_pattern; 01878 int DCT_offset, DCT_stride; 01879 01880 if (macroblock_modes & DCT_TYPE_INTERLACED) { 01881 DCT_offset = decoder->stride; 01882 DCT_stride = decoder->stride * 2; 01883 } else { 01884 DCT_offset = decoder->stride * 8; 01885 DCT_stride = decoder->stride; 01886 } 01887 01888 coded_block_pattern = get_coded_block_pattern (decoder); 01889 01890 if (likely (decoder->chroma_format == 0)) { 01891 int offset = decoder->offset; 01892 uint8_t * dest_y = decoder->dest[0] + offset; 01893 if (coded_block_pattern & 1) 01894 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride); 01895 if (coded_block_pattern & 2) 01896 slice_non_intra_DCT (decoder, 0, dest_y + 8, 01897 DCT_stride); 01898 if (coded_block_pattern & 4) 01899 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset, 01900 DCT_stride); 01901 if (coded_block_pattern & 8) 01902 slice_non_intra_DCT (decoder, 0, 01903 dest_y + DCT_offset + 8, 01904 DCT_stride); 01905 if (coded_block_pattern & 16) 01906 slice_non_intra_DCT (decoder, 1, 01907 decoder->dest[1] + (offset >> 1), 01908 decoder->uv_stride); 01909 if (coded_block_pattern & 32) 01910 slice_non_intra_DCT (decoder, 2, 01911 decoder->dest[2] + (offset >> 1), 01912 decoder->uv_stride); 01913 } else if (likely (decoder->chroma_format == 1)) { 01914 int offset; 01915 uint8_t * dest_y; 01916 01917 coded_block_pattern |= bit_buf & (3 << 30); 01918 DUMPBITS (bit_buf, bits, 2); 01919 01920 offset = decoder->offset; 01921 dest_y = decoder->dest[0] + offset; 01922 if (coded_block_pattern & 1) 01923 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride); 01924 if (coded_block_pattern & 2) 01925 slice_non_intra_DCT (decoder, 0, dest_y + 8, 01926 DCT_stride); 01927 if (coded_block_pattern & 4) 01928 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset, 01929 DCT_stride); 01930 if (coded_block_pattern & 8) 01931 slice_non_intra_DCT (decoder, 0, 01932 dest_y + DCT_offset + 8, 01933 DCT_stride); 01934 01935 DCT_stride >>= 1; 01936 DCT_offset = (DCT_offset + offset) >> 1; 01937 if (coded_block_pattern & 16) 01938 slice_non_intra_DCT (decoder, 1, 01939 decoder->dest[1] + (offset >> 1), 01940 DCT_stride); 01941 if (coded_block_pattern & 32) 01942 slice_non_intra_DCT (decoder, 2, 01943 decoder->dest[2] + (offset >> 1), 01944 DCT_stride); 01945 if (coded_block_pattern & (2 << 30)) 01946 slice_non_intra_DCT (decoder, 1, 01947 decoder->dest[1] + DCT_offset, 01948 DCT_stride); 01949 if (coded_block_pattern & (1 << 30)) 01950 slice_non_intra_DCT (decoder, 2, 01951 decoder->dest[2] + DCT_offset, 01952 DCT_stride); 01953 } else { 01954 int offset; 01955 uint8_t * dest_y, * dest_u, * dest_v; 01956 01957 coded_block_pattern |= bit_buf & (63 << 26); 01958 DUMPBITS (bit_buf, bits, 6); 01959 01960 offset = decoder->offset; 01961 dest_y = decoder->dest[0] + offset; 01962 dest_u = decoder->dest[1] + offset; 01963 dest_v = decoder->dest[2] + offset; 01964 01965 if (coded_block_pattern & 1) 01966 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride); 01967 if (coded_block_pattern & 2) 01968 slice_non_intra_DCT (decoder, 0, dest_y + 8, 01969 DCT_stride); 01970 if (coded_block_pattern & 4) 01971 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset, 01972 DCT_stride); 01973 if (coded_block_pattern & 8) 01974 slice_non_intra_DCT (decoder, 0, 01975 dest_y + DCT_offset + 8, 01976 DCT_stride); 01977 01978 if (coded_block_pattern & 16) 01979 slice_non_intra_DCT (decoder, 1, dest_u, DCT_stride); 01980 if (coded_block_pattern & 32) 01981 slice_non_intra_DCT (decoder, 2, dest_v, DCT_stride); 01982 if (coded_block_pattern & (32 << 26)) 01983 slice_non_intra_DCT (decoder, 1, dest_u + DCT_offset, 01984 DCT_stride); 01985 if (coded_block_pattern & (16 << 26)) 01986 slice_non_intra_DCT (decoder, 2, dest_v + DCT_offset, 01987 DCT_stride); 01988 if (coded_block_pattern & (8 << 26)) 01989 slice_non_intra_DCT (decoder, 1, dest_u + 8, 01990 DCT_stride); 01991 if (coded_block_pattern & (4 << 26)) 01992 slice_non_intra_DCT (decoder, 2, dest_v + 8, 01993 DCT_stride); 01994 if (coded_block_pattern & (2 << 26)) 01995 slice_non_intra_DCT (decoder, 1, 01996 dest_u + DCT_offset + 8, 01997 DCT_stride); 01998 if (coded_block_pattern & (1 << 26)) 01999 slice_non_intra_DCT (decoder, 2, 02000 dest_v + DCT_offset + 8, 02001 DCT_stride); 02002 } 02003 } 02004 02005 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] = 02006 decoder->dc_dct_pred[2] = 16384; 02007 } 02008 02009 NEXT_MACROBLOCK; 02010 02011 NEEDBITS (bit_buf, bits, bit_ptr); 02012 mba_inc = 0; 02013 while (1) { 02014 if (bit_buf >= 0x10000000) { 02015 mba = MBA_5 + (UBITS (bit_buf, 5) - 2); 02016 break; 02017 } else if (bit_buf >= 0x03000000) { 02018 mba = MBA_11 + (UBITS (bit_buf, 11) - 24); 02019 break; 02020 } else switch (UBITS (bit_buf, 11)) { 02021 case 8: /* macroblock_escape */ 02022 mba_inc += 33; 02023 /* pass through */ 02024 case 15: /* macroblock_stuffing (MPEG1 only) */ 02025 DUMPBITS (bit_buf, bits, 11); 02026 NEEDBITS (bit_buf, bits, bit_ptr); 02027 continue; 02028 default: /* end of slice, or error */ 02029 if (mpeg2_cpu_state_restore) 02030 mpeg2_cpu_state_restore (&cpu_state); 02031 return; 02032 } 02033 } 02034 DUMPBITS (bit_buf, bits, mba->len); 02035 mba_inc += mba->mba; 02036 02037 if (mba_inc) { 02038 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] = 02039 decoder->dc_dct_pred[2] = 16384; 02040 02041 if (decoder->coding_type == P_TYPE) { 02042 do { 02043 MOTION_CALL (decoder->motion_parser[0], 02044 MACROBLOCK_MOTION_FORWARD); 02045 NEXT_MACROBLOCK; 02046 } while (--mba_inc); 02047 } else { 02048 do { 02049 MOTION_CALL (decoder->motion_parser[4], macroblock_modes); 02050 NEXT_MACROBLOCK; 02051 } while (--mba_inc); 02052 } 02053 } 02054 } 02055 #undef bit_buf 02056 #undef bits 02057 #undef bit_ptr 02058 }
1.7.6.1