diff mbox series

[FFmpeg-devel,14/22] fftools/ffmpeg: move freeing an output stream into a separate function

Message ID 20221014101548.3486-1-anton@khirnov.net
State Accepted
Commit 2dcedd9af8c37b1c4dbdabaaaf1fa7ce783fd446
Headers show
Series [FFmpeg-devel,01/13] fftools/ffmpeg_mux: do not unref a NULL packet | expand

Checks

Context Check Description
yinshiyou/make_fate_loongarch64 success Make fate finished
yinshiyou/make_loongarch64 warning New warnings during build
andriy/make_fate_x86 success Make fate finished
andriy/make_x86 warning New warnings during build

Commit Message

Anton Khirnov Oct. 14, 2022, 10:15 a.m. UTC
---
 fftools/ffmpeg.c | 69 ++++++++++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 32 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 46d2912d07..afbfff6ba2 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -504,6 +504,41 @@  static int decode_interrupt_cb(void *ctx)
 
 const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
 
+static void ost_free(OutputStream **post)
+{
+    OutputStream *ost = *post;
+
+    if (!ost)
+        return;
+
+        av_bsf_free(&ost->bsf_ctx);
+
+        av_frame_free(&ost->filtered_frame);
+        av_frame_free(&ost->sq_frame);
+        av_frame_free(&ost->last_frame);
+        av_packet_free(&ost->pkt);
+        av_dict_free(&ost->encoder_opts);
+
+        av_freep(&ost->forced_keyframes);
+        av_expr_free(ost->forced_keyframes_pexpr);
+        av_freep(&ost->avfilter);
+        av_freep(&ost->logfile_prefix);
+
+#if FFMPEG_OPT_MAP_CHANNEL
+        av_freep(&ost->audio_channels_map);
+        ost->audio_channels_mapped = 0;
+#endif
+
+        av_dict_free(&ost->sws_dict);
+        av_dict_free(&ost->swr_opts);
+
+        if (ost->enc_ctx)
+            av_freep(&ost->enc_ctx->stats_in);
+        avcodec_free_context(&ost->enc_ctx);
+
+    av_freep(post);
+}
+
 static void ffmpeg_cleanup(int ret)
 {
     int i, j;
@@ -557,39 +592,9 @@  static void ffmpeg_cleanup(int ret)
     for (i = 0; i < nb_output_files; i++)
         of_close(&output_files[i]);
 
-    for (i = 0; i < nb_output_streams; i++) {
-        OutputStream *ost = output_streams[i];
-
-        if (!ost)
-            continue;
-
-        av_bsf_free(&ost->bsf_ctx);
-
-        av_frame_free(&ost->filtered_frame);
-        av_frame_free(&ost->sq_frame);
-        av_frame_free(&ost->last_frame);
-        av_packet_free(&ost->pkt);
-        av_dict_free(&ost->encoder_opts);
-
-        av_freep(&ost->forced_keyframes);
-        av_expr_free(ost->forced_keyframes_pexpr);
-        av_freep(&ost->avfilter);
-        av_freep(&ost->logfile_prefix);
-
-#if FFMPEG_OPT_MAP_CHANNEL
-        av_freep(&ost->audio_channels_map);
-        ost->audio_channels_mapped = 0;
-#endif
-
-        av_dict_free(&ost->sws_dict);
-        av_dict_free(&ost->swr_opts);
-
-        if (ost->enc_ctx)
-            av_freep(&ost->enc_ctx->stats_in);
-        avcodec_free_context(&ost->enc_ctx);
+    for (i = 0; i < nb_output_streams; i++)
+        ost_free(&output_streams[i]);
 
-        av_freep(&output_streams[i]);
-    }
     free_input_threads();
     for (i = 0; i < nb_input_files; i++) {
         avformat_close_input(&input_files[i]->ctx);