[FFmpeg-devel] [PATCH] avcodec/encode: ensure encoders don't return empty packets

James Almer jamrial at gmail.com
Tue Apr 13 22:16:46 EEST 2021


The current checks ensured that if they contained data, it was refcounted, but
then wrongly assumed that side data was present if there was no data, instead
of checking for it.

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/encode.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 9a4140f91a..aac32cd0f4 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -259,11 +259,6 @@ end:
         av_frame_unref(frame);
     }
 
-    if (got_packet)
-        // Encoders must always return ref-counted buffers.
-        // Side-data only packets have no data and can be not ref-counted.
-        av_assert0(!avpkt->data || avpkt->buf);
-
     return ret;
 }
 
@@ -301,14 +296,15 @@ static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt
         ret = avctx->codec->receive_packet(avctx, avpkt);
         if (ret < 0)
             av_packet_unref(avpkt);
-        else
-            // Encoders must always return ref-counted buffers.
-            // Side-data only packets have no data and can be not ref-counted.
-            av_assert0(!avpkt->data || avpkt->buf);
     } else
         ret = encode_simple_receive_packet(avctx, avpkt);
 
-    if (ret == AVERROR_EOF)
+    if (!ret) {
+        // Encoders must always return ref-counted buffers.
+        av_assert0(!avpkt->data || avpkt->buf);
+        // Side-data only packets have no data and can be not ref-counted.
+        av_assert0(avpkt->data || avpkt->side_data);
+    } else if (ret == AVERROR_EOF)
         avci->draining_done = 1;
 
     return ret;
-- 
2.31.1



More information about the ffmpeg-devel mailing list