28 #define UNCHECKED_BITSTREAM_READER 1
65 #define MB_TYPE_ZERO_MV 0x20000000
92 0, 1, 2, 3, 4, 5, 6, 7,
93 8, 10, 12, 14, 16, 18, 20, 22,
94 24, 28, 32, 36, 40, 44, 48, 52,
95 56, 64, 72, 80, 88, 96, 104, 112,
113 val = (val - 1) << shift;
125 #define check_scantable_index(ctx, x) \
128 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
129 ctx->mb_x, ctx->mb_y); \
130 return AVERROR_INVALIDDATA; \
142 const int qscale = s->
qscale;
145 component = (n <= 3 ? 0 : n - 4 + 1);
152 block[0] = dc * quant_matrix[0];
170 level = (level * qscale * quant_matrix[j]) >> 4;
171 level = (level - 1) | 1;
185 }
else if (level == 0) {
194 level = (level * qscale * quant_matrix[j]) >> 4;
195 level = (level - 1) | 1;
198 level = (level * qscale * quant_matrix[j]) >> 4;
199 level = (level - 1) | 1;
229 const int qscale = s->
qscale;
237 level = (3 * qscale * quant_matrix[0]) >> 5;
238 level = (level - 1) | 1;
256 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
257 level = (level - 1) | 1;
271 }
else if (level == 0) {
280 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
281 level = (level - 1) | 1;
284 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
285 level = (level - 1) | 1;
313 const int qscale = s->
qscale;
321 level = (3 * qscale) >> 1;
322 level = (level - 1) | 1;
341 level = ((level * 2 + 1) * qscale) >> 1;
342 level = (level - 1) | 1;
356 }
else if (level == 0) {
365 level = ((level * 2 + 1) * qscale) >> 1;
366 level = (level - 1) | 1;
369 level = ((level * 2 + 1) * qscale) >> 1;
370 level = (level - 1) | 1;
393 const uint16_t *quant_matrix;
394 const int qscale = s->
qscale;
410 level = (3 * qscale * quant_matrix[0]) >> 5;
430 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
446 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
449 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
463 block[63] ^= (mismatch & 1);
480 const int qscale = s->
qscale;
487 level = (3 * qscale) >> 1;
504 level = ((level * 2 + 1) * qscale) >> 1;
519 level = ((-level * 2 + 1) * qscale) >> 1;
522 level = ((level * 2 + 1) * qscale) >> 1;
546 const uint16_t *quant_matrix;
547 const int qscale = s->
qscale;
556 component = (n & 1) + 1;
566 mismatch = block[0] ^ 1;
583 }
else if (level != 0) {
587 level = (level * qscale * quant_matrix[j]) >> 4;
602 level = (-level * qscale * quant_matrix[j]) >> 4;
605 level = (level * qscale * quant_matrix[j]) >> 4;
614 block[63] ^= mismatch & 1;
632 const uint16_t *quant_matrix;
633 const int qscale = s->
qscale;
641 component = (n & 1) + 1;
664 if (level >= 64 || i > 63) {
666 }
else if (level != 0) {
669 level = (level * qscale * quant_matrix[j]) >> 4;
683 level = (-level * qscale * quant_matrix[j]) >> 4;
686 level = (level * qscale * quant_matrix[j]) >> 4;
728 int i, j, k, cbp,
val, mb_type, motion_type;
755 if ((s->
mv[0][0][0] | s->
mv[0][0][1] | s->
mv[1][0][0] | s->
mv[1][0][1]) == 0)
768 "invalid mb type in I Frame at %d %d\n",
781 "invalid mb type in P Frame at %d %d\n", s->
mb_x, s->
mb_y);
790 "invalid mb type in B Frame at %d %d\n", s->
mb_x, s->
mb_y);
834 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
839 for (i = 0; i < 6; i++)
842 for (i = 0; i < mb_block_count; i++)
847 for (i = 0; i < 6; i++)
892 s->
mv_dir = (mb_type >> 13) & 3;
894 switch (motion_type) {
899 for (i = 0; i < 2; i++) {
914 s->
mv[i][0][0] <<= 1;
915 s->
mv[i][0][1] <<= 1;
922 for (i = 0; i < 2; i++) {
925 for (j = 0; j < 2; j++) {
927 for (k = 0; k < 2; k++) {
931 s->
mv[i][j][k] =
val;
942 for (i = 0; i < 2; i++) {
944 for (j = 0; j < 2; j++) {
949 s->
mv[i][j][0] =
val;
953 s->
last_mv[i][j][1] = val << 1;
954 s->
mv[i][j][1] =
val;
962 for (i = 0; i < 2; i++) {
965 for (k = 0; k < 2; k++) {
970 s->
mv[i][0][k] =
val;
982 for (i = 0; i < 2; i++) {
984 int dmx, dmy, mx, my,
m;
993 s->
last_mv[i][0][1] >> my_shift);
997 s->
last_mv[i][0][1] = my << my_shift;
998 s->
last_mv[i][1][1] = my << my_shift;
1000 s->
mv[i][0][0] = mx;
1001 s->
mv[i][0][1] = my;
1002 s->
mv[i][1][0] = mx;
1003 s->
mv[i][1][1] = my;
1012 s->
mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1013 s->
mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1015 s->
mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1016 s->
mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1020 s->
mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1021 s->
mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1032 "00 motion_type at %d %d\n", s->
mb_x, s->
mb_y);
1042 if (mb_block_count > 6) {
1043 cbp <<= mb_block_count - 6;
1049 "invalid cbp %d at %d %d\n", cbp, s->
mb_x, s->
mb_y);
1054 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
1059 for (i = 0; i < 6; i++) {
1067 cbp <<= 12 - mb_block_count;
1069 for (i = 0; i < mb_block_count; i++) {
1070 if (cbp & (1 << 11)) {
1081 for (i = 0; i < 6; i++) {
1089 for (i = 0; i < 6; i++) {
1101 for (i = 0; i < 12; i++)
1119 for (i = 0; i < 64; i++)
1149 if (avctx == avctx_from ||
1150 !ctx_from->mpeg_enc_ctx_allocated ||
1151 !
s1->context_initialized)
1170 uint16_t temp_matrix[64];
1173 memcpy(temp_matrix, matrix, 64 *
sizeof(uint16_t));
1175 for (i = 0; i < 64; i++)
1176 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1180 #if CONFIG_MPEG1_XVMC_HWACCEL
1183 #if CONFIG_MPEG1_VDPAU_HWACCEL
1192 #if CONFIG_MPEG2_XVMC_HWACCEL
1195 #if CONFIG_MPEG2_VDPAU_HWACCEL
1199 #if CONFIG_MPEG2_DXVA2_HWACCEL
1202 #if CONFIG_MPEG2_VAAPI_HWACCEL
1343 s->avctx->sample_aspect_ratio =
1353 memcpy(old_permutation,
s->dsp.idct_permutation, 64 *
sizeof(
uint8_t));
1363 s1->mpeg_enc_ctx_allocated = 1;
1373 int ref, f_code, vbv_delay;
1408 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->
pict_type);
1418 int horiz_size_ext, vert_size_ext;
1428 s->
width |= (horiz_size_ext << 12);
1429 s->
height |= (vert_size_ext << 12);
1431 s->
bit_rate += (bit_rate_ext << 18) * 400;
1447 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
1455 int color_description, w, h;
1459 if (color_description) {
1495 for (i = 0; i < nofco; i++) {
1510 uint16_t matrix1[64],
int intra)
1514 for (i = 0; i < 64; i++) {
1521 if (intra && i == 0 && v != 8) {
1557 "Missing picture start code, guessing missing values\n");
1670 "hardware accelerator failed to decode first field\n");
1673 for (i = 0; i < 4; i++) {
1689 #define DECODE_SLICE_ERROR -1
1690 #define DECODE_SLICE_OK 0
1757 const uint8_t *buf_end, *buf_start = *buf - 4;
1760 if (buf_end < *buf + buf_size)
1777 "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",
1797 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
1808 int motion_x, motion_y, dir, i;
1810 for (i = 0; i < 2; i++) {
1811 for (dir = 0; dir < 2; dir++) {
1814 motion_x = motion_y = 0;
1817 motion_x = s->
mv[dir][0][0];
1818 motion_y = s->
mv[dir][0][1];
1820 motion_x = s->
mv[dir][i][0];
1821 motion_y = s->
mv[dir][i][1];
1851 s->
mb_y += 1 << field_pic;
1863 if (left >= 32 && !is_d10) {
1899 }
else if (code == 35) {
1916 "skipped MB in I frame at %d %d\n", s->
mb_x, s->
mb_y);
1922 for (i = 0; i < 12; i++)
1931 s->
mv[0][0][0] = s->
mv[0][0][1] = 0;
1968 av_dlog(c,
"ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1991 mb_y += (*buf&0xE0)<<2;
1995 if (mb_y < 0 || mb_y >= s->
end_mb_y)
2015 "hardware accelerator failed to decode picture\n");
2064 if (width == 0 || height == 0) {
2066 "Invalid horizontal or vertical size value.\n");
2092 for (i = 0; i < 64; i++) {
2102 for (i = 0; i < 64; i++) {
2160 for (i = 0; i < 64; i++) {
2190 const uint8_t *p,
int buf_size)
2194 if (buf_size >= 6 &&
2195 p[0] ==
'G' && p[1] ==
'A' && p[2] ==
'9' && p[3] ==
'4' &&
2196 p[4] == 3 && (p[5] & 0x40)) {
2198 int cc_count = p[5] & 0x1f;
2199 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2207 }
else if (buf_size >= 11 &&
2208 p[0] ==
'C' && p[1] ==
'C' && p[2] == 0x01 && p[3] == 0xf8) {
2214 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2222 uint8_t field1 = !!(p[4] & 0x80);
2225 for (i = 0; i < cc_count; i++) {
2226 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2229 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2243 const uint8_t *p,
int buf_size)
2246 const uint8_t *buf_end = p + buf_size;
2251 if (!memcmp(p+i,
"\0TMPGEXS\0", 9)){
2262 if (buf_end - p >= 5 &&
2263 p[0] ==
'D' && p[1] ==
'T' && p[2] ==
'G' && p[3] ==
'1') {
2271 if (buf_end - p < 1)
2275 }
else if (buf_end - p >= 6 &&
2276 p[0] ==
'J' && p[1] ==
'P' && p[2] ==
'3' && p[3] ==
'D' &&
2279 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2281 if (S3D_video_format_type == 0x03 ||
2282 S3D_video_format_type == 0x04 ||
2283 S3D_video_format_type == 0x08 ||
2284 S3D_video_format_type == 0x23) {
2289 switch (S3D_video_format_type) {
2331 "GOP (%s) closed_gop=%d broken_link=%d\n",
2337 int *got_output,
const uint8_t *
buf,
int buf_size)
2342 const uint8_t *buf_end = buf + buf_size;
2343 int ret, input_size;
2344 int last_code = 0, skip_frame = 0;
2345 int picture_start_code_seen = 0;
2351 if (start_code > 0x1ff) {
2366 if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
2387 input_size = buf_end - buf_ptr;
2391 start_code, buf_ptr - buf, input_size);
2394 switch (start_code) {
2396 if (last_code == 0) {
2402 "ignoring SEQ_START_CODE after %X\n", last_code);
2415 picture_start_code_seen = 1;
2442 "mpeg_decode_postinit() failure\n");
2453 "ignoring pic after %X\n", last_code);
2463 if (last_code == 0) {
2467 "ignoring seq ext after %X\n", last_code);
2486 "ignoring pic cod ext after %X\n", last_code);
2497 if (last_code == 0) {
2503 "ignoring GOP_START_CODE after %X\n", last_code);
2514 "interlaced frame in progressive sequence, ignoring\n");
2520 "picture_structure %d invalid, ignoring\n",
2543 mb_y += (*buf_ptr&0xE0)<<2;
2549 if (buf_end - buf_ptr < 2) {
2556 "slice below image (%d >= %d)\n", mb_y, s2->
mb_height);
2593 if (mb_y < avctx->skip_top ||
2613 "current_picture not initialized\n");
2629 if (threshold <= mb_y) {
2672 int buf_size = avpkt->
size;
2676 av_dlog(avctx,
"fill_buffer\n");
2697 (
const uint8_t **) &buf, &buf_size) < 0)
2723 ret =
decode_chunks(avctx, picture, got_output, buf, buf_size);
2724 if (ret<0 || *got_output)
2762 .
name =
"mpeg1video",
2779 .
name =
"mpeg2video",
2797 .
name =
"mpegvideo",
2811 #if CONFIG_MPEG_XVMC_DECODER
2819 av_dlog(avctx,
"mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2829 AVCodec ff_mpeg_xvmc_decoder = {
2830 .
name =
"mpegvideo_xvmc",
2833 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2835 .
init = mpeg_mc_decode_init,
2846 #if CONFIG_MPEG_VDPAU_DECODER
2847 AVCodec ff_mpeg_vdpau_decoder = {
2848 .
name =
"mpegvideo_vdpau",
2862 #if CONFIG_MPEG1_VDPAU_DECODER
2863 AVCodec ff_mpeg1_vdpau_decoder = {
2864 .
name =
"mpeg1video_vdpau",