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.

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