diff mbox series

[FFmpeg-devel,4/8] avformat/mux: factorize writing a packet

Message ID 20200328181515.5333-4-cus@passwd.hu
State Accepted
Headers show
Series [FFmpeg-devel,1/8] fftools/ffmpeg: also flush encoders which have a variable frame size
Related show

Checks

Context Check Description
andriy/ffmpeg-patchwork pending
andriy/ffmpeg-patchwork success Applied patch
andriy/ffmpeg-patchwork success Configure finished
andriy/ffmpeg-patchwork success Make finished
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Marton Balint March 28, 2020, 6:15 p.m. UTC
In preparation for N:M bsf support.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/mux.c | 82 +++++++++++++++++++++++++++++++------------------------
 1 file changed, 47 insertions(+), 35 deletions(-)

Comments

Andreas Rheinhardt March 31, 2020, 12:46 p.m. UTC | #1
Marton Balint:
> In preparation for N:M bsf support.
> 
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
>  libavformat/mux.c | 82 +++++++++++++++++++++++++++++++------------------------
>  1 file changed, 47 insertions(+), 35 deletions(-)
> 
> diff --git a/libavformat/mux.c b/libavformat/mux.c
> index f68f962a92..8c2d6a8060 100644
> --- a/libavformat/mux.c
> +++ b/libavformat/mux.c
> @@ -883,6 +883,49 @@ static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) {
>      return 1;
>  }
>  
> +static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush);
> +
> +static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
> +{
> +    int ret;
> +
> +    if (s->debug & FF_FDEBUG_TS)
> +        av_log(s, AV_LOG_DEBUG, "%s size:%d dts:%s pts:%s\n", __FUNCTION__,
> +               pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
> +
> +#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
> +    if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
> +        return ret;
> +#endif
> +
> +    if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
> +        return AVERROR(EINVAL);
> +
> +    if (interleaved)
> +        return interleaved_write_packet(s, pkt, 0);
> +    else
> +        return write_packet(s, pkt);
> +}
> +
> +static int write_packets_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
> +{
> +    int ret = do_packet_auto_bsf(s, pkt);
> +    if (ret == 0)
> +        return 0;
> +    else if (ret < 0)
> +        goto fail;
> +
> +    ret = write_packet_common(s, st, pkt, interleaved);
> +    if (ret < 0)
> +        goto fail;
> +    return ret;
> +
> +fail:
> +    if (interleaved)
> +        av_packet_unref(pkt);
> +    return ret;
> +}
> +
>  int av_write_frame(AVFormatContext *s, AVPacket *pkt)
>  {
>      int ret;
> @@ -902,18 +945,7 @@ int av_write_frame(AVFormatContext *s, AVPacket *pkt)
>          return 1;
>      }
>  
> -    ret = do_packet_auto_bsf(s, pkt);
> -    if (ret <= 0)
> -        return ret;
> -
> -#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
> -    ret = compute_muxer_pkt_fields(s, s->streams[pkt->stream_index], pkt);
> -
> -    if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
> -        return ret;
> -#endif
> -
> -    return write_packet(s, pkt);
> +    return write_packets_common(s, s->streams[pkt->stream_index], pkt, 0/*non-interleaved*/);
>  }
>  
>  #define CHUNK_START 0x1000
> @@ -1211,7 +1243,7 @@ static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush
>  
>  int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
>  {
> -    int ret, flush = 0;
> +    int ret;
>  
>      ret = prepare_input_packet(s, pkt);
>      if (ret < 0)
> @@ -1219,31 +1251,11 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
>  
>      if (pkt) {
>          AVStream *st = s->streams[pkt->stream_index];
> -
> -        ret = do_packet_auto_bsf(s, pkt);
> -        if (ret == 0)
> -            return 0;
> -        else if (ret < 0)
> -            goto fail;
> -
> -        if (s->debug & FF_FDEBUG_TS)
> -            av_log(s, AV_LOG_DEBUG, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
> -                pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
> -
> -#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
> -        if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
> -            goto fail;
> -#endif
> -
> -        if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
> -            ret = AVERROR(EINVAL);
> -            goto fail;
> -        }
> +        return write_packets_common(s, st, pkt, 1/*interleaved*/);
>      } else {
>          av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
> -        flush = 1;
> +        return interleaved_write_packet(s, NULL, 1/*flush*/);
>      }
> -    return interleaved_write_packet(s, pkt, flush);
>  
>  fail:
>      av_packet_unref(pkt);
> 
This and my patch [1] work very well together: It allows you to move the
prepare_input_packet() into write_packets_common().

- Andreas

[1]: https://ffmpeg.org/pipermail/ffmpeg-devel/2020-March/259383.html
diff mbox series

Patch

diff --git a/libavformat/mux.c b/libavformat/mux.c
index f68f962a92..8c2d6a8060 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -883,6 +883,49 @@  static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) {
     return 1;
 }
 
+static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush);
+
+static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
+{
+    int ret;
+
+    if (s->debug & FF_FDEBUG_TS)
+        av_log(s, AV_LOG_DEBUG, "%s size:%d dts:%s pts:%s\n", __FUNCTION__,
+               pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
+
+#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
+    if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
+        return ret;
+#endif
+
+    if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
+        return AVERROR(EINVAL);
+
+    if (interleaved)
+        return interleaved_write_packet(s, pkt, 0);
+    else
+        return write_packet(s, pkt);
+}
+
+static int write_packets_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
+{
+    int ret = do_packet_auto_bsf(s, pkt);
+    if (ret == 0)
+        return 0;
+    else if (ret < 0)
+        goto fail;
+
+    ret = write_packet_common(s, st, pkt, interleaved);
+    if (ret < 0)
+        goto fail;
+    return ret;
+
+fail:
+    if (interleaved)
+        av_packet_unref(pkt);
+    return ret;
+}
+
 int av_write_frame(AVFormatContext *s, AVPacket *pkt)
 {
     int ret;
@@ -902,18 +945,7 @@  int av_write_frame(AVFormatContext *s, AVPacket *pkt)
         return 1;
     }
 
-    ret = do_packet_auto_bsf(s, pkt);
-    if (ret <= 0)
-        return ret;
-
-#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
-    ret = compute_muxer_pkt_fields(s, s->streams[pkt->stream_index], pkt);
-
-    if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
-        return ret;
-#endif
-
-    return write_packet(s, pkt);
+    return write_packets_common(s, s->streams[pkt->stream_index], pkt, 0/*non-interleaved*/);
 }
 
 #define CHUNK_START 0x1000
@@ -1211,7 +1243,7 @@  static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush
 
 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
 {
-    int ret, flush = 0;
+    int ret;
 
     ret = prepare_input_packet(s, pkt);
     if (ret < 0)
@@ -1219,31 +1251,11 @@  int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
 
     if (pkt) {
         AVStream *st = s->streams[pkt->stream_index];
-
-        ret = do_packet_auto_bsf(s, pkt);
-        if (ret == 0)
-            return 0;
-        else if (ret < 0)
-            goto fail;
-
-        if (s->debug & FF_FDEBUG_TS)
-            av_log(s, AV_LOG_DEBUG, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
-                pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
-
-#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
-        if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
-            goto fail;
-#endif
-
-        if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
-            ret = AVERROR(EINVAL);
-            goto fail;
-        }
+        return write_packets_common(s, st, pkt, 1/*interleaved*/);
     } else {
         av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
-        flush = 1;
+        return interleaved_write_packet(s, NULL, 1/*flush*/);
     }
-    return interleaved_write_packet(s, pkt, flush);
 
 fail:
     av_packet_unref(pkt);