[FFmpeg-devel] [PATCH v2 4/5] fftools/ffmpeg: flush and recreate encoder instance if resolution changes
Linjie Fu
linjie.fu at intel.com
Thu Jun 11 18:43:02 EEST 2020
Add recreate_encoder_instance() function.
If resolution changing is allowed, discard AV_CODEC_FLAG_GLOBAL_HEADER
even if the avformat/container declares AVFMT_GLOBALHEADER flag. Place
header information in every keyframe instead of single global header.
Signed-off-by: Linjie Fu <linjie.fu at intel.com>
---
Should be squashed with:
https://patchwork.ffmpeg.org/project/ffmpeg/list/?series=1434
fftools/ffmpeg.c | 40 ++++++++++++++++++++++++++++++++++++++++
fftools/ffmpeg_opt.c | 2 +-
2 files changed, 41 insertions(+), 1 deletion(-)
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 5859781..86562c9 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -130,6 +130,7 @@ static void do_video_stats(OutputStream *ost, int frame_size);
static BenchmarkTimeStamps get_benchmark_time_stamps(void);
static int64_t getmaxrss(void);
static int ifilter_has_all_input_formats(FilterGraph *fg);
+static void flush_encoders(void);
static int run_as_daemon = 0;
static int nb_frames_dup = 0;
@@ -1039,6 +1040,37 @@ static void do_subtitle_out(OutputFile *of,
}
}
+static int recreate_encoder_instance(OutputStream *ost,
+ int width, int height)
+{
+ AVCodec *encoder = ost->enc_ctx->codec;
+ AVRational time_base = ost->enc_ctx->time_base;
+ int ret;
+
+ avcodec_free_context(&ost->enc_ctx);
+
+ ost->enc_ctx = avcodec_alloc_context3(encoder);
+ if (!ost->enc_ctx)
+ return AVERROR(ENOMEM);
+
+ ost->st->codecpar->width = width;
+ ost->st->codecpar->height = height;
+
+ if (ret = avcodec_parameters_to_context(ost->enc_ctx, ost->st->codecpar) < 0)
+ return ret;
+
+ ost->enc_ctx->time_base = time_base;
+
+ if (ret = avcodec_open2(ost->enc_ctx, encoder, &ost->encoder_opts) < 0) {
+ av_log(NULL, AV_LOG_ERROR, "Error while re-opening encoder for output stream #%d:%d - "
+ "maybe incorrect parameters such as bit_rate, rate, width or height.\n",
+ ost->file_index, ost->index);
+ return ret;
+ }
+
+ return 0;
+}
+
static void do_video_out(OutputFile *of,
OutputStream *ost,
AVFrame *next_picture,
@@ -1058,11 +1090,19 @@ static void do_video_out(OutputFile *of,
if (next_picture && (enc->width != next_picture->width ||
enc->height != next_picture->height)) {
+ flush_encoders();
+ avcodec_flush_buffers(enc);
+
if (!(enc->codec->capabilities & AV_CODEC_CAP_VARIABLE_DIMENSIONS)) {
av_log(NULL, AV_LOG_ERROR, "Variable dimension encoding "
"is not supported by %s.\n", enc->codec->name);
goto error;
}
+
+ if (recreate_encoder_instance(ost, next_picture->width, next_picture->height) < 0)
+ goto error;
+
+ enc = ost->enc_ctx;
}
if (ost->source_index >= 0)
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 9d1489c..334c6ec 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1562,7 +1562,7 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
MATCH_PER_STREAM_OPT(max_muxing_queue_size, i, ost->max_muxing_queue_size, oc, st);
ost->max_muxing_queue_size *= sizeof(AVPacket);
- if (oc->oformat->flags & AVFMT_GLOBALHEADER)
+ if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->autoscale)
ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
av_dict_copy(&ost->sws_dict, o->g->sws_dict, 0);
--
2.7.4
More information about the ffmpeg-devel
mailing list