40 #if !NVDECAPI_CHECK_VERSION(9, 0)
41 #define cudaVideoSurfaceFormat_YUV444 2
42 #define cudaVideoSurfaceFormat_YUV444_16Bit 3
45 #if NVDECAPI_CHECK_VERSION(11, 0)
46 #define CUVID_HAS_AV1_SUPPORT
112 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x)
119 CUVIDDECODECAPS *caps =
NULL;
120 CUVIDDECODECREATEINFO cuinfo;
124 int old_width = avctx->
width;
125 int old_height = avctx->
height;
133 memset(&cuinfo, 0,
sizeof(cuinfo));
135 ctx->internal_error = 0;
137 avctx->coded_width = cuinfo.ulWidth =
format->coded_width;
138 avctx->coded_height = cuinfo.ulHeight =
format->coded_height;
141 cuinfo.display_area.left =
format->display_area.left +
ctx->crop.left;
142 cuinfo.display_area.top =
format->display_area.top +
ctx->crop.top;
143 cuinfo.display_area.right =
format->display_area.right -
ctx->crop.right;
144 cuinfo.display_area.bottom =
format->display_area.bottom -
ctx->crop.bottom;
147 if (
ctx->resize_expr) {
148 avctx->width =
ctx->resize.width;
149 avctx->height =
ctx->resize.height;
151 avctx->width = cuinfo.display_area.right - cuinfo.display_area.left;
152 avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top;
156 cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1;
157 cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1;
160 cuinfo.target_rect.left = 0;
161 cuinfo.target_rect.top = 0;
162 cuinfo.target_rect.right = cuinfo.ulTargetWidth;
163 cuinfo.target_rect.bottom = cuinfo.ulTargetHeight;
165 chroma_444 =
format->chroma_format == cudaVideoChromaFormat_444;
167 switch (
format->bit_depth_luma_minus8) {
184 if (!caps || !caps->bIsSupported) {
186 format->bit_depth_luma_minus8 + 8);
192 if (surface_fmt < 0) {
203 avctx->pix_fmt = surface_fmt;
206 if (avctx->hw_frames_ctx) {
220 (
AVRational){ avctx->width, avctx->height }));
222 ctx->deint_mode_current =
format->progressive_sequence
223 ? cudaVideoDeinterlaceMode_Weave
226 ctx->progressive_sequence =
format->progressive_sequence;
228 if (!
format->progressive_sequence &&
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
233 if (
format->video_signal_description.video_full_range_flag)
238 avctx->color_primaries =
format->video_signal_description.color_primaries;
239 avctx->color_trc =
format->video_signal_description.transfer_characteristics;
240 avctx->colorspace =
format->video_signal_description.matrix_coefficients;
243 avctx->bit_rate =
format->bitrate;
245 if (
format->frame_rate.numerator &&
format->frame_rate.denominator) {
246 avctx->framerate.num =
format->frame_rate.numerator;
247 avctx->framerate.den =
format->frame_rate.denominator;
251 && avctx->coded_width ==
format->coded_width
252 && avctx->coded_height ==
format->coded_height
253 && avctx->width == old_width
254 && avctx->height == old_height
255 &&
ctx->chroma_format ==
format->chroma_format
259 if (
ctx->cudecoder) {
262 if (
ctx->internal_error < 0)
267 if (hwframe_ctx->pool && (
268 hwframe_ctx->width < avctx->width ||
269 hwframe_ctx->height < avctx->height ||
271 hwframe_ctx->sw_format != avctx->sw_pix_fmt)) {
272 av_log(avctx,
AV_LOG_ERROR,
"AVHWFramesContext is already initialized with incompatible parameters\n");
274 av_log(avctx,
AV_LOG_DEBUG,
"height: %d <-> %d\n", hwframe_ctx->height, avctx->height);
282 ctx->chroma_format =
format->chroma_format;
284 cuinfo.CodecType =
ctx->codec_type =
format->codec;
285 cuinfo.ChromaFormat =
format->chroma_format;
287 switch (avctx->sw_pix_fmt) {
289 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
293 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
308 cuinfo.ulNumDecodeSurfaces =
ctx->nb_surfaces;
309 cuinfo.ulNumOutputSurfaces = 1;
310 cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
311 cuinfo.bitDepthMinus8 =
format->bit_depth_luma_minus8;
312 cuinfo.DeinterlaceMode =
ctx->deint_mode_current;
314 if (
ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
317 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidCreateDecoder(&
ctx->cudecoder, &cuinfo));
318 if (
ctx->internal_error < 0)
321 if (!hwframe_ctx->pool) {
323 hwframe_ctx->sw_format = avctx->sw_pix_fmt;
324 hwframe_ctx->width = avctx->width;
325 hwframe_ctx->height = avctx->height;
343 if(picparams->intra_pic_flag)
344 ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
346 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidDecodePicture(
ctx->cudecoder, picparams));
347 if (
ctx->internal_error < 0)
360 ctx->internal_error = 0;
363 parsed_frame.
dispinfo.progressive_frame =
ctx->progressive_sequence;
365 if (
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
370 if (!
ctx->drop_second_field) {
383 int delay =
ctx->cuparseinfo.ulMaxDisplayDelay;
384 if (
ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
395 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
396 CUVIDSOURCEDATAPACKET cupkt;
397 int ret = 0, eret = 0, is_flush =
ctx->decoder_flushing;
401 if (is_flush && avpkt && avpkt->
size)
412 memset(&cupkt, 0,
sizeof(cupkt));
414 if (avpkt && avpkt->
size) {
415 cupkt.payload_size = avpkt->
size;
416 cupkt.payload = avpkt->
data;
419 cupkt.flags = CUVID_PKT_TIMESTAMP;
423 cupkt.timestamp = avpkt->
pts;
426 cupkt.flags = CUVID_PKT_ENDOFSTREAM;
427 ctx->decoder_flushing = 1;
436 if (
ctx->internal_error) {
438 ret =
ctx->internal_error;
460 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
461 CUdeviceptr mapped_frame = 0;
462 int ret = 0, eret = 0;
466 if (
ctx->decoder_flushing) {
493 CUVIDPROCPARAMS params;
494 unsigned int pitch = 0;
500 memset(¶ms, 0,
sizeof(params));
501 params.progressive_frame = parsed_frame.
dispinfo.progressive_frame;
503 params.top_field_first = parsed_frame.
dispinfo.top_field_first;
505 ret =
CHECK_CU(
ctx->cvdl->cuvidMapVideoFrame(
ctx->cudecoder, parsed_frame.
dispinfo.picture_index, &mapped_frame, &pitch, ¶ms));
526 CUDA_MEMCPY2D cpy = {
527 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
528 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
529 .srcDevice = mapped_frame,
530 .dstDevice = (CUdeviceptr)
frame->data[
i],
532 .dstPitch =
frame->linesize[
i],
538 ret =
CHECK_CU(
ctx->cudl->cuMemcpy2DAsync(&cpy, device_hwctx->stream));
576 tmp_frame->
data[
i] = (uint8_t*)mapped_frame +
offset;
601 ctx->key_frame[parsed_frame.
dispinfo.picture_index] = 0;
611 if (
ctx->prev_pts == INT64_MIN) {
615 int pts_diff = (
frame->pts -
ctx->prev_pts) / 2;
617 frame->pts += pts_diff;
625 frame->pkt_duration = 0;
626 frame->pkt_size = -1;
630 if (
frame->interlaced_frame)
631 frame->top_field_first = parsed_frame.
dispinfo.top_field_first;
632 }
else if (
ctx->decoder_flushing) {
643 eret =
CHECK_CU(
ctx->cvdl->cuvidUnmapVideoFrame(
ctx->cudecoder, mapped_frame));
658 CUcontext
dummy, cuda_ctx = device_hwctx ? device_hwctx->cuda_ctx :
NULL;
663 ctx->cudl->cuCtxPushCurrent(cuda_ctx);
666 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
669 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
682 cuvid_free_functions(&
ctx->cvdl);
688 const CUVIDPARSERPARAMS *cuparseinfo,
694 CUVIDDECODECAPS *caps;
695 int res8 = 0, res10 = 0, res12 = 0;
697 if (!
ctx->cvdl->cuvidGetDecoderCaps) {
698 av_log(avctx,
AV_LOG_WARNING,
"Used Nvidia driver is too old to perform a capability check.\n");
700 #
if defined(_WIN32) || defined(__CYGWIN__)
705 ". Continuing blind.\n");
706 ctx->caps8.bIsSupported =
ctx->caps10.bIsSupported = 1;
708 ctx->caps12.bIsSupported = 0;
712 ctx->caps8.eCodecType =
ctx->caps10.eCodecType =
ctx->caps12.eCodecType
713 = cuparseinfo->CodecType;
714 ctx->caps8.eChromaFormat =
ctx->caps10.eChromaFormat =
ctx->caps12.eChromaFormat
715 = cudaVideoChromaFormat_420;
717 ctx->caps8.nBitDepthMinus8 = 0;
718 ctx->caps10.nBitDepthMinus8 = 2;
719 ctx->caps12.nBitDepthMinus8 = 4;
726 av_log(avctx,
AV_LOG_VERBOSE,
"8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
727 ctx->caps8.bIsSupported,
ctx->caps8.nMinWidth,
ctx->caps8.nMaxWidth,
ctx->caps8.nMinHeight,
ctx->caps8.nMaxHeight);
728 av_log(avctx,
AV_LOG_VERBOSE,
"10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
729 ctx->caps10.bIsSupported,
ctx->caps10.nMinWidth,
ctx->caps10.nMaxWidth,
ctx->caps10.nMinHeight,
ctx->caps10.nMaxHeight);
730 av_log(avctx,
AV_LOG_VERBOSE,
"12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
731 ctx->caps12.bIsSupported,
ctx->caps12.nMinWidth,
ctx->caps12.nMaxWidth,
ctx->caps12.nMinHeight,
ctx->caps12.nMaxHeight);
750 if (!
ctx->caps8.bIsSupported) {
755 if (!caps->bIsSupported) {
760 if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
762 probed_width, caps->nMinWidth, caps->nMaxWidth);
766 if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
768 probed_height, caps->nMinHeight, caps->nMaxHeight);
772 if ((probed_width * probed_height) / 256 > caps->nMaxMBCount) {
774 (
int)(probed_width * probed_height) / 256, caps->nMaxMBCount);
787 CUVIDSOURCEDATAPACKET seq_pkt;
788 CUcontext cuda_ctx =
NULL;
800 int probed_bit_depth = 8;
804 probed_bit_depth = probe_desc->
comp[0].
depth;
818 if (
ctx->resize_expr && sscanf(
ctx->resize_expr,
"%dx%d",
819 &
ctx->resize.width, &
ctx->resize.height) != 2) {
825 if (
ctx->crop_expr && sscanf(
ctx->crop_expr,
"%dx%dx%dx%d",
826 &
ctx->crop.top, &
ctx->crop.bottom,
827 &
ctx->crop.left, &
ctx->crop.right) != 4) {
833 ret = cuvid_load_functions(&
ctx->cvdl, avctx);
840 if (!
ctx->frame_queue) {
855 if (!
ctx->hwdevice) {
862 if (!
ctx->hwdevice) {
883 device_hwctx = device_ctx->
hwctx;
888 memset(&
ctx->cuparseinfo, 0,
sizeof(
ctx->cuparseinfo));
889 memset(&seq_pkt, 0,
sizeof(seq_pkt));
892 #if CONFIG_H264_CUVID_DECODER
894 ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
897 #if CONFIG_HEVC_CUVID_DECODER
899 ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
902 #if CONFIG_MJPEG_CUVID_DECODER
904 ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
907 #if CONFIG_MPEG1_CUVID_DECODER
909 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
912 #if CONFIG_MPEG2_CUVID_DECODER
914 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
917 #if CONFIG_MPEG4_CUVID_DECODER
919 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
922 #if CONFIG_VP8_CUVID_DECODER
924 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
927 #if CONFIG_VP9_CUVID_DECODER
929 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
932 #if CONFIG_VC1_CUVID_DECODER
934 ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
937 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
939 ctx->cuparseinfo.CodecType = cudaVideoCodec_AV1;
960 extradata_size > 4 &&
961 extradata[0] & 0x80) {
967 +
FFMAX(extradata_size - (
int)
sizeof(
ctx->cuparse_ext->raw_seqhdr_data), 0));
968 if (!
ctx->cuparse_ext) {
973 if (extradata_size > 0)
974 memcpy(
ctx->cuparse_ext->raw_seqhdr_data, extradata, extradata_size);
975 ctx->cuparse_ext->format.seqhdr_data_length = extradata_size;
977 ctx->cuparseinfo.pExtVideoInfo =
ctx->cuparse_ext;
980 if (!
ctx->key_frame) {
985 ctx->cuparseinfo.ulMaxNumDecodeSurfaces =
ctx->nb_surfaces;
987 ctx->cuparseinfo.pUserData = avctx;
1007 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1008 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1010 if (seq_pkt.payload && seq_pkt.payload_size) {
1020 ctx->prev_pts = INT64_MIN;
1037 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
1038 CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
1048 if (!
ctx->frame_queue) {
1053 if (
ctx->cudecoder) {
1054 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
1058 if (
ctx->cuparser) {
1059 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
1067 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1068 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1070 if (seq_pkt.payload && seq_pkt.payload_size) {
1080 ctx->prev_pts = INT64_MIN;
1081 ctx->decoder_flushing = 0;
1088 #define OFFSET(x) offsetof(CuvidContext, x)
1089 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1091 {
"deint",
"Set deinterlacing mode",
OFFSET(deint_mode),
AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive,
VD,
"deint" },
1092 {
"weave",
"Weave deinterlacing (do nothing)", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0,
VD,
"deint" },
1093 {
"bob",
"Bob deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0,
VD,
"deint" },
1094 {
"adaptive",
"Adaptive deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0,
VD,
"deint" },
1096 {
"surfaces",
"Maximum surfaces to be used for decoding",
OFFSET(nb_surfaces),
AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX,
VD },
1097 {
"drop_second_field",
"Drop second field when deinterlacing",
OFFSET(drop_second_field),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VD },
1116 #define DEFINE_CUVID_CODEC(x, X, bsf_name) \
1117 static const AVClass x##_cuvid_class = { \
1118 .class_name = #x "_cuvid", \
1119 .item_name = av_default_item_name, \
1120 .option = options, \
1121 .version = LIBAVUTIL_VERSION_INT, \
1123 const AVCodec ff_##x##_cuvid_decoder = { \
1124 .name = #x "_cuvid", \
1125 .long_name = NULL_IF_CONFIG_SMALL("Nvidia CUVID " #X " decoder"), \
1126 .type = AVMEDIA_TYPE_VIDEO, \
1127 .id = AV_CODEC_ID_##X, \
1128 .priv_data_size = sizeof(CuvidContext), \
1129 .priv_class = &x##_cuvid_class, \
1130 .init = cuvid_decode_init, \
1131 .close = cuvid_decode_end, \
1132 .receive_frame = cuvid_output_frame, \
1133 .flush = cuvid_flush, \
1135 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
1136 .caps_internal = FF_CODEC_CAP_SETS_FRAME_PROPS, \
1137 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, \
1141 AV_PIX_FMT_NONE }, \
1142 .hw_configs = cuvid_hw_configs, \
1143 .wrapper_name = "cuvid", \
1146 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1150 #if CONFIG_HEVC_CUVID_DECODER
1154 #if CONFIG_H264_CUVID_DECODER
1158 #if CONFIG_MJPEG_CUVID_DECODER
1162 #if CONFIG_MPEG1_CUVID_DECODER
1166 #if CONFIG_MPEG2_CUVID_DECODER
1170 #if CONFIG_MPEG4_CUVID_DECODER
1174 #if CONFIG_VP8_CUVID_DECODER
1178 #if CONFIG_VP9_CUVID_DECODER
1182 #if CONFIG_VC1_CUVID_DECODER