28 #define UNCHECKED_BITSTREAM_READER 1
71 #define MB_TYPE_ZERO_MV 0x20000000
98 0, 1, 2, 3, 4, 5, 6, 7,
99 8, 10, 12, 14, 16, 18, 20, 22,
100 24, 28, 32, 36, 40, 44, 48, 52,
101 56, 64, 72, 80, 88, 96, 104, 112,
119 val = (val - 1) << shift;
131 #define check_scantable_index(ctx, x) \
134 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
135 ctx->mb_x, ctx->mb_y); \
136 return AVERROR_INVALIDDATA; \
148 const int qscale = s->
qscale;
151 component = (n <= 3 ? 0 : n - 4 + 1);
158 block[0] = dc * quant_matrix[0];
176 level = (level * qscale * quant_matrix[j]) >> 4;
177 level = (level - 1) | 1;
191 }
else if (level == 0) {
200 level = (level * qscale * quant_matrix[j]) >> 4;
201 level = (level - 1) | 1;
204 level = (level * qscale * quant_matrix[j]) >> 4;
205 level = (level - 1) | 1;
235 const int qscale = s->
qscale;
243 level = (3 * qscale * quant_matrix[0]) >> 5;
244 level = (level - 1) | 1;
262 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
263 level = (level - 1) | 1;
277 }
else if (level == 0) {
286 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
287 level = (level - 1) | 1;
290 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
291 level = (level - 1) | 1;
319 const int qscale = s->
qscale;
327 level = (3 * qscale) >> 1;
328 level = (level - 1) | 1;
347 level = ((level * 2 + 1) * qscale) >> 1;
348 level = (level - 1) | 1;
362 }
else if (level == 0) {
371 level = ((level * 2 + 1) * qscale) >> 1;
372 level = (level - 1) | 1;
375 level = ((level * 2 + 1) * qscale) >> 1;
376 level = (level - 1) | 1;
399 const uint16_t *quant_matrix;
400 const int qscale = s->
qscale;
416 level = (3 * qscale * quant_matrix[0]) >> 5;
436 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
452 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
455 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
469 block[63] ^= (mismatch & 1);
486 const int qscale = s->
qscale;
493 level = (3 * qscale) >> 1;
510 level = ((level * 2 + 1) * qscale) >> 1;
525 level = ((-level * 2 + 1) * qscale) >> 1;
528 level = ((level * 2 + 1) * qscale) >> 1;
552 const uint16_t *quant_matrix;
553 const int qscale = s->
qscale;
562 component = (n & 1) + 1;
572 mismatch = block[0] ^ 1;
589 }
else if (level != 0) {
593 level = (level * qscale * quant_matrix[j]) >> 4;
608 level = (-level * qscale * quant_matrix[j]) >> 4;
611 level = (level * qscale * quant_matrix[j]) >> 4;
620 block[63] ^= mismatch & 1;
638 const uint16_t *quant_matrix;
639 const int qscale = s->
qscale;
647 component = (n & 1) + 1;
670 if (level >= 64 || i > 63) {
672 }
else if (level != 0) {
675 level = (level * qscale * quant_matrix[j]) >> 4;
689 level = (-level * qscale * quant_matrix[j]) >> 4;
692 level = (level * qscale * quant_matrix[j]) >> 4;
734 int i, j, k, cbp,
val, mb_type, motion_type;
762 if ((s->
mv[0][0][0] | s->
mv[0][0][1] | s->
mv[1][0][0] | s->
mv[1][0][1]) == 0)
775 "invalid mb type in I Frame at %d %d\n",
788 "invalid mb type in P Frame at %d %d\n", s->
mb_x, s->
mb_y);
797 "invalid mb type in B Frame at %d %d\n", s->
mb_x, s->
mb_y);
841 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
846 for (i = 0; i < 6; i++)
849 for (i = 0; i < mb_block_count; i++)
854 for (i = 0; i < 6; i++)
899 s->
mv_dir = (mb_type >> 13) & 3;
901 switch (motion_type) {
906 for (i = 0; i < 2; i++) {
921 s->
mv[i][0][0] <<= 1;
922 s->
mv[i][0][1] <<= 1;
929 for (i = 0; i < 2; i++) {
932 for (j = 0; j < 2; j++) {
934 for (k = 0; k < 2; k++) {
938 s->
mv[i][j][k] =
val;
949 for (i = 0; i < 2; i++) {
951 for (j = 0; j < 2; j++) {
956 s->
mv[i][j][0] =
val;
960 s->
last_mv[i][j][1] = val << 1;
961 s->
mv[i][j][1] =
val;
969 for (i = 0; i < 2; i++) {
972 for (k = 0; k < 2; k++) {
977 s->
mv[i][0][k] =
val;
989 for (i = 0; i < 2; i++) {
991 int dmx, dmy, mx, my,
m;
1000 s->
last_mv[i][0][1] >> my_shift);
1004 s->
last_mv[i][0][1] = my << my_shift;
1005 s->
last_mv[i][1][1] = my << my_shift;
1007 s->
mv[i][0][0] = mx;
1008 s->
mv[i][0][1] = my;
1009 s->
mv[i][1][0] = mx;
1010 s->
mv[i][1][1] = my;
1019 s->
mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1020 s->
mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1022 s->
mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1023 s->
mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1027 s->
mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1028 s->
mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1039 "00 motion_type at %d %d\n", s->
mb_x, s->
mb_y);
1049 if (mb_block_count > 6) {
1050 cbp <<= mb_block_count - 6;
1056 "invalid cbp %d at %d %d\n", cbp, s->
mb_x, s->
mb_y);
1061 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
1066 for (i = 0; i < 6; i++) {
1074 cbp <<= 12 - mb_block_count;
1076 for (i = 0; i < mb_block_count; i++) {
1077 if (cbp & (1 << 11)) {
1088 for (i = 0; i < 6; i++) {
1096 for (i = 0; i < 6; i++) {
1108 for (i = 0; i < 12; i++)
1157 if (avctx == avctx_from ||
1158 !ctx_from->mpeg_enc_ctx_allocated ||
1159 !
s1->context_initialized)
1178 uint16_t temp_matrix[64];
1181 memcpy(temp_matrix, matrix, 64 *
sizeof(uint16_t));
1183 for (i = 0; i < 64; i++)
1184 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1188 #if CONFIG_MPEG1_XVMC_HWACCEL
1191 #if CONFIG_MPEG1_VDPAU_HWACCEL
1200 #if CONFIG_MPEG2_XVMC_HWACCEL
1203 #if CONFIG_MPEG2_VDPAU_HWACCEL
1207 #if CONFIG_MPEG2_DXVA2_HWACCEL
1210 #if CONFIG_MPEG2_VAAPI_HWACCEL
1262 avctx->xvmc_acceleration = 2;
1313 s->avctx->sample_aspect_ratio =
1320 if ((
s1->mpeg_enc_ctx_allocated == 0) ||
1321 avctx->coded_width !=
s->width ||
1322 avctx->coded_height !=
s->height ||
1323 s1->save_width !=
s->width ||
1324 s1->save_height !=
s->height ||
1325 av_cmp_q(
s1->save_aspect,
s->avctx->sample_aspect_ratio) ||
1326 (
s1->save_progressive_seq !=
s->progressive_sequence &&
FFALIGN(
s->height, 16) !=
FFALIGN(
s->height, 32)) ||
1328 if (
s1->mpeg_enc_ctx_allocated) {
1330 s->parse_context.buffer = 0;
1332 s->parse_context = pc;
1333 s1->mpeg_enc_ctx_allocated = 0;
1341 avctx->rc_max_rate =
s->bit_rate;
1343 (
s->bit_rate != 0x3FFFF*400 ||
s->vbv_delay != 0xFFFF)) {
1344 avctx->bit_rate =
s->bit_rate;
1346 s1->save_aspect =
s->avctx->sample_aspect_ratio;
1347 s1->save_width =
s->width;
1348 s1->save_height =
s->height;
1349 s1->save_progressive_seq =
s->progressive_sequence;
1353 avctx->has_b_frames = !
s->low_delay;
1358 avctx->ticks_per_frame = 1;
1362 &
s->avctx->framerate.den,
1366 avctx->ticks_per_frame = 2;
1374 memcpy(old_permutation,
s->idsp.idct_permutation, 64 *
sizeof(
uint8_t));
1385 s1->mpeg_enc_ctx_allocated = 1;
1395 int ref, f_code, vbv_delay;
1430 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->
pict_type);
1440 int horiz_size_ext, vert_size_ext;
1450 s->
width |= (horiz_size_ext << 12);
1451 s->
height |= (vert_size_ext << 12);
1453 s->
bit_rate += (bit_rate_ext << 18) * 400;
1469 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
1477 int color_description, w, h;
1481 if (color_description) {
1517 for (i = 0; i < nofco; i++) {
1526 "pde (%"PRId16
",%"PRId16
") (%"PRId16
",%"PRId16
") (%"PRId16
",%"PRId16
")\n",
1533 uint16_t matrix1[64],
int intra)
1537 for (i = 0; i < 64; i++) {
1544 if (intra && i == 0 && v != 8) {
1580 "Missing picture start code, guessing missing values\n");
1706 "hardware accelerator failed to decode first field\n");
1709 for (i = 0; i < 4; i++) {
1725 #define DECODE_SLICE_ERROR -1
1726 #define DECODE_SLICE_OK 0
1794 const uint8_t *buf_end, *buf_start = *buf - 4;
1797 if (buf_end < *buf + buf_size)
1814 "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",
1834 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
1845 int motion_x, motion_y, dir, i;
1847 for (i = 0; i < 2; i++) {
1848 for (dir = 0; dir < 2; dir++) {
1851 motion_x = motion_y = 0;
1854 motion_x = s->
mv[dir][0][0];
1855 motion_y = s->
mv[dir][0][1];
1857 motion_x = s->
mv[dir][i][0];
1858 motion_y = s->
mv[dir][i][1];
1888 s->
mb_y += 1 << field_pic;
1900 if (left >= 32 && !is_d10) {
1948 }
else if (code == 35) {
1965 "skipped MB in I frame at %d %d\n", s->
mb_x, s->
mb_y);
1971 for (i = 0; i < 12; i++)
1980 s->
mv[0][0][0] = s->
mv[0][0][1] = 0;
2019 av_dlog(c,
"ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2042 mb_y += (*buf&0xE0)<<2;
2046 if (mb_y < 0 || mb_y >= s->
end_mb_y)
2066 "hardware accelerator failed to decode picture\n");
2115 if (width == 0 || height == 0) {
2117 "Invalid horizontal or vertical size value.\n");
2148 for (i = 0; i < 64; i++) {
2158 for (i = 0; i < 64; i++) {
2217 for (i = 0; i < 64; i++) {
2247 const uint8_t *p,
int buf_size)
2251 if (buf_size >= 6 &&
2252 p[0] ==
'G' && p[1] ==
'A' && p[2] ==
'9' && p[3] ==
'4' &&
2253 p[4] == 3 && (p[5] & 0x40)) {
2255 int cc_count = p[5] & 0x1f;
2256 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2264 }
else if (buf_size >= 11 &&
2265 p[0] ==
'C' && p[1] ==
'C' && p[2] == 0x01 && p[3] == 0xf8) {
2271 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2279 uint8_t field1 = !!(p[4] & 0x80);
2282 for (i = 0; i < cc_count; i++) {
2283 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2286 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2300 const uint8_t *p,
int buf_size)
2303 const uint8_t *buf_end = p + buf_size;
2308 for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2317 if (!memcmp(p+i,
"\0TMPGEXS\0", 9)){
2322 if (buf_end - p >= 5 &&
2323 p[0] ==
'D' && p[1] ==
'T' && p[2] ==
'G' && p[3] ==
'1') {
2331 if (buf_end - p < 1)
2339 s1->
afd = p[0] & 0x0f;
2341 }
else if (buf_end - p >= 6 &&
2342 p[0] ==
'J' && p[1] ==
'P' && p[2] ==
'3' && p[3] ==
'D' &&
2345 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2347 if (S3D_video_format_type == 0x03 ||
2348 S3D_video_format_type == 0x04 ||
2349 S3D_video_format_type == 0x08 ||
2350 S3D_video_format_type == 0x23) {
2354 switch (S3D_video_format_type) {
2396 "GOP (%s) closed_gop=%d broken_link=%d\n",
2402 int *got_output,
const uint8_t *
buf,
int buf_size)
2407 const uint8_t *buf_end = buf + buf_size;
2408 int ret, input_size;
2409 int last_code = 0, skip_frame = 0;
2410 int picture_start_code_seen = 0;
2416 if (start_code > 0x1ff) {
2431 if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
2452 input_size = buf_end - buf_ptr;
2456 start_code, buf_ptr - buf, input_size);
2459 switch (start_code) {
2461 if (last_code == 0) {
2467 "ignoring SEQ_START_CODE after %X\n", last_code);
2480 picture_start_code_seen = 1;
2507 "mpeg_decode_postinit() failure\n");
2518 "ignoring pic after %X\n", last_code);
2528 if (last_code == 0) {
2532 "ignoring seq ext after %X\n", last_code);
2551 "ignoring pic cod ext after %X\n", last_code);
2562 if (last_code == 0) {
2568 "ignoring GOP_START_CODE after %X\n", last_code);
2579 "interlaced frame in progressive sequence, ignoring\n");
2585 "picture_structure %d invalid, ignoring\n",
2608 mb_y += (*buf_ptr&0xE0)<<2;
2614 if (buf_end - buf_ptr < 2) {
2621 "slice below image (%d >= %d)\n", mb_y, s2->
mb_height);
2658 if (mb_y < avctx->skip_top ||
2678 "current_picture not initialized\n");
2694 if (threshold <= mb_y) {
2737 int buf_size = avpkt->
size;
2761 (
const uint8_t **) &buf, &buf_size) < 0)
2787 ret =
decode_chunks(avctx, picture, got_output, buf, buf_size);
2788 if (ret<0 || *got_output)
2826 .
name =
"mpeg1video",
2843 .
name =
"mpeg2video",
2861 .
name =
"mpegvideo",
2875 #if CONFIG_MPEG_XVMC_DECODER
2883 av_dlog(avctx,
"mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2888 avctx->xvmc_acceleration = 2;
2893 AVCodec ff_mpeg_xvmc_decoder = {
2894 .
name =
"mpegvideo_xvmc",
2897 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2899 .
init = mpeg_mc_decode_init,
2910 #if CONFIG_MPEG_VDPAU_DECODER
2911 AVCodec ff_mpeg_vdpau_decoder = {
2912 .
name =
"mpegvideo_vdpau",
2926 #if CONFIG_MPEG1_VDPAU_DECODER
2927 AVCodec ff_mpeg1_vdpau_decoder = {
2928 .
name =
"mpeg1video_vdpau",