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.

810 lines
24KB

  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 "libavutil/buffer.h"
  22. #include "libavutil/mathematics.h"
  23. #include "libavutil/hwcontext.h"
  24. #include "libavutil/hwcontext_cuda.h"
  25. #include "libavutil/fifo.h"
  26. #include "libavutil/log.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #include <nvcuvid.h>
  30. #define MAX_FRAME_COUNT 20
  31. typedef struct CuvidContext
  32. {
  33. CUvideodecoder cudecoder;
  34. CUvideoparser cuparser;
  35. AVBufferRef *hwdevice;
  36. AVBufferRef *hwframe;
  37. AVBSFContext *bsf;
  38. AVFifoBuffer *frame_queue;
  39. int internal_error;
  40. int ever_flushed;
  41. cudaVideoCodec codec_type;
  42. cudaVideoChromaFormat chroma_format;
  43. CUVIDPARSERPARAMS cuparseinfo;
  44. CUVIDEOFORMATEX cuparse_ext;
  45. } CuvidContext;
  46. static int check_cu(AVCodecContext *avctx, CUresult err, const char *func)
  47. {
  48. const char *err_name;
  49. const char *err_string;
  50. av_log(avctx, AV_LOG_TRACE, "Calling %s\n", func);
  51. if (err == CUDA_SUCCESS)
  52. return 0;
  53. cuGetErrorName(err, &err_name);
  54. cuGetErrorString(err, &err_string);
  55. av_log(avctx, AV_LOG_ERROR, "%s failed", func);
  56. if (err_name && err_string)
  57. av_log(avctx, AV_LOG_ERROR, " -> %s: %s", err_name, err_string);
  58. av_log(avctx, AV_LOG_ERROR, "\n");
  59. return AVERROR_EXTERNAL;
  60. }
  61. #define CHECK_CU(x) check_cu(avctx, (x), #x)
  62. static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* format)
  63. {
  64. AVCodecContext *avctx = opaque;
  65. CuvidContext *ctx = avctx->priv_data;
  66. AVHWFramesContext *hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
  67. CUVIDDECODECREATEINFO cuinfo;
  68. av_log(avctx, AV_LOG_TRACE, "pfnSequenceCallback\n");
  69. ctx->internal_error = 0;
  70. avctx->width = format->display_area.right;
  71. avctx->height = format->display_area.bottom;
  72. ff_set_sar(avctx, av_div_q(
  73. (AVRational){ format->display_aspect_ratio.x, format->display_aspect_ratio.y },
  74. (AVRational){ avctx->width, avctx->height }));
  75. if (!format->progressive_sequence)
  76. avctx->flags |= AV_CODEC_FLAG_INTERLACED_DCT;
  77. else
  78. avctx->flags &= ~AV_CODEC_FLAG_INTERLACED_DCT;
  79. if (format->video_signal_description.video_full_range_flag)
  80. avctx->color_range = AVCOL_RANGE_JPEG;
  81. else
  82. avctx->color_range = AVCOL_RANGE_MPEG;
  83. avctx->color_primaries = format->video_signal_description.color_primaries;
  84. avctx->color_trc = format->video_signal_description.transfer_characteristics;
  85. avctx->colorspace = format->video_signal_description.matrix_coefficients;
  86. if (format->bitrate)
  87. avctx->bit_rate = format->bitrate;
  88. if (format->frame_rate.numerator && format->frame_rate.denominator) {
  89. avctx->framerate.num = format->frame_rate.numerator;
  90. avctx->framerate.den = format->frame_rate.denominator;
  91. }
  92. if (ctx->cudecoder
  93. && avctx->coded_width == format->coded_width
  94. && avctx->coded_height == format->coded_height
  95. && ctx->chroma_format == format->chroma_format
  96. && ctx->codec_type == format->codec)
  97. return 1;
  98. if (ctx->cudecoder) {
  99. av_log(avctx, AV_LOG_ERROR, "re-initializing decoder is not supported\n");
  100. ctx->internal_error = AVERROR(EINVAL);
  101. return 0;
  102. }
  103. if (hwframe_ctx->pool && !ctx->ever_flushed) {
  104. av_log(avctx, AV_LOG_ERROR, "AVHWFramesContext is already initialized\n");
  105. ctx->internal_error = AVERROR(EINVAL);
  106. return 0;
  107. }
  108. avctx->coded_width = format->coded_width;
  109. avctx->coded_height = format->coded_height;
  110. ctx->chroma_format = format->chroma_format;
  111. memset(&cuinfo, 0, sizeof(cuinfo));
  112. cuinfo.CodecType = ctx->codec_type = format->codec;
  113. cuinfo.ChromaFormat = format->chroma_format;
  114. cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
  115. cuinfo.ulWidth = avctx->coded_width;
  116. cuinfo.ulHeight = avctx->coded_height;
  117. cuinfo.ulTargetWidth = cuinfo.ulWidth;
  118. cuinfo.ulTargetHeight = cuinfo.ulHeight;
  119. cuinfo.target_rect.left = 0;
  120. cuinfo.target_rect.top = 0;
  121. cuinfo.target_rect.right = cuinfo.ulWidth;
  122. cuinfo.target_rect.bottom = cuinfo.ulHeight;
  123. cuinfo.ulNumDecodeSurfaces = MAX_FRAME_COUNT;
  124. cuinfo.ulNumOutputSurfaces = 1;
  125. cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
  126. cuinfo.DeinterlaceMode = cudaVideoDeinterlaceMode_Weave;
  127. ctx->internal_error = CHECK_CU(cuvidCreateDecoder(&ctx->cudecoder, &cuinfo));
  128. if (ctx->internal_error < 0)
  129. return 0;
  130. if (!hwframe_ctx->pool) {
  131. hwframe_ctx->format = AV_PIX_FMT_CUDA;
  132. hwframe_ctx->sw_format = AV_PIX_FMT_NV12;
  133. hwframe_ctx->width = FFALIGN(avctx->coded_width, 32);
  134. hwframe_ctx->height = FFALIGN(avctx->coded_height, 32);
  135. if ((ctx->internal_error = av_hwframe_ctx_init(ctx->hwframe)) < 0) {
  136. av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_init failed\n");
  137. return 0;
  138. }
  139. }
  140. return 1;
  141. }
  142. static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS* picparams)
  143. {
  144. AVCodecContext *avctx = opaque;
  145. CuvidContext *ctx = avctx->priv_data;
  146. av_log(avctx, AV_LOG_TRACE, "pfnDecodePicture\n");
  147. ctx->internal_error = CHECK_CU(cuvidDecodePicture(ctx->cudecoder, picparams));
  148. if (ctx->internal_error < 0)
  149. return 0;
  150. return 1;
  151. }
  152. static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO* dispinfo)
  153. {
  154. AVCodecContext *avctx = opaque;
  155. CuvidContext *ctx = avctx->priv_data;
  156. av_log(avctx, AV_LOG_TRACE, "pfnDisplayPicture\n");
  157. ctx->internal_error = 0;
  158. av_fifo_generic_write(ctx->frame_queue, dispinfo, sizeof(CUVIDPARSERDISPINFO), NULL);
  159. return 1;
  160. }
  161. static int cuvid_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
  162. {
  163. CuvidContext *ctx = avctx->priv_data;
  164. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
  165. AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
  166. CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
  167. AVFrame *frame = data;
  168. CUVIDSOURCEDATAPACKET cupkt;
  169. AVPacket filter_packet = { 0 };
  170. AVPacket filtered_packet = { 0 };
  171. CUdeviceptr mapped_frame = 0;
  172. int ret = 0, eret = 0;
  173. if (ctx->bsf && avpkt->size) {
  174. if ((ret = av_packet_ref(&filter_packet, avpkt)) < 0) {
  175. av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed\n");
  176. return ret;
  177. }
  178. if ((ret = av_bsf_send_packet(ctx->bsf, &filter_packet)) < 0) {
  179. av_log(avctx, AV_LOG_ERROR, "av_bsf_send_packet failed\n");
  180. av_packet_unref(&filter_packet);
  181. return ret;
  182. }
  183. if ((ret = av_bsf_receive_packet(ctx->bsf, &filtered_packet)) < 0) {
  184. av_log(avctx, AV_LOG_ERROR, "av_bsf_receive_packet failed\n");
  185. return ret;
  186. }
  187. avpkt = &filtered_packet;
  188. }
  189. ret = CHECK_CU(cuCtxPushCurrent(cuda_ctx));
  190. if (ret < 0) {
  191. av_packet_unref(&filtered_packet);
  192. return ret;
  193. }
  194. memset(&cupkt, 0, sizeof(cupkt));
  195. if (avpkt->size) {
  196. cupkt.payload_size = avpkt->size;
  197. cupkt.payload = avpkt->data;
  198. if (avpkt->pts != AV_NOPTS_VALUE) {
  199. cupkt.flags = CUVID_PKT_TIMESTAMP;
  200. if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
  201. cupkt.timestamp = av_rescale_q(avpkt->pts, avctx->pkt_timebase, (AVRational){1, 10000000});
  202. else
  203. cupkt.timestamp = avpkt->pts;
  204. }
  205. } else {
  206. cupkt.flags = CUVID_PKT_ENDOFSTREAM;
  207. }
  208. ret = CHECK_CU(cuvidParseVideoData(ctx->cuparser, &cupkt));
  209. av_packet_unref(&filtered_packet);
  210. if (ret < 0) {
  211. goto error;
  212. }
  213. // cuvidParseVideoData doesn't return an error just because stuff failed...
  214. if (ctx->internal_error) {
  215. av_log(avctx, AV_LOG_ERROR, "cuvid decode callback error\n");
  216. ret = ctx->internal_error;
  217. goto error;
  218. }
  219. if (av_fifo_size(ctx->frame_queue)) {
  220. CUVIDPARSERDISPINFO dispinfo;
  221. CUVIDPROCPARAMS params;
  222. unsigned int pitch = 0;
  223. int offset = 0;
  224. int i;
  225. av_fifo_generic_read(ctx->frame_queue, &dispinfo, sizeof(CUVIDPARSERDISPINFO), NULL);
  226. memset(&params, 0, sizeof(params));
  227. params.progressive_frame = dispinfo.progressive_frame;
  228. params.second_field = 0;
  229. params.top_field_first = dispinfo.top_field_first;
  230. ret = CHECK_CU(cuvidMapVideoFrame(ctx->cudecoder, dispinfo.picture_index, &mapped_frame, &pitch, &params));
  231. if (ret < 0)
  232. goto error;
  233. if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
  234. ret = av_hwframe_get_buffer(ctx->hwframe, frame, 0);
  235. if (ret < 0) {
  236. av_log(avctx, AV_LOG_ERROR, "av_hwframe_get_buffer failed\n");
  237. goto error;
  238. }
  239. ret = ff_decode_frame_props(avctx, frame);
  240. if (ret < 0) {
  241. av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props failed\n");
  242. goto error;
  243. }
  244. for (i = 0; i < 2; i++) {
  245. CUDA_MEMCPY2D cpy = {
  246. .srcMemoryType = CU_MEMORYTYPE_DEVICE,
  247. .dstMemoryType = CU_MEMORYTYPE_DEVICE,
  248. .srcDevice = mapped_frame,
  249. .dstDevice = (CUdeviceptr)frame->data[i],
  250. .srcPitch = pitch,
  251. .dstPitch = frame->linesize[i],
  252. .srcY = offset,
  253. .WidthInBytes = FFMIN(pitch, frame->linesize[i]),
  254. .Height = avctx->coded_height >> (i ? 1 : 0),
  255. };
  256. ret = CHECK_CU(cuMemcpy2D(&cpy));
  257. if (ret < 0)
  258. goto error;
  259. offset += avctx->coded_height;
  260. }
  261. } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
  262. AVFrame *tmp_frame = av_frame_alloc();
  263. if (!tmp_frame) {
  264. av_log(avctx, AV_LOG_ERROR, "av_frame_alloc failed\n");
  265. ret = AVERROR(ENOMEM);
  266. goto error;
  267. }
  268. tmp_frame->format = AV_PIX_FMT_CUDA;
  269. tmp_frame->hw_frames_ctx = av_buffer_ref(ctx->hwframe);
  270. tmp_frame->data[0] = (uint8_t*)mapped_frame;
  271. tmp_frame->linesize[0] = pitch;
  272. tmp_frame->data[1] = (uint8_t*)(mapped_frame + avctx->coded_height * pitch);
  273. tmp_frame->linesize[1] = pitch;
  274. tmp_frame->width = avctx->width;
  275. tmp_frame->height = avctx->height;
  276. ret = ff_get_buffer(avctx, frame, 0);
  277. if (ret < 0) {
  278. av_log(avctx, AV_LOG_ERROR, "ff_get_buffer failed\n");
  279. av_frame_free(&tmp_frame);
  280. goto error;
  281. }
  282. ret = av_hwframe_transfer_data(frame, tmp_frame, 0);
  283. if (ret) {
  284. av_log(avctx, AV_LOG_ERROR, "av_hwframe_transfer_data failed\n");
  285. av_frame_free(&tmp_frame);
  286. goto error;
  287. }
  288. av_frame_free(&tmp_frame);
  289. } else {
  290. ret = AVERROR_BUG;
  291. goto error;
  292. }
  293. frame->width = avctx->width;
  294. frame->height = avctx->height;
  295. if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
  296. frame->pts = av_rescale_q(dispinfo.timestamp, (AVRational){1, 10000000}, avctx->pkt_timebase);
  297. else
  298. frame->pts = dispinfo.timestamp;
  299. /* CUVIDs opaque reordering breaks the internal pkt logic.
  300. * So set pkt_pts and clear all the other pkt_ fields.
  301. */
  302. frame->pkt_pts = frame->pts;
  303. av_frame_set_pkt_pos(frame, -1);
  304. av_frame_set_pkt_duration(frame, 0);
  305. av_frame_set_pkt_size(frame, -1);
  306. frame->interlaced_frame = !dispinfo.progressive_frame;
  307. if (!dispinfo.progressive_frame)
  308. frame->top_field_first = dispinfo.top_field_first;
  309. *got_frame = 1;
  310. } else {
  311. *got_frame = 0;
  312. }
  313. error:
  314. if (mapped_frame)
  315. eret = CHECK_CU(cuvidUnmapVideoFrame(ctx->cudecoder, mapped_frame));
  316. eret = CHECK_CU(cuCtxPopCurrent(&dummy));
  317. if (eret < 0)
  318. return eret;
  319. else
  320. return ret;
  321. }
  322. static av_cold int cuvid_decode_end(AVCodecContext *avctx)
  323. {
  324. CuvidContext *ctx = avctx->priv_data;
  325. av_fifo_freep(&ctx->frame_queue);
  326. if (ctx->bsf)
  327. av_bsf_free(&ctx->bsf);
  328. if (ctx->cuparser)
  329. cuvidDestroyVideoParser(ctx->cuparser);
  330. if (ctx->cudecoder)
  331. cuvidDestroyDecoder(ctx->cudecoder);
  332. av_buffer_unref(&ctx->hwframe);
  333. av_buffer_unref(&ctx->hwdevice);
  334. return 0;
  335. }
  336. static void cuvid_ctx_free(AVHWDeviceContext *ctx)
  337. {
  338. AVCUDADeviceContext *hwctx = ctx->hwctx;
  339. cuCtxDestroy(hwctx->cuda_ctx);
  340. }
  341. static int cuvid_test_dummy_decoder(AVCodecContext *avctx, CUVIDPARSERPARAMS *cuparseinfo)
  342. {
  343. CUVIDDECODECREATEINFO cuinfo;
  344. CUvideodecoder cudec = 0;
  345. int ret = 0;
  346. memset(&cuinfo, 0, sizeof(cuinfo));
  347. cuinfo.CodecType = cuparseinfo->CodecType;
  348. cuinfo.ChromaFormat = cudaVideoChromaFormat_420;
  349. cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
  350. cuinfo.ulWidth = 1280;
  351. cuinfo.ulHeight = 720;
  352. cuinfo.ulTargetWidth = cuinfo.ulWidth;
  353. cuinfo.ulTargetHeight = cuinfo.ulHeight;
  354. cuinfo.target_rect.left = 0;
  355. cuinfo.target_rect.top = 0;
  356. cuinfo.target_rect.right = cuinfo.ulWidth;
  357. cuinfo.target_rect.bottom = cuinfo.ulHeight;
  358. cuinfo.ulNumDecodeSurfaces = MAX_FRAME_COUNT;
  359. cuinfo.ulNumOutputSurfaces = 1;
  360. cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
  361. cuinfo.DeinterlaceMode = cudaVideoDeinterlaceMode_Weave;
  362. ret = CHECK_CU(cuvidCreateDecoder(&cudec, &cuinfo));
  363. if (ret < 0)
  364. return ret;
  365. ret = CHECK_CU(cuvidDestroyDecoder(cudec));
  366. if (ret < 0)
  367. return ret;
  368. return 0;
  369. }
  370. static av_cold int cuvid_decode_init(AVCodecContext *avctx)
  371. {
  372. CuvidContext *ctx = avctx->priv_data;
  373. AVCUDADeviceContext *device_hwctx;
  374. AVHWDeviceContext *device_ctx;
  375. AVHWFramesContext *hwframe_ctx;
  376. CUVIDSOURCEDATAPACKET seq_pkt;
  377. CUdevice device;
  378. CUcontext cuda_ctx = NULL;
  379. CUcontext dummy;
  380. const AVBitStreamFilter *bsf;
  381. int ret = 0;
  382. enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_CUDA,
  383. AV_PIX_FMT_NV12,
  384. AV_PIX_FMT_NONE };
  385. ret = ff_get_format(avctx, pix_fmts);
  386. if (ret < 0) {
  387. av_log(avctx, AV_LOG_ERROR, "ff_get_format failed: %d\n", ret);
  388. return ret;
  389. }
  390. ctx->frame_queue = av_fifo_alloc(MAX_FRAME_COUNT * sizeof(CUVIDPARSERDISPINFO));
  391. if (!ctx->frame_queue) {
  392. ret = AVERROR(ENOMEM);
  393. goto error;
  394. }
  395. avctx->pix_fmt = ret;
  396. if (avctx->hw_frames_ctx) {
  397. ctx->hwframe = av_buffer_ref(avctx->hw_frames_ctx);
  398. if (!ctx->hwframe) {
  399. ret = AVERROR(ENOMEM);
  400. goto error;
  401. }
  402. hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
  403. ctx->hwdevice = av_buffer_ref(hwframe_ctx->device_ref);
  404. if (!ctx->hwdevice) {
  405. ret = AVERROR(ENOMEM);
  406. goto error;
  407. }
  408. device_ctx = hwframe_ctx->device_ctx;
  409. device_hwctx = device_ctx->hwctx;
  410. cuda_ctx = device_hwctx->cuda_ctx;
  411. } else {
  412. ctx->hwdevice = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA);
  413. if (!ctx->hwdevice) {
  414. av_log(avctx, AV_LOG_ERROR, "Error allocating hwdevice\n");
  415. ret = AVERROR(ENOMEM);
  416. goto error;
  417. }
  418. ret = CHECK_CU(cuInit(0));
  419. if (ret < 0)
  420. goto error;
  421. ret = CHECK_CU(cuDeviceGet(&device, 0));
  422. if (ret < 0)
  423. goto error;
  424. ret = CHECK_CU(cuCtxCreate(&cuda_ctx, CU_CTX_SCHED_BLOCKING_SYNC, device));
  425. if (ret < 0)
  426. goto error;
  427. device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
  428. device_ctx->free = cuvid_ctx_free;
  429. device_hwctx = device_ctx->hwctx;
  430. device_hwctx->cuda_ctx = cuda_ctx;
  431. ret = CHECK_CU(cuCtxPopCurrent(&dummy));
  432. if (ret < 0)
  433. goto error;
  434. ret = av_hwdevice_ctx_init(ctx->hwdevice);
  435. if (ret < 0) {
  436. av_log(avctx, AV_LOG_ERROR, "av_hwdevice_ctx_init failed\n");
  437. goto error;
  438. }
  439. ctx->hwframe = av_hwframe_ctx_alloc(ctx->hwdevice);
  440. if (!ctx->hwframe) {
  441. av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n");
  442. ret = AVERROR(ENOMEM);
  443. goto error;
  444. }
  445. }
  446. memset(&ctx->cuparseinfo, 0, sizeof(ctx->cuparseinfo));
  447. memset(&ctx->cuparse_ext, 0, sizeof(ctx->cuparse_ext));
  448. memset(&seq_pkt, 0, sizeof(seq_pkt));
  449. ctx->cuparseinfo.pExtVideoInfo = &ctx->cuparse_ext;
  450. switch (avctx->codec->id) {
  451. #if CONFIG_H263_CUVID_DECODER
  452. case AV_CODEC_ID_H263:
  453. ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
  454. break;
  455. #endif
  456. #if CONFIG_H264_CUVID_DECODER
  457. case AV_CODEC_ID_H264:
  458. ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
  459. break;
  460. #endif
  461. #if CONFIG_HEVC_CUVID_DECODER
  462. case AV_CODEC_ID_HEVC:
  463. ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
  464. break;
  465. #endif
  466. #if CONFIG_MJPEG_CUVID_DECODER
  467. case AV_CODEC_ID_MJPEG:
  468. ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
  469. break;
  470. #endif
  471. #if CONFIG_MPEG1_CUVID_DECODER
  472. case AV_CODEC_ID_MPEG1VIDEO:
  473. ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
  474. break;
  475. #endif
  476. #if CONFIG_MPEG2_CUVID_DECODER
  477. case AV_CODEC_ID_MPEG2VIDEO:
  478. ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
  479. break;
  480. #endif
  481. #if CONFIG_MPEG4_CUVID_DECODER
  482. case AV_CODEC_ID_MPEG4:
  483. ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
  484. break;
  485. #endif
  486. #if CONFIG_VP8_CUVID_DECODER
  487. case AV_CODEC_ID_VP8:
  488. ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
  489. break;
  490. #endif
  491. #if CONFIG_VP9_CUVID_DECODER
  492. case AV_CODEC_ID_VP9:
  493. ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
  494. break;
  495. #endif
  496. #if CONFIG_VC1_CUVID_DECODER
  497. case AV_CODEC_ID_VC1:
  498. ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
  499. break;
  500. #endif
  501. default:
  502. av_log(avctx, AV_LOG_ERROR, "Invalid CUVID codec!\n");
  503. return AVERROR_BUG;
  504. }
  505. if (avctx->codec->id == AV_CODEC_ID_H264 || avctx->codec->id == AV_CODEC_ID_HEVC) {
  506. if (avctx->codec->id == AV_CODEC_ID_H264)
  507. bsf = av_bsf_get_by_name("h264_mp4toannexb");
  508. else
  509. bsf = av_bsf_get_by_name("hevc_mp4toannexb");
  510. if (!bsf) {
  511. ret = AVERROR_BSF_NOT_FOUND;
  512. goto error;
  513. }
  514. if (ret = av_bsf_alloc(bsf, &ctx->bsf)) {
  515. goto error;
  516. }
  517. if (((ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx)) < 0) || ((ret = av_bsf_init(ctx->bsf)) < 0)) {
  518. av_bsf_free(&ctx->bsf);
  519. goto error;
  520. }
  521. ctx->cuparse_ext.format.seqhdr_data_length = ctx->bsf->par_out->extradata_size;
  522. memcpy(ctx->cuparse_ext.raw_seqhdr_data,
  523. ctx->bsf->par_out->extradata,
  524. FFMIN(sizeof(ctx->cuparse_ext.raw_seqhdr_data), ctx->bsf->par_out->extradata_size));
  525. } else if (avctx->extradata_size > 0) {
  526. ctx->cuparse_ext.format.seqhdr_data_length = avctx->extradata_size;
  527. memcpy(ctx->cuparse_ext.raw_seqhdr_data,
  528. avctx->extradata,
  529. FFMIN(sizeof(ctx->cuparse_ext.raw_seqhdr_data), avctx->extradata_size));
  530. }
  531. ctx->cuparseinfo.ulMaxNumDecodeSurfaces = MAX_FRAME_COUNT;
  532. ctx->cuparseinfo.ulMaxDisplayDelay = 4;
  533. ctx->cuparseinfo.pUserData = avctx;
  534. ctx->cuparseinfo.pfnSequenceCallback = cuvid_handle_video_sequence;
  535. ctx->cuparseinfo.pfnDecodePicture = cuvid_handle_picture_decode;
  536. ctx->cuparseinfo.pfnDisplayPicture = cuvid_handle_picture_display;
  537. ret = CHECK_CU(cuCtxPushCurrent(cuda_ctx));
  538. if (ret < 0)
  539. goto error;
  540. ret = cuvid_test_dummy_decoder(avctx, &ctx->cuparseinfo);
  541. if (ret < 0)
  542. goto error;
  543. ret = CHECK_CU(cuvidCreateVideoParser(&ctx->cuparser, &ctx->cuparseinfo));
  544. if (ret < 0)
  545. goto error;
  546. seq_pkt.payload = ctx->cuparse_ext.raw_seqhdr_data;
  547. seq_pkt.payload_size = ctx->cuparse_ext.format.seqhdr_data_length;
  548. if (seq_pkt.payload && seq_pkt.payload_size) {
  549. ret = CHECK_CU(cuvidParseVideoData(ctx->cuparser, &seq_pkt));
  550. if (ret < 0)
  551. goto error;
  552. }
  553. ret = CHECK_CU(cuCtxPopCurrent(&dummy));
  554. if (ret < 0)
  555. goto error;
  556. ctx->ever_flushed = 0;
  557. if (!avctx->pkt_timebase.num || !avctx->pkt_timebase.den)
  558. av_log(avctx, AV_LOG_WARNING, "Invalid pkt_timebase, passing timestamps as-is.\n");
  559. return 0;
  560. error:
  561. cuvid_decode_end(avctx);
  562. return ret;
  563. }
  564. static void cuvid_flush(AVCodecContext *avctx)
  565. {
  566. CuvidContext *ctx = avctx->priv_data;
  567. AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
  568. AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
  569. CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
  570. int ret;
  571. ctx->ever_flushed = 1;
  572. ret = CHECK_CU(cuCtxPushCurrent(cuda_ctx));
  573. if (ret < 0)
  574. goto error;
  575. av_fifo_freep(&ctx->frame_queue);
  576. ctx->frame_queue = av_fifo_alloc(MAX_FRAME_COUNT * sizeof(CUVIDPARSERDISPINFO));
  577. if (!ctx->frame_queue) {
  578. av_log(avctx, AV_LOG_ERROR, "Failed to recreate frame queue on flush\n");
  579. return;
  580. }
  581. if (ctx->cudecoder) {
  582. cuvidDestroyDecoder(ctx->cudecoder);
  583. ctx->cudecoder = NULL;
  584. }
  585. if (ctx->cuparser) {
  586. cuvidDestroyVideoParser(ctx->cuparser);
  587. ctx->cuparser = NULL;
  588. }
  589. ret = CHECK_CU(cuvidCreateVideoParser(&ctx->cuparser, &ctx->cuparseinfo));
  590. if (ret < 0)
  591. goto error;
  592. ret = CHECK_CU(cuCtxPopCurrent(&dummy));
  593. if (ret < 0)
  594. goto error;
  595. return;
  596. error:
  597. av_log(avctx, AV_LOG_ERROR, "CUDA reinit on flush failed\n");
  598. }
  599. #define DEFINE_CUVID_CODEC(x, X) \
  600. AVHWAccel ff_##x##_cuvid_hwaccel = { \
  601. .name = #x "_cuvid", \
  602. .type = AVMEDIA_TYPE_VIDEO, \
  603. .id = AV_CODEC_ID_##X, \
  604. .pix_fmt = AV_PIX_FMT_CUDA, \
  605. }; \
  606. AVCodec ff_##x##_cuvid_decoder = { \
  607. .name = #x "_cuvid", \
  608. .long_name = NULL_IF_CONFIG_SMALL("Nvidia CUVID " #X " decoder"), \
  609. .type = AVMEDIA_TYPE_VIDEO, \
  610. .id = AV_CODEC_ID_##X, \
  611. .priv_data_size = sizeof(CuvidContext), \
  612. .init = cuvid_decode_init, \
  613. .close = cuvid_decode_end, \
  614. .decode = cuvid_decode_frame, \
  615. .flush = cuvid_flush, \
  616. .capabilities = AV_CODEC_CAP_DELAY, \
  617. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, \
  618. AV_PIX_FMT_NV12, \
  619. AV_PIX_FMT_NONE }, \
  620. };
  621. #if CONFIG_HEVC_CUVID_DECODER
  622. DEFINE_CUVID_CODEC(hevc, HEVC)
  623. #endif
  624. #if CONFIG_H263_CUVID_DECODER
  625. DEFINE_CUVID_CODEC(h263, H263)
  626. #endif
  627. #if CONFIG_H264_CUVID_DECODER
  628. DEFINE_CUVID_CODEC(h264, H264)
  629. #endif
  630. #if CONFIG_MJPEG_CUVID_DECODER
  631. DEFINE_CUVID_CODEC(mjpeg, MJPEG)
  632. #endif
  633. #if CONFIG_MPEG1_CUVID_DECODER
  634. DEFINE_CUVID_CODEC(mpeg1, MPEG1VIDEO)
  635. #endif
  636. #if CONFIG_MPEG2_CUVID_DECODER
  637. DEFINE_CUVID_CODEC(mpeg2, MPEG2VIDEO)
  638. #endif
  639. #if CONFIG_MPEG4_CUVID_DECODER
  640. DEFINE_CUVID_CODEC(mpeg4, MPEG4)
  641. #endif
  642. #if CONFIG_VP8_CUVID_DECODER
  643. DEFINE_CUVID_CODEC(vp8, VP8)
  644. #endif
  645. #if CONFIG_VP9_CUVID_DECODER
  646. DEFINE_CUVID_CODEC(vp9, VP9)
  647. #endif
  648. #if CONFIG_VC1_CUVID_DECODER
  649. DEFINE_CUVID_CODEC(vc1, VC1)
  650. #endif