From da85ffccf686798287223720ab0a630a349b3c59 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Sat, 1 Mar 2025 22:36:35 +0100 Subject: [PATCH 06/40] avcodec/mpeg12dec: Don't count errors from first thread twice Compilers can not perform this optimization on their own given that they don't know that the different thread_context pointers don't alias. Also avoid using sequentially consistent operations with atomics when only a single thread accesses them. Signed-off-by: Andreas Rheinhardt --- libavcodec/mpeg12dec.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c index 843640edbf..6bbb8e7afd 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -29,6 +29,7 @@ #define UNCHECKED_BITSTREAM_READER 1 #include +#include #include "libavutil/attributes.h" #include "libavutil/emms.h" @@ -46,7 +47,6 @@ #include "hwaccel_internal.h" #include "hwconfig.h" #include "idctdsp.h" -#include "internal.h" #include "mpeg_er.h" #include "mpeg12.h" #include "mpeg12codecs.h" @@ -2252,6 +2252,7 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && !avctx->hwaccel) { + int error_count = 0; int i; av_assert0(avctx->thread_count > 1); @@ -2259,7 +2260,10 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, &s2->thread_context[0], NULL, s->slice_count, sizeof(void *)); for (i = 0; i < s->slice_count; i++) - s2->er.error_count += s2->thread_context[i]->er.error_count; + error_count += atomic_load_explicit(&s2->thread_context[i]->er.error_count, + memory_order_relaxed); + atomic_store_explicit(&s2->er.error_count, error_count, + memory_order_relaxed); } ret = slice_end(avctx, picture, got_output); @@ -2321,13 +2325,17 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, } if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && !avctx->hwaccel && s->slice_count) { + int error_count = 0; int i; avctx->execute(avctx, slice_decode_thread, s2->thread_context, NULL, s->slice_count, sizeof(void *)); for (i = 0; i < s->slice_count; i++) - s2->er.error_count += s2->thread_context[i]->er.error_count; + error_count += atomic_load_explicit(&s2->thread_context[i]->er.error_count, + memory_order_relaxed); + atomic_store_explicit(&s2->er.error_count, error_count, + memory_order_relaxed); s->slice_count = 0; } if (last_code == 0 || last_code == SLICE_MIN_START_CODE) { -- 2.45.2