[FFmpeg-devel] [PATCH 3/4] ffmpeg: pass decoded or filtered AVFrame to output stream initialization

Jan Ekström jeebjp at gmail.com
Fri Sep 11 19:11:06 EEST 2020


Additionally, reap the first rewards by being able to set the
color related encoding values based on the passed AVFrame.
---
 fftools/ffmpeg.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 24b8e175a6..63c8771a01 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -897,9 +897,11 @@ static int check_recording_time(OutputStream *ost)
     return 1;
 }
 
-static int init_output_stream(OutputStream *ost, char *error, int error_len);
+static int init_output_stream(OutputStream *ost, AVFrame *frame,
+                              char *error, int error_len);
 
-static int init_output_stream_wrapper(OutputStream *ost, unsigned int fatal)
+static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame,
+                                      unsigned int fatal)
 {
     int ret = AVERROR_BUG;
     char error[1024] = {0};
@@ -907,7 +909,7 @@ static int init_output_stream_wrapper(OutputStream *ost, unsigned int fatal)
     if (ost->initialized)
         return 0;
 
-    ret = init_output_stream(ost, error, sizeof(error));
+    ret = init_output_stream(ost, frame, error, sizeof(error));
     if (ret < 0) {
         av_log(NULL, AV_LOG_ERROR, "Error initializing output stream %d:%d -- %s\n",
                ost->file_index, ost->index, error);
@@ -930,7 +932,7 @@ static void do_audio_out(OutputFile *of, OutputStream *ost,
     pkt.data = NULL;
     pkt.size = 0;
 
-    init_output_stream_wrapper(ost, 1);
+    init_output_stream_wrapper(ost, frame, 1);
 
     if (!check_recording_time(ost))
         return;
@@ -1081,7 +1083,7 @@ static void do_video_out(OutputFile *of,
     InputStream *ist = NULL;
     AVFilterContext *filter = ost->filter->filter;
 
-    init_output_stream_wrapper(ost, 1);
+    init_output_stream_wrapper(ost, next_picture, 1);
 
     if (ost->source_index >= 0)
         ist = input_streams[ost->source_index];
@@ -1899,7 +1901,7 @@ static void flush_encoders(void)
                 finish_output_stream(ost);
             }
 
-            init_output_stream_wrapper(ost, 1);
+            init_output_stream_wrapper(ost, NULL, 1);
         }
 
         if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
@@ -3271,7 +3273,7 @@ static void init_encoder_time_base(OutputStream *ost, AVRational default_time_ba
     enc_ctx->time_base = default_time_base;
 }
 
-static int init_output_stream_encode(OutputStream *ost)
+static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
 {
     InputStream *ist = get_input_stream(ost);
     AVCodecContext *enc_ctx = ost->enc_ctx;
@@ -3368,6 +3370,23 @@ static int init_output_stream_encode(OutputStream *ost)
             enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
                                                  av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
 
+        if (frame) {
+            if (!av_dict_get(ost->encoder_opts, "color_range", NULL, 0))
+                enc_ctx->color_range = frame->color_range;
+
+            if (!av_dict_get(ost->encoder_opts, "color_primaries", NULL, 0))
+                enc_ctx->color_primaries = frame->color_primaries;
+
+            if (!av_dict_get(ost->encoder_opts, "color_trc", NULL, 0))
+                enc_ctx->color_trc = frame->color_trc;
+
+            if (!av_dict_get(ost->encoder_opts, "colorspace", NULL, 0))
+                enc_ctx->colorspace = frame->colorspace;
+
+            if (!av_dict_get(ost->encoder_opts, "chroma_sample_location", NULL, 0))
+                enc_ctx->chroma_sample_location = frame->chroma_location;
+        }
+
         enc_ctx->framerate = ost->frame_rate;
 
         ost->st->avg_frame_rate = ost->frame_rate;
@@ -3425,7 +3444,8 @@ static int init_output_stream_encode(OutputStream *ost)
     return 0;
 }
 
-static int init_output_stream(OutputStream *ost, char *error, int error_len)
+static int init_output_stream(OutputStream *ost, AVFrame *frame,
+                              char *error, int error_len)
 {
     int ret = 0;
 
@@ -3434,7 +3454,7 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len)
         AVCodecContext *dec = NULL;
         InputStream *ist;
 
-        ret = init_output_stream_encode(ost);
+        ret = init_output_stream_encode(ost, frame);
         if (ret < 0)
             return ret;
 
@@ -3682,7 +3702,7 @@ static int transcode_init(void)
              output_streams[i]->enc_ctx->codec_type == AVMEDIA_TYPE_AUDIO))
             continue;
 
-        ret = init_output_stream_wrapper(output_streams[i], 0);
+        ret = init_output_stream_wrapper(output_streams[i], NULL, 0);
         if (ret < 0)
             goto dump_format;
     }
-- 
2.26.2



More information about the ffmpeg-devel mailing list