diff options
Diffstat (limited to 'libavcodec/mpeg12enc.c')
-rw-r--r-- | libavcodec/mpeg12enc.c | 90 |
1 files changed, 57 insertions, 33 deletions
diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c index ceb31e0270..c6154170e8 100644 --- a/libavcodec/mpeg12enc.c +++ b/libavcodec/mpeg12enc.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 */ @@ -33,9 +33,10 @@ #include "mpeg12.h" #include "mpeg12data.h" #include "bytestream.h" - #include "libavutil/log.h" #include "libavutil/opt.h" +#include "libavutil/avassert.h" +#include "libavutil/timecode.h" static const uint8_t inv_non_linear_qscale[13] = { 0, 2, 4, 6, 8, @@ -107,21 +108,34 @@ static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){ static int find_frame_rate_index(MpegEncContext *s){ int i; - int64_t dmin= INT64_MAX; - int64_t d; + AVRational bestq= (AVRational){0, 0}; + AVRational ext; + AVRational target = av_inv_q(s->avctx->time_base); for(i=1;i<14;i++) { - int64_t n0 = 1001LL / ff_mpeg12_frame_rate_tab[i].den * ff_mpeg12_frame_rate_tab[i].num * s->avctx->time_base.num; - int64_t n1= 1001LL*s->avctx->time_base.den; if(s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && i>=9) break; - d = FFABS(n0 - n1); - if(d < dmin){ - dmin=d; - s->frame_rate_index= i; + for (ext.num=1; ext.num <= 4; ext.num++) { + for (ext.den=1; ext.den <= 32; ext.den++) { + AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]); + + if(s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1)) + continue; + if(av_gcd(ext.den, ext.num) != 1) + continue; + + if( bestq.num==0 + || av_nearer_q(target, bestq, q) < 0 + || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0){ + bestq = q; + s->frame_rate_index= i; + s->mpeg2_frame_rate_ext.num = ext.num; + s->mpeg2_frame_rate_ext.den = ext.den; + } + } } } - if(dmin) + if(av_cmp_q(target, bestq)) return -1; else return 0; @@ -166,11 +180,24 @@ static av_cold int encode_init(AVCodecContext *avctx) } } + s->drop_frame_timecode = s->drop_frame_timecode || !!(avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE); + if (s->drop_frame_timecode) + s->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME; if (s->drop_frame_timecode && s->frame_rate_index != 4) { av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n"); return -1; } + if (s->tc_opt_str) { + AVRational rate = ff_mpeg12_frame_rate_tab[s->frame_rate_index]; + int ret = av_timecode_init_from_string(&s->tc, rate, s->tc_opt_str, s); + if (ret < 0) + return ret; + s->drop_frame_timecode = !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME); + s->avctx->timecode_frame_start = s->tc.start; + } else { + s->avctx->timecode_frame_start = 0; // default is -1 + } return 0; } @@ -272,8 +299,8 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s) put_bits(&s->pb, 1, 1); //marker put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext put_bits(&s->pb, 1, s->low_delay); - put_bits(&s->pb, 2, 0); // frame_rate_ext_n - put_bits(&s->pb, 5, 0); // frame_rate_ext_d + put_bits(&s->pb, 2, s->mpeg2_frame_rate_ext.num-1); // frame_rate_ext_n + put_bits(&s->pb, 5, s->mpeg2_frame_rate_ext.den-1); // frame_rate_ext_d } put_header(s, GOP_START_CODE); @@ -284,13 +311,9 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s) time_code = s->current_picture_ptr->f.coded_picture_number + s->avctx->timecode_frame_start; s->gop_picture_number = s->current_picture_ptr->f.coded_picture_number; - if (s->drop_frame_timecode) { - /* only works for NTSC 29.97 */ - int d = time_code / 17982; - int m = time_code % 17982; - //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */ - time_code += 18 * d + 2 * ((m - 2) / 1798); - } + av_assert0(s->drop_frame_timecode == !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME)); + if (s->drop_frame_timecode) + time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps); put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24)); put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60)); put_bits(&s->pb, 1, 1); @@ -313,7 +336,7 @@ static inline void encode_mb_skip_run(MpegEncContext *s, int run){ static av_always_inline void put_qscale(MpegEncContext *s) { if(s->q_scale_type){ - assert(s->qscale>=1 && s->qscale <=12); + av_assert2(s->qscale>=1 && s->qscale <=12); put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]); }else{ put_bits(&s->pb, 5, s->qscale); @@ -321,7 +344,7 @@ static av_always_inline void put_qscale(MpegEncContext *s) } void ff_mpeg1_encode_slice_header(MpegEncContext *s){ - if (s->height > 2800) { + if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) { put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127)); put_bits(&s->pb, 3, s->mb_y >> 7); /* slice_vertical_position_extension */ } else { @@ -385,7 +408,7 @@ void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) } put_bits(&s->pb, 2, s->intra_dc_precision); - assert(s->picture_structure == PICT_FRAME); + av_assert0(s->picture_structure == PICT_FRAME); put_bits(&s->pb, 2, s->picture_structure); if (s->progressive_sequence) { put_bits(&s->pb, 1, 0); /* no repeat */ @@ -464,7 +487,7 @@ static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s, } } else { if(first_mb){ - assert(s->mb_skip_run == 0); + av_assert0(s->mb_skip_run == 0); encode_mb_skip_run(s, s->mb_x); }else{ encode_mb_skip_run(s, s->mb_skip_run); @@ -527,7 +550,7 @@ static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s, s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x; s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y; }else{ - assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD); + av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD); if (cbp) { if(s->dquant){ @@ -594,8 +617,8 @@ static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s, s->b_count++; } }else{ - assert(s->mv_type == MV_TYPE_FIELD); - assert(!s->frame_pred_frame_dct); + av_assert2(s->mv_type == MV_TYPE_FIELD); + av_assert2(!s->frame_pred_frame_dct); if (cbp){ // With coded bloc pattern if (s->dquant) { if(s->mv_dir == MV_DIR_FORWARD) @@ -690,7 +713,7 @@ static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code) sign = 1; } - assert(code > 0 && code <= 16); + av_assert2(code > 0 && code <= 16); put_bits(&s->pb, ff_mpeg12_mbMotionVectorTable[code][1], @@ -921,6 +944,7 @@ static void mpeg1_encode_block(MpegEncContext *s, #define OFFSET(x) offsetof(MpegEncContext, x) #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM #define COMMON_OPTS\ + { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format", OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, VE },\ { "intra_vlc", "Use MPEG-2 intra VLC table.", OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },\ { "drop_frame_timecode", "Timecode is in drop frame format.", OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE}, \ { "scan_offset", "Reserve space for SVCD scan offset user data.", OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, @@ -961,7 +985,7 @@ AVCodec ff_mpeg1video_encoder = { .supported_framerates = ff_mpeg12_frame_rate_tab + 1, .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE }, - .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS, + .capabilities = CODEC_CAP_DELAY, .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"), .priv_class = &mpeg1_class, }; @@ -974,7 +998,7 @@ AVCodec ff_mpeg2video_encoder = { .init = encode_init, .encode2 = ff_MPV_encode_picture, .close = ff_MPV_encode_end, - .supported_framerates = ff_mpeg12_frame_rate_tab + 1, + .supported_framerates = ff_mpeg2_frame_rate_tab, .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE }, |