aboutsummaryrefslogtreecommitdiffstats
path: root/fftools
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2024-04-09 14:11:12 +0200
committerAnton Khirnov <anton@khirnov.net>2024-07-03 11:38:52 +0200
commit9a7686e5458dad8d40b3b3f70f6a19530933468e (patch)
tree22d71fdcff8697472691551c41d2cb37b9a7d50e /fftools
parentf1664aabb18b2d726c3c0c0b7fa8202f3b52b8da (diff)
downloadffmpeg-9a7686e5458dad8d40b3b3f70f6a19530933468e.tar.gz
fftools/ffmpeg_mux_init: apply encoder options manually
Do not pass an options dictionary to the avcodec_open2() in enc_open(). This is cleaner and more robust, as previously various bits of code would try to interpret the contents of the options dictionary, with varying degrees of correctness. Now they can just access the encoder AVCodecContext directly. Cf. 372c78dd42f2b1ca743473b9c32fad71c65919e0 - analogous change for decoding. A non-progressive field order is now written on the container level in interlaced ProRes encoding tests.
Diffstat (limited to 'fftools')
-rw-r--r--fftools/ffmpeg_enc.c18
-rw-r--r--fftools/ffmpeg_mux_init.c66
2 files changed, 39 insertions, 45 deletions
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 928114c20f..c1c8aa0e78 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -307,16 +307,10 @@ int enc_open(void *opaque, const AVFrame *frame)
if (ost->bitexact)
enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
- if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
- av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
+ if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)
+ enc_ctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
- if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE) {
- ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
- if (ret < 0)
- return ret;
- }
-
- av_dict_set(&ost->encoder_opts, "flags", "+frame_duration", AV_DICT_MULTIKEY);
+ enc_ctx->flags |= AV_CODEC_FLAG_FRAME_DURATION;
ret = hw_device_setup_for_encode(ost, frame ? frame->hw_frames_ctx : NULL);
if (ret < 0) {
@@ -325,7 +319,7 @@ int enc_open(void *opaque, const AVFrame *frame)
return ret;
}
- if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
+ if ((ret = avcodec_open2(ost->enc_ctx, enc, NULL)) < 0) {
if (ret != AVERROR_EXPERIMENTAL)
av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
"incorrect parameters such as bit_rate, rate, width or height.\n");
@@ -337,10 +331,6 @@ int enc_open(void *opaque, const AVFrame *frame)
if (ost->enc_ctx->frame_size)
frame_samples = ost->enc_ctx->frame_size;
- ret = check_avoptions(ost->encoder_opts);
- if (ret < 0)
- return ret;
-
if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low."
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index da68d1e5f0..cd413e0b7b 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -711,14 +711,10 @@ static int new_stream_video(Muxer *mux, const OptionsContext *o,
/* two pass mode */
MATCH_PER_STREAM_OPT(pass, i, do_pass, oc, st);
if (do_pass) {
- if (do_pass & 1) {
+ if (do_pass & 1)
video_enc->flags |= AV_CODEC_FLAG_PASS1;
- av_dict_set(&ost->encoder_opts, "flags", "+pass1", AV_DICT_APPEND);
- }
- if (do_pass & 2) {
+ if (do_pass & 2)
video_enc->flags |= AV_CODEC_FLAG_PASS2;
- av_dict_set(&ost->encoder_opts, "flags", "+pass2", AV_DICT_APPEND);
- }
}
MATCH_PER_STREAM_OPT(passlogfiles, str, ost->logfile_prefix, oc, st);
@@ -740,7 +736,10 @@ static int new_stream_video(Muxer *mux, const OptionsContext *o,
DEFAULT_PASS_LOGFILENAME_PREFIX,
ost_idx);
if (!strcmp(ost->enc_ctx->codec->name, "libx264") || !strcmp(ost->enc_ctx->codec->name, "libvvenc")) {
- av_dict_set(&ost->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
+ if (av_opt_is_set_to_default_by_name(ost->enc_ctx, "stats",
+ AV_OPT_SEARCH_CHILDREN) > 0)
+ av_opt_set(ost->enc_ctx, "stats", logfilename,
+ AV_OPT_SEARCH_CHILDREN);
} else {
if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
char *logbuffer = file_read(logfilename);
@@ -1041,6 +1040,7 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
const AVCodec *enc;
AVStream *st;
int ret = 0, keep_pix_fmt = 0, autoscale = 1;
+ int threads_manual = 0;
AVRational enc_tb = { 0, 0 };
enum VideoSyncMethod vsync_method = VSYNC_AUTO;
const char *bsfs = NULL, *time_base = NULL;
@@ -1264,6 +1264,23 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
enc_tb = q;
}
+
+ threads_manual = !!av_dict_get(ost->encoder_opts, "threads", NULL, 0);
+
+ ret = av_opt_set_dict2(ost->enc_ctx, &ost->encoder_opts, AV_OPT_SEARCH_CHILDREN);
+ if (ret < 0) {
+ av_log(ost, AV_LOG_ERROR, "Error applying encoder options: %s\n",
+ av_err2str(ret));
+ return ret;
+ }
+
+ ret = check_avoptions(ost->encoder_opts);
+ if (ret < 0)
+ return ret;
+
+ // default to automatic thread count
+ if (!threads_manual)
+ ost->enc_ctx->thread_count = 0;
} else {
ret = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st,
NULL, &ost->encoder_opts,
@@ -1276,8 +1293,7 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
if (o->bitexact) {
ost->bitexact = 1;
} else if (ost->enc_ctx) {
- ost->bitexact = check_opt_bitexact(ost->enc_ctx, ost->encoder_opts, "flags",
- AV_CODEC_FLAG_BITEXACT);
+ ost->bitexact = !!(ost->enc_ctx->flags & AV_CODEC_FLAG_BITEXACT);
}
MATCH_PER_STREAM_OPT(time_bases, str, time_base, oc, st);
@@ -1372,7 +1388,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
if (ost->enc &&
(type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
- const AVDictionaryEntry *e;
char name[16];
OutputFilterOptions opts = {
.enc = enc,
@@ -1398,10 +1413,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
snprintf(name, sizeof(name), "#%d:%d", mux->of.index, ost->index);
- e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
- if (e)
- opts.nb_threads = e->value;
-
// MJPEG encoder exports a full list of supported pixel formats,
// but the full-range ones are experimental-only.
// Restrict the auto-conversion list unless -strict experimental
@@ -1413,33 +1424,26 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
{ AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
AV_PIX_FMT_NONE };
- const AVDictionaryEntry *strict = av_dict_get(ost->encoder_opts, "strict", NULL, 0);
- int strict_val = ost->enc_ctx->strict_std_compliance;
-
- if (strict) {
- const AVOption *o = av_opt_find(ost->enc_ctx, strict->key, NULL, 0, 0);
- av_assert0(o);
- av_opt_eval_int(ost->enc_ctx, o, strict->value, &strict_val);
- }
-
- if (strict_val > FF_COMPLIANCE_UNOFFICIAL)
+ if (ost->enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
opts.pix_fmts = mjpeg_formats;
}
+ if (threads_manual) {
+ ret = av_opt_get(ost->enc_ctx, "threads", 0, (uint8_t**)&opts.nb_threads);
+ if (ret < 0)
+ return ret;
+ }
+
if (ofilter) {
ost->filter = ofilter;
ret = ofilter_bind_ost(ofilter, ost, ms->sch_idx_enc, &opts);
- if (ret < 0)
- return ret;
} else {
ret = init_simple_filtergraph(ost->ist, ost, filters,
mux->sch, ms->sch_idx_enc, &opts);
- if (ret < 0) {
- av_log(ost, AV_LOG_ERROR,
- "Error initializing a simple filtergraph\n");
- return ret;
- }
}
+ av_freep(&opts.nb_threads);
+ if (ret < 0)
+ return ret;
ret = sch_connect(mux->sch, SCH_ENC(ms->sch_idx_enc),
SCH_MSTREAM(mux->sch_idx, ms->sch_idx));