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;
761 if ((s->
mv[0][0][0] | s->
mv[0][0][1] | s->
mv[1][0][0] | s->
mv[1][0][1]) == 0)
774 "invalid mb type in I Frame at %d %d\n",
787 "invalid mb type in P Frame at %d %d\n", s->
mb_x, s->
mb_y);
796 "invalid mb type in B Frame at %d %d\n", s->
mb_x, s->
mb_y);
840 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
845 for (i = 0; i < 6; i++)
848 for (i = 0; i < mb_block_count; i++)
853 for (i = 0; i < 6; i++)
898 s->
mv_dir = (mb_type >> 13) & 3;
900 switch (motion_type) {
905 for (i = 0; i < 2; i++) {
920 s->
mv[i][0][0] <<= 1;
921 s->
mv[i][0][1] <<= 1;
928 for (i = 0; i < 2; i++) {
931 for (j = 0; j < 2; j++) {
933 for (k = 0; k < 2; k++) {
937 s->
mv[i][j][k] =
val;
948 for (i = 0; i < 2; i++) {
950 for (j = 0; j < 2; j++) {
955 s->
mv[i][j][0] =
val;
959 s->
last_mv[i][j][1] = val << 1;
960 s->
mv[i][j][1] =
val;
968 for (i = 0; i < 2; i++) {
971 for (k = 0; k < 2; k++) {
976 s->
mv[i][0][k] =
val;
988 for (i = 0; i < 2; i++) {
990 int dmx, dmy, mx, my,
m;
999 s->
last_mv[i][0][1] >> my_shift);
1003 s->
last_mv[i][0][1] = my << my_shift;
1004 s->
last_mv[i][1][1] = my << my_shift;
1006 s->
mv[i][0][0] = mx;
1007 s->
mv[i][0][1] = my;
1008 s->
mv[i][1][0] = mx;
1009 s->
mv[i][1][1] = my;
1018 s->
mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1019 s->
mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1021 s->
mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1022 s->
mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1026 s->
mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1027 s->
mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1038 "00 motion_type at %d %d\n", s->
mb_x, s->
mb_y);
1048 if (mb_block_count > 6) {
1049 cbp <<= mb_block_count - 6;
1055 "invalid cbp %d at %d %d\n", cbp, s->
mb_x, s->
mb_y);
1060 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
1065 for (i = 0; i < 6; i++) {
1073 cbp <<= 12 - mb_block_count;
1075 for (i = 0; i < mb_block_count; i++) {
1076 if (cbp & (1 << 11)) {
1087 for (i = 0; i < 6; i++) {
1095 for (i = 0; i < 6; i++) {
1107 for (i = 0; i < 12; i++)
1152 if (avctx == avctx_from ||
1153 !ctx_from->mpeg_enc_ctx_allocated ||
1154 !
s1->context_initialized)
1173 uint16_t temp_matrix[64];
1176 memcpy(temp_matrix, matrix, 64 *
sizeof(uint16_t));
1178 for (i = 0; i < 64; i++)
1179 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1183 #if CONFIG_MPEG1_XVMC_HWACCEL
1186 #if CONFIG_MPEG1_VDPAU_HWACCEL
1195 #if CONFIG_MPEG2_XVMC_HWACCEL
1198 #if CONFIG_MPEG2_VDPAU_HWACCEL
1202 #if CONFIG_MPEG2_DXVA2_HWACCEL
1205 #if CONFIG_MPEG2_VAAPI_HWACCEL
1245 avctx->xvmc_acceleration = 2;
1345 s->avctx->sample_aspect_ratio =
1357 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");
1688 "hardware accelerator failed to decode first field\n");
1691 for (i = 0; i < 4; i++) {
1707 #define DECODE_SLICE_ERROR -1
1708 #define DECODE_SLICE_OK 0
1775 const uint8_t *buf_end, *buf_start = *buf - 4;
1778 if (buf_end < *buf + buf_size)
1795 "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",
1815 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->
pack_pblocks)
1826 int motion_x, motion_y, dir, i;
1828 for (i = 0; i < 2; i++) {
1829 for (dir = 0; dir < 2; dir++) {
1832 motion_x = motion_y = 0;
1835 motion_x = s->
mv[dir][0][0];
1836 motion_y = s->
mv[dir][0][1];
1838 motion_x = s->
mv[dir][i][0];
1839 motion_y = s->
mv[dir][i][1];
1869 s->
mb_y += 1 << field_pic;
1881 if (left >= 32 && !is_d10) {
1929 }
else if (code == 35) {
1946 "skipped MB in I frame at %d %d\n", s->
mb_x, s->
mb_y);
1952 for (i = 0; i < 12; i++)
1961 s->
mv[0][0][0] = s->
mv[0][0][1] = 0;
2000 av_dlog(c,
"ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2023 mb_y += (*buf&0xE0)<<2;
2027 if (mb_y < 0 || mb_y >= s->
end_mb_y)
2047 "hardware accelerator failed to decode picture\n");
2096 if (width == 0 || height == 0) {
2098 "Invalid horizontal or vertical size value.\n");
2124 for (i = 0; i < 64; i++) {
2134 for (i = 0; i < 64; i++) {
2193 for (i = 0; i < 64; i++) {
2223 const uint8_t *p,
int buf_size)
2227 if (buf_size >= 6 &&
2228 p[0] ==
'G' && p[1] ==
'A' && p[2] ==
'9' && p[3] ==
'4' &&
2229 p[4] == 3 && (p[5] & 0x40)) {
2231 int cc_count = p[5] & 0x1f;
2232 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2240 }
else if (buf_size >= 11 &&
2241 p[0] ==
'C' && p[1] ==
'C' && p[2] == 0x01 && p[3] == 0xf8) {
2247 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2255 uint8_t field1 = !!(p[4] & 0x80);
2258 for (i = 0; i < cc_count; i++) {
2259 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2262 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2276 const uint8_t *p,
int buf_size)
2279 const uint8_t *buf_end = p + buf_size;
2285 if (!memcmp(p+i,
"\0TMPGEXS\0", 9)){
2296 if (buf_end - p >= 5 &&
2297 p[0] ==
'D' && p[1] ==
'T' && p[2] ==
'G' && p[3] ==
'1') {
2305 if (buf_end - p < 1)
2313 s1->
afd = p[0] & 0x0f;
2315 }
else if (buf_end - p >= 6 &&
2316 p[0] ==
'J' && p[1] ==
'P' && p[2] ==
'3' && p[3] ==
'D' &&
2319 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2321 if (S3D_video_format_type == 0x03 ||
2322 S3D_video_format_type == 0x04 ||
2323 S3D_video_format_type == 0x08 ||
2324 S3D_video_format_type == 0x23) {
2328 switch (S3D_video_format_type) {
2370 "GOP (%s) closed_gop=%d broken_link=%d\n",
2376 int *got_output,
const uint8_t *
buf,
int buf_size)
2381 const uint8_t *buf_end = buf + buf_size;
2382 int ret, input_size;
2383 int last_code = 0, skip_frame = 0;
2384 int picture_start_code_seen = 0;
2390 if (start_code > 0x1ff) {
2405 if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
2426 input_size = buf_end - buf_ptr;
2430 start_code, buf_ptr - buf, input_size);
2433 switch (start_code) {
2435 if (last_code == 0) {
2441 "ignoring SEQ_START_CODE after %X\n", last_code);
2454 picture_start_code_seen = 1;
2481 "mpeg_decode_postinit() failure\n");
2492 "ignoring pic after %X\n", last_code);
2502 if (last_code == 0) {
2506 "ignoring seq ext after %X\n", last_code);
2525 "ignoring pic cod ext after %X\n", last_code);
2536 if (last_code == 0) {
2542 "ignoring GOP_START_CODE after %X\n", last_code);
2553 "interlaced frame in progressive sequence, ignoring\n");
2559 "picture_structure %d invalid, ignoring\n",
2582 mb_y += (*buf_ptr&0xE0)<<2;
2588 if (buf_end - buf_ptr < 2) {
2595 "slice below image (%d >= %d)\n", mb_y, s2->
mb_height);
2632 if (mb_y < avctx->skip_top ||
2652 "current_picture not initialized\n");
2668 if (threshold <= mb_y) {
2711 int buf_size = avpkt->
size;
2735 (
const uint8_t **) &buf, &buf_size) < 0)
2761 ret =
decode_chunks(avctx, picture, got_output, buf, buf_size);
2762 if (ret<0 || *got_output)
2800 .
name =
"mpeg1video",
2817 .
name =
"mpeg2video",
2835 .
name =
"mpegvideo",
2849 #if CONFIG_MPEG_XVMC_DECODER
2857 av_dlog(avctx,
"mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2862 avctx->xvmc_acceleration = 2;
2867 AVCodec ff_mpeg_xvmc_decoder = {
2868 .
name =
"mpegvideo_xvmc",
2871 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2873 .
init = mpeg_mc_decode_init,
2884 #if CONFIG_MPEG_VDPAU_DECODER
2885 AVCodec ff_mpeg_vdpau_decoder = {
2886 .
name =
"mpegvideo_vdpau",
2900 #if CONFIG_MPEG1_VDPAU_DECODER
2901 AVCodec ff_mpeg1_vdpau_decoder = {
2902 .
name =
"mpeg1video_vdpau",