diff mbox series

[FFmpeg-devel,2/6] avformat/mux: Fix leaks on error when writing noninterleaved uncoded frames

Message ID 20200411211955.20843-2-andreas.rheinhardt@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel,1/6] avformat/mux: Make uncoded frames av_packet_unref() compatible | expand


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

Commit Message

Andreas Rheinhardt April 11, 2020, 9:19 p.m. UTC
If writing uncoded frames in noninterleaved mode fails at the preparatory
steps (i.e. before it reaches write_packet()), the packet would not be
unreferenced and the frame would leak. This is fixed by unreferencing
the packet in write_uncoded_frame_internal() instead.

This also makes it possible to remove the unreferencing in
write_packet() itself: In noninterleaved mode frames are now freed in
write_uncoded_frame_internal(), while they are freed in interleaved
mode when their containing packet gets unreferenced (like normal

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
After this commit the best way to send uncoded frames to muxers
supporting them would be via the normal AVOutputFormat.write_packet(),
because that way ownership of the AVFrame can be passed. Then
write_packet() in mux.c could be further simplified. (This of course
only applies if the uncoded frame API is kept.)

 libavformat/mux.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
diff mbox series


diff --git a/libavformat/mux.c b/libavformat/mux.c
index 712ba0c319..e86214d585 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -744,7 +744,6 @@  static int write_packet(AVFormatContext *s, AVPacket *pkt)
         av_assert0(pkt->size == sizeof(*frame));
         ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, &frame, 0);
         av_assert0((void*)frame == pkt->data);
-        av_packet_unref(pkt);
     } else {
         ret = s->oformat->write_packet(s, pkt);
@@ -1320,6 +1319,7 @@  static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
                                         AVFrame *frame, int interleaved)
     AVPacket pkt, *pktp;
+    int ret;
     if (!s->oformat->write_uncoded_frame) {
@@ -1349,8 +1349,11 @@  static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
         pkt.flags |= AV_PKT_FLAG_UNCODED_FRAME;
-    return interleaved ? av_interleaved_write_frame(s, pktp) :
-                         av_write_frame(s, pktp);
+    ret = interleaved ? av_interleaved_write_frame(s, pktp) :
+                        av_write_frame(s, pktp);
+    if (pktp)
+        av_packet_unref(pktp);
+    return ret;
 int av_write_uncoded_frame(AVFormatContext *s, int stream_index,