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