diff mbox series

[FFmpeg-devel,19/28] ffmpeg_mux: split queuing packets into a separate function

Message ID 20220111095830.31542-19-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/28] ffmpeg: pass the muxer context explicitly to some functions | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished
andriy/make_aarch64_jetson success Make finished
andriy/make_fate_aarch64_jetson success Make fate finished
andriy/make_armv7_RPi4 success Make finished
andriy/make_fate_armv7_RPi4 success Make fate finished

Commit Message

Anton Khirnov Jan. 11, 2022, 9:58 a.m. UTC
---
 fftools/ffmpeg_mux.c | 72 +++++++++++++++++++++++++++-----------------
 1 file changed, 44 insertions(+), 28 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index f48031096d..4ab2279739 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -63,12 +63,50 @@  static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream,
     }
 }
 
+static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
+{
+    MuxStream *ms = &of->mux->streams[ost->index];
+    AVPacket *tmp_pkt;
+    int ret;
+
+    if (!av_fifo_space(ms->muxing_queue)) {
+        size_t cur_size = av_fifo_size(ms->muxing_queue);
+        unsigned int are_we_over_size =
+            (ms->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold;
+        size_t limit    = are_we_over_size ? ost->max_muxing_queue_size : INT_MAX;
+        size_t new_size = FFMIN(2 * cur_size, limit);
+
+        if (new_size <= cur_size) {
+            av_log(NULL, AV_LOG_ERROR,
+                   "Too many packets buffered for output stream %d:%d.\n",
+                   ost->file_index, ost->st->index);
+            return AVERROR(ENOSPC);
+        }
+        ret = av_fifo_realloc2(ms->muxing_queue, new_size);
+        if (ret < 0)
+            return ret;
+    }
+
+    ret = av_packet_make_refcounted(pkt);
+    if (ret < 0)
+        return ret;
+
+    tmp_pkt = av_packet_alloc();
+    if (!tmp_pkt)
+        return AVERROR(ENOMEM);
+
+    av_packet_move_ref(tmp_pkt, pkt);
+    ms->muxing_queue_data_size += tmp_pkt->size;
+    av_fifo_generic_write(ms->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
+
+    return 0;
+}
+
 void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
                      int unqueue)
 {
     AVFormatContext *s = of->ctx;
     AVStream *st = ost->st;
-    MuxStream *ms = &of->mux->streams[st->index];
     int ret;
 
     /*
@@ -87,35 +125,13 @@  void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
         ost->frame_number++;
     }
 
+    /* the muxer is not initialized yet, buffer the packet */
     if (!of->mux->header_written) {
-        AVPacket *tmp_pkt;
-        /* the muxer is not initialized yet, buffer the packet */
-        if (!av_fifo_space(ms->muxing_queue)) {
-            size_t cur_size = av_fifo_size(ms->muxing_queue);
-            unsigned int are_we_over_size =
-                (ms->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold;
-            size_t limit    = are_we_over_size ? ost->max_muxing_queue_size : INT_MAX;
-            size_t new_size = FFMIN(2 * cur_size, limit);
-
-            if (new_size <= cur_size) {
-                av_log(NULL, AV_LOG_ERROR,
-                       "Too many packets buffered for output stream %d:%d.\n",
-                       ost->file_index, ost->st->index);
-                exit_program(1);
-            }
-            ret = av_fifo_realloc2(ms->muxing_queue, new_size);
-            if (ret < 0)
-                exit_program(1);
-        }
-        ret = av_packet_make_refcounted(pkt);
-        if (ret < 0)
-            exit_program(1);
-        tmp_pkt = av_packet_alloc();
-        if (!tmp_pkt)
+        ret = queue_packet(of, ost, pkt);
+        if (ret < 0) {
+            av_packet_unref(pkt);
             exit_program(1);
-        av_packet_move_ref(tmp_pkt, pkt);
-        ms->muxing_queue_data_size += tmp_pkt->size;
-        av_fifo_generic_write(ms->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
+        }
         return;
     }