1 /*
2 * Nvidia CUVID decoder
3 * Copyright (c) 2016 Timo Rothenpieler <timo@rothenpieler.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config_components.h"
23
25
36
44
45 #if !NVDECAPI_CHECK_VERSION(9, 0)
46 #define cudaVideoSurfaceFormat_YUV444 2
47 #define cudaVideoSurfaceFormat_YUV444_16Bit 3
48 #endif
49
50 #if NVDECAPI_CHECK_VERSION(11, 0)
51 #define CUVID_HAS_AV1_SUPPORT
52 #endif
53
55 {
57
60
61 /* This packet coincides with AVCodecInternal.in_pkt
62 * and is not owned by us. */
64
70
71 struct {
77
78 struct {
82
85
87
92
95
97
100
102
105
109
111 {
116
117 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x)
118
119 // NV recommends [2;4] range
120 #define CUVID_MAX_DISPLAY_DELAY (4)
121
122 // Actual pool size will be determined by parser.
123 #define CUVID_DEFAULT_NUM_SURFACES (CUVID_MAX_DISPLAY_DELAY + 1)
124
126 {
130 CUVIDDECODECAPS *caps =
NULL;
131 CUVIDDECODECREATEINFO cuinfo;
132 int surface_fmt;
133 int chroma_444;
134 int old_nb_surfaces, fifo_size_inc, fifo_size_mul = 1;
135
136 int old_width = avctx->
width;
137 int old_height = avctx->
height;
138
142
144
145 memset(&cuinfo, 0, sizeof(cuinfo));
146
147 ctx->internal_error = 0;
148
149 avctx->coded_width = cuinfo.ulWidth =
format->coded_width;
150 avctx->coded_height = cuinfo.ulHeight =
format->coded_height;
151
152 // apply cropping
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;
157
158 // width and height need to be set before calling ff_get_format
159 if (
ctx->resize_expr) {
160 avctx->width =
ctx->resize.width;
161 avctx->height =
ctx->resize.height;
162 } else {
163 avctx->width = cuinfo.display_area.right - cuinfo.display_area.left;
164 avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top;
165 }
166
167 // target width/height need to be multiples of two
168 cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1;
169 cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1;
170
171 // aspect ratio conversion, 1:1, depends on scaled resolution
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;
176
177 chroma_444 =
format->chroma_format == cudaVideoChromaFormat_444;
178
179 switch (
format->bit_depth_luma_minus8) {
180 case 0: // 8-bit
181 if (chroma_444) {
183 #ifdef NVDEC_HAVE_422_SUPPORT
184 }
else if (
format->chroma_format == cudaVideoChromaFormat_422) {
186 #endif
187 } else {
189 }
191 break;
192 case 2: // 10-bit
193 if (chroma_444) {
194 #if FF_API_NVDEC_OLD_PIX_FMTS
196 #else
198 #endif
199 #ifdef NVDEC_HAVE_422_SUPPORT
200 }
else if (
format->chroma_format == cudaVideoChromaFormat_422) {
201 #if FF_API_NVDEC_OLD_PIX_FMTS
203 #else
205 #endif
206 #endif
207 } else {
209 }
211 break;
212 case 4: // 12-bit
213 if (chroma_444) {
214 #if FF_API_NVDEC_OLD_PIX_FMTS
216 #else
218 #endif
219 #ifdef NVDEC_HAVE_422_SUPPORT
220 }
else if (
format->chroma_format == cudaVideoChromaFormat_422) {
221 #if FF_API_NVDEC_OLD_PIX_FMTS
223 #else
225 #endif
226 #endif
227 } else {
228 #if FF_API_NVDEC_OLD_PIX_FMTS
230 #else
232 #endif
233 }
235 break;
236 default:
237 break;
238 }
239
240 if (!caps || !caps->bIsSupported) {
242 format->bit_depth_luma_minus8 + 8);
244 return 0;
245 }
246
248 if (surface_fmt < 0) {
251 return 0;
252 }
253
258
259 avctx->pix_fmt = surface_fmt;
260
261 // Update our hwframe ctx, as the get_format callback might have refreshed it!
262 if (avctx->hw_frames_ctx) {
264
268 return 0;
269 }
270
272 }
273
276 (
AVRational){ avctx->width, avctx->height }));
277
278 ctx->deint_mode_current =
format->progressive_sequence
279 ? cudaVideoDeinterlaceMode_Weave
281
282 ctx->progressive_sequence =
format->progressive_sequence;
283
284 if (!
format->progressive_sequence &&
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
286 else
288
289 if (
format->video_signal_description.video_full_range_flag)
291 else
293
294 avctx->color_primaries =
format->video_signal_description.color_primaries;
295 avctx->color_trc =
format->video_signal_description.transfer_characteristics;
296 avctx->colorspace =
format->video_signal_description.matrix_coefficients;
297
299 avctx->bit_rate =
format->bitrate;
300
301 if (
format->frame_rate.numerator &&
format->frame_rate.denominator) {
302 avctx->framerate.num =
format->frame_rate.numerator;
303 avctx->framerate.den =
format->frame_rate.denominator;
304 }
305
307 && avctx->coded_width ==
format->coded_width
308 && avctx->coded_height ==
format->coded_height
309 && avctx->width == old_width
310 && avctx->height == old_height
311 &&
ctx->chroma_format ==
format->chroma_format
313 return 1;
314
315 if (
ctx->cudecoder) {
318 if (
ctx->internal_error < 0)
319 return 0;
321 }
322
323 if (hwframe_ctx->pool && (
324 hwframe_ctx->width < avctx->width ||
325 hwframe_ctx->height < avctx->height ||
327 hwframe_ctx->sw_format != avctx->sw_pix_fmt)) {
328 av_log(avctx,
AV_LOG_ERROR,
"AVHWFramesContext is already initialized with incompatible parameters\n");
330 av_log(avctx,
AV_LOG_DEBUG,
"height: %d <-> %d\n", hwframe_ctx->height, avctx->height);
335 return 0;
336 }
337
338 ctx->chroma_format =
format->chroma_format;
339
340 cuinfo.CodecType =
ctx->codec_type =
format->codec;
341 cuinfo.ChromaFormat =
format->chroma_format;
342
343 switch (avctx->sw_pix_fmt) {
345 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
346 break;
349 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
350 break;
351 #ifdef NVDEC_HAVE_422_SUPPORT
353 cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV16;
354 break;
356 cuinfo.OutputFormat = cudaVideoSurfaceFormat_P216;
357 break;
358 #endif
361 break;
364 break;
365 default:
369 return 0;
370 }
371
372 if (
ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field) {
374 fifo_size_mul = 2;
375 }
376
377 old_nb_surfaces =
ctx->nb_surfaces;
379 if (avctx->extra_hw_frames > 0)
380 ctx->nb_surfaces += avctx->extra_hw_frames;
381
383 if (fifo_size_inc > 0 &&
av_fifo_grow2(
ctx->frame_queue, fifo_size_inc) < 0) {
384 av_log(avctx,
AV_LOG_ERROR,
"Failed to grow frame queue on video sequence callback\n");
386 return 0;
387 }
388
390 av_log(avctx,
AV_LOG_ERROR,
"Failed to grow key frame array on video sequence callback\n");
392 return 0;
393 }
394
395 cuinfo.ulNumDecodeSurfaces =
ctx->nb_surfaces;
396 cuinfo.ulNumOutputSurfaces = 1;
397 cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
398 cuinfo.bitDepthMinus8 =
format->bit_depth_luma_minus8;
399 cuinfo.DeinterlaceMode =
ctx->deint_mode_current;
400
401 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidCreateDecoder(&
ctx->cudecoder, &cuinfo));
402 if (
ctx->internal_error < 0)
403 return 0;
404
405 if (!hwframe_ctx->pool) {
407 hwframe_ctx->sw_format = avctx->sw_pix_fmt;
408 hwframe_ctx->width = avctx->width;
409 hwframe_ctx->height = avctx->height;
410
413 return 0;
414 }
415 }
416
417 if(
ctx->cuparseinfo.ulMaxNumDecodeSurfaces != cuinfo.ulNumDecodeSurfaces) {
418 ctx->cuparseinfo.ulMaxNumDecodeSurfaces = cuinfo.ulNumDecodeSurfaces;
419 return cuinfo.ulNumDecodeSurfaces;
420 }
421
422 return 1;
423 }
424
426 {
429
431
432 if(picparams->intra_pic_flag)
433 ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
434
435 ctx->internal_error =
CHECK_CU(
ctx->cvdl->cuvidDecodePicture(
ctx->cudecoder, picparams));
436 if (
ctx->internal_error < 0)
437 return 0;
438
439 return 1;
440 }
441
443 {
448
450 ctx->internal_error = 0;
451
452 // For some reason, dispinfo->progressive_frame is sometimes wrong.
453 parsed_frame.
dispinfo.progressive_frame =
ctx->progressive_sequence;
454
455 if (
ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
459 } else {
464
465 if (!
ctx->drop_second_field) {
470 }
471 }
472
473 return 1;
474 }
475
477 {
479
481 if (
ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !
ctx->drop_second_field)
483
484 // shift/divide frame count to ensure the buffer is still signalled full if one half-frame has already been returned when deinterlacing.
486 }
487
489 {
493 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
494 CUVIDSOURCEDATAPACKET cupkt;
495 int ret = 0, eret = 0, is_flush =
ctx->decoder_flushing;
496
498
499 if (is_flush && avpkt && avpkt->
size)
501
504
508 }
509
510 memset(&cupkt, 0, sizeof(cupkt));
511
512 if (avpkt && avpkt->
size) {
513 cupkt.payload_size = avpkt->
size;
514 cupkt.payload = avpkt->
data;
515
517 cupkt.flags = CUVID_PKT_TIMESTAMP;
520 else
521 cupkt.timestamp = avpkt->
pts;
522 }
523 } else {
524 cupkt.flags = CUVID_PKT_ENDOFSTREAM;
525 ctx->decoder_flushing = 1;
526 }
527
528 // When flushing, only actually flush cuvid when the output buffer has been fully emptied.
529 // CUVID happily dumps out a ton of frames with no regard for its own available surfaces.
532 else
534
537
538 // cuvidParseVideoData doesn't return an error just because stuff failed...
539 if (
ctx->internal_error) {
541 ret =
ctx->internal_error;
543 }
544
547
548 if (eret < 0)
549 return eret;
552 else if (is_flush)
554 else
555 return 0;
556 }
557
559 {
563 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
565 CUdeviceptr mapped_frame = 0;
566 int ret = 0, eret = 0;
567
569
570 if (
ctx->decoder_flushing) {
574 }
575
583 // cuvid_is_buffer_full() should avoid this.
588 }
589
593
596 CUVIDPROCPARAMS params;
597 unsigned int pitch = 0;
600
601 memset(¶ms, 0, sizeof(params));
602 params.progressive_frame = parsed_frame.dispinfo.progressive_frame;
603 params.second_field = parsed_frame.second_field;
604 params.top_field_first = parsed_frame.dispinfo.top_field_first;
605
606 ret =
CHECK_CU(
ctx->cvdl->cuvidMapVideoFrame(
ctx->cudecoder, parsed_frame.dispinfo.picture_index, &mapped_frame, &pitch, ¶ms));
609
615 }
616
621 }
622
624
627 CUDA_MEMCPY2D cpy = {
628 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
629 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
630 .srcDevice = mapped_frame,
631 .dstDevice = (CUdeviceptr)
frame->data[
i],
632 .srcPitch = pitch,
633 .dstPitch =
frame->linesize[
i],
637 };
638
639 ret =
CHECK_CU(
ctx->cudl->cuMemcpy2DAsync(&cpy, device_hwctx->stream));
642
644 }
648 #ifdef NVDEC_HAVE_422_SUPPORT
651 #endif
656 if (!tmp_frame) {
660 }
661
663
670 }
671
674
675 /*
676 * Note that the following logic would not work for three plane
677 * YUV420 because the pitch value is different for the chroma
678 * planes.
679 */
681 tmp_frame->
data[
i] = (uint8_t*)mapped_frame +
offset;
684 }
685
691 }
692
698 }
700 } else {
703 }
704
705 if (
ctx->key_frame[parsed_frame.dispinfo.picture_index])
707 else
709 ctx->key_frame[parsed_frame.dispinfo.picture_index] = 0;
710
715 else
716 frame->pts = parsed_frame.dispinfo.timestamp;
717
718 if (parsed_frame.second_field) {
719 if (
ctx->prev_pts == INT64_MIN) {
722 } else {
723 int pts_diff = (
frame->pts -
ctx->prev_pts) / 2;
725 frame->pts += pts_diff;
726 }
727 }
728
729 /* CUVIDs opaque reordering breaks the internal pkt logic.
730 * So set pkt_pts and clear all the other pkt_ fields.
731 */
733
734 if (!parsed_frame.is_deinterlacing && !parsed_frame.dispinfo.progressive_frame)
736
739 }
else if (
ctx->decoder_flushing) {
741 } else {
743 }
744
748
749 if (mapped_frame)
750 eret =
CHECK_CU(
ctx->cvdl->cuvidUnmapVideoFrame(
ctx->cudecoder, mapped_frame));
751
753
754 if (eret < 0)
755 return eret;
756 else
758 }
759
761 {
765 CUcontext
dummy, cuda_ctx = device_hwctx ? device_hwctx->cuda_ctx :
NULL;
766
768
769 if (cuda_ctx) {
770 ctx->cudl->cuCtxPushCurrent(cuda_ctx);
771
773 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
774
776 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
777
779 }
780
782
785
788
789 cuvid_free_functions(&
ctx->cvdl);
790
791 return 0;
792 }
793
795 const CUVIDPARSERPARAMS *cuparseinfo,
796 int probed_width,
797 int probed_height,
798 int bit_depth,
int is_yuv422,
int is_yuv444)
799 {
801 CUVIDDECODECAPS *caps;
802 int res8 = 0, res10 = 0, res12 = 0;
803
804 if (!
ctx->cvdl->cuvidGetDecoderCaps) {
805 av_log(avctx,
AV_LOG_WARNING,
"Used Nvidia driver is too old to perform a capability check.\n");
807 #if defined(_WIN32) || defined(__CYGWIN__)
808 "378.66"
809 #else
810 "378.13"
811 #endif
812 ". Continuing blind.\n");
813 ctx->caps8.bIsSupported =
ctx->caps10.bIsSupported = 1;
814 // 12 bit was not supported before the capability check was introduced, so disable it.
815 ctx->caps12.bIsSupported = 0;
816 return 0;
817 }
818
819 ctx->caps8.eCodecType =
ctx->caps10.eCodecType =
ctx->caps12.eCodecType
820 = cuparseinfo->CodecType;
821
822 ctx->caps8.eChromaFormat =
ctx->caps10.eChromaFormat =
ctx->caps12.eChromaFormat
823 = is_yuv444 ? cudaVideoChromaFormat_444 :
824 #ifdef NVDEC_HAVE_422_SUPPORT
825 (is_yuv422 ? cudaVideoChromaFormat_422 : cudaVideoChromaFormat_420);
826 #else
827 cudaVideoChromaFormat_420;
828 #endif
829
830 ctx->caps8.nBitDepthMinus8 = 0;
831 ctx->caps10.nBitDepthMinus8 = 2;
832 ctx->caps12.nBitDepthMinus8 = 4;
833
837
839 av_log(avctx,
AV_LOG_VERBOSE,
"8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
840 ctx->caps8.bIsSupported,
ctx->caps8.nMinWidth,
ctx->caps8.nMaxWidth,
ctx->caps8.nMinHeight,
ctx->caps8.nMaxHeight);
841 av_log(avctx,
AV_LOG_VERBOSE,
"10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
842 ctx->caps10.bIsSupported,
ctx->caps10.nMinWidth,
ctx->caps10.nMaxWidth,
ctx->caps10.nMinHeight,
ctx->caps10.nMaxHeight);
843 av_log(avctx,
AV_LOG_VERBOSE,
"12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
844 ctx->caps12.bIsSupported,
ctx->caps12.nMinWidth,
ctx->caps12.nMaxWidth,
ctx->caps12.nMinHeight,
ctx->caps12.nMaxHeight);
845
847 case 10:
849 if (res10 < 0)
850 return res10;
851 break;
852 case 12:
854 if (res12 < 0)
855 return res12;
856 break;
857 default:
859 if (res8 < 0)
860 return res8;
861 }
862
863 if (!
ctx->caps8.bIsSupported) {
866 }
867
868 if (!caps->bIsSupported) {
871 }
872
873 if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
875 probed_width, caps->nMinWidth, caps->nMaxWidth);
877 }
878
879 if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
881 probed_height, caps->nMinHeight, caps->nMaxHeight);
883 }
884
885 if ((probed_width * probed_height) / 256 > caps->nMaxMBCount) {
887 (int)(probed_width * probed_height) / 256, caps->nMaxMBCount);
889 }
890
891 return 0;
892 }
893
895 {
900 CUVIDSOURCEDATAPACKET seq_pkt;
901 CUcontext cuda_ctx =
NULL;
903 uint8_t *extradata;
904 int extradata_size;
906
910
913 int probed_bit_depth = 8, is_yuv444 = 0, is_yuv422 = 0;
914
917 probed_bit_depth = probe_desc->
comp[0].
depth;
918
920 is_yuv444 = 1;
921
922 #ifdef NVDEC_HAVE_422_SUPPORT
924 is_yuv422 = 1;
925 #endif
926
927 // Pick pixel format based on bit depth and chroma sampling.
928 switch (probed_bit_depth) {
929 case 10:
930 #if FF_API_NVDEC_OLD_PIX_FMTS
932 #else
934 #endif
935 break;
936 case 12:
937 #if FF_API_NVDEC_OLD_PIX_FMTS
939 #else
941 #endif
942 break;
943 default:
945 break;
946 }
947
949 // Accelerated transcoding scenarios with 'ffmpeg' require that the
950 // pix_fmt be set to AV_PIX_FMT_CUDA early. The sw_pix_fmt, and the
951 // pix_fmt for non-accelerated transcoding, do not need to be correct
952 // but need to be set to something.
957 }
959
960 if (
ctx->resize_expr && sscanf(
ctx->resize_expr,
"%dx%d",
961 &
ctx->resize.width, &
ctx->resize.height) != 2) {
965 }
966
967 if (
ctx->crop_expr && sscanf(
ctx->crop_expr,
"%dx%dx%dx%d",
968 &
ctx->crop.top, &
ctx->crop.bottom,
969 &
ctx->crop.left, &
ctx->crop.right) != 4) {
973 }
974
975 ret = cuvid_load_functions(&
ctx->cvdl, avctx);
979 }
980
981 // respect the deprecated "surfaces" option if non-default value is given by user;
982 if(
ctx->nb_surfaces < 0)
984
986 if (!
ctx->frame_queue) {
989 }
990
996 }
997
999
1001 if (!
ctx->hwdevice) {
1004 }
1005 } else {
1008 if (!
ctx->hwdevice) {
1011 }
1012 } else {
1016 }
1017
1019 if (!
ctx->hwframe) {
1023 }
1024
1026 }
1027
1029 device_hwctx = device_ctx->
hwctx;
1030
1033
1034 memset(&
ctx->cuparseinfo, 0,
sizeof(
ctx->cuparseinfo));
1035 memset(&seq_pkt, 0, sizeof(seq_pkt));
1036
1038 #if CONFIG_H264_CUVID_DECODER
1040 ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
1041 break;
1042 #endif
1043 #if CONFIG_HEVC_CUVID_DECODER
1045 ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
1046 break;
1047 #endif
1048 #if CONFIG_MJPEG_CUVID_DECODER
1050 ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
1051 break;
1052 #endif
1053 #if CONFIG_MPEG1_CUVID_DECODER
1055 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
1056 break;
1057 #endif
1058 #if CONFIG_MPEG2_CUVID_DECODER
1060 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
1061 break;
1062 #endif
1063 #if CONFIG_MPEG4_CUVID_DECODER
1065 ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
1066 break;
1067 #endif
1068 #if CONFIG_VP8_CUVID_DECODER
1070 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
1071 break;
1072 #endif
1073 #if CONFIG_VP9_CUVID_DECODER
1075 ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
1076 break;
1077 #endif
1078 #if CONFIG_VC1_CUVID_DECODER
1080 ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
1081 break;
1082 #endif
1083 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1085 ctx->cuparseinfo.CodecType = cudaVideoCodec_AV1;
1086 break;
1087 #endif
1088 default:
1091 }
1092
1097 } else {
1100 }
1101
1102 // Check first bit to determine whether it's AV1CodecConfigurationRecord.
1103 // Skip first 4 bytes of AV1CodecConfigurationRecord to keep configOBUs
1104 // only, otherwise cuvidParseVideoData report unknown error.
1106 extradata_size > 4 &&
1107 extradata[0] & 0x80) {
1108 extradata += 4;
1109 extradata_size -= 4;
1110 }
1111
1113 +
FFMAX(extradata_size - (
int)
sizeof(
ctx->cuparse_ext->raw_seqhdr_data), 0));
1114 if (!
ctx->cuparse_ext) {
1117 }
1118
1119 if (extradata_size > 0)
1120 memcpy(
ctx->cuparse_ext->raw_seqhdr_data, extradata, extradata_size);
1121 ctx->cuparse_ext->format.seqhdr_data_length = extradata_size;
1122
1123 ctx->cuparseinfo.pExtVideoInfo =
ctx->cuparse_ext;
1124
1126 if (!
ctx->key_frame) {
1129 }
1130
1131 ctx->cuparseinfo.ulMaxNumDecodeSurfaces = 1;
1133 ctx->cuparseinfo.pUserData = avctx;
1137
1141
1143 probed_width,
1144 probed_height,
1145 probed_bit_depth, is_yuv422, is_yuv444);
1148
1152
1153 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1154 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1155
1156 if (seq_pkt.payload && seq_pkt.payload_size) {
1160 }
1161
1165
1166 ctx->prev_pts = INT64_MIN;
1167
1170
1171 return 0;
1172
1176 }
1177
1179 {
1183 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
1184 CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
1186
1190
1192
1193 if (
ctx->cudecoder) {
1194 ctx->cvdl->cuvidDestroyDecoder(
ctx->cudecoder);
1196 }
1197
1198 if (
ctx->cuparser) {
1199 ctx->cvdl->cuvidDestroyVideoParser(
ctx->cuparser);
1201 }
1202
1206
1207 seq_pkt.payload =
ctx->cuparse_ext->raw_seqhdr_data;
1208 seq_pkt.payload_size =
ctx->cuparse_ext->format.seqhdr_data_length;
1209
1210 if (seq_pkt.payload && seq_pkt.payload_size) {
1214 }
1215
1219
1220 ctx->prev_pts = INT64_MIN;
1221 ctx->decoder_flushing = 0;
1222
1223 return;
1226 }
1227
1228 #define OFFSET(x) offsetof(CuvidContext, x)
1229 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1231 {
"deint",
"Set deinterlacing mode",
OFFSET(deint_mode),
AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive,
VD, .unit =
"deint" },
1232 {
"weave",
"Weave deinterlacing (do nothing)", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0,
VD, .unit =
"deint" },
1233 {
"bob",
"Bob deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0,
VD, .unit =
"deint" },
1234 {
"adaptive",
"Adaptive deinterlacing", 0,
AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0,
VD, .unit =
"deint" },
1237 {
"drop_second_field",
"Drop second field when deinterlacing",
OFFSET(drop_second_field),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VD },
1241 };
1242
1251 },
1253 },
1255 };
1256
1257 #define DEFINE_CUVID_CODEC(x, X, bsf_name) \
1258 static const AVClass x##_cuvid_class = { \
1259 .class_name = #x "_cuvid", \
1260 .item_name = av_default_item_name, \
1261 .option = options, \
1262 .version = LIBAVUTIL_VERSION_INT, \
1263 }; \
1264 const FFCodec ff_##x##_cuvid_decoder = { \
1265 .p.name = #x "_cuvid", \
1266 CODEC_LONG_NAME("Nvidia CUVID " #X " decoder"), \
1267 .p.type = AVMEDIA_TYPE_VIDEO, \
1268 .p.id = AV_CODEC_ID_##X, \
1269 .priv_data_size = sizeof(CuvidContext), \
1270 .p.priv_class = &x##_cuvid_class, \
1271 .init = cuvid_decode_init, \
1272 .close = cuvid_decode_end, \
1273 FF_CODEC_RECEIVE_FRAME_CB(cuvid_output_frame), \
1274 .flush = cuvid_flush, \
1275 .bsfs = bsf_name, \
1276 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
1277 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | \
1278 FF_CODEC_CAP_SETS_FRAME_PROPS, \
1279 .hw_configs = cuvid_hw_configs, \
1280 .p.wrapper_name = "cuvid", \
1281 };
1282
1283 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1285 #endif
1286
1287 #if CONFIG_HEVC_CUVID_DECODER
1289 #endif
1290
1291 #if CONFIG_H264_CUVID_DECODER
1293 #endif
1294
1295 #if CONFIG_MJPEG_CUVID_DECODER
1297 #endif
1298
1299 #if CONFIG_MPEG1_CUVID_DECODER
1301 #endif
1302
1303 #if CONFIG_MPEG2_CUVID_DECODER
1305 #endif
1306
1307 #if CONFIG_MPEG4_CUVID_DECODER
1309 #endif
1310
1311 #if CONFIG_VP8_CUVID_DECODER
1313 #endif
1314
1315 #if CONFIG_VP9_CUVID_DECODER
1317 #endif
1318
1319 #if CONFIG_VC1_CUVID_DECODER
1321 #endif