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.

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