[FFmpeg-devel] [PATCH 03/10] avcodec/internal: Allow receive_frame codecs to use decode_simple pkt

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Mon Oct 11 18:36:42 EEST 2021


Decoders implementing the receive_frame API currently mostly use
stack packets to temporarily hold the packet they receive from
ff_decode_get_packet(). This role directly parallels the role of
in_pkt, the spare packet used in decode_simple_internal for the
decoders implementing the traditional decoding API. Said packet
is unused by the generic code for the decoders implementing the
receive_frame API, so allow them to use it to fulfill the function
it already fulfills for the traditional API for both APIs.

There is only one caveat in this: The packet is automatically
unreferenced in avcodec_flush_buffers(). But this is actually
positive as it means the decoders don't have to do this themselves
(in case the packet is preserved between receive_frame calls).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/avcodec.c  |  8 ++++----
 libavcodec/decode.c   |  3 +--
 libavcodec/internal.h | 14 +++++++++-----
 3 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index ff3d73e237..c00a9b2af8 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -181,11 +181,11 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     avci->buffer_frame = av_frame_alloc();
     avci->buffer_pkt = av_packet_alloc();
     avci->es.in_frame = av_frame_alloc();
-    avci->ds.in_pkt = av_packet_alloc();
+    avci->in_pkt = av_packet_alloc();
     avci->last_pkt_props = av_packet_alloc();
     avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
     if (!avci->buffer_frame || !avci->buffer_pkt          ||
-        !avci->es.in_frame  || !avci->ds.in_pkt           ||
+        !avci->es.in_frame  || !avci->in_pkt     ||
         !avci->last_pkt_props || !avci->pkt_props) {
         ret = AVERROR(ENOMEM);
         goto free_and_end;
@@ -408,7 +408,7 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
     av_fifo_reset(avci->pkt_props);
 
     av_frame_unref(avci->es.in_frame);
-    av_packet_unref(avci->ds.in_pkt);
+    av_packet_unref(avci->in_pkt);
 
     if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
         ff_thread_flush(avctx);
@@ -473,7 +473,7 @@ av_cold int avcodec_close(AVCodecContext *avctx)
         }
         av_packet_free(&avci->last_pkt_props);
 
-        av_packet_free(&avci->ds.in_pkt);
+        av_packet_free(&avci->in_pkt);
         av_frame_free(&avci->es.in_frame);
 
         av_buffer_unref(&avci->pool);
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 294c040716..c44724d150 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -296,8 +296,7 @@ static int64_t guess_correct_pts(AVCodecContext *ctx,
 static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
 {
     AVCodecInternal   *avci = avctx->internal;
-    DecodeSimpleContext *ds = &avci->ds;
-    AVPacket           *pkt = ds->in_pkt;
+    AVPacket     *const pkt = avci->in_pkt;
     int got_frame, actual_got_frame;
     int ret;
 
diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index b6180f15a5..a62f8dbd4e 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -112,10 +112,6 @@
 #   define STRIDE_ALIGN 8
 #endif
 
-typedef struct DecodeSimpleContext {
-    AVPacket *in_pkt;
-} DecodeSimpleContext;
-
 typedef struct EncodeSimpleContext {
     AVFrame *in_frame;
 } EncodeSimpleContext;
@@ -137,7 +133,15 @@ typedef struct AVCodecInternal {
 
     void *thread_ctx;
 
-    DecodeSimpleContext ds;
+    /**
+     * This packet is used to hold the packet given to decoders
+     * implementing the .decode API; it is unused by the generic
+     * code for decoders implementing the .receive_frame API and
+     * may be freely used (but not freed) by them with the caveat
+     * that the packet will be unreferenced generically in
+     * avcodec_flush_buffers().
+     */
+    AVPacket *in_pkt;
     AVBSFContext *bsf;
 
     /**
-- 
2.30.2



More information about the ffmpeg-devel mailing list