[FFmpeg-devel] [PATCH 2/7] avcodec/dxvenc: Fix data races with slice threading

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Wed Jan 24 21:52:34 EET 2024


The old code set a common struct from each thread;
this only "worked" (but is still UB) because the values
written are the same for each thread.
Fix this by moving the assignments to the main thread.

(This also avoids casting const away from a const AVFrame*.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/dxvenc.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)

diff --git a/libavcodec/dxvenc.c b/libavcodec/dxvenc.c
index 10473038cc..94e522d72b 100644
--- a/libavcodec/dxvenc.c
+++ b/libavcodec/dxvenc.c
@@ -130,25 +130,6 @@ typedef struct DXVEncContext {
     HTEntry lut_lookback_ht[LOOKBACK_HT_ELEMS];
 } DXVEncContext;
 
-static int compress_texture_thread(AVCodecContext *avctx, void *arg,
-                                   int slice, int thread_nb)
-{
-    DXVEncContext *ctx = avctx->priv_data;
-    AVFrame *frame = arg;
-
-    if (ctx->enc.tex_funct) {
-        ctx->enc.tex_data.out = ctx->tex_data;
-        ctx->enc.frame_data.in = frame->data[0];
-        ctx->enc.stride = frame->linesize[0];
-        return ff_texturedsp_compress_thread(avctx, &ctx->enc, slice, thread_nb);
-    } else {
-        /* unimplemented: YCoCg formats */
-        return AVERROR_INVALIDDATA;
-    }
-
-    return 0;
-}
-
 /* Converts an index offset value to a 2-bit opcode and pushes it to a stream.
  * Inverse of CHECKPOINT in dxv.c.  */
 #define PUSH_OP(x)                                                            \
@@ -252,7 +233,15 @@ static int dxv_encode(AVCodecContext *avctx, AVPacket *pkt,
     if (ret < 0)
         return ret;
 
-    avctx->execute2(avctx, compress_texture_thread, (void*)frame, NULL, ctx->enc.slice_count);
+    if (ctx->enc.tex_funct) {
+        ctx->enc.tex_data.out = ctx->tex_data;
+        ctx->enc.frame_data.in = frame->data[0];
+        ctx->enc.stride = frame->linesize[0];
+        avctx->execute2(avctx, ff_texturedsp_compress_thread, &ctx->enc, NULL, ctx->enc.slice_count);
+    } else {
+        /* unimplemented: YCoCg formats */
+        return AVERROR_INVALIDDATA;
+    }
 
     bytestream2_init_writer(pbc, pkt->data, pkt->size);
 
-- 
2.34.1



More information about the ffmpeg-devel mailing list