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;
1361 s->avctx->sample_aspect_ratio =
1373 memcpy(old_permutation,
s->idsp.idct_permutation, 64 *
sizeof(
uint8_t));
1384 s1->mpeg_enc_ctx_allocated = 1;
1394 int ref, f_code, vbv_delay;
1429 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->
pict_type);
1439 int horiz_size_ext, vert_size_ext;
1449 s->
width |= (horiz_size_ext << 12);
1450 s->
height |= (vert_size_ext << 12);
1452 s->
bit_rate += (bit_rate_ext << 18) * 400;
1468 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
1476 int color_description, w, h;
1480 if (color_description) {
1516 for (i = 0; i < nofco; i++) {
1525 "pde (%"PRId16
",%"PRId16
") (%"PRId16
",%"PRId16
") (%"PRId16
",%"PRId16
")\n",
1532 uint16_t matrix1[64],
int intra)
1536 for (i = 0; i < 64; i++) {
1543 if (intra && i == 0 && v != 8) {
1579 "Missing picture start code, guessing missing values\n");
1705 "hardware accelerator failed to decode first field\n");
1708 for (i = 0; i < 4; i++) {
1724 #define DECODE_SLICE_ERROR -1
1725 #define DECODE_SLICE_OK 0
1793 const uint8_t *buf_end, *buf_start = *buf - 4;
1796 if (buf_end < *buf + buf_size)
1813 "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",
1833 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
1844 int motion_x, motion_y, dir, i;
1846 for (i = 0; i < 2; i++) {
1847 for (dir = 0; dir < 2; dir++) {
1850 motion_x = motion_y = 0;
1853 motion_x = s->
mv[dir][0][0];
1854 motion_y = s->
mv[dir][0][1];
1856 motion_x = s->
mv[dir][i][0];
1857 motion_y = s->
mv[dir][i][1];
1887 s->
mb_y += 1 << field_pic;
1899 if (left >= 32 && !is_d10) {
1947 }
else if (code == 35) {
1964 "skipped MB in I frame at %d %d\n", s->
mb_x, s->
mb_y);
1970 for (i = 0; i < 12; i++)
1979 s->
mv[0][0][0] = s->
mv[0][0][1] = 0;
2018 av_dlog(c,
"ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2041 mb_y += (*buf&0xE0)<<2;
2045 if (mb_y < 0 || mb_y >= s->
end_mb_y)
2065 "hardware accelerator failed to decode picture\n");
2114 if (width == 0 || height == 0) {
2116 "Invalid horizontal or vertical size value.\n");
2146 for (i = 0; i < 64; i++) {
2156 for (i = 0; i < 64; i++) {
2216 for (i = 0; i < 64; i++) {
2246 const uint8_t *p,
int buf_size)
2250 if (buf_size >= 6 &&
2251 p[0] ==
'G' && p[1] ==
'A' && p[2] ==
'9' && p[3] ==
'4' &&
2252 p[4] == 3 && (p[5] & 0x40)) {
2254 int cc_count = p[5] & 0x1f;
2255 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2263 }
else if (buf_size >= 11 &&
2264 p[0] ==
'C' && p[1] ==
'C' && p[2] == 0x01 && p[3] == 0xf8) {
2270 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2278 uint8_t field1 = !!(p[4] & 0x80);
2281 for (i = 0; i < cc_count; i++) {
2282 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2285 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2299 const uint8_t *p,
int buf_size)
2302 const uint8_t *buf_end = p + buf_size;
2308 if (!memcmp(p+i,
"\0TMPGEXS\0", 9)){
2319 if (buf_end - p >= 5 &&
2320 p[0] ==
'D' && p[1] ==
'T' && p[2] ==
'G' && p[3] ==
'1') {
2328 if (buf_end - p < 1)
2336 s1->
afd = p[0] & 0x0f;
2338 }
else if (buf_end - p >= 6 &&
2339 p[0] ==
'J' && p[1] ==
'P' && p[2] ==
'3' && p[3] ==
'D' &&
2342 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2344 if (S3D_video_format_type == 0x03 ||
2345 S3D_video_format_type == 0x04 ||
2346 S3D_video_format_type == 0x08 ||
2347 S3D_video_format_type == 0x23) {
2351 switch (S3D_video_format_type) {
2393 "GOP (%s) closed_gop=%d broken_link=%d\n",
2399 int *got_output,
const uint8_t *
buf,
int buf_size)
2404 const uint8_t *buf_end = buf + buf_size;
2405 int ret, input_size;
2406 int last_code = 0, skip_frame = 0;
2407 int picture_start_code_seen = 0;
2413 if (start_code > 0x1ff) {
2428 if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
2449 input_size = buf_end - buf_ptr;
2453 start_code, buf_ptr - buf, input_size);
2456 switch (start_code) {
2458 if (last_code == 0) {
2464 "ignoring SEQ_START_CODE after %X\n", last_code);
2477 picture_start_code_seen = 1;
2504 "mpeg_decode_postinit() failure\n");
2515 "ignoring pic after %X\n", last_code);
2525 if (last_code == 0) {
2529 "ignoring seq ext after %X\n", last_code);
2548 "ignoring pic cod ext after %X\n", last_code);
2559 if (last_code == 0) {
2565 "ignoring GOP_START_CODE after %X\n", last_code);
2576 "interlaced frame in progressive sequence, ignoring\n");
2582 "picture_structure %d invalid, ignoring\n",
2605 mb_y += (*buf_ptr&0xE0)<<2;
2611 if (buf_end - buf_ptr < 2) {
2618 "slice below image (%d >= %d)\n", mb_y, s2->
mb_height);
2655 if (mb_y < avctx->skip_top ||
2675 "current_picture not initialized\n");
2691 if (threshold <= mb_y) {
2734 int buf_size = avpkt->
size;
2758 (
const uint8_t **) &buf, &buf_size) < 0)
2784 ret =
decode_chunks(avctx, picture, got_output, buf, buf_size);
2785 if (ret<0 || *got_output)
2823 .
name =
"mpeg1video",
2840 .
name =
"mpeg2video",
2858 .
name =
"mpegvideo",
2872 #if CONFIG_MPEG_XVMC_DECODER
2880 av_dlog(avctx,
"mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2885 avctx->xvmc_acceleration = 2;
2890 AVCodec ff_mpeg_xvmc_decoder = {
2891 .
name =
"mpegvideo_xvmc",
2894 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2896 .
init = mpeg_mc_decode_init,
2907 #if CONFIG_MPEG_VDPAU_DECODER
2908 AVCodec ff_mpeg_vdpau_decoder = {
2909 .
name =
"mpegvideo_vdpau",
2923 #if CONFIG_MPEG1_VDPAU_DECODER
2924 AVCodec ff_mpeg1_vdpau_decoder = {
2925 .
name =
"mpeg1video_vdpau",