22#include "config_components.h"
47#if !NVDECAPI_CHECK_VERSION(9, 0)
48#define cudaVideoSurfaceFormat_YUV444 2
49#define cudaVideoSurfaceFormat_YUV444_16Bit 3
52#if NVDECAPI_CHECK_VERSION(11, 0)
53#define CUVID_HAS_AV1_SUPPORT
56#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
57typedef struct CuvidDecoderCleanup {
58 CUvideodecoder cudecoder;
59 CUarray *cuarray_surfaces;
60 int cuarray_num_surfaces;
126#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
127 CUarray *cuarray_surfaces;
128 int cuarray_num_surfaces;
131 CuvidDecoderCleanup *decoder_cleanup;
142#define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x)
145#define CUVID_MAX_DISPLAY_DELAY (4)
148#define CUVID_DEFAULT_NUM_SURFACES (CUVID_MAX_DISPLAY_DELAY + 1)
155 if (
ctx->zero_copy) {
158 "zero_copy requires cuarray output format; "
159 "overriding -output_format %s -> cuarray\n",
171#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
175 "CUARRAY output requires Video Codec SDK 13.1 or later\n");
180 "Unsupported cuvid output format: %s\n",
201 "ff_get_format returned %s, overriding to explicitly requested %s\n",
203 selected = requested;
219#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
220typedef struct CuvidSurfaceRelease {
221 AVBufferRef *in_use_ref;
223} CuvidSurfaceRelease;
225static void cuvid_cuarray_buf_free(
void *opaque, uint8_t *
data)
227 CuvidSurfaceRelease *rel = opaque;
234static void cuvid_decoder_cleanup_free(
void *opaque, uint8_t *
data)
236 CuvidDecoderCleanup *
cleanup = opaque;
244 cudl->cuCtxPushCurrent(device_hwctx->
cuda_ctx);
249 if (
cleanup->cuarray_surfaces) {
250 for (
int i = 0;
i <
cleanup->cuarray_num_surfaces;
i++)
251 cudl->cuArrayDestroy(
cleanup->cuarray_surfaces[
i]);
254 cudl->cuCtxPopCurrent(&
dummy);
260 cuvid_free_functions(&
cleanup->cvdl);
274#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
275 if (
ctx->decoder_cleanup) {
276 ctx->decoder_cleanup->cudecoder =
ctx->cudecoder;
277 ctx->decoder_cleanup->cuarray_surfaces =
ctx->cuarray_surfaces;
278 ctx->decoder_cleanup->cuarray_num_surfaces =
ctx->cuarray_num_surfaces;
283 for (
int i = 0;
i <
ctx->cuarray_num_surfaces;
i++)
288 ctx->cuarray_num_surfaces = 0;
305 CUVIDDECODECAPS *caps =
NULL;
306 CUVIDDECODECREATEINFO cuinfo;
309 int old_nb_surfaces, old_opaque_output =
ctx->opaque_output;
310 int fifo_size_inc, fifo_size_mul = 1;
313 int old_width = avctx->
width;
314 int old_height = avctx->
height;
320 memset(&cuinfo, 0,
sizeof(cuinfo));
322 ctx->internal_error = 0;
325 if (surface_fmt < 0) {
326 ctx->internal_error = surface_fmt;
334 cuinfo.display_area.left =
format->display_area.left +
ctx->crop.left;
335 cuinfo.display_area.top =
format->display_area.top +
ctx->crop.top;
336 cuinfo.display_area.right =
format->display_area.right -
ctx->crop.right;
337 cuinfo.display_area.bottom =
format->display_area.bottom -
ctx->crop.bottom;
340 if (
ctx->resize_expr) {
344 avctx->
width = cuinfo.display_area.right - cuinfo.display_area.left;
345 avctx->
height = cuinfo.display_area.bottom - cuinfo.display_area.top;
352 cuinfo.ulTargetWidth = (avctx->
width + 1) & ~1;
353 cuinfo.ulTargetHeight = (avctx->
height + 1) & ~1;
354 if (
format->chroma_format == cudaVideoChromaFormat_420 ||
355 format->chroma_format == cudaVideoChromaFormat_422) {
356 avctx->
width = cuinfo.ulTargetWidth;
357 avctx->
height = cuinfo.ulTargetHeight;
361 cuinfo.target_rect.left = 0;
362 cuinfo.target_rect.top = 0;
363 cuinfo.target_rect.right = cuinfo.ulTargetWidth;
364 cuinfo.target_rect.bottom = cuinfo.ulTargetHeight;
366 chroma_444 =
format->chroma_format == cudaVideoChromaFormat_444;
368 switch (
format->bit_depth_luma_minus8) {
372#ifdef NVDEC_HAVE_422_SUPPORT
373 }
else if (
format->chroma_format == cudaVideoChromaFormat_422) {
384#ifdef NVDEC_HAVE_422_SUPPORT
385 }
else if (
format->chroma_format == cudaVideoChromaFormat_422) {
396#ifdef NVDEC_HAVE_422_SUPPORT
397 }
else if (
format->chroma_format == cudaVideoChromaFormat_422) {
409 if (!caps || !caps->bIsSupported) {
411 format->bit_depth_luma_minus8 + 8);
419 if (surface_fmt < 0) {
426 requested_hw_format);
438 if (
ctx->opaque_output) {
464 ctx->deint_mode_current =
format->progressive_sequence
465 ? cudaVideoDeinterlaceMode_Weave
468 ctx->progressive_sequence =
format->progressive_sequence;
470 if (!
format->progressive_sequence &&
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
475 if (
format->video_signal_description.video_full_range_flag)
480 if (
format->video_signal_description.color_primaries)
482 if (
format->video_signal_description.transfer_characteristics)
483 avctx->
color_trc =
format->video_signal_description.transfer_characteristics;
484 if (
format->video_signal_description.matrix_coefficients)
490 if (
format->frame_rate.numerator &&
format->frame_rate.denominator) {
495 if (
ctx->cudecoder &&
ctx->opaque_output != old_opaque_output) {
497 "Changing between opaque and non-opaque output is not supported\n");
505 && avctx->
width == old_width
506 && avctx->
height == old_height
507 &&
ctx->chroma_format ==
format->chroma_format
511 if (
ctx->cudecoder) {
514 if (
ctx->internal_error < 0)
518 if (hwframe_ctx->
pool && (
521 hwframe_ctx->
format != internal_hw_format ||
523 av_log(avctx,
AV_LOG_ERROR,
"AVHWFramesContext is already initialized with incompatible parameters\n");
534 ctx->chroma_format =
format->chroma_format;
536 cuinfo.CodecType =
ctx->codec_type =
format->codec;
537 cuinfo.ChromaFormat =
format->chroma_format;
541 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
546 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
548#ifdef NVDEC_HAVE_422_SUPPORT
550 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV16;
555 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P216;
577#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
578 if (
ctx->opaque_output) {
579 switch (cuinfo.OutputFormat) {
580 case cudaVideoSurfaceFormat_NV12: cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12_Opaque;
break;
581 case cudaVideoSurfaceFormat_P016: cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016_Opaque;
break;
582#ifdef NVDEC_HAVE_422_SUPPORT
583 case cudaVideoSurfaceFormat_NV16: cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV16_Opaque;
break;
584 case cudaVideoSurfaceFormat_P216: cuinfo.OutputFormat = cudaVideoSurfaceFormat_P216_Opaque;
break;
593 if (
ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field) {
598 old_nb_surfaces =
ctx->nb_surfaces;
602#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
603 if (
ctx->opaque_output &&
ctx->zero_copy)
605 MAX_NUM_REGISTERED_DECODE_SURFACES);
609 if (fifo_size_inc > 0 &&
av_fifo_grow2(
ctx->frame_queue, fifo_size_inc) < 0) {
610 av_log(avctx,
AV_LOG_ERROR,
"Failed to grow frame queue on video sequence callback\n");
616 av_log(avctx,
AV_LOG_ERROR,
"Failed to grow key frame array on video sequence callback\n");
621 cuinfo.ulNumDecodeSurfaces =
ctx->nb_surfaces;
622 cuinfo.ulNumOutputSurfaces =
ctx->opaque_output ? 0 : 1;
623 cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
624 cuinfo.bitDepthMinus8 =
format->bit_depth_luma_minus8;
625 cuinfo.DeinterlaceMode =
ctx->deint_mode_current;
627 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidCreateDecoder(&
ctx->cudecoder, &cuinfo));
628 if (
ctx->internal_error < 0)
631#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
632 if (
ctx->opaque_output) {
633 CUDA_ARRAY3D_DESCRIPTOR arr_desc;
634 CUVIDREGISTERDECODESURFACESINFO reg_info;
638 ff_nvdec_fill_cuarray_desc(&arr_desc, avctx, cuinfo.OutputFormat);
641 if (!
ctx->cuarray_surfaces) {
652 if (
ctx->zero_copy) {
658 err = cuvid_load_functions(&
cleanup->cvdl, avctx);
662 ctx->internal_error = err;
670 if (!
ctx->surface_in_use_ref) {
679 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
681 &
ctx->cuarray_surfaces[
i], &arr_desc));
682 if (
ctx->internal_error < 0) {
683 ctx->cuarray_num_surfaces =
i;
688 ctx->cuarray_num_surfaces =
ctx->nb_surfaces;
697 if (
ctx->internal_error < 0)
700 memset(®_info, 0,
sizeof(reg_info));
701 reg_info.ulNumDecodeSurfaces =
ctx->cuarray_num_surfaces;
702 reg_info.pDecodeSurfaces =
ctx->cuarray_surfaces;
704 if (
ctx->cvdl->cuvidRegisterDecodeSurfaces) {
706 ctx->cvdl->cuvidRegisterDecodeSurfaces(
707 ctx->cudecoder, ®_info));
709 av_log(avctx,
AV_LOG_ERROR,
"cuvidRegisterDecodeSurfaces not available in loaded driver\n");
713 if (
ctx->internal_error < 0)
718 if (!hwframe_ctx->
pool) {
719 hwframe_ctx->
format = internal_hw_format;
724#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
727 ff_nvdec_fill_cuarray_desc(&cuda_hwctx->
cuarray_desc, avctx, cuinfo.OutputFormat);
737 if(
ctx->cuparseinfo.ulMaxNumDecodeSurfaces != cuinfo.ulNumDecodeSurfaces) {
738 ctx->cuparseinfo.ulMaxNumDecodeSurfaces = cuinfo.ulNumDecodeSurfaces;
739 return cuinfo.ulNumDecodeSurfaces;
755 if(picparams->intra_pic_flag)
756 ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
758#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
759 if (
ctx->opaque_output) {
760 if (
ctx->surface_in_use &&
764 "CUARRAY surface %d still in use by the encoder. "
765 "Increase surfaces with -extra_hw_frames or reduce "
766 "encoder buffering (disable lookahead, reduce B-frames).\n",
767 picparams->CurrPicIdx);
772 if (
ctx->cvdl->cuvidDecodePictureAsync)
773 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidDecodePictureAsync(
774 ctx->cudecoder, picparams,
ctx->cuda_stream));
776 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidDecodePicture(
ctx->cudecoder, picparams));
779 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidDecodePicture(
ctx->cudecoder, picparams));
780 if (
ctx->internal_error < 0)
797 ctx->internal_error = 0;
800 parsed_frame.
dispinfo.progressive_frame =
ctx->progressive_sequence;
802 if (
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
812 if (!
ctx->drop_second_field) {
828 if (
ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
841 CUVIDSOURCEDATAPACKET cupkt;
842 int ret = 0, eret = 0, is_flush =
ctx->decoder_flushing;
849 if (is_flush && avpkt && avpkt->
size)
860 memset(&cupkt, 0,
sizeof(cupkt));
862 if (avpkt && avpkt->
size) {
863 cupkt.payload_size = avpkt->
size;
864 cupkt.payload = avpkt->
data;
867 cupkt.flags = CUVID_PKT_TIMESTAMP;
871 cupkt.timestamp = avpkt->
pts;
874 cupkt.flags = CUVID_PKT_ENDOFSTREAM;
875 ctx->decoder_flushing = 1;
889 if (
ctx->internal_error) {
891 ret =
ctx->internal_error;
892 ctx->internal_error = 0;
916 CUdeviceptr mapped_frame = 0;
917 int ret = 0, eret = 0;
922 if (
ctx->decoder_flushing) {
951 unsigned int pitch = 0;
956 params.progressive_frame = parsed_frame.
dispinfo.progressive_frame;
960#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
962 if (
ctx->zero_copy) {
963 int idx = parsed_frame.
dispinfo.picture_index;
965 if (idx < 0 || idx >=
ctx->cuarray_num_surfaces) {
967 idx,
ctx->cuarray_num_surfaces);
979 if (!
frame->hw_frames_ctx) {
985 CuvidSurfaceRelease *rel =
av_malloc(
sizeof(*rel));
991 if (!rel->in_use_ref) {
1000 (uint8_t *)
ctx->cuarray_surfaces[idx], 0,
1001 cuvid_cuarray_buf_free, rel,
1003 if (!
frame->buf[0]) {
1013 frame->data[0] = (uint8_t *)
ctx->cuarray_surfaces[idx];
1017 CUDA_ARRAY3D_DESCRIPTOR plane_desc = { 0 };
1018 CUarray plane_array;
1020 CUresult cures =
ctx->cudl->cuArrayGetPlane(
1021 &plane_array,
ctx->cuarray_surfaces[idx], p);
1022 if (cures == CUDA_ERROR_INVALID_VALUE)
1024 if (cures != CUDA_SUCCESS) {
1030 &plane_desc, plane_array));
1035 if (elem_size <= 0) {
1037 "Unknown CUarray element format %d on plane %d\n",
1038 plane_desc.Format, p);
1043 frame->linesize[p] = plane_desc.Width *
1044 plane_desc.NumChannels * elem_size;
1047 int src_idx = parsed_frame.
dispinfo.picture_index;
1050 if (src_idx < 0 || src_idx >=
ctx->cuarray_num_surfaces) {
1051 av_log(avctx,
AV_LOG_ERROR,
"CUARRAY source surface index %d out of range [0, %d)\n",
1052 src_idx,
ctx->cuarray_num_surfaces);
1069 memset(&tmp_frame, 0,
sizeof(tmp_frame));
1071 tmp_frame.
data[0] = (uint8_t *)
ctx->cuarray_surfaces[src_idx];
1107 CUDA_MEMCPY2D cpy = {
1108 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
1109 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
1110 .srcDevice = mapped_frame,
1111 .dstDevice = (CUdeviceptr)
frame->data[
i],
1113 .dstPitch =
frame->linesize[
i],
1129#ifdef NVDEC_HAVE_422_SUPPORT
1166 tmp_frame->
data[
i] = (uint8_t*)mapped_frame +
offset;
1191 if (
ctx->key_frame[parsed_frame.
dispinfo.picture_index])
1195 ctx->key_frame[parsed_frame.
dispinfo.picture_index] = 0;
1205 if (
ctx->prev_pts == INT64_MIN) {
1209 int pts_diff = (
frame->pts -
ctx->prev_pts) / 2;
1211 frame->pts += pts_diff;
1218 frame->duration = 0;
1225 }
else if (
ctx->decoder_flushing ||
1237 eret =
CHECK_CU(
ctx->cvdl->cuvidUnmapVideoFrame(
ctx->cudecoder, mapped_frame));
1259 ctx->cudl->cuCtxPushCurrent(cuda_ctx);
1261#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
1262 if (
ctx->opaque_output)
1263 ctx->cudl->cuCtxSynchronize();
1267 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
1270 ctx->cudl->cuCtxPopCurrent(&
dummy);
1281 cuvid_free_functions(&
ctx->cvdl);
1287 const CUVIDPARSERPARAMS *cuparseinfo,
1290 int bit_depth,
int is_yuv422,
int is_yuv444)
1293 CUVIDDECODECAPS *caps;
1294 int res8 = 0, res10 = 0, res12 = 0;
1296 if (!
ctx->cvdl->cuvidGetDecoderCaps) {
1297 av_log(avctx,
AV_LOG_WARNING,
"Used Nvidia driver is too old to perform a capability check.\n");
1299#
if defined(_WIN32) || defined(__CYGWIN__)
1304 ". Continuing blind.\n");
1305 ctx->caps8.bIsSupported =
ctx->caps10.bIsSupported = 1;
1307 ctx->caps12.bIsSupported = 0;
1311 ctx->caps8.eCodecType =
ctx->caps10.eCodecType =
ctx->caps12.eCodecType
1312 = cuparseinfo->CodecType;
1314 ctx->caps8.eChromaFormat =
ctx->caps10.eChromaFormat =
ctx->caps12.eChromaFormat
1315 = is_yuv444 ? cudaVideoChromaFormat_444 :
1316#ifdef NVDEC_HAVE_422_SUPPORT
1317 (is_yuv422 ? cudaVideoChromaFormat_422 : cudaVideoChromaFormat_420);
1319 cudaVideoChromaFormat_420;
1322 ctx->caps8.nBitDepthMinus8 = 0;
1323 ctx->caps10.nBitDepthMinus8 = 2;
1324 ctx->caps12.nBitDepthMinus8 = 4;
1331 av_log(avctx,
AV_LOG_VERBOSE,
"8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
1332 ctx->caps8.bIsSupported,
ctx->caps8.nMinWidth,
ctx->caps8.nMaxWidth,
ctx->caps8.nMinHeight,
ctx->caps8.nMaxHeight);
1333 av_log(avctx,
AV_LOG_VERBOSE,
"10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
1334 ctx->caps10.bIsSupported,
ctx->caps10.nMinWidth,
ctx->caps10.nMaxWidth,
ctx->caps10.nMinHeight,
ctx->caps10.nMaxHeight);
1335 av_log(avctx,
AV_LOG_VERBOSE,
"12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
1336 ctx->caps12.bIsSupported,
ctx->caps12.nMinWidth,
ctx->caps12.nMaxWidth,
ctx->caps12.nMinHeight,
ctx->caps12.nMaxHeight);
1340 caps = &
ctx->caps10;
1345 caps = &
ctx->caps12;
1355 if (!
ctx->caps8.bIsSupported) {
1360 if (!caps->bIsSupported) {
1365 if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
1367 probed_width, caps->nMinWidth, caps->nMaxWidth);
1371 if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
1373 probed_height, caps->nMinHeight, caps->nMaxHeight);
1377 if ((probed_width * probed_height) / 256 > caps->nMaxMBCount) {
1379 (
int)(probed_width * probed_height) / 256, caps->nMaxMBCount);
1392 CUVIDSOURCEDATAPACKET seq_pkt;
1393 CUcontext cuda_ctx =
NULL;
1404 int probed_bit_depth = 8, is_yuv444 = 0, is_yuv422 = 0;
1408 probed_bit_depth = probe_desc->
comp[0].
depth;
1413#ifdef NVDEC_HAVE_422_SUPPORT
1423 switch (probed_bit_depth) {
1449 requested_hw_format);
1454 if (
ctx->opaque_output) {
1463 if (
ctx->resize_expr && sscanf(
ctx->resize_expr,
"%dx%d",
1464 &
ctx->resize.width, &
ctx->resize.height) != 2) {
1470 if (
ctx->crop_expr && sscanf(
ctx->crop_expr,
"%dx%dx%dx%d",
1471 &
ctx->crop.top, &
ctx->crop.bottom,
1472 &
ctx->crop.left, &
ctx->crop.right) != 4) {
1478 if (
ctx->opaque_output) {
1479 if (
ctx->crop_expr) {
1481 "Cropping is not supported with cuarray output; "
1482 "crop option will be ignored\n");
1483 memset(&
ctx->crop, 0,
sizeof(
ctx->crop));
1485 if (
ctx->resize_expr) {
1487 "Resizing is not supported with cuarray output; "
1488 "resize option will be ignored\n");
1491 if (
ctx->deint_mode != cudaVideoDeinterlaceMode_Weave) {
1493 "Deinterlacing is not supported with cuarray output; "
1494 "deint mode will be forced to weave\n");
1495 ctx->deint_mode = cudaVideoDeinterlaceMode_Weave;
1499 ret = cuvid_load_functions(&
ctx->cvdl, avctx);
1506 if(
ctx->nb_surfaces < 0)
1510 if (!
ctx->frame_queue) {
1517 if (!
ctx->hwframe) {
1525 if (!
ctx->hwdevice) {
1532 if (!
ctx->hwdevice) {
1543 if (!
ctx->hwframe) {
1553 device_hwctx = device_ctx->
hwctx;
1558 ctx->cuda_stream = device_hwctx->
stream;
1560 memset(&
ctx->cuparseinfo, 0,
sizeof(
ctx->cuparseinfo));
1561 memset(&seq_pkt, 0,
sizeof(seq_pkt));
1564#if CONFIG_H264_CUVID_DECODER
1566 ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
1569#if CONFIG_HEVC_CUVID_DECODER
1571 ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
1574#if CONFIG_MJPEG_CUVID_DECODER
1576 ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
1579#if CONFIG_MPEG1_CUVID_DECODER
1581 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
1584#if CONFIG_MPEG2_CUVID_DECODER
1586 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
1589#if CONFIG_MPEG4_CUVID_DECODER
1591 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
1594#if CONFIG_VP8_CUVID_DECODER
1596 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
1599#if CONFIG_VP9_CUVID_DECODER
1601 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
1604#if CONFIG_VC1_CUVID_DECODER
1606 ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
1609#if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1611 ctx->cuparseinfo.CodecType = cudaVideoCodec_AV1;
1632 extradata_size >= 4 &&
1633 extradata[0] & 0x80) {
1635 extradata_size -= 4;
1639 +
FFMAX(extradata_size - (
int)
sizeof(
ctx->cuparse_ext->raw_seqhdr_data), 0));
1640 if (!
ctx->cuparse_ext) {
1645 if (extradata_size > 0)
1646 memcpy(
ctx->cuparse_ext->raw_seqhdr_data, extradata, extradata_size);
1647 ctx->cuparse_ext->format.seqhdr_data_length = extradata_size;
1649 ctx->cuparseinfo.pExtVideoInfo =
ctx->cuparse_ext;
1652 if (!
ctx->key_frame) {
1657 ctx->cuparseinfo.ulMaxNumDecodeSurfaces = 1;
1659 ctx->cuparseinfo.pUserData = avctx;
1671 probed_bit_depth, is_yuv422, is_yuv444);
1679 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1680 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1682 if (seq_pkt.payload && seq_pkt.payload_size) {
1692 ctx->prev_pts = INT64_MIN;
1710 CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
1721 if (
ctx->cuparser) {
1722 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
1730 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1731 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1733 if (seq_pkt.payload && seq_pkt.payload_size) {
1743 ctx->prev_pts = INT64_MIN;
1744 ctx->decoder_flushing = 0;
1751#define OFFSET(x) offsetof(CuvidContext, x)
1752#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1754 {
"deint",
"Set deinterlacing mode",
OFFSET(deint_mode),
AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive,
VD, .unit =
"deint" },
1755 {
"weave",
"Weave deinterlacing (do nothing)", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0,
VD, .unit =
"deint" },
1756 {
"bob",
"Bob deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0,
VD, .unit =
"deint" },
1757 {
"adaptive",
"Adaptive deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0,
VD, .unit =
"deint" },
1760 {
"drop_second_field",
"Drop second field when deinterlacing",
OFFSET(drop_second_field),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VD },
1766#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
1768 {
"zero_copy",
"Enable zero-copy opaque decode output (forces output_format=cuarray)",
OFFSET(zero_copy),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VD },
1784#ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT
1799#define DEFINE_CUVID_CODEC(x, X, bsf_name) \
1800 static const AVClass x##_cuvid_class = { \
1801 .class_name = #x "_cuvid", \
1802 .item_name = av_default_item_name, \
1803 .option = options, \
1804 .version = LIBAVUTIL_VERSION_INT, \
1806 const FFCodec ff_##x##_cuvid_decoder = { \
1807 .p.name = #x "_cuvid", \
1808 CODEC_LONG_NAME("Nvidia CUVID " #X " decoder"), \
1809 .p.type = AVMEDIA_TYPE_VIDEO, \
1810 .p.id = AV_CODEC_ID_##X, \
1811 .priv_data_size = sizeof(CuvidContext), \
1812 .p.priv_class = &x##_cuvid_class, \
1813 .init = cuvid_decode_init, \
1814 .close = cuvid_decode_end, \
1815 FF_CODEC_RECEIVE_FRAME_CB(cuvid_output_frame), \
1816 .flush = cuvid_flush, \
1818 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
1819 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | \
1820 FF_CODEC_CAP_SETS_FRAME_PROPS, \
1821 .hw_configs = cuvid_hw_configs, \
1822 .p.wrapper_name = "cuvid", \
1825#if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1829#if CONFIG_HEVC_CUVID_DECODER
1833#if CONFIG_H264_CUVID_DECODER
1837#if CONFIG_MJPEG_CUVID_DECODER
1841#if CONFIG_MPEG1_CUVID_DECODER
1845#if CONFIG_MPEG2_CUVID_DECODER
1849#if CONFIG_MPEG4_CUVID_DECODER
1853#if CONFIG_VP8_CUVID_DECODER
1857#if CONFIG_VP9_CUVID_DECODER
1861#if CONFIG_VC1_CUVID_DECODER
SwsAArch64OpImplParams params
static const char *const format[]
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
static AVFormatContext * ctx
Libavcodec external API header.
refcounted data buffer API
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
static av_always_inline const FFCodec * ffcodec(const AVCodec *codec)
static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS *picparams)
static int cuvid_destroy_decoder(AVCodecContext *avctx)
static const AVCodecHWConfigInternal *const cuvid_hw_configs[]
static int cuvid_output_frame(AVCodecContext *avctx, AVFrame *frame)
static av_cold int cuvid_decode_end(AVCodecContext *avctx)
static int cuvid_output_format_is_forced(const CuvidContext *ctx)
static enum AVPixelFormat cuvid_apply_forced_output_format(AVCodecContext *avctx, const CuvidContext *ctx, enum AVPixelFormat selected, enum AVPixelFormat requested)
#define CUVID_MAX_DISPLAY_DELAY
#define cudaVideoSurfaceFormat_YUV444
static void cuvid_prepare_format_list(enum AVPixelFormat *pix_fmts, enum AVPixelFormat hw_format, enum AVPixelFormat sw_format)
static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO *dispinfo)
static int cuvid_get_requested_hw_format(AVCodecContext *avctx, CuvidContext *ctx, enum AVPixelFormat *fmt)
static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT *format)
static av_cold int cuvid_decode_init(AVCodecContext *avctx)
static int cuvid_test_capabilities(AVCodecContext *avctx, const CUVIDPARSERPARAMS *cuparseinfo, int probed_width, int probed_height, int bit_depth, int is_yuv422, int is_yuv444)
#define DEFINE_CUVID_CODEC(x, X, bsf_name)
#define CUVID_DEFAULT_NUM_SURFACES
#define cudaVideoSurfaceFormat_YUV444_16Bit
static int cuvid_is_buffer_full(AVCodecContext *avctx)
static void cuvid_flush(AVCodecContext *avctx)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data.
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
static char * output_format
static av_cold void cleanup(FlashSV2Context *s)
#define AV_OPT_FLAG_DEPRECATED
Set if option is deprecated, users should refer to AVOption.help text for more information.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
@ AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
The codec supports this format via the hw_frames_ctx interface.
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
@ AV_CODEC_HW_CONFIG_METHOD_INTERNAL
The codec supports this format by some internal method.
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
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_ref(const 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.
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.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AVERROR_EOF
End of file.
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
void av_fifo_reset2(AVFifo *f)
size_t av_fifo_can_write(const AVFifo *f)
size_t av_fifo_can_read(const AVFifo *f)
int av_fifo_grow2(AVFifo *f, size_t inc)
Enlarge an AVFifo.
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#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.
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
#define AV_NOPTS_VALUE
Undefined timestamp value.
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
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.
static int shift(int a, int b)
common internal api header.
static enum AVPixelFormat pix_fmts[]
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AVCOL_RANGE_JPEG
Full range content.
#define AV_PIX_FMT_YUV444P12MSB
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_NV24
planar YUV 4:4:4, 24bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
@ AV_PIX_FMT_NV16
interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_CUARRAY
hardware decoding through openharmony
#define AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P10MSB
#define FF_ARRAY_ELEMS(a)
#define atomic_load_explicit(object, order)
#define atomic_store_explicit(object, desired, order)
AVCodecParameters * par_out
Parameters of the output stream.
A reference to a data buffer.
This struct is allocated as AVHWDeviceContext.hwctx.
AVCUDADeviceContextInternal * internal
This struct is allocated as AVHWFramesContext.hwctx.
CUDA_ARRAY3D_DESCRIPTOR cuarray_desc
CUDA_ARRAY3D_DESCRIPTOR CUarrays will be initialized with.
Describe the class of an AVClass context structure.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
int64_t bit_rate
the average bitrate
const struct AVCodec * codec
enum AVColorSpace colorspace
YUV colorspace type.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
int extra_hw_frames
Video decoding only.
int coded_width
Bitstream width / height, may be different from width/height e.g.
struct AVCodecInternal * internal
Private context used for internal data.
AVPacket * in_pkt
This packet is used to hold the packet given to decoders implementing the .decode API; it is unused b...
struct AVBSFContext * bsf
This struct describes the properties of an encoded stream.
int extradata_size
Size of the extradata content in bytes.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
const char * name
Name of the codec implementation.
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.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
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.
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
int width
The allocated dimensions of the frames in this pool.
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
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.
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
uint8_t nb_components
The number of components each pixel has, (1-4)
Rational number (pair of numerator and denominator).
struct CuvidContext::@006255247064232322304150030351010216270027246216 resize
enum AVPixelFormat output_format
cudaVideoCodec codec_type
cudaVideoChromaFormat chroma_format
struct CuvidContext::@133300314247370073362260244350264314175314272354 crop
CUVIDPARSERPARAMS cuparseinfo
CUVIDEOFORMATEX * cuparse_ext
CUVIDPARSERDISPINFO dispinfo
const char * bsfs
Decoding only, a comma-separated list of bitstream filters to apply to packets before decoding.
static void error(const char *err)