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
54 av_buffer_unref(&pic->motion_val_buf[i]);54 av_buffer_unref(&pic->motion_val_buf[i]);
55 av_buffer_unref(&pic->ref_index_buf[i]);55 av_buffer_unref(&pic->ref_index_buf[i]);
56 }56 }
57 av_buffer_unref(&pic->decode_error_flags);
57 58
58 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);59 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
59}60}
@@ -136,6 +137,10 @@ int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src) · 76 unmodified lines
136 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;137 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
137 }138 }
138 139
140 ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
141 if (ret < 0)
142 goto fail;
143
139 h264_copy_picture_params(dst, src);144 h264_copy_picture_params(dst, src);
140 145
141 return 0;146 return 0;
@@ -186,6 +191,10 @@ int ff_h264_replace_picture(H264Context *h, H264Picture *dst, const H264Picture · 44 unmodified lines
186 191
187 dst->hwaccel_picture_private = src->hwaccel_picture_private;192 dst->hwaccel_picture_private = src->hwaccel_picture_private;
188 193
194 ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
195 if (ret < 0)
196 goto fail;
197
189 h264_copy_picture_params(dst, src);198 h264_copy_picture_params(dst, src);
190 199
191 return 0;200 return 0;
libavcodec/h264_slice.c+7−0
@@ -210,6 +210,13 @@ static int alloc_picture(H264Context *h, H264Picture *pic) · 209 unmodified lines
210 if (ret < 0)210 if (ret < 0)
211 goto fail;211 goto fail;
212 212
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
213 if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {220 if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
214 int h_chroma_shift, v_chroma_shift;221 int h_chroma_shift, v_chroma_shift;
215 av_pix_fmt_get_chroma_sub_sample(pic->f->format,222 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
307 307
308 ff_h264_sei_uninit(&h->sei);308 ff_h264_sei_uninit(&h->sei);
309 309
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
310 h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? avctx->thread_count : 1;316 h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? avctx->thread_count : 1;
311 h->slice_ctx = av_calloc(h->nb_slice_ctx, sizeof(*h->slice_ctx));317 h->slice_ctx = av_calloc(h->nb_slice_ctx, sizeof(*h->slice_ctx));
312 if (!h->slice_ctx) {318 if (!h->slice_ctx) {
@@ -353,6 +359,8 @@ static av_cold int h264_decode_end(AVCodecContext *avctx) · 40 unmodified lines
353 359
354 h->cur_pic_ptr = NULL;360 h->cur_pic_ptr = NULL;
355 361
362 av_buffer_pool_uninit(&h->decode_error_flags_pool);
363
356 av_freep(&h->slice_ctx);364 av_freep(&h->slice_ctx);
357 h->nb_slice_ctx = 0;365 h->nb_slice_ctx = 0;
358 366
@@ -739,7 +747,16 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size) · 380 unmodified lines
739 747
740 // set decode_error_flags to allow users to detect concealed decoding errors748 // set decode_error_flags to allow users to detect concealed decoding errors
741 if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) {749 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;
743 }760 }
744 761
745 ret = 0;762 ret = 0;
@@ -762,6 +779,7 @@ end: · 16 unmodified lines
762 779
763 H264SliceContext *sl = h->slice_ctx;780 H264SliceContext *sl = h->slice_ctx;
764 int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];781 int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
782 int decode_error_flags = 0;
765 783
766 ff_h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);784 ff_h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
767 785
@@ -779,7 +797,15 @@ end: · 11 unmodified lines
779 if (sl->ref_count[1])797 if (sl->ref_count[1])
780 ff_h264_set_erpic(&h->er.next_pic, sl->ref_list[1][0].parent);798 ff_h264_set_erpic(&h->er.next_pic, sl->ref_list[1][0].parent);
781 799
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 }
783 if (use_last_pic)809 if (use_last_pic)
784 memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));810 memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
785 }811 }
@@ -851,6 +877,14 @@ static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp) · 65 unmodified lines
851 if (srcp->needs_fg && (ret = av_frame_copy_props(dst, srcp->f)) < 0)877 if (srcp->needs_fg && (ret = av_frame_copy_props(dst, srcp->f)) < 0)
852 return ret;878 return ret;
853 879
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
854 av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(&h->sei.common.frame_packing), 0);888 av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(&h->sei.common.frame_packing), 0);
855 889
856 if (srcp->sei_recovery_frame_cnt == 0)890 if (srcp->sei_recovery_frame_cnt == 0)
libavcodec/h264dec.h+4−0
@@ -152,6 +152,9 @@ typedef struct H264Picture { · 151 unmodified lines
152 152
153 int mb_width, mb_height;153 int mb_width, mb_height;
154 int mb_stride;154 int mb_stride;
155
156 /* data points to an atomic_int */
157 AVBufferRef *decode_error_flags;
155} H264Picture;158} H264Picture;
156 159
157typedef struct H264Ref {160typedef struct H264Ref {
@@ -549,6 +552,7 @@ typedef struct H264Context { · 391 unmodified lines
549 AVBufferPool *mb_type_pool;552 AVBufferPool *mb_type_pool;
550 AVBufferPool *motion_val_pool;553 AVBufferPool *motion_val_pool;
551 AVBufferPool *ref_index_pool;554 AVBufferPool *ref_index_pool;
555 AVBufferPool *decode_error_flags_pool;
552 int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1556 int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1
553} H264Context;557} H264Context;
554 558

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