diff mbox series

[FFmpeg-devel,12/34] avformat/mux: Don't use stack packet when writing interleaved packets

Message ID AM7PR03MB66609FBD6E29DB53BB3060398FD29@AM7PR03MB6660.eurprd03.prod.outlook.com
State New
Headers show
Series [FFmpeg-devel,01/11] avformat/mux: Sanitize packets without data and side-data
Related show

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

Commit Message

Andreas Rheinhardt Sept. 6, 2021, 2:27 a.m. UTC
Currently the interleave_packet functions use a packet for
a new packet to be interleaved (may be NULL if there is none) and
a packet for output; said packet is always a stack packet in
interleaved_write_packet(). But all the interleave_packet functions
in use first move the packet to the packet list and then check whether
a packet can be returned, i.e. the effective lifetime of the new packet
ends before the packet for output is touched.

So one can use one packet both for input and output by adding a new
parameter that indicates whether there is a packet to add to the packet
list; there is just one complication: In case the muxer is flushed,
there is no packet available. This can be solved by reusing one of
the packets from AVFormatInternal. They are currently unused when
flushing in av_interleaved_write_frame().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/avformat.h | 18 ++++++++++++++---
 libavformat/gxfenc.c   |  7 ++++---
 libavformat/internal.h | 19 ++++++-----------
 libavformat/mux.c      | 46 ++++++++++++++++++------------------------
 libavformat/mxfenc.c   |  9 +++++----
 5 files changed, 50 insertions(+), 49 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 9b560c15be..7262ce8c5b 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -553,9 +553,21 @@  typedef struct AVOutputFormat {
     /**
      * A format-specific function for interleavement.
      * If unset, packets will be interleaved by dts.
-     */
-    int (*interleave_packet)(struct AVFormatContext *, AVPacket *out,
-                             AVPacket *in, int flush);
+     *
+     * @param s           An AVFormatContext for output. pkt will be added to
+     *                    resp. taken from its packet buffer.
+     * @param[in,out] pkt A packet to be interleaved if has_packet is set;
+     *                    also used to return packets. If no packet is returned
+     *                    (e.g. on error), pkt is blank on return.
+     * @param flush       1 if no further packets are available as input and
+     *                    all remaining packets should be output.
+     * @param has_packet  If set, pkt contains a packet to be interleaved
+     *                    on input; otherwise pkt is blank on input.
+     * @return 1 if a packet was output, 0 if no packet could be output,
+     *         < 0 if an error occurred
+     */
+    int (*interleave_packet)(struct AVFormatContext *s, AVPacket *pkt,
+                             int flush, int has_packet);
     /**
      * Test if the given codec can be stored in this container.
      *
diff --git a/libavformat/gxfenc.c b/libavformat/gxfenc.c
index 1a80ecb603..70a911673f 100644
--- a/libavformat/gxfenc.c
+++ b/libavformat/gxfenc.c
@@ -1008,10 +1008,11 @@  static int gxf_compare_field_nb(AVFormatContext *s, const AVPacket *next,
         (field_nb[1] == field_nb[0] && sc[1]->order > sc[0]->order);
 }
 
-static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
+static int gxf_interleave_packet(AVFormatContext *s, AVPacket *pkt,
+                                 int flush, int has_packet)
 {
     int ret;
-    if (pkt) {
+    if (has_packet) {
         AVStream *st = s->streams[pkt->stream_index];
         GXFStreamContext *sc = st->priv_data;
         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
@@ -1022,7 +1023,7 @@  static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pk
         if ((ret = ff_interleave_add_packet(s, pkt, gxf_compare_field_nb)) < 0)
             return ret;
     }
-    return ff_interleave_packet_per_dts(s, out, NULL, flush);
+    return ff_interleave_packet_per_dts(s, pkt, flush, 0);
 }
 
 const AVOutputFormat ff_gxf_muxer = {
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 9d7312c0e2..c3d0ff6b88 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -102,7 +102,8 @@  struct AVFormatInternal {
     struct PacketList *parse_queue_end;
     /**
      * The generic code uses this as a temporary packet
-     * to parse packets; it may also be used for other means
+     * to parse packets or for muxing, especially flushing.
+     * For demuxers, it may also be used for other means
      * for short periods that are guaranteed not to overlap
      * with calls to av_read_frame() (or ff_read_packet())
      * or with each other.
@@ -701,18 +702,10 @@  int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb,
 
 /**
  * Interleave an AVPacket per dts so it can be muxed.
- *
- * @param s   an AVFormatContext for output. pkt resp. out will be added to
- *            resp. taken from its packet buffer.
- * @param out the interleaved packet will be output here
- * @param pkt the input packet; will be blank on return if not NULL
- * @param flush 1 if no further packets are available as input and all
- *              remaining packets should be output
- * @return 1 if a packet was output, 0 if no packet could be output
- *         (in which case out may be uninitialized), < 0 if an error occurred
- */
-int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
-                                 AVPacket *pkt, int flush);
+ * See the documentation of AVOutputFormat.interleave_packet for details.
+ */
+int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *pkt,
+                                 int flush, int has_packet);
 
 void ff_free_stream(AVFormatContext *s, AVStream *st);
 
diff --git a/libavformat/mux.c b/libavformat/mux.c
index dbcd3835c2..ea298c1221 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -898,8 +898,8 @@  static int interleave_compare_dts(AVFormatContext *s, const AVPacket *next,
     return comp > 0;
 }
 
-int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
-                                 AVPacket *pkt, int flush)
+int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *pkt,
+                                 int flush, int has_packet)
 {
     PacketList *pktl;
     int stream_count = 0;
@@ -907,7 +907,7 @@  int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
     int i, ret;
     int eof = flush;
 
-    if (pkt) {
+    if (has_packet) {
         if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
             return ret;
     }
@@ -999,8 +999,8 @@  int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
     if (stream_count && flush) {
         AVStream *st;
         pktl = s->internal->packet_buffer;
-        *out = pktl->pkt;
-        st   = s->streams[out->stream_index];
+        *pkt = pktl->pkt;
+        st   = s->streams[pkt->stream_index];
 
         s->internal->packet_buffer = pktl->next;
         if (!s->internal->packet_buffer)
@@ -1045,20 +1045,16 @@  const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream)
 }
 
 /**
- * Interleave an AVPacket correctly so it can be muxed.
- * @param out the interleaved packet will be output here
- * @param in the input packet; will always be blank on return if not NULL
- * @param flush 1 if no further packets are available as input and all
- *              remaining packets should be output
- * @return 1 if a packet was output, 0 if no packet could be output,
- *         < 0 if an error occurred
+ * A wrapper around AVOutputFormat.interleave_packet.
+ * See its documentation for details.
  */
-static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
+static int interleave_packet(AVFormatContext *s, AVPacket *pkt,
+                             int flush, int has_packet)
 {
     if (s->oformat->interleave_packet) {
-        return s->oformat->interleave_packet(s, out, in, flush);
+        return s->oformat->interleave_packet(s, pkt, flush, has_packet);
     } else
-        return ff_interleave_packet_per_dts(s, out, in, flush);
+        return ff_interleave_packet_per_dts(s, pkt, flush, has_packet);
 }
 
 static int check_bitstream(AVFormatContext *s, AVStream *st, AVPacket *pkt)
@@ -1080,20 +1076,18 @@  static int check_bitstream(AVFormatContext *s, AVStream *st, AVPacket *pkt)
     return 1;
 }
 
-static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush)
+static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt,
+                                    int flush, int has_packet)
 {
     for (;; ) {
-        AVPacket opkt;
-        int ret = interleave_packet(s, &opkt, pkt, flush);
+        int ret = interleave_packet(s, pkt, flush, has_packet);
         if (ret <= 0)
             return ret;
 
-        pkt = NULL;
-
-        ret = write_packet(s, &opkt);
-
-        av_packet_unref(&opkt);
+        has_packet = 0;
 
+        ret = write_packet(s, pkt);
+        av_packet_unref(pkt);
         if (ret < 0)
             return ret;
     }
@@ -1117,7 +1111,7 @@  static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt,
     if (interleaved) {
         if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
             return AVERROR(EINVAL);
-        return interleaved_write_packet(s, pkt, 0);
+        return interleaved_write_packet(s, pkt, 0, 1);
     } else {
         return write_packet(s, pkt);
     }
@@ -1237,7 +1231,7 @@  int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
         return ret;
     } else {
         av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
-        return interleaved_write_packet(s, NULL, 1/*flush*/);
+        return interleaved_write_packet(s, s->internal->parse_pkt, 1/*flush*/, 0);
     }
 }
 
@@ -1256,7 +1250,7 @@  int av_write_trailer(AVFormatContext *s)
                 ret = ret1;
         }
     }
-    ret1 = interleaved_write_packet(s, NULL, 1);
+    ret1 = interleaved_write_packet(s, pkt, 1, 0);
     if (ret >= 0)
         ret = ret1;
 
diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 229817dba6..dd7b6314bc 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -3098,7 +3098,7 @@  static void mxf_deinit(AVFormatContext *s)
     }
 }
 
-static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
+static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, int flush)
 {
     int i, stream_count = 0;
 
@@ -3162,16 +3162,17 @@  static int mxf_compare_timestamps(AVFormatContext *s, const AVPacket *next,
         (next->dts == pkt->dts && sc->order < sc2->order);
 }
 
-static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
+static int mxf_interleave(AVFormatContext *s, AVPacket *pkt,
+                          int flush, int has_packet)
 {
     int ret;
-    if (pkt) {
+    if (has_packet) {
         MXFStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
         pkt->pts = pkt->dts = sc->pkt_cnt++;
         if ((ret = ff_interleave_add_packet(s, pkt, mxf_compare_timestamps)) < 0)
             return ret;
     }
-    return mxf_interleave_get_packet(s, out, NULL, flush);
+    return mxf_interleave_get_packet(s, pkt, flush);
 }
 
 #define MXF_COMMON_OPTIONS \