22 #include "config_components.h"
44 #if !NVDECAPI_CHECK_VERSION(9, 0)
45 #define cudaVideoSurfaceFormat_YUV444 2
46 #define cudaVideoSurfaceFormat_YUV444_16Bit 3
49 #if NVDECAPI_CHECK_VERSION(11, 0)
50 #define CUVID_HAS_AV1_SUPPORT
116 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x)
123 CUVIDDECODECAPS *caps =
NULL;
124 CUVIDDECODECREATEINFO cuinfo;
128 int old_width = avctx->
width;
129 int old_height = avctx->
height;
137 memset(&cuinfo, 0,
sizeof(cuinfo));
139 ctx->internal_error = 0;
141 avctx->coded_width = cuinfo.ulWidth =
format->coded_width;
142 avctx->coded_height = cuinfo.ulHeight =
format->coded_height;
145 cuinfo.display_area.left =
format->display_area.left +
ctx->crop.left;
146 cuinfo.display_area.top =
format->display_area.top +
ctx->crop.top;
147 cuinfo.display_area.right =
format->display_area.right -
ctx->crop.right;
148 cuinfo.display_area.bottom =
format->display_area.bottom -
ctx->crop.bottom;
151 if (
ctx->resize_expr) {
152 avctx->width =
ctx->resize.width;
153 avctx->height =
ctx->resize.height;
155 avctx->width = cuinfo.display_area.right - cuinfo.display_area.left;
156 avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top;
160 cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1;
161 cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1;
164 cuinfo.target_rect.left = 0;
165 cuinfo.target_rect.top = 0;
166 cuinfo.target_rect.right = cuinfo.ulTargetWidth;
167 cuinfo.target_rect.bottom = cuinfo.ulTargetHeight;
169 chroma_444 =
format->chroma_format == cudaVideoChromaFormat_444;
171 switch (
format->bit_depth_luma_minus8) {
188 if (!caps || !caps->bIsSupported) {
190 format->bit_depth_luma_minus8 + 8);
196 if (surface_fmt < 0) {
207 avctx->pix_fmt = surface_fmt;
210 if (avctx->hw_frames_ctx) {
226 ctx->deint_mode_current =
format->progressive_sequence
227 ? cudaVideoDeinterlaceMode_Weave
230 ctx->progressive_sequence =
format->progressive_sequence;
232 if (!
format->progressive_sequence &&
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
237 if (
format->video_signal_description.video_full_range_flag)
243 avctx->color_trc =
format->video_signal_description.transfer_characteristics;
247 avctx->bit_rate =
format->bitrate;
249 if (
format->frame_rate.numerator &&
format->frame_rate.denominator) {
250 avctx->framerate.num =
format->frame_rate.numerator;
251 avctx->framerate.den =
format->frame_rate.denominator;
255 && avctx->coded_width ==
format->coded_width
256 && avctx->coded_height ==
format->coded_height
257 && avctx->
width == old_width
258 && avctx->height == old_height
259 &&
ctx->chroma_format ==
format->chroma_format
263 if (
ctx->cudecoder) {
266 if (
ctx->internal_error < 0)
271 if (hwframe_ctx->pool && (
272 hwframe_ctx->width < avctx->width ||
273 hwframe_ctx->height < avctx->height ||
275 hwframe_ctx->sw_format != avctx->sw_pix_fmt)) {
276 av_log(avctx,
AV_LOG_ERROR,
"AVHWFramesContext is already initialized with incompatible parameters\n");
278 av_log(avctx,
AV_LOG_DEBUG,
"height: %d <-> %d\n", hwframe_ctx->height, avctx->height);
286 ctx->chroma_format =
format->chroma_format;
288 cuinfo.CodecType =
ctx->codec_type =
format->codec;
289 cuinfo.ChromaFormat =
format->chroma_format;
291 switch (avctx->sw_pix_fmt) {
293 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
297 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
312 cuinfo.ulNumDecodeSurfaces =
ctx->nb_surfaces;
313 cuinfo.ulNumOutputSurfaces = 1;
314 cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
315 cuinfo.bitDepthMinus8 =
format->bit_depth_luma_minus8;
316 cuinfo.DeinterlaceMode =
ctx->deint_mode_current;
318 if (
ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
321 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidCreateDecoder(&
ctx->cudecoder, &cuinfo));
322 if (
ctx->internal_error < 0)
325 if (!hwframe_ctx->pool) {
327 hwframe_ctx->sw_format = avctx->sw_pix_fmt;
328 hwframe_ctx->width = avctx->width;
329 hwframe_ctx->height = avctx->height;
347 if(picparams->intra_pic_flag)
348 ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
350 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidDecodePicture(
ctx->cudecoder, picparams));
351 if (
ctx->internal_error < 0)
364 ctx->internal_error = 0;
367 parsed_frame.
dispinfo.progressive_frame =
ctx->progressive_sequence;
369 if (
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
374 if (!
ctx->drop_second_field) {
387 int delay =
ctx->cuparseinfo.ulMaxDisplayDelay;
388 if (
ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
399 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
400 CUVIDSOURCEDATAPACKET cupkt;
401 int ret = 0, eret = 0, is_flush =
ctx->decoder_flushing;
405 if (is_flush && avpkt && avpkt->
size)
416 memset(&cupkt, 0,
sizeof(cupkt));
418 if (avpkt && avpkt->
size) {
419 cupkt.payload_size = avpkt->
size;
420 cupkt.payload = avpkt->
data;
423 cupkt.flags = CUVID_PKT_TIMESTAMP;
427 cupkt.timestamp = avpkt->
pts;
430 cupkt.flags = CUVID_PKT_ENDOFSTREAM;
431 ctx->decoder_flushing = 1;
440 if (
ctx->internal_error) {
442 ret =
ctx->internal_error;
464 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
466 CUdeviceptr mapped_frame = 0;
467 int ret = 0, eret = 0;
471 if (
ctx->decoder_flushing) {
497 CUVIDPROCPARAMS params;
498 unsigned int pitch = 0;
502 memset(¶ms, 0,
sizeof(params));
503 params.progressive_frame = parsed_frame.dispinfo.progressive_frame;
504 params.second_field = parsed_frame.second_field;
505 params.top_field_first = parsed_frame.dispinfo.top_field_first;
507 ret =
CHECK_CU(
ctx->cvdl->cuvidMapVideoFrame(
ctx->cudecoder, parsed_frame.dispinfo.picture_index, &mapped_frame, &pitch, ¶ms));
528 CUDA_MEMCPY2D cpy = {
529 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
530 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
531 .srcDevice = mapped_frame,
532 .dstDevice = (CUdeviceptr)
frame->data[
i],
534 .dstPitch =
frame->linesize[
i],
540 ret =
CHECK_CU(
ctx->cudl->cuMemcpy2DAsync(&cpy, device_hwctx->stream));
578 tmp_frame->
data[
i] = (uint8_t*)mapped_frame +
offset;
602 frame->key_frame =
ctx->key_frame[parsed_frame.dispinfo.picture_index];
603 ctx->key_frame[parsed_frame.dispinfo.picture_index] = 0;
610 frame->pts = parsed_frame.dispinfo.timestamp;
612 if (parsed_frame.second_field) {
613 if (
ctx->prev_pts == INT64_MIN) {
617 int pts_diff = (
frame->pts -
ctx->prev_pts) / 2;
619 frame->pts += pts_diff;
627 frame->pkt_duration = 0;
628 frame->pkt_size = -1;
630 frame->interlaced_frame = !parsed_frame.is_deinterlacing && !parsed_frame.dispinfo.progressive_frame;
632 if (
frame->interlaced_frame)
633 frame->top_field_first = parsed_frame.dispinfo.top_field_first;
634 }
else if (
ctx->decoder_flushing) {
645 eret =
CHECK_CU(
ctx->cvdl->cuvidUnmapVideoFrame(
ctx->cudecoder, mapped_frame));
660 CUcontext
dummy, cuda_ctx = device_hwctx ? device_hwctx->cuda_ctx :
NULL;
665 ctx->cudl->cuCtxPushCurrent(cuda_ctx);
668 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
671 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
684 cuvid_free_functions(&
ctx->cvdl);
690 const CUVIDPARSERPARAMS *cuparseinfo,
696 CUVIDDECODECAPS *caps;
697 int res8 = 0, res10 = 0, res12 = 0;
699 if (!
ctx->cvdl->cuvidGetDecoderCaps) {
700 av_log(avctx,
AV_LOG_WARNING,
"Used Nvidia driver is too old to perform a capability check.\n");
702 #
if defined(_WIN32) || defined(__CYGWIN__)
707 ". Continuing blind.\n");
708 ctx->caps8.bIsSupported =
ctx->caps10.bIsSupported = 1;
710 ctx->caps12.bIsSupported = 0;
714 ctx->caps8.eCodecType =
ctx->caps10.eCodecType =
ctx->caps12.eCodecType
715 = cuparseinfo->CodecType;
716 ctx->caps8.eChromaFormat =
ctx->caps10.eChromaFormat =
ctx->caps12.eChromaFormat
717 = cudaVideoChromaFormat_420;
719 ctx->caps8.nBitDepthMinus8 = 0;
720 ctx->caps10.nBitDepthMinus8 = 2;
721 ctx->caps12.nBitDepthMinus8 = 4;
728 av_log(avctx,
AV_LOG_VERBOSE,
"8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
729 ctx->caps8.bIsSupported,
ctx->caps8.nMinWidth,
ctx->caps8.nMaxWidth,
ctx->caps8.nMinHeight,
ctx->caps8.nMaxHeight);
730 av_log(avctx,
AV_LOG_VERBOSE,
"10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
731 ctx->caps10.bIsSupported,
ctx->caps10.nMinWidth,
ctx->caps10.nMaxWidth,
ctx->caps10.nMinHeight,
ctx->caps10.nMaxHeight);
732 av_log(avctx,
AV_LOG_VERBOSE,
"12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
733 ctx->caps12.bIsSupported,
ctx->caps12.nMinWidth,
ctx->caps12.nMaxWidth,
ctx->caps12.nMinHeight,
ctx->caps12.nMaxHeight);
752 if (!
ctx->caps8.bIsSupported) {
757 if (!caps->bIsSupported) {
762 if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
764 probed_width, caps->nMinWidth, caps->nMaxWidth);
768 if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
770 probed_height, caps->nMinHeight, caps->nMaxHeight);
774 if ((probed_width * probed_height) / 256 > caps->nMaxMBCount) {
776 (
int)(probed_width * probed_height) / 256, caps->nMaxMBCount);
789 CUVIDSOURCEDATAPACKET seq_pkt;
790 CUcontext cuda_ctx =
NULL;
802 int probed_bit_depth = 8;
806 probed_bit_depth = probe_desc->
comp[0].
depth;
820 if (
ctx->resize_expr && sscanf(
ctx->resize_expr,
"%dx%d",
821 &
ctx->resize.width, &
ctx->resize.height) != 2) {
827 if (
ctx->crop_expr && sscanf(
ctx->crop_expr,
"%dx%dx%dx%d",
828 &
ctx->crop.top, &
ctx->crop.bottom,
829 &
ctx->crop.left, &
ctx->crop.right) != 4) {
835 ret = cuvid_load_functions(&
ctx->cvdl, avctx);
842 if (!
ctx->frame_queue) {
857 if (!
ctx->hwdevice) {
864 if (!
ctx->hwdevice) {
885 device_hwctx = device_ctx->
hwctx;
890 memset(&
ctx->cuparseinfo, 0,
sizeof(
ctx->cuparseinfo));
891 memset(&seq_pkt, 0,
sizeof(seq_pkt));
894 #if CONFIG_H264_CUVID_DECODER
896 ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
899 #if CONFIG_HEVC_CUVID_DECODER
901 ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
904 #if CONFIG_MJPEG_CUVID_DECODER
906 ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
909 #if CONFIG_MPEG1_CUVID_DECODER
911 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
914 #if CONFIG_MPEG2_CUVID_DECODER
916 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
919 #if CONFIG_MPEG4_CUVID_DECODER
921 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
924 #if CONFIG_VP8_CUVID_DECODER
926 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
929 #if CONFIG_VP9_CUVID_DECODER
931 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
934 #if CONFIG_VC1_CUVID_DECODER
936 ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
939 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
941 ctx->cuparseinfo.CodecType = cudaVideoCodec_AV1;
962 extradata_size > 4 &&
963 extradata[0] & 0x80) {
969 +
FFMAX(extradata_size - (
int)
sizeof(
ctx->cuparse_ext->raw_seqhdr_data), 0));
970 if (!
ctx->cuparse_ext) {
975 if (extradata_size > 0)
976 memcpy(
ctx->cuparse_ext->raw_seqhdr_data, extradata, extradata_size);
977 ctx->cuparse_ext->format.seqhdr_data_length = extradata_size;
979 ctx->cuparseinfo.pExtVideoInfo =
ctx->cuparse_ext;
982 if (!
ctx->key_frame) {
987 ctx->cuparseinfo.ulMaxNumDecodeSurfaces =
ctx->nb_surfaces;
989 ctx->cuparseinfo.pUserData = avctx;
1009 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1010 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1012 if (seq_pkt.payload && seq_pkt.payload_size) {
1022 ctx->prev_pts = INT64_MIN;
1039 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
1040 CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
1049 if (
ctx->cudecoder) {
1050 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
1054 if (
ctx->cuparser) {
1055 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
1063 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1064 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1066 if (seq_pkt.payload && seq_pkt.payload_size) {
1076 ctx->prev_pts = INT64_MIN;
1077 ctx->decoder_flushing = 0;
1084 #define OFFSET(x) offsetof(CuvidContext, x)
1085 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1087 {
"deint",
"Set deinterlacing mode",
OFFSET(deint_mode),
AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive,
VD,
"deint" },
1088 {
"weave",
"Weave deinterlacing (do nothing)", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0,
VD,
"deint" },
1089 {
"bob",
"Bob deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0,
VD,
"deint" },
1090 {
"adaptive",
"Adaptive deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0,
VD,
"deint" },
1092 {
"surfaces",
"Maximum surfaces to be used for decoding",
OFFSET(nb_surfaces),
AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX,
VD },
1093 {
"drop_second_field",
"Drop second field when deinterlacing",
OFFSET(drop_second_field),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VD },
1112 #define DEFINE_CUVID_CODEC(x, X, bsf_name) \
1113 static const AVClass x##_cuvid_class = { \
1114 .class_name = #x "_cuvid", \
1115 .item_name = av_default_item_name, \
1116 .option = options, \
1117 .version = LIBAVUTIL_VERSION_INT, \
1119 const FFCodec ff_##x##_cuvid_decoder = { \
1120 .p.name = #x "_cuvid", \
1121 .p.long_name = NULL_IF_CONFIG_SMALL("Nvidia CUVID " #X " decoder"), \
1122 .p.type = AVMEDIA_TYPE_VIDEO, \
1123 .p.id = AV_CODEC_ID_##X, \
1124 .priv_data_size = sizeof(CuvidContext), \
1125 .p.priv_class = &x##_cuvid_class, \
1126 .init = cuvid_decode_init, \
1127 .close = cuvid_decode_end, \
1128 FF_CODEC_RECEIVE_FRAME_CB(cuvid_output_frame), \
1129 .flush = cuvid_flush, \
1131 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
1132 .caps_internal = FF_CODEC_CAP_SETS_FRAME_PROPS, \
1133 .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, \
1137 AV_PIX_FMT_NONE }, \
1138 .hw_configs = cuvid_hw_configs, \
1139 .p.wrapper_name = "cuvid", \
1142 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1146 #if CONFIG_HEVC_CUVID_DECODER
1150 #if CONFIG_H264_CUVID_DECODER
1154 #if CONFIG_MJPEG_CUVID_DECODER
1158 #if CONFIG_MPEG1_CUVID_DECODER
1162 #if CONFIG_MPEG2_CUVID_DECODER
1166 #if CONFIG_MPEG4_CUVID_DECODER
1170 #if CONFIG_VP8_CUVID_DECODER
1174 #if CONFIG_VP9_CUVID_DECODER
1178 #if CONFIG_VC1_CUVID_DECODER