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
237 return ret;237 return ret;
238}238}
239 239
240static AVBufferRef *nvdec_decoder_frame_alloc(void *opaque, size_t size)240static int nvdec_decoder_frame_init(FFRefStructOpaque opaque, void *obj)
241{241{
242 NVDECFramePool *pool = opaque;242 NVDECFramePool *pool = opaque.nc;
243 AVBufferRef *ret;243 unsigned int *intp = obj;
244 244
245 if (pool->nb_allocated >= pool->dpb_size)245 if (pool->nb_allocated >= pool->dpb_size)
246 return NULL;246 return AVERROR(ENOMEM);
247 247
248 ret = av_buffer_alloc(sizeof(unsigned int));248 *intp = pool->nb_allocated++;
249 if (!ret)
250 return NULL;
251 249
252 *(unsigned int*)ret->data = pool->nb_allocated++;250 return 0;
251}
253 252
254 return ret;253static void nvdec_decoder_frame_pool_free(FFRefStructOpaque opaque)
254{
255 av_free(opaque.nc);
255}256}
256 257
257int ff_nvdec_decode_uninit(AVCodecContext *avctx)258int ff_nvdec_decode_uninit(AVCodecContext *avctx)
@@ -268,7 +269,7 @@ int ff_nvdec_decode_uninit(AVCodecContext *avctx) · 10 unmodified lines
268 ctx->slice_offsets_allocated = 0;269 ctx->slice_offsets_allocated = 0;
269 270
270 ff_refstruct_unref(&ctx->decoder);271 ff_refstruct_unref(&ctx->decoder);
271 av_buffer_pool_uninit(&ctx->decoder_pool);272 ff_refstruct_pool_uninit(&ctx->decoder_pool);
272 273
273 return 0;274 return 0;
274}275}
@@ -424,8 +425,9 @@ int ff_nvdec_decode_init(AVCodecContext *avctx) · 149 unmodified lines
424 }425 }
425 pool->dpb_size = frames_ctx->initial_pool_size;426 pool->dpb_size = frames_ctx->initial_pool_size;
426 427
427 ctx->decoder_pool = av_buffer_pool_init2(sizeof(int), pool,428 ctx->decoder_pool = ff_refstruct_pool_alloc_ext(sizeof(unsigned int), 0, pool,
428 nvdec_decoder_frame_alloc, av_free);429 nvdec_decoder_frame_init,
430 NULL, NULL, nvdec_decoder_frame_pool_free);
429 if (!ctx->decoder_pool) {431 if (!ctx->decoder_pool) {
430 ret = AVERROR(ENOMEM);432 ret = AVERROR(ENOMEM);
431 goto fail;433 goto fail;
@@ -444,8 +446,8 @@ static void nvdec_fdd_priv_free(void *priv) · 12 unmodified lines
444 if (!cf)446 if (!cf)
445 return;447 return;
446 448
447 av_buffer_unref(&cf->idx_ref);449 ff_refstruct_unref(&cf->idx_ref);
448 av_buffer_unref(&cf->ref_idx_ref);450 ff_refstruct_unref(&cf->ref_idx_ref);
449 ff_refstruct_unref(&cf->decoder);451 ff_refstruct_unref(&cf->decoder);
450 452
451 av_freep(&priv);453 av_freep(&priv);
@@ -469,8 +471,8 @@ static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data) · 17 unmodified lines
469 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));471 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
470 472
471finish:473finish:
472 av_buffer_unref(&unmap_data->idx_ref);474 ff_refstruct_unref(&unmap_data->idx_ref);
473 av_buffer_unref(&unmap_data->ref_idx_ref);475 ff_refstruct_unref(&unmap_data->ref_idx_ref);
474 ff_refstruct_unref(&unmap_data->decoder);476 ff_refstruct_unref(&unmap_data->decoder);
475 av_free(unmap_data);477 av_free(unmap_data);
476}478}
@@ -526,10 +528,7 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame) · 49 unmodified lines
526 goto copy_fail;528 goto copy_fail;
527 529
528 unmap_data->idx = cf->idx;530 unmap_data->idx = cf->idx;
529 if (!(unmap_data->idx_ref = av_buffer_ref(cf->idx_ref))) {531 unmap_data->idx_ref = ff_refstruct_ref(cf->idx_ref);
530 ret = AVERROR(ENOMEM);
531 goto copy_fail;
532 }
533 unmap_data->decoder = ff_refstruct_ref(cf->decoder);532 unmap_data->decoder = ff_refstruct_ref(cf->decoder);
534 533
535 av_pix_fmt_get_chroma_sub_sample(hwctx->sw_format, &shift_h, &shift_v);534 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
577 576
578 cf->decoder = ff_refstruct_ref(ctx->decoder);577 cf->decoder = ff_refstruct_ref(ctx->decoder);
579 578
580 cf->idx_ref = av_buffer_pool_get(ctx->decoder_pool);579 cf->idx_ref = ff_refstruct_pool_get(ctx->decoder_pool);
581 if (!cf->idx_ref) {580 if (!cf->idx_ref) {
582 av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");581 av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
583 ret = AVERROR(ENOMEM);582 ret = AVERROR(ENOMEM);
584 goto fail;583 goto fail;
585 }584 }
586 cf->ref_idx = cf->idx = *(unsigned int*)cf->idx_ref->data;585 cf->ref_idx = cf->idx = *cf->idx_ref;
587 586
588 fdd->hwaccel_priv = cf;587 fdd->hwaccel_priv = cf;
589 fdd->hwaccel_priv_free = nvdec_fdd_priv_free;588 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
611 610
612 if (has_sep_ref) {611 if (has_sep_ref) {
613 if (!cf->ref_idx_ref) {612 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);
615 if (!cf->ref_idx_ref) {614 if (!cf->ref_idx_ref) {
616 av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");615 av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
617 ret = AVERROR(ENOMEM);616 ret = AVERROR(ENOMEM);
618 goto fail;617 goto fail;
619 }618 }
620 }619 }
621 cf->ref_idx = *(unsigned int*)cf->ref_idx_ref->data;620 cf->ref_idx = *cf->ref_idx_ref;
622 } else {621 } else {
623 av_buffer_unref(&cf->ref_idx_ref);622 ff_refstruct_unref(&cf->ref_idx_ref);
624 cf->ref_idx = cf->idx;623 cf->ref_idx = cf->idx;
625 }624 }
626 625
libavcodec/nvdec.h+3−3
@@ -44,15 +44,15 @@ · 43 unmodified lines
44typedef struct NVDECFrame {44typedef struct NVDECFrame {
45 unsigned int idx;45 unsigned int idx;
46 unsigned int ref_idx;46 unsigned int ref_idx;
47 AVBufferRef *idx_ref;47 unsigned int *idx_ref; ///< RefStruct reference
48 AVBufferRef *ref_idx_ref;48 unsigned int *ref_idx_ref; ///< RefStruct reference
49 struct NVDECDecoder *decoder; ///< RefStruct reference49 struct NVDECDecoder *decoder; ///< RefStruct reference
50} NVDECFrame;50} NVDECFrame;
51 51
52typedef struct NVDECContext {52typedef struct NVDECContext {
53 CUVIDPICPARAMS pic_params;53 CUVIDPICPARAMS pic_params;
54 54
55 AVBufferPool *decoder_pool;55 struct FFRefStructPool *decoder_pool;
56 56
57 struct NVDECDecoder *decoder; ///< RefStruct reference57 struct NVDECDecoder *decoder; ///< RefStruct reference
58 58

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