FFmpeg
Loading...
Searching...
No Matches
nvdec.c
Go to the documentation of this file.
1/*
2 * HW decode acceleration through NVDEC
3 *
4 * Copyright (c) 2016 Anton Khirnov
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "config.h"
24#include "config_components.h"
25
26#include <stdatomic.h>
27
28#include "libavutil/avassert.h"
29#include "libavutil/common.h"
30#include "libavutil/error.h"
31#include "libavutil/hwcontext.h"
34#include "libavutil/mem.h"
35#include "libavutil/pixdesc.h"
36#include "libavutil/pixfmt.h"
37
38#include "avcodec.h"
39#include "decode.h"
40#include "nvdec.h"
41#include "internal.h"
42#include "libavutil/refstruct.h"
43
44#if !NVDECAPI_CHECK_VERSION(9, 0)
45#define cudaVideoSurfaceFormat_YUV444 2
46#define cudaVideoSurfaceFormat_YUV444_16Bit 3
47#endif
48
49typedef struct NVDECDecoder {
50 CUvideodecoder decoder;
51
55 CUcontext cuda_ctx;
56 CUstream stream;
57
58 CudaFunctions *cudl;
59 CuvidFunctions *cvdl;
60
63#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
64 atomic_int *surface_in_use;
65 int num_surfaces;
66#endif
68
69typedef struct NVDECFramePool {
70 unsigned int dpb_size;
71 unsigned int nb_allocated;
73
74#define CHECK_CU(x) FF_CUDA_CHECK_DL(logctx, decoder->cudl, x)
75
76static int map_avcodec_id(enum AVCodecID id)
77{
78 switch (id) {
79#if CONFIG_AV1_NVDEC_HWACCEL
80 case AV_CODEC_ID_AV1: return cudaVideoCodec_AV1;
81#endif
82 case AV_CODEC_ID_H264: return cudaVideoCodec_H264;
83 case AV_CODEC_ID_HEVC: return cudaVideoCodec_HEVC;
84 case AV_CODEC_ID_MJPEG: return cudaVideoCodec_JPEG;
85 case AV_CODEC_ID_MPEG1VIDEO: return cudaVideoCodec_MPEG1;
86 case AV_CODEC_ID_MPEG2VIDEO: return cudaVideoCodec_MPEG2;
87 case AV_CODEC_ID_MPEG4: return cudaVideoCodec_MPEG4;
88 case AV_CODEC_ID_VC1: return cudaVideoCodec_VC1;
89 case AV_CODEC_ID_VP8: return cudaVideoCodec_VP8;
90 case AV_CODEC_ID_VP9: return cudaVideoCodec_VP9;
91 case AV_CODEC_ID_WMV3: return cudaVideoCodec_VC1;
92 }
93 return -1;
94}
95
97{
98 int shift_h = 0, shift_v = 0;
99
101 return cudaVideoChromaFormat_Monochrome;
102
103 av_pix_fmt_get_chroma_sub_sample(pix_fmt, &shift_h, &shift_v);
104
105 if (shift_h == 1 && shift_v == 1)
106 return cudaVideoChromaFormat_420;
107 else if (shift_h == 1 && shift_v == 0)
108 return cudaVideoChromaFormat_422;
109 else if (shift_h == 0 && shift_v == 0)
110 return cudaVideoChromaFormat_444;
111
112 return -1;
113}
114
116 CUVIDDECODECREATEINFO *params, void *logctx)
117{
118 int ret;
119 CUVIDDECODECAPS caps = { 0 };
120
121 caps.eCodecType = params->CodecType;
122 caps.eChromaFormat = params->ChromaFormat;
123 caps.nBitDepthMinus8 = params->bitDepthMinus8;
124
125 if (!decoder->cvdl->cuvidGetDecoderCaps) {
126 av_log(logctx, AV_LOG_WARNING, "Used Nvidia driver is too old to perform a capability check.\n");
127 av_log(logctx, AV_LOG_WARNING, "The minimum required version is "
128#if defined(_WIN32) || defined(__CYGWIN__)
129 "378.66"
130#else
131 "378.13"
132#endif
133 ". Continuing blind.\n");
134 return 0;
135 }
136
137 ret = CHECK_CU(decoder->cvdl->cuvidGetDecoderCaps(&caps));
138 if (ret < 0)
139 return ret;
140
141 av_log(logctx, AV_LOG_VERBOSE, "NVDEC capabilities:\n");
142 av_log(logctx, AV_LOG_VERBOSE, "format supported: %s, max_mb_count: %d\n",
143 caps.bIsSupported ? "yes" : "no", caps.nMaxMBCount);
144 av_log(logctx, AV_LOG_VERBOSE, "min_width: %d, max_width: %d\n",
145 caps.nMinWidth, caps.nMaxWidth);
146 av_log(logctx, AV_LOG_VERBOSE, "min_height: %d, max_height: %d\n",
147 caps.nMinHeight, caps.nMaxHeight);
148
149 if (!caps.bIsSupported) {
150 av_log(logctx, AV_LOG_ERROR, "Hardware is lacking required capabilities\n");
151 return AVERROR(EINVAL);
152 }
153
154 if (params->ulWidth > caps.nMaxWidth || params->ulWidth < caps.nMinWidth) {
155 av_log(logctx, AV_LOG_ERROR, "Video width %d not within range from %d to %d\n",
156 (int)params->ulWidth, caps.nMinWidth, caps.nMaxWidth);
157 return AVERROR(EINVAL);
158 }
159
160 if (params->ulHeight > caps.nMaxHeight || params->ulHeight < caps.nMinHeight) {
161 av_log(logctx, AV_LOG_ERROR, "Video height %d not within range from %d to %d\n",
162 (int)params->ulHeight, caps.nMinHeight, caps.nMaxHeight);
163 return AVERROR(EINVAL);
164 }
165
166 if ((params->ulWidth * params->ulHeight) / 256 > caps.nMaxMBCount) {
167 av_log(logctx, AV_LOG_ERROR, "Video macroblock count %d exceeds maximum of %d\n",
168 (int)(params->ulWidth * params->ulHeight) / 256, caps.nMaxMBCount);
169 return AVERROR(EINVAL);
170 }
171
172 return 0;
173}
174
175static void nvdec_decoder_free(AVRefStructOpaque unused, void *obj)
176{
178 void *logctx = decoder->hw_device_ref ? decoder->hw_device_ref->data : NULL;
179
180 if (decoder->decoder) {
181 CUcontext dummy;
182 CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
183#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
184 if (decoder->opaque_output) {
185 /*
186 * Every opaque output frame holds a reference to this decoder
187 * (NVDECOpaqueRelease.decoder, attached as frame->buf[0]) and only
188 * clears its surface_in_use slot when that buffer is released. This
189 * RefStruct free callback therefore cannot run until all surfaces
190 * have been handed back, so the decoder always outlives its
191 * surfaces and is safe to destroy unconditionally. The check below
192 * is purely a guard against a future regression of that invariant;
193 * it must never defer destruction (which would leak the decoder).
194 */
195 decoder->cudl->cuCtxSynchronize();
196 if (decoder->surface_in_use) {
197 int busy = 0;
198 for (int i = 0; i < decoder->num_surfaces; i++)
199 busy += atomic_load_explicit(&decoder->surface_in_use[i],
201 if (busy)
202 av_log(logctx, AV_LOG_ERROR,
203 "%d CUarray surface(s) unexpectedly still in use at "
204 "decoder teardown; destroying anyway\n", busy);
205 }
206 }
207#endif
208 CHECK_CU(decoder->cvdl->cuvidDestroyDecoder(decoder->decoder));
209 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
210 }
211
212#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
213 av_freep(&decoder->surface_in_use);
214#endif
215 av_buffer_unref(&decoder->decode_hw_frames_ref);
216 av_buffer_unref(&decoder->real_hw_frames_ref);
217 av_buffer_unref(&decoder->hw_device_ref);
218
219 cuvid_free_functions(&decoder->cvdl);
220}
221
223 CUVIDDECODECREATEINFO *params, void *logctx)
224{
226 AVCUDADeviceContext *device_hwctx = hw_device_ctx->hwctx;
227
229
230 CUcontext dummy;
231 int ret;
232
235 if (!decoder)
236 return AVERROR(ENOMEM);
237
238 decoder->hw_device_ref = av_buffer_ref(hw_device_ref);
239 if (!decoder->hw_device_ref) {
240 ret = AVERROR(ENOMEM);
241 goto fail;
242 }
243 decoder->cuda_ctx = device_hwctx->cuda_ctx;
244 decoder->cudl = device_hwctx->internal->cuda_dl;
245 decoder->stream = device_hwctx->stream;
246
247 ret = cuvid_load_functions(&decoder->cvdl, logctx);
248 if (ret < 0) {
249 av_log(logctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n");
250 goto fail;
251 }
252
253 ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
254 if (ret < 0)
255 goto fail;
256
257 ret = nvdec_test_capabilities(decoder, params, logctx);
258 if (ret < 0) {
259 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
260 goto fail;
261 }
262
263 ret = CHECK_CU(decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params));
264
265 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
266
267 if (ret < 0) {
268 goto fail;
269 }
270
271 *out = decoder;
272
273 return 0;
274fail:
276 return ret;
277}
278
280{
281 NVDECFramePool *pool = opaque.nc;
282 unsigned int *intp = obj;
283
284 if (pool->nb_allocated >= pool->dpb_size)
285 return AVERROR(ENOMEM);
286
287 *intp = pool->nb_allocated++;
288
289 return 0;
290}
291
293{
294 av_free(opaque.nc);
295}
296
298{
300
301 av_freep(&ctx->bitstream_internal);
302 ctx->bitstream = NULL;
303 ctx->bitstream_len = 0;
304 ctx->bitstream_allocated = 0;
305
306 av_freep(&ctx->slice_offsets);
307 ctx->nb_slices = 0;
308 ctx->slice_offsets_allocated = 0;
309
310 av_refstruct_unref(&ctx->decoder);
311 av_refstruct_pool_uninit(&ctx->decoder_pool);
312
313 return 0;
314}
315
317{
319}
320
322{
323 return av_buffer_create(NULL, 0, NULL, NULL, 0);
324}
325
326static int nvdec_init_hwframes(AVCodecContext *avctx, AVBufferRef **out_frames_ref, int dummy)
327{
328 AVHWFramesContext *frames_ctx;
329 int ret;
330
332 avctx->hw_device_ctx,
333 avctx->hwaccel->pix_fmt,
334 out_frames_ref);
335 if (ret < 0)
336 return ret;
337
338 frames_ctx = (AVHWFramesContext*)(*out_frames_ref)->data;
339
340 if (dummy) {
341 // Copied from ff_decode_get_hw_frames_ctx for compatibility
342 frames_ctx->initial_pool_size += 3;
343
344 frames_ctx->free = nvdec_free_dummy;
346
347 if (!frames_ctx->pool) {
348 av_buffer_unref(out_frames_ref);
349 return AVERROR(ENOMEM);
350 }
351 } else {
352 // This is normally not used to actually allocate frames from
353 frames_ctx->initial_pool_size = 0;
354 }
355
356 ret = av_hwframe_ctx_init(*out_frames_ref);
357 if (ret < 0) {
358 av_buffer_unref(out_frames_ref);
359 return ret;
360 }
361
362 return 0;
363}
364
365#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
366void ff_nvdec_fill_cuarray_desc(CUDA_ARRAY3D_DESCRIPTOR *desc,
367 AVCodecContext *avctx,
368 cudaVideoSurfaceFormat output_format)
369{
370 CUarray_format arr_fmt;
371
372 switch (output_format) {
373 case cudaVideoSurfaceFormat_NV12:
374 case cudaVideoSurfaceFormat_NV12_Opaque: arr_fmt = CU_AD_FORMAT_NV12; break;
375 case cudaVideoSurfaceFormat_P016:
376 case cudaVideoSurfaceFormat_P016_Opaque: arr_fmt = CU_AD_FORMAT_P016; break;
377 case cudaVideoSurfaceFormat_NV16:
378 case cudaVideoSurfaceFormat_NV16_Opaque: arr_fmt = CU_AD_FORMAT_NV16; break;
379 case cudaVideoSurfaceFormat_P216:
380 case cudaVideoSurfaceFormat_P216_Opaque: arr_fmt = CU_AD_FORMAT_P216; break;
382 case cudaVideoSurfaceFormat_YUV444_Opaque: arr_fmt = CU_AD_FORMAT_YUV444_8BIT_SEMIPLANAR; break;
384 case cudaVideoSurfaceFormat_YUV444_16Bit_Opaque: arr_fmt = CU_AD_FORMAT_YUV444_16BIT_SEMIPLANAR; break;
385 default:
386 /* The caller's bit-depth/chroma switch only ever yields the formats
387 * handled above (and errors out otherwise), so this is unreachable;
388 * assert rather than silently producing a wrong descriptor. */
389 av_assert0(0);
390 }
391
392 memset(desc, 0, sizeof(*desc));
393 desc->Width = avctx->coded_width;
394 desc->Height = avctx->coded_height;
395 desc->Depth = 0;
396 desc->Format = arr_fmt;
397 desc->NumChannels = 3;
398 desc->Flags = CUDA_ARRAY3D_SURFACE_LDST |
399 CUDA_ARRAY3D_VIDEO_ENCODE_DECODE;
400}
401
402#endif
403
405{
407
409 AVBufferRef *decode_hw_frames_ref = NULL;
410 AVBufferRef *real_hw_frames_ref = NULL;
411 NVDECFramePool *pool;
412 AVHWFramesContext *frames_ctx;
413 const AVPixFmtDescriptor *sw_desc;
414
415 CUVIDDECODECREATEINFO params = { 0 };
416
417 cudaVideoSurfaceFormat output_format;
418 int cuvid_codec_type, cuvid_chroma_format, chroma_444;
419 int ret = 0;
420 int need_real_hwframes = 0;
421
422 int unsafe_output = !!(avctx->hwaccel_flags & AV_HWACCEL_FLAG_UNSAFE_OUTPUT);
423
424 int opaque_output = 0;
425 int decode_pool_size;
426
427 sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
428 if (!sw_desc)
429 return AVERROR_BUG;
430
431 cuvid_codec_type = map_avcodec_id(avctx->codec_id);
432 if (cuvid_codec_type < 0) {
433 av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
434 return AVERROR_BUG;
435 }
436
437 cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
438 if (cuvid_chroma_format < 0) {
439 av_log(avctx, AV_LOG_ERROR, "Unsupported chroma format\n");
440 return AVERROR(ENOSYS);
441 }
442 chroma_444 = ctx->supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
443
444 if (!avctx->hw_frames_ctx) {
445 ret = nvdec_init_hwframes(avctx, &avctx->hw_frames_ctx, 1);
446 if (ret < 0)
447 return ret;
448 need_real_hwframes = 1;
449 }
450
451 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
452
453#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
454 if (frames_ctx->format == AV_PIX_FMT_CUARRAY) {
455 opaque_output = 1;
456 }
457#else
458 if (frames_ctx->format == AV_PIX_FMT_CUARRAY) {
459 av_log(avctx, AV_LOG_ERROR,
460 "CUarray opaque output requires Video Codec SDK 13.1 or later\n");
461 return AVERROR(ENOSYS);
462 }
463#endif
464
465 decode_pool_size = frames_ctx->initial_pool_size;
466#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
467 if (opaque_output) {
468 if (unsafe_output) {
469 decode_pool_size = FFMIN(frames_ctx->initial_pool_size + 16, MAX_NUM_REGISTERED_DECODE_SURFACES);
470 av_log(avctx, AV_LOG_VERBOSE, "unsafe_output + cuarray: %d decode surfaces (pool=%d, max=%d)\n",
471 decode_pool_size, frames_ctx->initial_pool_size, MAX_NUM_REGISTERED_DECODE_SURFACES);
472 } else if (avctx->extra_hw_frames > 0) {
473 decode_pool_size = FFMAX(frames_ctx->initial_pool_size - avctx->extra_hw_frames, 1);
474 av_log(avctx, AV_LOG_VERBOSE, "Opaque copy mode: %d decode surfaces, %d extra surfaces reserved for output pool\n",
475 decode_pool_size, avctx->extra_hw_frames);
476 }
477 }
478#endif
479
480 switch (sw_desc->comp[0].depth) {
481 case 8:
482 if (chroma_444) {
484#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
485 if (opaque_output) output_format = cudaVideoSurfaceFormat_YUV444_Opaque;
486#endif
487#ifdef NVDEC_HAVE_422_SUPPORT
488 } else if (cuvid_chroma_format == cudaVideoChromaFormat_422) {
489 output_format = cudaVideoSurfaceFormat_NV16;
490#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
491 if (opaque_output) output_format = cudaVideoSurfaceFormat_NV16_Opaque;
492#endif
493#endif
494 } else {
495 output_format = cudaVideoSurfaceFormat_NV12;
496#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
497 if (opaque_output) output_format = cudaVideoSurfaceFormat_NV12_Opaque;
498#endif
499 }
500 break;
501 case 10:
502 case 12:
503 if (chroma_444) {
505#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
506 if (opaque_output) output_format = cudaVideoSurfaceFormat_YUV444_16Bit_Opaque;
507#endif
508#ifdef NVDEC_HAVE_422_SUPPORT
509 } else if (cuvid_chroma_format == cudaVideoChromaFormat_422) {
510 output_format = cudaVideoSurfaceFormat_P216;
511#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
512 if (opaque_output) output_format = cudaVideoSurfaceFormat_P216_Opaque;
513#endif
514#endif
515 } else {
516 output_format = cudaVideoSurfaceFormat_P016;
517#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
518 if (opaque_output) output_format = cudaVideoSurfaceFormat_P016_Opaque;
519#endif
520 }
521 break;
522 default:
523 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth\n");
524 return AVERROR(ENOSYS);
525 }
526
527 if (need_real_hwframes) {
528 if (frames_ctx->format == AV_PIX_FMT_CUARRAY) {
529#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
530 AVHWFramesContext *real_ctx;
531 AVCUDAFramesContext *cuda_priv;
532
534 avctx->hwaccel->pix_fmt,
535 &real_hw_frames_ref);
536 if (ret < 0)
537 goto fail;
538
539 real_ctx = (AVHWFramesContext*)real_hw_frames_ref->data;
540 real_ctx->initial_pool_size = 0;
541
542 cuda_priv = real_ctx->hwctx;
543 ff_nvdec_fill_cuarray_desc(&cuda_priv->cuarray_desc, avctx, output_format);
544 cuda_priv->cuarray_num_surfaces = unsafe_output ? decode_pool_size
545 : frames_ctx->initial_pool_size;
546
547 ret = av_hwframe_ctx_init(real_hw_frames_ref);
548 if (ret < 0)
549 goto fail;
550
551 if (unsafe_output) {
552 decode_hw_frames_ref = av_buffer_ref(real_hw_frames_ref);
553 if (!decode_hw_frames_ref) {
554 ret = AVERROR(ENOMEM);
555 goto fail;
556 }
557 } else {
559 avctx->hwaccel->pix_fmt,
560 &decode_hw_frames_ref);
561 if (ret < 0)
562 goto fail;
563
564 real_ctx = (AVHWFramesContext*)decode_hw_frames_ref->data;
565 real_ctx->initial_pool_size = 0;
566
567 cuda_priv = real_ctx->hwctx;
568 ff_nvdec_fill_cuarray_desc(&cuda_priv->cuarray_desc, avctx, output_format);
569 cuda_priv->cuarray_num_surfaces = decode_pool_size;
570
571 ret = av_hwframe_ctx_init(decode_hw_frames_ref);
572 if (ret < 0)
573 goto fail;
574 }
575#endif
576 } else {
577 ret = nvdec_init_hwframes(avctx, &real_hw_frames_ref, 0);
578 if (ret < 0)
579 goto fail;
580 }
581 } else {
582 real_hw_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
583 if (!real_hw_frames_ref)
584 return AVERROR(ENOMEM);
585
586#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
587 if (frames_ctx->format == AV_PIX_FMT_CUARRAY) {
588 if (unsafe_output) {
589 decode_hw_frames_ref = av_buffer_ref(real_hw_frames_ref);
590 } else {
591 AVHWFramesContext *real_ctx;
592 AVCUDAFramesContext *cuda_priv;
593
594 ret = avcodec_get_hw_frames_parameters(avctx, frames_ctx->device_ref,
595 avctx->hwaccel->pix_fmt,
596 &decode_hw_frames_ref);
597 if (ret < 0)
598 goto fail;
599
600 real_ctx = (AVHWFramesContext*)decode_hw_frames_ref->data;
601 real_ctx->initial_pool_size = 0;
602
603 cuda_priv = real_ctx->hwctx;
604 ff_nvdec_fill_cuarray_desc(&cuda_priv->cuarray_desc, avctx, output_format);
605 cuda_priv->cuarray_num_surfaces = decode_pool_size;
606
607 ret = av_hwframe_ctx_init(decode_hw_frames_ref);
608 if (ret < 0)
609 goto fail;
610 }
611
612 if (!decode_hw_frames_ref) {
613 av_buffer_unref(&real_hw_frames_ref);
614 return AVERROR(ENOMEM);
615 }
616 }
617#endif
618 }
619
620 params.ulWidth = avctx->coded_width;
621 params.ulHeight = avctx->coded_height;
622 params.ulTargetWidth = avctx->coded_width;
623 params.ulTargetHeight = avctx->coded_height;
624 params.bitDepthMinus8 = sw_desc->comp[0].depth - 8;
625 params.OutputFormat = output_format;
626 params.CodecType = cuvid_codec_type;
627 params.ChromaFormat = cuvid_chroma_format;
628 /* Hwaccels that need every surface must not start with a capped pool, so
629 * reject the configuration and let format negotiation fall back. The pool
630 * in use is not proof on its own: frame_params() never runs for a
631 * caller-supplied frames context, so the hwaccel requirement wins. */
632 if (ctx->strict_pool_layers) {
633 int required = FFMAX(decode_pool_size, ctx->strict_pool_min);
634
635 if (required > NVDEC_MAX_DECODE_SURFACES) {
636 int fit = avctx->thread_count -
637 (required - NVDEC_MAX_DECODE_SURFACES +
638 ctx->strict_pool_layers - 1) / ctx->strict_pool_layers;
639
640 if ((avctx->active_thread_type & FF_THREAD_FRAME) && fit >= 1)
641 av_log(avctx, AV_LOG_ERROR,
642 "NVDEC requires %d decode surfaces for this multi-layer "
643 "stream, exceeding the hardware limit of %d; retry with "
644 "-threads %d or lower.\n", required,
646 else
647 av_log(avctx, AV_LOG_ERROR,
648 "NVDEC requires %d decode surfaces for this multi-layer "
649 "stream, exceeding the hardware limit of %d.\n",
650 required, NVDEC_MAX_DECODE_SURFACES);
651 ret = AVERROR(ENOSYS);
652 goto fail;
653 }
654 } else if (decode_pool_size > NVDEC_MAX_DECODE_SURFACES) {
655 av_log(avctx, AV_LOG_WARNING,
656 "NVDEC requires %d decode surfaces, exceeding the hardware limit "
657 "of %d; capping to %d. Decoding may fail for this stream.\n",
658 decode_pool_size, NVDEC_MAX_DECODE_SURFACES,
660 }
661 params.ulNumDecodeSurfaces = FFMIN(decode_pool_size, NVDEC_MAX_DECODE_SURFACES);
662 params.ulNumOutputSurfaces = opaque_output ? 0 : (unsafe_output ? FFMIN(frames_ctx->initial_pool_size, 64) : 1);
663
664 ret = nvdec_decoder_create(&ctx->decoder, frames_ctx->device_ref, &params, avctx);
665 if (ret < 0)
666 goto fail;
667
668 decoder = ctx->decoder;
669 decoder->unsafe_output = unsafe_output;
670 decoder->opaque_output = opaque_output;
671
672#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
673 if (opaque_output) {
674 void *logctx = avctx;
675 AVHWFramesContext *real_ctx = (AVHWFramesContext*)decode_hw_frames_ref->data;
676 AVCUDAFramesContext *cuda_priv = real_ctx->hwctx;
677 CUVIDREGISTERDECODESURFACESINFO reg_info = { 0 };
678 CUcontext dummy;
679
680 if (!decoder->cvdl->cuvidRegisterDecodeSurfaces) {
681 av_log(logctx, AV_LOG_ERROR, "Driver lacking CUarray output support.\n");
682 ret = AVERROR(ENOSYS);
683 goto fail;
684 }
685
686 ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
687 if (ret < 0)
688 goto fail;
689
690 reg_info.ulNumDecodeSurfaces = FFMIN(cuda_priv->cuarray_num_surfaces,
691 (unsigned)decode_pool_size);
692 reg_info.pDecodeSurfaces = cuda_priv->cuarray_surfaces;
693 ret = CHECK_CU(decoder->cvdl->cuvidRegisterDecodeSurfaces(decoder->decoder, &reg_info));
694 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
695 if (ret < 0)
696 goto fail;
697
698 decoder->num_surfaces = reg_info.ulNumDecodeSurfaces;
699 decoder->surface_in_use = av_calloc(decoder->num_surfaces,
700 sizeof(*decoder->surface_in_use));
701 if (!decoder->surface_in_use) {
702 ret = AVERROR(ENOMEM);
703 goto fail;
704 }
705 }
706#endif
707
708 decoder->decode_hw_frames_ref = decode_hw_frames_ref;
709 decode_hw_frames_ref = NULL;
710 decoder->real_hw_frames_ref = real_hw_frames_ref;
711 real_hw_frames_ref = NULL;
712
713 pool = av_mallocz(sizeof(*pool));
714 if (!pool) {
715 ret = AVERROR(ENOMEM);
716 goto fail;
717 }
718 pool->dpb_size = FFMIN(decode_pool_size, NVDEC_MAX_DECODE_SURFACES);
719
720 ctx->decoder_pool = av_refstruct_pool_alloc_ext(sizeof(unsigned int), 0, pool,
723 if (!ctx->decoder_pool) {
724 ret = AVERROR(ENOMEM);
725 goto fail;
726 }
727
728 ret = 0;
729
730fail:
731 av_buffer_unref(&decode_hw_frames_ref);
732 av_buffer_unref(&real_hw_frames_ref);
733
734 if (ret < 0)
736
737 return ret;
738}
739
740static void nvdec_fdd_priv_free(void *priv)
741{
742 NVDECFrame *cf = priv;
743
744 if (!cf)
745 return;
746
750
751 av_freep(&priv);
752}
753
754static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
755{
756 NVDECFrame *unmap_data = (NVDECFrame*)data;
757 NVDECDecoder *decoder = unmap_data->decoder;
758 void *logctx = decoder->hw_device_ref->data;
759 CUdeviceptr devptr = (CUdeviceptr)opaque;
760 int ret;
761 CUcontext dummy;
762
763 ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
764 if (ret < 0)
765 goto finish;
766
767 CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
768
769 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
770
771finish:
772 av_refstruct_unref(&unmap_data->idx_ref);
773 av_refstruct_unref(&unmap_data->ref_idx_ref);
774 av_refstruct_unref(&unmap_data->decoder);
775 av_free(unmap_data);
776}
777
778#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
779typedef struct NVDECOpaqueRelease {
780 NVDECDecoder *decoder;
781 int idx;
782 unsigned int *idx_ref; ///< RefStruct reference keeping the surface index locked
783} NVDECOpaqueRelease;
784
785static void nvdec_opaque_release_slot(void *opaque, uint8_t *data)
786{
787 NVDECOpaqueRelease *r = (NVDECOpaqueRelease *)opaque;
788 if (r->decoder->surface_in_use)
789 atomic_store_explicit(&r->decoder->surface_in_use[r->idx], 0,
791 av_refstruct_unref(&r->idx_ref);
792 av_refstruct_unref(&r->decoder);
793 av_free(r);
794}
795#endif
796
797static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
798{
799 FrameDecodeData *fdd = frame->private_ref;
802
803 AVHWFramesContext *hwctx = (AVHWFramesContext *)frame->hw_frames_ctx->data;
804
805 CUVIDPROCPARAMS vpp = { 0 };
806 NVDECFrame *unmap_data = NULL;
807
808 CUcontext dummy;
809 CUdeviceptr devptr;
810
811 unsigned int pitch, i;
812 unsigned int offset = 0;
813 int shift_h = 0, shift_v = 0;
814 int ret = 0;
815
816#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
817 if (decoder->opaque_output) {
818 AVHWFramesContext *decode_ctx = (AVHWFramesContext *)decoder->decode_hw_frames_ref->data;
819 AVCUDAFramesContext *cuda_priv = decode_ctx->hwctx;
820
821 if (cf->idx >= (unsigned)cuda_priv->cuarray_num_surfaces) {
822 av_log(logctx, AV_LOG_ERROR,
823 "NVDEC opaque surface index %u out of range (%d surfaces)\n",
824 cf->idx, cuda_priv->cuarray_num_surfaces);
825 return AVERROR_BUG;
826 }
827
828 {
829 CUarray arr = cuda_priv->cuarray_surfaces[cf->idx];
830 AVBufferRef *buf0;
831 NVDECOpaqueRelease *rel;
832
833 rel = av_malloc(sizeof(*rel));
834 if (!rel)
835 return AVERROR(ENOMEM);
836 rel->decoder = av_refstruct_ref(decoder);
837 rel->idx = cf->idx;
838 rel->idx_ref = av_refstruct_ref(cf->idx_ref);
839 buf0 = av_buffer_create((uint8_t *)arr, 0,
840 nvdec_opaque_release_slot, rel,
842 if (!buf0) {
843 av_refstruct_unref(&rel->idx_ref);
844 av_refstruct_unref(&rel->decoder);
845 av_free(rel);
846 return AVERROR(ENOMEM);
847 }
848
849 /*
850 * Mark the surface in use only now that buf0 owns rel:
851 * nvdec_opaque_release_slot (buf0's free callback) is the sole path
852 * that clears the flag, so deferring the set until buf0 exists keeps
853 * every set paired with a clear, even on a later error return.
854 */
855 if (decoder->surface_in_use)
856 atomic_store_explicit(&decoder->surface_in_use[cf->idx], 1,
858
859 ret = av_buffer_replace(&frame->hw_frames_ctx,
860 decoder->unsafe_output
861 ? decoder->decode_hw_frames_ref
862 : decoder->real_hw_frames_ref);
863 if (ret < 0) {
864 av_buffer_unref(&buf0);
865 return ret;
866 }
867
868 av_buffer_unref(&frame->buf[0]);
869 frame->buf[0] = buf0;
870 frame->data[0] = (uint8_t *)arr;
871
872 {
873 CUcontext dummy;
874 ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
875 if (ret < 0) {
876 return ret;
877 }
878 for (int p = 0; p < FF_ARRAY_ELEMS(frame->linesize); p++) {
879 CUDA_ARRAY3D_DESCRIPTOR plane_desc = { 0 };
880 CUarray plane_array;
881 int elem_size;
882 CUresult cures = decoder->cudl->cuArrayGetPlane(&plane_array, arr, p);
883 if (cures == CUDA_ERROR_INVALID_VALUE)
884 break;
885 if (cures != CUDA_SUCCESS) {
886 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
887 return AVERROR_EXTERNAL;
888 }
889 ret = CHECK_CU(decoder->cudl->cuArray3DGetDescriptor(&plane_desc, plane_array));
890 if (ret < 0) {
891 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
892 return ret;
893 }
894 elem_size = ff_cuda_cuarray_elem_size(plane_desc.Format);
895 if (elem_size <= 0)
896 elem_size = 1;
897 frame->linesize[p] = plane_desc.Width * plane_desc.NumChannels * elem_size;
898 }
899 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
900 }
901
902 frame->format = AV_PIX_FMT_CUARRAY;
903
904 if (decoder->unsafe_output)
905 return 0;
906
908 }
909 }
910#endif
911
912 vpp.progressive_frame = 1;
913 vpp.output_stream = decoder->stream;
914
915 ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
916 if (ret < 0)
917 return ret;
918
919 ret = CHECK_CU(decoder->cvdl->cuvidMapVideoFrame(decoder->decoder,
920 cf->idx, &devptr,
921 &pitch, &vpp));
922 if (ret < 0)
923 goto finish;
924
925 unmap_data = av_mallocz(sizeof(*unmap_data));
926 if (!unmap_data) {
927 ret = AVERROR(ENOMEM);
928 goto copy_fail;
929 }
930
931 frame->buf[1] = av_buffer_create((uint8_t *)unmap_data, sizeof(*unmap_data),
932 nvdec_unmap_mapped_frame, (void*)devptr,
934 if (!frame->buf[1]) {
935 ret = AVERROR(ENOMEM);
936 goto copy_fail;
937 }
938
939 ret = av_buffer_replace(&frame->hw_frames_ctx, decoder->real_hw_frames_ref);
940 if (ret < 0)
941 goto copy_fail;
942
943 unmap_data->idx = cf->idx;
944 unmap_data->decoder = av_refstruct_ref(cf->decoder);
945
946 av_pix_fmt_get_chroma_sub_sample(hwctx->sw_format, &shift_h, &shift_v);
947 for (i = 0; frame->linesize[i]; i++) {
948 frame->data[i] = (uint8_t*)(devptr + offset);
949 frame->linesize[i] = pitch;
950 offset += pitch * (frame->height >> (i ? shift_v : 0));
951 }
952
953 goto finish;
954
955copy_fail:
956 if (!frame->buf[1]) {
957 CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
958 av_freep(&unmap_data);
959 } else {
960 av_buffer_unref(&frame->buf[1]);
961 }
962
963finish:
964 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
965
966 if (ret < 0 || decoder->unsafe_output)
967 return ret;
968
970}
971
973{
975 FrameDecodeData *fdd = frame->private_ref;
976 NVDECFrame *cf = NULL;
977 int ret;
978
979 ctx->bitstream_len = 0;
980 ctx->nb_slices = 0;
981
982 if (fdd->hwaccel_priv)
983 return 0;
984
985 cf = av_mallocz(sizeof(*cf));
986 if (!cf)
987 return AVERROR(ENOMEM);
988
989 cf->decoder = av_refstruct_ref(ctx->decoder);
990
991 cf->idx_ref = av_refstruct_pool_get(ctx->decoder_pool);
992 if (!cf->idx_ref) {
993 av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
994 ret = AVERROR(ENOMEM);
995 goto fail;
996 }
997 cf->ref_idx = cf->idx = *cf->idx_ref;
998
999 fdd->hwaccel_priv = cf;
1002
1003 return 0;
1004fail:
1006 return ret;
1007
1008}
1009
1011{
1013 FrameDecodeData *fdd = frame->private_ref;
1014 NVDECFrame *cf;
1015 int ret;
1016
1017 ret = ff_nvdec_start_frame(avctx, frame);
1018 if (ret < 0)
1019 return ret;
1020
1021 cf = fdd->hwaccel_priv;
1022
1023 if (has_sep_ref) {
1024 if (!cf->ref_idx_ref) {
1025 cf->ref_idx_ref = av_refstruct_pool_get(ctx->decoder_pool);
1026 if (!cf->ref_idx_ref) {
1027 av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
1028 return AVERROR(ENOMEM);
1029 }
1030 }
1031 cf->ref_idx = *cf->ref_idx_ref;
1032 } else {
1034 cf->ref_idx = cf->idx;
1035 }
1036
1037 return 0;
1038}
1039
1041{
1043 NVDECDecoder *decoder = ctx->decoder;
1044 void *logctx = avctx;
1045 CUVIDPICPARAMS *pp = &ctx->pic_params;
1046
1047 CUcontext dummy;
1048
1049 int ret = 0;
1050
1051 pp->nBitstreamDataLen = ctx->bitstream_len;
1052 pp->pBitstreamData = ctx->bitstream;
1053 pp->nNumSlices = ctx->nb_slices;
1054 pp->pSliceDataOffsets = ctx->slice_offsets;
1055
1056 ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
1057 if (ret < 0)
1058 return ret;
1059
1060#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
1061 if (decoder->opaque_output && decoder->cvdl->cuvidDecodePictureAsync)
1062 ret = CHECK_CU(decoder->cvdl->cuvidDecodePictureAsync(decoder->decoder, &ctx->pic_params, decoder->stream));
1063 else
1064 ret = CHECK_CU(decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params));
1065#else
1066 ret = CHECK_CU(decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params));
1067#endif
1068 if (ret < 0)
1069 goto finish;
1070
1071finish:
1072 CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
1073
1074 return ret;
1075}
1076
1078{
1080 int ret = ff_nvdec_end_frame(avctx);
1081 ctx->bitstream = NULL;
1082 ctx->bitstream_len = 0;
1083 ctx->nb_slices = 0;
1084 return ret;
1085}
1086
1088 uint32_t size)
1089{
1091 void *tmp;
1092
1093 tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
1094 (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
1095 if (!tmp)
1096 return AVERROR(ENOMEM);
1097 ctx->slice_offsets = tmp;
1098
1099 if (!ctx->bitstream)
1100 ctx->bitstream = buffer;
1101
1102 ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
1103 ctx->bitstream_len += size;
1104 ctx->nb_slices++;
1105
1106 return 0;
1107}
1108
1110 AVBufferRef *hw_frames_ctx,
1111 enum AVPixelFormat hw_format,
1112 int dpb_size,
1113 int supports_444)
1114{
1115 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
1116 const AVPixFmtDescriptor *sw_desc;
1117 int cuvid_codec_type, cuvid_chroma_format, chroma_444;
1118
1119 /*
1120 * This may be called from get_format() before avctx->hwaccel is set,
1121 * so the selected hardware format is passed explicitly by the wrapper.
1122 */
1123 if (hw_format != AV_PIX_FMT_CUDA && hw_format != AV_PIX_FMT_CUARRAY)
1124 return AVERROR_BUG;
1125
1126 sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
1127 if (!sw_desc)
1128 return AVERROR_BUG;
1129
1130 cuvid_codec_type = map_avcodec_id(avctx->codec_id);
1131 if (cuvid_codec_type < 0) {
1132 av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
1133 return AVERROR_BUG;
1134 }
1135
1136 cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
1137 if (cuvid_chroma_format < 0) {
1138 av_log(avctx, AV_LOG_VERBOSE, "Unsupported chroma format\n");
1139 return AVERROR(EINVAL);
1140 }
1141 chroma_444 = supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
1142
1143 frames_ctx->format = hw_format;
1144 // NVDEC target dimensions must be even-aligned for internal surface allocation.
1145 // For chroma-subsampled formats (420/422), the output dimensions must also be
1146 // even. For monochrome/444, keep the original output dimensions and only
1147 // even-align the NVDEC target — the frame copy will crop to avctx dimensions.
1148 if (cuvid_chroma_format == cudaVideoChromaFormat_420 ||
1149 cuvid_chroma_format == cudaVideoChromaFormat_422) {
1150 frames_ctx->width = (avctx->coded_width + 1) & ~1;
1151 frames_ctx->height = (avctx->coded_height + 1) & ~1;
1152 } else {
1153 frames_ctx->width = avctx->coded_width;
1154 frames_ctx->height = avctx->coded_height;
1155 }
1156 /*
1157 * We add two extra frames to the pool to account for deinterlacing filters
1158 * holding onto their frames.
1159 */
1160 frames_ctx->initial_pool_size = dpb_size + 2;
1161
1162 switch (sw_desc->comp[0].depth) {
1163 case 8:
1164 if (chroma_444) {
1165 frames_ctx->sw_format = (frames_ctx->format == AV_PIX_FMT_CUARRAY)
1167#ifdef NVDEC_HAVE_422_SUPPORT
1168 } else if (cuvid_chroma_format == cudaVideoChromaFormat_422) {
1169 frames_ctx->sw_format = AV_PIX_FMT_NV16;
1170#endif
1171 } else {
1172 frames_ctx->sw_format = AV_PIX_FMT_NV12;
1173 }
1174 break;
1175 case 10:
1176 if (chroma_444) {
1177 frames_ctx->sw_format = (frames_ctx->format == AV_PIX_FMT_CUARRAY)
1179#ifdef NVDEC_HAVE_422_SUPPORT
1180 } else if (cuvid_chroma_format == cudaVideoChromaFormat_422) {
1181 frames_ctx->sw_format = AV_PIX_FMT_P210;
1182#endif
1183 } else {
1184 frames_ctx->sw_format = AV_PIX_FMT_P010;
1185 }
1186 break;
1187 case 12:
1188 if (chroma_444) {
1189 frames_ctx->sw_format = (frames_ctx->format == AV_PIX_FMT_CUARRAY)
1191#ifdef NVDEC_HAVE_422_SUPPORT
1192 } else if (cuvid_chroma_format == cudaVideoChromaFormat_422) {
1193 frames_ctx->sw_format = AV_PIX_FMT_P212;
1194#endif
1195 } else {
1196 frames_ctx->sw_format = AV_PIX_FMT_P012;
1197 }
1198 break;
1199 default:
1200 return AVERROR(EINVAL);
1201 }
1202
1203 return 0;
1204}
1205
1207{
1208 FrameDecodeData *fdd;
1209 NVDECFrame *cf;
1210
1211 if (!frame || !frame->private_ref)
1212 return -1;
1213
1214 fdd = frame->private_ref;
1215 cf = (NVDECFrame*)fdd->hwaccel_priv;
1216 if (!cf)
1217 return -1;
1218
1219 return cf->ref_idx;
1220}
SwsAArch64OpImplParams params
Definition ops.c:51
static FILE * out
static AVFormatContext * ctx
static void finish(void)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition avcodec.h:1595
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
common internal and external API header
#define NULL
Definition coverity.c:32
#define cudaVideoSurfaceFormat_YUV444
Definition cuviddec.c:48
#define cudaVideoSurfaceFormat_YUV444_16Bit
Definition cuviddec.c:49
static DecodeContext * decode_ctx(AVCodecInternal *avci)
Definition decode.c:112
static enum AVPixelFormat pix_fmt
static AVFrame * frame
error code definitions
static int dummy
Definition ffplay.c:3764
static char * output_format
Definition ffprobe.c:145
#define fail
Definition test.h:479
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
@ AV_CODEC_ID_VC1
Definition codec_id.h:120
@ AV_CODEC_ID_VP8
Definition codec_id.h:190
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_MJPEG
Definition codec_id.h:57
@ AV_CODEC_ID_WMV3
Definition codec_id.h:121
@ AV_CODEC_ID_VP9
Definition codec_id.h:217
@ AV_CODEC_ID_MPEG1VIDEO
Definition codec_id.h:51
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
int avcodec_get_hw_frames_parameters(AVCodecContext *avctx, AVBufferRef *device_ref, enum AVPixelFormat hw_pix_fmt, AVBufferRef **out_frames_ref)
Create and return a AVHWFramesContext with values adequate for hardware decoding.
Definition decode.c:1123
#define AV_HWACCEL_FLAG_UNSAFE_OUTPUT
Some hardware decoders (namely nvdec) can either output direct decoder surfaces, or make an on-device...
Definition avcodec.h:2049
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition buffer.c:233
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:103
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition buffer.h:114
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition buffer.c:55
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition buffer.c:328
AVBufferPool * av_buffer_pool_init(size_t size, AVBufferRef *(*alloc)(size_t size))
Allocate and initialize a buffer pool.
Definition buffer.c:283
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR(e)
Definition error.h:45
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition frame.c:552
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:495
void * av_calloc(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition mem.c:264
static AVBufferRef * hw_device_ctx
Definition hw_decode.c:45
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition hwcontext.c:337
FFmpeg internal API for CUDA.
static int ff_cuda_cuarray_elem_size(CUarray_format fmt)
Return the element size in bytes for a CUarray_format, or 0 for unknown.
#define r
Definition input.c:42
unsigned offset
Definition libaomenc.c:763
static const chunk_decoder decoder[8]
Definition dfa.c:331
common internal api header.
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
static const char * obj
Definition mscl.c:57
const char data[16]
Definition mxf.c:149
int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_sep_ref)
Definition nvdec.c:1010
static int nvdec_decoder_create(NVDECDecoder **out, AVBufferRef *hw_device_ref, CUVIDDECODECREATEINFO *params, void *logctx)
Definition nvdec.c:222
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
Definition nvdec.c:1077
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition nvdec.c:1087
static int map_chroma_format(enum AVPixelFormat pix_fmt)
Definition nvdec.c:96
static void nvdec_decoder_frame_pool_free(AVRefStructOpaque opaque)
Definition nvdec.c:292
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition nvdec.c:404
static void nvdec_free_dummy(struct AVHWFramesContext *ctx)
Definition nvdec.c:316
int ff_nvdec_get_ref_idx(AVFrame *frame)
Definition nvdec.c:1206
static void nvdec_decoder_free(AVRefStructOpaque unused, void *obj)
Definition nvdec.c:175
#define CHECK_CU(x)
Definition nvdec.c:74
int ff_nvdec_end_frame(AVCodecContext *avctx)
Definition nvdec.c:1040
static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
Definition nvdec.c:754
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition nvdec.c:972
static int nvdec_init_hwframes(AVCodecContext *avctx, AVBufferRef **out_frames_ref, int dummy)
Definition nvdec.c:326
static int nvdec_test_capabilities(NVDECDecoder *decoder, CUVIDDECODECREATEINFO *params, void *logctx)
Definition nvdec.c:115
static int nvdec_decoder_frame_init(AVRefStructOpaque opaque, void *obj)
Definition nvdec.c:279
static int map_avcodec_id(enum AVCodecID id)
Definition nvdec.c:76
static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
Definition nvdec.c:797
static void nvdec_fdd_priv_free(void *priv)
Definition nvdec.c:740
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition nvdec.c:297
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, enum AVPixelFormat hw_format, int dpb_size, int supports_444)
Definition nvdec.c:1109
static AVBufferRef * nvdec_alloc_dummy(size_t size)
Definition nvdec.c:321
#define NVDEC_MAX_DECODE_SURFACES
Definition nvdec.h:57
#define av_malloc(s)
Definition ops_static.c:52
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition pixdesc.c:3488
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
pixel format definitions
#define AV_PIX_FMT_P212
Definition pixfmt.h:624
#define AV_PIX_FMT_P412
Definition pixfmt.h:625
#define AV_PIX_FMT_P210
Definition pixfmt.h:622
#define AV_PIX_FMT_P012
Definition pixfmt.h:609
#define AV_PIX_FMT_P010
Definition pixfmt.h:608
#define AV_PIX_FMT_YUV444P12MSB
Definition pixfmt.h:561
#define AV_PIX_FMT_P410
Definition pixfmt.h:623
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:96
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition pixfmt.h:260
@ AV_PIX_FMT_NV24
planar YUV 4:4:4, 24bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:371
@ AV_PIX_FMT_NV16
interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:198
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_CUARRAY
hardware decoding through openharmony
Definition pixfmt.h:506
#define AV_PIX_FMT_YUV444P10MSB
Definition pixfmt.h:560
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:120
void * av_refstruct_pool_get(AVRefStructPool *pool)
Get an object from the pool, reusing an old one from the pool when available.
Definition refstruct.c:297
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition refstruct.c:140
static void av_refstruct_pool_uninit(AVRefStructPool **poolp)
Mark the pool as being available for freeing.
Definition refstruct.h:292
static void * av_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
A wrapper around av_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition refstruct.h:94
static AVRefStructPool * av_refstruct_pool_alloc_ext(size_t size, unsigned flags, void *opaque, int(*init_cb)(AVRefStructOpaque opaque, void *obj), void(*reset_cb)(AVRefStructOpaque opaque, void *obj), void(*free_entry_cb)(AVRefStructOpaque opaque, void *obj), void(*free_cb)(AVRefStructOpaque opaque))
A wrapper around av_refstruct_pool_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition refstruct.h:258
#define FF_ARRAY_ELEMS(a)
@ memory_order_release
Definition stdatomic.h:32
@ memory_order_acquire
Definition stdatomic.h:31
int atomic_int
Definition stdatomic.h:63
#define atomic_load_explicit(object, order)
Definition stdatomic.h:247
#define atomic_store_explicit(object, desired, order)
Definition stdatomic.h:253
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
This struct is allocated as AVHWDeviceContext.hwctx.
AVCUDADeviceContextInternal * internal
This struct is allocated as AVHWFramesContext.hwctx.
CUarray * cuarray_surfaces
If cuarray_num_surfaces is >0, this contains the array of pre-allocated surfaces.
CUDA_ARRAY3D_DESCRIPTOR cuarray_desc
CUDA_ARRAY3D_DESCRIPTOR CUarrays will be initialized with.
int cuarray_num_surfaces
If >0, pre-allocate a fixed pool of surfaces.
main external API structure.
Definition avcodec.h:443
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active).
Definition avcodec.h:1503
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:650
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition avcodec.h:1472
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition avcodec.h:1424
int active_thread_type
Which multithreading methods are in use by the codec.
Definition avcodec.h:1603
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1584
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition avcodec.h:1494
int extra_hw_frames
Video decoding only.
Definition avcodec.h:1517
enum AVCodecID codec_id
Definition avcodec.h:453
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:619
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * hwaccel_priv_data
hwaccel-specific private data
Definition internal.h:130
int depth
Number of bits in the component.
Definition pixdesc.h:57
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
enum AVPixelFormat pix_fmt
Supported pixel format.
Definition avcodec.h:1990
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition hwcontext.h:200
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition hwcontext.h:129
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition hwcontext.h:153
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
int initial_pool_size
Initial size of the frame pool.
Definition hwcontext.h:190
int width
The allocated dimensions of the frames in this pool.
Definition hwcontext.h:220
void(* free)(struct AVHWFramesContext *ctx)
This field may be set by the caller before calling av_hwframe_ctx_init().
Definition hwcontext.h:161
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Definition hwcontext.h:181
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition decode.h:33
void(* hwaccel_priv_free)(void *priv)
Definition decode.h:55
int(* hwaccel_priv_post_process)(void *logctx, AVFrame *frame)
Per-frame private data for hwaccels.
Definition decode.h:53
void * hwaccel_priv
Definition decode.h:54
int unsafe_output
Definition nvdec.c:61
CudaFunctions * cudl
Definition nvdec.c:58
AVBufferRef * decode_hw_frames_ref
Definition nvdec.c:53
CUcontext cuda_ctx
Definition nvdec.c:55
AVBufferRef * real_hw_frames_ref
Definition nvdec.c:54
int opaque_output
Definition nvdec.c:62
AVBufferRef * hw_device_ref
Definition nvdec.c:52
CUstream stream
Definition nvdec.c:56
CuvidFunctions * cvdl
Definition nvdec.c:59
CUvideodecoder decoder
Definition nvdec.c:50
unsigned int nb_allocated
Definition nvdec.c:71
unsigned int dpb_size
Definition nvdec.c:70
struct NVDECDecoder * decoder
RefStruct reference.
Definition nvdec.h:65
unsigned int ref_idx
Definition nvdec.h:62
unsigned int idx
Definition nvdec.h:61
unsigned int * ref_idx_ref
RefStruct reference.
Definition nvdec.h:64
unsigned int * idx_ref
RefStruct reference.
Definition nvdec.h:63
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int dpb_size
static char buffer[20]
Definition seek.c:32
int size
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition refstruct.h:58