diff mbox series

[FFmpeg-devel,v2,2/6] avformat/mux: factorize interleaved write_packet

Message ID 20200418191847.25815-2-cus@passwd.hu
State Superseded
Headers show
Series [FFmpeg-devel,v2,1/6] avformat: only allow a single bitstream filter when muxing | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Marton Balint April 18, 2020, 7:18 p.m. UTC
Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/mux.c | 52 +++++++++++++++++++++-------------------------------
 1 file changed, 21 insertions(+), 31 deletions(-)

Comments

Andreas Rheinhardt April 26, 2020, 12:44 a.m. UTC | #1
Marton Balint:
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
>  libavformat/mux.c | 52 +++++++++++++++++++++-------------------------------
>  1 file changed, 21 insertions(+), 31 deletions(-)
> 
> diff --git a/libavformat/mux.c b/libavformat/mux.c
> index 5209c84f40..90faf51768 100644
> --- a/libavformat/mux.c
> +++ b/libavformat/mux.c
> @@ -1195,6 +1195,25 @@ static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, in
>          return ff_interleave_packet_per_dts(s, out, in, flush);
>  }
>  
> +static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush)
> +{
> +    for (;; ) {
> +        AVPacket opkt;
> +        int ret = interleave_packet(s, &opkt, pkt, flush);
> +        if (ret <= 0)
> +            return ret;
> +
> +        pkt = NULL;
> +
> +        ret = write_packet(s, &opkt);
> +
> +        av_packet_unref(&opkt);
> +
> +        if (ret < 0)
> +            return ret;
> +    }
> +}
> +
>  int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
>  {
>      int ret, flush = 0;
> @@ -1229,22 +1248,8 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
>          av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
>          flush = 1;
>      }
> +    return interleaved_write_packet(s, pkt, flush);
>  
> -    for (;; ) {
> -        AVPacket opkt;
> -        int ret = interleave_packet(s, &opkt, pkt, flush);
> -        if (ret <= 0)
> -            return ret;
> -
> -        pkt = NULL;
> -
> -        ret = write_packet(s, &opkt);
> -
> -        av_packet_unref(&opkt);
> -
> -        if (ret < 0)
> -            return ret;
> -    }
>  fail:
>      av_packet_unref(pkt);
>      return ret;
> @@ -1254,23 +1259,8 @@ int av_write_trailer(AVFormatContext *s)
>  {
>      int ret, i;
>  
> -    for (;; ) {
> -        AVPacket pkt;
> -        ret = interleave_packet(s, &pkt, NULL, 1);
> -        if (ret < 0)
> -            goto fail;
> -        if (!ret)
> -            break;
> -
> -        ret = write_packet(s, &pkt);
> -
> -        av_packet_unref(&pkt);
> +    ret = interleaved_write_packet(s, NULL, 1);
>  
> -        if (ret < 0)
> -            goto fail;
> -    }
> -
> -fail:
>      if (s->oformat->write_trailer) {
>          if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
>              avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER);
> 
Looks good on its own; yet I wonder about the placement of
interleaved_write_packet(). The current one requires a forward
declaration in the next patch.

- Andreas
diff mbox series

Patch

diff --git a/libavformat/mux.c b/libavformat/mux.c
index 5209c84f40..90faf51768 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -1195,6 +1195,25 @@  static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, in
         return ff_interleave_packet_per_dts(s, out, in, flush);
 }
 
+static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush)
+{
+    for (;; ) {
+        AVPacket opkt;
+        int ret = interleave_packet(s, &opkt, pkt, flush);
+        if (ret <= 0)
+            return ret;
+
+        pkt = NULL;
+
+        ret = write_packet(s, &opkt);
+
+        av_packet_unref(&opkt);
+
+        if (ret < 0)
+            return ret;
+    }
+}
+
 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
 {
     int ret, flush = 0;
@@ -1229,22 +1248,8 @@  int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
         av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
         flush = 1;
     }
+    return interleaved_write_packet(s, pkt, flush);
 
-    for (;; ) {
-        AVPacket opkt;
-        int ret = interleave_packet(s, &opkt, pkt, flush);
-        if (ret <= 0)
-            return ret;
-
-        pkt = NULL;
-
-        ret = write_packet(s, &opkt);
-
-        av_packet_unref(&opkt);
-
-        if (ret < 0)
-            return ret;
-    }
 fail:
     av_packet_unref(pkt);
     return ret;
@@ -1254,23 +1259,8 @@  int av_write_trailer(AVFormatContext *s)
 {
     int ret, i;
 
-    for (;; ) {
-        AVPacket pkt;
-        ret = interleave_packet(s, &pkt, NULL, 1);
-        if (ret < 0)
-            goto fail;
-        if (!ret)
-            break;
-
-        ret = write_packet(s, &pkt);
-
-        av_packet_unref(&pkt);
+    ret = interleaved_write_packet(s, NULL, 1);
 
-        if (ret < 0)
-            goto fail;
-    }
-
-fail:
     if (s->oformat->write_trailer) {
         if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
             avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER);