71 int shift_h = 0, shift_v = 0;
75 if (shift_h == 1 && shift_v == 1)
76 return cudaVideoChromaFormat_420;
77 else if (shift_h == 1 && shift_v == 0)
78 return cudaVideoChromaFormat_422;
79 else if (shift_h == 0 && shift_v == 0)
80 return cudaVideoChromaFormat_444;
86 CUVIDDECODECREATEINFO *
params,
void *logctx)
89 CUVIDDECODECAPS caps = { 0 };
91 caps.eCodecType = params->CodecType;
92 caps.eChromaFormat = params->ChromaFormat;
93 caps.nBitDepthMinus8 = params->bitDepthMinus8;
95 if (!decoder->
cvdl->cuvidGetDecoderCaps) {
96 av_log(logctx,
AV_LOG_WARNING,
"Used Nvidia driver is too old to perform a capability check.\n");
98 #
if defined(_WIN32) || defined(__CYGWIN__)
103 ". Continuing blind.\n");
107 err = decoder->
cvdl->cuvidGetDecoderCaps(&caps);
108 if (err != CUDA_SUCCESS) {
115 caps.bIsSupported ?
"yes" :
"no", caps.nMaxMBCount);
117 caps.nMinWidth, caps.nMaxWidth);
119 caps.nMinHeight, caps.nMaxHeight);
121 if (!caps.bIsSupported) {
126 if (params->ulWidth > caps.nMaxWidth || params->ulWidth < caps.nMinWidth) {
128 (
int)params->ulWidth, caps.nMinWidth, caps.nMaxWidth);
132 if (params->ulHeight > caps.nMaxHeight || params->ulHeight < caps.nMinHeight) {
134 (
int)params->ulHeight, caps.nMinHeight, caps.nMaxHeight);
138 if ((params->ulWidth * params->ulHeight) / 256 > caps.nMaxMBCount) {
140 (
int)(params->ulWidth * params->ulHeight) / 256, caps.nMaxMBCount);
152 decoder->
cvdl->cuvidDestroyDecoder(decoder->
decoder);
156 cuvid_free_functions(&decoder->
cvdl);
162 CUVIDDECODECREATEINFO *
params,
void *logctx)
186 if (!decoder->hw_device_ref) {
190 decoder->cuda_ctx = device_hwctx->cuda_ctx;
191 decoder->cudl = device_hwctx->internal->cuda_dl;
193 ret = cuvid_load_functions(&decoder->cvdl, logctx);
199 err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx);
200 if (err != CUDA_SUCCESS) {
207 decoder->cudl->cuCtxPopCurrent(&
dummy);
211 err = decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params);
213 decoder->cudl->cuCtxPopCurrent(&
dummy);
215 if (err != CUDA_SUCCESS) {
272 CUVIDDECODECREATEINFO
params = { 0 };
274 int cuvid_codec_type, cuvid_chroma_format;
282 if (cuvid_codec_type < 0) {
288 if (cuvid_chroma_format < 0) {
305 params.bitDepthMinus8 = sw_desc->
comp[0].
depth - 8;
306 params.OutputFormat = params.bitDepthMinus8 ?
307 cudaVideoSurfaceFormat_P016 : cudaVideoSurfaceFormat_NV12;
308 params.CodecType = cuvid_codec_type;
309 params.ChromaFormat = cuvid_chroma_format;
311 params.ulNumOutputSurfaces = 1;
315 if (params.ulNumDecodeSurfaces > 32) {
316 av_log(avctx,
AV_LOG_WARNING,
"Using more than 32 (%d) decode surfaces might cause nvdec to fail.\n",
317 (
int)params.ulNumDecodeSurfaces);
363 CUVIDPROCPARAMS vpp = { .progressive_frame = 1 };
369 unsigned int pitch, i;
374 if (err != CUDA_SUCCESS)
377 err =
decoder->cvdl->cuvidMapVideoFrame(
decoder->decoder, cf->idx, &devptr,
379 if (err != CUDA_SUCCESS) {
387 CUDA_MEMCPY2D cpy = {
388 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
389 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
391 .dstDevice = (CUdeviceptr)
frame->
data[i],
399 err =
decoder->cudl->cuMemcpy2D(&cpy);
400 if (err != CUDA_SUCCESS) {
436 if (!cf->decoder_ref) {
447 cf->idx = *(
unsigned int*)cf->idx_ref->data;
476 err = decoder->
cudl->cuCtxPushCurrent(decoder->
cuda_ctx);
477 if (err != CUDA_SUCCESS)
481 if (err != CUDA_SUCCESS) {
530 int cuvid_codec_type, cuvid_chroma_format;
537 if (cuvid_codec_type < 0) {
543 if (cuvid_chroma_format < 0) {
553 switch (sw_desc->comp[0].depth) {
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
static enum AVPixelFormat pix_fmt
AVBufferRef * decoder_ref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
int coded_width
Bitstream width / height, may be different from width/height e.g.
#define AV_LOG_WARNING
Something somehow does not look correct.
int width
The allocated dimensions of the frames in this pool.
void(* hwaccel_priv_free)(void *priv)
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref, CUVIDDECODECREATEINFO *params, void *logctx)
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
CUVIDPICPARAMS pic_params
AVBufferRef * private_ref
AVBufferRef for internal use by a single libav* library.
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
static AVBufferRef * nvdec_decoder_frame_alloc(void *opaque, int size)
#define AV_LOG_VERBOSE
Detailed information.
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
AVBufferRef * decoder_ref
static int nvdec_test_capabilities(NVDECDecoder *decoder, CUVIDDECODECREATEINFO *params, void *logctx)
static int map_chroma_format(enum AVPixelFormat pix_fmt)
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static AVBufferRef * hw_device_ctx
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.
int ff_nvdec_end_frame(AVCodecContext *avctx)
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
static void nvdec_fdd_priv_free(void *priv)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
static const uint8_t offset[127][2]
int ff_nvdec_decode_init(AVCodecContext *avctx)
int initial_pool_size
Initial size of the frame pool.
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size)
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
static void nvdec_decoder_free(void *opaque, uint8_t *data)
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.
FFmpeg internal API for CUDA.
HW acceleration through CUDA.
AVBufferPool * av_buffer_pool_init2(int size, void *opaque, AVBufferRef *(*alloc)(void *opaque, int size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
preferred ID for MPEG-1/2 video decoding
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
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...
Libavcodec external API header.
unsigned int bitstream_allocated
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
main external API structure.
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
uint8_t * data
The data buffer.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
This struct is allocated as AVHWDeviceContext.hwctx.
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
unsigned int slice_offsets_allocated
This struct describes a set or pool of "hardware" frames (i.e.
unsigned int nb_allocated
static int map_avcodec_id(enum AVCodecID id)
int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx, enum AVHWDeviceType dev_type)
Make sure avctx.hw_frames_ctx is set.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
A reference to a data buffer.
const char const char * params
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
common internal api header.
common internal and external API header
int ff_nvdec_get_ref_idx(AVFrame *frame)
void * hwaccel_priv_data
hwaccel-specific private data
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
struct AVCodecInternal * internal
Private context used for internal data.
AVBufferPool * decoder_pool
static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
void * hwaccel_priv
Per-frame private data for hwaccels.
int depth
Number of bits in the component.
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
AVPixelFormat
Pixel format.
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
AVBufferRef * hw_device_ref