diff options
Diffstat (limited to 'libavcodec/mpeg12.c')
-rw-r--r-- | libavcodec/mpeg12.c | 226 |
1 files changed, 163 insertions, 63 deletions
diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c index c40649d..697eebb 100644 --- a/libavcodec/mpeg12.c +++ b/libavcodec/mpeg12.c @@ -3,20 +3,20 @@ * Copyright (c) 2000, 2001 Fabrice Bellard * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -25,11 +25,15 @@ * MPEG-1/2 decoder */ +#define UNCHECKED_BITSTREAM_READER 1 + //#define DEBUG #include "internal.h" #include "avcodec.h" #include "dsputil.h" #include "mpegvideo.h" +#include "libavutil/avassert.h" +#include "libavutil/timecode.h" #include "mpeg12.h" #include "mpeg12data.h" @@ -956,6 +960,7 @@ static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64]) } } } else { + av_assert0(!s->progressive_sequence); mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED; for (i = 0; i < 2; i++) { if (USES_LIST(mb_type, i)) { @@ -972,6 +977,10 @@ static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64]) } break; case MT_DMV: + if(s->progressive_sequence){ + av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n"); + return -1; + } s->mv_type = MV_TYPE_DMV; for (i = 0; i < 2; i++) { if (USES_LIST(mb_type, i)) { @@ -1171,31 +1180,61 @@ static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm, } } -static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = { +static const enum PixelFormat mpeg1_hwaccel_pixfmt_list_420[] = { +#if CONFIG_MPEG_XVMC_DECODER PIX_FMT_XVMC_MPEG2_IDCT, PIX_FMT_XVMC_MPEG2_MC, - PIX_FMT_NONE }; +#endif +#if CONFIG_MPEG1_VDPAU_HWACCEL + PIX_FMT_VDPAU_MPEG1, +#endif + PIX_FMT_YUV420P, + PIX_FMT_NONE +}; + +static const enum PixelFormat mpeg2_hwaccel_pixfmt_list_420[] = { +#if CONFIG_MPEG_XVMC_DECODER + PIX_FMT_XVMC_MPEG2_IDCT, + PIX_FMT_XVMC_MPEG2_MC, +#endif +#if CONFIG_MPEG2_VDPAU_HWACCEL + PIX_FMT_VDPAU_MPEG2, +#endif +#if CONFIG_MPEG2_DXVA2_HWACCEL + PIX_FMT_DXVA2_VLD, +#endif +#if CONFIG_MPEG2_VAAPI_HWACCEL + PIX_FMT_VAAPI_VLD, +#endif + PIX_FMT_YUV420P, + PIX_FMT_NONE +}; + +static inline int uses_vdpau(AVCodecContext *avctx) { + return avctx->pix_fmt == PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == PIX_FMT_VDPAU_MPEG2; +} static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx) { Mpeg1Context *s1 = avctx->priv_data; MpegEncContext *s = &s1->mpeg_enc_ctx; - if (avctx->xvmc_acceleration) - return avctx->get_format(avctx, pixfmt_xvmc_mpg2_420); - else if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) { - if (avctx->codec_id == CODEC_ID_MPEG1VIDEO) - return PIX_FMT_VDPAU_MPEG1; - else - return PIX_FMT_VDPAU_MPEG2; - } else { - if (s->chroma_format < 2) - return avctx->get_format(avctx, ff_hwaccel_pixfmt_list_420); - else if (s->chroma_format == 2) - return PIX_FMT_YUV422P; - else - return PIX_FMT_YUV444P; - } + if(s->chroma_format < 2) { + enum PixelFormat res; + res = avctx->get_format(avctx, + avctx->codec_id == CODEC_ID_MPEG1VIDEO ? + mpeg1_hwaccel_pixfmt_list_420 : + mpeg2_hwaccel_pixfmt_list_420); + if (res != PIX_FMT_XVMC_MPEG2_IDCT && res != PIX_FMT_XVMC_MPEG2_MC) { + avctx->xvmc_acceleration = 0; + } else if (!avctx->xvmc_acceleration) { + avctx->xvmc_acceleration = 2; + } + return res; + } else if(s->chroma_format == 2) + return PIX_FMT_YUV422P; + else + return PIX_FMT_YUV444P; } /* Call this function when we know all parameters. @@ -1288,8 +1327,7 @@ static int mpeg_decode_postinit(AVCodecContext *avctx) avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt); // until then pix_fmt may be changed right after codec init if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || - avctx->hwaccel || - s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) + avctx->hwaccel ) if (avctx->idct_algo == FF_IDCT_AUTO) avctx->idct_algo = FF_IDCT_SIMPLE; @@ -1328,16 +1366,18 @@ static int mpeg1_decode_picture(AVCodecContext *avctx, if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) { s->full_pel[0] = get_bits1(&s->gb); f_code = get_bits(&s->gb, 3); - if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM)) + if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) return -1; + f_code += !f_code; s->mpeg_f_code[0][0] = f_code; s->mpeg_f_code[0][1] = f_code; } if (s->pict_type == AV_PICTURE_TYPE_B) { s->full_pel[1] = get_bits1(&s->gb); f_code = get_bits(&s->gb, 3); - if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM)) + if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) return -1; + f_code += !f_code; s->mpeg_f_code[1][0] = f_code; s->mpeg_f_code[1][1] = f_code; } @@ -1457,7 +1497,7 @@ static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1 return -1; } if (intra && i == 0 && v != 8) { - av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n"); + av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v); v = 8; // needed by pink.mpg / issue1046 } matrix0[j] = v; @@ -1498,6 +1538,11 @@ static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1) s->current_picture.f.pict_type = s->pict_type; s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I; } + s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0]; + s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1]; + s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0]; + s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1]; + s->intra_dc_precision = get_bits(&s->gb, 2); s->picture_structure = get_bits(&s->gb, 2); s->top_field_first = get_bits1(&s->gb); @@ -1631,6 +1676,7 @@ static int mpeg_decode_slice(MpegEncContext *s, int mb_y, const uint8_t **buf, int buf_size) { AVCodecContext *avctx = s->avctx; + const int lowres = s->avctx->lowres; const int field_pic = s->picture_structure != PICT_FRAME; s->resync_mb_x = @@ -1639,6 +1685,8 @@ static int mpeg_decode_slice(MpegEncContext *s, int mb_y, assert(mb_y < s->mb_height); init_get_bits(&s->gb, *buf, buf_size * 8); + if(s->codec_id != CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16) + skip_bits(&s->gb, 3); ff_mpeg1_clean_buffers(s); s->interlaced_dct = 0; @@ -1751,14 +1799,14 @@ static int mpeg_decode_slice(MpegEncContext *s, int mb_y, } } - s->dest[0] += 16; - s->dest[1] += 16 >> s->chroma_x_shift; - s->dest[2] += 16 >> s->chroma_x_shift; + s->dest[0] += 16 >> lowres; + s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift; + s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift; ff_MPV_decode_mb(s, s->block); if (++s->mb_x >= s->mb_width) { - const int mb_size = 16; + const int mb_size = 16 >> s->avctx->lowres; ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size); ff_MPV_report_decode_progress(s); @@ -1773,7 +1821,7 @@ static int mpeg_decode_slice(MpegEncContext *s, int mb_y, && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/; if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) - || ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) { + || ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) { av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23))); return -1; } else @@ -1878,7 +1926,10 @@ static int slice_decode_thread(AVCodecContext *c, void *arg) start_code = -1; buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code); - mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic; + mb_y= start_code - SLICE_MIN_START_CODE; + if(s->codec_id != CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16) + mb_y += (*buf&0xE0)<<2; + mb_y <<= field_pic; if (s->picture_structure == PICT_BOTTOM_FIELD) mb_y++; if (mb_y < 0 || mb_y >= s->end_mb_y) @@ -1907,7 +1958,7 @@ static int slice_end(AVCodecContext *avctx, AVFrame *pict) ff_xvmc_field_end(s); /* end of slice reached */ - if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field) { + if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field && !s->first_slice) { /* end of image */ s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_MPEG2; @@ -1953,7 +2004,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx, s->aspect_ratio_info = get_bits(&s->gb, 4); if (s->aspect_ratio_info == 0) { av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n"); - if (avctx->err_recognition & AV_EF_BITSTREAM) + if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT)) return -1; } s->frame_rate_index = get_bits(&s->gb, 4); @@ -1999,6 +2050,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx, s->progressive_sequence = 1; s->progressive_frame = 1; s->picture_structure = PICT_FRAME; + s->first_field = 0; s->frame_pred_frame_dct = 1; s->chroma_format = 1; s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO; @@ -2033,15 +2085,12 @@ static int vcr2_init_sequence(AVCodecContext *avctx) avctx->pix_fmt = mpeg_get_pixelformat(avctx); avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt); - if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel || - s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) + if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ) if (avctx->idct_algo == FF_IDCT_AUTO) avctx->idct_algo = FF_IDCT_SIMPLE; if (ff_MPV_common_init(s) < 0) return -1; - exchange_uv(s); // common init reset pblocks, so we swap them here - s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB s1->mpeg_enc_ctx_allocated = 1; for (i = 0; i < 64; i++) { @@ -2058,9 +2107,16 @@ static int vcr2_init_sequence(AVCodecContext *avctx) s->progressive_sequence = 1; s->progressive_frame = 1; s->picture_structure = PICT_FRAME; + s->first_field = 0; s->frame_pred_frame_dct = 1; s->chroma_format = 1; - s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO; + if (s->codec_tag == AV_RL32("BW10")) { + s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO; + } else { + exchange_uv(s); // common init reset pblocks, so we swap them here + s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB + s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO; + } s1->save_width = s->width; s1->save_height = s->height; s1->save_progressive_seq = s->progressive_sequence; @@ -2071,8 +2127,22 @@ static int vcr2_init_sequence(AVCodecContext *avctx) static void mpeg_decode_user_data(AVCodecContext *avctx, const uint8_t *p, int buf_size) { + Mpeg1Context *s = avctx->priv_data; const uint8_t *buf_end = p + buf_size; + if(buf_size > 29){ + int i; + for(i=0; i<20; i++) + if(!memcmp(p+i, "\0TMPGEXS\0", 9)){ + s->tmpgexs= 1; + } + +/* for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){ + av_log(0,0, "%c", p[i]); + } + av_log(0,0, "\n");*/ + } + /* we parse the DTG active format information */ if (buf_end - p >= 5 && p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') { @@ -2095,31 +2165,26 @@ static void mpeg_decode_gop(AVCodecContext *avctx, { Mpeg1Context *s1 = avctx->priv_data; MpegEncContext *s = &s1->mpeg_enc_ctx; - - int time_code_hours, time_code_minutes; - int time_code_seconds, time_code_pictures; int broken_link; + int64_t tc; init_get_bits(&s->gb, buf, buf_size*8); - skip_bits1(&s->gb); /* drop_frame_flag */ - - time_code_hours = get_bits(&s->gb, 5); - time_code_minutes = get_bits(&s->gb, 6); - skip_bits1(&s->gb); // marker bit - time_code_seconds = get_bits(&s->gb, 6); - time_code_pictures = get_bits(&s->gb, 6); + tc = avctx->timecode_frame_start = get_bits(&s->gb, 25); - s1->closed_gop = get_bits1(&s->gb); + s->closed_gop = get_bits1(&s->gb); /*broken_link indicate that after editing the reference frames of the first B-Frames after GOP I-Frame are missing (open gop)*/ broken_link = get_bits1(&s->gb); - if (s->avctx->debug & FF_DEBUG_PICT_INFO) - av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n", - time_code_hours, time_code_minutes, time_code_seconds, - time_code_pictures, s1->closed_gop, broken_link); + if (s->avctx->debug & FF_DEBUG_PICT_INFO) { + char tcbuf[AV_TIMECODE_STR_SIZE]; + av_timecode_make_mpeg_tc_string(tcbuf, tc); + av_log(s->avctx, AV_LOG_DEBUG, + "GOP (%s) closed_gop=%d broken_link=%d\n", + tcbuf, s->closed_gop, broken_link); + } } /** * Find the end of the current frame in the bitstream. @@ -2219,13 +2284,21 @@ static int mpeg_decode_frame(AVCodecContext *avctx, return buf_size; } - if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2")) + s2->codec_tag = avpriv_toupper4(avctx->codec_tag); + if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2") + || s2->codec_tag == AV_RL32("BW10") + )) vcr2_init_sequence(avctx); s->slice_count = 0; - if (avctx->extradata && !avctx->frame_number) { + if (avctx->extradata && !s->parsed_extra) { int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size); + if(*data_size) { + av_log(avctx, AV_LOG_ERROR, "picture in extradata\n"); + *data_size = 0; + } + s->parsed_extra = 1; if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) return ret; } @@ -2252,15 +2325,17 @@ static int decode_chunks(AVCodecContext *avctx, if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) { if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) { int i; + av_assert0(avctx->thread_count > 1); avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*)); for (i = 0; i < s->slice_count; i++) s2->error_count += s2->thread_context[i]->error_count; } - if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) + if (CONFIG_VDPAU && uses_vdpau(avctx)) ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count); + if (slice_end(avctx, picture)) { if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice *data_size = sizeof(AVPicture); @@ -2281,7 +2356,8 @@ static int decode_chunks(AVCodecContext *avctx, case SEQ_START_CODE: if (last_code == 0) { mpeg1_decode_sequence(avctx, buf_ptr, input_size); - s->sync=1; + if(buf != avctx->extradata) + s->sync=1; } else { av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code); if (avctx->err_recognition & AV_EF_EXPLODE) @@ -2290,6 +2366,10 @@ static int decode_chunks(AVCodecContext *avctx, break; case PICTURE_START_CODE: + if(s->tmpgexs){ + s2->intra_dc_precision= 3; + s2->intra_matrix[0]= 1; + } if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) { int i; @@ -2369,9 +2449,12 @@ static int decode_chunks(AVCodecContext *avctx, if (start_code >= SLICE_MIN_START_CODE && start_code <= SLICE_MAX_START_CODE && last_code != 0) { const int field_pic = s2->picture_structure != PICT_FRAME; - int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic; + int mb_y = start_code - SLICE_MIN_START_CODE; last_code = SLICE_MIN_START_CODE; + if(s2->codec_id != CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16) + mb_y += (*buf_ptr&0xE0)<<2; + mb_y <<= field_pic; if (s2->picture_structure == PICT_BOTTOM_FIELD) mb_y++; @@ -2383,11 +2466,11 @@ static int decode_chunks(AVCodecContext *avctx, if (s2->last_picture_ptr == NULL) { /* Skip B-frames if we do not have reference frames and gop is not closed */ if (s2->pict_type == AV_PICTURE_TYPE_B) { - if (!s->closed_gop) + if (!s2->closed_gop) break; } } - if (s2->pict_type == AV_PICTURE_TYPE_I) + if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL)) s->sync=1; if (s2->next_picture_ptr == NULL) { /* Skip P-frames if we do not have a reference frame or we have an invalid header. */ @@ -2423,7 +2506,7 @@ static int decode_chunks(AVCodecContext *avctx, return AVERROR_INVALIDDATA; } - if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) { + if (uses_vdpau(avctx)) { s->slice_count++; break; } @@ -2432,6 +2515,7 @@ static int decode_chunks(AVCodecContext *avctx, int threshold = (s2->mb_height * s->slice_count + s2->slice_context_count / 2) / s2->slice_context_count; + av_assert0(avctx->thread_count > 1); if (threshold <= mb_y) { MpegEncContext *thread_context = s2->thread_context[s->slice_count]; @@ -2469,7 +2553,6 @@ static void flush(AVCodecContext *avctx) Mpeg1Context *s = avctx->priv_data; s->sync=0; - s->closed_gop = 0; ff_mpeg_flush(avctx); } @@ -2508,6 +2591,7 @@ AVCodec ff_mpeg1video_decoder = { CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS, .flush = flush, + .max_lowres = 3, .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"), .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context) }; @@ -2524,10 +2608,26 @@ AVCodec ff_mpeg2video_decoder = { CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS, .flush = flush, + .max_lowres = 3, .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"), .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles), }; +//legacy decoder +AVCodec ff_mpegvideo_decoder = { + .name = "mpegvideo", + .type = AVMEDIA_TYPE_VIDEO, + .id = CODEC_ID_MPEG2VIDEO, + .priv_data_size = sizeof(Mpeg1Context), + .init = mpeg_decode_init, + .close = mpeg_decode_end, + .decode = mpeg_decode_frame, + .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS, + .flush = flush, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"), +}; + #if CONFIG_MPEG_XVMC_DECODER static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx) { |