diff mbox series

[FFmpeg-devel,6/8] lavc/avcodec: only allocate decoding packets for decoders

Message ID 20220323155720.20017-6-anton@khirnov.net
State Accepted
Commit 2cb86cd00c64ff16b16920eec37ec5b25a59b284
Headers show
Series [FFmpeg-devel,1/8] lavc/avcodec: simplify codec id/type validity checking | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Anton Khirnov March 23, 2022, 3:57 p.m. UTC
---
 libavcodec/avcodec.c | 7 +------
 libavcodec/decode.c  | 8 ++++++++
 2 files changed, 9 insertions(+), 6 deletions(-)

Comments

Andreas Rheinhardt April 13, 2022, 2:51 p.m. UTC | #1
Anton Khirnov:
> ---
>  libavcodec/avcodec.c | 7 +------
>  libavcodec/decode.c  | 8 ++++++++
>  2 files changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
> index c7daa385e7..5fd988a41c 100644
> --- a/libavcodec/avcodec.c
> +++ b/libavcodec/avcodec.c
> @@ -180,12 +180,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
>  
>      avci->buffer_frame = av_frame_alloc();
>      avci->buffer_pkt = av_packet_alloc();
> -    avci->in_pkt = av_packet_alloc();
> -    avci->last_pkt_props = av_packet_alloc();
> -    avci->pkt_props = av_fifo_alloc2(1, sizeof(*avci->last_pkt_props),
> -                                     AV_FIFO_FLAG_AUTO_GROW);
> -    if (!avci->buffer_frame || !avci->buffer_pkt          ||
> -        !avci->in_pkt || !avci->last_pkt_props || !avci->pkt_props) {
> +    if (!avci->buffer_frame || !avci->buffer_pkt) {
>          ret = AVERROR(ENOMEM);
>          goto free_and_end;
>      }
> diff --git a/libavcodec/decode.c b/libavcodec/decode.c
> index 3733e6d4b8..bb3857afd9 100644
> --- a/libavcodec/decode.c
> +++ b/libavcodec/decode.c
> @@ -1527,6 +1527,7 @@ int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
>  
>  int ff_decode_preinit(AVCodecContext *avctx)
>  {
> +    AVCodecInternal *avci = avctx->internal;
>      int ret = 0;
>  
>      /* if the decoder init function was already called previously,
> @@ -1605,6 +1606,13 @@ FF_ENABLE_DEPRECATION_WARNINGS
>          avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
>      }
>  
> +    avci->in_pkt         = av_packet_alloc();
> +    avci->last_pkt_props = av_packet_alloc();
> +    avci->pkt_props      = av_fifo_alloc2(1, sizeof(*avci->last_pkt_props),
> +                                          AV_FIFO_FLAG_AUTO_GROW);
> +    if (!avci->in_pkt || !avci->last_pkt_props || !avci->pkt_props)
> +        return AVERROR(ENOMEM);
> +
>      ret = decode_bsfs_init(avctx);
>      if (ret < 0)
>          return ret;

https://ffmpeg.org/pipermail/ffmpeg-devel/2022-April/295326.html needs
to be applied before this patch or flushing an encoder will segfault.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index c7daa385e7..5fd988a41c 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -180,12 +180,7 @@  int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
 
     avci->buffer_frame = av_frame_alloc();
     avci->buffer_pkt = av_packet_alloc();
-    avci->in_pkt = av_packet_alloc();
-    avci->last_pkt_props = av_packet_alloc();
-    avci->pkt_props = av_fifo_alloc2(1, sizeof(*avci->last_pkt_props),
-                                     AV_FIFO_FLAG_AUTO_GROW);
-    if (!avci->buffer_frame || !avci->buffer_pkt          ||
-        !avci->in_pkt || !avci->last_pkt_props || !avci->pkt_props) {
+    if (!avci->buffer_frame || !avci->buffer_pkt) {
         ret = AVERROR(ENOMEM);
         goto free_and_end;
     }
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 3733e6d4b8..bb3857afd9 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -1527,6 +1527,7 @@  int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
 
 int ff_decode_preinit(AVCodecContext *avctx)
 {
+    AVCodecInternal *avci = avctx->internal;
     int ret = 0;
 
     /* if the decoder init function was already called previously,
@@ -1605,6 +1606,13 @@  FF_ENABLE_DEPRECATION_WARNINGS
         avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
     }
 
+    avci->in_pkt         = av_packet_alloc();
+    avci->last_pkt_props = av_packet_alloc();
+    avci->pkt_props      = av_fifo_alloc2(1, sizeof(*avci->last_pkt_props),
+                                          AV_FIFO_FLAG_AUTO_GROW);
+    if (!avci->in_pkt || !avci->last_pkt_props || !avci->pkt_props)
+        return AVERROR(ENOMEM);
+
     ret = decode_bsfs_init(avctx);
     if (ret < 0)
         return ret;