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
108 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x)
115 CUVIDDECODECAPS *caps =
NULL;
116 CUVIDDECODECREATEINFO cuinfo;
120 int old_width = avctx->
width;
121 int old_height = avctx->
height;
129 memset(&cuinfo, 0,
sizeof(cuinfo));
131 ctx->internal_error = 0;
133 avctx->coded_width = cuinfo.ulWidth =
format->coded_width;
134 avctx->coded_height = cuinfo.ulHeight =
format->coded_height;
137 cuinfo.display_area.left =
format->display_area.left +
ctx->crop.left;
138 cuinfo.display_area.top =
format->display_area.top +
ctx->crop.top;
139 cuinfo.display_area.right =
format->display_area.right -
ctx->crop.right;
140 cuinfo.display_area.bottom =
format->display_area.bottom -
ctx->crop.bottom;
143 if (
ctx->resize_expr) {
144 avctx->width =
ctx->resize.width;
145 avctx->height =
ctx->resize.height;
147 avctx->width = cuinfo.display_area.right - cuinfo.display_area.left;
148 avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top;
152 cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1;
153 cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1;
156 cuinfo.target_rect.left = 0;
157 cuinfo.target_rect.top = 0;
158 cuinfo.target_rect.right = cuinfo.ulTargetWidth;
159 cuinfo.target_rect.bottom = cuinfo.ulTargetHeight;
161 chroma_444 =
format->chroma_format == cudaVideoChromaFormat_444;
163 switch (
format->bit_depth_luma_minus8) {
180 if (!caps || !caps->bIsSupported) {
182 format->bit_depth_luma_minus8 + 8);
188 if (surface_fmt < 0) {
199 avctx->pix_fmt = surface_fmt;
202 if (avctx->hw_frames_ctx) {
218 ctx->deint_mode_current =
format->progressive_sequence
219 ? cudaVideoDeinterlaceMode_Weave
222 ctx->progressive_sequence =
format->progressive_sequence;
224 if (!
format->progressive_sequence &&
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
229 if (
format->video_signal_description.video_full_range_flag)
235 avctx->color_trc =
format->video_signal_description.transfer_characteristics;
239 avctx->bit_rate =
format->bitrate;
241 if (
format->frame_rate.numerator &&
format->frame_rate.denominator) {
242 avctx->framerate.num =
format->frame_rate.numerator;
243 avctx->framerate.den =
format->frame_rate.denominator;
247 && avctx->coded_width ==
format->coded_width
248 && avctx->coded_height ==
format->coded_height
249 && avctx->
width == old_width
250 && avctx->height == old_height
251 &&
ctx->chroma_format ==
format->chroma_format
255 if (
ctx->cudecoder) {
258 if (
ctx->internal_error < 0)
263 if (hwframe_ctx->pool && (
264 hwframe_ctx->width < avctx->width ||
265 hwframe_ctx->height < avctx->height ||
267 hwframe_ctx->sw_format != avctx->sw_pix_fmt)) {
268 av_log(avctx,
AV_LOG_ERROR,
"AVHWFramesContext is already initialized with incompatible parameters\n");
270 av_log(avctx,
AV_LOG_DEBUG,
"height: %d <-> %d\n", hwframe_ctx->height, avctx->height);
278 ctx->chroma_format =
format->chroma_format;
280 cuinfo.CodecType =
ctx->codec_type =
format->codec;
281 cuinfo.ChromaFormat =
format->chroma_format;
283 switch (avctx->sw_pix_fmt) {
285 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
289 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
304 cuinfo.ulNumDecodeSurfaces =
ctx->nb_surfaces;
305 cuinfo.ulNumOutputSurfaces = 1;
306 cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
307 cuinfo.bitDepthMinus8 =
format->bit_depth_luma_minus8;
308 cuinfo.DeinterlaceMode =
ctx->deint_mode_current;
310 if (
ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
313 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidCreateDecoder(&
ctx->cudecoder, &cuinfo));
314 if (
ctx->internal_error < 0)
317 if (!hwframe_ctx->pool) {
319 hwframe_ctx->sw_format = avctx->sw_pix_fmt;
320 hwframe_ctx->width = avctx->width;
321 hwframe_ctx->height = avctx->height;
339 ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
341 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidDecodePicture(
ctx->cudecoder, picparams));
342 if (
ctx->internal_error < 0)
355 ctx->internal_error = 0;
358 parsed_frame.
dispinfo.progressive_frame =
ctx->progressive_sequence;
360 if (
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
365 if (!
ctx->drop_second_field) {
378 int delay =
ctx->cuparseinfo.ulMaxDisplayDelay;
379 if (
ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
390 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
391 CUVIDSOURCEDATAPACKET cupkt;
392 int ret = 0, eret = 0, is_flush =
ctx->decoder_flushing;
396 if (is_flush && avpkt && avpkt->
size)
407 memset(&cupkt, 0,
sizeof(cupkt));
409 if (avpkt && avpkt->
size) {
410 cupkt.payload_size = avpkt->
size;
411 cupkt.payload = avpkt->
data;
414 cupkt.flags = CUVID_PKT_TIMESTAMP;
418 cupkt.timestamp = avpkt->
pts;
421 cupkt.flags = CUVID_PKT_ENDOFSTREAM;
422 ctx->decoder_flushing = 1;
431 if (
ctx->internal_error) {
433 ret =
ctx->internal_error;
455 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
456 CUdeviceptr mapped_frame = 0;
457 int ret = 0, eret = 0;
461 if (
ctx->decoder_flushing) {
488 CUVIDPROCPARAMS params;
489 unsigned int pitch = 0;
495 memset(¶ms, 0,
sizeof(params));
496 params.progressive_frame = parsed_frame.
dispinfo.progressive_frame;
498 params.top_field_first = parsed_frame.
dispinfo.top_field_first;
500 ret =
CHECK_CU(
ctx->cvdl->cuvidMapVideoFrame(
ctx->cudecoder, parsed_frame.
dispinfo.picture_index, &mapped_frame, &pitch, ¶ms));
521 CUDA_MEMCPY2D cpy = {
522 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
523 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
524 .srcDevice = mapped_frame,
525 .dstDevice = (CUdeviceptr)
frame->data[
i],
527 .dstPitch =
frame->linesize[
i],
533 ret =
CHECK_CU(
ctx->cudl->cuMemcpy2DAsync(&cpy, device_hwctx->stream));
604 if (
ctx->prev_pts == INT64_MIN) {
608 int pts_diff = (
frame->pts -
ctx->prev_pts) / 2;
610 frame->pts += pts_diff;
623 frame->pkt_duration = 0;
624 frame->pkt_size = -1;
628 if (
frame->interlaced_frame)
629 frame->top_field_first = parsed_frame.
dispinfo.top_field_first;
630 }
else if (
ctx->decoder_flushing) {
641 eret =
CHECK_CU(
ctx->cvdl->cuvidUnmapVideoFrame(
ctx->cudecoder, mapped_frame));
659 if (
ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave) {
664 if (!
ctx->decoder_flushing) {
673 }
else if (
ret < 0) {
687 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
691 ctx->cudl->cuCtxPushCurrent(cuda_ctx);
694 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
697 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
709 cuvid_free_functions(&
ctx->cvdl);
715 const CUVIDPARSERPARAMS *cuparseinfo,
721 CUVIDDECODECAPS *caps;
722 int res8 = 0, res10 = 0, res12 = 0;
724 if (!
ctx->cvdl->cuvidGetDecoderCaps) {
725 av_log(avctx,
AV_LOG_WARNING,
"Used Nvidia driver is too old to perform a capability check.\n");
727 #
if defined(_WIN32) || defined(__CYGWIN__)
732 ". Continuing blind.\n");
733 ctx->caps8.bIsSupported =
ctx->caps10.bIsSupported = 1;
735 ctx->caps12.bIsSupported = 0;
739 ctx->caps8.eCodecType =
ctx->caps10.eCodecType =
ctx->caps12.eCodecType
740 = cuparseinfo->CodecType;
741 ctx->caps8.eChromaFormat =
ctx->caps10.eChromaFormat =
ctx->caps12.eChromaFormat
742 = cudaVideoChromaFormat_420;
744 ctx->caps8.nBitDepthMinus8 = 0;
745 ctx->caps10.nBitDepthMinus8 = 2;
746 ctx->caps12.nBitDepthMinus8 = 4;
753 av_log(avctx,
AV_LOG_VERBOSE,
"8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
754 ctx->caps8.bIsSupported,
ctx->caps8.nMinWidth,
ctx->caps8.nMaxWidth,
ctx->caps8.nMinHeight,
ctx->caps8.nMaxHeight);
755 av_log(avctx,
AV_LOG_VERBOSE,
"10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
756 ctx->caps10.bIsSupported,
ctx->caps10.nMinWidth,
ctx->caps10.nMaxWidth,
ctx->caps10.nMinHeight,
ctx->caps10.nMaxHeight);
757 av_log(avctx,
AV_LOG_VERBOSE,
"12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
758 ctx->caps12.bIsSupported,
ctx->caps12.nMinWidth,
ctx->caps12.nMaxWidth,
ctx->caps12.nMinHeight,
ctx->caps12.nMaxHeight);
777 if (!
ctx->caps8.bIsSupported) {
782 if (!caps->bIsSupported) {
787 if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
789 probed_width, caps->nMinWidth, caps->nMaxWidth);
793 if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
795 probed_height, caps->nMinHeight, caps->nMaxHeight);
799 if ((probed_width * probed_height) / 256 > caps->nMaxMBCount) {
801 (
int)(probed_width * probed_height) / 256, caps->nMaxMBCount);
814 CUVIDSOURCEDATAPACKET seq_pkt;
815 CUcontext cuda_ctx =
NULL;
827 int probed_bit_depth = 8;
831 probed_bit_depth = probe_desc->
comp[0].
depth;
844 if (
ctx->resize_expr && sscanf(
ctx->resize_expr,
"%dx%d",
845 &
ctx->resize.width, &
ctx->resize.height) != 2) {
851 if (
ctx->crop_expr && sscanf(
ctx->crop_expr,
"%dx%dx%dx%d",
852 &
ctx->crop.top, &
ctx->crop.bottom,
853 &
ctx->crop.left, &
ctx->crop.right) != 4) {
859 ret = cuvid_load_functions(&
ctx->cvdl, avctx);
866 if (!
ctx->frame_queue) {
881 if (!
ctx->hwdevice) {
888 if (!
ctx->hwdevice) {
909 device_hwctx = device_ctx->
hwctx;
914 memset(&
ctx->cuparseinfo, 0,
sizeof(
ctx->cuparseinfo));
915 memset(&seq_pkt, 0,
sizeof(seq_pkt));
918 #if CONFIG_H264_CUVID_DECODER
920 ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
923 #if CONFIG_HEVC_CUVID_DECODER
925 ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
928 #if CONFIG_MJPEG_CUVID_DECODER
930 ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
933 #if CONFIG_MPEG1_CUVID_DECODER
935 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
938 #if CONFIG_MPEG2_CUVID_DECODER
940 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
943 #if CONFIG_MPEG4_CUVID_DECODER
945 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
948 #if CONFIG_VP8_CUVID_DECODER
950 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
953 #if CONFIG_VP9_CUVID_DECODER
955 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
958 #if CONFIG_VC1_CUVID_DECODER
960 ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
963 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
965 ctx->cuparseinfo.CodecType = cudaVideoCodec_AV1;
983 +
FFMAX(extradata_size - (
int)
sizeof(
ctx->cuparse_ext->raw_seqhdr_data), 0));
984 if (!
ctx->cuparse_ext) {
989 if (extradata_size > 0)
990 memcpy(
ctx->cuparse_ext->raw_seqhdr_data, extradata, extradata_size);
991 ctx->cuparse_ext->format.seqhdr_data_length = extradata_size;
993 ctx->cuparseinfo.pExtVideoInfo =
ctx->cuparse_ext;
996 if (!
ctx->key_frame) {
1001 ctx->cuparseinfo.ulMaxNumDecodeSurfaces =
ctx->nb_surfaces;
1003 ctx->cuparseinfo.pUserData = avctx;
1023 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1024 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1026 if (seq_pkt.payload && seq_pkt.payload_size) {
1036 ctx->prev_pts = INT64_MIN;
1053 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
1054 CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
1064 if (!
ctx->frame_queue) {
1069 if (
ctx->cudecoder) {
1070 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
1074 if (
ctx->cuparser) {
1075 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
1083 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1084 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1086 if (seq_pkt.payload && seq_pkt.payload_size) {
1096 ctx->prev_pts = INT64_MIN;
1097 ctx->decoder_flushing = 0;
1104 #define OFFSET(x) offsetof(CuvidContext, x)
1105 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1107 {
"deint",
"Set deinterlacing mode",
OFFSET(deint_mode),
AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive,
VD,
"deint" },
1108 {
"weave",
"Weave deinterlacing (do nothing)", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0,
VD,
"deint" },
1109 {
"bob",
"Bob deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0,
VD,
"deint" },
1110 {
"adaptive",
"Adaptive deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0,
VD,
"deint" },
1112 {
"surfaces",
"Maximum surfaces to be used for decoding",
OFFSET(nb_surfaces),
AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX,
VD },
1113 {
"drop_second_field",
"Drop second field when deinterlacing",
OFFSET(drop_second_field),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VD },
1132 #define DEFINE_CUVID_CODEC(x, X, bsf_name) \
1133 static const AVClass x##_cuvid_class = { \
1134 .class_name = #x "_cuvid", \
1135 .item_name = av_default_item_name, \
1136 .option = options, \
1137 .version = LIBAVUTIL_VERSION_INT, \
1139 AVCodec ff_##x##_cuvid_decoder = { \
1140 .name = #x "_cuvid", \
1141 .long_name = NULL_IF_CONFIG_SMALL("Nvidia CUVID " #X " decoder"), \
1142 .type = AVMEDIA_TYPE_VIDEO, \
1143 .id = AV_CODEC_ID_##X, \
1144 .priv_data_size = sizeof(CuvidContext), \
1145 .priv_class = &x##_cuvid_class, \
1146 .init = cuvid_decode_init, \
1147 .close = cuvid_decode_end, \
1148 .decode = cuvid_decode_frame, \
1149 .receive_frame = cuvid_output_frame, \
1150 .flush = cuvid_flush, \
1152 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
1153 .caps_internal = FF_CODEC_CAP_SETS_FRAME_PROPS, \
1154 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, \
1158 AV_PIX_FMT_NONE }, \
1159 .hw_configs = cuvid_hw_configs, \
1160 .wrapper_name = "cuvid", \
1163 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1167 #if CONFIG_HEVC_CUVID_DECODER
1171 #if CONFIG_H264_CUVID_DECODER
1175 #if CONFIG_MJPEG_CUVID_DECODER
1179 #if CONFIG_MPEG1_CUVID_DECODER
1183 #if CONFIG_MPEG2_CUVID_DECODER
1187 #if CONFIG_MPEG4_CUVID_DECODER
1191 #if CONFIG_VP8_CUVID_DECODER
1195 #if CONFIG_VP9_CUVID_DECODER
1199 #if CONFIG_VC1_CUVID_DECODER