28 #define UNCHECKED_BITSTREAM_READER 1
69 #define MB_TYPE_ZERO_MV 0x20000000
96 0, 1, 2, 3, 4, 5, 6, 7,
97 8, 10, 12, 14, 16, 18, 20, 22,
98 24, 28, 32, 36, 40, 44, 48, 52,
99 56, 64, 72, 80, 88, 96, 104, 112,
117 val = (val - 1) << shift;
129 #define check_scantable_index(ctx, x) \
132 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
133 ctx->mb_x, ctx->mb_y); \
134 return AVERROR_INVALIDDATA; \
146 const int qscale = s->
qscale;
149 component = (n <= 3 ? 0 : n - 4 + 1);
156 block[0] = dc * quant_matrix[0];
174 level = (level * qscale * quant_matrix[j]) >> 4;
175 level = (level - 1) | 1;
189 }
else if (level == 0) {
198 level = (level * qscale * quant_matrix[j]) >> 4;
199 level = (level - 1) | 1;
202 level = (level * qscale * quant_matrix[j]) >> 4;
203 level = (level - 1) | 1;
233 const int qscale = s->
qscale;
241 level = (3 * qscale * quant_matrix[0]) >> 5;
242 level = (level - 1) | 1;
260 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
261 level = (level - 1) | 1;
275 }
else if (level == 0) {
284 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
285 level = (level - 1) | 1;
288 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
289 level = (level - 1) | 1;
317 const int qscale = s->
qscale;
325 level = (3 * qscale) >> 1;
326 level = (level - 1) | 1;
345 level = ((level * 2 + 1) * qscale) >> 1;
346 level = (level - 1) | 1;
360 }
else if (level == 0) {
369 level = ((level * 2 + 1) * qscale) >> 1;
370 level = (level - 1) | 1;
373 level = ((level * 2 + 1) * qscale) >> 1;
374 level = (level - 1) | 1;
397 const uint16_t *quant_matrix;
398 const int qscale = s->
qscale;
414 level = (3 * qscale * quant_matrix[0]) >> 5;
434 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
450 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
453 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
467 block[63] ^= (mismatch & 1);
484 const int qscale = s->
qscale;
491 level = (3 * qscale) >> 1;
508 level = ((level * 2 + 1) * qscale) >> 1;
523 level = ((-level * 2 + 1) * qscale) >> 1;
526 level = ((level * 2 + 1) * qscale) >> 1;
550 const uint16_t *quant_matrix;
551 const int qscale = s->
qscale;
560 component = (n & 1) + 1;
570 mismatch = block[0] ^ 1;
587 }
else if (level != 0) {
591 level = (level * qscale * quant_matrix[j]) >> 4;
606 level = (-level * qscale * quant_matrix[j]) >> 4;
609 level = (level * qscale * quant_matrix[j]) >> 4;
618 block[63] ^= mismatch & 1;
636 const uint16_t *quant_matrix;
637 const int qscale = s->
qscale;
645 component = (n & 1) + 1;
668 if (level >= 64 || i > 63) {
670 }
else if (level != 0) {
673 level = (level * qscale * quant_matrix[j]) >> 4;
687 level = (-level * qscale * quant_matrix[j]) >> 4;
690 level = (level * qscale * quant_matrix[j]) >> 4;
732 int i, j, k, cbp,
val, mb_type, motion_type;
759 if ((s->
mv[0][0][0] | s->
mv[0][0][1] | s->
mv[1][0][0] | s->
mv[1][0][1]) == 0)
772 "invalid mb type in I Frame at %d %d\n",
785 "invalid mb type in P Frame at %d %d\n", s->
mb_x, s->
mb_y);
794 "invalid mb type in B Frame at %d %d\n", s->
mb_x, s->
mb_y);
838 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
843 for (i = 0; i < 6; i++)
846 for (i = 0; i < mb_block_count; i++)
851 for (i = 0; i < 6; i++)
896 s->
mv_dir = (mb_type >> 13) & 3;
898 switch (motion_type) {
903 for (i = 0; i < 2; i++) {
918 s->
mv[i][0][0] <<= 1;
919 s->
mv[i][0][1] <<= 1;
926 for (i = 0; i < 2; i++) {
929 for (j = 0; j < 2; j++) {
931 for (k = 0; k < 2; k++) {
935 s->
mv[i][j][k] =
val;
946 for (i = 0; i < 2; i++) {
948 for (j = 0; j < 2; j++) {
953 s->
mv[i][j][0] =
val;
957 s->
last_mv[i][j][1] = val << 1;
958 s->
mv[i][j][1] =
val;
966 for (i = 0; i < 2; i++) {
969 for (k = 0; k < 2; k++) {
974 s->
mv[i][0][k] =
val;
986 for (i = 0; i < 2; i++) {
988 int dmx, dmy, mx, my,
m;
997 s->
last_mv[i][0][1] >> my_shift);
1001 s->
last_mv[i][0][1] = my << my_shift;
1002 s->
last_mv[i][1][1] = my << my_shift;
1004 s->
mv[i][0][0] = mx;
1005 s->
mv[i][0][1] = my;
1006 s->
mv[i][1][0] = mx;
1007 s->
mv[i][1][1] = my;
1016 s->
mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1017 s->
mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1019 s->
mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1020 s->
mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1024 s->
mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1025 s->
mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1036 "00 motion_type at %d %d\n", s->
mb_x, s->
mb_y);
1046 if (mb_block_count > 6) {
1047 cbp <<= mb_block_count - 6;
1053 "invalid cbp %d at %d %d\n", cbp, s->
mb_x, s->
mb_y);
1058 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
1063 for (i = 0; i < 6; i++) {
1071 cbp <<= 12 - mb_block_count;
1073 for (i = 0; i < mb_block_count; i++) {
1074 if (cbp & (1 << 11)) {
1085 for (i = 0; i < 6; i++) {
1093 for (i = 0; i < 6; i++) {
1105 for (i = 0; i < 12; i++)
1123 for (i = 0; i < 64; i++)
1153 if (avctx == avctx_from ||
1154 !ctx_from->mpeg_enc_ctx_allocated ||
1155 !
s1->context_initialized)
1174 uint16_t temp_matrix[64];
1177 memcpy(temp_matrix, matrix, 64 *
sizeof(uint16_t));
1179 for (i = 0; i < 64; i++)
1180 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1184 #if CONFIG_MPEG1_XVMC_HWACCEL
1187 #if CONFIG_MPEG1_VDPAU_HWACCEL
1196 #if CONFIG_MPEG2_XVMC_HWACCEL
1199 #if CONFIG_MPEG2_VDPAU_HWACCEL
1203 #if CONFIG_MPEG2_DXVA2_HWACCEL
1206 #if CONFIG_MPEG2_VAAPI_HWACCEL
1246 avctx->xvmc_acceleration = 2;
1346 s->avctx->sample_aspect_ratio =
1358 memcpy(old_permutation,
s->idsp.idct_permutation, 64 *
sizeof(
uint8_t));
1368 s1->mpeg_enc_ctx_allocated = 1;
1378 int ref, f_code, vbv_delay;
1413 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->
pict_type);
1423 int horiz_size_ext, vert_size_ext;
1433 s->
width |= (horiz_size_ext << 12);
1434 s->
height |= (vert_size_ext << 12);
1436 s->
bit_rate += (bit_rate_ext << 18) * 400;
1452 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
1460 int color_description, w, h;
1464 if (color_description) {
1500 for (i = 0; i < nofco; i++) {
1509 "pde (%"PRId16
",%"PRId16
") (%"PRId16
",%"PRId16
") (%"PRId16
",%"PRId16
")\n",
1516 uint16_t matrix1[64],
int intra)
1520 for (i = 0; i < 64; i++) {
1527 if (intra && i == 0 && v != 8) {
1563 "Missing picture start code, guessing missing values\n");
1676 "hardware accelerator failed to decode first field\n");
1679 for (i = 0; i < 4; i++) {
1695 #define DECODE_SLICE_ERROR -1
1696 #define DECODE_SLICE_OK 0
1763 const uint8_t *buf_end, *buf_start = *buf - 4;
1766 if (buf_end < *buf + buf_size)
1783 "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",
1803 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
1814 int motion_x, motion_y, dir, i;
1816 for (i = 0; i < 2; i++) {
1817 for (dir = 0; dir < 2; dir++) {
1820 motion_x = motion_y = 0;
1823 motion_x = s->
mv[dir][0][0];
1824 motion_y = s->
mv[dir][0][1];
1826 motion_x = s->
mv[dir][i][0];
1827 motion_y = s->
mv[dir][i][1];
1857 s->
mb_y += 1 << field_pic;
1869 if (left >= 32 && !is_d10) {
1905 }
else if (code == 35) {
1922 "skipped MB in I frame at %d %d\n", s->
mb_x, s->
mb_y);
1928 for (i = 0; i < 12; i++)
1937 s->
mv[0][0][0] = s->
mv[0][0][1] = 0;
1974 av_dlog(c,
"ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1997 mb_y += (*buf&0xE0)<<2;
2001 if (mb_y < 0 || mb_y >= s->
end_mb_y)
2021 "hardware accelerator failed to decode picture\n");
2070 if (width == 0 || height == 0) {
2072 "Invalid horizontal or vertical size value.\n");
2098 for (i = 0; i < 64; i++) {
2108 for (i = 0; i < 64; i++) {
2166 for (i = 0; i < 64; i++) {
2196 const uint8_t *p,
int buf_size)
2200 if (buf_size >= 6 &&
2201 p[0] ==
'G' && p[1] ==
'A' && p[2] ==
'9' && p[3] ==
'4' &&
2202 p[4] == 3 && (p[5] & 0x40)) {
2204 int cc_count = p[5] & 0x1f;
2205 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2213 }
else if (buf_size >= 11 &&
2214 p[0] ==
'C' && p[1] ==
'C' && p[2] == 0x01 && p[3] == 0xf8) {
2220 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2228 uint8_t field1 = !!(p[4] & 0x80);
2231 for (i = 0; i < cc_count; i++) {
2232 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2235 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2249 const uint8_t *p,
int buf_size)
2252 const uint8_t *buf_end = p + buf_size;
2257 if (!memcmp(p+i,
"\0TMPGEXS\0", 9)){
2268 if (buf_end - p >= 5 &&
2269 p[0] ==
'D' && p[1] ==
'T' && p[2] ==
'G' && p[3] ==
'1') {
2277 if (buf_end - p < 1)
2281 }
else if (buf_end - p >= 6 &&
2282 p[0] ==
'J' && p[1] ==
'P' && p[2] ==
'3' && p[3] ==
'D' &&
2285 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2287 if (S3D_video_format_type == 0x03 ||
2288 S3D_video_format_type == 0x04 ||
2289 S3D_video_format_type == 0x08 ||
2290 S3D_video_format_type == 0x23) {
2295 switch (S3D_video_format_type) {
2337 "GOP (%s) closed_gop=%d broken_link=%d\n",
2343 int *got_output,
const uint8_t *
buf,
int buf_size)
2348 const uint8_t *buf_end = buf + buf_size;
2349 int ret, input_size;
2350 int last_code = 0, skip_frame = 0;
2351 int picture_start_code_seen = 0;
2357 if (start_code > 0x1ff) {
2372 if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
2393 input_size = buf_end - buf_ptr;
2397 start_code, buf_ptr - buf, input_size);
2400 switch (start_code) {
2402 if (last_code == 0) {
2408 "ignoring SEQ_START_CODE after %X\n", last_code);
2421 picture_start_code_seen = 1;
2448 "mpeg_decode_postinit() failure\n");
2459 "ignoring pic after %X\n", last_code);
2469 if (last_code == 0) {
2473 "ignoring seq ext after %X\n", last_code);
2492 "ignoring pic cod ext after %X\n", last_code);
2503 if (last_code == 0) {
2509 "ignoring GOP_START_CODE after %X\n", last_code);
2520 "interlaced frame in progressive sequence, ignoring\n");
2526 "picture_structure %d invalid, ignoring\n",
2549 mb_y += (*buf_ptr&0xE0)<<2;
2555 if (buf_end - buf_ptr < 2) {
2562 "slice below image (%d >= %d)\n", mb_y, s2->
mb_height);
2599 if (mb_y < avctx->skip_top ||
2619 "current_picture not initialized\n");
2635 if (threshold <= mb_y) {
2678 int buf_size = avpkt->
size;
2702 (
const uint8_t **) &buf, &buf_size) < 0)
2728 ret =
decode_chunks(avctx, picture, got_output, buf, buf_size);
2729 if (ret<0 || *got_output)
2767 .
name =
"mpeg1video",
2784 .
name =
"mpeg2video",
2802 .
name =
"mpegvideo",
2816 #if CONFIG_MPEG_XVMC_DECODER
2824 av_dlog(avctx,
"mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2829 avctx->xvmc_acceleration = 2;
2834 AVCodec ff_mpeg_xvmc_decoder = {
2835 .
name =
"mpegvideo_xvmc",
2838 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2840 .
init = mpeg_mc_decode_init,
2851 #if CONFIG_MPEG_VDPAU_DECODER
2852 AVCodec ff_mpeg_vdpau_decoder = {
2853 .
name =
"mpegvideo_vdpau",
2867 #if CONFIG_MPEG1_VDPAU_DECODER
2868 AVCodec ff_mpeg1_vdpau_decoder = {
2869 .
name =
"mpeg1video_vdpau",