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
427427 return skip_bytes(&lc->cc, 0) == NULL ? AVERROR_INVALIDDATA : 0;
428428 }
429429
430−static 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−
438430 static void cabac_init_state(HEVCLocalContext *lc, const HEVCContext *s)
439431 {
440432 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
459451 lc->stat_coeff[i] = 0;
460452 }
461453
462−int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts)
454+int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts,
455+ const uint8_t *data, size_t size)
463456 {
464457 const HEVCContext *const s = lc->parent;
465458
466459 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);
468461 if (ret < 0)
469462 return ret;
470463 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
488481 if (s->threads_number == 1)
489482 ret = cabac_reinit(lc);
490483 else {
491− ret = cabac_init_decoder(lc);
484+ ret = ff_init_cabac_decoder(&lc->cc, data, size);
492485 }
493486 if (ret < 0)
494487 return ret;
@@ -501,7 +494,7 @@ int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts) · 6 unmodified lines
501494 if (s->threads_number == 1)
502495 ret = cabac_reinit(lc);
503496 else {
504− ret = cabac_init_decoder(lc);
497+ ret = ff_init_cabac_decoder(&lc->cc, data, size);
505498 }
506499 if (ret < 0)
507500 return ret;
libavcodec/hevcdec.c+20−23
@@ -593,9 +593,8 @@ fail: · 592 unmodified lines
593593 return ret;
594594 }
595595
596−static int hls_slice_header(HEVCContext *s)
596+static int hls_slice_header(HEVCContext *s, GetBitContext *gb)
597597 {
598− GetBitContext *gb = &s->HEVClc->gb;
599598 SliceHeader *sh = &s->sh;
600599 int i, ret;
601600
@@ -2533,9 +2532,11 @@ static void hls_decode_neighbour(HEVCLocalContext *lc, int x_ctb, int y_ctb, · 1931 unmodified lines
25332532 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]]));
25342533 }
25352534
2536−static int hls_decode_entry(HEVCContext *s)
2535+static int hls_decode_entry(HEVCContext *s, GetBitContext *gb)
25372536 {
25382537 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;
25392540 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
25402541 int more_data = 1;
25412542 int x_ctb = 0;
@@ -2563,7 +2564,7 @@ static int hls_decode_entry(HEVCContext *s) · 21 unmodified lines
25632564 y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
25642565 hls_decode_neighbour(lc, x_ctb, y_ctb, ctb_addr_ts);
25652566
2566− ret = ff_hevc_cabac_init(lc, ctb_addr_ts);
2567+ ret = ff_hevc_cabac_init(lc, ctb_addr_ts, slice_data, slice_size);
25672568 if (ret < 0) {
25682569 s->tab_slice_address[ctb_addr_rs] = -1;
25692570 return ret;
@@ -2605,14 +2606,14 @@ static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *hevc_lclist, · 35 unmodified lines
26052606 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);
26062607 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
26072608 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+
26082613 int ret;
26092614
2610− if(ctb_row) {
2611− ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row], s->sh.size[ctb_row]);
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− }
2615+ if (ctb_row)
2616+ ff_init_cabac_decoder(&lc->cc, data, data_size);
26162617
26172618 while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
26182619 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
26302631 return 0;
26312632 }
26322633
2633− ret = ff_hevc_cabac_init(lc, ctb_addr_ts);
2634+ ret = ff_hevc_cabac_init(lc, ctb_addr_ts, data, data_size);
26342635 if (ret < 0)
26352636 goto error;
26362637 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
26812682 {
26822683 const uint8_t *data = nal->data;
26832684 int length = nal->size;
2684− HEVCLocalContext *lc;
26852685 int *ret;
26862686 int64_t offset;
26872687 int64_t startheader, cmpt = 0;
@@ -2719,8 +2719,7 @@ static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal) · 31 unmodified lines
27192719 s->nb_local_ctx = s->threads_number;
27202720 }
27212721
2722− lc = &s->local_ctx[0];
2723− offset = (lc->gb.index >> 3);
2722+ offset = s->sh.data_offset;
27242723
27252724 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
27262725 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
29812980
29822981 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
29832982 {
2984− HEVCLocalContext *lc = s->HEVClc;
2985− GetBitContext *gb = &lc->gb;
2983+ GetBitContext gb = nal->gb;
29862984 int ctb_addr_ts, ret;
29872985
2988− *gb = nal->gb;
29892986 s->nal_unit_type = nal->type;
29902987 s->temporal_id = nal->temporal_id;
29912988
@@ -2997,7 +2994,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 5 unmodified lines
29972994 if (ret < 0)
29982995 goto fail;
29992996 }
3000− ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2997+ ret = ff_hevc_decode_nal_vps(&gb, s->avctx, &s->ps);
30012998 if (ret < 0)
30022999 goto fail;
30033000 break;
@@ -3008,7 +3005,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 4 unmodified lines
30083005 if (ret < 0)
30093006 goto fail;
30103007 }
3011− ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
3008+ ret = ff_hevc_decode_nal_sps(&gb, s->avctx, &s->ps,
30123009 s->apply_defdispwin);
30133010 if (ret < 0)
30143011 goto fail;
@@ -3020,7 +3017,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 5 unmodified lines
30203017 if (ret < 0)
30213018 goto fail;
30223019 }
3023− ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
3020+ ret = ff_hevc_decode_nal_pps(&gb, s->avctx, &s->ps);
30243021 if (ret < 0)
30253022 goto fail;
30263023 break;
@@ -3032,7 +3029,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 5 unmodified lines
30323029 if (ret < 0)
30333030 goto fail;
30343031 }
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);
30363033 if (ret < 0)
30373034 goto fail;
30383035 break;
@@ -3052,7 +3049,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 13 unmodified lines
30523049 case HEVC_NAL_RADL_R:
30533050 case HEVC_NAL_RASL_N:
30543051 case HEVC_NAL_RASL_R:
3055− ret = hls_slice_header(s);
3052+ ret = hls_slice_header(s, &gb);
30563053 if (ret < 0)
30573054 return ret;
30583055 if (ret == 1) {
@@ -3134,7 +3131,7 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal) · 75 unmodified lines
31343131 if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
31353132 ctb_addr_ts = hls_slice_data_wpp(s, nal);
31363133 else
3137− ctb_addr_ts = hls_decode_entry(s);
3134+ ctb_addr_ts = hls_decode_entry(s, &gb);
31383135 if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
31393136 ret = hevc_frame_end(s);
31403137 if (ret < 0)
libavcodec/hevcdec.h+2−2
@@ -394,7 +394,6 @@ typedef struct HEVCLocalContext { · 393 unmodified lines
394394 void *logctx;
395395 const struct HEVCContext *parent;
396396
397− GetBitContext gb;
398397 CABACContext cc;
399398
400399 /**
@@ -583,7 +582,8 @@ int ff_hevc_frame_rps(HEVCContext *s); · 182 unmodified lines
583582 int ff_hevc_slice_rpl(HEVCContext *s);
584583
585584 void ff_hevc_save_states(HEVCLocalContext *lc, int ctb_addr_ts);
586−int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts);
585+int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts,
586+ const uint8_t *data, size_t size);
587587 int ff_hevc_sao_merge_flag_decode(HEVCLocalContext *lc);
588588 int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc);
589589 int 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