diff options
author | Andreas Rheinhardt <andreas.rheinhardt@outlook.com> | 2022-06-30 15:01:22 +0200 |
---|---|---|
committer | Andreas Rheinhardt <andreas.rheinhardt@outlook.com> | 2022-07-25 23:31:37 +0200 |
commit | 72d5ce9fa6cd67497fbfffc97031cb6b7ab23c6d (patch) | |
tree | a3c4c3b1c2a9c7b1e462cf229a6022e7e8e6664e /libavcodec | |
parent | 8c4f95e1e1488bbbaca19bc6c6214393136e1b51 (diff) | |
download | ffmpeg-72d5ce9fa6cd67497fbfffc97031cb6b7ab23c6d.tar.gz |
avcodec/hevcdec: Add stat_coeffs to HEVCABACState
The HEVC decoder has both HEVCContext and HEVCLocalContext
structures. The latter is supposed to be the structure
containing the per-slicethread state.
Yet that is not how it is handled in practice: Each HEVCLocalContext
has a unique HEVCContext allocated for it and each of these
coincides with the main HEVCContext except in exactly one field:
The corresponding HEVCLocalContext.
This makes it possible to pass the HEVCContext everywhere where
logically a HEVCLocalContext should be used.
This led to confusion in the first version of what eventually became
commit c8bc0f66a875bc3708d8dc11b757f2198606ffd7:
Before said commit, the initialization of the Rice parameter derivation
state was incorrect; the fix for single-threaded as well as
frame-threaded decoding was to add backup stats to HEVCContext
that are used when the cabac state is updated*, see
https://ffmpeg.org/pipermail/ffmpeg-devel/2020-August/268861.html
Yet due to what has been said above, this does not work for
slice-threading, because the each HEVCLocalContext has its own
HEVCContext, so the Rice parameter state would not be transferred
between threads.
This is fixed in c8bc0f66a875bc3708d8dc11b757f2198606ffd7
by a hack: It rederives what the previous thread was and accesses
the corresponding HEVCContext.
Fix this by treating the Rice parameter state the same way
the ordinary CABAC parameters are shared between threads:
Make them part of the same struct that is shared between
slice threads. This does not cause races, because
the parts of the code that access these Rice parameters
are a subset of the parts of code that access the CABAC parameters.
*: And if the persistent_rice_adaptation_enabled_flag is set.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Diffstat (limited to 'libavcodec')
-rw-r--r-- | libavcodec/hevc_cabac.c | 17 | ||||
-rw-r--r-- | libavcodec/hevcdec.c | 10 | ||||
-rw-r--r-- | libavcodec/hevcdec.h | 10 |
3 files changed, 20 insertions, 17 deletions
diff --git a/libavcodec/hevc_cabac.c b/libavcodec/hevc_cabac.c index a194f8a02a..985c97ef2a 100644 --- a/libavcodec/hevc_cabac.c +++ b/libavcodec/hevc_cabac.c @@ -453,19 +453,18 @@ void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts) (ctb_addr_ts % s->ps.sps->ctb_width == 2 || (s->ps.sps->ctb_width == 2 && ctb_addr_ts % s->ps.sps->ctb_width == 0))) { - memcpy(s->cabac_state, s->HEVClc->cabac_state, HEVC_CONTEXTS); + memcpy(s->cabac->state, s->HEVClc->cabac_state, HEVC_CONTEXTS); if (s->ps.sps->persistent_rice_adaptation_enabled_flag) { - memcpy(s->stat_coeff, s->HEVClc->stat_coeff, HEVC_STAT_COEFFS); + memcpy(s->cabac->stat_coeff, s->HEVClc->stat_coeff, HEVC_STAT_COEFFS); } } } -static void load_states(HEVCContext *s, int thread) +static void load_states(HEVCContext *s) { - memcpy(s->HEVClc->cabac_state, s->cabac_state, HEVC_CONTEXTS); + memcpy(s->HEVClc->cabac_state, s->cabac->state, HEVC_CONTEXTS); if (s->ps.sps->persistent_rice_adaptation_enabled_flag) { - const HEVCContext *prev = s->sList[(thread + s->threads_number - 1) % s->threads_number]; - memcpy(s->HEVClc->stat_coeff, prev->stat_coeff, HEVC_STAT_COEFFS); + memcpy(s->HEVClc->stat_coeff, s->cabac->stat_coeff, HEVC_STAT_COEFFS); } } @@ -508,7 +507,7 @@ static void cabac_init_state(HEVCContext *s) s->HEVClc->stat_coeff[i] = 0; } -int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts, int thread) +int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts) { if (ctb_addr_ts == s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) { int ret = cabac_init_decoder(s); @@ -525,7 +524,7 @@ int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts, int thread) if (s->ps.sps->ctb_width == 1) cabac_init_state(s); else if (s->sh.dependent_slice_segment_flag == 1) - load_states(s, thread); + load_states(s); } } } else { @@ -556,7 +555,7 @@ int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts, int thread) if (s->ps.sps->ctb_width == 1) cabac_init_state(s); else - load_states(s, thread); + load_states(s); } } } diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c index b0212a10a5..de1688cd2e 100644 --- a/libavcodec/hevcdec.c +++ b/libavcodec/hevcdec.c @@ -2502,7 +2502,7 @@ static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread) y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size; hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts); - ret = ff_hevc_cabac_init(s, ctb_addr_ts, 0); + ret = ff_hevc_cabac_init(s, ctb_addr_ts); if (ret < 0) { s->tab_slice_address[ctb_addr_rs] = -1; return ret; @@ -2580,7 +2580,7 @@ static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int return 0; } - ret = ff_hevc_cabac_init(s, ctb_addr_ts, thread); + ret = ff_hevc_cabac_init(s, ctb_addr_ts); if (ret < 0) goto error; hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size); @@ -3598,7 +3598,7 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx) av_freep(&s->md5_ctx); - av_freep(&s->cabac_state); + av_freep(&s->cabac); for (i = 0; i < 3; i++) { av_freep(&s->sao_pixel_buffer_h[i]); @@ -3652,8 +3652,8 @@ static av_cold int hevc_init_context(AVCodecContext *avctx) s->HEVClcList[0] = s->HEVClc; s->sList[0] = s; - s->cabac_state = av_malloc(HEVC_CONTEXTS); - if (!s->cabac_state) + s->cabac = av_malloc(sizeof(*s->cabac)); + if (!s->cabac) return AVERROR(ENOMEM); s->output_frame = av_frame_alloc(); diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h index e2dba54f26..b97b7d1354 100644 --- a/libavcodec/hevcdec.h +++ b/libavcodec/hevcdec.h @@ -226,6 +226,11 @@ enum ScanType { SCAN_VERT, }; +typedef struct HEVCCABACState { + uint8_t state[HEVC_CONTEXTS]; + uint8_t stat_coeff[HEVC_STAT_COEFFS]; +} HEVCCABACState; + typedef struct LongTermRPS { int poc[32]; uint8_t poc_msb_present[32]; @@ -482,8 +487,7 @@ typedef struct HEVCContext { int width; int height; - uint8_t *cabac_state; - uint8_t stat_coeff[HEVC_STAT_COEFFS]; + HEVCCABACState *cabac; /** 1 if the independent slice segment header was successfully parsed */ uint8_t slice_initialized; @@ -601,7 +605,7 @@ int ff_hevc_frame_rps(HEVCContext *s); int ff_hevc_slice_rpl(HEVCContext *s); void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts); -int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts, int thread); +int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts); int ff_hevc_sao_merge_flag_decode(HEVCContext *s); int ff_hevc_sao_type_idx_decode(HEVCContext *s); int ff_hevc_sao_band_position_decode(HEVCContext *s); |