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