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