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.

826 lines
25KB

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