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.

1174 lines
39KB

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