History

avcodec/nvdec: Use RefStruct-pool API for decoder pool

It involves less allocations, in particular no allocations after the entry has been created. Therefore creating a new reference from an existing one can't fail and therefore need not be checked. It also avoids indirections and casts. Also note that nvdec_decoder_frame_init() (the callback to initialize new entries from the pool) does not use atomics to read and replace the number of entries currently used by the pool. This relies on nvdec (like most other hwaccels) not being run in a truely frame-threaded way.

Andreas Rheinhardt committed 4 years ago

Commit e01e30e1 parent fd2e6582 files · +27 −28Browse files

Unified · Split

2 files
  1. libavcodec/nvdec.c +24 −25
  2. libavcodec/nvdec.h +3 −3
libavcodec/nvdec.c+24−25
@@ -237,21 +237,22 @@ fail: · 236 unmodified lines
237237 return ret;
238238 }
239239
240−static AVBufferRef *nvdec_decoder_frame_alloc(void *opaque, size_t size)
240+static int nvdec_decoder_frame_init(FFRefStructOpaque opaque, void *obj)
241241 {
242− NVDECFramePool *pool = opaque;
243− AVBufferRef *ret;
242+ NVDECFramePool *pool = opaque.nc;
243+ unsigned int *intp = obj;
244244
245245 if (pool->nb_allocated >= pool->dpb_size)
246− return NULL;
246+ return AVERROR(ENOMEM);
247247
248− ret = av_buffer_alloc(sizeof(unsigned int));
249− if (!ret)
250− return NULL;
248+ *intp = pool->nb_allocated++;
251249
252− *(unsigned int*)ret->data = pool->nb_allocated++;
250+ return 0;
251+}
253252
254− return ret;
253+static void nvdec_decoder_frame_pool_free(FFRefStructOpaque opaque)
254+{
255+ av_free(opaque.nc);
255256 }
256257
257258 int ff_nvdec_decode_uninit(AVCodecContext *avctx)
@@ -268,7 +269,7 @@ int ff_nvdec_decode_uninit(AVCodecContext *avctx) · 10 unmodified lines
268269 ctx->slice_offsets_allocated = 0;
269270
270271 ff_refstruct_unref(&ctx->decoder);
271− av_buffer_pool_uninit(&ctx->decoder_pool);
272+ ff_refstruct_pool_uninit(&ctx->decoder_pool);
272273
273274 return 0;
274275 }
@@ -424,8 +425,9 @@ int ff_nvdec_decode_init(AVCodecContext *avctx) · 149 unmodified lines
424425 }
425426 pool->dpb_size = frames_ctx->initial_pool_size;
426427
427− ctx->decoder_pool = av_buffer_pool_init2(sizeof(int), pool,
428− nvdec_decoder_frame_alloc, av_free);
428+ ctx->decoder_pool = ff_refstruct_pool_alloc_ext(sizeof(unsigned int), 0, pool,
429+ nvdec_decoder_frame_init,
430+ NULL, NULL, nvdec_decoder_frame_pool_free);
429431 if (!ctx->decoder_pool) {
430432 ret = AVERROR(ENOMEM);
431433 goto fail;
@@ -444,8 +446,8 @@ static void nvdec_fdd_priv_free(void *priv) · 12 unmodified lines
444446 if (!cf)
445447 return;
446448
447− av_buffer_unref(&cf->idx_ref);
448− av_buffer_unref(&cf->ref_idx_ref);
449+ ff_refstruct_unref(&cf->idx_ref);
450+ ff_refstruct_unref(&cf->ref_idx_ref);
449451 ff_refstruct_unref(&cf->decoder);
450452
451453 av_freep(&priv);
@@ -469,8 +471,8 @@ static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data) · 17 unmodified lines
469471 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
470472
471473 finish:
472− av_buffer_unref(&unmap_data->idx_ref);
473− av_buffer_unref(&unmap_data->ref_idx_ref);
474+ ff_refstruct_unref(&unmap_data->idx_ref);
475+ ff_refstruct_unref(&unmap_data->ref_idx_ref);
474476 ff_refstruct_unref(&unmap_data->decoder);
475477 av_free(unmap_data);
476478 }
@@ -526,10 +528,7 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame) · 49 unmodified lines
526528 goto copy_fail;
527529
528530 unmap_data->idx = cf->idx;
529− if (!(unmap_data->idx_ref = av_buffer_ref(cf->idx_ref))) {
530− ret = AVERROR(ENOMEM);
531− goto copy_fail;
532− }
531+ unmap_data->idx_ref = ff_refstruct_ref(cf->idx_ref);
533532 unmap_data->decoder = ff_refstruct_ref(cf->decoder);
534533
535534 av_pix_fmt_get_chroma_sub_sample(hwctx->sw_format, &shift_h, &shift_v);
@@ -577,13 +576,13 @@ int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame) · 41 unmodified lines
577576
578577 cf->decoder = ff_refstruct_ref(ctx->decoder);
579578
580− cf->idx_ref = av_buffer_pool_get(ctx->decoder_pool);
579+ cf->idx_ref = ff_refstruct_pool_get(ctx->decoder_pool);
581580 if (!cf->idx_ref) {
582581 av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
583582 ret = AVERROR(ENOMEM);
584583 goto fail;
585584 }
586− cf->ref_idx = cf->idx = *(unsigned int*)cf->idx_ref->data;
585+ cf->ref_idx = cf->idx = *cf->idx_ref;
587586
588587 fdd->hwaccel_priv = cf;
589588 fdd->hwaccel_priv_free = nvdec_fdd_priv_free;
@@ -611,16 +610,16 @@ int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_ · 21 unmodified lines
611610
612611 if (has_sep_ref) {
613612 if (!cf->ref_idx_ref) {
614− cf->ref_idx_ref = av_buffer_pool_get(ctx->decoder_pool);
613+ cf->ref_idx_ref = ff_refstruct_pool_get(ctx->decoder_pool);
615614 if (!cf->ref_idx_ref) {
616615 av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
617616 ret = AVERROR(ENOMEM);
618617 goto fail;
619618 }
620619 }
621− cf->ref_idx = *(unsigned int*)cf->ref_idx_ref->data;
620+ cf->ref_idx = *cf->ref_idx_ref;
622621 } else {
623− av_buffer_unref(&cf->ref_idx_ref);
622+ ff_refstruct_unref(&cf->ref_idx_ref);
624623 cf->ref_idx = cf->idx;
625624 }
626625
libavcodec/nvdec.h+3−3
@@ -44,15 +44,15 @@ · 43 unmodified lines
4444 typedef struct NVDECFrame {
4545 unsigned int idx;
4646 unsigned int ref_idx;
47− AVBufferRef *idx_ref;
48− AVBufferRef *ref_idx_ref;
47+ unsigned int *idx_ref; ///< RefStruct reference
48+ unsigned int *ref_idx_ref; ///< RefStruct reference
4949 struct NVDECDecoder *decoder; ///< RefStruct reference
5050 } NVDECFrame;
5151
5252 typedef struct NVDECContext {
5353 CUVIDPICPARAMS pic_params;
5454
55− AVBufferPool *decoder_pool;
55+ struct FFRefStructPool *decoder_pool;
5656
5757 struct NVDECDecoder *decoder; ///< RefStruct reference
5858

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