PR #24938 opened by Sean McGovern (sean_mcg)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24938
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24938.patch

# Summary of changes

Prevent a data race condition by only calling `ff_thread_finish_setup()` once 
per frame.

`jpeg2000dec` declares both frame and slice threading capabilities, so gate it 
accordingly.


>From 3cac8948a1692f1ffac314c19150e35182780a1b Mon Sep 17 00:00:00 2001
From: Sean McGovern <[email protected]>
Date: Thu, 8 Oct 2026 04:57:27 -0400
Subject: [PATCH] jpeg2000dec: ensure ff_thread_finish_setup() is only called
 once per frame

---
 libavcodec/jpeg2000dec.c | 7 ++++++-
 libavcodec/jpeg2000dec.h | 3 +++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 870678f0b0..84e403e4de 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -2861,6 +2861,8 @@ static int jpeg2000_decode_frame(AVCodecContext *avctx, 
AVFrame *picture,
     Jpeg2000DecoderContext *s = avctx->priv_data;
     int ret;
 
+    s->setup_finished = 0;
+
     s->avctx     = avctx;
     bytestream2_init(&s->g, avpkt->data, avpkt->size);
     s->curtileno = -1;
@@ -2917,7 +2919,10 @@ static int jpeg2000_decode_frame(AVCodecContext *avctx, 
AVFrame *picture,
         if (++x == s->ncomponents)
             picture->flags |= AV_FRAME_FLAG_LOSSLESS;
 
-    ff_thread_finish_setup(avctx);
+    if (avctx->active_thread_type & FF_THREAD_FRAME && !s->setup_finished) {
+        ff_thread_finish_setup(avctx);
+        s->setup_finished = 1;
+    }
 
     avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL,
                     s->numXtiles * s->numYtiles);
diff --git a/libavcodec/jpeg2000dec.h b/libavcodec/jpeg2000dec.h
index fce3823164..ad801732b8 100644
--- a/libavcodec/jpeg2000dec.h
+++ b/libavcodec/jpeg2000dec.h
@@ -120,6 +120,9 @@ typedef struct Jpeg2000DecoderContext {
 
     /*options parameters*/
     int             reduction_factor;
+
+    /* frame threading */
+    int             setup_finished;
 } Jpeg2000DecoderContext;
 
 #endif //AVCODEC_JPEG2000DEC_H
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to