History

lavc/hevcdec: allocate local_ctx as array of structs rather than pointers

It is more efficient and easier to manage.

Anton Khirnov committed 2 years ago

Commit f0aece91 parent 25ce44e2 files · +36 −25Browse files

Showing the changes to libavcodec (2 of 2 files). Show all

Unified · Split

2 files
  1. libavcodec/hevcdec.c +31 −24
  2. libavcodec/hevcdec.h +5 −1
libavcodec/hevcdec.c+31−24
@@ -2598,7 +2598,7 @@ static int hls_slice_data(HEVCContext *s) · 2597 unmodified lines
25982598 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *hevc_lclist,
25992599 int job, int self_id)
26002600 {
2601− HEVCLocalContext *lc = ((HEVCLocalContext**)hevc_lclist)[self_id];
2601+ HEVCLocalContext *lc = &((HEVCLocalContext*)hevc_lclist)[self_id];
26022602 const HEVCContext *const s = lc->parent;
26032603 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
26042604 int more_data = 1;
@@ -2682,7 +2682,7 @@ static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal) · 77 unmodified lines
26822682 {
26832683 const uint8_t *data = nal->data;
26842684 int length = nal->size;
2685− HEVCLocalContext *lc = s->HEVClc;
2685+ HEVCLocalContext *lc;
26862686 int *ret;
26872687 int64_t offset;
26882688 int64_t startheader, cmpt = 0;
@@ -2696,19 +2696,31 @@ static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal) · 7 unmodified lines
26962696 return AVERROR_INVALIDDATA;
26972697 }
26982698
2699− for (i = 1; i < s->threads_number; i++) {
2700− if (i < s->nb_local_ctx)
2701− continue;
2702− s->local_ctx[i] = av_mallocz(sizeof(HEVCLocalContext));
2703− if (!s->local_ctx[i])
2699+ if (s->threads_number > s->nb_local_ctx) {
2700+ HEVCLocalContext *tmp = av_malloc_array(s->threads_number, sizeof(*s->local_ctx));
2701+
2702+ if (!tmp)
27042703 return AVERROR(ENOMEM);
2705− s->nb_local_ctx++;
27062704
2707− s->local_ctx[i]->logctx = s->avctx;
2708− s->local_ctx[i]->parent = s;
2709− s->local_ctx[i]->common_cabac_state = &s->cabac;
2705+ memcpy(tmp, s->local_ctx, sizeof(*s->local_ctx) * s->nb_local_ctx);
2706+ av_free(s->local_ctx);
2707+ s->local_ctx = tmp;
2708+ s->HEVClc = &s->local_ctx[0];
2709+
2710+ for (unsigned i = s->nb_local_ctx; i < s->threads_number; i++) {
2711+ tmp = &s->local_ctx[i];
2712+
2713+ memset(tmp, 0, sizeof(*tmp));
2714+
2715+ tmp->logctx = s->avctx;
2716+ tmp->parent = s;
2717+ tmp->common_cabac_state = &s->cabac;
2718+ }
2719+
2720+ s->nb_local_ctx = s->threads_number;
27102721 }
27112722
2723+ lc = &s->local_ctx[0];
27122724 offset = (lc->gb.index >> 3);
27132725
27142726 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
@@ -2744,8 +2756,8 @@ static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal) · 29 unmodified lines
27442756 s->data = data;
27452757
27462758 for (i = 1; i < s->threads_number; i++) {
2747− s->local_ctx[i]->first_qp_group = 1;
2748− s->local_ctx[i]->qp_y = s->HEVClc->qp_y;
2759+ s->local_ctx[i].first_qp_group = 1;
2760+ s->local_ctx[i].qp_y = s->HEVClc->qp_y;
27492761 }
27502762
27512763 atomic_store(&s->wpp_err, 0);
@@ -3474,12 +3486,6 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx) · 722 unmodified lines
34743486 av_freep(&s->sh.offset);
34753487 av_freep(&s->sh.size);
34763488
3477− if (s->local_ctx) {
3478− for (i = 1; i < s->nb_local_ctx; i++) {
3479− av_freep(&s->local_ctx[i]);
3480− }
3481− }
3482− av_freep(&s->HEVClc);
34833489 av_freep(&s->local_ctx);
34843490
34853491 ff_h2645_packet_uninit(&s->pkt);
@@ -3496,15 +3502,16 @@ static av_cold int hevc_init_context(AVCodecContext *avctx) · 10 unmodified lines
34963502
34973503 s->avctx = avctx;
34983504
3499− s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
3500− s->local_ctx = av_mallocz(sizeof(HEVCLocalContext*) * s->threads_number);
3501− if (!s->HEVClc || !s->local_ctx)
3505+ s->local_ctx = av_mallocz(sizeof(*s->local_ctx));
3506+ if (!s->local_ctx)
35023507 return AVERROR(ENOMEM);
3508+ s->nb_local_ctx = 1;
3509+
3510+ s->HEVClc = &s->local_ctx[0];
3511+
35033512 s->HEVClc->parent = s;
35043513 s->HEVClc->logctx = avctx;
35053514 s->HEVClc->common_cabac_state = &s->cabac;
3506− s->local_ctx[0] = s->HEVClc;
3507− s->nb_local_ctx = 1;
35083515
35093516 s->output_frame = av_frame_alloc();
35103517 if (!s->output_frame)
libavcodec/hevcdec.h+5−1
@@ -439,13 +439,17 @@ typedef struct HEVCLocalContext { · 438 unmodified lines
439439 /* properties of the boundary of the current CTB for the purposes
440440 * of the deblocking filter */
441441 int boundary_flags;
442+
443+ // an array of these structs is used for per-thread state - pad its size
444+ // to avoid false sharing
445+ char padding[128];
442446 } HEVCLocalContext;
443447
444448 typedef struct HEVCContext {
445449 const AVClass *c; // needed by private avoptions
446450 AVCodecContext *avctx;
447451
448− HEVCLocalContext **local_ctx;
452+ HEVCLocalContext *local_ctx;
449453 unsigned nb_local_ctx;
450454
451455 HEVCLocalContext *HEVClc;

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