History

avcodec/h264dec: Fix data race when updating decode_error_flags

When using multi-threaded decoding, every decoding thread has its own DBP consisting of H264Pictures and each of these points to its own AVFrames. They are synced during update_thread_context via av_frame_ref() and therefore the threads actually decoding (as well as all the others) must not modify any field that is copied by av_frame_ref() after ff_thread_finish_setup(). Yet this is exactly what happens when an error occurs during decoding and the AVFrame's decode_error_flags are updated. Given that these errors only become apparent during decoding, this can't be set before ff_thread_finish_setup() without defeating the point of frame-threading; in practice, this meant that the decoder did not set these flags correctly in case frame-threading was in use. (This means that e.g. the ffmpeg cli tool fails to output its "corrupt decoded frame" message in a nondeterministic fashion.) This commit fixes this by adding a new H264Picture field that is actually propagated across threads; the field is an AVBufferRef* whose data is an atomic_int; it is atomic in order to allow multiple threads to update it concurrently and not to provide synchronization between the threads setting the field and the thread ultimately returning the AVFrame. This unfortunately has the overhead of one allocation per H264Picture (both the original one as well as creating a reference to an existing one), even in case of no errors. In order to mitigate this, an AVBufferPool has been used and only if frame-threading is actually in use. This expense will be removed as soon as a proper API for refcounted objects (not based upon AVBuffer) is in place.

Andreas Rheinhardt committed 3 years ago

Commit fa77cb21 parent d2bc0394 files · +56 −2Browse files

Unified · Split

4 files
  1. libavcodec/h264_picture.c +9 −0
  2. libavcodec/h264_slice.c +7 −0
  3. libavcodec/h264dec.c +36 −2
  4. libavcodec/h264dec.h +4 −0
libavcodec/h264_picture.c+9−0
@@ -54,6 +54,7 @@ void ff_h264_unref_picture(H264Context *h, H264Picture *pic) · 53 unmodified lines
5454 av_buffer_unref(&pic->motion_val_buf[i]);
5555 av_buffer_unref(&pic->ref_index_buf[i]);
5656 }
57+ av_buffer_unref(&pic->decode_error_flags);
5758
5859 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
5960 }
@@ -136,6 +137,10 @@ int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src) · 76 unmodified lines
136137 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
137138 }
138139
140+ ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
141+ if (ret < 0)
142+ goto fail;
143+
139144 h264_copy_picture_params(dst, src);
140145
141146 return 0;
@@ -186,6 +191,10 @@ int ff_h264_replace_picture(H264Context *h, H264Picture *dst, const H264Picture · 44 unmodified lines
186191
187192 dst->hwaccel_picture_private = src->hwaccel_picture_private;
188193
194+ ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
195+ if (ret < 0)
196+ goto fail;
197+
189198 h264_copy_picture_params(dst, src);
190199
191200 return 0;
libavcodec/h264_slice.c+7−0
@@ -210,6 +210,13 @@ static int alloc_picture(H264Context *h, H264Picture *pic) · 209 unmodified lines
210210 if (ret < 0)
211211 goto fail;
212212
213+ if (h->decode_error_flags_pool) {
214+ pic->decode_error_flags = av_buffer_pool_get(h->decode_error_flags_pool);
215+ if (!pic->decode_error_flags)
216+ goto fail;
217+ atomic_init((atomic_int*)pic->decode_error_flags->data, 0);
218+ }
219+
213220 if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
214221 int h_chroma_shift, v_chroma_shift;
215222 av_pix_fmt_get_chroma_sub_sample(pic->f->format,
libavcodec/h264dec.c+36−2
@@ -307,6 +307,12 @@ static int h264_init_context(AVCodecContext *avctx, H264Context *h) · 306 unmodified lines
307307
308308 ff_h264_sei_uninit(&h->sei);
309309
310+ if (avctx->active_thread_type & FF_THREAD_FRAME) {
311+ h->decode_error_flags_pool = av_buffer_pool_init(sizeof(atomic_int), NULL);
312+ if (!h->decode_error_flags_pool)
313+ return AVERROR(ENOMEM);
314+ }
315+
310316 h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? avctx->thread_count : 1;
311317 h->slice_ctx = av_calloc(h->nb_slice_ctx, sizeof(*h->slice_ctx));
312318 if (!h->slice_ctx) {
@@ -353,6 +359,8 @@ static av_cold int h264_decode_end(AVCodecContext *avctx) · 40 unmodified lines
353359
354360 h->cur_pic_ptr = NULL;
355361
362+ av_buffer_pool_uninit(&h->decode_error_flags_pool);
363+
356364 av_freep(&h->slice_ctx);
357365 h->nb_slice_ctx = 0;
358366
@@ -739,7 +747,16 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size) · 380 unmodified lines
739747
740748 // set decode_error_flags to allow users to detect concealed decoding errors
741749 if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) {
742− h->cur_pic_ptr->f->decode_error_flags |= FF_DECODE_ERROR_DECODE_SLICES;
750+ if (h->cur_pic_ptr->decode_error_flags) {
751+ /* Frame-threading in use */
752+ atomic_int *decode_error = (atomic_int*)h->cur_pic_ptr->decode_error_flags->data;
753+ /* Using atomics here is not supposed to provide syncronisation;
754+ * they are merely used to allow to set decode_error from both
755+ * decoding threads in case of coded slices. */
756+ atomic_fetch_or_explicit(decode_error, FF_DECODE_ERROR_DECODE_SLICES,
757+ memory_order_relaxed);
758+ } else
759+ h->cur_pic_ptr->f->decode_error_flags |= FF_DECODE_ERROR_DECODE_SLICES;
743760 }
744761
745762 ret = 0;
@@ -762,6 +779,7 @@ end: · 16 unmodified lines
762779
763780 H264SliceContext *sl = h->slice_ctx;
764781 int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
782+ int decode_error_flags = 0;
765783
766784 ff_h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
767785
@@ -779,7 +797,15 @@ end: · 11 unmodified lines
779797 if (sl->ref_count[1])
780798 ff_h264_set_erpic(&h->er.next_pic, sl->ref_list[1][0].parent);
781799
782− ff_er_frame_end(&h->er, NULL);
800+ ff_er_frame_end(&h->er, &decode_error_flags);
801+ if (decode_error_flags) {
802+ if (h->cur_pic_ptr->decode_error_flags) {
803+ atomic_int *decode_error = (atomic_int*)h->cur_pic_ptr->decode_error_flags->data;
804+ atomic_fetch_or_explicit(decode_error, decode_error_flags,
805+ memory_order_relaxed);
806+ } else
807+ h->cur_pic_ptr->f->decode_error_flags |= decode_error_flags;
808+ }
783809 if (use_last_pic)
784810 memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
785811 }
@@ -851,6 +877,14 @@ static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp) · 65 unmodified lines
851877 if (srcp->needs_fg && (ret = av_frame_copy_props(dst, srcp->f)) < 0)
852878 return ret;
853879
880+ if (srcp->decode_error_flags) {
881+ atomic_int *decode_error = (atomic_int*)srcp->decode_error_flags->data;
882+ /* The following is not supposed to provide synchronisation at all:
883+ * given that srcp has already finished decoding, decode_error
884+ * has already been set to its final value. */
885+ dst->decode_error_flags |= atomic_load_explicit(decode_error, memory_order_relaxed);
886+ }
887+
854888 av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(&h->sei.common.frame_packing), 0);
855889
856890 if (srcp->sei_recovery_frame_cnt == 0)
libavcodec/h264dec.h+4−0
@@ -152,6 +152,9 @@ typedef struct H264Picture { · 151 unmodified lines
152152
153153 int mb_width, mb_height;
154154 int mb_stride;
155+
156+ /* data points to an atomic_int */
157+ AVBufferRef *decode_error_flags;
155158 } H264Picture;
156159
157160 typedef struct H264Ref {
@@ -549,6 +552,7 @@ typedef struct H264Context { · 391 unmodified lines
549552 AVBufferPool *mb_type_pool;
550553 AVBufferPool *motion_val_pool;
551554 AVBufferPool *ref_index_pool;
555+ AVBufferPool *decode_error_flags_pool;
552556 int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1
553557 } H264Context;
554558

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t