[FFmpeg-cvslog] avformat/mux: factorize header writing code

Marton Balint git at videolan.org
Mon Jun 13 22:45:00 CEST 2016


ffmpeg | branch: master | Marton Balint <cus at passwd.hu> | Sat Jun 11 20:02:02 2016 +0200| [9da27fb579586377ee7f764c5de04fda6740c486] | committer: Marton Balint

avformat/mux: factorize header writing code

Reviewed-by: Michael Niedermayer <michael at niedermayer.cc>
Signed-off-by: Marton Balint <cus at passwd.hu>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=9da27fb579586377ee7f764c5de04fda6740c486
---

 libavformat/mux.c |   44 ++++++++++++++++++++++----------------------
 1 file changed, 22 insertions(+), 22 deletions(-)

diff --git a/libavformat/mux.c b/libavformat/mux.c
index bef230f..08ed940 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -473,6 +473,21 @@ static int init_pts(AVFormatContext *s)
     return 0;
 }
 
+static int write_header_internal(AVFormatContext *s)
+{
+    if (s->oformat->write_header) {
+        int ret = s->oformat->write_header(s);
+        if (ret >= 0 && s->pb && s->pb->error < 0)
+            ret = s->pb->error;
+        if (ret < 0)
+            return ret;
+        if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
+            avio_flush(s->pb);
+    }
+    s->internal->header_written = 1;
+    return 0;
+}
+
 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
 {
     int ret = 0;
@@ -480,15 +495,10 @@ int avformat_write_header(AVFormatContext *s, AVDictionary **options)
     if ((ret = init_muxer(s, options)) < 0)
         return ret;
 
-    if (s->oformat->write_header && !s->oformat->check_bitstream) {
-        ret = s->oformat->write_header(s);
-        if (ret >= 0 && s->pb && s->pb->error < 0)
-            ret = s->pb->error;
+    if (!s->oformat->check_bitstream) {
+        ret = write_header_internal(s);
         if (ret < 0)
             goto fail;
-        if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
-            avio_flush(s->pb);
-        s->internal->header_written = 1;
     }
 
     if ((ret = init_pts(s)) < 0)
@@ -702,15 +712,10 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
 
     did_split = av_packet_split_side_data(pkt);
 
-    if (!s->internal->header_written && s->oformat->write_header) {
-        ret = s->oformat->write_header(s);
-        if (ret >= 0 && s->pb && s->pb->error < 0)
-            ret = s->pb->error;
+    if (!s->internal->header_written) {
+        ret = write_header_internal(s);
         if (ret < 0)
             goto fail;
-        if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
-            avio_flush(s->pb);
-        s->internal->header_written = 1;
     }
 
     if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
@@ -1146,19 +1151,14 @@ int av_write_trailer(AVFormatContext *s)
             goto fail;
     }
 
-    if (!s->internal->header_written && s->oformat->write_header) {
-        ret = s->oformat->write_header(s);
-        if (ret >= 0 && s->pb && s->pb->error < 0)
-            ret = s->pb->error;
+    if (!s->internal->header_written) {
+        ret = write_header_internal(s);
         if (ret < 0)
             goto fail;
-        if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
-            avio_flush(s->pb);
-        s->internal->header_written = 1;
     }
 
 fail:
-    if ((s->internal->header_written || !s->oformat->write_header) && s->oformat->write_trailer)
+    if (s->internal->header_written && s->oformat->write_trailer)
         if (ret >= 0) {
         ret = s->oformat->write_trailer(s);
         } else {



More information about the ffmpeg-cvslog mailing list