38 #if !NVDECAPI_CHECK_VERSION(9, 0)
39 #define cudaVideoSurfaceFormat_YUV444 2
40 #define cudaVideoSurfaceFormat_YUV444_16Bit 3
60 #define CHECK_CU(x) FF_CUDA_CHECK_DL(logctx, decoder->cudl, x)
65 #if CONFIG_AV1_NVDEC_HWACCEL
84 int shift_h = 0, shift_v = 0;
87 return cudaVideoChromaFormat_Monochrome;
91 if (shift_h == 1 && shift_v == 1)
92 return cudaVideoChromaFormat_420;
93 else if (shift_h == 1 && shift_v == 0)
94 return cudaVideoChromaFormat_422;
95 else if (shift_h == 0 && shift_v == 0)
96 return cudaVideoChromaFormat_444;
102 CUVIDDECODECREATEINFO *params,
void *logctx)
105 CUVIDDECODECAPS caps = { 0 };
107 caps.eCodecType = params->CodecType;
108 caps.eChromaFormat = params->ChromaFormat;
109 caps.nBitDepthMinus8 = params->bitDepthMinus8;
111 if (!
decoder->cvdl->cuvidGetDecoderCaps) {
112 av_log(logctx,
AV_LOG_WARNING,
"Used Nvidia driver is too old to perform a capability check.\n");
114 #
if defined(_WIN32) || defined(__CYGWIN__)
119 ". Continuing blind.\n");
129 caps.bIsSupported ?
"yes" :
"no", caps.nMaxMBCount);
131 caps.nMinWidth, caps.nMaxWidth);
133 caps.nMinHeight, caps.nMaxHeight);
135 if (!caps.bIsSupported) {
140 if (params->ulWidth > caps.nMaxWidth || params->ulWidth < caps.nMinWidth) {
142 (
int)params->ulWidth, caps.nMinWidth, caps.nMaxWidth);
146 if (params->ulHeight > caps.nMaxHeight || params->ulHeight < caps.nMinHeight) {
148 (
int)params->ulHeight, caps.nMinHeight, caps.nMaxHeight);
152 if ((params->ulWidth * params->ulHeight) / 256 > caps.nMaxMBCount) {
154 (
int)(params->ulWidth * params->ulHeight) / 256, caps.nMaxMBCount);
166 void *logctx =
decoder->hw_device_ref->data;
176 cuvid_free_functions(&
decoder->cvdl);
182 CUVIDDECODECREATEINFO *params,
void *logctx)
209 decoder->cuda_ctx = device_hwctx->cuda_ctx;
210 decoder->cudl = device_hwctx->internal->cuda_dl;
211 decoder->stream = device_hwctx->stream;
213 ret = cuvid_load_functions(&
decoder->cvdl, logctx);
268 ctx->bitstream_len = 0;
269 ctx->bitstream_allocated = 0;
273 ctx->slice_offsets_allocated = 0;
312 if (!frames_ctx->
pool) {
340 CUVIDDECODECREATEINFO params = { 0 };
342 cudaVideoSurfaceFormat output_format;
343 int cuvid_codec_type, cuvid_chroma_format, chroma_444;
351 if (cuvid_codec_type < 0) {
357 if (cuvid_chroma_format < 0) {
361 chroma_444 =
ctx->supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
373 if (!real_hw_frames_ref)
380 cudaVideoSurfaceFormat_NV12;
385 cudaVideoSurfaceFormat_P016;
399 params.bitDepthMinus8 = sw_desc->
comp[0].
depth - 8;
400 params.OutputFormat = output_format;
401 params.CodecType = cuvid_codec_type;
402 params.ChromaFormat = cuvid_chroma_format;
408 if (params.ulNumDecodeSurfaces > 32) {
409 av_log(avctx,
AV_LOG_WARNING,
"Using more than 32 (%d) decode surfaces might cause nvdec to fail.\n",
410 (
int)params.ulNumDecodeSurfaces);
419 decoder->real_hw_frames_ref = real_hw_frames_ref;
420 real_hw_frames_ref =
NULL;
431 if (!
ctx->decoder_pool) {
460 void *logctx =
decoder->hw_device_ref->data;
461 CUdeviceptr devptr = (CUdeviceptr)opaque;
488 CUVIDPROCPARAMS vpp = { 0 };
494 unsigned int pitch,
i;
496 int shift_h = 0, shift_v = 0;
499 vpp.progressive_frame = 1;
500 vpp.output_stream =
decoder->stream;
533 unmap_data->
idx = cf->idx;
566 ctx->bitstream_len = 0;
577 if (!cf->decoder_ref) {
588 cf->ref_idx = cf->idx = *(
unsigned int*)cf->idx_ref->data;
615 if (!cf->ref_idx_ref) {
617 if (!cf->ref_idx_ref) {
623 cf->ref_idx = *(
unsigned int*)cf->ref_idx_ref->data;
626 cf->ref_idx = cf->idx;
639 void *logctx = avctx;
640 CUVIDPICPARAMS *pp = &
ctx->pic_params;
646 pp->nBitstreamDataLen =
ctx->bitstream_len;
647 pp->pBitstreamData =
ctx->bitstream;
648 pp->nNumSlices =
ctx->nb_slices;
649 pp->pSliceDataOffsets =
ctx->slice_offsets;
680 (
ctx->nb_slices + 1) *
sizeof(*
ctx->slice_offsets));
702 int cuvid_codec_type, cuvid_chroma_format, chroma_444;
709 if (cuvid_codec_type < 0) {
715 if (cuvid_chroma_format < 0) {
719 chroma_444 = supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
730 switch (sw_desc->comp[0].depth) {
Libavcodec external API header.
common internal and external API header
static enum AVPixelFormat pix_fmt
AVCodecID
Identify the syntax and semantics of the bitstream.
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
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.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * av_buffer_create(uint8_t *data, buffer_size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
AVBufferRef * av_buffer_alloc(buffer_size_t size)
Allocate an AVBuffer of the given size using av_malloc().
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
AVBufferPool * av_buffer_pool_init(buffer_size_t size, AVBufferRef *(*alloc)(buffer_size_t size))
Allocate and initialize a buffer pool.
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
AVBufferPool * av_buffer_pool_init2(buffer_size_t size, void *opaque, AVBufferRef *(*alloc)(void *opaque, buffer_size_t size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
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.
static AVBufferRef * hw_device_ctx
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
FFmpeg internal API for CUDA.
static const chunk_decoder decoder[8]
common internal API header
int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_sep_ref)
static void nvdec_decoder_free(void *opaque, uint8_t *data)
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size, int supports_444)
static AVBufferRef * nvdec_decoder_frame_alloc(void *opaque, buffer_size_t size)
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
static int map_chroma_format(enum AVPixelFormat pix_fmt)
#define cudaVideoSurfaceFormat_YUV444
int ff_nvdec_decode_init(AVCodecContext *avctx)
static void nvdec_free_dummy(struct AVHWFramesContext *ctx)
int ff_nvdec_get_ref_idx(AVFrame *frame)
int ff_nvdec_end_frame(AVCodecContext *avctx)
static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
static int nvdec_init_hwframes(AVCodecContext *avctx, AVBufferRef **out_frames_ref, int dummy)
static int nvdec_test_capabilities(NVDECDecoder *decoder, CUVIDDECODECREATEINFO *params, void *logctx)
static int map_avcodec_id(enum AVCodecID id)
static AVBufferRef * nvdec_alloc_dummy(buffer_size_t size)
#define cudaVideoSurfaceFormat_YUV444_16Bit
static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref, CUVIDDECODECREATEINFO *params, void *logctx)
static void nvdec_fdd_priv_free(void *priv)
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
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.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
AVPixelFormat
Pixel format.
@ 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...
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
#define AV_PIX_FMT_YUV444P16
A reference to a data buffer.
uint8_t * data
The data buffer.
This struct is allocated as AVHWDeviceContext.hwctx.
main external API structure.
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
int coded_width
Bitstream width / height, may be different from width/height e.g.
struct AVCodecInternal * internal
Private context used for internal data.
void * hwaccel_priv_data
hwaccel-specific private data
int depth
Number of bits in the component.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
AVBufferRef * private_ref
AVBufferRef for internal use by a single libav* library.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
enum AVPixelFormat pix_fmt
Supported pixel format.
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
This struct describes a set or pool of "hardware" frames (i.e.
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
int initial_pool_size
Initial size of the frame pool.
int width
The allocated dimensions of the frames in this pool.
void(* free)(struct AVHWFramesContext *ctx)
This field may be set by the caller before calling av_hwframe_ctx_init().
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
void(* hwaccel_priv_free)(void *priv)
void * hwaccel_priv
Per-frame private data for hwaccels.
int(* post_process)(void *logctx, AVFrame *frame)
The callback to perform some delayed processing on the frame right before it is returned to the calle...
AVBufferRef * real_hw_frames_ref
AVBufferRef * hw_device_ref
unsigned int nb_allocated
AVBufferRef * ref_idx_ref
AVBufferRef * decoder_ref
static const uint8_t offset[127][2]