28 #define UNCHECKED_BITSTREAM_READER 1
72 #define MB_TYPE_ZERO_MV 0x20000000
99 0, 1, 2, 3, 4, 5, 6, 7,
100 8, 10, 12, 14, 16, 18, 20, 22,
101 24, 28, 32, 36, 40, 44, 48, 52,
102 56, 64, 72, 80, 88, 96, 104, 112,
120 val = (val - 1) << shift;
132 #define check_scantable_index(ctx, x) \
135 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
136 ctx->mb_x, ctx->mb_y); \
137 return AVERROR_INVALIDDATA; \
149 const int qscale = s->
qscale;
152 component = (n <= 3 ? 0 : n - 4 + 1);
159 block[0] = dc * quant_matrix[0];
177 level = (level * qscale * quant_matrix[j]) >> 4;
178 level = (level - 1) | 1;
192 }
else if (level == 0) {
201 level = (level * qscale * quant_matrix[j]) >> 4;
202 level = (level - 1) | 1;
205 level = (level * qscale * quant_matrix[j]) >> 4;
206 level = (level - 1) | 1;
236 const int qscale = s->
qscale;
244 level = (3 * qscale * quant_matrix[0]) >> 5;
245 level = (level - 1) | 1;
263 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
264 level = (level - 1) | 1;
278 }
else if (level == 0) {
287 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
288 level = (level - 1) | 1;
291 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
292 level = (level - 1) | 1;
320 const int qscale = s->
qscale;
328 level = (3 * qscale) >> 1;
329 level = (level - 1) | 1;
348 level = ((level * 2 + 1) * qscale) >> 1;
349 level = (level - 1) | 1;
363 }
else if (level == 0) {
372 level = ((level * 2 + 1) * qscale) >> 1;
373 level = (level - 1) | 1;
376 level = ((level * 2 + 1) * qscale) >> 1;
377 level = (level - 1) | 1;
400 const uint16_t *quant_matrix;
401 const int qscale = s->
qscale;
417 level = (3 * qscale * quant_matrix[0]) >> 5;
437 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
453 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
456 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
470 block[63] ^= (mismatch & 1);
487 const int qscale = s->
qscale;
494 level = (3 * qscale) >> 1;
511 level = ((level * 2 + 1) * qscale) >> 1;
526 level = ((-level * 2 + 1) * qscale) >> 1;
529 level = ((level * 2 + 1) * qscale) >> 1;
553 const uint16_t *quant_matrix;
554 const int qscale = s->
qscale;
563 component = (n & 1) + 1;
573 mismatch = block[0] ^ 1;
590 }
else if (level != 0) {
594 level = (level * qscale * quant_matrix[j]) >> 4;
609 level = (-level * qscale * quant_matrix[j]) >> 4;
612 level = (level * qscale * quant_matrix[j]) >> 4;
621 block[63] ^= mismatch & 1;
639 const uint16_t *quant_matrix;
640 const int qscale = s->
qscale;
648 component = (n & 1) + 1;
671 if (level >= 64 || i > 63) {
673 }
else if (level != 0) {
676 level = (level * qscale * quant_matrix[j]) >> 4;
690 level = (-level * qscale * quant_matrix[j]) >> 4;
693 level = (level * qscale * quant_matrix[j]) >> 4;
735 int i, j, k, cbp,
val, mb_type, motion_type;
763 if ((s->
mv[0][0][0] | s->
mv[0][0][1] | s->
mv[1][0][0] | s->
mv[1][0][1]) == 0)
776 "invalid mb type in I Frame at %d %d\n",
789 "invalid mb type in P Frame at %d %d\n", s->
mb_x, s->
mb_y);
798 "invalid mb type in B Frame at %d %d\n", s->
mb_x, s->
mb_y);
847 for (i = 0; i < 6; i++)
850 for (i = 0; i < mb_block_count; i++)
855 for (i = 0; i < 6; i++)
900 s->
mv_dir = (mb_type >> 13) & 3;
902 switch (motion_type) {
907 for (i = 0; i < 2; i++) {
922 s->
mv[i][0][0] <<= 1;
923 s->
mv[i][0][1] <<= 1;
930 for (i = 0; i < 2; i++) {
933 for (j = 0; j < 2; j++) {
935 for (k = 0; k < 2; k++) {
939 s->
mv[i][j][k] =
val;
950 for (i = 0; i < 2; i++) {
952 for (j = 0; j < 2; j++) {
957 s->
mv[i][j][0] =
val;
961 s->
last_mv[i][j][1] = val << 1;
962 s->
mv[i][j][1] =
val;
970 for (i = 0; i < 2; i++) {
973 for (k = 0; k < 2; k++) {
978 s->
mv[i][0][k] =
val;
990 for (i = 0; i < 2; i++) {
992 int dmx, dmy, mx, my,
m;
1001 s->
last_mv[i][0][1] >> my_shift);
1005 s->
last_mv[i][0][1] = my << my_shift;
1006 s->
last_mv[i][1][1] = my << my_shift;
1008 s->
mv[i][0][0] = mx;
1009 s->
mv[i][0][1] = my;
1010 s->
mv[i][1][0] = mx;
1011 s->
mv[i][1][1] = my;
1020 s->
mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1021 s->
mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1023 s->
mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1024 s->
mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1028 s->
mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1029 s->
mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1040 "00 motion_type at %d %d\n", s->
mb_x, s->
mb_y);
1050 if (mb_block_count > 6) {
1051 cbp <<= mb_block_count - 6;
1057 "invalid cbp %d at %d %d\n", cbp, s->
mb_x, s->
mb_y);
1067 for (i = 0; i < 6; i++) {
1075 cbp <<= 12 - mb_block_count;
1077 for (i = 0; i < mb_block_count; i++) {
1078 if (cbp & (1 << 11)) {
1089 for (i = 0; i < 6; i++) {
1097 for (i = 0; i < 6; i++) {
1109 for (i = 0; i < 12; i++)
1158 if (avctx == avctx_from ||
1159 !ctx_from->mpeg_enc_ctx_allocated ||
1160 !
s1->context_initialized)
1179 uint16_t temp_matrix[64];
1182 memcpy(temp_matrix, matrix, 64 *
sizeof(uint16_t));
1184 for (i = 0; i < 64; i++)
1185 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1189 #if CONFIG_MPEG1_XVMC_HWACCEL
1192 #if CONFIG_MPEG1_VDPAU_HWACCEL
1201 #if CONFIG_MPEG2_XVMC_HWACCEL
1204 #if CONFIG_MPEG2_VDPAU_HWACCEL
1208 #if CONFIG_MPEG2_DXVA2_HWACCEL
1211 #if CONFIG_MPEG2_VAAPI_HWACCEL
1263 avctx->xvmc_acceleration = 2;
1314 s->avctx->sample_aspect_ratio =
1320 avctx->sample_aspect_ratio) < 0) {
1322 avctx->sample_aspect_ratio.num,
1323 avctx->sample_aspect_ratio.den);
1324 avctx->sample_aspect_ratio = (
AVRational){ 0, 1 };
1327 if ((
s1->mpeg_enc_ctx_allocated == 0) ||
1328 avctx->coded_width !=
s->width ||
1329 avctx->coded_height !=
s->height ||
1330 s1->save_width !=
s->width ||
1331 s1->save_height !=
s->height ||
1332 av_cmp_q(
s1->save_aspect,
s->avctx->sample_aspect_ratio) ||
1333 (
s1->save_progressive_seq !=
s->progressive_sequence &&
FFALIGN(
s->height, 16) !=
FFALIGN(
s->height, 32)) ||
1335 if (
s1->mpeg_enc_ctx_allocated) {
1337 s->parse_context.buffer = 0;
1339 s->parse_context = pc;
1340 s1->mpeg_enc_ctx_allocated = 0;
1348 avctx->rc_max_rate =
s->bit_rate;
1350 (
s->bit_rate != 0x3FFFF*400 ||
s->vbv_delay != 0xFFFF)) {
1351 avctx->bit_rate =
s->bit_rate;
1353 s1->save_aspect =
s->avctx->sample_aspect_ratio;
1354 s1->save_width =
s->width;
1355 s1->save_height =
s->height;
1356 s1->save_progressive_seq =
s->progressive_sequence;
1360 avctx->has_b_frames = !
s->low_delay;
1365 avctx->ticks_per_frame = 1;
1369 &
s->avctx->framerate.den,
1373 avctx->ticks_per_frame = 2;
1381 memcpy(old_permutation,
s->idsp.idct_permutation, 64 *
sizeof(
uint8_t));
1392 s1->mpeg_enc_ctx_allocated = 1;
1402 int ref, f_code, vbv_delay;
1437 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->
pict_type);
1447 int horiz_size_ext, vert_size_ext;
1457 s->
width |= (horiz_size_ext << 12);
1458 s->
height |= (vert_size_ext << 12);
1460 s->
bit_rate += (bit_rate_ext << 18) * 400;
1476 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
1484 int color_description, w, h;
1488 if (color_description) {
1524 for (i = 0; i < nofco; i++) {
1533 "pde (%"PRId16
",%"PRId16
") (%"PRId16
",%"PRId16
") (%"PRId16
",%"PRId16
")\n",
1540 uint16_t matrix1[64],
int intra)
1544 for (i = 0; i < 64; i++) {
1551 if (intra && i == 0 && v != 8) {
1587 "Missing picture start code, guessing missing values\n");
1713 "hardware accelerator failed to decode first field\n");
1716 for (i = 0; i < 4; i++) {
1732 #define DECODE_SLICE_ERROR -1
1733 #define DECODE_SLICE_OK 0
1801 const uint8_t *buf_end, *buf_start = *buf - 4;
1804 if (buf_end < *buf + buf_size)
1821 "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",
1852 int motion_x, motion_y, dir, i;
1854 for (i = 0; i < 2; i++) {
1855 for (dir = 0; dir < 2; dir++) {
1858 motion_x = motion_y = 0;
1861 motion_x = s->
mv[dir][0][0];
1862 motion_y = s->
mv[dir][0][1];
1864 motion_x = s->
mv[dir][i][0];
1865 motion_y = s->
mv[dir][i][1];
1895 s->
mb_y += 1 << field_pic;
1907 if (left >= 32 && !is_d10) {
1955 }
else if (code == 35) {
1972 "skipped MB in I frame at %d %d\n", s->
mb_x, s->
mb_y);
1978 for (i = 0; i < 12; i++)
1987 s->
mv[0][0][0] = s->
mv[0][0][1] = 0;
2026 av_dlog(c,
"ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2049 mb_y += (*buf&0xE0)<<2;
2053 if (mb_y < 0 || mb_y >= s->
end_mb_y)
2073 "hardware accelerator failed to decode picture\n");
2122 if (width == 0 || height == 0) {
2124 "Invalid horizontal or vertical size value.\n");
2153 for (i = 0; i < 64; i++) {
2163 for (i = 0; i < 64; i++) {
2225 for (i = 0; i < 64; i++) {
2255 const uint8_t *p,
int buf_size)
2259 if (buf_size >= 6 &&
2260 p[0] ==
'G' && p[1] ==
'A' && p[2] ==
'9' && p[3] ==
'4' &&
2261 p[4] == 3 && (p[5] & 0x40)) {
2263 int cc_count = p[5] & 0x1f;
2264 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2272 }
else if (buf_size >= 11 &&
2273 p[0] ==
'C' && p[1] ==
'C' && p[2] == 0x01 && p[3] == 0xf8) {
2279 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2287 uint8_t field1 = !!(p[4] & 0x80);
2290 for (i = 0; i < cc_count; i++) {
2291 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2294 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2308 const uint8_t *p,
int buf_size)
2311 const uint8_t *buf_end = p + buf_size;
2316 for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2325 if (!memcmp(p+i,
"\0TMPGEXS\0", 9)){
2330 if (buf_end - p >= 5 &&
2331 p[0] ==
'D' && p[1] ==
'T' && p[2] ==
'G' && p[3] ==
'1') {
2339 if (buf_end - p < 1)
2347 s1->
afd = p[0] & 0x0f;
2349 }
else if (buf_end - p >= 6 &&
2350 p[0] ==
'J' && p[1] ==
'P' && p[2] ==
'3' && p[3] ==
'D' &&
2353 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2355 if (S3D_video_format_type == 0x03 ||
2356 S3D_video_format_type == 0x04 ||
2357 S3D_video_format_type == 0x08 ||
2358 S3D_video_format_type == 0x23) {
2362 switch (S3D_video_format_type) {
2404 "GOP (%s) closed_gop=%d broken_link=%d\n",
2410 int *got_output,
const uint8_t *
buf,
int buf_size)
2415 const uint8_t *buf_end = buf + buf_size;
2416 int ret, input_size;
2417 int last_code = 0, skip_frame = 0;
2418 int picture_start_code_seen = 0;
2424 if (start_code > 0x1ff) {
2460 input_size = buf_end - buf_ptr;
2464 start_code, buf_ptr - buf, input_size);
2467 switch (start_code) {
2469 if (last_code == 0) {
2475 "ignoring SEQ_START_CODE after %X\n", last_code);
2488 picture_start_code_seen = 1;
2515 "mpeg_decode_postinit() failure\n");
2526 "ignoring pic after %X\n", last_code);
2536 if (last_code == 0) {
2540 "ignoring seq ext after %X\n", last_code);
2559 "ignoring pic cod ext after %X\n", last_code);
2570 if (last_code == 0) {
2576 "ignoring GOP_START_CODE after %X\n", last_code);
2587 "interlaced frame in progressive sequence, ignoring\n");
2593 "picture_structure %d invalid, ignoring\n",
2616 mb_y += (*buf_ptr&0xE0)<<2;
2622 if (buf_end - buf_ptr < 2) {
2629 "slice below image (%d >= %d)\n", mb_y, s2->
mb_height);
2666 if (mb_y < avctx->skip_top ||
2686 "current_picture not initialized\n");
2702 if (threshold <= mb_y) {
2745 int buf_size = avpkt->
size;
2769 (
const uint8_t **) &buf, &buf_size) < 0)
2795 ret =
decode_chunks(avctx, picture, got_output, buf, buf_size);
2796 if (ret<0 || *got_output)
2834 .
name =
"mpeg1video",
2851 .
name =
"mpeg2video",
2869 .
name =
"mpegvideo",
2883 #if CONFIG_MPEG_XVMC_DECODER
2891 av_dlog(avctx,
"mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2896 avctx->xvmc_acceleration = 2;
2901 AVCodec ff_mpeg_xvmc_decoder = {
2902 .
name =
"mpegvideo_xvmc",
2905 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2907 .
init = mpeg_mc_decode_init,
2918 #if CONFIG_MPEG_VDPAU_DECODER
2919 AVCodec ff_mpeg_vdpau_decoder = {
2920 .
name =
"mpegvideo_vdpau",
2934 #if CONFIG_MPEG1_VDPAU_DECODER
2935 AVCodec ff_mpeg1_vdpau_decoder = {
2936 .
name =
"mpeg1video_vdpau",