22 #include "config_components.h"
45 #if !NVDECAPI_CHECK_VERSION(9, 0)
46 #define cudaVideoSurfaceFormat_YUV444 2
47 #define cudaVideoSurfaceFormat_YUV444_16Bit 3
50 #if NVDECAPI_CHECK_VERSION(11, 0)
51 #define CUVID_HAS_AV1_SUPPORT
117 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x)
120 #define CUVID_MAX_DISPLAY_DELAY (4)
123 #define CUVID_DEFAULT_NUM_SURFACES (CUVID_MAX_DISPLAY_DELAY + 1)
130 CUVIDDECODECAPS *caps =
NULL;
131 CUVIDDECODECREATEINFO cuinfo;
136 int old_width = avctx->
width;
137 int old_height = avctx->
height;
145 memset(&cuinfo, 0,
sizeof(cuinfo));
147 ctx->internal_error = 0;
149 avctx->coded_width = cuinfo.ulWidth =
format->coded_width;
150 avctx->coded_height = cuinfo.ulHeight =
format->coded_height;
153 cuinfo.display_area.left =
format->display_area.left +
ctx->crop.left;
154 cuinfo.display_area.top =
format->display_area.top +
ctx->crop.top;
155 cuinfo.display_area.right =
format->display_area.right -
ctx->crop.right;
156 cuinfo.display_area.bottom =
format->display_area.bottom -
ctx->crop.bottom;
159 if (
ctx->resize_expr) {
160 avctx->width =
ctx->resize.width;
161 avctx->height =
ctx->resize.height;
163 avctx->width = cuinfo.display_area.right - cuinfo.display_area.left;
164 avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top;
168 cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1;
169 cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1;
172 cuinfo.target_rect.left = 0;
173 cuinfo.target_rect.top = 0;
174 cuinfo.target_rect.right = cuinfo.ulTargetWidth;
175 cuinfo.target_rect.bottom = cuinfo.ulTargetHeight;
177 chroma_444 =
format->chroma_format == cudaVideoChromaFormat_444;
179 switch (
format->bit_depth_luma_minus8) {
183 #ifdef NVDEC_HAVE_422_SUPPORT
184 }
else if (
format->chroma_format == cudaVideoChromaFormat_422) {
195 #ifdef NVDEC_HAVE_422_SUPPORT
196 }
else if (
format->chroma_format == cudaVideoChromaFormat_422) {
207 #ifdef NVDEC_HAVE_422_SUPPORT
208 }
else if (
format->chroma_format == cudaVideoChromaFormat_422) {
220 if (!caps || !caps->bIsSupported) {
222 format->bit_depth_luma_minus8 + 8);
228 if (surface_fmt < 0) {
239 avctx->pix_fmt = surface_fmt;
242 if (avctx->hw_frames_ctx) {
258 ctx->deint_mode_current =
format->progressive_sequence
259 ? cudaVideoDeinterlaceMode_Weave
262 ctx->progressive_sequence =
format->progressive_sequence;
264 if (!
format->progressive_sequence &&
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
269 if (
format->video_signal_description.video_full_range_flag)
275 avctx->color_trc =
format->video_signal_description.transfer_characteristics;
279 avctx->bit_rate =
format->bitrate;
281 if (
format->frame_rate.numerator &&
format->frame_rate.denominator) {
282 avctx->framerate.num =
format->frame_rate.numerator;
283 avctx->framerate.den =
format->frame_rate.denominator;
287 && avctx->coded_width ==
format->coded_width
288 && avctx->coded_height ==
format->coded_height
289 && avctx->
width == old_width
290 && avctx->height == old_height
291 &&
ctx->chroma_format ==
format->chroma_format
295 if (
ctx->cudecoder) {
298 if (
ctx->internal_error < 0)
303 if (hwframe_ctx->pool && (
304 hwframe_ctx->width < avctx->width ||
305 hwframe_ctx->height < avctx->height ||
307 hwframe_ctx->sw_format != avctx->sw_pix_fmt)) {
308 av_log(avctx,
AV_LOG_ERROR,
"AVHWFramesContext is already initialized with incompatible parameters\n");
310 av_log(avctx,
AV_LOG_DEBUG,
"height: %d <-> %d\n", hwframe_ctx->height, avctx->height);
318 ctx->chroma_format =
format->chroma_format;
320 cuinfo.CodecType =
ctx->codec_type =
format->codec;
321 cuinfo.ChromaFormat =
format->chroma_format;
323 switch (avctx->sw_pix_fmt) {
325 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
329 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
331 #ifdef NVDEC_HAVE_422_SUPPORT
333 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV16;
336 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P216;
352 fifo_size_inc =
ctx->nb_surfaces;
355 if (avctx->extra_hw_frames > 0)
356 ctx->nb_surfaces += avctx->extra_hw_frames;
358 fifo_size_inc =
ctx->nb_surfaces - fifo_size_inc;
359 if (fifo_size_inc > 0 &&
av_fifo_grow2(
ctx->frame_queue, fifo_size_inc) < 0) {
360 av_log(avctx,
AV_LOG_ERROR,
"Failed to grow frame queue on video sequence callback\n");
366 av_log(avctx,
AV_LOG_ERROR,
"Failed to grow key frame array on video sequence callback\n");
371 cuinfo.ulNumDecodeSurfaces =
ctx->nb_surfaces;
372 cuinfo.ulNumOutputSurfaces = 1;
373 cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
374 cuinfo.bitDepthMinus8 =
format->bit_depth_luma_minus8;
375 cuinfo.DeinterlaceMode =
ctx->deint_mode_current;
377 if (
ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
380 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidCreateDecoder(&
ctx->cudecoder, &cuinfo));
381 if (
ctx->internal_error < 0)
384 if (!hwframe_ctx->pool) {
386 hwframe_ctx->sw_format = avctx->sw_pix_fmt;
387 hwframe_ctx->width = avctx->width;
388 hwframe_ctx->height = avctx->height;
396 if(
ctx->cuparseinfo.ulMaxNumDecodeSurfaces != cuinfo.ulNumDecodeSurfaces) {
397 ctx->cuparseinfo.ulMaxNumDecodeSurfaces = cuinfo.ulNumDecodeSurfaces;
398 return cuinfo.ulNumDecodeSurfaces;
411 if(picparams->intra_pic_flag)
412 ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
414 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidDecodePicture(
ctx->cudecoder, picparams));
415 if (
ctx->internal_error < 0)
428 ctx->internal_error = 0;
431 parsed_frame.
dispinfo.progressive_frame =
ctx->progressive_sequence;
433 if (
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
438 if (!
ctx->drop_second_field) {
451 int delay =
ctx->cuparseinfo.ulMaxDisplayDelay;
452 if (
ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
463 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
464 CUVIDSOURCEDATAPACKET cupkt;
465 int ret = 0, eret = 0, is_flush =
ctx->decoder_flushing;
469 if (is_flush && avpkt && avpkt->
size)
480 memset(&cupkt, 0,
sizeof(cupkt));
482 if (avpkt && avpkt->
size) {
483 cupkt.payload_size = avpkt->
size;
484 cupkt.payload = avpkt->
data;
487 cupkt.flags = CUVID_PKT_TIMESTAMP;
491 cupkt.timestamp = avpkt->
pts;
494 cupkt.flags = CUVID_PKT_ENDOFSTREAM;
495 ctx->decoder_flushing = 1;
504 if (
ctx->internal_error) {
506 ret =
ctx->internal_error;
528 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
530 CUdeviceptr mapped_frame = 0;
531 int ret = 0, eret = 0;
535 if (
ctx->decoder_flushing) {
561 CUVIDPROCPARAMS params;
562 unsigned int pitch = 0;
566 memset(¶ms, 0,
sizeof(params));
567 params.progressive_frame = parsed_frame.dispinfo.progressive_frame;
568 params.second_field = parsed_frame.second_field;
569 params.top_field_first = parsed_frame.dispinfo.top_field_first;
571 ret =
CHECK_CU(
ctx->cvdl->cuvidMapVideoFrame(
ctx->cudecoder, parsed_frame.dispinfo.picture_index, &mapped_frame, &pitch, ¶ms));
592 CUDA_MEMCPY2D cpy = {
593 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
594 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
595 .srcDevice = mapped_frame,
596 .dstDevice = (CUdeviceptr)
frame->data[
i],
598 .dstPitch =
frame->linesize[
i],
604 ret =
CHECK_CU(
ctx->cudl->cuMemcpy2DAsync(&cpy, device_hwctx->stream));
613 #ifdef NVDEC_HAVE_422_SUPPORT
646 tmp_frame->
data[
i] = (uint8_t*)mapped_frame +
offset;
670 if (
ctx->key_frame[parsed_frame.dispinfo.picture_index])
674 ctx->key_frame[parsed_frame.dispinfo.picture_index] = 0;
681 frame->pts = parsed_frame.dispinfo.timestamp;
683 if (parsed_frame.second_field) {
684 if (
ctx->prev_pts == INT64_MIN) {
688 int pts_diff = (
frame->pts -
ctx->prev_pts) / 2;
690 frame->pts += pts_diff;
701 frame->pkt_size = -1;
705 if (!parsed_frame.is_deinterlacing && !parsed_frame.dispinfo.progressive_frame)
710 }
else if (
ctx->decoder_flushing) {
721 eret =
CHECK_CU(
ctx->cvdl->cuvidUnmapVideoFrame(
ctx->cudecoder, mapped_frame));
736 CUcontext
dummy, cuda_ctx = device_hwctx ? device_hwctx->cuda_ctx :
NULL;
741 ctx->cudl->cuCtxPushCurrent(cuda_ctx);
744 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
747 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
760 cuvid_free_functions(&
ctx->cvdl);
766 const CUVIDPARSERPARAMS *cuparseinfo,
772 CUVIDDECODECAPS *caps;
773 cudaVideoChromaFormat chroma_format;
774 int res8 = 0, res10 = 0, res12 = 0;
776 if (!
ctx->cvdl->cuvidGetDecoderCaps) {
777 av_log(avctx,
AV_LOG_WARNING,
"Used Nvidia driver is too old to perform a capability check.\n");
779 #
if defined(_WIN32) || defined(__CYGWIN__)
784 ". Continuing blind.\n");
785 ctx->caps8.bIsSupported =
ctx->caps10.bIsSupported = 1;
787 ctx->caps12.bIsSupported = 0;
791 ctx->caps8.eCodecType =
ctx->caps10.eCodecType =
ctx->caps12.eCodecType
792 = cuparseinfo->CodecType;
796 chroma_format = cudaVideoChromaFormat_444;
798 #ifdef NVDEC_HAVE_422_SUPPORT
802 chroma_format = cudaVideoChromaFormat_422;
808 chroma_format = cudaVideoChromaFormat_420;
811 chroma_format = cudaVideoChromaFormat_Monochrome;
813 ctx->caps8.eChromaFormat =
ctx->caps10.eChromaFormat =
ctx->caps12.eChromaFormat
816 ctx->caps8.nBitDepthMinus8 = 0;
817 ctx->caps10.nBitDepthMinus8 = 2;
818 ctx->caps12.nBitDepthMinus8 = 4;
825 av_log(avctx,
AV_LOG_VERBOSE,
"8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
826 ctx->caps8.bIsSupported,
ctx->caps8.nMinWidth,
ctx->caps8.nMaxWidth,
ctx->caps8.nMinHeight,
ctx->caps8.nMaxHeight);
827 av_log(avctx,
AV_LOG_VERBOSE,
"10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
828 ctx->caps10.bIsSupported,
ctx->caps10.nMinWidth,
ctx->caps10.nMaxWidth,
ctx->caps10.nMinHeight,
ctx->caps10.nMaxHeight);
829 av_log(avctx,
AV_LOG_VERBOSE,
"12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
830 ctx->caps12.bIsSupported,
ctx->caps12.nMinWidth,
ctx->caps12.nMaxWidth,
ctx->caps12.nMinHeight,
ctx->caps12.nMaxHeight);
849 if (!
ctx->caps8.bIsSupported) {
854 if (!caps->bIsSupported) {
859 if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
861 probed_width, caps->nMinWidth, caps->nMaxWidth);
865 if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
867 probed_height, caps->nMinHeight, caps->nMaxHeight);
871 if ((probed_width * probed_height) / 256 > caps->nMaxMBCount) {
873 (
int)(probed_width * probed_height) / 256, caps->nMaxMBCount);
886 CUVIDSOURCEDATAPACKET seq_pkt;
887 CUcontext cuda_ctx =
NULL;
899 int probed_bit_depth = 8, is_yuv444 = 0, is_yuv422 = 0;
903 probed_bit_depth = probe_desc->
comp[0].
depth;
908 #ifdef NVDEC_HAVE_422_SUPPORT
914 switch (probed_bit_depth) {
938 if (
ctx->resize_expr && sscanf(
ctx->resize_expr,
"%dx%d",
939 &
ctx->resize.width, &
ctx->resize.height) != 2) {
945 if (
ctx->crop_expr && sscanf(
ctx->crop_expr,
"%dx%dx%dx%d",
946 &
ctx->crop.top, &
ctx->crop.bottom,
947 &
ctx->crop.left, &
ctx->crop.right) != 4) {
953 ret = cuvid_load_functions(&
ctx->cvdl, avctx);
960 if(
ctx->nb_surfaces < 0)
964 if (!
ctx->frame_queue) {
979 if (!
ctx->hwdevice) {
986 if (!
ctx->hwdevice) {
1007 device_hwctx = device_ctx->
hwctx;
1012 memset(&
ctx->cuparseinfo, 0,
sizeof(
ctx->cuparseinfo));
1013 memset(&seq_pkt, 0,
sizeof(seq_pkt));
1016 #if CONFIG_H264_CUVID_DECODER
1018 ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
1021 #if CONFIG_HEVC_CUVID_DECODER
1023 ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
1026 #if CONFIG_MJPEG_CUVID_DECODER
1028 ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
1031 #if CONFIG_MPEG1_CUVID_DECODER
1033 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
1036 #if CONFIG_MPEG2_CUVID_DECODER
1038 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
1041 #if CONFIG_MPEG4_CUVID_DECODER
1043 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
1046 #if CONFIG_VP8_CUVID_DECODER
1048 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
1051 #if CONFIG_VP9_CUVID_DECODER
1053 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
1056 #if CONFIG_VC1_CUVID_DECODER
1058 ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
1061 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1063 ctx->cuparseinfo.CodecType = cudaVideoCodec_AV1;
1084 extradata_size > 4 &&
1085 extradata[0] & 0x80) {
1087 extradata_size -= 4;
1091 +
FFMAX(extradata_size - (
int)
sizeof(
ctx->cuparse_ext->raw_seqhdr_data), 0));
1092 if (!
ctx->cuparse_ext) {
1097 if (extradata_size > 0)
1098 memcpy(
ctx->cuparse_ext->raw_seqhdr_data, extradata, extradata_size);
1099 ctx->cuparse_ext->format.seqhdr_data_length = extradata_size;
1101 ctx->cuparseinfo.pExtVideoInfo =
ctx->cuparse_ext;
1104 if (!
ctx->key_frame) {
1109 ctx->cuparseinfo.ulMaxNumDecodeSurfaces = 1;
1111 ctx->cuparseinfo.pUserData = avctx;
1131 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1132 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1134 if (seq_pkt.payload && seq_pkt.payload_size) {
1144 ctx->prev_pts = INT64_MIN;
1161 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
1162 CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
1171 if (
ctx->cudecoder) {
1172 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
1176 if (
ctx->cuparser) {
1177 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
1185 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1186 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1188 if (seq_pkt.payload && seq_pkt.payload_size) {
1198 ctx->prev_pts = INT64_MIN;
1199 ctx->decoder_flushing = 0;
1206 #define OFFSET(x) offsetof(CuvidContext, x)
1207 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1209 {
"deint",
"Set deinterlacing mode",
OFFSET(deint_mode),
AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive,
VD, .unit =
"deint" },
1210 {
"weave",
"Weave deinterlacing (do nothing)", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0,
VD, .unit =
"deint" },
1211 {
"bob",
"Bob deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0,
VD, .unit =
"deint" },
1212 {
"adaptive",
"Adaptive deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0,
VD, .unit =
"deint" },
1215 {
"drop_second_field",
"Drop second field when deinterlacing",
OFFSET(drop_second_field),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VD },
1235 #define DEFINE_CUVID_CODEC(x, X, bsf_name) \
1236 static const AVClass x##_cuvid_class = { \
1237 .class_name = #x "_cuvid", \
1238 .item_name = av_default_item_name, \
1239 .option = options, \
1240 .version = LIBAVUTIL_VERSION_INT, \
1242 const FFCodec ff_##x##_cuvid_decoder = { \
1243 .p.name = #x "_cuvid", \
1244 CODEC_LONG_NAME("Nvidia CUVID " #X " decoder"), \
1245 .p.type = AVMEDIA_TYPE_VIDEO, \
1246 .p.id = AV_CODEC_ID_##X, \
1247 .priv_data_size = sizeof(CuvidContext), \
1248 .p.priv_class = &x##_cuvid_class, \
1249 .init = cuvid_decode_init, \
1250 .close = cuvid_decode_end, \
1251 FF_CODEC_RECEIVE_FRAME_CB(cuvid_output_frame), \
1252 .flush = cuvid_flush, \
1254 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
1255 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | \
1256 FF_CODEC_CAP_SETS_FRAME_PROPS, \
1257 .hw_configs = cuvid_hw_configs, \
1258 .p.wrapper_name = "cuvid", \
1261 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1265 #if CONFIG_HEVC_CUVID_DECODER
1269 #if CONFIG_H264_CUVID_DECODER
1273 #if CONFIG_MJPEG_CUVID_DECODER
1277 #if CONFIG_MPEG1_CUVID_DECODER
1281 #if CONFIG_MPEG2_CUVID_DECODER
1285 #if CONFIG_MPEG4_CUVID_DECODER
1289 #if CONFIG_VP8_CUVID_DECODER
1293 #if CONFIG_VP9_CUVID_DECODER
1297 #if CONFIG_VC1_CUVID_DECODER