You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1203 lines
40KB

  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. #include "compat/cuda/dynlink_loader.h"
  22. #include "libavutil/buffer.h"
  23. #include "libavutil/mathematics.h"
  24. #include "libavutil/hwcontext.h"
  25. #include "libavutil/hwcontext_cuda_internal.h"
  26. #include "libavutil/cuda_check.h"
  27. #include "libavutil/fifo.h"
  28. #include "libavutil/log.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avcodec.h"
  32. #include "decode.h"
  33. #include "hwaccel.h"
  34. #include "nvdec.h"
  35. #include "internal.h"
  36. #if !NVDECAPI_CHECK_VERSION(9, 0)
  37. #define cudaVideoSurfaceFormat_YUV444 2
  38. #define cudaVideoSurfaceFormat_YUV444_16Bit 3
  39. #endif
  40. typedef struct CuvidContext
  41. {
  42. AVClass *avclass;
  43. CUvideodecoder cudecoder;
  44. CUvideoparser cuparser;
  45. char *cu_gpu;
  46. int nb_surfaces;
  47. int drop_second_field;
  48. char *crop_expr;
  49. char *resize_expr;
  50. struct {
  51. int left;
  52. int top;
  53. int right;
  54. int bottom;
  55. } crop;
  56. struct {
  57. int width;
  58. int height;
  59. } resize;
  60. AVBufferRef *hwdevice;
  61. AVBufferRef *hwframe;
  62. AVBSFContext *bsf;
  63. AVFifoBuffer *frame_queue;
  64. int deint_mode;
  65. int deint_mode_current;
  66. int64_t prev_pts;
  67. int progressive_sequence;
  68. int internal_error;
  69. int decoder_flushing;
  70. int *key_frame;
  71. cudaVideoCodec codec_type;
  72. cudaVideoChromaFormat chroma_format;
  73. CUVIDDECODECAPS caps8, caps10, caps12;
  74. CUVIDPARSERPARAMS cuparseinfo;
  75. CUVIDEOFORMATEX cuparse_ext;
  76. CudaFunctions *cudl;
  77. CuvidFunctions *cvdl;
  78. } CuvidContext;
  79. typedef struct CuvidParsedFrame
  80. {
  81. CUVIDPARSERDISPINFO dispinfo;
  82. int second_field;
  83. int is_deinterlacing;
  84. } CuvidParsedFrame;
  85. #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x)
  86. static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* format)
  87. {
  88. AVCodecContext *avctx = opaque;
  89. CuvidContext *ctx = avctx->priv_data;
  90. AVHWFramesContext *hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
  91. CUVIDDECODECAPS *caps = NULL;
  92. CUVIDDECODECREATEINFO cuinfo;
  93. int surface_fmt;
  94. int chroma_444;
  95. int old_width = avctx->width;
  96. int old_height = avctx->height;
  97. enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_CUDA,
  98. AV_PIX_FMT_NONE, // Will be updated below
  99. AV_PIX_FMT_NONE };
  100. av_log(avctx, AV_LOG_TRACE, "pfnSequenceCallback, progressive_sequence=%d\n", format->progressive_sequence);
  101. memset(&cuinfo, 0, sizeof(cuinfo));
  102. ctx->internal_error = 0;
  103. avctx->coded_width = cuinfo.ulWidth = format->coded_width;
  104. avctx->coded_height = cuinfo.ulHeight = format->coded_height;
  105. // apply cropping
  106. cuinfo.display_area.left = format->display_area.left + ctx->crop.left;
  107. cuinfo.display_area.top = format->display_area.top + ctx->crop.top;
  108. cuinfo.display_area.right = format->display_area.right - ctx->crop.right;
  109. cuinfo.display_area.bottom = format->display_area.bottom - ctx->crop.bottom;
  110. // width and height need to be set before calling ff_get_format
  111. if (ctx->resize_expr) {
  112. avctx->width = ctx->resize.width;
  113. avctx->height = ctx->resize.height;
  114. } else {
  115. avctx->width = cuinfo.display_area.right - cuinfo.display_area.left;
  116. avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top;
  117. }
  118. // target width/height need to be multiples of two
  119. cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1;
  120. cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1;
  121. // aspect ratio conversion, 1:1, depends on scaled resolution
  122. cuinfo.target_rect.left = 0;
  123. cuinfo.target_rect.top = 0;
  124. cuinfo.target_rect.right = cuinfo.ulTargetWidth;
  125. cuinfo.target_rect.bottom = cuinfo.ulTargetHeight;
  126. chroma_444 = format->chroma_format == cudaVideoChromaFormat_444;
  127. switch (format->bit_depth_luma_minus8) {
  128. case 0: // 8-bit
  129. pix_fmts[1] = chroma_444 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_NV12;
  130. caps = &ctx->caps8;
  131. break;
  132. case 2: // 10-bit
  133. pix_fmts[1] = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P010;
  134. caps = &ctx->caps10;
  135. break;
  136. case 4: // 12-bit
  137. pix_fmts[1] = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P016;
  138. caps = &ctx->caps12;
  139. break;
  140. default:
  141. break;
  142. }
  143. if (!caps || !caps->bIsSupported) {
  144. av_log(avctx, AV_LOG_ERROR, "unsupported bit depth: %d\n",
  145. format->bit_depth_luma_minus8 + 8);
  146. ctx->internal_error = AVERROR(EINVAL);
  147. return 0;
  148. }
  149. surface_fmt = ff_get_format(avctx, pix_fmts);
  150. if (surface_fmt < 0) {
  151. av_log(avctx, AV_LOG_ERROR, "ff_get_format failed: %d\n", surface_fmt);
  152. ctx->internal_error = AVERROR(EINVAL);
  153. return 0;
  154. }
  155. av_log(avctx, AV_LOG_VERBOSE, "Formats: Original: %s | HW: %s | SW: %s\n",
  156. av_get_pix_fmt_name(avctx->pix_fmt),
  157. av_get_pix_fmt_name(surface_fmt),
  158. av_get_pix_fmt_name(avctx->sw_pix_fmt));
  159. avctx->pix_fmt = surface_fmt;
  160. // Update our hwframe ctx, as the get_format callback might have refreshed it!
  161. if (avctx->hw_frames_ctx) {
  162. av_buffer_unref(&ctx->hwframe);
  163. ctx->hwframe = av_buffer_ref(avctx->hw_frames_ctx);
  164. if (!ctx->hwframe) {
  165. ctx->internal_error = AVERROR(ENOMEM);
  166. return 0;
  167. }
  168. hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
  169. }
  170. ff_set_sar(avctx, av_div_q(
  171. (AVRational){ format->display_aspect_ratio.x, format->display_aspect_ratio.y },
  172. (AVRational){ avctx->width, avctx->height }));
  173. ctx->deint_mode_current = format->progressive_sequence
  174. ? cudaVideoDeinterlaceMode_Weave
  175. : ctx->deint_mode;
  176. ctx->progressive_sequence = format->progressive_sequence;
  177. if (!format->progressive_sequence && ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
  178. avctx->flags |= AV_CODEC_FLAG_INTERLACED_DCT;
  179. else
  180. avctx->flags &= ~AV_CODEC_FLAG_INTERLACED_DCT;
  181. if (format->video_signal_description.video_full_range_flag)
  182. avctx->color_range = AVCOL_RANGE_JPEG;
  183. else
  184. avctx->color_range = AVCOL_RANGE_MPEG;
  185. avctx->color_primaries = format->video_signal_description.color_primaries;
  186. avctx->color_trc = format->video_signal_description.transfer_characteristics;
  187. avctx->colorspace = format->video_signal_description.matrix_coefficients;
  188. if (format->bitrate)
  189. avctx->bit_rate = format->bitrate;
  190. if (format->frame_rate.numerator && format->frame_rate.denominator) {
  191. avctx->framerate.num = format->frame_rate.numerator;
  192. avctx->framerate.den = format->frame_rate.denominator;
  193. }
  194. if (ctx->cudecoder
  195. && avctx->coded_width == format->coded_width
  196. && avctx->coded_height == format->coded_height
  197. && avctx->width == old_width
  198. && avctx->height == old_height
  199. && ctx->chroma_format == format->chroma_format
  200. && ctx->codec_type == format->codec)
  201. return 1;
  202. if (ctx->cudecoder) {
  203. av_log(avctx, AV_LOG_TRACE, "Re-initializing decoder\n");
  204. ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder));
  205. if (ctx->internal_error < 0)
  206. return 0;
  207. ctx->cudecoder = NULL;
  208. }
  209. if (hwframe_ctx->pool && (
  210. hwframe_ctx->width < avctx->width ||
  211. hwframe_ctx->height < avctx->height ||
  212. hwframe_ctx->format != AV_PIX_FMT_CUDA ||
  213. hwframe_ctx->sw_format != avctx->sw_pix_fmt)) {
  214. av_log(avctx, AV_LOG_ERROR, "AVHWFramesContext is already initialized with incompatible parameters\n");
  215. av_log(avctx, AV_LOG_DEBUG, "width: %d <-> %d\n", hwframe_ctx->width, avctx->width);
  216. av_log(avctx, AV_LOG_DEBUG, "height: %d <-> %d\n", hwframe_ctx->height, avctx->height);
  217. av_log(avctx, AV_LOG_DEBUG, "format: %s <-> cuda\n", av_get_pix_fmt_name(hwframe_ctx->format));
  218. av_log(avctx, AV_LOG_DEBUG, "sw_format: %s <-> %s\n",
  219. av_get_pix_fmt_name(hwframe_ctx->sw_format), av_get_pix_fmt_name(avctx->sw_pix_fmt));
  220. ctx->internal_error = AVERROR(EINVAL);
  221. return 0;
  222. }
  223. ctx->chroma_format = format->chroma_format;
  224. cuinfo.CodecType = ctx->codec_type = format->codec;
  225. cuinfo.ChromaFormat = format->chroma_format;
  226. switch (avctx->sw_pix_fmt) {
  227. case AV_PIX_FMT_NV12:
  228. cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
  229. break;
  230. case AV_PIX_FMT_P010:
  231. case AV_PIX_FMT_P016:
  232. cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
  233. break;
  234. case AV_PIX_FMT_YUV444P:
  235. cuinfo.OutputFormat = cudaVideoSurfaceFormat_YUV444;
  236. break;
  237. case AV_PIX_FMT_YUV444P16:
  238. cuinfo.OutputFormat = cudaVideoSurfaceFormat_YUV444_16Bit;
  239. break;
  240. default:
  241. av_log(avctx, AV_LOG_ERROR, "Unsupported output format: %s\n",
  242. av_get_pix_fmt_name(avctx->sw_pix_fmt));
  243. ctx->internal_error = AVERROR(EINVAL);
  244. return 0;
  245. }
  246. cuinfo.ulNumDecodeSurfaces = ctx->nb_surfaces;
  247. cuinfo.ulNumOutputSurfaces = 1;
  248. cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
  249. cuinfo.bitDepthMinus8 = format->bit_depth_luma_minus8;
  250. cuinfo.DeinterlaceMode = ctx->deint_mode_current;
  251. if (ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field)
  252. avctx->framerate = av_mul_q(avctx->framerate, (AVRational){2, 1});
  253. ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidCreateDecoder(&ctx->cudecoder, &cuinfo));
  254. if (ctx->internal_error < 0)
  255. return 0;
  256. if (!hwframe_ctx->pool) {
  257. hwframe_ctx->format = AV_PIX_FMT_CUDA;
  258. hwframe_ctx->sw_format = avctx->sw_pix_fmt;
  259. hwframe_ctx->width = avctx->width;
  260. hwframe_ctx->height = avctx->height;
  261. if ((ctx->internal_error = av_hwframe_ctx_init(ctx->hwframe)) < 0) {
  262. av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_init failed\n");
  263. return 0;
  264. }
  265. }
  266. return 1;
  267. }
  268. static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS* picparams)
  269. {
  270. AVCodecContext *avctx = opaque;
  271. CuvidContext *ctx = avctx->priv_data;
  272. av_log(avctx, AV_LOG_TRACE, "pfnDecodePicture\n");
  273. ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
  274. ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidDecodePicture(ctx->cudecoder, picparams));
  275. if (ctx->internal_error < 0)
  276. return 0;
  277. return 1;
  278. }
  279. static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO* dispinfo)
  280. {
  281. AVCodecContext *avctx = opaque;
  282. CuvidContext *ctx = avctx->priv_data;
  283. CuvidParsedFrame parsed_frame = { { 0 } };
  284. parsed_frame.dispinfo = *dispinfo;
  285. ctx->internal_error = 0;
  286. // For some reason, dispinfo->progressive_frame is sometimes wrong.
  287. parsed_frame.dispinfo.progressive_frame = ctx->progressive_sequence;
  288. if (ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
  289. av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
  290. } else {
  291. parsed_frame.is_deinterlacing = 1;
  292. av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
  293. if (!ctx->drop_second_field) {
  294. parsed_frame.second_field = 1;
  295. av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
  296. }
  297. }
  298. return 1;
  299. }
  300. static int cuvid_is_buffer_full(AVCodecContext *avctx)
  301. {
  302. CuvidContext *ctx = avctx->priv_data;
  303. int delay = ctx->cuparseinfo.ulMaxDisplayDelay;
  304. if (ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field)
  305. delay *= 2;
  306. return (av_fifo_size(ctx->frame_queue) / sizeof(CuvidParsedFrame)) + delay >= ctx->nb_surfaces;
  307. }
  308. static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  309. {
  310. CuvidContext *ctx = avctx->priv_data;
  311. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
  312. AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
  313. CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
  314. CUVIDSOURCEDATAPACKET cupkt;
  315. AVPacket filter_packet = { 0 };
  316. AVPacket filtered_packet = { 0 };
  317. int ret = 0, eret = 0, is_flush = ctx->decoder_flushing;
  318. av_log(avctx, AV_LOG_TRACE, "cuvid_decode_packet\n");
  319. if (is_flush && avpkt && avpkt->size)
  320. return AVERROR_EOF;
  321. if (cuvid_is_buffer_full(avctx) && avpkt && avpkt->size)
  322. return AVERROR(EAGAIN);
  323. if (ctx->bsf && avpkt && avpkt->size) {
  324. if ((ret = av_packet_ref(&filter_packet, avpkt)) < 0) {
  325. av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed\n");
  326. return ret;
  327. }
  328. if ((ret = av_bsf_send_packet(ctx->bsf, &filter_packet)) < 0) {
  329. av_log(avctx, AV_LOG_ERROR, "av_bsf_send_packet failed\n");
  330. av_packet_unref(&filter_packet);
  331. return ret;
  332. }
  333. if ((ret = av_bsf_receive_packet(ctx->bsf, &filtered_packet)) < 0) {
  334. av_log(avctx, AV_LOG_ERROR, "av_bsf_receive_packet failed\n");
  335. return ret;
  336. }
  337. avpkt = &filtered_packet;
  338. }
  339. ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
  340. if (ret < 0) {
  341. av_packet_unref(&filtered_packet);
  342. return ret;
  343. }
  344. memset(&cupkt, 0, sizeof(cupkt));
  345. if (avpkt && avpkt->size) {
  346. cupkt.payload_size = avpkt->size;
  347. cupkt.payload = avpkt->data;
  348. if (avpkt->pts != AV_NOPTS_VALUE) {
  349. cupkt.flags = CUVID_PKT_TIMESTAMP;
  350. if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
  351. cupkt.timestamp = av_rescale_q(avpkt->pts, avctx->pkt_timebase, (AVRational){1, 10000000});
  352. else
  353. cupkt.timestamp = avpkt->pts;
  354. }
  355. } else {
  356. cupkt.flags = CUVID_PKT_ENDOFSTREAM;
  357. ctx->decoder_flushing = 1;
  358. }
  359. ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &cupkt));
  360. av_packet_unref(&filtered_packet);
  361. if (ret < 0)
  362. goto error;
  363. // cuvidParseVideoData doesn't return an error just because stuff failed...
  364. if (ctx->internal_error) {
  365. av_log(avctx, AV_LOG_ERROR, "cuvid decode callback error\n");
  366. ret = ctx->internal_error;
  367. goto error;
  368. }
  369. error:
  370. eret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
  371. if (eret < 0)
  372. return eret;
  373. else if (ret < 0)
  374. return ret;
  375. else if (is_flush)
  376. return AVERROR_EOF;
  377. else
  378. return 0;
  379. }
  380. static int cuvid_output_frame(AVCodecContext *avctx, AVFrame *frame)
  381. {
  382. CuvidContext *ctx = avctx->priv_data;
  383. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
  384. AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
  385. CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
  386. CUdeviceptr mapped_frame = 0;
  387. int ret = 0, eret = 0;
  388. av_log(avctx, AV_LOG_TRACE, "cuvid_output_frame\n");
  389. if (ctx->decoder_flushing) {
  390. ret = cuvid_decode_packet(avctx, NULL);
  391. if (ret < 0 && ret != AVERROR_EOF)
  392. return ret;
  393. }
  394. if (!cuvid_is_buffer_full(avctx)) {
  395. AVPacket pkt = {0};
  396. ret = ff_decode_get_packet(avctx, &pkt);
  397. if (ret < 0 && ret != AVERROR_EOF)
  398. return ret;
  399. ret = cuvid_decode_packet(avctx, &pkt);
  400. av_packet_unref(&pkt);
  401. // cuvid_is_buffer_full() should avoid this.
  402. if (ret == AVERROR(EAGAIN))
  403. ret = AVERROR_EXTERNAL;
  404. if (ret < 0 && ret != AVERROR_EOF)
  405. return ret;
  406. }
  407. ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
  408. if (ret < 0)
  409. return ret;
  410. if (av_fifo_size(ctx->frame_queue)) {
  411. const AVPixFmtDescriptor *pixdesc;
  412. CuvidParsedFrame parsed_frame;
  413. CUVIDPROCPARAMS params;
  414. unsigned int pitch = 0;
  415. int offset = 0;
  416. int i;
  417. av_fifo_generic_read(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
  418. memset(&params, 0, sizeof(params));
  419. params.progressive_frame = parsed_frame.dispinfo.progressive_frame;
  420. params.second_field = parsed_frame.second_field;
  421. params.top_field_first = parsed_frame.dispinfo.top_field_first;
  422. ret = CHECK_CU(ctx->cvdl->cuvidMapVideoFrame(ctx->cudecoder, parsed_frame.dispinfo.picture_index, &mapped_frame, &pitch, &params));
  423. if (ret < 0)
  424. goto error;
  425. if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
  426. ret = av_hwframe_get_buffer(ctx->hwframe, frame, 0);
  427. if (ret < 0) {
  428. av_log(avctx, AV_LOG_ERROR, "av_hwframe_get_buffer failed\n");
  429. goto error;
  430. }
  431. ret = ff_decode_frame_props(avctx, frame);
  432. if (ret < 0) {
  433. av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props failed\n");
  434. goto error;
  435. }
  436. pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  437. for (i = 0; i < pixdesc->nb_components; i++) {
  438. int height = avctx->height >> (i ? pixdesc->log2_chroma_h : 0);
  439. CUDA_MEMCPY2D cpy = {
  440. .srcMemoryType = CU_MEMORYTYPE_DEVICE,
  441. .dstMemoryType = CU_MEMORYTYPE_DEVICE,
  442. .srcDevice = mapped_frame,
  443. .dstDevice = (CUdeviceptr)frame->data[i],
  444. .srcPitch = pitch,
  445. .dstPitch = frame->linesize[i],
  446. .srcY = offset,
  447. .WidthInBytes = FFMIN(pitch, frame->linesize[i]),
  448. .Height = height,
  449. };
  450. ret = CHECK_CU(ctx->cudl->cuMemcpy2DAsync(&cpy, device_hwctx->stream));
  451. if (ret < 0)
  452. goto error;
  453. offset += height;
  454. }
  455. } else if (avctx->pix_fmt == AV_PIX_FMT_NV12 ||
  456. avctx->pix_fmt == AV_PIX_FMT_P010 ||
  457. avctx->pix_fmt == AV_PIX_FMT_P016 ||
  458. avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
  459. avctx->pix_fmt == AV_PIX_FMT_YUV444P16) {
  460. unsigned int offset = 0;
  461. AVFrame *tmp_frame = av_frame_alloc();
  462. if (!tmp_frame) {
  463. av_log(avctx, AV_LOG_ERROR, "av_frame_alloc failed\n");
  464. ret = AVERROR(ENOMEM);
  465. goto error;
  466. }
  467. pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  468. tmp_frame->format = AV_PIX_FMT_CUDA;
  469. tmp_frame->hw_frames_ctx = av_buffer_ref(ctx->hwframe);
  470. tmp_frame->width = avctx->width;
  471. tmp_frame->height = avctx->height;
  472. /*
  473. * Note that the following logic would not work for three plane
  474. * YUV420 because the pitch value is different for the chroma
  475. * planes.
  476. */
  477. for (i = 0; i < pixdesc->nb_components; i++) {
  478. tmp_frame->data[i] = (uint8_t*)mapped_frame + offset;
  479. tmp_frame->linesize[i] = pitch;
  480. offset += pitch * (avctx->height >> (i ? pixdesc->log2_chroma_h : 0));
  481. }
  482. ret = ff_get_buffer(avctx, frame, 0);
  483. if (ret < 0) {
  484. av_log(avctx, AV_LOG_ERROR, "ff_get_buffer failed\n");
  485. av_frame_free(&tmp_frame);
  486. goto error;
  487. }
  488. ret = av_hwframe_transfer_data(frame, tmp_frame, 0);
  489. if (ret) {
  490. av_log(avctx, AV_LOG_ERROR, "av_hwframe_transfer_data failed\n");
  491. av_frame_free(&tmp_frame);
  492. goto error;
  493. }
  494. av_frame_free(&tmp_frame);
  495. } else {
  496. ret = AVERROR_BUG;
  497. goto error;
  498. }
  499. frame->key_frame = ctx->key_frame[parsed_frame.dispinfo.picture_index];
  500. frame->width = avctx->width;
  501. frame->height = avctx->height;
  502. if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
  503. frame->pts = av_rescale_q(parsed_frame.dispinfo.timestamp, (AVRational){1, 10000000}, avctx->pkt_timebase);
  504. else
  505. frame->pts = parsed_frame.dispinfo.timestamp;
  506. if (parsed_frame.second_field) {
  507. if (ctx->prev_pts == INT64_MIN) {
  508. ctx->prev_pts = frame->pts;
  509. frame->pts += (avctx->pkt_timebase.den * avctx->framerate.den) / (avctx->pkt_timebase.num * avctx->framerate.num);
  510. } else {
  511. int pts_diff = (frame->pts - ctx->prev_pts) / 2;
  512. ctx->prev_pts = frame->pts;
  513. frame->pts += pts_diff;
  514. }
  515. }
  516. /* CUVIDs opaque reordering breaks the internal pkt logic.
  517. * So set pkt_pts and clear all the other pkt_ fields.
  518. */
  519. #if FF_API_PKT_PTS
  520. FF_DISABLE_DEPRECATION_WARNINGS
  521. frame->pkt_pts = frame->pts;
  522. FF_ENABLE_DEPRECATION_WARNINGS
  523. #endif
  524. frame->pkt_pos = -1;
  525. frame->pkt_duration = 0;
  526. frame->pkt_size = -1;
  527. frame->interlaced_frame = !parsed_frame.is_deinterlacing && !parsed_frame.dispinfo.progressive_frame;
  528. if (frame->interlaced_frame)
  529. frame->top_field_first = parsed_frame.dispinfo.top_field_first;
  530. } else if (ctx->decoder_flushing) {
  531. ret = AVERROR_EOF;
  532. } else {
  533. ret = AVERROR(EAGAIN);
  534. }
  535. error:
  536. if (mapped_frame)
  537. eret = CHECK_CU(ctx->cvdl->cuvidUnmapVideoFrame(ctx->cudecoder, mapped_frame));
  538. eret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
  539. if (eret < 0)
  540. return eret;
  541. else
  542. return ret;
  543. }
  544. static int cuvid_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
  545. {
  546. CuvidContext *ctx = avctx->priv_data;
  547. AVFrame *frame = data;
  548. int ret = 0;
  549. av_log(avctx, AV_LOG_TRACE, "cuvid_decode_frame\n");
  550. if (ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave) {
  551. av_log(avctx, AV_LOG_ERROR, "Deinterlacing is not supported via the old API\n");
  552. return AVERROR(EINVAL);
  553. }
  554. if (!ctx->decoder_flushing) {
  555. ret = cuvid_decode_packet(avctx, avpkt);
  556. if (ret < 0)
  557. return ret;
  558. }
  559. ret = cuvid_output_frame(avctx, frame);
  560. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  561. *got_frame = 0;
  562. } else if (ret < 0) {
  563. return ret;
  564. } else {
  565. *got_frame = 1;
  566. }
  567. return 0;
  568. }
  569. static av_cold int cuvid_decode_end(AVCodecContext *avctx)
  570. {
  571. CuvidContext *ctx = avctx->priv_data;
  572. av_fifo_freep(&ctx->frame_queue);
  573. if (ctx->bsf)
  574. av_bsf_free(&ctx->bsf);
  575. if (ctx->cuparser)
  576. ctx->cvdl->cuvidDestroyVideoParser(ctx->cuparser);
  577. if (ctx->cudecoder)
  578. ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder);
  579. ctx->cudl = NULL;
  580. av_buffer_unref(&ctx->hwframe);
  581. av_buffer_unref(&ctx->hwdevice);
  582. av_freep(&ctx->key_frame);
  583. cuvid_free_functions(&ctx->cvdl);
  584. return 0;
  585. }
  586. static int cuvid_test_capabilities(AVCodecContext *avctx,
  587. const CUVIDPARSERPARAMS *cuparseinfo,
  588. int probed_width,
  589. int probed_height,
  590. int bit_depth)
  591. {
  592. CuvidContext *ctx = avctx->priv_data;
  593. CUVIDDECODECAPS *caps;
  594. int res8 = 0, res10 = 0, res12 = 0;
  595. if (!ctx->cvdl->cuvidGetDecoderCaps) {
  596. av_log(avctx, AV_LOG_WARNING, "Used Nvidia driver is too old to perform a capability check.\n");
  597. av_log(avctx, AV_LOG_WARNING, "The minimum required version is "
  598. #if defined(_WIN32) || defined(__CYGWIN__)
  599. "378.66"
  600. #else
  601. "378.13"
  602. #endif
  603. ". Continuing blind.\n");
  604. ctx->caps8.bIsSupported = ctx->caps10.bIsSupported = 1;
  605. // 12 bit was not supported before the capability check was introduced, so disable it.
  606. ctx->caps12.bIsSupported = 0;
  607. return 0;
  608. }
  609. ctx->caps8.eCodecType = ctx->caps10.eCodecType = ctx->caps12.eCodecType
  610. = cuparseinfo->CodecType;
  611. ctx->caps8.eChromaFormat = ctx->caps10.eChromaFormat = ctx->caps12.eChromaFormat
  612. = cudaVideoChromaFormat_420;
  613. ctx->caps8.nBitDepthMinus8 = 0;
  614. ctx->caps10.nBitDepthMinus8 = 2;
  615. ctx->caps12.nBitDepthMinus8 = 4;
  616. res8 = CHECK_CU(ctx->cvdl->cuvidGetDecoderCaps(&ctx->caps8));
  617. res10 = CHECK_CU(ctx->cvdl->cuvidGetDecoderCaps(&ctx->caps10));
  618. res12 = CHECK_CU(ctx->cvdl->cuvidGetDecoderCaps(&ctx->caps12));
  619. av_log(avctx, AV_LOG_VERBOSE, "CUVID capabilities for %s:\n", avctx->codec->name);
  620. av_log(avctx, AV_LOG_VERBOSE, "8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
  621. ctx->caps8.bIsSupported, ctx->caps8.nMinWidth, ctx->caps8.nMaxWidth, ctx->caps8.nMinHeight, ctx->caps8.nMaxHeight);
  622. av_log(avctx, AV_LOG_VERBOSE, "10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
  623. ctx->caps10.bIsSupported, ctx->caps10.nMinWidth, ctx->caps10.nMaxWidth, ctx->caps10.nMinHeight, ctx->caps10.nMaxHeight);
  624. av_log(avctx, AV_LOG_VERBOSE, "12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
  625. ctx->caps12.bIsSupported, ctx->caps12.nMinWidth, ctx->caps12.nMaxWidth, ctx->caps12.nMinHeight, ctx->caps12.nMaxHeight);
  626. switch (bit_depth) {
  627. case 10:
  628. caps = &ctx->caps10;
  629. if (res10 < 0)
  630. return res10;
  631. break;
  632. case 12:
  633. caps = &ctx->caps12;
  634. if (res12 < 0)
  635. return res12;
  636. break;
  637. default:
  638. caps = &ctx->caps8;
  639. if (res8 < 0)
  640. return res8;
  641. }
  642. if (!ctx->caps8.bIsSupported) {
  643. av_log(avctx, AV_LOG_ERROR, "Codec %s is not supported.\n", avctx->codec->name);
  644. return AVERROR(EINVAL);
  645. }
  646. if (!caps->bIsSupported) {
  647. av_log(avctx, AV_LOG_ERROR, "Bit depth %d is not supported.\n", bit_depth);
  648. return AVERROR(EINVAL);
  649. }
  650. if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
  651. av_log(avctx, AV_LOG_ERROR, "Video width %d not within range from %d to %d\n",
  652. probed_width, caps->nMinWidth, caps->nMaxWidth);
  653. return AVERROR(EINVAL);
  654. }
  655. if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
  656. av_log(avctx, AV_LOG_ERROR, "Video height %d not within range from %d to %d\n",
  657. probed_height, caps->nMinHeight, caps->nMaxHeight);
  658. return AVERROR(EINVAL);
  659. }
  660. return 0;
  661. }
  662. static av_cold int cuvid_decode_init(AVCodecContext *avctx)
  663. {
  664. CuvidContext *ctx = avctx->priv_data;
  665. AVCUDADeviceContext *device_hwctx;
  666. AVHWDeviceContext *device_ctx;
  667. AVHWFramesContext *hwframe_ctx;
  668. CUVIDSOURCEDATAPACKET seq_pkt;
  669. CUcontext cuda_ctx = NULL;
  670. CUcontext dummy;
  671. const AVBitStreamFilter *bsf;
  672. int ret = 0;
  673. enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_CUDA,
  674. AV_PIX_FMT_NV12,
  675. AV_PIX_FMT_NONE };
  676. int probed_width = avctx->coded_width ? avctx->coded_width : 1280;
  677. int probed_height = avctx->coded_height ? avctx->coded_height : 720;
  678. int probed_bit_depth = 8;
  679. const AVPixFmtDescriptor *probe_desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  680. if (probe_desc && probe_desc->nb_components)
  681. probed_bit_depth = probe_desc->comp[0].depth;
  682. // Accelerated transcoding scenarios with 'ffmpeg' require that the
  683. // pix_fmt be set to AV_PIX_FMT_CUDA early. The sw_pix_fmt, and the
  684. // pix_fmt for non-accelerated transcoding, do not need to be correct
  685. // but need to be set to something. We arbitrarily pick NV12.
  686. ret = ff_get_format(avctx, pix_fmts);
  687. if (ret < 0) {
  688. av_log(avctx, AV_LOG_ERROR, "ff_get_format failed: %d\n", ret);
  689. return ret;
  690. }
  691. avctx->pix_fmt = ret;
  692. if (ctx->resize_expr && sscanf(ctx->resize_expr, "%dx%d",
  693. &ctx->resize.width, &ctx->resize.height) != 2) {
  694. av_log(avctx, AV_LOG_ERROR, "Invalid resize expressions\n");
  695. ret = AVERROR(EINVAL);
  696. goto error;
  697. }
  698. if (ctx->crop_expr && sscanf(ctx->crop_expr, "%dx%dx%dx%d",
  699. &ctx->crop.top, &ctx->crop.bottom,
  700. &ctx->crop.left, &ctx->crop.right) != 4) {
  701. av_log(avctx, AV_LOG_ERROR, "Invalid cropping expressions\n");
  702. ret = AVERROR(EINVAL);
  703. goto error;
  704. }
  705. ret = cuvid_load_functions(&ctx->cvdl, avctx);
  706. if (ret < 0) {
  707. av_log(avctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n");
  708. goto error;
  709. }
  710. ctx->frame_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(CuvidParsedFrame));
  711. if (!ctx->frame_queue) {
  712. ret = AVERROR(ENOMEM);
  713. goto error;
  714. }
  715. if (avctx->hw_frames_ctx) {
  716. ctx->hwframe = av_buffer_ref(avctx->hw_frames_ctx);
  717. if (!ctx->hwframe) {
  718. ret = AVERROR(ENOMEM);
  719. goto error;
  720. }
  721. hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
  722. ctx->hwdevice = av_buffer_ref(hwframe_ctx->device_ref);
  723. if (!ctx->hwdevice) {
  724. ret = AVERROR(ENOMEM);
  725. goto error;
  726. }
  727. } else {
  728. if (avctx->hw_device_ctx) {
  729. ctx->hwdevice = av_buffer_ref(avctx->hw_device_ctx);
  730. if (!ctx->hwdevice) {
  731. ret = AVERROR(ENOMEM);
  732. goto error;
  733. }
  734. } else {
  735. ret = av_hwdevice_ctx_create(&ctx->hwdevice, AV_HWDEVICE_TYPE_CUDA, ctx->cu_gpu, NULL, 0);
  736. if (ret < 0)
  737. goto error;
  738. }
  739. ctx->hwframe = av_hwframe_ctx_alloc(ctx->hwdevice);
  740. if (!ctx->hwframe) {
  741. av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n");
  742. ret = AVERROR(ENOMEM);
  743. goto error;
  744. }
  745. hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
  746. }
  747. device_ctx = hwframe_ctx->device_ctx;
  748. device_hwctx = device_ctx->hwctx;
  749. cuda_ctx = device_hwctx->cuda_ctx;
  750. ctx->cudl = device_hwctx->internal->cuda_dl;
  751. memset(&ctx->cuparseinfo, 0, sizeof(ctx->cuparseinfo));
  752. memset(&ctx->cuparse_ext, 0, sizeof(ctx->cuparse_ext));
  753. memset(&seq_pkt, 0, sizeof(seq_pkt));
  754. ctx->cuparseinfo.pExtVideoInfo = &ctx->cuparse_ext;
  755. switch (avctx->codec->id) {
  756. #if CONFIG_H264_CUVID_DECODER
  757. case AV_CODEC_ID_H264:
  758. ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
  759. break;
  760. #endif
  761. #if CONFIG_HEVC_CUVID_DECODER
  762. case AV_CODEC_ID_HEVC:
  763. ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
  764. break;
  765. #endif
  766. #if CONFIG_MJPEG_CUVID_DECODER
  767. case AV_CODEC_ID_MJPEG:
  768. ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
  769. break;
  770. #endif
  771. #if CONFIG_MPEG1_CUVID_DECODER
  772. case AV_CODEC_ID_MPEG1VIDEO:
  773. ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
  774. break;
  775. #endif
  776. #if CONFIG_MPEG2_CUVID_DECODER
  777. case AV_CODEC_ID_MPEG2VIDEO:
  778. ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
  779. break;
  780. #endif
  781. #if CONFIG_MPEG4_CUVID_DECODER
  782. case AV_CODEC_ID_MPEG4:
  783. ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
  784. break;
  785. #endif
  786. #if CONFIG_VP8_CUVID_DECODER
  787. case AV_CODEC_ID_VP8:
  788. ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
  789. break;
  790. #endif
  791. #if CONFIG_VP9_CUVID_DECODER
  792. case AV_CODEC_ID_VP9:
  793. ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
  794. break;
  795. #endif
  796. #if CONFIG_VC1_CUVID_DECODER
  797. case AV_CODEC_ID_VC1:
  798. ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
  799. break;
  800. #endif
  801. default:
  802. av_log(avctx, AV_LOG_ERROR, "Invalid CUVID codec!\n");
  803. return AVERROR_BUG;
  804. }
  805. if (avctx->codec->id == AV_CODEC_ID_H264 || avctx->codec->id == AV_CODEC_ID_HEVC) {
  806. if (avctx->codec->id == AV_CODEC_ID_H264)
  807. bsf = av_bsf_get_by_name("h264_mp4toannexb");
  808. else
  809. bsf = av_bsf_get_by_name("hevc_mp4toannexb");
  810. if (!bsf) {
  811. ret = AVERROR_BSF_NOT_FOUND;
  812. goto error;
  813. }
  814. if (ret = av_bsf_alloc(bsf, &ctx->bsf)) {
  815. goto error;
  816. }
  817. if (((ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx)) < 0) || ((ret = av_bsf_init(ctx->bsf)) < 0)) {
  818. av_bsf_free(&ctx->bsf);
  819. goto error;
  820. }
  821. ctx->cuparse_ext.format.seqhdr_data_length = ctx->bsf->par_out->extradata_size;
  822. memcpy(ctx->cuparse_ext.raw_seqhdr_data,
  823. ctx->bsf->par_out->extradata,
  824. FFMIN(sizeof(ctx->cuparse_ext.raw_seqhdr_data), ctx->bsf->par_out->extradata_size));
  825. } else if (avctx->extradata_size > 0) {
  826. ctx->cuparse_ext.format.seqhdr_data_length = avctx->extradata_size;
  827. memcpy(ctx->cuparse_ext.raw_seqhdr_data,
  828. avctx->extradata,
  829. FFMIN(sizeof(ctx->cuparse_ext.raw_seqhdr_data), avctx->extradata_size));
  830. }
  831. ctx->key_frame = av_mallocz(ctx->nb_surfaces * sizeof(int));
  832. if (!ctx->key_frame) {
  833. ret = AVERROR(ENOMEM);
  834. goto error;
  835. }
  836. ctx->cuparseinfo.ulMaxNumDecodeSurfaces = ctx->nb_surfaces;
  837. ctx->cuparseinfo.ulMaxDisplayDelay = 4;
  838. ctx->cuparseinfo.pUserData = avctx;
  839. ctx->cuparseinfo.pfnSequenceCallback = cuvid_handle_video_sequence;
  840. ctx->cuparseinfo.pfnDecodePicture = cuvid_handle_picture_decode;
  841. ctx->cuparseinfo.pfnDisplayPicture = cuvid_handle_picture_display;
  842. ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
  843. if (ret < 0)
  844. goto error;
  845. ret = cuvid_test_capabilities(avctx, &ctx->cuparseinfo,
  846. probed_width,
  847. probed_height,
  848. probed_bit_depth);
  849. if (ret < 0)
  850. goto error;
  851. ret = CHECK_CU(ctx->cvdl->cuvidCreateVideoParser(&ctx->cuparser, &ctx->cuparseinfo));
  852. if (ret < 0)
  853. goto error;
  854. seq_pkt.payload = ctx->cuparse_ext.raw_seqhdr_data;
  855. seq_pkt.payload_size = ctx->cuparse_ext.format.seqhdr_data_length;
  856. if (seq_pkt.payload && seq_pkt.payload_size) {
  857. ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &seq_pkt));
  858. if (ret < 0)
  859. goto error;
  860. }
  861. ret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
  862. if (ret < 0)
  863. goto error;
  864. ctx->prev_pts = INT64_MIN;
  865. if (!avctx->pkt_timebase.num || !avctx->pkt_timebase.den)
  866. av_log(avctx, AV_LOG_WARNING, "Invalid pkt_timebase, passing timestamps as-is.\n");
  867. return 0;
  868. error:
  869. cuvid_decode_end(avctx);
  870. return ret;
  871. }
  872. static void cuvid_flush(AVCodecContext *avctx)
  873. {
  874. CuvidContext *ctx = avctx->priv_data;
  875. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
  876. AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
  877. CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
  878. CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
  879. int ret;
  880. ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
  881. if (ret < 0)
  882. goto error;
  883. av_fifo_freep(&ctx->frame_queue);
  884. ctx->frame_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(CuvidParsedFrame));
  885. if (!ctx->frame_queue) {
  886. av_log(avctx, AV_LOG_ERROR, "Failed to recreate frame queue on flush\n");
  887. return;
  888. }
  889. if (ctx->cudecoder) {
  890. ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder);
  891. ctx->cudecoder = NULL;
  892. }
  893. if (ctx->cuparser) {
  894. ctx->cvdl->cuvidDestroyVideoParser(ctx->cuparser);
  895. ctx->cuparser = NULL;
  896. }
  897. ret = CHECK_CU(ctx->cvdl->cuvidCreateVideoParser(&ctx->cuparser, &ctx->cuparseinfo));
  898. if (ret < 0)
  899. goto error;
  900. seq_pkt.payload = ctx->cuparse_ext.raw_seqhdr_data;
  901. seq_pkt.payload_size = ctx->cuparse_ext.format.seqhdr_data_length;
  902. if (seq_pkt.payload && seq_pkt.payload_size) {
  903. ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &seq_pkt));
  904. if (ret < 0)
  905. goto error;
  906. }
  907. ret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
  908. if (ret < 0)
  909. goto error;
  910. ctx->prev_pts = INT64_MIN;
  911. ctx->decoder_flushing = 0;
  912. return;
  913. error:
  914. av_log(avctx, AV_LOG_ERROR, "CUDA reinit on flush failed\n");
  915. }
  916. #define OFFSET(x) offsetof(CuvidContext, x)
  917. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  918. static const AVOption options[] = {
  919. { "deint", "Set deinterlacing mode", OFFSET(deint_mode), AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive, VD, "deint" },
  920. { "weave", "Weave deinterlacing (do nothing)", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0, VD, "deint" },
  921. { "bob", "Bob deinterlacing", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0, VD, "deint" },
  922. { "adaptive", "Adaptive deinterlacing", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0, VD, "deint" },
  923. { "gpu", "GPU to be used for decoding", OFFSET(cu_gpu), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
  924. { "surfaces", "Maximum surfaces to be used for decoding", OFFSET(nb_surfaces), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, VD },
  925. { "drop_second_field", "Drop second field when deinterlacing", OFFSET(drop_second_field), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
  926. { "crop", "Crop (top)x(bottom)x(left)x(right)", OFFSET(crop_expr), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
  927. { "resize", "Resize (width)x(height)", OFFSET(resize_expr), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
  928. { NULL }
  929. };
  930. static const AVCodecHWConfigInternal *cuvid_hw_configs[] = {
  931. &(const AVCodecHWConfigInternal) {
  932. .public = {
  933. .pix_fmt = AV_PIX_FMT_CUDA,
  934. .methods = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX |
  935. AV_CODEC_HW_CONFIG_METHOD_INTERNAL,
  936. .device_type = AV_HWDEVICE_TYPE_CUDA
  937. },
  938. .hwaccel = NULL,
  939. },
  940. NULL
  941. };
  942. #define DEFINE_CUVID_CODEC(x, X) \
  943. static const AVClass x##_cuvid_class = { \
  944. .class_name = #x "_cuvid", \
  945. .item_name = av_default_item_name, \
  946. .option = options, \
  947. .version = LIBAVUTIL_VERSION_INT, \
  948. }; \
  949. AVCodec ff_##x##_cuvid_decoder = { \
  950. .name = #x "_cuvid", \
  951. .long_name = NULL_IF_CONFIG_SMALL("Nvidia CUVID " #X " decoder"), \
  952. .type = AVMEDIA_TYPE_VIDEO, \
  953. .id = AV_CODEC_ID_##X, \
  954. .priv_data_size = sizeof(CuvidContext), \
  955. .priv_class = &x##_cuvid_class, \
  956. .init = cuvid_decode_init, \
  957. .close = cuvid_decode_end, \
  958. .decode = cuvid_decode_frame, \
  959. .receive_frame = cuvid_output_frame, \
  960. .flush = cuvid_flush, \
  961. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
  962. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, \
  963. AV_PIX_FMT_NV12, \
  964. AV_PIX_FMT_P010, \
  965. AV_PIX_FMT_P016, \
  966. AV_PIX_FMT_NONE }, \
  967. .hw_configs = cuvid_hw_configs, \
  968. .wrapper_name = "cuvid", \
  969. };
  970. #if CONFIG_HEVC_CUVID_DECODER
  971. DEFINE_CUVID_CODEC(hevc, HEVC)
  972. #endif
  973. #if CONFIG_H264_CUVID_DECODER
  974. DEFINE_CUVID_CODEC(h264, H264)
  975. #endif
  976. #if CONFIG_MJPEG_CUVID_DECODER
  977. DEFINE_CUVID_CODEC(mjpeg, MJPEG)
  978. #endif
  979. #if CONFIG_MPEG1_CUVID_DECODER
  980. DEFINE_CUVID_CODEC(mpeg1, MPEG1VIDEO)
  981. #endif
  982. #if CONFIG_MPEG2_CUVID_DECODER
  983. DEFINE_CUVID_CODEC(mpeg2, MPEG2VIDEO)
  984. #endif
  985. #if CONFIG_MPEG4_CUVID_DECODER
  986. DEFINE_CUVID_CODEC(mpeg4, MPEG4)
  987. #endif
  988. #if CONFIG_VP8_CUVID_DECODER
  989. DEFINE_CUVID_CODEC(vp8, VP8)
  990. #endif
  991. #if CONFIG_VP9_CUVID_DECODER
  992. DEFINE_CUVID_CODEC(vp9, VP9)
  993. #endif
  994. #if CONFIG_VC1_CUVID_DECODER
  995. DEFINE_CUVID_CODEC(vc1, VC1)
  996. #endif