History

nvdec: attach real hw_frames to post-processed frames

Timo Rothenpieler committed 6 years ago

Commit 767f5351 parent 33623302 files · +76 −18Browse files

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

Unified · Split

2 files
  1. libavcodec/nvdec.c +75 −17
  2. libavcodec/version.h +1 −1
libavcodec/nvdec.c+75−17
@@ -44,6 +44,7 @@ typedef struct NVDECDecoder { · 43 unmodified lines
4444 CUvideodecoder decoder;
4545
4646 AVBufferRef *hw_device_ref;
47+ AVBufferRef *real_hw_frames_ref;
4748 CUcontext cuda_ctx;
4849 CUstream stream;
4950
@@ -163,6 +164,7 @@ static void nvdec_decoder_free(void *opaque, uint8_t *data) · 113 unmodified lines
163164 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
164165 }
165166
167+ av_buffer_unref(&decoder->real_hw_frames_ref);
166168 av_buffer_unref(&decoder->hw_device_ref);
167169
168170 cuvid_free_functions(&decoder->cvdl);
@@ -269,10 +271,61 @@ int ff_nvdec_decode_uninit(AVCodecContext *avctx) · 100 unmodified lines
269271 return 0;
270272 }
271273
274+static void nvdec_free_dummy(struct AVHWFramesContext *ctx)
275+{
276+ av_buffer_pool_uninit(&ctx->pool);
277+}
278+
279+static AVBufferRef *nvdec_alloc_dummy(int size)
280+{
281+ return av_buffer_create(NULL, 0, NULL, NULL, 0);
282+}
283+
284+static int nvdec_init_hwframes(AVCodecContext *avctx, AVBufferRef **out_frames_ref, int dummy)
285+{
286+ AVHWFramesContext *frames_ctx;
287+ int ret;
288+
289+ ret = avcodec_get_hw_frames_parameters(avctx,
290+ avctx->hw_device_ctx,
291+ avctx->hwaccel->pix_fmt,
292+ out_frames_ref);
293+ if (ret < 0)
294+ return ret;
295+
296+ frames_ctx = (AVHWFramesContext*)(*out_frames_ref)->data;
297+
298+ if (dummy) {
299+ // Copied from ff_decode_get_hw_frames_ctx for compatibility
300+ frames_ctx->initial_pool_size += 3;
301+
302+ frames_ctx->free = nvdec_free_dummy;
303+ frames_ctx->pool = av_buffer_pool_init(0, nvdec_alloc_dummy);
304+
305+ if (!frames_ctx->pool) {
306+ av_buffer_unref(out_frames_ref);
307+ return AVERROR(ENOMEM);
308+ }
309+ } else {
310+ // This is normally not used to actually allocate frames from
311+ frames_ctx->initial_pool_size = 0;
312+ }
313+
314+ ret = av_hwframe_ctx_init(*out_frames_ref);
315+ if (ret < 0) {
316+ av_buffer_unref(out_frames_ref);
317+ return ret;
318+ }
319+
320+ return 0;
321+}
322+
272323 int ff_nvdec_decode_init(AVCodecContext *avctx)
273324 {
274325 NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
275326
327+ NVDECDecoder *decoder;
328+ AVBufferRef *real_hw_frames_ref;
276329 NVDECFramePool *pool;
277330 AVHWFramesContext *frames_ctx;
278331 const AVPixFmtDescriptor *sw_desc;
@@ -301,9 +354,17 @@ int ff_nvdec_decode_init(AVCodecContext *avctx) · 22 unmodified lines
301354 chroma_444 = ctx->supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
302355
303356 if (!avctx->hw_frames_ctx) {
304− ret = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_CUDA);
357+ ret = nvdec_init_hwframes(avctx, &avctx->hw_frames_ctx, 1);
358+ if (ret < 0)
359+ return ret;
360+
361+ ret = nvdec_init_hwframes(avctx, &real_hw_frames_ref, 0);
305362 if (ret < 0)
306363 return ret;
364+ } else {
365+ real_hw_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
366+ if (!real_hw_frames_ref)
367+ return AVERROR(ENOMEM);
307368 }
308369
309370 switch (sw_desc->comp[0].depth) {
@@ -318,6 +379,7 @@ int ff_nvdec_decode_init(AVCodecContext *avctx) · 8 unmodified lines
318379 break;
319380 default:
320381 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth\n");
382+ av_buffer_unref(&real_hw_frames_ref);
321383 return AVERROR(ENOSYS);
322384 }
323385
@@ -342,9 +404,14 @@ int ff_nvdec_decode_init(AVCodecContext *avctx) · 18 unmodified lines
342404 av_log(avctx, AV_LOG_WARNING, "Try lowering the amount of threads. Using %d right now.\n",
343405 avctx->thread_count);
344406 }
407+ av_buffer_unref(&real_hw_frames_ref);
345408 return ret;
346409 }
347410
411+ decoder = (NVDECDecoder*)ctx->decoder_ref->data;
412+ decoder->real_hw_frames_ref = real_hw_frames_ref;
413+ real_hw_frames_ref = NULL;
414+
348415 pool = av_mallocz(sizeof(*pool));
349416 if (!pool) {
350417 ret = AVERROR(ENOMEM);
@@ -447,6 +514,13 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame) · 96 unmodified lines
447514 goto copy_fail;
448515 }
449516
517+ av_buffer_unref(&frame->hw_frames_ctx);
518+ frame->hw_frames_ctx = av_buffer_ref(decoder->real_hw_frames_ref);
519+ if (!frame->hw_frames_ctx) {
520+ ret = AVERROR(ENOMEM);
521+ goto copy_fail;
522+ }
523+
450524 unmap_data->idx = cf->idx;
451525 unmap_data->idx_ref = av_buffer_ref(cf->idx_ref);
452526 unmap_data->decoder_ref = av_buffer_ref(cf->decoder_ref);
@@ -575,16 +649,6 @@ int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, · 122 unmodified lines
575649 return 0;
576650 }
577651
578−static void nvdec_free_dummy(struct AVHWFramesContext *ctx)
579−{
580− av_buffer_pool_uninit(&ctx->pool);
581−}
582−
583−static AVBufferRef *nvdec_alloc_dummy(int size)
584−{
585− return av_buffer_create(NULL, 0, NULL, NULL, 0);
586−}
587−
588652 int ff_nvdec_frame_params(AVCodecContext *avctx,
589653 AVBufferRef *hw_frames_ctx,
590654 int dpb_size,
@@ -620,12 +684,6 @@ int ff_nvdec_frame_params(AVCodecContext *avctx, · 29 unmodified lines
620684 */
621685 frames_ctx->initial_pool_size = dpb_size + 2;
622686
623− frames_ctx->free = nvdec_free_dummy;
624− frames_ctx->pool = av_buffer_pool_init(0, nvdec_alloc_dummy);
625−
626− if (!frames_ctx->pool)
627− return AVERROR(ENOMEM);
628−
629687 switch (sw_desc->comp[0].depth) {
630688 case 8:
631689 frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_NV12;
libavcodec/version.h+1−1
@@ -29,7 +29,7 @@ · 28 unmodified lines
2929
3030 #define LIBAVCODEC_VERSION_MAJOR 58
3131 #define LIBAVCODEC_VERSION_MINOR 77
32−#define LIBAVCODEC_VERSION_MICRO 100
32+#define LIBAVCODEC_VERSION_MICRO 101
3333
3434 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
3535 LIBAVCODEC_VERSION_MINOR, \

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