40 #if !NVDECAPI_CHECK_VERSION(9, 0)
41 #define cudaVideoSurfaceFormat_YUV444 2
42 #define cudaVideoSurfaceFormat_YUV444_16Bit 3
106 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x)
113 CUVIDDECODECAPS *caps =
NULL;
114 CUVIDDECODECREATEINFO cuinfo;
118 int old_width = avctx->
width;
119 int old_height = avctx->
height;
127 memset(&cuinfo, 0,
sizeof(cuinfo));
129 ctx->internal_error = 0;
131 avctx->coded_width = cuinfo.ulWidth =
format->coded_width;
132 avctx->coded_height = cuinfo.ulHeight =
format->coded_height;
135 cuinfo.display_area.left =
format->display_area.left +
ctx->crop.left;
136 cuinfo.display_area.top =
format->display_area.top +
ctx->crop.top;
137 cuinfo.display_area.right =
format->display_area.right -
ctx->crop.right;
138 cuinfo.display_area.bottom =
format->display_area.bottom -
ctx->crop.bottom;
141 if (
ctx->resize_expr) {
142 avctx->width =
ctx->resize.width;
143 avctx->height =
ctx->resize.height;
145 avctx->width = cuinfo.display_area.right - cuinfo.display_area.left;
146 avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top;
150 cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1;
151 cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1;
154 cuinfo.target_rect.left = 0;
155 cuinfo.target_rect.top = 0;
156 cuinfo.target_rect.right = cuinfo.ulTargetWidth;
157 cuinfo.target_rect.bottom = cuinfo.ulTargetHeight;
159 chroma_444 =
format->chroma_format == cudaVideoChromaFormat_444;
161 switch (
format->bit_depth_luma_minus8) {
178 if (!caps || !caps->bIsSupported) {
180 format->bit_depth_luma_minus8 + 8);
186 if (surface_fmt < 0) {
197 avctx->pix_fmt = surface_fmt;
200 if (avctx->hw_frames_ctx) {
216 ctx->deint_mode_current =
format->progressive_sequence
217 ? cudaVideoDeinterlaceMode_Weave
220 ctx->progressive_sequence =
format->progressive_sequence;
222 if (!
format->progressive_sequence &&
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
227 if (
format->video_signal_description.video_full_range_flag)
233 avctx->color_trc =
format->video_signal_description.transfer_characteristics;
237 avctx->bit_rate =
format->bitrate;
239 if (
format->frame_rate.numerator &&
format->frame_rate.denominator) {
240 avctx->framerate.num =
format->frame_rate.numerator;
241 avctx->framerate.den =
format->frame_rate.denominator;
245 && avctx->coded_width ==
format->coded_width
246 && avctx->coded_height ==
format->coded_height
247 && avctx->
width == old_width
248 && avctx->height == old_height
249 &&
ctx->chroma_format ==
format->chroma_format
253 if (
ctx->cudecoder) {
256 if (
ctx->internal_error < 0)
261 if (hwframe_ctx->pool && (
262 hwframe_ctx->width < avctx->width ||
263 hwframe_ctx->height < avctx->height ||
265 hwframe_ctx->sw_format != avctx->sw_pix_fmt)) {
266 av_log(avctx,
AV_LOG_ERROR,
"AVHWFramesContext is already initialized with incompatible parameters\n");
268 av_log(avctx,
AV_LOG_DEBUG,
"height: %d <-> %d\n", hwframe_ctx->height, avctx->height);
276 ctx->chroma_format =
format->chroma_format;
278 cuinfo.CodecType =
ctx->codec_type =
format->codec;
279 cuinfo.ChromaFormat =
format->chroma_format;
281 switch (avctx->sw_pix_fmt) {
283 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
287 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
302 cuinfo.ulNumDecodeSurfaces =
ctx->nb_surfaces;
303 cuinfo.ulNumOutputSurfaces = 1;
304 cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
305 cuinfo.bitDepthMinus8 =
format->bit_depth_luma_minus8;
306 cuinfo.DeinterlaceMode =
ctx->deint_mode_current;
308 if (
ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
311 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidCreateDecoder(&
ctx->cudecoder, &cuinfo));
312 if (
ctx->internal_error < 0)
315 if (!hwframe_ctx->pool) {
317 hwframe_ctx->sw_format = avctx->sw_pix_fmt;
318 hwframe_ctx->width = avctx->width;
319 hwframe_ctx->height = avctx->height;
337 ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
339 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidDecodePicture(
ctx->cudecoder, picparams));
340 if (
ctx->internal_error < 0)
353 ctx->internal_error = 0;
356 parsed_frame.
dispinfo.progressive_frame =
ctx->progressive_sequence;
358 if (
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
363 if (!
ctx->drop_second_field) {
376 int delay =
ctx->cuparseinfo.ulMaxDisplayDelay;
377 if (
ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
388 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
389 CUVIDSOURCEDATAPACKET cupkt;
392 int ret = 0, eret = 0, is_flush =
ctx->decoder_flushing;
396 if (is_flush && avpkt && avpkt->size)
402 if (
ctx->bsf && avpkt && avpkt->size) {
419 avpkt = &filtered_packet;
428 memset(&cupkt, 0,
sizeof(cupkt));
430 if (avpkt && avpkt->size) {
431 cupkt.payload_size = avpkt->size;
432 cupkt.payload = avpkt->data;
435 cupkt.flags = CUVID_PKT_TIMESTAMP;
436 if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
439 cupkt.timestamp = avpkt->pts;
442 cupkt.flags = CUVID_PKT_ENDOFSTREAM;
443 ctx->decoder_flushing = 1;
454 if (
ctx->internal_error) {
456 ret =
ctx->internal_error;
478 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
479 CUdeviceptr mapped_frame = 0;
480 int ret = 0, eret = 0;
484 if (
ctx->decoder_flushing) {
512 unsigned int pitch = 0;
519 params.progressive_frame = parsed_frame.
dispinfo.progressive_frame;
544 CUDA_MEMCPY2D cpy = {
545 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
546 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
547 .srcDevice = mapped_frame,
548 .dstDevice = (CUdeviceptr)
frame->data[
i],
550 .dstPitch =
frame->linesize[
i],
556 ret =
CHECK_CU(
ctx->cudl->cuMemcpy2DAsync(&cpy, device_hwctx->stream));
621 if (
ctx->prev_pts == INT64_MIN) {
625 int pts_diff = (
frame->pts -
ctx->prev_pts) / 2;
627 frame->pts += pts_diff;
640 frame->pkt_duration = 0;
641 frame->pkt_size = -1;
645 if (
frame->interlaced_frame)
646 frame->top_field_first = parsed_frame.
dispinfo.top_field_first;
647 }
else if (
ctx->decoder_flushing) {
655 eret =
CHECK_CU(
ctx->cvdl->cuvidUnmapVideoFrame(
ctx->cudecoder, mapped_frame));
673 if (
ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave) {
678 if (!
ctx->decoder_flushing) {
687 }
else if (
ret < 0) {
706 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
709 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
719 cuvid_free_functions(&
ctx->cvdl);
725 const CUVIDPARSERPARAMS *cuparseinfo,
731 CUVIDDECODECAPS *caps;
732 int res8 = 0, res10 = 0, res12 = 0;
734 if (!
ctx->cvdl->cuvidGetDecoderCaps) {
735 av_log(avctx,
AV_LOG_WARNING,
"Used Nvidia driver is too old to perform a capability check.\n");
737 #
if defined(_WIN32) || defined(__CYGWIN__)
742 ". Continuing blind.\n");
743 ctx->caps8.bIsSupported =
ctx->caps10.bIsSupported = 1;
745 ctx->caps12.bIsSupported = 0;
749 ctx->caps8.eCodecType =
ctx->caps10.eCodecType =
ctx->caps12.eCodecType
750 = cuparseinfo->CodecType;
751 ctx->caps8.eChromaFormat =
ctx->caps10.eChromaFormat =
ctx->caps12.eChromaFormat
752 = cudaVideoChromaFormat_420;
754 ctx->caps8.nBitDepthMinus8 = 0;
755 ctx->caps10.nBitDepthMinus8 = 2;
756 ctx->caps12.nBitDepthMinus8 = 4;
763 av_log(avctx,
AV_LOG_VERBOSE,
"8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
764 ctx->caps8.bIsSupported,
ctx->caps8.nMinWidth,
ctx->caps8.nMaxWidth,
ctx->caps8.nMinHeight,
ctx->caps8.nMaxHeight);
765 av_log(avctx,
AV_LOG_VERBOSE,
"10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
766 ctx->caps10.bIsSupported,
ctx->caps10.nMinWidth,
ctx->caps10.nMaxWidth,
ctx->caps10.nMinHeight,
ctx->caps10.nMaxHeight);
767 av_log(avctx,
AV_LOG_VERBOSE,
"12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
768 ctx->caps12.bIsSupported,
ctx->caps12.nMinWidth,
ctx->caps12.nMaxWidth,
ctx->caps12.nMinHeight,
ctx->caps12.nMaxHeight);
787 if (!
ctx->caps8.bIsSupported) {
792 if (!caps->bIsSupported) {
797 if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
799 probed_width, caps->nMinWidth, caps->nMaxWidth);
803 if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
805 probed_height, caps->nMinHeight, caps->nMaxHeight);
809 if ((probed_width * probed_height) / 256 > caps->nMaxMBCount) {
811 (
int)(probed_width * probed_height) / 256, caps->nMaxMBCount);
824 CUVIDSOURCEDATAPACKET seq_pkt;
825 CUcontext cuda_ctx =
NULL;
838 int probed_bit_depth = 8;
842 probed_bit_depth = probe_desc->
comp[0].
depth;
855 if (
ctx->resize_expr && sscanf(
ctx->resize_expr,
"%dx%d",
856 &
ctx->resize.width, &
ctx->resize.height) != 2) {
862 if (
ctx->crop_expr && sscanf(
ctx->crop_expr,
"%dx%dx%dx%d",
863 &
ctx->crop.top, &
ctx->crop.bottom,
864 &
ctx->crop.left, &
ctx->crop.right) != 4) {
870 ret = cuvid_load_functions(&
ctx->cvdl, avctx);
877 if (!
ctx->frame_queue) {
892 if (!
ctx->hwdevice) {
899 if (!
ctx->hwdevice) {
920 device_hwctx = device_ctx->
hwctx;
925 memset(&
ctx->cuparseinfo, 0,
sizeof(
ctx->cuparseinfo));
926 memset(&seq_pkt, 0,
sizeof(seq_pkt));
929 #if CONFIG_H264_CUVID_DECODER
931 ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
934 #if CONFIG_HEVC_CUVID_DECODER
936 ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
939 #if CONFIG_MJPEG_CUVID_DECODER
941 ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
944 #if CONFIG_MPEG1_CUVID_DECODER
946 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
949 #if CONFIG_MPEG2_CUVID_DECODER
951 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
954 #if CONFIG_MPEG4_CUVID_DECODER
956 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
959 #if CONFIG_VP8_CUVID_DECODER
961 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
964 #if CONFIG_VP9_CUVID_DECODER
966 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
969 #if CONFIG_VC1_CUVID_DECODER
971 ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
997 extradata =
ctx->bsf->par_out->extradata;
998 extradata_size =
ctx->bsf->par_out->extradata_size;
1005 +
FFMAX(extradata_size - (
int)
sizeof(
ctx->cuparse_ext->raw_seqhdr_data), 0));
1006 if (!
ctx->cuparse_ext) {
1011 if (extradata_size > 0)
1012 memcpy(
ctx->cuparse_ext->raw_seqhdr_data, extradata, extradata_size);
1013 ctx->cuparse_ext->format.seqhdr_data_length = extradata_size;
1015 ctx->cuparseinfo.pExtVideoInfo =
ctx->cuparse_ext;
1018 if (!
ctx->key_frame) {
1023 ctx->cuparseinfo.ulMaxNumDecodeSurfaces =
ctx->nb_surfaces;
1024 ctx->cuparseinfo.ulMaxDisplayDelay = 4;
1025 ctx->cuparseinfo.pUserData = avctx;
1045 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1046 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1048 if (seq_pkt.payload && seq_pkt.payload_size) {
1058 ctx->prev_pts = INT64_MIN;
1075 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
1076 CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
1086 if (!
ctx->frame_queue) {
1091 if (
ctx->cudecoder) {
1092 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
1096 if (
ctx->cuparser) {
1097 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
1105 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1106 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1108 if (seq_pkt.payload && seq_pkt.payload_size) {
1118 ctx->prev_pts = INT64_MIN;
1119 ctx->decoder_flushing = 0;
1126 #define OFFSET(x) offsetof(CuvidContext, x)
1127 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1129 {
"deint",
"Set deinterlacing mode",
OFFSET(deint_mode),
AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive,
VD,
"deint" },
1130 {
"weave",
"Weave deinterlacing (do nothing)", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0,
VD,
"deint" },
1131 {
"bob",
"Bob deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0,
VD,
"deint" },
1132 {
"adaptive",
"Adaptive deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0,
VD,
"deint" },
1134 {
"surfaces",
"Maximum surfaces to be used for decoding",
OFFSET(nb_surfaces),
AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX,
VD },
1135 {
"drop_second_field",
"Drop second field when deinterlacing",
OFFSET(drop_second_field),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VD },
1154 #define DEFINE_CUVID_CODEC(x, X) \
1155 static const AVClass x##_cuvid_class = { \
1156 .class_name = #x "_cuvid", \
1157 .item_name = av_default_item_name, \
1158 .option = options, \
1159 .version = LIBAVUTIL_VERSION_INT, \
1161 AVCodec ff_##x##_cuvid_decoder = { \
1162 .name = #x "_cuvid", \
1163 .long_name = NULL_IF_CONFIG_SMALL("Nvidia CUVID " #X " decoder"), \
1164 .type = AVMEDIA_TYPE_VIDEO, \
1165 .id = AV_CODEC_ID_##X, \
1166 .priv_data_size = sizeof(CuvidContext), \
1167 .priv_class = &x##_cuvid_class, \
1168 .init = cuvid_decode_init, \
1169 .close = cuvid_decode_end, \
1170 .decode = cuvid_decode_frame, \
1171 .receive_frame = cuvid_output_frame, \
1172 .flush = cuvid_flush, \
1173 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
1174 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, \
1178 AV_PIX_FMT_NONE }, \
1179 .hw_configs = cuvid_hw_configs, \
1180 .wrapper_name = "cuvid", \
1183 #if CONFIG_HEVC_CUVID_DECODER
1187 #if CONFIG_H264_CUVID_DECODER
1191 #if CONFIG_MJPEG_CUVID_DECODER
1195 #if CONFIG_MPEG1_CUVID_DECODER
1199 #if CONFIG_MPEG2_CUVID_DECODER
1203 #if CONFIG_MPEG4_CUVID_DECODER
1207 #if CONFIG_VP8_CUVID_DECODER
1211 #if CONFIG_VP9_CUVID_DECODER
1215 #if CONFIG_VC1_CUVID_DECODER