diff mbox series

[FFmpeg-devel,02/10] lavc/hevcdec: track local context count separately from WPP thread count

Message ID 20240410133118.28144-2-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/10] lavc/hevcdec: rename HEVCContext.HEVClcList to local_ctx | 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 April 10, 2024, 1:31 p.m. UTC
The latter can be lowered while decoding, which would lead to memleaks.
---
 libavcodec/hevcdec.c | 7 +++++--
 libavcodec/hevcdec.h | 2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index c70937a756..55f72af972 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -2697,11 +2697,13 @@  static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
     }
 
     for (i = 1; i < s->threads_number; i++) {
-        if (s->local_ctx[i])
+        if (i < s->nb_local_ctx)
             continue;
         s->local_ctx[i] = av_mallocz(sizeof(HEVCLocalContext));
         if (!s->local_ctx[i])
             return AVERROR(ENOMEM);
+        s->nb_local_ctx++;
+
         s->local_ctx[i]->logctx = s->avctx;
         s->local_ctx[i]->parent = s;
         s->local_ctx[i]->common_cabac_state = &s->cabac;
@@ -3475,7 +3477,7 @@  static av_cold int hevc_decode_free(AVCodecContext *avctx)
     av_freep(&s->sh.size);
 
     if (s->local_ctx) {
-        for (i = 1; i < s->threads_number; i++) {
+        for (i = 1; i < s->nb_local_ctx; i++) {
             av_freep(&s->local_ctx[i]);
         }
     }
@@ -3504,6 +3506,7 @@  static av_cold int hevc_init_context(AVCodecContext *avctx)
     s->HEVClc->logctx = avctx;
     s->HEVClc->common_cabac_state = &s->cabac;
     s->local_ctx[0] = s->HEVClc;
+    s->nb_local_ctx = 1;
 
     s->output_frame = av_frame_alloc();
     if (!s->output_frame)
diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h
index 9e3e6a8cd7..a881eb9981 100644
--- a/libavcodec/hevcdec.h
+++ b/libavcodec/hevcdec.h
@@ -442,6 +442,8 @@  typedef struct HEVCContext {
     AVCodecContext *avctx;
 
     HEVCLocalContext    **local_ctx;
+    unsigned           nb_local_ctx;
+
     HEVCLocalContext    *HEVClc;
 
     uint8_t             threads_type;