X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Flibx264.c;h=2bed61f91e17512f8e9a491070cba4d8c1af483e;hb=bd96c54fe4819b3ca9a975e2083d67f4443c559b;hp=c13e8a58d4a8a575c11cc6500dfc2245f0974172;hpb=f5e33514e7a879b09fabfeffd78402590225bf47;p=ffmpeg diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index c13e8a58d4a..2bed61f91e1 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -29,6 +29,8 @@ #include "libavutil/intreadwrite.h" #include "avcodec.h" #include "internal.h" +#include "packet_internal.h" +#include "atsc_a53.h" #if defined(_MSC_VER) #define X264_API_IMPORTS 1 @@ -195,51 +197,51 @@ static void reconfig_encoder(AVCodecContext *ctx, const AVFrame *frame) AVFrameSideData *side_data; - if (x4->avcintra_class < 0) { - if (x4->params.b_interlaced && x4->params.b_tff != frame->top_field_first) { + if (x4->avcintra_class < 0) { + if (x4->params.b_interlaced && x4->params.b_tff != frame->top_field_first) { - x4->params.b_tff = frame->top_field_first; - x264_encoder_reconfig(x4->enc, &x4->params); - } - if (x4->params.vui.i_sar_height*ctx->sample_aspect_ratio.num != ctx->sample_aspect_ratio.den * x4->params.vui.i_sar_width) { - x4->params.vui.i_sar_height = ctx->sample_aspect_ratio.den; - x4->params.vui.i_sar_width = ctx->sample_aspect_ratio.num; - x264_encoder_reconfig(x4->enc, &x4->params); - } + x4->params.b_tff = frame->top_field_first; + x264_encoder_reconfig(x4->enc, &x4->params); + } + if (x4->params.vui.i_sar_height*ctx->sample_aspect_ratio.num != ctx->sample_aspect_ratio.den * x4->params.vui.i_sar_width) { + x4->params.vui.i_sar_height = ctx->sample_aspect_ratio.den; + x4->params.vui.i_sar_width = ctx->sample_aspect_ratio.num; + x264_encoder_reconfig(x4->enc, &x4->params); + } - if (x4->params.rc.i_vbv_buffer_size != ctx->rc_buffer_size / 1000 || - x4->params.rc.i_vbv_max_bitrate != ctx->rc_max_rate / 1000) { - x4->params.rc.i_vbv_buffer_size = ctx->rc_buffer_size / 1000; - x4->params.rc.i_vbv_max_bitrate = ctx->rc_max_rate / 1000; - x264_encoder_reconfig(x4->enc, &x4->params); - } + if (x4->params.rc.i_vbv_buffer_size != ctx->rc_buffer_size / 1000 || + x4->params.rc.i_vbv_max_bitrate != ctx->rc_max_rate / 1000) { + x4->params.rc.i_vbv_buffer_size = ctx->rc_buffer_size / 1000; + x4->params.rc.i_vbv_max_bitrate = ctx->rc_max_rate / 1000; + x264_encoder_reconfig(x4->enc, &x4->params); + } - if (x4->params.rc.i_rc_method == X264_RC_ABR && - x4->params.rc.i_bitrate != ctx->bit_rate / 1000) { - x4->params.rc.i_bitrate = ctx->bit_rate / 1000; - x264_encoder_reconfig(x4->enc, &x4->params); - } + if (x4->params.rc.i_rc_method == X264_RC_ABR && + x4->params.rc.i_bitrate != ctx->bit_rate / 1000) { + x4->params.rc.i_bitrate = ctx->bit_rate / 1000; + x264_encoder_reconfig(x4->enc, &x4->params); + } - if (x4->crf >= 0 && - x4->params.rc.i_rc_method == X264_RC_CRF && - x4->params.rc.f_rf_constant != x4->crf) { - x4->params.rc.f_rf_constant = x4->crf; - x264_encoder_reconfig(x4->enc, &x4->params); - } + if (x4->crf >= 0 && + x4->params.rc.i_rc_method == X264_RC_CRF && + x4->params.rc.f_rf_constant != x4->crf) { + x4->params.rc.f_rf_constant = x4->crf; + x264_encoder_reconfig(x4->enc, &x4->params); + } - if (x4->params.rc.i_rc_method == X264_RC_CQP && - x4->cqp >= 0 && - x4->params.rc.i_qp_constant != x4->cqp) { - x4->params.rc.i_qp_constant = x4->cqp; - x264_encoder_reconfig(x4->enc, &x4->params); - } + if (x4->params.rc.i_rc_method == X264_RC_CQP && + x4->cqp >= 0 && + x4->params.rc.i_qp_constant != x4->cqp) { + x4->params.rc.i_qp_constant = x4->cqp; + x264_encoder_reconfig(x4->enc, &x4->params); + } - if (x4->crf_max >= 0 && - x4->params.rc.f_rf_constant_max != x4->crf_max) { - x4->params.rc.f_rf_constant_max = x4->crf_max; - x264_encoder_reconfig(x4->enc, &x4->params); + if (x4->crf_max >= 0 && + x4->params.rc.f_rf_constant_max != x4->crf_max) { + x4->params.rc.f_rf_constant_max = x4->crf_max; + x264_encoder_reconfig(x4->enc, &x4->params); + } } - } side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_STEREO3D); if (side_data) { @@ -476,25 +478,15 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, pict_type = AV_PICTURE_TYPE_B; break; default: - pict_type = AV_PICTURE_TYPE_NONE; + av_log(ctx, AV_LOG_ERROR, "Unknown picture type encountered.\n"); + return AVERROR_EXTERNAL; } -#if FF_API_CODED_FRAME -FF_DISABLE_DEPRECATION_WARNINGS - ctx->coded_frame->pict_type = pict_type; -FF_ENABLE_DEPRECATION_WARNINGS -#endif pkt->flags |= AV_PKT_FLAG_KEY*pic_out.b_keyframe; if (ret) { ff_side_data_set_encoder_stats(pkt, (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA, NULL, 0, pict_type); if (wallclock) ff_side_data_set_prft(pkt, wallclock); - -#if FF_API_CODED_FRAME -FF_DISABLE_DEPRECATION_WARNINGS - ctx->coded_frame->quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA; -FF_ENABLE_DEPRECATION_WARNINGS -#endif } *got_packet = ret; @@ -509,6 +501,10 @@ static av_cold int X264_close(AVCodecContext *avctx) av_freep(&x4->sei); av_freep(&x4->reordered_opaque); +#if X264_BUILD >= 161 + x264_param_cleanup(&x4->params); +#endif + if (x4->enc) { x264_encoder_close(x4->enc); x4->enc = NULL; @@ -517,19 +513,31 @@ static av_cold int X264_close(AVCodecContext *avctx) return 0; } -#define OPT_STR(opt, param) \ - do { \ - int ret; \ - if ((ret = x264_param_parse(&x4->params, opt, param)) < 0) { \ - if(ret == X264_PARAM_BAD_NAME) \ - av_log(avctx, AV_LOG_ERROR, \ - "bad option '%s': '%s'\n", opt, param); \ - else \ - av_log(avctx, AV_LOG_ERROR, \ - "bad value for '%s': '%s'\n", opt, param); \ - return -1; \ - } \ - } while (0) +static int parse_opts(AVCodecContext *avctx, const char *opt, const char *param) +{ + X264Context *x4 = avctx->priv_data; + int ret; + + if ((ret = x264_param_parse(&x4->params, opt, param)) < 0) { + if (ret == X264_PARAM_BAD_NAME) { + av_log(avctx, AV_LOG_ERROR, + "bad option '%s': '%s'\n", opt, param); + ret = AVERROR(EINVAL); +#if X264_BUILD >= 161 + } else if (ret == X264_PARAM_ALLOC_FAILED) { + av_log(avctx, AV_LOG_ERROR, + "out of memory parsing option '%s': '%s'\n", opt, param); + ret = AVERROR(ENOMEM); +#endif + } else { + av_log(avctx, AV_LOG_ERROR, + "bad value for '%s': '%s'\n", opt, param); + ret = AVERROR(EINVAL); + } + } + + return ret; +} static int convert_pix_fmt(enum AVPixelFormat pix_fmt) { @@ -579,6 +587,7 @@ static av_cold int X264_init(AVCodecContext *avctx) X264Context *x4 = avctx->priv_data; AVCPBProperties *cpb_props; int sw,sh; + int ret; if (avctx->global_quality > 0) av_log(avctx, AV_LOG_WARNING, "-qscale is ignored, -crf is recommended.\n"); @@ -660,13 +669,7 @@ static av_cold int X264_init(AVCodecContext *avctx) if (avctx->b_quant_factor > 0) x4->params.rc.f_pb_factor = avctx->b_quant_factor; -#if FF_API_PRIVATE_OPT -FF_DISABLE_DEPRECATION_WARNINGS - if (avctx->chromaoffset >= 0) - x4->chroma_offset = avctx->chromaoffset; -FF_ENABLE_DEPRECATION_WARNINGS -#endif - if (x4->chroma_offset >= 0) + if (x4->chroma_offset) x4->params.analyse.i_chroma_qp_offset = x4->chroma_offset; if (avctx->gop_size >= 0) @@ -674,12 +677,6 @@ FF_ENABLE_DEPRECATION_WARNINGS if (avctx->max_b_frames >= 0) x4->params.i_bframe = avctx->max_b_frames; -#if FF_API_PRIVATE_OPT -FF_DISABLE_DEPRECATION_WARNINGS - if (avctx->scenechange_threshold >= 0) - x4->scenechange_threshold = avctx->scenechange_threshold; -FF_ENABLE_DEPRECATION_WARNINGS -#endif if (x4->scenechange_threshold >= 0) x4->params.i_scenecut_threshold = x4->scenechange_threshold; @@ -709,30 +706,12 @@ FF_ENABLE_DEPRECATION_WARNINGS x4->params.analyse.i_trellis = avctx->trellis; if (avctx->me_range >= 0) x4->params.analyse.i_me_range = avctx->me_range; -#if FF_API_PRIVATE_OPT - FF_DISABLE_DEPRECATION_WARNINGS - if (avctx->noise_reduction >= 0) - x4->noise_reduction = avctx->noise_reduction; - FF_ENABLE_DEPRECATION_WARNINGS -#endif if (x4->noise_reduction >= 0) x4->params.analyse.i_noise_reduction = x4->noise_reduction; if (avctx->me_subpel_quality >= 0) x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality; -#if FF_API_PRIVATE_OPT -FF_DISABLE_DEPRECATION_WARNINGS - if (avctx->b_frame_strategy >= 0) - x4->b_frame_strategy = avctx->b_frame_strategy; -FF_ENABLE_DEPRECATION_WARNINGS -#endif if (avctx->keyint_min >= 0) x4->params.i_keyint_min = avctx->keyint_min; -#if FF_API_CODER_TYPE -FF_DISABLE_DEPRECATION_WARNINGS - if (avctx->coder_type >= 0) - x4->coder = avctx->coder_type == FF_CODER_TYPE_AC; -FF_ENABLE_DEPRECATION_WARNINGS -#endif if (avctx->me_cmp >= 0) x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA; @@ -888,9 +867,14 @@ FF_ENABLE_DEPRECATION_WARNINGS while(p){ char param[4096]={0}, val[4096]={0}; if(sscanf(p, "%4095[^:=]=%4095[^:]", param, val) == 1){ - OPT_STR(param, "1"); - }else - OPT_STR(param, val); + ret = parse_opts(avctx, param, "1"); + if (ret < 0) + return ret; + } else { + ret = parse_opts(avctx, param, val); + if (ret < 0) + return ret; + } p= strchr(p, ':'); p+=!!p; } @@ -900,10 +884,15 @@ FF_ENABLE_DEPRECATION_WARNINGS { AVDictionaryEntry *en = NULL; while (en = av_dict_get(x4->x264_params, "", en, AV_DICT_IGNORE_SUFFIX)) { - if (x264_param_parse(&x4->params, en->key, en->value) < 0) + if ((ret = x264_param_parse(&x4->params, en->key, en->value)) < 0) { av_log(avctx, AV_LOG_WARNING, "Error parsing option '%s = %s'.\n", en->key, en->value); +#if X264_BUILD >= 161 + if (ret == X264_PARAM_ALLOC_FAILED) + return AVERROR(ENOMEM); +#endif + } } } @@ -1021,19 +1010,17 @@ static const enum AVPixelFormat pix_fmts_8bit_rgb[] = { }; #endif +#if X264_BUILD < 153 static av_cold void X264_init_static(AVCodec *codec) { -#if X264_BUILD < 153 if (x264_bit_depth == 8) codec->pix_fmts = pix_fmts_8bit; else if (x264_bit_depth == 9) codec->pix_fmts = pix_fmts_9bit; else if (x264_bit_depth == 10) codec->pix_fmts = pix_fmts_10bit; -#else - codec->pix_fmts = pix_fmts_all; -#endif } +#endif #define OFFSET(x) offsetof(X264Context, x) #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM @@ -1111,7 +1098,7 @@ static const AVOption options[] = { { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "coder" }, { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "coder" }, { "b_strategy", "Strategy to choose between I/P/B-frames", OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE }, - { "chromaoffset", "QP difference between chroma and luma", OFFSET(chroma_offset), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE }, + { "chromaoffset", "QP difference between chroma and luma", OFFSET(chroma_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, VE }, { "sc_threshold", "Scene change threshold", OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE }, { "noise_reduction", "Noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE }, @@ -1133,22 +1120,10 @@ static const AVCodecDefault x264_defaults[] = { { "qcomp", "-1" }, // { "rc_lookahead", "-1" }, { "refs", "-1" }, -#if FF_API_PRIVATE_OPT - { "sc_threshold", "-1" }, -#endif { "trellis", "-1" }, -#if FF_API_PRIVATE_OPT - { "nr", "-1" }, -#endif { "me_range", "-1" }, { "subq", "-1" }, -#if FF_API_PRIVATE_OPT - { "b_strategy", "-1" }, -#endif { "keyint_min", "-1" }, -#if FF_API_CODER_TYPE - { "coder", "-1" }, -#endif { "cmp", "-1" }, { "threads", AV_STRINGIFY(X264_THREADS_AUTO) }, { "thread_type", "0" }, @@ -1165,6 +1140,9 @@ static const AVClass x264_class = { .version = LIBAVUTIL_VERSION_INT, }; +#if X264_BUILD >= 153 +const +#endif AVCodec ff_libx264_encoder = { .name = "libx264", .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"), @@ -1174,16 +1152,21 @@ AVCodec ff_libx264_encoder = { .init = X264_init, .encode2 = X264_frame, .close = X264_close, - .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS | + .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, + .caps_internal = FF_CODEC_CAP_AUTO_THREADS, .priv_class = &x264_class, .defaults = x264_defaults, +#if X264_BUILD < 153 .init_static_data = X264_init_static, -#if X264_BUILD >= 158 - .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE, #else - .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, + .pix_fmts = pix_fmts_all, #endif + .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_AUTO_THREADS +#if X264_BUILD >= 158 + | FF_CODEC_CAP_INIT_THREADSAFE +#endif + , .wrapper_name = "libx264", }; #endif @@ -1196,7 +1179,7 @@ static const AVClass rgbclass = { .version = LIBAVUTIL_VERSION_INT, }; -AVCodec ff_libx264rgb_encoder = { +const AVCodec ff_libx264rgb_encoder = { .name = "libx264rgb", .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB"), .type = AVMEDIA_TYPE_VIDEO, @@ -1205,16 +1188,16 @@ AVCodec ff_libx264rgb_encoder = { .init = X264_init, .encode2 = X264_frame, .close = X264_close, - .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS | + .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, .priv_class = &rgbclass, .defaults = x264_defaults, .pix_fmts = pix_fmts_8bit_rgb, + .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_AUTO_THREADS #if X264_BUILD >= 158 - .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE, -#else - .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, + | FF_CODEC_CAP_INIT_THREADSAFE #endif + , .wrapper_name = "libx264", }; #endif @@ -1227,7 +1210,7 @@ static const AVClass X262_class = { .version = LIBAVUTIL_VERSION_INT, }; -AVCodec ff_libx262_encoder = { +const AVCodec ff_libx262_encoder = { .name = "libx262", .long_name = NULL_IF_CONFIG_SMALL("libx262 MPEG2VIDEO"), .type = AVMEDIA_TYPE_VIDEO, @@ -1236,12 +1219,12 @@ AVCodec ff_libx262_encoder = { .init = X264_init, .encode2 = X264_frame, .close = X264_close, - .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS | + .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, .priv_class = &X262_class, .defaults = x264_defaults, .pix_fmts = pix_fmts_8bit, - .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, + .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_AUTO_THREADS, .wrapper_name = "libx264", }; #endif