From f07229d258b425d273f8b719747e3b4fe650de5d Mon Sep 17 00:00:00 2001 From: Pu Cheng Date: Fri, 20 Nov 2026 07:52:55 +0800 Subject: [PATCH] decoder: Decode a picture on one thread when it is its own reference When a P picture has no forward reference or a B picture has no references (a stream starting with a B picture, the leading B pictures of an open GOP, the first P picture after a reset, corrupted data), impeg2d_pre_pic_dec_proc() uses the current picture buffer as the reference. With more than one core, impeg2d_dec_pic_data() then decodes the slices of that picture on several threads at once, and motion compensation in one thread reads rows of the picture that another thread is writing. The output depends on thread scheduling, and ThreadSanitizer reports data races between impeg2_mc_*_sse42 writes and reads. Decode such a picture on the calling thread only. --- decoder/impeg2d_dec_hdr.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/decoder/impeg2d_dec_hdr.c b/decoder/impeg2d_dec_hdr.c index 9ef2be6..bacfad8 100644 --- a/decoder/impeg2d_dec_hdr.c +++ b/decoder/impeg2d_dec_hdr.c @@ -1471,6 +1471,13 @@ void impeg2d_dec_pic_data(dec_state_t *ps_dec) dec_state_t *ps_dec_thd; WORD32 i4_status; WORD32 i4_min_mb_y; + WORD32 i4_num_cores = ps_dec->i4_num_cores; + + if((ps_dec->as_ref_buf[FORW][TOP].pu1_y == ps_dec->s_cur_frm_buf.pu1_y) || + (ps_dec->as_ref_buf[BACK][TOP].pu1_y == ps_dec->s_cur_frm_buf.pu1_y)) + { + i4_num_cores = 1; + } /* Resetting the MB address and MB coordinates at the start of the Frame */ @@ -1485,7 +1492,7 @@ void impeg2d_dec_pic_data(dec_state_t *ps_dec) } i4_min_mb_y = 1; - for(i=1; i < ps_dec->i4_num_cores; i++) + for(i=1; i < i4_num_cores; i++) { // initialize decoder context for thread // launch dec->u4_num_cores-1 threads @@ -1531,7 +1538,7 @@ void impeg2d_dec_pic_data(dec_state_t *ps_dec) impeg2d_dec_pic_data_thread(ps_dec); // wait for threads to complete - for(i=1; i < ps_dec->i4_num_cores; i++) + for(i=1; i < i4_num_cores; i++) { if(ps_dec_state_multi_core->au4_thread_launched[i]) {