diff mbox series

[FFmpeg-devel,46/49] fftools/ffmpeg: do not write the output file header from init_output_stream()

Message ID 20220404113037.13070-47-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/49] fftools/ffmpeg: drop an obsolete hack | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 fail Make fate failed

Commit Message

Anton Khirnov April 4, 2022, 11:30 a.m. UTC
---
 fftools/ffmpeg.c     |   4 -
 fftools/ffmpeg.h     |   2 -
 fftools/ffmpeg_mux.c | 186 ++++++++++++++++++++++---------------------
 3 files changed, 96 insertions(+), 96 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 0cab73ad7a..2dff41e57f 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3230,10 +3230,6 @@  static int init_output_stream(OutputStream *ost, AVFrame *frame,
 
     ost->initialized = 1;
 
-    ret = of_check_init(output_files[ost->file_index]);
-    if (ret < 0)
-        return ret;
-
     return ret;
 }
 
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index fef66c2325..14b98909d0 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -699,8 +699,6 @@  int hwaccel_decode_init(AVCodecContext *avctx);
 
 int of_muxer_init(OutputFile *of, AVFormatContext *fc,
                   AVDictionary *opts, int64_t limit_filesize);
-/* open the muxer when all the streams are initialized */
-int of_check_init(OutputFile *of);
 int of_write_trailer(OutputFile *of);
 void of_close(OutputFile **pof);
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 2fd131d1bc..6ca9a51dd6 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -63,6 +63,59 @@  struct Muxer {
 
 static int want_sdp = 1;
 
+static int print_sdp(void)
+{
+    char sdp[16384];
+    int i;
+    int j, ret;
+    AVIOContext *sdp_pb;
+    AVFormatContext **avc;
+
+    for (i = 0; i < nb_output_files; i++) {
+        if (!output_files[i]->mux->header_written)
+            return 0;
+    }
+
+    avc = av_malloc_array(nb_output_files, sizeof(*avc));
+    if (!avc)
+        return AVERROR(ENOMEM);
+    for (i = 0, j = 0; i < nb_output_files; i++) {
+        if (!strcmp(output_files[i]->format->name, "rtp")) {
+            avc[j] = output_files[i]->mux->fc;
+            j++;
+        }
+    }
+
+    if (!j) {
+        av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
+
+    ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
+    if (ret < 0)
+        goto fail;
+
+    if (!sdp_filename) {
+        printf("SDP:\n%s\n", sdp);
+        fflush(stdout);
+    } else {
+        ret = avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL);
+        if (ret < 0) {
+            av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename);
+            goto fail;
+        }
+
+        avio_print(sdp_pb, sdp);
+        avio_closep(&sdp_pb);
+        av_freep(&sdp_filename);
+    }
+
+fail:
+    av_freep(&avc);
+    return ret;
+}
+
 static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
 {
     const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
@@ -273,92 +326,8 @@  static int submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
     return ret;
 }
 
-int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof)
-{
-    if (ost->sq_idx_mux >= 0) {
-        int ret = sq_send(of->sq_mux, ost->sq_idx_mux,
-                          SQPKT(eof ? NULL: pkt));
-        if (ret < 0) {
-            av_packet_unref(pkt);
-            if (ret == AVERROR_EOF) {
-                ost->finished |= MUXER_FINISHED;
-                return 0;
-            } else
-                return ret;
-        }
-
-        while (1) {
-            ret = sq_receive(of->sq_mux, -1, SQPKT(pkt));
-            if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
-                return 0;
-            else if (ret < 0)
-                return ret;
-
-            ret = submit_packet(of, pkt, output_streams[of->ost_index + ret]);
-            if (ret < 0)
-                return ret;
-        }
-    } else if (!eof)
-        return submit_packet(of, pkt, ost);
-
-    return 0;
-}
-
-static int print_sdp(void)
-{
-    char sdp[16384];
-    int i;
-    int j, ret;
-    AVIOContext *sdp_pb;
-    AVFormatContext **avc;
-
-    for (i = 0; i < nb_output_files; i++) {
-        if (!output_files[i]->mux->header_written)
-            return 0;
-    }
-
-    avc = av_malloc_array(nb_output_files, sizeof(*avc));
-    if (!avc)
-        return AVERROR(ENOMEM);
-    for (i = 0, j = 0; i < nb_output_files; i++) {
-        if (!strcmp(output_files[i]->format->name, "rtp")) {
-            avc[j] = output_files[i]->mux->fc;
-            j++;
-        }
-    }
-
-    if (!j) {
-        av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
-        ret = AVERROR(EINVAL);
-        goto fail;
-    }
-
-    ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
-    if (ret < 0)
-        goto fail;
-
-    if (!sdp_filename) {
-        printf("SDP:\n%s\n", sdp);
-        fflush(stdout);
-    } else {
-        ret = avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL);
-        if (ret < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename);
-            goto fail;
-        }
-
-        avio_print(sdp_pb, sdp);
-        avio_closep(&sdp_pb);
-        av_freep(&sdp_filename);
-    }
-
-fail:
-    av_freep(&avc);
-    return ret;
-}
-
 /* open the muxer when all the streams are initialized */
-int of_check_init(OutputFile *of)
+static int check_write_header(OutputFile *of)
 {
     AVFormatContext *fc = of->mux->fc;
     int ret, i;
@@ -397,10 +366,6 @@  int of_check_init(OutputFile *of)
         OutputStream *ost = output_streams[of->ost_index + i];
         AVPacket *pkt;
 
-        /* try to improve muxing time_base (only possible if nothing has been written yet) */
-        if (!av_fifo_can_read(ms->muxing_queue))
-            ost->mux_timebase = ost->st->time_base;
-
         while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) {
             ms->muxing_queue_data_size -= pkt->size;
             ret = write_packet(of, ost, pkt);
@@ -413,6 +378,47 @@  int of_check_init(OutputFile *of)
     return 0;
 }
 
+int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof)
+{
+    int ret;
+
+    if (!of->mux->header_written) {
+        ret  = check_write_header(of);
+        if (ret < 0) {
+            av_packet_unref(pkt);
+            return ret;
+        }
+    }
+
+    if (ost->sq_idx_mux >= 0) {
+        ret = sq_send(of->sq_mux, ost->sq_idx_mux,
+                      SQPKT(eof ? NULL: pkt));
+        if (ret < 0) {
+            av_packet_unref(pkt);
+            if (ret == AVERROR_EOF) {
+                ost->finished |= MUXER_FINISHED;
+                return 0;
+            } else
+                return ret;
+        }
+
+        while (1) {
+            ret = sq_receive(of->sq_mux, -1, SQPKT(pkt));
+            if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
+                return 0;
+            else if (ret < 0)
+                return ret;
+
+            ret = submit_packet(of, pkt, output_streams[of->ost_index + ret]);
+            if (ret < 0)
+                return ret;
+        }
+    } else if (!eof)
+        return submit_packet(of, pkt, ost);
+
+    return 0;
+}
+
 int of_write_trailer(OutputFile *of)
 {
     AVFormatContext *fc = of->mux->fc;
@@ -540,7 +546,7 @@  int of_muxer_init(OutputFile *of, AVFormatContext *fc,
 
     /* write the header for files with no streams */
     if (of->format->flags & AVFMT_NOSTREAMS && fc->nb_streams == 0) {
-        ret = of_check_init(of);
+        ret = check_write_header(of);
         if (ret < 0)
             goto fail;
     }