From d2bc039501cdfe2067365ca1bf4be7201f071c05 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Tue, 12 Sep 2023 15:47:42 +0200 Subject: [PATCH] avcodec/error_resilience: Make applying decode_error_flags optional Add a pointer parameter that if supplied will be used to return the updated decode_error_flags. This will allow to fix several races when using frame-threading; these resulted from AVFrame that the earlier code updated concurrently being used as source in an av_frame_ref() call in the decoder's update_thread_context. Signed-off-by: Andreas Rheinhardt --- libavcodec/error_resilience.c | 7 +++++-- libavcodec/error_resilience.h | 12 +++++++++++- libavcodec/h263dec.c | 2 +- libavcodec/h264dec.c | 2 +- libavcodec/mpeg12dec.c | 2 +- libavcodec/mss2.c | 2 +- libavcodec/rv10.c | 4 ++-- libavcodec/rv34.c | 6 +++--- libavcodec/vc1dec.c | 2 +- 9 files changed, 26 insertions(+), 13 deletions(-) diff --git a/libavcodec/error_resilience.c b/libavcodec/error_resilience.c index 68e20925e0..880aea6f30 100644 --- a/libavcodec/error_resilience.c +++ b/libavcodec/error_resilience.c @@ -889,7 +889,7 @@ void ff_er_add_slice(ERContext *s, int startx, int starty, } } -void ff_er_frame_end(ERContext *s) +void ff_er_frame_end(ERContext *s, int *decode_error_flags) { int *linesize = NULL; int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error; @@ -1114,7 +1114,10 @@ void ff_er_frame_end(ERContext *s) av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors in %c frame\n", dc_error, ac_error, mv_error, av_get_picture_type_char(s->cur_pic.f->pict_type)); - s->cur_pic.f->decode_error_flags |= FF_DECODE_ERROR_CONCEALMENT_ACTIVE; + if (decode_error_flags) + *decode_error_flags |= FF_DECODE_ERROR_CONCEALMENT_ACTIVE; + else + s->cur_pic.f->decode_error_flags |= FF_DECODE_ERROR_CONCEALMENT_ACTIVE; is_intra_likely = is_intra_more_likely(s); diff --git a/libavcodec/error_resilience.h b/libavcodec/error_resilience.h index 47cc8a4fc6..b03f8ec896 100644 --- a/libavcodec/error_resilience.h +++ b/libavcodec/error_resilience.h @@ -90,7 +90,17 @@ typedef struct ERContext { } ERContext; void ff_er_frame_start(ERContext *s); -void ff_er_frame_end(ERContext *s); + +/** + * Indicate that a frame has finished decoding and perform error concealment + * in case it has been enabled and is necessary and supported. + * + * @param s ERContext in use + * @param decode_error_flags pointer where updated decode_error_flags are written + * if supplied; if not, the new flags are directly + * applied to the AVFrame whose errors are concealed + */ +void ff_er_frame_end(ERContext *s, int *decode_error_flags); void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status); diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c index 52e51dd489..9f63f1a7cb 100644 --- a/libavcodec/h263dec.c +++ b/libavcodec/h263dec.c @@ -622,7 +622,7 @@ retry: av_assert1(s->bitstream_buffer_size == 0); frame_end: if (!s->studio_profile) - ff_er_frame_end(&s->er); + ff_er_frame_end(&s->er, NULL); if (avctx->hwaccel) { ret = FF_HW_SIMPLE_CALL(avctx, end_frame); diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c index f13b1081fc..8e90678125 100644 --- a/libavcodec/h264dec.c +++ b/libavcodec/h264dec.c @@ -779,7 +779,7 @@ end: if (sl->ref_count[1]) ff_h264_set_erpic(&h->er.next_pic, sl->ref_list[1][0].parent); - ff_er_frame_end(&h->er); + ff_er_frame_end(&h->er, NULL); if (use_last_pic) memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0])); } diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c index 677360f954..92ef6944fa 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -2033,7 +2033,7 @@ static int slice_end(AVCodecContext *avctx, AVFrame *pict) if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) { /* end of image */ - ff_er_frame_end(&s->er); + ff_er_frame_end(&s->er, NULL); ff_mpv_frame_end(s); diff --git a/libavcodec/mss2.c b/libavcodec/mss2.c index 70aa56cb84..2237cc8bb1 100644 --- a/libavcodec/mss2.c +++ b/libavcodec/mss2.c @@ -422,7 +422,7 @@ static int decode_wmv9(AVCodecContext *avctx, const uint8_t *buf, int buf_size, ff_vc1_decode_blocks(v); if (v->end_mb_x == s->mb_width && s->end_mb_y == s->mb_height) { - ff_er_frame_end(&s->er); + ff_er_frame_end(&s->er, NULL); } else { av_log(v->s.avctx, AV_LOG_WARNING, "disabling error correction due to block count mismatch %dx%d != %dx%d\n", diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c index 5edd934f82..6abceade4e 100644 --- a/libavcodec/rv10.c +++ b/libavcodec/rv10.c @@ -477,7 +477,7 @@ static int rv10_decode_packet(AVCodecContext *avctx, const uint8_t *buf, if ((s->mb_x == 0 && s->mb_y == 0) || !s->current_picture_ptr) { // FIXME write parser so we always have complete frames? if (s->current_picture_ptr) { - ff_er_frame_end(&s->er); + ff_er_frame_end(&s->er, NULL); ff_mpv_frame_end(s); s->mb_x = s->mb_y = s->resync_mb_x = s->resync_mb_y = 0; } @@ -649,7 +649,7 @@ static int rv10_decode_frame(AVCodecContext *avctx, AVFrame *pict, } if (s->current_picture_ptr && s->mb_y >= s->mb_height) { - ff_er_frame_end(&s->er); + ff_er_frame_end(&s->er, NULL); ff_mpv_frame_end(s); if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) { diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c index af4d6a3400..e9660bb457 100644 --- a/libavcodec/rv34.c +++ b/libavcodec/rv34.c @@ -1560,7 +1560,7 @@ static int finish_frame(AVCodecContext *avctx, AVFrame *pict) MpegEncContext *s = &r->s; int got_picture = 0, ret; - ff_er_frame_end(&s->er); + ff_er_frame_end(&s->er, NULL); ff_mpv_frame_end(s); s->mb_num_left = 0; @@ -1655,7 +1655,7 @@ int ff_rv34_decode_frame(AVCodecContext *avctx, AVFrame *pict, av_log(avctx, AV_LOG_ERROR, "New frame but still %d MB left.\n", s->mb_num_left); if (!s->context_reinit) - ff_er_frame_end(&s->er); + ff_er_frame_end(&s->er, NULL); ff_mpv_frame_end(s); } @@ -1790,7 +1790,7 @@ int ff_rv34_decode_frame(AVCodecContext *avctx, AVFrame *pict, av_log(avctx, AV_LOG_INFO, "marking unfished frame as finished\n"); /* always mark the current frame as finished, frame-mt supports * only complete frames */ - ff_er_frame_end(&s->er); + ff_er_frame_end(&s->er, NULL); ff_mpv_frame_end(s); s->mb_num_left = 0; ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0); diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c index b8663aaf98..449d2fea5e 100644 --- a/libavcodec/vc1dec.c +++ b/libavcodec/vc1dec.c @@ -1348,7 +1348,7 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, if ( !v->field_mode && avctx->codec_id != AV_CODEC_ID_WMV3IMAGE && avctx->codec_id != AV_CODEC_ID_VC1IMAGE) - ff_er_frame_end(&s->er); + ff_er_frame_end(&s->er, NULL); } ff_mpv_frame_end(s);