00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #define UNCHECKED_BITSTREAM_READER 1
00029
00030
00031 #include "internal.h"
00032 #include "avcodec.h"
00033 #include "dsputil.h"
00034 #include "mpegvideo.h"
00035 #include "libavutil/avassert.h"
00036 #include "libavutil/timecode.h"
00037
00038 #include "mpeg12.h"
00039 #include "mpeg12data.h"
00040 #include "mpeg12decdata.h"
00041 #include "bytestream.h"
00042 #include "vdpau_internal.h"
00043 #include "xvmc_internal.h"
00044 #include "thread.h"
00045
00046
00047
00048
00049
00050 #define MV_VLC_BITS 9
00051 #define MBINCR_VLC_BITS 9
00052 #define MB_PAT_VLC_BITS 9
00053 #define MB_PTYPE_VLC_BITS 6
00054 #define MB_BTYPE_VLC_BITS 6
00055
00056 static VLC mv_vlc;
00057
00058
00059 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
00060 {
00061 int code, sign, val, shift;
00062
00063 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
00064 if (code == 0) {
00065 return pred;
00066 }
00067 if (code < 0) {
00068 return 0xffff;
00069 }
00070
00071 sign = get_bits1(&s->gb);
00072 shift = fcode - 1;
00073 val = code;
00074 if (shift) {
00075 val = (val - 1) << shift;
00076 val |= get_bits(&s->gb, shift);
00077 val++;
00078 }
00079 if (sign)
00080 val = -val;
00081 val += pred;
00082
00083
00084 return sign_extend(val, 5 + shift);
00085 }
00086
00087 static inline int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
00088 {
00089 int level, dc, diff, i, j, run;
00090 int component;
00091 RLTable *rl = &ff_rl_mpeg1;
00092 uint8_t * const scantable = s->intra_scantable.permutated;
00093 const uint16_t *quant_matrix = s->intra_matrix;
00094 const int qscale = s->qscale;
00095
00096
00097 component = (n <= 3 ? 0 : n - 4 + 1);
00098 diff = decode_dc(&s->gb, component);
00099 if (diff >= 0xffff)
00100 return -1;
00101 dc = s->last_dc[component];
00102 dc += diff;
00103 s->last_dc[component] = dc;
00104 block[0] = dc * quant_matrix[0];
00105 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
00106 i = 0;
00107 {
00108 OPEN_READER(re, &s->gb);
00109
00110 for (;;) {
00111 UPDATE_CACHE(re, &s->gb);
00112 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00113
00114 if (level == 127) {
00115 break;
00116 } else if (level != 0) {
00117 i += run;
00118 j = scantable[i];
00119 level = (level * qscale * quant_matrix[j]) >> 4;
00120 level = (level - 1) | 1;
00121 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00122 LAST_SKIP_BITS(re, &s->gb, 1);
00123 } else {
00124
00125 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00126 UPDATE_CACHE(re, &s->gb);
00127 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00128 if (level == -128) {
00129 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
00130 } else if (level == 0) {
00131 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
00132 }
00133 i += run;
00134 j = scantable[i];
00135 if (level < 0) {
00136 level = -level;
00137 level = (level * qscale * quant_matrix[j]) >> 4;
00138 level = (level - 1) | 1;
00139 level = -level;
00140 } else {
00141 level = (level * qscale * quant_matrix[j]) >> 4;
00142 level = (level - 1) | 1;
00143 }
00144 }
00145 if (i > 63) {
00146 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00147 return -1;
00148 }
00149
00150 block[j] = level;
00151 }
00152 CLOSE_READER(re, &s->gb);
00153 }
00154 s->block_last_index[n] = i;
00155 return 0;
00156 }
00157
00158 int ff_mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
00159 {
00160 return mpeg1_decode_block_intra(s, block, n);
00161 }
00162
00163 static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
00164 {
00165 int level, i, j, run;
00166 RLTable *rl = &ff_rl_mpeg1;
00167 uint8_t * const scantable = s->intra_scantable.permutated;
00168 const uint16_t *quant_matrix = s->inter_matrix;
00169 const int qscale = s->qscale;
00170
00171 {
00172 OPEN_READER(re, &s->gb);
00173 i = -1;
00174
00175 UPDATE_CACHE(re, &s->gb);
00176 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00177 level = (3 * qscale * quant_matrix[0]) >> 5;
00178 level = (level - 1) | 1;
00179 if (GET_CACHE(re, &s->gb) & 0x40000000)
00180 level = -level;
00181 block[0] = level;
00182 i++;
00183 SKIP_BITS(re, &s->gb, 2);
00184 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00185 goto end;
00186 }
00187
00188 for (;;) {
00189 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00190
00191 if (level != 0) {
00192 i += run;
00193 j = scantable[i];
00194 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00195 level = (level - 1) | 1;
00196 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00197 SKIP_BITS(re, &s->gb, 1);
00198 } else {
00199
00200 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00201 UPDATE_CACHE(re, &s->gb);
00202 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00203 if (level == -128) {
00204 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
00205 } else if (level == 0) {
00206 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
00207 }
00208 i += run;
00209 j = scantable[i];
00210 if (level < 0) {
00211 level = -level;
00212 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00213 level = (level - 1) | 1;
00214 level = -level;
00215 } else {
00216 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00217 level = (level - 1) | 1;
00218 }
00219 }
00220 if (i > 63) {
00221 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00222 return -1;
00223 }
00224
00225 block[j] = level;
00226 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00227 break;
00228 UPDATE_CACHE(re, &s->gb);
00229 }
00230 end:
00231 LAST_SKIP_BITS(re, &s->gb, 2);
00232 CLOSE_READER(re, &s->gb);
00233 }
00234 s->block_last_index[n] = i;
00235 return 0;
00236 }
00237
00238 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
00239 {
00240 int level, i, j, run;
00241 RLTable *rl = &ff_rl_mpeg1;
00242 uint8_t * const scantable = s->intra_scantable.permutated;
00243 const int qscale = s->qscale;
00244
00245 {
00246 OPEN_READER(re, &s->gb);
00247 i = -1;
00248
00249 UPDATE_CACHE(re, &s->gb);
00250 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00251 level = (3 * qscale) >> 1;
00252 level = (level - 1) | 1;
00253 if (GET_CACHE(re, &s->gb) & 0x40000000)
00254 level = -level;
00255 block[0] = level;
00256 i++;
00257 SKIP_BITS(re, &s->gb, 2);
00258 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00259 goto end;
00260 }
00261
00262
00263 for (;;) {
00264 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00265
00266 if (level != 0) {
00267 i += run;
00268 j = scantable[i];
00269 level = ((level * 2 + 1) * qscale) >> 1;
00270 level = (level - 1) | 1;
00271 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00272 SKIP_BITS(re, &s->gb, 1);
00273 } else {
00274
00275 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00276 UPDATE_CACHE(re, &s->gb);
00277 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00278 if (level == -128) {
00279 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
00280 } else if (level == 0) {
00281 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
00282 }
00283 i += run;
00284 j = scantable[i];
00285 if (level < 0) {
00286 level = -level;
00287 level = ((level * 2 + 1) * qscale) >> 1;
00288 level = (level - 1) | 1;
00289 level = -level;
00290 } else {
00291 level = ((level * 2 + 1) * qscale) >> 1;
00292 level = (level - 1) | 1;
00293 }
00294 }
00295
00296 block[j] = level;
00297 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00298 break;
00299 UPDATE_CACHE(re, &s->gb);
00300 }
00301 end:
00302 LAST_SKIP_BITS(re, &s->gb, 2);
00303 CLOSE_READER(re, &s->gb);
00304 }
00305 s->block_last_index[n] = i;
00306 return 0;
00307 }
00308
00309
00310 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n)
00311 {
00312 int level, i, j, run;
00313 RLTable *rl = &ff_rl_mpeg1;
00314 uint8_t * const scantable = s->intra_scantable.permutated;
00315 const uint16_t *quant_matrix;
00316 const int qscale = s->qscale;
00317 int mismatch;
00318
00319 mismatch = 1;
00320
00321 {
00322 OPEN_READER(re, &s->gb);
00323 i = -1;
00324 if (n < 4)
00325 quant_matrix = s->inter_matrix;
00326 else
00327 quant_matrix = s->chroma_inter_matrix;
00328
00329
00330 UPDATE_CACHE(re, &s->gb);
00331 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00332 level= (3 * qscale * quant_matrix[0]) >> 5;
00333 if (GET_CACHE(re, &s->gb) & 0x40000000)
00334 level = -level;
00335 block[0] = level;
00336 mismatch ^= level;
00337 i++;
00338 SKIP_BITS(re, &s->gb, 2);
00339 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00340 goto end;
00341 }
00342
00343
00344 for (;;) {
00345 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00346
00347 if (level != 0) {
00348 i += run;
00349 j = scantable[i];
00350 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00351 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00352 SKIP_BITS(re, &s->gb, 1);
00353 } else {
00354
00355 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00356 UPDATE_CACHE(re, &s->gb);
00357 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00358
00359 i += run;
00360 j = scantable[i];
00361 if (level < 0) {
00362 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00363 level = -level;
00364 } else {
00365 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
00366 }
00367 }
00368 if (i > 63) {
00369 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00370 return -1;
00371 }
00372
00373 mismatch ^= level;
00374 block[j] = level;
00375 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00376 break;
00377 UPDATE_CACHE(re, &s->gb);
00378 }
00379 end:
00380 LAST_SKIP_BITS(re, &s->gb, 2);
00381 CLOSE_READER(re, &s->gb);
00382 }
00383 block[63] ^= (mismatch & 1);
00384
00385 s->block_last_index[n] = i;
00386 return 0;
00387 }
00388
00389 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
00390 DCTELEM *block, int n)
00391 {
00392 int level, i, j, run;
00393 RLTable *rl = &ff_rl_mpeg1;
00394 uint8_t * const scantable = s->intra_scantable.permutated;
00395 const int qscale = s->qscale;
00396 OPEN_READER(re, &s->gb);
00397 i = -1;
00398
00399
00400 UPDATE_CACHE(re, &s->gb);
00401 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00402 level = (3 * qscale) >> 1;
00403 if (GET_CACHE(re, &s->gb) & 0x40000000)
00404 level = -level;
00405 block[0] = level;
00406 i++;
00407 SKIP_BITS(re, &s->gb, 2);
00408 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00409 goto end;
00410 }
00411
00412
00413 for (;;) {
00414 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00415
00416 if (level != 0) {
00417 i += run;
00418 j = scantable[i];
00419 level = ((level * 2 + 1) * qscale) >> 1;
00420 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00421 SKIP_BITS(re, &s->gb, 1);
00422 } else {
00423
00424 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00425 UPDATE_CACHE(re, &s->gb);
00426 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00427
00428 i += run;
00429 j = scantable[i];
00430 if (level < 0) {
00431 level = ((-level * 2 + 1) * qscale) >> 1;
00432 level = -level;
00433 } else {
00434 level = ((level * 2 + 1) * qscale) >> 1;
00435 }
00436 }
00437
00438 block[j] = level;
00439 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00440 break;
00441 UPDATE_CACHE(re, &s->gb);
00442 }
00443 end:
00444 LAST_SKIP_BITS(re, &s->gb, 2);
00445 CLOSE_READER(re, &s->gb);
00446 s->block_last_index[n] = i;
00447 return 0;
00448 }
00449
00450
00451 static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
00452 {
00453 int level, dc, diff, i, j, run;
00454 int component;
00455 RLTable *rl;
00456 uint8_t * const scantable = s->intra_scantable.permutated;
00457 const uint16_t *quant_matrix;
00458 const int qscale = s->qscale;
00459 int mismatch;
00460
00461
00462 if (n < 4) {
00463 quant_matrix = s->intra_matrix;
00464 component = 0;
00465 } else {
00466 quant_matrix = s->chroma_intra_matrix;
00467 component = (n & 1) + 1;
00468 }
00469 diff = decode_dc(&s->gb, component);
00470 if (diff >= 0xffff)
00471 return -1;
00472 dc = s->last_dc[component];
00473 dc += diff;
00474 s->last_dc[component] = dc;
00475 block[0] = dc << (3 - s->intra_dc_precision);
00476 av_dlog(s->avctx, "dc=%d\n", block[0]);
00477 mismatch = block[0] ^ 1;
00478 i = 0;
00479 if (s->intra_vlc_format)
00480 rl = &ff_rl_mpeg2;
00481 else
00482 rl = &ff_rl_mpeg1;
00483
00484 {
00485 OPEN_READER(re, &s->gb);
00486
00487 for (;;) {
00488 UPDATE_CACHE(re, &s->gb);
00489 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00490
00491 if (level == 127) {
00492 break;
00493 } else if (level != 0) {
00494 i += run;
00495 j = scantable[i];
00496 level = (level * qscale * quant_matrix[j]) >> 4;
00497 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00498 LAST_SKIP_BITS(re, &s->gb, 1);
00499 } else {
00500
00501 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00502 UPDATE_CACHE(re, &s->gb);
00503 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00504 i += run;
00505 j = scantable[i];
00506 if (level < 0) {
00507 level = (-level * qscale * quant_matrix[j]) >> 4;
00508 level = -level;
00509 } else {
00510 level = (level * qscale * quant_matrix[j]) >> 4;
00511 }
00512 }
00513 if (i > 63) {
00514 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00515 return -1;
00516 }
00517
00518 mismatch ^= level;
00519 block[j] = level;
00520 }
00521 CLOSE_READER(re, &s->gb);
00522 }
00523 block[63] ^= mismatch & 1;
00524
00525 s->block_last_index[n] = i;
00526 return 0;
00527 }
00528
00529 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
00530 {
00531 int level, dc, diff, j, run;
00532 int component;
00533 RLTable *rl;
00534 uint8_t * scantable = s->intra_scantable.permutated;
00535 const uint16_t *quant_matrix;
00536 const int qscale = s->qscale;
00537
00538
00539 if (n < 4) {
00540 quant_matrix = s->intra_matrix;
00541 component = 0;
00542 } else {
00543 quant_matrix = s->chroma_intra_matrix;
00544 component = (n & 1) + 1;
00545 }
00546 diff = decode_dc(&s->gb, component);
00547 if (diff >= 0xffff)
00548 return -1;
00549 dc = s->last_dc[component];
00550 dc += diff;
00551 s->last_dc[component] = dc;
00552 block[0] = dc << (3 - s->intra_dc_precision);
00553 if (s->intra_vlc_format)
00554 rl = &ff_rl_mpeg2;
00555 else
00556 rl = &ff_rl_mpeg1;
00557
00558 {
00559 OPEN_READER(re, &s->gb);
00560
00561 for (;;) {
00562 UPDATE_CACHE(re, &s->gb);
00563 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00564
00565 if (level == 127) {
00566 break;
00567 } else if (level != 0) {
00568 scantable += run;
00569 j = *scantable;
00570 level = (level * qscale * quant_matrix[j]) >> 4;
00571 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00572 LAST_SKIP_BITS(re, &s->gb, 1);
00573 } else {
00574
00575 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
00576 UPDATE_CACHE(re, &s->gb);
00577 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00578 scantable += run;
00579 j = *scantable;
00580 if (level < 0) {
00581 level = (-level * qscale * quant_matrix[j]) >> 4;
00582 level = -level;
00583 } else {
00584 level = (level * qscale * quant_matrix[j]) >> 4;
00585 }
00586 }
00587
00588 block[j] = level;
00589 }
00590 CLOSE_READER(re, &s->gb);
00591 }
00592
00593 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
00594 return 0;
00595 }
00596
00597 uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
00598
00599 #define INIT_2D_VLC_RL(rl, static_size)\
00600 {\
00601 static RL_VLC_ELEM rl_vlc_table[static_size];\
00602 INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
00603 &rl.table_vlc[0][1], 4, 2,\
00604 &rl.table_vlc[0][0], 4, 2, static_size);\
00605 \
00606 rl.rl_vlc[0] = rl_vlc_table;\
00607 init_2d_vlc_rl(&rl);\
00608 }
00609
00610 static void init_2d_vlc_rl(RLTable *rl)
00611 {
00612 int i;
00613
00614 for (i = 0; i < rl->vlc.table_size; i++) {
00615 int code = rl->vlc.table[i][0];
00616 int len = rl->vlc.table[i][1];
00617 int level, run;
00618
00619 if (len == 0) {
00620 run = 65;
00621 level = MAX_LEVEL;
00622 } else if (len<0) {
00623 run = 0;
00624 level = code;
00625 } else {
00626 if (code == rl->n) {
00627 run = 65;
00628 level = 0;
00629 } else if (code == rl->n+1) {
00630 run = 0;
00631 level = 127;
00632 } else {
00633 run = rl->table_run [code] + 1;
00634 level = rl->table_level[code];
00635 }
00636 }
00637 rl->rl_vlc[0][i].len = len;
00638 rl->rl_vlc[0][i].level = level;
00639 rl->rl_vlc[0][i].run = run;
00640 }
00641 }
00642
00643 void ff_mpeg12_common_init(MpegEncContext *s)
00644 {
00645
00646 s->y_dc_scale_table =
00647 s->c_dc_scale_table = ff_mpeg2_dc_scale_table[s->intra_dc_precision];
00648
00649 }
00650
00651 void ff_mpeg1_clean_buffers(MpegEncContext *s)
00652 {
00653 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
00654 s->last_dc[1] = s->last_dc[0];
00655 s->last_dc[2] = s->last_dc[0];
00656 memset(s->last_mv, 0, sizeof(s->last_mv));
00657 }
00658
00659
00660
00661
00662
00663 VLC ff_dc_lum_vlc;
00664 VLC ff_dc_chroma_vlc;
00665
00666 static VLC mbincr_vlc;
00667 static VLC mb_ptype_vlc;
00668 static VLC mb_btype_vlc;
00669 static VLC mb_pat_vlc;
00670
00671 av_cold void ff_mpeg12_init_vlcs(void)
00672 {
00673 static int done = 0;
00674
00675 if (!done) {
00676 done = 1;
00677
00678 INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
00679 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
00680 ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
00681 INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
00682 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
00683 ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
00684 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
00685 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
00686 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
00687 INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
00688 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
00689 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
00690 INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
00691 &ff_mpeg12_mbPatTable[0][1], 2, 1,
00692 &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
00693
00694 INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
00695 &table_mb_ptype[0][1], 2, 1,
00696 &table_mb_ptype[0][0], 2, 1, 64);
00697 INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
00698 &table_mb_btype[0][1], 2, 1,
00699 &table_mb_btype[0][0], 2, 1, 64);
00700 ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
00701 ff_init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
00702
00703 INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
00704 INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
00705 }
00706 }
00707
00708 static inline int get_dmv(MpegEncContext *s)
00709 {
00710 if (get_bits1(&s->gb))
00711 return 1 - (get_bits1(&s->gb) << 1);
00712 else
00713 return 0;
00714 }
00715
00716 static inline int get_qscale(MpegEncContext *s)
00717 {
00718 int qscale = get_bits(&s->gb, 5);
00719 if (s->q_scale_type) {
00720 return non_linear_qscale[qscale];
00721 } else {
00722 return qscale << 1;
00723 }
00724 }
00725
00726 static void exchange_uv(MpegEncContext *s)
00727 {
00728 DCTELEM (*tmp)[64];
00729
00730 tmp = s->pblocks[4];
00731 s->pblocks[4] = s->pblocks[5];
00732 s->pblocks[5] = tmp;
00733 }
00734
00735
00736 #define MT_FIELD 1
00737 #define MT_FRAME 2
00738 #define MT_16X8 2
00739 #define MT_DMV 3
00740
00741 static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
00742 {
00743 int i, j, k, cbp, val, mb_type, motion_type;
00744 const int mb_block_count = 4 + (1 << s->chroma_format);
00745
00746 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
00747
00748 assert(s->mb_skipped == 0);
00749
00750 if (s->mb_skip_run-- != 0) {
00751 if (s->pict_type == AV_PICTURE_TYPE_P) {
00752 s->mb_skipped = 1;
00753 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
00754 } else {
00755 int mb_type;
00756
00757 if (s->mb_x)
00758 mb_type = s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
00759 else
00760 mb_type = s->current_picture.f.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
00761 if (IS_INTRA(mb_type))
00762 return -1;
00763 s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
00764 mb_type | MB_TYPE_SKIP;
00765
00766
00767 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
00768 s->mb_skipped = 1;
00769 }
00770
00771 return 0;
00772 }
00773
00774 switch (s->pict_type) {
00775 default:
00776 case AV_PICTURE_TYPE_I:
00777 if (get_bits1(&s->gb) == 0) {
00778 if (get_bits1(&s->gb) == 0) {
00779 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
00780 return -1;
00781 }
00782 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
00783 } else {
00784 mb_type = MB_TYPE_INTRA;
00785 }
00786 break;
00787 case AV_PICTURE_TYPE_P:
00788 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
00789 if (mb_type < 0) {
00790 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
00791 return -1;
00792 }
00793 mb_type = ptype2mb_type[mb_type];
00794 break;
00795 case AV_PICTURE_TYPE_B:
00796 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
00797 if (mb_type < 0) {
00798 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
00799 return -1;
00800 }
00801 mb_type = btype2mb_type[mb_type];
00802 break;
00803 }
00804 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
00805
00806 if (IS_INTRA(mb_type)) {
00807 s->dsp.clear_blocks(s->block[0]);
00808
00809 if (!s->chroma_y_shift) {
00810 s->dsp.clear_blocks(s->block[6]);
00811 }
00812
00813
00814 if (s->picture_structure == PICT_FRAME &&
00815 !s->frame_pred_frame_dct) {
00816 s->interlaced_dct = get_bits1(&s->gb);
00817 }
00818
00819 if (IS_QUANT(mb_type))
00820 s->qscale = get_qscale(s);
00821
00822 if (s->concealment_motion_vectors) {
00823
00824 if (s->picture_structure != PICT_FRAME)
00825 skip_bits1(&s->gb);
00826
00827 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
00828 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
00829 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
00830 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
00831
00832 skip_bits1(&s->gb);
00833 } else
00834 memset(s->last_mv, 0, sizeof(s->last_mv));
00835 s->mb_intra = 1;
00836
00837 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
00838 ff_xvmc_pack_pblocks(s, -1);
00839 if (s->swap_uv) {
00840 exchange_uv(s);
00841 }
00842 }
00843
00844 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
00845 if (s->flags2 & CODEC_FLAG2_FAST) {
00846 for (i = 0; i < 6; i++) {
00847 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
00848 }
00849 } else {
00850 for (i = 0; i < mb_block_count; i++) {
00851 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
00852 return -1;
00853 }
00854 }
00855 } else {
00856 for (i = 0; i < 6; i++) {
00857 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
00858 return -1;
00859 }
00860 }
00861 } else {
00862 if (mb_type & MB_TYPE_ZERO_MV) {
00863 assert(mb_type & MB_TYPE_CBP);
00864
00865 s->mv_dir = MV_DIR_FORWARD;
00866 if (s->picture_structure == PICT_FRAME) {
00867 if (!s->frame_pred_frame_dct)
00868 s->interlaced_dct = get_bits1(&s->gb);
00869 s->mv_type = MV_TYPE_16X16;
00870 } else {
00871 s->mv_type = MV_TYPE_FIELD;
00872 mb_type |= MB_TYPE_INTERLACED;
00873 s->field_select[0][0] = s->picture_structure - 1;
00874 }
00875
00876 if (IS_QUANT(mb_type))
00877 s->qscale = get_qscale(s);
00878
00879 s->last_mv[0][0][0] = 0;
00880 s->last_mv[0][0][1] = 0;
00881 s->last_mv[0][1][0] = 0;
00882 s->last_mv[0][1][1] = 0;
00883 s->mv[0][0][0] = 0;
00884 s->mv[0][0][1] = 0;
00885 } else {
00886 assert(mb_type & MB_TYPE_L0L1);
00887
00888
00889 if (s->frame_pred_frame_dct)
00890 motion_type = MT_FRAME;
00891 else {
00892 motion_type = get_bits(&s->gb, 2);
00893 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
00894 s->interlaced_dct = get_bits1(&s->gb);
00895 }
00896
00897 if (IS_QUANT(mb_type))
00898 s->qscale = get_qscale(s);
00899
00900
00901 s->mv_dir = (mb_type >> 13) & 3;
00902 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
00903 switch (motion_type) {
00904 case MT_FRAME:
00905 if (s->picture_structure == PICT_FRAME) {
00906 mb_type |= MB_TYPE_16x16;
00907 s->mv_type = MV_TYPE_16X16;
00908 for (i = 0; i < 2; i++) {
00909 if (USES_LIST(mb_type, i)) {
00910
00911 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
00912 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
00913 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
00914 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
00915
00916 if (s->full_pel[i]) {
00917 s->mv[i][0][0] <<= 1;
00918 s->mv[i][0][1] <<= 1;
00919 }
00920 }
00921 }
00922 } else {
00923 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
00924 s->mv_type = MV_TYPE_16X8;
00925 for (i = 0; i < 2; i++) {
00926 if (USES_LIST(mb_type, i)) {
00927
00928 for (j = 0; j < 2; j++) {
00929 s->field_select[i][j] = get_bits1(&s->gb);
00930 for (k = 0; k < 2; k++) {
00931 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
00932 s->last_mv[i][j][k]);
00933 s->last_mv[i][j][k] = val;
00934 s->mv[i][j][k] = val;
00935 }
00936 }
00937 }
00938 }
00939 }
00940 break;
00941 case MT_FIELD:
00942 s->mv_type = MV_TYPE_FIELD;
00943 if (s->picture_structure == PICT_FRAME) {
00944 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
00945 for (i = 0; i < 2; i++) {
00946 if (USES_LIST(mb_type, i)) {
00947 for (j = 0; j < 2; j++) {
00948 s->field_select[i][j] = get_bits1(&s->gb);
00949 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
00950 s->last_mv[i][j][0]);
00951 s->last_mv[i][j][0] = val;
00952 s->mv[i][j][0] = val;
00953 av_dlog(s->avctx, "fmx=%d\n", val);
00954 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
00955 s->last_mv[i][j][1] >> 1);
00956 s->last_mv[i][j][1] = val << 1;
00957 s->mv[i][j][1] = val;
00958 av_dlog(s->avctx, "fmy=%d\n", val);
00959 }
00960 }
00961 }
00962 } else {
00963 av_assert0(!s->progressive_sequence);
00964 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
00965 for (i = 0; i < 2; i++) {
00966 if (USES_LIST(mb_type, i)) {
00967 s->field_select[i][0] = get_bits1(&s->gb);
00968 for (k = 0; k < 2; k++) {
00969 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
00970 s->last_mv[i][0][k]);
00971 s->last_mv[i][0][k] = val;
00972 s->last_mv[i][1][k] = val;
00973 s->mv[i][0][k] = val;
00974 }
00975 }
00976 }
00977 }
00978 break;
00979 case MT_DMV:
00980 if(s->progressive_sequence){
00981 av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
00982 return -1;
00983 }
00984 s->mv_type = MV_TYPE_DMV;
00985 for (i = 0; i < 2; i++) {
00986 if (USES_LIST(mb_type, i)) {
00987 int dmx, dmy, mx, my, m;
00988 const int my_shift = s->picture_structure == PICT_FRAME;
00989
00990 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
00991 s->last_mv[i][0][0]);
00992 s->last_mv[i][0][0] = mx;
00993 s->last_mv[i][1][0] = mx;
00994 dmx = get_dmv(s);
00995 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
00996 s->last_mv[i][0][1] >> my_shift);
00997 dmy = get_dmv(s);
00998
00999
01000 s->last_mv[i][0][1] = my << my_shift;
01001 s->last_mv[i][1][1] = my << my_shift;
01002
01003 s->mv[i][0][0] = mx;
01004 s->mv[i][0][1] = my;
01005 s->mv[i][1][0] = mx;
01006 s->mv[i][1][1] = my;
01007
01008 if (s->picture_structure == PICT_FRAME) {
01009 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
01010
01011
01012 m = s->top_field_first ? 1 : 3;
01013
01014
01015 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
01016 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
01017 m = 4 - m;
01018 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
01019 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
01020 } else {
01021 mb_type |= MB_TYPE_16x16;
01022
01023 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
01024 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
01025 if (s->picture_structure == PICT_TOP_FIELD)
01026 s->mv[i][2][1]--;
01027 else
01028 s->mv[i][2][1]++;
01029 }
01030 }
01031 }
01032 break;
01033 default:
01034 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
01035 return -1;
01036 }
01037 }
01038
01039 s->mb_intra = 0;
01040 if (HAS_CBP(mb_type)) {
01041 s->dsp.clear_blocks(s->block[0]);
01042
01043 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
01044 if (mb_block_count > 6) {
01045 cbp <<= mb_block_count - 6;
01046 cbp |= get_bits(&s->gb, mb_block_count - 6);
01047 s->dsp.clear_blocks(s->block[6]);
01048 }
01049 if (cbp <= 0) {
01050 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
01051 return -1;
01052 }
01053
01054
01055 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
01056 ff_xvmc_pack_pblocks(s, cbp);
01057 if (s->swap_uv) {
01058 exchange_uv(s);
01059 }
01060 }
01061
01062 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
01063 if (s->flags2 & CODEC_FLAG2_FAST) {
01064 for (i = 0; i < 6; i++) {
01065 if (cbp & 32) {
01066 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
01067 } else {
01068 s->block_last_index[i] = -1;
01069 }
01070 cbp += cbp;
01071 }
01072 } else {
01073 cbp <<= 12-mb_block_count;
01074
01075 for (i = 0; i < mb_block_count; i++) {
01076 if (cbp & (1 << 11)) {
01077 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
01078 return -1;
01079 } else {
01080 s->block_last_index[i] = -1;
01081 }
01082 cbp += cbp;
01083 }
01084 }
01085 } else {
01086 if (s->flags2 & CODEC_FLAG2_FAST) {
01087 for (i = 0; i < 6; i++) {
01088 if (cbp & 32) {
01089 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
01090 } else {
01091 s->block_last_index[i] = -1;
01092 }
01093 cbp += cbp;
01094 }
01095 } else {
01096 for (i = 0; i < 6; i++) {
01097 if (cbp & 32) {
01098 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
01099 return -1;
01100 } else {
01101 s->block_last_index[i] = -1;
01102 }
01103 cbp += cbp;
01104 }
01105 }
01106 }
01107 } else {
01108 for (i = 0; i < 12; i++)
01109 s->block_last_index[i] = -1;
01110 }
01111 }
01112
01113 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
01114
01115 return 0;
01116 }
01117
01118 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
01119 {
01120 Mpeg1Context *s = avctx->priv_data;
01121 MpegEncContext *s2 = &s->mpeg_enc_ctx;
01122 int i;
01123
01124
01125
01126 for (i = 0; i < 64; i++)
01127 s2->dsp.idct_permutation[i]=i;
01128
01129 ff_MPV_decode_defaults(s2);
01130
01131 s->mpeg_enc_ctx.avctx = avctx;
01132 s->mpeg_enc_ctx.flags = avctx->flags;
01133 s->mpeg_enc_ctx.flags2 = avctx->flags2;
01134 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
01135 ff_mpeg12_init_vlcs();
01136
01137 s->mpeg_enc_ctx_allocated = 0;
01138 s->mpeg_enc_ctx.picture_number = 0;
01139 s->repeat_field = 0;
01140 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
01141 avctx->color_range = AVCOL_RANGE_MPEG;
01142 if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
01143 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
01144 else
01145 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
01146 return 0;
01147 }
01148
01149 static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
01150 {
01151 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
01152 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
01153 int err;
01154
01155 if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
01156 return 0;
01157
01158 err = ff_mpeg_update_thread_context(avctx, avctx_from);
01159 if (err) return err;
01160
01161 if (!ctx->mpeg_enc_ctx_allocated)
01162 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
01163
01164 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
01165 s->picture_number++;
01166
01167 return 0;
01168 }
01169
01170 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
01171 const uint8_t *new_perm)
01172 {
01173 uint16_t temp_matrix[64];
01174 int i;
01175
01176 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
01177
01178 for (i = 0; i < 64; i++) {
01179 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
01180 }
01181 }
01182
01183 static const enum PixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
01184 #if CONFIG_MPEG_XVMC_DECODER
01185 PIX_FMT_XVMC_MPEG2_IDCT,
01186 PIX_FMT_XVMC_MPEG2_MC,
01187 #endif
01188 #if CONFIG_MPEG1_VDPAU_HWACCEL
01189 PIX_FMT_VDPAU_MPEG1,
01190 #endif
01191 PIX_FMT_YUV420P,
01192 PIX_FMT_NONE
01193 };
01194
01195 static const enum PixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
01196 #if CONFIG_MPEG_XVMC_DECODER
01197 PIX_FMT_XVMC_MPEG2_IDCT,
01198 PIX_FMT_XVMC_MPEG2_MC,
01199 #endif
01200 #if CONFIG_MPEG2_VDPAU_HWACCEL
01201 PIX_FMT_VDPAU_MPEG2,
01202 #endif
01203 #if CONFIG_MPEG2_DXVA2_HWACCEL
01204 PIX_FMT_DXVA2_VLD,
01205 #endif
01206 #if CONFIG_MPEG2_VAAPI_HWACCEL
01207 PIX_FMT_VAAPI_VLD,
01208 #endif
01209 PIX_FMT_YUV420P,
01210 PIX_FMT_NONE
01211 };
01212
01213 static inline int uses_vdpau(AVCodecContext *avctx) {
01214 return avctx->pix_fmt == PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == PIX_FMT_VDPAU_MPEG2;
01215 }
01216
01217 static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
01218 {
01219 Mpeg1Context *s1 = avctx->priv_data;
01220 MpegEncContext *s = &s1->mpeg_enc_ctx;
01221
01222 if(s->chroma_format < 2) {
01223 enum PixelFormat res;
01224 res = avctx->get_format(avctx,
01225 avctx->codec_id == CODEC_ID_MPEG1VIDEO ?
01226 mpeg1_hwaccel_pixfmt_list_420 :
01227 mpeg2_hwaccel_pixfmt_list_420);
01228 if (res != PIX_FMT_XVMC_MPEG2_IDCT && res != PIX_FMT_XVMC_MPEG2_MC) {
01229 avctx->xvmc_acceleration = 0;
01230 } else if (!avctx->xvmc_acceleration) {
01231 avctx->xvmc_acceleration = 2;
01232 }
01233 return res;
01234 } else if(s->chroma_format == 2)
01235 return PIX_FMT_YUV422P;
01236 else
01237 return PIX_FMT_YUV444P;
01238 }
01239
01240
01241
01242 static int mpeg_decode_postinit(AVCodecContext *avctx)
01243 {
01244 Mpeg1Context *s1 = avctx->priv_data;
01245 MpegEncContext *s = &s1->mpeg_enc_ctx;
01246 uint8_t old_permutation[64];
01247
01248 if ((s1->mpeg_enc_ctx_allocated == 0) ||
01249 avctx->coded_width != s->width ||
01250 avctx->coded_height != s->height ||
01251 s1->save_width != s->width ||
01252 s1->save_height != s->height ||
01253 s1->save_aspect_info != s->aspect_ratio_info ||
01254 s1->save_progressive_seq != s->progressive_sequence ||
01255 0)
01256 {
01257
01258 if (s1->mpeg_enc_ctx_allocated) {
01259 ParseContext pc = s->parse_context;
01260 s->parse_context.buffer = 0;
01261 ff_MPV_common_end(s);
01262 s->parse_context = pc;
01263 }
01264
01265 if ((s->width == 0) || (s->height == 0))
01266 return -2;
01267
01268 avcodec_set_dimensions(avctx, s->width, s->height);
01269 avctx->bit_rate = s->bit_rate;
01270 s1->save_aspect_info = s->aspect_ratio_info;
01271 s1->save_width = s->width;
01272 s1->save_height = s->height;
01273 s1->save_progressive_seq = s->progressive_sequence;
01274
01275
01276
01277 avctx->has_b_frames = !s->low_delay;
01278
01279 if (avctx->codec_id == CODEC_ID_MPEG1VIDEO) {
01280
01281 avctx->time_base.den = avpriv_frame_rate_tab[s->frame_rate_index].num;
01282 avctx->time_base.num = avpriv_frame_rate_tab[s->frame_rate_index].den;
01283
01284 avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
01285 avctx->ticks_per_frame=1;
01286 } else {
01287
01288 av_reduce(&s->avctx->time_base.den,
01289 &s->avctx->time_base.num,
01290 avpriv_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
01291 avpriv_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
01292 1 << 30);
01293 avctx->ticks_per_frame = 2;
01294
01295 if (s->aspect_ratio_info > 1) {
01296 AVRational dar =
01297 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
01298 (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
01299 (AVRational) {s->width, s->height});
01300
01301
01302
01303
01304 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
01305 (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
01306 s->avctx->sample_aspect_ratio =
01307 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
01308 (AVRational) {s->width, s->height});
01309 } else {
01310 s->avctx->sample_aspect_ratio =
01311 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
01312 (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
01313
01314
01315
01316
01317
01318
01319 }
01320 } else {
01321 s->avctx->sample_aspect_ratio =
01322 ff_mpeg2_aspect[s->aspect_ratio_info];
01323 }
01324 }
01325
01326 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
01327 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
01328
01329 if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
01330 avctx->hwaccel )
01331 if (avctx->idct_algo == FF_IDCT_AUTO)
01332 avctx->idct_algo = FF_IDCT_SIMPLE;
01333
01334
01335
01336 memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
01337
01338 if (ff_MPV_common_init(s) < 0)
01339 return -2;
01340
01341 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
01342 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
01343 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
01344 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
01345
01346 s1->mpeg_enc_ctx_allocated = 1;
01347 }
01348 return 0;
01349 }
01350
01351 static int mpeg1_decode_picture(AVCodecContext *avctx,
01352 const uint8_t *buf, int buf_size)
01353 {
01354 Mpeg1Context *s1 = avctx->priv_data;
01355 MpegEncContext *s = &s1->mpeg_enc_ctx;
01356 int ref, f_code, vbv_delay;
01357
01358 init_get_bits(&s->gb, buf, buf_size*8);
01359
01360 ref = get_bits(&s->gb, 10);
01361 s->pict_type = get_bits(&s->gb, 3);
01362 if (s->pict_type == 0 || s->pict_type > 3)
01363 return -1;
01364
01365 vbv_delay = get_bits(&s->gb, 16);
01366 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
01367 s->full_pel[0] = get_bits1(&s->gb);
01368 f_code = get_bits(&s->gb, 3);
01369 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
01370 return -1;
01371 f_code += !f_code;
01372 s->mpeg_f_code[0][0] = f_code;
01373 s->mpeg_f_code[0][1] = f_code;
01374 }
01375 if (s->pict_type == AV_PICTURE_TYPE_B) {
01376 s->full_pel[1] = get_bits1(&s->gb);
01377 f_code = get_bits(&s->gb, 3);
01378 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
01379 return -1;
01380 f_code += !f_code;
01381 s->mpeg_f_code[1][0] = f_code;
01382 s->mpeg_f_code[1][1] = f_code;
01383 }
01384 s->current_picture.f.pict_type = s->pict_type;
01385 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
01386
01387 if (avctx->debug & FF_DEBUG_PICT_INFO)
01388 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
01389
01390 s->y_dc_scale = 8;
01391 s->c_dc_scale = 8;
01392 return 0;
01393 }
01394
01395 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
01396 {
01397 MpegEncContext *s= &s1->mpeg_enc_ctx;
01398 int horiz_size_ext, vert_size_ext;
01399 int bit_rate_ext;
01400
01401 skip_bits(&s->gb, 1);
01402 s->avctx->profile = get_bits(&s->gb, 3);
01403 s->avctx->level = get_bits(&s->gb, 4);
01404 s->progressive_sequence = get_bits1(&s->gb);
01405 s->chroma_format = get_bits(&s->gb, 2);
01406 horiz_size_ext = get_bits(&s->gb, 2);
01407 vert_size_ext = get_bits(&s->gb, 2);
01408 s->width |= (horiz_size_ext << 12);
01409 s->height |= (vert_size_ext << 12);
01410 bit_rate_ext = get_bits(&s->gb, 12);
01411 s->bit_rate += (bit_rate_ext << 18) * 400;
01412 skip_bits1(&s->gb);
01413 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
01414
01415 s->low_delay = get_bits1(&s->gb);
01416 if (s->flags & CODEC_FLAG_LOW_DELAY)
01417 s->low_delay = 1;
01418
01419 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
01420 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
01421
01422 av_dlog(s->avctx, "sequence extension\n");
01423 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
01424
01425 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01426 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
01427 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
01428
01429 }
01430
01431 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
01432 {
01433 MpegEncContext *s = &s1->mpeg_enc_ctx;
01434 int color_description, w, h;
01435
01436 skip_bits(&s->gb, 3);
01437 color_description = get_bits1(&s->gb);
01438 if (color_description) {
01439 s->avctx->color_primaries = get_bits(&s->gb, 8);
01440 s->avctx->color_trc = get_bits(&s->gb, 8);
01441 s->avctx->colorspace = get_bits(&s->gb, 8);
01442 }
01443 w = get_bits(&s->gb, 14);
01444 skip_bits(&s->gb, 1);
01445 h = get_bits(&s->gb, 14);
01446
01447
01448 s1->pan_scan.width = 16 * w;
01449 s1->pan_scan.height = 16 * h;
01450
01451 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01452 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
01453 }
01454
01455 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
01456 {
01457 MpegEncContext *s = &s1->mpeg_enc_ctx;
01458 int i, nofco;
01459
01460 nofco = 1;
01461 if (s->progressive_sequence) {
01462 if (s->repeat_first_field) {
01463 nofco++;
01464 if (s->top_field_first)
01465 nofco++;
01466 }
01467 } else {
01468 if (s->picture_structure == PICT_FRAME) {
01469 nofco++;
01470 if (s->repeat_first_field)
01471 nofco++;
01472 }
01473 }
01474 for (i = 0; i < nofco; i++) {
01475 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
01476 skip_bits(&s->gb, 1);
01477 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
01478 skip_bits(&s->gb, 1);
01479 }
01480
01481 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01482 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
01483 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
01484 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
01485 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
01486 }
01487
01488 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
01489 {
01490 int i;
01491
01492 for (i = 0; i < 64; i++) {
01493 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
01494 int v = get_bits(&s->gb, 8);
01495 if (v == 0) {
01496 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
01497 return -1;
01498 }
01499 if (intra && i == 0 && v != 8) {
01500 av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
01501 v = 8;
01502 }
01503 matrix0[j] = v;
01504 if (matrix1)
01505 matrix1[j] = v;
01506 }
01507 return 0;
01508 }
01509
01510 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
01511 {
01512 av_dlog(s->avctx, "matrix extension\n");
01513
01514 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
01515 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
01516 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
01517 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
01518 }
01519
01520 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
01521 {
01522 MpegEncContext *s = &s1->mpeg_enc_ctx;
01523
01524 s->full_pel[0] = s->full_pel[1] = 0;
01525 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
01526 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
01527 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
01528 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
01529 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
01530 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
01531 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
01532 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
01533 s->pict_type = AV_PICTURE_TYPE_I;
01534 else
01535 s->pict_type = AV_PICTURE_TYPE_P;
01536 } else
01537 s->pict_type = AV_PICTURE_TYPE_B;
01538 s->current_picture.f.pict_type = s->pict_type;
01539 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
01540 }
01541 s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
01542 s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
01543 s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
01544 s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
01545
01546 s->intra_dc_precision = get_bits(&s->gb, 2);
01547 s->picture_structure = get_bits(&s->gb, 2);
01548 s->top_field_first = get_bits1(&s->gb);
01549 s->frame_pred_frame_dct = get_bits1(&s->gb);
01550 s->concealment_motion_vectors = get_bits1(&s->gb);
01551 s->q_scale_type = get_bits1(&s->gb);
01552 s->intra_vlc_format = get_bits1(&s->gb);
01553 s->alternate_scan = get_bits1(&s->gb);
01554 s->repeat_first_field = get_bits1(&s->gb);
01555 s->chroma_420_type = get_bits1(&s->gb);
01556 s->progressive_frame = get_bits1(&s->gb);
01557
01558 if (s->progressive_sequence && !s->progressive_frame) {
01559 s->progressive_frame = 1;
01560 av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
01561 }
01562
01563 if (s->picture_structure == 0 || (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
01564 av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
01565 s->picture_structure = PICT_FRAME;
01566 }
01567
01568 if (s->progressive_sequence && !s->frame_pred_frame_dct) {
01569 av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
01570 }
01571
01572 if (s->picture_structure == PICT_FRAME) {
01573 s->first_field = 0;
01574 s->v_edge_pos = 16 * s->mb_height;
01575 } else {
01576 s->first_field ^= 1;
01577 s->v_edge_pos = 8 * s->mb_height;
01578 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
01579 }
01580
01581 if (s->alternate_scan) {
01582 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
01583 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
01584 } else {
01585 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
01586 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
01587 }
01588
01589
01590 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
01591 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
01592 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
01593 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
01594 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
01595 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
01596 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
01597 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
01598 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
01599 }
01600
01601 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
01602 {
01603 AVCodecContext *avctx = s->avctx;
01604 Mpeg1Context *s1 = (Mpeg1Context*)s;
01605
01606
01607 if (s->first_field || s->picture_structure == PICT_FRAME) {
01608 if (ff_MPV_frame_start(s, avctx) < 0)
01609 return -1;
01610
01611 ff_er_frame_start(s);
01612
01613
01614 s->current_picture_ptr->f.repeat_pict = 0;
01615 if (s->repeat_first_field) {
01616 if (s->progressive_sequence) {
01617 if (s->top_field_first)
01618 s->current_picture_ptr->f.repeat_pict = 4;
01619 else
01620 s->current_picture_ptr->f.repeat_pict = 2;
01621 } else if (s->progressive_frame) {
01622 s->current_picture_ptr->f.repeat_pict = 1;
01623 }
01624 }
01625
01626 *s->current_picture_ptr->f.pan_scan = s1->pan_scan;
01627
01628 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
01629 ff_thread_finish_setup(avctx);
01630 } else {
01631 int i;
01632
01633 if (!s->current_picture_ptr) {
01634 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
01635 return -1;
01636 }
01637
01638 if (s->avctx->hwaccel &&
01639 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
01640 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
01641 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode first field\n");
01642 }
01643
01644 for (i = 0; i < 4; i++) {
01645 s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
01646 if (s->picture_structure == PICT_BOTTOM_FIELD) {
01647 s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
01648 }
01649 }
01650 }
01651
01652 if (avctx->hwaccel) {
01653 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
01654 return -1;
01655 }
01656
01657
01658
01659 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01660 if (ff_xvmc_field_start(s, avctx) < 0)
01661 return -1;
01662
01663 return 0;
01664 }
01665
01666 #define DECODE_SLICE_ERROR -1
01667 #define DECODE_SLICE_OK 0
01668
01675 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
01676 const uint8_t **buf, int buf_size)
01677 {
01678 AVCodecContext *avctx = s->avctx;
01679 const int lowres = s->avctx->lowres;
01680 const int field_pic = s->picture_structure != PICT_FRAME;
01681
01682 s->resync_mb_x =
01683 s->resync_mb_y = -1;
01684
01685 assert(mb_y < s->mb_height);
01686
01687 init_get_bits(&s->gb, *buf, buf_size * 8);
01688 if(s->codec_id != CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
01689 skip_bits(&s->gb, 3);
01690
01691 ff_mpeg1_clean_buffers(s);
01692 s->interlaced_dct = 0;
01693
01694 s->qscale = get_qscale(s);
01695
01696 if (s->qscale == 0) {
01697 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
01698 return -1;
01699 }
01700
01701
01702 while (get_bits1(&s->gb) != 0) {
01703 skip_bits(&s->gb, 8);
01704 }
01705
01706 s->mb_x = 0;
01707
01708 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
01709 skip_bits1(&s->gb);
01710 } else {
01711 while (get_bits_left(&s->gb) > 0) {
01712 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01713 if (code < 0) {
01714 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
01715 return -1;
01716 }
01717 if (code >= 33) {
01718 if (code == 33) {
01719 s->mb_x += 33;
01720 }
01721
01722 } else {
01723 s->mb_x += code;
01724 break;
01725 }
01726 }
01727 }
01728
01729 if (s->mb_x >= (unsigned)s->mb_width) {
01730 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
01731 return -1;
01732 }
01733
01734 if (avctx->hwaccel) {
01735 const uint8_t *buf_end, *buf_start = *buf - 4;
01736 int start_code = -1;
01737 buf_end = avpriv_mpv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
01738 if (buf_end < *buf + buf_size)
01739 buf_end -= 4;
01740 s->mb_y = mb_y;
01741 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
01742 return DECODE_SLICE_ERROR;
01743 *buf = buf_end;
01744 return DECODE_SLICE_OK;
01745 }
01746
01747 s->resync_mb_x = s->mb_x;
01748 s->resync_mb_y = s->mb_y = mb_y;
01749 s->mb_skip_run = 0;
01750 ff_init_block_index(s);
01751
01752 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
01753 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
01754 av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
01755 s->qscale, s->mpeg_f_code[0][0], s->mpeg_f_code[0][1], s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
01756 s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
01757 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
01758 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
01759 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
01760 }
01761 }
01762
01763 for (;;) {
01764
01765 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
01766 ff_xvmc_init_block(s);
01767
01768 if (mpeg_decode_mb(s, s->block) < 0)
01769 return -1;
01770
01771 if (s->current_picture.f.motion_val[0] && !s->encoding) {
01772 const int wrap = s->b8_stride;
01773 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
01774 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
01775 int motion_x, motion_y, dir, i;
01776
01777 for (i = 0; i < 2; i++) {
01778 for (dir = 0; dir < 2; dir++) {
01779 if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
01780 motion_x = motion_y = 0;
01781 } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
01782 motion_x = s->mv[dir][0][0];
01783 motion_y = s->mv[dir][0][1];
01784 } else {
01785 motion_x = s->mv[dir][i][0];
01786 motion_y = s->mv[dir][i][1];
01787 }
01788
01789 s->current_picture.f.motion_val[dir][xy ][0] = motion_x;
01790 s->current_picture.f.motion_val[dir][xy ][1] = motion_y;
01791 s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
01792 s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
01793 s->current_picture.f.ref_index [dir][b8_xy ] =
01794 s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
01795 assert(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
01796 }
01797 xy += wrap;
01798 b8_xy +=2;
01799 }
01800 }
01801
01802 s->dest[0] += 16 >> lowres;
01803 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
01804 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
01805
01806 ff_MPV_decode_mb(s, s->block);
01807
01808 if (++s->mb_x >= s->mb_width) {
01809 const int mb_size = 16 >> s->avctx->lowres;
01810
01811 ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
01812 ff_MPV_report_decode_progress(s);
01813
01814 s->mb_x = 0;
01815 s->mb_y += 1 << field_pic;
01816
01817 if (s->mb_y >= s->mb_height) {
01818 int left = get_bits_left(&s->gb);
01819 int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
01820 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
01821 && s->progressive_frame == 0 ;
01822
01823 if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
01824 || ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
01825 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
01826 return -1;
01827 } else
01828 goto eos;
01829 }
01830
01831 ff_init_block_index(s);
01832 }
01833
01834
01835 if (s->mb_skip_run == -1) {
01836
01837 s->mb_skip_run = 0;
01838 for (;;) {
01839 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01840 if (code < 0) {
01841 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
01842 return -1;
01843 }
01844 if (code >= 33) {
01845 if (code == 33) {
01846 s->mb_skip_run += 33;
01847 } else if (code == 35) {
01848 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
01849 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
01850 return -1;
01851 }
01852 goto eos;
01853 }
01854
01855 } else {
01856 s->mb_skip_run += code;
01857 break;
01858 }
01859 }
01860 if (s->mb_skip_run) {
01861 int i;
01862 if (s->pict_type == AV_PICTURE_TYPE_I) {
01863 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
01864 return -1;
01865 }
01866
01867
01868 s->mb_intra = 0;
01869 for (i = 0; i < 12; i++)
01870 s->block_last_index[i] = -1;
01871 if (s->picture_structure == PICT_FRAME)
01872 s->mv_type = MV_TYPE_16X16;
01873 else
01874 s->mv_type = MV_TYPE_FIELD;
01875 if (s->pict_type == AV_PICTURE_TYPE_P) {
01876
01877 s->mv_dir = MV_DIR_FORWARD;
01878 s->mv[0][0][0] = s->mv[0][0][1] = 0;
01879 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
01880 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
01881 s->field_select[0][0] = (s->picture_structure - 1) & 1;
01882 } else {
01883
01884 s->mv[0][0][0] = s->last_mv[0][0][0];
01885 s->mv[0][0][1] = s->last_mv[0][0][1];
01886 s->mv[1][0][0] = s->last_mv[1][0][0];
01887 s->mv[1][0][1] = s->last_mv[1][0][1];
01888 }
01889 }
01890 }
01891 }
01892 eos:
01893 *buf += (get_bits_count(&s->gb)-1)/8;
01894
01895 return 0;
01896 }
01897
01898 static int slice_decode_thread(AVCodecContext *c, void *arg)
01899 {
01900 MpegEncContext *s = *(void**)arg;
01901 const uint8_t *buf = s->gb.buffer;
01902 int mb_y = s->start_mb_y;
01903 const int field_pic = s->picture_structure != PICT_FRAME;
01904
01905 s->error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
01906
01907 for (;;) {
01908 uint32_t start_code;
01909 int ret;
01910
01911 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
01912 emms_c();
01913
01914
01915 if (ret < 0) {
01916 if (c->err_recognition & AV_EF_EXPLODE)
01917 return ret;
01918 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
01919 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
01920 } else {
01921 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
01922 }
01923
01924 if (s->mb_y == s->end_mb_y)
01925 return 0;
01926
01927 start_code = -1;
01928 buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code);
01929 mb_y= start_code - SLICE_MIN_START_CODE;
01930 if(s->codec_id != CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
01931 mb_y += (*buf&0xE0)<<2;
01932 mb_y <<= field_pic;
01933 if (s->picture_structure == PICT_BOTTOM_FIELD)
01934 mb_y++;
01935 if (mb_y < 0 || mb_y >= s->end_mb_y)
01936 return -1;
01937 }
01938 }
01939
01944 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
01945 {
01946 Mpeg1Context *s1 = avctx->priv_data;
01947 MpegEncContext *s = &s1->mpeg_enc_ctx;
01948
01949 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
01950 return 0;
01951
01952 if (s->avctx->hwaccel) {
01953 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
01954 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
01955 }
01956
01957 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01958 ff_xvmc_field_end(s);
01959
01960
01961 if ( !s->first_field && !s->first_slice) {
01962
01963
01964 s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_MPEG2;
01965
01966 ff_er_frame_end(s);
01967
01968 ff_MPV_frame_end(s);
01969
01970 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
01971 *pict = s->current_picture_ptr->f;
01972 ff_print_debug_info(s, pict);
01973 } else {
01974 if (avctx->active_thread_type & FF_THREAD_FRAME)
01975 s->picture_number++;
01976
01977
01978 if (s->last_picture_ptr != NULL) {
01979 *pict = s->last_picture_ptr->f;
01980 ff_print_debug_info(s, pict);
01981 }
01982 }
01983
01984 return 1;
01985 } else {
01986 return 0;
01987 }
01988 }
01989
01990 static int mpeg1_decode_sequence(AVCodecContext *avctx,
01991 const uint8_t *buf, int buf_size)
01992 {
01993 Mpeg1Context *s1 = avctx->priv_data;
01994 MpegEncContext *s = &s1->mpeg_enc_ctx;
01995 int width, height;
01996 int i, v, j;
01997
01998 init_get_bits(&s->gb, buf, buf_size*8);
01999
02000 width = get_bits(&s->gb, 12);
02001 height = get_bits(&s->gb, 12);
02002 if (width <= 0 || height <= 0)
02003 return -1;
02004 s->aspect_ratio_info = get_bits(&s->gb, 4);
02005 if (s->aspect_ratio_info == 0) {
02006 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
02007 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
02008 return -1;
02009 }
02010 s->frame_rate_index = get_bits(&s->gb, 4);
02011 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
02012 return -1;
02013 s->bit_rate = get_bits(&s->gb, 18) * 400;
02014 if (get_bits1(&s->gb) == 0)
02015 return -1;
02016 s->width = width;
02017 s->height = height;
02018
02019 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
02020 skip_bits(&s->gb, 1);
02021
02022
02023 if (get_bits1(&s->gb)) {
02024 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
02025 } else {
02026 for (i = 0; i < 64; i++) {
02027 j = s->dsp.idct_permutation[i];
02028 v = ff_mpeg1_default_intra_matrix[i];
02029 s->intra_matrix[j] = v;
02030 s->chroma_intra_matrix[j] = v;
02031 }
02032 }
02033 if (get_bits1(&s->gb)) {
02034 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
02035 } else {
02036 for (i = 0; i < 64; i++) {
02037 int j = s->dsp.idct_permutation[i];
02038 v = ff_mpeg1_default_non_intra_matrix[i];
02039 s->inter_matrix[j] = v;
02040 s->chroma_inter_matrix[j] = v;
02041 }
02042 }
02043
02044 if (show_bits(&s->gb, 23) != 0) {
02045 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
02046 return -1;
02047 }
02048
02049
02050 s->progressive_sequence = 1;
02051 s->progressive_frame = 1;
02052 s->picture_structure = PICT_FRAME;
02053 s->first_field = 0;
02054 s->frame_pred_frame_dct = 1;
02055 s->chroma_format = 1;
02056 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO;
02057 s->out_format = FMT_MPEG1;
02058 s->swap_uv = 0;
02059 if (s->flags & CODEC_FLAG_LOW_DELAY)
02060 s->low_delay = 1;
02061
02062 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
02063 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
02064 s->avctx->rc_buffer_size, s->bit_rate);
02065
02066 return 0;
02067 }
02068
02069 static int vcr2_init_sequence(AVCodecContext *avctx)
02070 {
02071 Mpeg1Context *s1 = avctx->priv_data;
02072 MpegEncContext *s = &s1->mpeg_enc_ctx;
02073 int i, v;
02074
02075
02076 s->out_format = FMT_MPEG1;
02077 if (s1->mpeg_enc_ctx_allocated) {
02078 ff_MPV_common_end(s);
02079 }
02080 s->width = avctx->coded_width;
02081 s->height = avctx->coded_height;
02082 avctx->has_b_frames = 0;
02083 s->low_delay = 1;
02084
02085 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
02086 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
02087
02088 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel )
02089 if (avctx->idct_algo == FF_IDCT_AUTO)
02090 avctx->idct_algo = FF_IDCT_SIMPLE;
02091
02092 if (ff_MPV_common_init(s) < 0)
02093 return -1;
02094 s1->mpeg_enc_ctx_allocated = 1;
02095
02096 for (i = 0; i < 64; i++) {
02097 int j = s->dsp.idct_permutation[i];
02098 v = ff_mpeg1_default_intra_matrix[i];
02099 s->intra_matrix[j] = v;
02100 s->chroma_intra_matrix[j] = v;
02101
02102 v = ff_mpeg1_default_non_intra_matrix[i];
02103 s->inter_matrix[j] = v;
02104 s->chroma_inter_matrix[j] = v;
02105 }
02106
02107 s->progressive_sequence = 1;
02108 s->progressive_frame = 1;
02109 s->picture_structure = PICT_FRAME;
02110 s->first_field = 0;
02111 s->frame_pred_frame_dct = 1;
02112 s->chroma_format = 1;
02113 if (s->codec_tag == AV_RL32("BW10")) {
02114 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO;
02115 } else {
02116 exchange_uv(s);
02117 s->swap_uv = 1;
02118 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
02119 }
02120 s1->save_width = s->width;
02121 s1->save_height = s->height;
02122 s1->save_progressive_seq = s->progressive_sequence;
02123 return 0;
02124 }
02125
02126
02127 static void mpeg_decode_user_data(AVCodecContext *avctx,
02128 const uint8_t *p, int buf_size)
02129 {
02130 Mpeg1Context *s = avctx->priv_data;
02131 const uint8_t *buf_end = p + buf_size;
02132
02133 if(buf_size > 29){
02134 int i;
02135 for(i=0; i<20; i++)
02136 if(!memcmp(p+i, "\0TMPGEXS\0", 9)){
02137 s->tmpgexs= 1;
02138 }
02139
02140
02141
02142
02143
02144 }
02145
02146
02147 if (buf_end - p >= 5 &&
02148 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
02149 int flags = p[4];
02150 p += 5;
02151 if (flags & 0x80) {
02152
02153 p += 2;
02154 }
02155 if (flags & 0x40) {
02156 if (buf_end - p < 1)
02157 return;
02158 avctx->dtg_active_format = p[0] & 0x0f;
02159 }
02160 }
02161 }
02162
02163 static void mpeg_decode_gop(AVCodecContext *avctx,
02164 const uint8_t *buf, int buf_size)
02165 {
02166 Mpeg1Context *s1 = avctx->priv_data;
02167 MpegEncContext *s = &s1->mpeg_enc_ctx;
02168 int broken_link;
02169 int64_t tc;
02170
02171 init_get_bits(&s->gb, buf, buf_size*8);
02172
02173 tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
02174
02175 s->closed_gop = get_bits1(&s->gb);
02176
02177
02178
02179 broken_link = get_bits1(&s->gb);
02180
02181 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
02182 char tcbuf[AV_TIMECODE_STR_SIZE];
02183 av_timecode_make_mpeg_tc_string(tcbuf, tc);
02184 av_log(s->avctx, AV_LOG_DEBUG,
02185 "GOP (%s) closed_gop=%d broken_link=%d\n",
02186 tcbuf, s->closed_gop, broken_link);
02187 }
02188 }
02193 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
02194 {
02195 int i;
02196 uint32_t state = pc->state;
02197
02198
02199 if (buf_size == 0)
02200 return 0;
02201
02202
02203
02204
02205
02206
02207
02208
02209
02210 for (i = 0; i < buf_size; i++) {
02211 assert(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
02212 if (pc->frame_start_found & 1) {
02213 if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
02214 pc->frame_start_found--;
02215 else if (state == EXT_START_CODE + 2) {
02216 if ((buf[i] & 3) == 3)
02217 pc->frame_start_found = 0;
02218 else
02219 pc->frame_start_found = (pc->frame_start_found + 1) & 3;
02220 }
02221 state++;
02222 } else {
02223 i = avpriv_mpv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
02224 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
02225 i++;
02226 pc->frame_start_found = 4;
02227 }
02228 if (state == SEQ_END_CODE) {
02229 pc->frame_start_found = 0;
02230 pc->state=-1;
02231 return i+1;
02232 }
02233 if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
02234 pc->frame_start_found = 0;
02235 if (pc->frame_start_found < 4 && state == EXT_START_CODE)
02236 pc->frame_start_found++;
02237 if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
02238 if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
02239 pc->frame_start_found = 0;
02240 pc->state = -1;
02241 return i - 3;
02242 }
02243 }
02244 if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
02245 ff_fetch_timestamp(s, i - 3, 1);
02246 }
02247 }
02248 }
02249 pc->state = state;
02250 return END_NOT_FOUND;
02251 }
02252
02253 static int decode_chunks(AVCodecContext *avctx,
02254 AVFrame *picture, int *data_size,
02255 const uint8_t *buf, int buf_size);
02256
02257
02258 static int mpeg_decode_frame(AVCodecContext *avctx,
02259 void *data, int *data_size,
02260 AVPacket *avpkt)
02261 {
02262 const uint8_t *buf = avpkt->data;
02263 int buf_size = avpkt->size;
02264 Mpeg1Context *s = avctx->priv_data;
02265 AVFrame *picture = data;
02266 MpegEncContext *s2 = &s->mpeg_enc_ctx;
02267 av_dlog(avctx, "fill_buffer\n");
02268
02269 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
02270
02271 if (s2->low_delay == 0 && s2->next_picture_ptr) {
02272 *picture = s2->next_picture_ptr->f;
02273 s2->next_picture_ptr = NULL;
02274
02275 *data_size = sizeof(AVFrame);
02276 }
02277 return buf_size;
02278 }
02279
02280 if (s2->flags & CODEC_FLAG_TRUNCATED) {
02281 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
02282
02283 if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
02284 return buf_size;
02285 }
02286
02287 s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
02288 if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
02289 || s2->codec_tag == AV_RL32("BW10")
02290 ))
02291 vcr2_init_sequence(avctx);
02292
02293 s->slice_count = 0;
02294
02295 if (avctx->extradata && !s->parsed_extra) {
02296 int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
02297 if(*data_size) {
02298 av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
02299 *data_size = 0;
02300 }
02301 s->parsed_extra = 1;
02302 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
02303 return ret;
02304 }
02305
02306 return decode_chunks(avctx, picture, data_size, buf, buf_size);
02307 }
02308
02309 static int decode_chunks(AVCodecContext *avctx,
02310 AVFrame *picture, int *data_size,
02311 const uint8_t *buf, int buf_size)
02312 {
02313 Mpeg1Context *s = avctx->priv_data;
02314 MpegEncContext *s2 = &s->mpeg_enc_ctx;
02315 const uint8_t *buf_ptr = buf;
02316 const uint8_t *buf_end = buf + buf_size;
02317 int ret, input_size;
02318 int last_code = 0;
02319
02320 for (;;) {
02321
02322 uint32_t start_code = -1;
02323 buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
02324 if (start_code > 0x1ff) {
02325 if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
02326 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
02327 int i;
02328 av_assert0(avctx->thread_count > 1);
02329
02330 avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
02331 for (i = 0; i < s->slice_count; i++)
02332 s2->error_count += s2->thread_context[i]->error_count;
02333 }
02334
02335 if (CONFIG_VDPAU && uses_vdpau(avctx))
02336 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
02337
02338
02339 if (slice_end(avctx, picture)) {
02340 if (s2->last_picture_ptr || s2->low_delay)
02341 *data_size = sizeof(AVPicture);
02342 }
02343 }
02344 s2->pict_type = 0;
02345 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
02346 }
02347
02348 input_size = buf_end - buf_ptr;
02349
02350 if (avctx->debug & FF_DEBUG_STARTCODE) {
02351 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
02352 }
02353
02354
02355 switch (start_code) {
02356 case SEQ_START_CODE:
02357 if (last_code == 0) {
02358 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
02359 if(buf != avctx->extradata)
02360 s->sync=1;
02361 } else {
02362 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
02363 if (avctx->err_recognition & AV_EF_EXPLODE)
02364 return AVERROR_INVALIDDATA;
02365 }
02366 break;
02367
02368 case PICTURE_START_CODE:
02369 if(s->tmpgexs){
02370 s2->intra_dc_precision= 3;
02371 s2->intra_matrix[0]= 1;
02372 }
02373 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
02374 int i;
02375
02376 avctx->execute(avctx, slice_decode_thread,
02377 s2->thread_context, NULL,
02378 s->slice_count, sizeof(void*));
02379 for (i = 0; i < s->slice_count; i++)
02380 s2->error_count += s2->thread_context[i]->error_count;
02381 s->slice_count = 0;
02382 }
02383 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
02384 ret = mpeg_decode_postinit(avctx);
02385 if (ret < 0) {
02386 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
02387 return ret;
02388 }
02389
02390
02391 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
02392 s2->pict_type = 0;
02393 s2->first_slice = 1;
02394 last_code = PICTURE_START_CODE;
02395 } else {
02396 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
02397 if (avctx->err_recognition & AV_EF_EXPLODE)
02398 return AVERROR_INVALIDDATA;
02399 }
02400 break;
02401 case EXT_START_CODE:
02402 init_get_bits(&s2->gb, buf_ptr, input_size*8);
02403
02404 switch (get_bits(&s2->gb, 4)) {
02405 case 0x1:
02406 if (last_code == 0) {
02407 mpeg_decode_sequence_extension(s);
02408 } else {
02409 av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
02410 if (avctx->err_recognition & AV_EF_EXPLODE)
02411 return AVERROR_INVALIDDATA;
02412 }
02413 break;
02414 case 0x2:
02415 mpeg_decode_sequence_display_extension(s);
02416 break;
02417 case 0x3:
02418 mpeg_decode_quant_matrix_extension(s2);
02419 break;
02420 case 0x7:
02421 mpeg_decode_picture_display_extension(s);
02422 break;
02423 case 0x8:
02424 if (last_code == PICTURE_START_CODE) {
02425 mpeg_decode_picture_coding_extension(s);
02426 } else {
02427 av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
02428 if (avctx->err_recognition & AV_EF_EXPLODE)
02429 return AVERROR_INVALIDDATA;
02430 }
02431 break;
02432 }
02433 break;
02434 case USER_START_CODE:
02435 mpeg_decode_user_data(avctx, buf_ptr, input_size);
02436 break;
02437 case GOP_START_CODE:
02438 if (last_code == 0) {
02439 s2->first_field=0;
02440 mpeg_decode_gop(avctx, buf_ptr, input_size);
02441 s->sync=1;
02442 } else {
02443 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
02444 if (avctx->err_recognition & AV_EF_EXPLODE)
02445 return AVERROR_INVALIDDATA;
02446 }
02447 break;
02448 default:
02449 if (start_code >= SLICE_MIN_START_CODE &&
02450 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
02451 const int field_pic = s2->picture_structure != PICT_FRAME;
02452 int mb_y = start_code - SLICE_MIN_START_CODE;
02453 last_code = SLICE_MIN_START_CODE;
02454 if(s2->codec_id != CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
02455 mb_y += (*buf_ptr&0xE0)<<2;
02456
02457 mb_y <<= field_pic;
02458 if (s2->picture_structure == PICT_BOTTOM_FIELD)
02459 mb_y++;
02460
02461 if (mb_y >= s2->mb_height) {
02462 av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
02463 return -1;
02464 }
02465
02466 if (s2->last_picture_ptr == NULL) {
02467
02468 if (s2->pict_type == AV_PICTURE_TYPE_B) {
02469 if (!s2->closed_gop)
02470 break;
02471 }
02472 }
02473 if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
02474 s->sync=1;
02475 if (s2->next_picture_ptr == NULL) {
02476
02477 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
02478 }
02479 if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
02480 (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
02481 avctx->skip_frame >= AVDISCARD_ALL)
02482 break;
02483
02484 if (!s->mpeg_enc_ctx_allocated)
02485 break;
02486
02487 if (s2->codec_id == CODEC_ID_MPEG2VIDEO) {
02488 if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
02489 break;
02490 }
02491
02492 if (!s2->pict_type) {
02493 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
02494 if (avctx->err_recognition & AV_EF_EXPLODE)
02495 return AVERROR_INVALIDDATA;
02496 break;
02497 }
02498
02499 if (s2->first_slice) {
02500 s2->first_slice = 0;
02501 if (mpeg_field_start(s2, buf, buf_size) < 0)
02502 return -1;
02503 }
02504 if (!s2->current_picture_ptr) {
02505 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
02506 return AVERROR_INVALIDDATA;
02507 }
02508
02509 if (uses_vdpau(avctx)) {
02510 s->slice_count++;
02511 break;
02512 }
02513
02514 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
02515 int threshold = (s2->mb_height * s->slice_count +
02516 s2->slice_context_count / 2) /
02517 s2->slice_context_count;
02518 av_assert0(avctx->thread_count > 1);
02519 if (threshold <= mb_y) {
02520 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
02521
02522 thread_context->start_mb_y = mb_y;
02523 thread_context->end_mb_y = s2->mb_height;
02524 if (s->slice_count) {
02525 s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
02526 ff_update_duplicate_context(thread_context, s2);
02527 }
02528 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
02529 s->slice_count++;
02530 }
02531 buf_ptr += 2;
02532 } else {
02533 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
02534 emms_c();
02535
02536 if (ret < 0) {
02537 if (avctx->err_recognition & AV_EF_EXPLODE)
02538 return ret;
02539 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
02540 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
02541 } else {
02542 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
02543 }
02544 }
02545 }
02546 break;
02547 }
02548 }
02549 }
02550
02551 static void flush(AVCodecContext *avctx)
02552 {
02553 Mpeg1Context *s = avctx->priv_data;
02554
02555 s->sync=0;
02556
02557 ff_mpeg_flush(avctx);
02558 }
02559
02560 static int mpeg_decode_end(AVCodecContext *avctx)
02561 {
02562 Mpeg1Context *s = avctx->priv_data;
02563
02564 if (s->mpeg_enc_ctx_allocated)
02565 ff_MPV_common_end(&s->mpeg_enc_ctx);
02566 return 0;
02567 }
02568
02569 static const AVProfile mpeg2_video_profiles[] = {
02570 { FF_PROFILE_MPEG2_422, "4:2:2" },
02571 { FF_PROFILE_MPEG2_HIGH, "High" },
02572 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
02573 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
02574 { FF_PROFILE_MPEG2_MAIN, "Main" },
02575 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
02576 { FF_PROFILE_RESERVED, "Reserved" },
02577 { FF_PROFILE_RESERVED, "Reserved" },
02578 { FF_PROFILE_UNKNOWN },
02579 };
02580
02581
02582 AVCodec ff_mpeg1video_decoder = {
02583 .name = "mpeg1video",
02584 .type = AVMEDIA_TYPE_VIDEO,
02585 .id = CODEC_ID_MPEG1VIDEO,
02586 .priv_data_size = sizeof(Mpeg1Context),
02587 .init = mpeg_decode_init,
02588 .close = mpeg_decode_end,
02589 .decode = mpeg_decode_frame,
02590 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
02591 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
02592 CODEC_CAP_SLICE_THREADS,
02593 .flush = flush,
02594 .max_lowres = 3,
02595 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02596 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
02597 };
02598
02599 AVCodec ff_mpeg2video_decoder = {
02600 .name = "mpeg2video",
02601 .type = AVMEDIA_TYPE_VIDEO,
02602 .id = CODEC_ID_MPEG2VIDEO,
02603 .priv_data_size = sizeof(Mpeg1Context),
02604 .init = mpeg_decode_init,
02605 .close = mpeg_decode_end,
02606 .decode = mpeg_decode_frame,
02607 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
02608 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
02609 CODEC_CAP_SLICE_THREADS,
02610 .flush = flush,
02611 .max_lowres = 3,
02612 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
02613 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
02614 };
02615
02616
02617 AVCodec ff_mpegvideo_decoder = {
02618 .name = "mpegvideo",
02619 .type = AVMEDIA_TYPE_VIDEO,
02620 .id = CODEC_ID_MPEG2VIDEO,
02621 .priv_data_size = sizeof(Mpeg1Context),
02622 .init = mpeg_decode_init,
02623 .close = mpeg_decode_end,
02624 .decode = mpeg_decode_frame,
02625 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
02626 .flush = flush,
02627 .max_lowres = 3,
02628 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02629 };
02630
02631 #if CONFIG_MPEG_XVMC_DECODER
02632 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
02633 {
02634 if (avctx->active_thread_type & FF_THREAD_SLICE)
02635 return -1;
02636 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
02637 return -1;
02638 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
02639 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
02640 }
02641 mpeg_decode_init(avctx);
02642
02643 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
02644 avctx->xvmc_acceleration = 2;
02645
02646 return 0;
02647 }
02648
02649 AVCodec ff_mpeg_xvmc_decoder = {
02650 .name = "mpegvideo_xvmc",
02651 .type = AVMEDIA_TYPE_VIDEO,
02652 .id = CODEC_ID_MPEG2VIDEO_XVMC,
02653 .priv_data_size = sizeof(Mpeg1Context),
02654 .init = mpeg_mc_decode_init,
02655 .close = mpeg_decode_end,
02656 .decode = mpeg_decode_frame,
02657 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
02658 CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
02659 .flush = flush,
02660 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
02661 };
02662
02663 #endif
02664
02665 #if CONFIG_MPEG_VDPAU_DECODER
02666 AVCodec ff_mpeg_vdpau_decoder = {
02667 .name = "mpegvideo_vdpau",
02668 .type = AVMEDIA_TYPE_VIDEO,
02669 .id = CODEC_ID_MPEG2VIDEO,
02670 .priv_data_size = sizeof(Mpeg1Context),
02671 .init = mpeg_decode_init,
02672 .close = mpeg_decode_end,
02673 .decode = mpeg_decode_frame,
02674 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
02675 CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02676 .flush = flush,
02677 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
02678 };
02679 #endif
02680
02681 #if CONFIG_MPEG1_VDPAU_DECODER
02682 AVCodec ff_mpeg1_vdpau_decoder = {
02683 .name = "mpeg1video_vdpau",
02684 .type = AVMEDIA_TYPE_VIDEO,
02685 .id = CODEC_ID_MPEG1VIDEO,
02686 .priv_data_size = sizeof(Mpeg1Context),
02687 .init = mpeg_decode_init,
02688 .close = mpeg_decode_end,
02689 .decode = mpeg_decode_frame,
02690 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
02691 CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02692 .flush = flush,
02693 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
02694 };
02695 #endif