From 53b90bb25edfd608cb6ae9201ca42052bb54b62f Mon Sep 17 00:00:00 2001 From: Reinhard Tartler Date: Wed, 24 Feb 2010 22:40:10 +0000 Subject: backport libx264.c from trunk now compiles with x264 API versions 65 up to 85 patch prepared by darkshikari Originally committed as revision 22042 to svn://svn.ffmpeg.org/ffmpeg/branches/0.5 --- configure | 2 +- ffpresets/libx264-baseline.ffpreset | 3 +- ffpresets/libx264-default.ffpreset | 8 ++- ffpresets/libx264-fastfirstpass.ffpreset | 7 +- ffpresets/libx264-hq.ffpreset | 5 +- ffpresets/libx264-ipod320.ffpreset | 3 +- ffpresets/libx264-ipod640.ffpreset | 3 +- ffpresets/libx264-lossless_fast.ffpreset | 3 +- ffpresets/libx264-lossless_max.ffpreset | 3 +- ffpresets/libx264-lossless_medium.ffpreset | 3 +- ffpresets/libx264-lossless_slow.ffpreset | 3 +- ffpresets/libx264-lossless_slower.ffpreset | 3 +- ffpresets/libx264-lossless_ultrafast.ffpreset | 2 +- ffpresets/libx264-main.ffpreset | 2 +- ffpresets/libx264-max.ffpreset | 9 +-- ffpresets/libx264-normal.ffpreset | 5 +- ffpresets/libx264-slowfirstpass.ffpreset | 5 +- libavcodec/avcodec.h | 11 +++ libavcodec/libx264.c | 100 ++++++++++++++++++++++---- libavcodec/options.c | 2 + 20 files changed, 144 insertions(+), 38 deletions(-) diff --git a/configure b/configure index 3e74ab8311..e96db9e352 100755 --- a/configure +++ b/configure @@ -2017,7 +2017,7 @@ enabled libschroedinger && add_cflags $(pkg-config --cflags schroedinger-1.0) && enabled libspeex && require libspeex speex/speex.h speex_decoder_init -lspeex enabled libtheora && require libtheora theora/theora.h theora_info_init -ltheora -logg enabled libvorbis && require libvorbis vorbis/vorbisenc.h vorbis_info_init -lvorbisenc -lvorbis -logg -enabled libx264 && require libx264 x264.h x264_encoder_open -lx264 -lm && +enabled libx264 && require libx264 x264.h x264_encoder_encode -lx264 -lm && { check_cpp_condition x264.h "X264_BUILD >= 65" || die "ERROR: libx264 version must be >= 0.65."; } enabled libxvid && require libxvid xvid.h xvid_global -lxvidcore diff --git a/ffpresets/libx264-baseline.ffpreset b/ffpresets/libx264-baseline.ffpreset index 47664189b2..a23b746ded 100644 --- a/ffpresets/libx264-baseline.ffpreset +++ b/ffpresets/libx264-baseline.ffpreset @@ -1,3 +1,4 @@ coder=0 bf=0 -flags2=-wpred-dct8x8 +flags2=-wpred-dct8x8+mbtree +wpredp=0 diff --git a/ffpresets/libx264-default.ffpreset b/ffpresets/libx264-default.ffpreset index ec20174c20..a10c633240 100644 --- a/ffpresets/libx264-default.ffpreset +++ b/ffpresets/libx264-default.ffpreset @@ -3,7 +3,7 @@ flags=+loop cmp=+chroma partitions=+parti8x8+parti4x4+partp8x8+partb8x8 me_method=hex -subq=6 +subq=7 me_range=16 g=250 keyint_min=25 @@ -14,5 +14,9 @@ qcomp=0.6 qmin=10 qmax=51 qdiff=4 +bf=3 +refs=3 directpred=1 -flags2=+fastpskip +trellis=1 +flags2=+mixed_refs+wpred+dct8x8+fastpskip+mbtree +wpredp=2 diff --git a/ffpresets/libx264-fastfirstpass.ffpreset b/ffpresets/libx264-fastfirstpass.ffpreset index aaad4615d0..7cf6faae41 100644 --- a/ffpresets/libx264-fastfirstpass.ffpreset +++ b/ffpresets/libx264-fastfirstpass.ffpreset @@ -3,7 +3,7 @@ flags=+loop cmp=+chroma partitions=-parti8x8-parti4x4-partp8x8-partp4x4-partb8x8 me_method=dia -subq=1 +subq=2 me_range=16 g=250 keyint_min=25 @@ -14,8 +14,9 @@ qcomp=0.6 qmin=10 qmax=51 qdiff=4 -bf=4 +bf=3 refs=1 directpred=3 trellis=0 -flags2=-bpyramid-wpred-mixed_refs-dct8x8+fastpskip +flags2=-bpyramid-wpred-mixed_refs-dct8x8+fastpskip+mbtree +wpredp=2 diff --git a/ffpresets/libx264-hq.ffpreset b/ffpresets/libx264-hq.ffpreset index cdf67eb067..eafc52e410 100644 --- a/ffpresets/libx264-hq.ffpreset +++ b/ffpresets/libx264-hq.ffpreset @@ -14,8 +14,9 @@ qcomp=0.6 qmin=10 qmax=51 qdiff=4 -bf=4 +bf=3 refs=4 directpred=3 trellis=1 -flags2=+bpyramid+wpred+mixed_refs+dct8x8+fastpskip +flags2=+wpred+mixed_refs+dct8x8+fastpskip+mbtree +wpredp=2 diff --git a/ffpresets/libx264-ipod320.ffpreset b/ffpresets/libx264-ipod320.ffpreset index 89441c7deb..b10205025e 100644 --- a/ffpresets/libx264-ipod320.ffpreset +++ b/ffpresets/libx264-ipod320.ffpreset @@ -1,6 +1,7 @@ coder=0 bf=0 -flags2=-wpred-dct8x8 +flags2=-wpred-dct8x8+mbtree level=13 maxrate=768000 bufsize=3000000 +wpredp=0 diff --git a/ffpresets/libx264-ipod640.ffpreset b/ffpresets/libx264-ipod640.ffpreset index fd58e2ad71..aa731dccc5 100644 --- a/ffpresets/libx264-ipod640.ffpreset +++ b/ffpresets/libx264-ipod640.ffpreset @@ -1,7 +1,8 @@ coder=0 bf=0 refs=1 -flags2=-wpred-dct8x8 +flags2=-wpred-dct8x8+mbtree level=30 maxrate=10000000 bufsize=10000000 +wpredp=0 diff --git a/ffpresets/libx264-lossless_fast.ffpreset b/ffpresets/libx264-lossless_fast.ffpreset index dcf418afda..9bbabf5f7c 100644 --- a/ffpresets/libx264-lossless_fast.ffpreset +++ b/ffpresets/libx264-lossless_fast.ffpreset @@ -15,5 +15,6 @@ qmin=10 qmax=51 qdiff=4 directpred=1 -flags2=+fastpskip +flags2=+fastpskip+mbtree cqp=0 +wpredp=0 diff --git a/ffpresets/libx264-lossless_max.ffpreset b/ffpresets/libx264-lossless_max.ffpreset index b8506c227f..d4ba018b75 100644 --- a/ffpresets/libx264-lossless_max.ffpreset +++ b/ffpresets/libx264-lossless_max.ffpreset @@ -16,5 +16,6 @@ qmax=51 qdiff=4 refs=16 directpred=1 -flags2=+mixed_refs+dct8x8+fastpskip +flags2=+mixed_refs+dct8x8+fastpskip+mbtree cqp=0 +wpredp=2 diff --git a/ffpresets/libx264-lossless_medium.ffpreset b/ffpresets/libx264-lossless_medium.ffpreset index 99fb6b9d07..c4133c2279 100644 --- a/ffpresets/libx264-lossless_medium.ffpreset +++ b/ffpresets/libx264-lossless_medium.ffpreset @@ -15,5 +15,6 @@ qmin=10 qmax=51 qdiff=4 directpred=1 -flags2=+fastpskip +flags2=+fastpskip+mbtree cqp=0 +wpredp=2 diff --git a/ffpresets/libx264-lossless_slow.ffpreset b/ffpresets/libx264-lossless_slow.ffpreset index 2ecb55b07e..1421b95603 100644 --- a/ffpresets/libx264-lossless_slow.ffpreset +++ b/ffpresets/libx264-lossless_slow.ffpreset @@ -16,5 +16,6 @@ qmax=51 qdiff=4 refs=2 directpred=1 -flags2=+dct8x8+fastpskip +flags2=+dct8x8+fastpskip+mbtree cqp=0 +wpredp=2 diff --git a/ffpresets/libx264-lossless_slower.ffpreset b/ffpresets/libx264-lossless_slower.ffpreset index dd499c7afd..942445921e 100644 --- a/ffpresets/libx264-lossless_slower.ffpreset +++ b/ffpresets/libx264-lossless_slower.ffpreset @@ -16,5 +16,6 @@ qmax=51 qdiff=4 refs=4 directpred=1 -flags2=+mixed_refs+dct8x8+fastpskip +flags2=+mixed_refs+dct8x8+fastpskip+mbtree cqp=0 +wpredp=2 diff --git a/ffpresets/libx264-lossless_ultrafast.ffpreset b/ffpresets/libx264-lossless_ultrafast.ffpreset index 1c429f21ff..3c44488e8d 100644 --- a/ffpresets/libx264-lossless_ultrafast.ffpreset +++ b/ffpresets/libx264-lossless_ultrafast.ffpreset @@ -15,5 +15,5 @@ qmin=10 qmax=51 qdiff=4 directpred=1 -flags2=+fastpskip +flags2=+fastpskip+mbtree cqp=0 diff --git a/ffpresets/libx264-main.ffpreset b/ffpresets/libx264-main.ffpreset index d1dc7ddae9..0868c53cff 100644 --- a/ffpresets/libx264-main.ffpreset +++ b/ffpresets/libx264-main.ffpreset @@ -1 +1 @@ -flags2=-dct8x8 +flags2=-dct8x8+mbtree diff --git a/ffpresets/libx264-max.ffpreset b/ffpresets/libx264-max.ffpreset index afdde53469..4289ab531e 100644 --- a/ffpresets/libx264-max.ffpreset +++ b/ffpresets/libx264-max.ffpreset @@ -3,8 +3,8 @@ flags=+loop cmp=+chroma partitions=+parti8x8+parti4x4+partp8x8+partp4x4+partb8x8 me_method=tesa -subq=9 -me_range=32 +subq=10 +me_range=24 g=250 keyint_min=25 sc_threshold=40 @@ -14,8 +14,9 @@ qcomp=0.6 qmin=10 qmax=51 qdiff=4 -bf=4 +bf=3 refs=16 directpred=3 trellis=2 -flags2=+bpyramid+wpred+mixed_refs+dct8x8-fastpskip +flags2=+wpred+mixed_refs+dct8x8-fastpskip+mbtree +wpredp=2 diff --git a/ffpresets/libx264-normal.ffpreset b/ffpresets/libx264-normal.ffpreset index 99ac2e319a..dd7991bab4 100644 --- a/ffpresets/libx264-normal.ffpreset +++ b/ffpresets/libx264-normal.ffpreset @@ -14,8 +14,9 @@ qcomp=0.6 qmin=10 qmax=51 qdiff=4 -bf=4 +bf=3 refs=2 directpred=3 trellis=0 -flags2=+bpyramid+wpred+dct8x8+fastpskip +flags2=+wpred+dct8x8+fastpskip+mbtree +wpredp=2 diff --git a/ffpresets/libx264-slowfirstpass.ffpreset b/ffpresets/libx264-slowfirstpass.ffpreset index 7358d446d2..ba49065c14 100644 --- a/ffpresets/libx264-slowfirstpass.ffpreset +++ b/ffpresets/libx264-slowfirstpass.ffpreset @@ -14,8 +14,9 @@ qcomp=0.6 qmin=10 qmax=51 qdiff=4 -bf=4 +bf=3 refs=1 directpred=3 trellis=0 -flags2=+bpyramid+wpred+dct8x8+fastpskip +flags2=+wpred+dct8x8+fastpskip+mbtree +wpredp=2 diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 835b589ded..065565baf6 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -501,6 +501,7 @@ typedef struct RcOverride{ #define CODEC_FLAG2_CHUNKS 0x00008000 ///< Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries. #define CODEC_FLAG2_NON_LINEAR_QUANT 0x00010000 ///< Use MPEG-2 nonlinear quantizer. #define CODEC_FLAG2_BIT_RESERVOIR 0x00020000 ///< Use a bit reservoir when encoding if possible +#define CODEC_FLAG2_MBTREE 0x00040000 ///< Use macroblock tree ratecontrol (x264 only) /* Unsupported options : * Syntax Arithmetic coding (SAC) @@ -2332,6 +2333,16 @@ typedef struct AVCodecContext { * Set to time_base ticks per frame. Default 1, e.g., H.264/MPEG-2 set it to 2. */ int ticks_per_frame; + + /** + * explicit P-frame weighted prediction analysis method + * 0: off + * 1: fast blind weighting (one reference duplicate with -1 offset) + * 2: smart weighting (full fade detection analysis) + * - encoding: Set by user. + * - decoding: unused + */ + int weighted_p_pred; } AVCodecContext; /** diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index d2d3b270d0..645d45dbbb 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -30,6 +30,8 @@ typedef struct X264Context { x264_param_t params; x264_t *enc; x264_picture_t pic; + uint8_t *sei; + int sei_size; AVFrame out_pic; } X264Context; @@ -48,21 +50,66 @@ static void X264_log(void *p, int level, const char *fmt, va_list args) av_vlog(p, level_map[level], fmt, args); } - -static int encode_nals(uint8_t *buf, int size, x264_nal_t *nals, int nnal) +#if X264_BUILD >= 76 +static int encode_nals(AVCodecContext *ctx, uint8_t *buf, int size, + x264_nal_t *nals, int nnal, int skip_sei) { + X264Context *x4 = ctx->priv_data; uint8_t *p = buf; int i; + /* Write the SEI as part of the first frame. */ + if (x4->sei_size > 0 && nnal > 0) { + memcpy(p, x4->sei, x4->sei_size); + p += x4->sei_size; + x4->sei_size = 0; + } + + for (i = 0; i < nnal; i++){ + /* Don't put the SEI in extradata. */ + if (skip_sei && nals[i].i_type == NAL_SEI) { + x4->sei_size = nals[i].i_payload; + x4->sei = av_malloc(x4->sei_size); + memcpy(x4->sei, nals[i].p_payload, nals[i].i_payload); + continue; + } + memcpy(p, nals[i].p_payload, nals[i].i_payload); + p += nals[i].i_payload; + } + + return p - buf; +} +#else +static int encode_nals(AVCodecContext *ctx, uint8_t *buf, int size, x264_nal_t *nals, int nnal, int skip_sei) +{ + X264Context *x4 = ctx->priv_data; + uint8_t *p = buf; + int i, s; + + /* Write the SEI as part of the first frame. */ + if (x4->sei_size > 0 && nnal > 0) { + memcpy(p, x4->sei, x4->sei_size); + p += x4->sei_size; + x4->sei_size = 0; + } + for (i = 0; i < nnal; i++) { - int s = x264_nal_encode(p, &size, 1, nals + i); - if(s < 0) + /* Don't put the SEI in extradata. */ + if (skip_sei && nals[i].i_type == NAL_SEI) { + x4->sei = av_malloc( 5 + nals[i].i_payload * 4 / 3 ); + if(x264_nal_encode(x4->sei, &x4->sei_size, 1, nals + i) < 0) + return -1; + continue; + } + s = x264_nal_encode(p, &size, 1, nals + i); + if (s < 0) return -1; p += s; } return p - buf; } +#endif static int X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data) @@ -86,15 +133,14 @@ static int X264_frame(AVCodecContext *ctx, uint8_t *buf, x4->pic.i_type = X264_TYPE_AUTO; } - if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, - &pic_out)) + if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0) return -1; - bufsize = encode_nals(buf, bufsize, nal, nnal); + bufsize = encode_nals(ctx, buf, bufsize, nal, nnal, 0); if (bufsize < 0) return -1; - /* FIXME: dts */ + /* FIXME: libx264 now provides DTS, but AVFrame doesn't have a field for it. */ x4->out_pic.pts = pic_out.i_pts; switch (pic_out.i_type) { @@ -111,7 +157,11 @@ static int X264_frame(AVCodecContext *ctx, uint8_t *buf, break; } +#if X264_BUILD < 82 x4->out_pic.key_frame = pic_out.i_type == X264_TYPE_IDR; +#else + x4->out_pic.key_frame = pic_out.b_keyframe; +#endif x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA; return bufsize; @@ -122,6 +172,7 @@ static av_cold int X264_close(AVCodecContext *avctx) X264Context *x4 = avctx->priv_data; av_freep(&avctx->extradata); + av_free(x4->sei); if (x4->enc) x264_encoder_close(x4->enc); @@ -133,6 +184,7 @@ static av_cold int X264_init(AVCodecContext *avctx) { X264Context *x4 = avctx->priv_data; + x4->sei_size = 0; x264_param_default(&x4->params); x4->params.pf_log = X264_log; @@ -164,7 +216,11 @@ static av_cold int X264_init(AVCodecContext *avctx) x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC; x4->params.i_bframe_adaptive = avctx->b_frame_strategy; x4->params.i_bframe_bias = avctx->bframebias; +#if X264_BUILD >= 78 + x4->params.i_bframe_pyramid = avctx->flags2 & CODEC_FLAG2_BPYRAMID ? X264_B_PYRAMID_NORMAL : X264_B_PYRAMID_NONE; +#else x4->params.b_bframe_pyramid = avctx->flags2 & CODEC_FLAG2_BPYRAMID; +#endif avctx->has_b_frames = avctx->flags2 & CODEC_FLAG2_BPYRAMID ? 2 : !!avctx->max_b_frames; x4->params.i_keyint_min = avctx->keyint_min; @@ -191,8 +247,10 @@ static av_cold int X264_init(AVCodecContext *avctx) x4->params.i_height = avctx->height; x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num; x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den; - x4->params.i_fps_num = avctx->time_base.den; - x4->params.i_fps_den = avctx->time_base.num; +#if X264_BUILD >= 81 + x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den; + x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num; +#endif x4->params.analyse.inter = 0; if (avctx->partitions) { @@ -211,6 +269,9 @@ static av_cold int X264_init(AVCodecContext *avctx) x4->params.analyse.i_direct_mv_pred = avctx->directpred; x4->params.analyse.b_weighted_bipred = avctx->flags2 & CODEC_FLAG2_WPRED; +#if X264_BUILD >= 79 + x4->params.analyse.i_weighted_pred = avctx->weighted_p_pred; +#endif if (avctx->me_method == ME_EPZS) x4->params.analyse.i_me_method = X264_ME_DIA; @@ -248,6 +309,9 @@ static av_cold int X264_init(AVCodecContext *avctx) } else x4->params.rc.f_vbv_buffer_init = 0.9; +#if X264_BUILD >= 69 + x4->params.rc.b_mb_tree = !!(avctx->flags2 & CODEC_FLAG2_MBTREE); +#endif x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor); x4->params.rc.f_pb_factor = avctx->b_quant_factor; x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset; @@ -270,7 +334,18 @@ static av_cold int X264_init(AVCodecContext *avctx) avctx->coded_frame = &x4->out_pic; +#if X264_BUILD >= 76 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { + x264_nal_t *nal; + int nnal, s; + + s = x264_encoder_headers(x4->enc, &nal, &nnal); + + avctx->extradata = av_malloc(s); + avctx->extradata_size = encode_nals(avctx, avctx->extradata, s, nal, nnal, 1); + } +#else + if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){ x264_nal_t *nal; int nnal, i, s = 0; @@ -281,8 +356,9 @@ static av_cold int X264_init(AVCodecContext *avctx) s += 5 + nal[i].i_payload * 4 / 3; avctx->extradata = av_malloc(s); - avctx->extradata_size = encode_nals(avctx->extradata, s, nal, nnal); + avctx->extradata_size = encode_nals(avctx, avctx->extradata, s, nal, nnal, 1); } +#endif return 0; } @@ -296,6 +372,6 @@ AVCodec libx264_encoder = { .encode = X264_frame, .close = X264_close, .capabilities = CODEC_CAP_DELAY, - .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_NONE }, + .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_NONE }, .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"), }; diff --git a/libavcodec/options.c b/libavcodec/options.c index e5a67a4893..1ab78180a3 100644 --- a/libavcodec/options.c +++ b/libavcodec/options.c @@ -122,6 +122,7 @@ static const AVOption options[]={ {"b_qfactor", "qp factor between p and b frames", OFFSET(b_quant_factor), FF_OPT_TYPE_FLOAT, 1.25, -FLT_MAX, FLT_MAX, V|E}, {"rc_strategy", "ratecontrol method", OFFSET(rc_strategy), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|E}, {"b_strategy", "strategy to choose between I/P/B-frames", OFFSET(b_frame_strategy), FF_OPT_TYPE_INT, 0, INT_MIN, INT_MAX, V|E}, +{"wpredp", "weighted prediction analysis method", OFFSET(weighted_p_pred), FF_OPT_TYPE_INT, 0, INT_MIN, INT_MAX, V|E}, {"hurry_up", NULL, OFFSET(hurry_up), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|D}, {"ps", "rtp payload size in bits", OFFSET(rtp_payload_size), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|E}, {"mv_bits", NULL, OFFSET(mv_bits), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX}, @@ -388,6 +389,7 @@ static const AVOption options[]={ {"request_channels", "set desired number of audio channels", OFFSET(request_channels), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, A|D}, {"drc_scale", "percentage of dynamic range compression to apply", OFFSET(drc_scale), FF_OPT_TYPE_FLOAT, 1.0, 0.0, 1.0, A|D}, {"reservoir", "use bit reservoir", 0, FF_OPT_TYPE_CONST, CODEC_FLAG2_BIT_RESERVOIR, INT_MIN, INT_MAX, A|E, "flags2"}, +{"mbtree", "use macroblock tree ratecontrol (x264 only)", 0, FF_OPT_TYPE_CONST, CODEC_FLAG2_MBTREE, INT_MIN, INT_MAX, V|E, "flags2"}, {"bits_per_raw_sample", NULL, OFFSET(bits_per_raw_sample), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX}, {"channel_layout", NULL, OFFSET(channel_layout), FF_OPT_TYPE_INT64, DEFAULT, 0, INT64_MAX, A|E|D, "channel_layout"}, {"request_channel_layout", NULL, OFFSET(request_channel_layout), FF_OPT_TYPE_INT64, DEFAULT, 0, INT64_MAX, A|D, "request_channel_layout"}, -- cgit v1.2.3