History

lavc/hevcdec: drop HEVCLocalContext.gb

In all HEVCLocalContext instances except the first one, the bitreader is never used for actually reading bits, but merely for passing the buffer to ff_init_cabac_decoder(), which is better done directly. The instance that actually is used for bitreading gets moved to stack in decode_nal_unit(), which makes its lifetime clearer.

Anton Khirnov committed 2 years ago

Commit 67ca18d1 parent ac69e6c3 files · +27 −37Browse files

Unified · Split

3 files
  1. libavcodec/hevc_cabac.c +5 −12
  2. libavcodec/hevcdec.c +20 −23
  3. libavcodec/hevcdec.h +2 −2
libavcodec/hevc_cabac.c+5−12
@@ -427,14 +427,6 @@ static int cabac_reinit(HEVCLocalContext *lc) · 426 unmodified lines
427 return skip_bytes(&lc->cc, 0) == NULL ? AVERROR_INVALIDDATA : 0;427 return skip_bytes(&lc->cc, 0) == NULL ? AVERROR_INVALIDDATA : 0;
428}428}
429 429
430static int cabac_init_decoder(HEVCLocalContext *lc)
431{
432 GetBitContext *gb = &lc->gb;
433 return ff_init_cabac_decoder(&lc->cc,
434 gb->buffer + get_bits_count(gb) / 8,
435 (get_bits_left(gb) + 7) / 8);
436}
437
438static void cabac_init_state(HEVCLocalContext *lc, const HEVCContext *s)430static void cabac_init_state(HEVCLocalContext *lc, const HEVCContext *s)
439{431{
440 int init_type = 2 - s->sh.slice_type;432 int init_type = 2 - s->sh.slice_type;
@@ -459,12 +451,13 @@ static void cabac_init_state(HEVCLocalContext *lc, const HEVCContext *s) · 18 unmodified lines
459 lc->stat_coeff[i] = 0;451 lc->stat_coeff[i] = 0;
460}452}
461 453
462int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts)454int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts,
455 const uint8_t *data, size_t size)
463{456{
464 const HEVCContext *const s = lc->parent;457 const HEVCContext *const s = lc->parent;
465 458
466 if (ctb_addr_ts == s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) {459 if (ctb_addr_ts == s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) {
467 int ret = cabac_init_decoder(lc);460 int ret = ff_init_cabac_decoder(&lc->cc, data, size);
468 if (ret < 0)461 if (ret < 0)
469 return ret;462 return ret;
470 if (s->sh.dependent_slice_segment_flag == 0 ||463 if (s->sh.dependent_slice_segment_flag == 0 ||
@@ -488,7 +481,7 @@ int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts) · 17 unmodified lines
488 if (s->threads_number == 1)481 if (s->threads_number == 1)
489 ret = cabac_reinit(lc);482 ret = cabac_reinit(lc);
490 else {483 else {
491 ret = cabac_init_decoder(lc);484 ret = ff_init_cabac_decoder(&lc->cc, data, size);
492 }485 }
493 if (ret < 0)486 if (ret < 0)
494 return ret;487 return ret;
@@ -501,7 +494,7 @@ int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts) · 6 unmodified lines
501 if (s->threads_number == 1)494 if (s->threads_number == 1)
502 ret = cabac_reinit(lc);495 ret = cabac_reinit(lc);
503 else {496 else {
504 ret = cabac_init_decoder(lc);497 ret = ff_init_cabac_decoder(&lc->cc, data, size);
505 }498 }
506 if (ret < 0)499 if (ret < 0)
507 return ret;500 return ret;
libavcodec/hevcdec.c+20−23
@@ -593,9 +593,8 @@ fail: · 592 unmodified lines
593 return ret;593 return ret;
594}594}
595 595
596static int hls_slice_header(HEVCContext *s)596static int hls_slice_header(HEVCContext *s, GetBitContext *gb)
597{597{
598 GetBitContext *gb = &s->HEVClc->gb;
599 SliceHeader *sh = &s->sh;598 SliceHeader *sh = &s->sh;
600 int i, ret;599 int i, ret;
601 600
@@ -2533,9 +2532,11 @@ static void hls_decode_neighbour(HEVCLocalContext *lc, int x_ctb, int y_ctb, · 1931 unmodified lines
2533 lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->ps.sps->ctb_width]]));2532 lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->ps.sps->ctb_width]]));
2534}2533}
2535 2534
2536static int hls_decode_entry(HEVCContext *s)2535static int hls_decode_entry(HEVCContext *s, GetBitContext *gb)
2537{2536{
2538 HEVCLocalContext *const lc = s->HEVClc;2537 HEVCLocalContext *const lc = s->HEVClc;
2538 const uint8_t *slice_data = gb->buffer + s->sh.data_offset;
2539 const size_t slice_size = gb->buffer_end - gb->buffer - s->sh.data_offset;
2539 int ctb_size = 1 << s->ps.sps->log2_ctb_size;2540 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2540 int more_data = 1;2541 int more_data = 1;
2541 int x_ctb = 0;2542 int x_ctb = 0;
@@ -2563,7 +2564,7 @@ static int hls_decode_entry(HEVCContext *s) · 21 unmodified lines
2563 y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;2564 y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2564 hls_decode_neighbour(lc, x_ctb, y_ctb, ctb_addr_ts);2565 hls_decode_neighbour(lc, x_ctb, y_ctb, ctb_addr_ts);
2565 2566
2566 ret = ff_hevc_cabac_init(lc, ctb_addr_ts);2567 ret = ff_hevc_cabac_init(lc, ctb_addr_ts, slice_data, slice_size);
2567 if (ret < 0) {2568 if (ret < 0) {
2568 s->tab_slice_address[ctb_addr_rs] = -1;2569 s->tab_slice_address[ctb_addr_rs] = -1;
2569 return ret;2570 return ret;
@@ -2605,14 +2606,14 @@ static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *hevc_lclist, · 35 unmodified lines
2605 int ctb_addr_rs = s->sh.slice_ctb_addr_rs + ctb_row * ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size);2606 int ctb_addr_rs = s->sh.slice_ctb_addr_rs + ctb_row * ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size);
2606 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];2607 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2607 int thread = ctb_row % s->threads_number;2608 int thread = ctb_row % s->threads_number;
2609
2610 const uint8_t *data = s->data + s->sh.offset[ctb_row];
2611 const size_t data_size = s->sh.size[ctb_row];
2612
2608 int ret;2613 int ret;
2609 2614
2610 if(ctb_row) {2615 if (ctb_row)
2611 ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row], s->sh.size[ctb_row]);2616 ff_init_cabac_decoder(&lc->cc, data, data_size);
2612 if (ret < 0)
2613 goto error;
2614 ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[ctb_row], s->sh.size[ctb_row]);
2615 }
2616 2617
2617 while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {2618 while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2618 int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;2619 int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
@@ -2630,7 +2631,7 @@ static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *hevc_lclist, · 11 unmodified lines
2630 return 0;2631 return 0;
2631 }2632 }
2632 2633
2633 ret = ff_hevc_cabac_init(lc, ctb_addr_ts);2634 ret = ff_hevc_cabac_init(lc, ctb_addr_ts, data, data_size);
2634 if (ret < 0)2635 if (ret < 0)
2635 goto error;2636 goto error;
2636 hls_sao_param(lc, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);2637 hls_sao_param(lc, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
@@ -2681,7 +2682,6 @@ static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal) · 44 unmodified lines
2681{2682{
2682 const uint8_t *data = nal->data;2683 const uint8_t *data = nal->data;
2683 int length = nal->size;2684 int length = nal->size;
2684 HEVCLocalContext *lc;
2685 int *ret;2685 int *ret;
2686 int64_t offset;2686 int64_t offset;
2687 int64_t startheader, cmpt = 0;2687 int64_t startheader, cmpt = 0;
@@ -2719,8 +2719,7 @@ static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal) · 31 unmodified lines
2719 s->nb_local_ctx = s->threads_number;2719 s->nb_local_ctx = s->threads_number;
2720 }2720 }
2721 2721
2722 lc = &s->local_ctx[0];2722 offset = s->sh.data_offset;
2723 offset = (lc->gb.index >> 3);
2724 2723
2725 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {2724 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2726 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {2725 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
@@ -2981,11 +2980,9 @@ static int hevc_frame_end(HEVCContext *s) · 254 unmodified lines
2981 2980
2982static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)2981static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
2983{2982{
2984 HEVCLocalContext *lc = s->HEVClc;2983 GetBitContext gb = nal->gb;
2985 GetBitContext *gb = &lc->gb;
2986 int ctb_addr_ts, ret;2984 int ctb_addr_ts, ret;
2987 2985
2988 *gb = nal->gb;
2989 s->nal_unit_type = nal->type;2986 s->nal_unit_type = nal->type;
2990 s->temporal_id = nal->temporal_id;2987 s->temporal_id = nal->temporal_id;
2991 2988
@@ -2997,7 +2994,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 5 unmodified lines
2997 if (ret < 0)2994 if (ret < 0)
2998 goto fail;2995 goto fail;
2999 }2996 }
3000 ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);2997 ret = ff_hevc_decode_nal_vps(&gb, s->avctx, &s->ps);
3001 if (ret < 0)2998 if (ret < 0)
3002 goto fail;2999 goto fail;
3003 break;3000 break;
@@ -3008,7 +3005,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 4 unmodified lines
3008 if (ret < 0)3005 if (ret < 0)
3009 goto fail;3006 goto fail;
3010 }3007 }
3011 ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,3008 ret = ff_hevc_decode_nal_sps(&gb, s->avctx, &s->ps,
3012 s->apply_defdispwin);3009 s->apply_defdispwin);
3013 if (ret < 0)3010 if (ret < 0)
3014 goto fail;3011 goto fail;
@@ -3020,7 +3017,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 5 unmodified lines
3020 if (ret < 0)3017 if (ret < 0)
3021 goto fail;3018 goto fail;
3022 }3019 }
3023 ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);3020 ret = ff_hevc_decode_nal_pps(&gb, s->avctx, &s->ps);
3024 if (ret < 0)3021 if (ret < 0)
3025 goto fail;3022 goto fail;
3026 break;3023 break;
@@ -3032,7 +3029,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 5 unmodified lines
3032 if (ret < 0)3029 if (ret < 0)
3033 goto fail;3030 goto fail;
3034 }3031 }
3035 ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);3032 ret = ff_hevc_decode_nal_sei(&gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
3036 if (ret < 0)3033 if (ret < 0)
3037 goto fail;3034 goto fail;
3038 break;3035 break;
@@ -3052,7 +3049,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 13 unmodified lines
3052 case HEVC_NAL_RADL_R:3049 case HEVC_NAL_RADL_R:
3053 case HEVC_NAL_RASL_N:3050 case HEVC_NAL_RASL_N:
3054 case HEVC_NAL_RASL_R:3051 case HEVC_NAL_RASL_R:
3055 ret = hls_slice_header(s);3052 ret = hls_slice_header(s, &gb);
3056 if (ret < 0)3053 if (ret < 0)
3057 return ret;3054 return ret;
3058 if (ret == 1) {3055 if (ret == 1) {
@@ -3134,7 +3131,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 75 unmodified lines
3134 if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)3131 if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
3135 ctb_addr_ts = hls_slice_data_wpp(s, nal);3132 ctb_addr_ts = hls_slice_data_wpp(s, nal);
3136 else3133 else
3137 ctb_addr_ts = hls_decode_entry(s);3134 ctb_addr_ts = hls_decode_entry(s, &gb);
3138 if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {3135 if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
3139 ret = hevc_frame_end(s);3136 ret = hevc_frame_end(s);
3140 if (ret < 0)3137 if (ret < 0)
libavcodec/hevcdec.h+2−2
@@ -394,7 +394,6 @@ typedef struct HEVCLocalContext { · 393 unmodified lines
394 void *logctx;394 void *logctx;
395 const struct HEVCContext *parent;395 const struct HEVCContext *parent;
396 396
397 GetBitContext gb;
398 CABACContext cc;397 CABACContext cc;
399 398
400 /**399 /**
@@ -583,7 +582,8 @@ int ff_hevc_frame_rps(HEVCContext *s); · 182 unmodified lines
583int ff_hevc_slice_rpl(HEVCContext *s);582int ff_hevc_slice_rpl(HEVCContext *s);
584 583
585void ff_hevc_save_states(HEVCLocalContext *lc, int ctb_addr_ts);584void ff_hevc_save_states(HEVCLocalContext *lc, int ctb_addr_ts);
586int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts);585int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts,
586 const uint8_t *data, size_t size);
587int ff_hevc_sao_merge_flag_decode(HEVCLocalContext *lc);587int ff_hevc_sao_merge_flag_decode(HEVCLocalContext *lc);
588int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc);588int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc);
589int ff_hevc_sao_band_position_decode(HEVCLocalContext *lc);589int ff_hevc_sao_band_position_decode(HEVCLocalContext *lc);

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