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)
119 #define CUVID_MAX_DISPLAY_DELAY (4)
122 #define CUVID_DEFAULT_NUM_SURFACES (CUVID_MAX_DISPLAY_DELAY + 1)
129 CUVIDDECODECAPS *caps =
NULL;
130 CUVIDDECODECREATEINFO cuinfo;
135 int old_width = avctx->
width;
136 int old_height = avctx->
height;
144 memset(&cuinfo, 0,
sizeof(cuinfo));
146 ctx->internal_error = 0;
148 avctx->coded_width = cuinfo.ulWidth =
format->coded_width;
149 avctx->coded_height = cuinfo.ulHeight =
format->coded_height;
152 cuinfo.display_area.left =
format->display_area.left +
ctx->crop.left;
153 cuinfo.display_area.top =
format->display_area.top +
ctx->crop.top;
154 cuinfo.display_area.right =
format->display_area.right -
ctx->crop.right;
155 cuinfo.display_area.bottom =
format->display_area.bottom -
ctx->crop.bottom;
158 if (
ctx->resize_expr) {
159 avctx->width =
ctx->resize.width;
160 avctx->height =
ctx->resize.height;
162 avctx->width = cuinfo.display_area.right - cuinfo.display_area.left;
163 avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top;
167 cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1;
168 cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1;
171 cuinfo.target_rect.left = 0;
172 cuinfo.target_rect.top = 0;
173 cuinfo.target_rect.right = cuinfo.ulTargetWidth;
174 cuinfo.target_rect.bottom = cuinfo.ulTargetHeight;
176 chroma_444 =
format->chroma_format == cudaVideoChromaFormat_444;
178 switch (
format->bit_depth_luma_minus8) {
195 if (!caps || !caps->bIsSupported) {
197 format->bit_depth_luma_minus8 + 8);
203 if (surface_fmt < 0) {
214 avctx->pix_fmt = surface_fmt;
217 if (avctx->hw_frames_ctx) {
233 ctx->deint_mode_current =
format->progressive_sequence
234 ? cudaVideoDeinterlaceMode_Weave
237 ctx->progressive_sequence =
format->progressive_sequence;
239 if (!
format->progressive_sequence &&
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
244 if (
format->video_signal_description.video_full_range_flag)
250 avctx->color_trc =
format->video_signal_description.transfer_characteristics;
254 avctx->bit_rate =
format->bitrate;
256 if (
format->frame_rate.numerator &&
format->frame_rate.denominator) {
257 avctx->framerate.num =
format->frame_rate.numerator;
258 avctx->framerate.den =
format->frame_rate.denominator;
262 && avctx->coded_width ==
format->coded_width
263 && avctx->coded_height ==
format->coded_height
264 && avctx->
width == old_width
265 && avctx->height == old_height
266 &&
ctx->chroma_format ==
format->chroma_format
270 if (
ctx->cudecoder) {
273 if (
ctx->internal_error < 0)
278 if (hwframe_ctx->pool && (
279 hwframe_ctx->width < avctx->width ||
280 hwframe_ctx->height < avctx->height ||
282 hwframe_ctx->sw_format != avctx->sw_pix_fmt)) {
283 av_log(avctx,
AV_LOG_ERROR,
"AVHWFramesContext is already initialized with incompatible parameters\n");
285 av_log(avctx,
AV_LOG_DEBUG,
"height: %d <-> %d\n", hwframe_ctx->height, avctx->height);
293 ctx->chroma_format =
format->chroma_format;
295 cuinfo.CodecType =
ctx->codec_type =
format->codec;
296 cuinfo.ChromaFormat =
format->chroma_format;
298 switch (avctx->sw_pix_fmt) {
300 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
304 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
319 fifo_size_inc =
ctx->nb_surfaces;
322 if (avctx->extra_hw_frames > 0)
323 ctx->nb_surfaces += avctx->extra_hw_frames;
325 fifo_size_inc =
ctx->nb_surfaces - fifo_size_inc;
326 if (fifo_size_inc > 0 &&
av_fifo_grow2(
ctx->frame_queue, fifo_size_inc) < 0) {
327 av_log(avctx,
AV_LOG_ERROR,
"Failed to grow frame queue on video sequence callback\n");
333 av_log(avctx,
AV_LOG_ERROR,
"Failed to grow key frame array on video sequence callback\n");
338 cuinfo.ulNumDecodeSurfaces =
ctx->nb_surfaces;
339 cuinfo.ulNumOutputSurfaces = 1;
340 cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
341 cuinfo.bitDepthMinus8 =
format->bit_depth_luma_minus8;
342 cuinfo.DeinterlaceMode =
ctx->deint_mode_current;
344 if (
ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
347 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidCreateDecoder(&
ctx->cudecoder, &cuinfo));
348 if (
ctx->internal_error < 0)
351 if (!hwframe_ctx->pool) {
353 hwframe_ctx->sw_format = avctx->sw_pix_fmt;
354 hwframe_ctx->width = avctx->width;
355 hwframe_ctx->height = avctx->height;
363 if(
ctx->cuparseinfo.ulMaxNumDecodeSurfaces != cuinfo.ulNumDecodeSurfaces) {
364 ctx->cuparseinfo.ulMaxNumDecodeSurfaces = cuinfo.ulNumDecodeSurfaces;
365 return cuinfo.ulNumDecodeSurfaces;
378 if(picparams->intra_pic_flag)
379 ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
381 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidDecodePicture(
ctx->cudecoder, picparams));
382 if (
ctx->internal_error < 0)
395 ctx->internal_error = 0;
398 parsed_frame.
dispinfo.progressive_frame =
ctx->progressive_sequence;
400 if (
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
405 if (!
ctx->drop_second_field) {
418 int delay =
ctx->cuparseinfo.ulMaxDisplayDelay;
419 if (
ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
430 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
431 CUVIDSOURCEDATAPACKET cupkt;
432 int ret = 0, eret = 0, is_flush =
ctx->decoder_flushing;
436 if (is_flush && avpkt && avpkt->
size)
447 memset(&cupkt, 0,
sizeof(cupkt));
449 if (avpkt && avpkt->
size) {
450 cupkt.payload_size = avpkt->
size;
451 cupkt.payload = avpkt->
data;
454 cupkt.flags = CUVID_PKT_TIMESTAMP;
458 cupkt.timestamp = avpkt->
pts;
461 cupkt.flags = CUVID_PKT_ENDOFSTREAM;
462 ctx->decoder_flushing = 1;
471 if (
ctx->internal_error) {
473 ret =
ctx->internal_error;
495 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
497 CUdeviceptr mapped_frame = 0;
498 int ret = 0, eret = 0;
502 if (
ctx->decoder_flushing) {
528 CUVIDPROCPARAMS params;
529 unsigned int pitch = 0;
533 memset(¶ms, 0,
sizeof(params));
534 params.progressive_frame = parsed_frame.dispinfo.progressive_frame;
535 params.second_field = parsed_frame.second_field;
536 params.top_field_first = parsed_frame.dispinfo.top_field_first;
538 ret =
CHECK_CU(
ctx->cvdl->cuvidMapVideoFrame(
ctx->cudecoder, parsed_frame.dispinfo.picture_index, &mapped_frame, &pitch, ¶ms));
559 CUDA_MEMCPY2D cpy = {
560 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
561 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
562 .srcDevice = mapped_frame,
571 ret =
CHECK_CU(
ctx->cudl->cuMemcpy2DAsync(&cpy, device_hwctx->stream));
609 tmp_frame->
data[
i] = (uint8_t*)mapped_frame +
offset;
633 if (
ctx->key_frame[parsed_frame.dispinfo.picture_index])
637 ctx->key_frame[parsed_frame.dispinfo.picture_index] = 0;
644 frame->
pts = parsed_frame.dispinfo.timestamp;
646 if (parsed_frame.second_field) {
647 if (
ctx->prev_pts == INT64_MIN) {
668 if (!parsed_frame.is_deinterlacing && !parsed_frame.dispinfo.progressive_frame)
673 }
else if (
ctx->decoder_flushing) {
684 eret =
CHECK_CU(
ctx->cvdl->cuvidUnmapVideoFrame(
ctx->cudecoder, mapped_frame));
699 CUcontext
dummy, cuda_ctx = device_hwctx ? device_hwctx->cuda_ctx :
NULL;
704 ctx->cudl->cuCtxPushCurrent(cuda_ctx);
707 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
710 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
723 cuvid_free_functions(&
ctx->cvdl);
729 const CUVIDPARSERPARAMS *cuparseinfo,
735 CUVIDDECODECAPS *caps;
736 int res8 = 0, res10 = 0, res12 = 0;
738 if (!
ctx->cvdl->cuvidGetDecoderCaps) {
739 av_log(avctx,
AV_LOG_WARNING,
"Used Nvidia driver is too old to perform a capability check.\n");
741 #
if defined(_WIN32) || defined(__CYGWIN__)
746 ". Continuing blind.\n");
747 ctx->caps8.bIsSupported =
ctx->caps10.bIsSupported = 1;
749 ctx->caps12.bIsSupported = 0;
753 ctx->caps8.eCodecType =
ctx->caps10.eCodecType =
ctx->caps12.eCodecType
754 = cuparseinfo->CodecType;
755 ctx->caps8.eChromaFormat =
ctx->caps10.eChromaFormat =
ctx->caps12.eChromaFormat
756 = cudaVideoChromaFormat_420;
758 ctx->caps8.nBitDepthMinus8 = 0;
759 ctx->caps10.nBitDepthMinus8 = 2;
760 ctx->caps12.nBitDepthMinus8 = 4;
767 av_log(avctx,
AV_LOG_VERBOSE,
"8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
768 ctx->caps8.bIsSupported,
ctx->caps8.nMinWidth,
ctx->caps8.nMaxWidth,
ctx->caps8.nMinHeight,
ctx->caps8.nMaxHeight);
769 av_log(avctx,
AV_LOG_VERBOSE,
"10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
770 ctx->caps10.bIsSupported,
ctx->caps10.nMinWidth,
ctx->caps10.nMaxWidth,
ctx->caps10.nMinHeight,
ctx->caps10.nMaxHeight);
771 av_log(avctx,
AV_LOG_VERBOSE,
"12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
772 ctx->caps12.bIsSupported,
ctx->caps12.nMinWidth,
ctx->caps12.nMaxWidth,
ctx->caps12.nMinHeight,
ctx->caps12.nMaxHeight);
791 if (!
ctx->caps8.bIsSupported) {
796 if (!caps->bIsSupported) {
801 if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
803 probed_width, caps->nMinWidth, caps->nMaxWidth);
807 if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
809 probed_height, caps->nMinHeight, caps->nMaxHeight);
813 if ((probed_width * probed_height) / 256 > caps->nMaxMBCount) {
815 (
int)(probed_width * probed_height) / 256, caps->nMaxMBCount);
828 CUVIDSOURCEDATAPACKET seq_pkt;
829 CUcontext cuda_ctx =
NULL;
841 int probed_bit_depth = 8;
845 probed_bit_depth = probe_desc->
comp[0].
depth;
859 if (
ctx->resize_expr && sscanf(
ctx->resize_expr,
"%dx%d",
860 &
ctx->resize.width, &
ctx->resize.height) != 2) {
866 if (
ctx->crop_expr && sscanf(
ctx->crop_expr,
"%dx%dx%dx%d",
867 &
ctx->crop.top, &
ctx->crop.bottom,
868 &
ctx->crop.left, &
ctx->crop.right) != 4) {
874 ret = cuvid_load_functions(&
ctx->cvdl, avctx);
881 if(
ctx->nb_surfaces < 0)
885 if (!
ctx->frame_queue) {
900 if (!
ctx->hwdevice) {
907 if (!
ctx->hwdevice) {
928 device_hwctx = device_ctx->
hwctx;
933 memset(&
ctx->cuparseinfo, 0,
sizeof(
ctx->cuparseinfo));
934 memset(&seq_pkt, 0,
sizeof(seq_pkt));
937 #if CONFIG_H264_CUVID_DECODER
939 ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
942 #if CONFIG_HEVC_CUVID_DECODER
944 ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
947 #if CONFIG_MJPEG_CUVID_DECODER
949 ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
952 #if CONFIG_MPEG1_CUVID_DECODER
954 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
957 #if CONFIG_MPEG2_CUVID_DECODER
959 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
962 #if CONFIG_MPEG4_CUVID_DECODER
964 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
967 #if CONFIG_VP8_CUVID_DECODER
969 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
972 #if CONFIG_VP9_CUVID_DECODER
974 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
977 #if CONFIG_VC1_CUVID_DECODER
979 ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
982 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
984 ctx->cuparseinfo.CodecType = cudaVideoCodec_AV1;
1005 extradata_size > 4 &&
1006 extradata[0] & 0x80) {
1008 extradata_size -= 4;
1012 +
FFMAX(extradata_size - (
int)
sizeof(
ctx->cuparse_ext->raw_seqhdr_data), 0));
1013 if (!
ctx->cuparse_ext) {
1018 if (extradata_size > 0)
1019 memcpy(
ctx->cuparse_ext->raw_seqhdr_data, extradata, extradata_size);
1020 ctx->cuparse_ext->format.seqhdr_data_length = extradata_size;
1022 ctx->cuparseinfo.pExtVideoInfo =
ctx->cuparse_ext;
1025 if (!
ctx->key_frame) {
1030 ctx->cuparseinfo.ulMaxNumDecodeSurfaces = 1;
1032 ctx->cuparseinfo.pUserData = avctx;
1052 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1053 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1055 if (seq_pkt.payload && seq_pkt.payload_size) {
1065 ctx->prev_pts = INT64_MIN;
1082 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
1083 CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
1092 if (
ctx->cudecoder) {
1093 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
1097 if (
ctx->cuparser) {
1098 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
1106 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1107 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1109 if (seq_pkt.payload && seq_pkt.payload_size) {
1119 ctx->prev_pts = INT64_MIN;
1120 ctx->decoder_flushing = 0;
1127 #define OFFSET(x) offsetof(CuvidContext, x)
1128 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1130 {
"deint",
"Set deinterlacing mode",
OFFSET(deint_mode),
AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive,
VD,
"deint" },
1131 {
"weave",
"Weave deinterlacing (do nothing)", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0,
VD,
"deint" },
1132 {
"bob",
"Bob deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0,
VD,
"deint" },
1133 {
"adaptive",
"Adaptive deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0,
VD,
"deint" },
1136 {
"drop_second_field",
"Drop second field when deinterlacing",
OFFSET(drop_second_field),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VD },
1155 #define DEFINE_CUVID_CODEC(x, X, bsf_name) \
1156 static const AVClass x##_cuvid_class = { \
1157 .class_name = #x "_cuvid", \
1158 .item_name = av_default_item_name, \
1159 .option = options, \
1160 .version = LIBAVUTIL_VERSION_INT, \
1162 const FFCodec ff_##x##_cuvid_decoder = { \
1163 .p.name = #x "_cuvid", \
1164 CODEC_LONG_NAME("Nvidia CUVID " #X " decoder"), \
1165 .p.type = AVMEDIA_TYPE_VIDEO, \
1166 .p.id = AV_CODEC_ID_##X, \
1167 .priv_data_size = sizeof(CuvidContext), \
1168 .p.priv_class = &x##_cuvid_class, \
1169 .init = cuvid_decode_init, \
1170 .close = cuvid_decode_end, \
1171 FF_CODEC_RECEIVE_FRAME_CB(cuvid_output_frame), \
1172 .flush = cuvid_flush, \
1174 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
1175 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | \
1176 FF_CODEC_CAP_SETS_FRAME_PROPS, \
1177 .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, \
1181 AV_PIX_FMT_NONE }, \
1182 .hw_configs = cuvid_hw_configs, \
1183 .p.wrapper_name = "cuvid", \
1186 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1190 #if CONFIG_HEVC_CUVID_DECODER
1194 #if CONFIG_H264_CUVID_DECODER
1198 #if CONFIG_MJPEG_CUVID_DECODER
1202 #if CONFIG_MPEG1_CUVID_DECODER
1206 #if CONFIG_MPEG2_CUVID_DECODER
1210 #if CONFIG_MPEG4_CUVID_DECODER
1214 #if CONFIG_VP8_CUVID_DECODER
1218 #if CONFIG_VP9_CUVID_DECODER
1222 #if CONFIG_VC1_CUVID_DECODER