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.

762 lines
22KB

  1. /*
  2. * HW decode acceleration through NVDEC
  3. *
  4. * Copyright (c) 2016 Anton Khirnov
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/error.h"
  25. #include "libavutil/hwcontext.h"
  26. #include "libavutil/hwcontext_cuda_internal.h"
  27. #include "libavutil/cuda_check.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/pixfmt.h"
  30. #include "avcodec.h"
  31. #include "decode.h"
  32. #include "nvdec.h"
  33. #include "internal.h"
  34. #if !NVDECAPI_CHECK_VERSION(9, 0)
  35. #define cudaVideoSurfaceFormat_YUV444 2
  36. #define cudaVideoSurfaceFormat_YUV444_16Bit 3
  37. #endif
  38. typedef struct NVDECDecoder {
  39. CUvideodecoder decoder;
  40. AVBufferRef *hw_device_ref;
  41. AVBufferRef *real_hw_frames_ref;
  42. CUcontext cuda_ctx;
  43. CUstream stream;
  44. CudaFunctions *cudl;
  45. CuvidFunctions *cvdl;
  46. } NVDECDecoder;
  47. typedef struct NVDECFramePool {
  48. unsigned int dpb_size;
  49. unsigned int nb_allocated;
  50. } NVDECFramePool;
  51. #define CHECK_CU(x) FF_CUDA_CHECK_DL(logctx, decoder->cudl, x)
  52. static int map_avcodec_id(enum AVCodecID id)
  53. {
  54. switch (id) {
  55. #if CONFIG_AV1_NVDEC_HWACCEL
  56. case AV_CODEC_ID_AV1: return cudaVideoCodec_AV1;
  57. #endif
  58. case AV_CODEC_ID_H264: return cudaVideoCodec_H264;
  59. case AV_CODEC_ID_HEVC: return cudaVideoCodec_HEVC;
  60. case AV_CODEC_ID_MJPEG: return cudaVideoCodec_JPEG;
  61. case AV_CODEC_ID_MPEG1VIDEO: return cudaVideoCodec_MPEG1;
  62. case AV_CODEC_ID_MPEG2VIDEO: return cudaVideoCodec_MPEG2;
  63. case AV_CODEC_ID_MPEG4: return cudaVideoCodec_MPEG4;
  64. case AV_CODEC_ID_VC1: return cudaVideoCodec_VC1;
  65. case AV_CODEC_ID_VP8: return cudaVideoCodec_VP8;
  66. case AV_CODEC_ID_VP9: return cudaVideoCodec_VP9;
  67. case AV_CODEC_ID_WMV3: return cudaVideoCodec_VC1;
  68. }
  69. return -1;
  70. }
  71. static int map_chroma_format(enum AVPixelFormat pix_fmt)
  72. {
  73. int shift_h = 0, shift_v = 0;
  74. if (av_pix_fmt_count_planes(pix_fmt) == 1)
  75. return cudaVideoChromaFormat_Monochrome;
  76. av_pix_fmt_get_chroma_sub_sample(pix_fmt, &shift_h, &shift_v);
  77. if (shift_h == 1 && shift_v == 1)
  78. return cudaVideoChromaFormat_420;
  79. else if (shift_h == 1 && shift_v == 0)
  80. return cudaVideoChromaFormat_422;
  81. else if (shift_h == 0 && shift_v == 0)
  82. return cudaVideoChromaFormat_444;
  83. return -1;
  84. }
  85. static int nvdec_test_capabilities(NVDECDecoder *decoder,
  86. CUVIDDECODECREATEINFO *params, void *logctx)
  87. {
  88. int ret;
  89. CUVIDDECODECAPS caps = { 0 };
  90. caps.eCodecType = params->CodecType;
  91. caps.eChromaFormat = params->ChromaFormat;
  92. caps.nBitDepthMinus8 = params->bitDepthMinus8;
  93. if (!decoder->cvdl->cuvidGetDecoderCaps) {
  94. av_log(logctx, AV_LOG_WARNING, "Used Nvidia driver is too old to perform a capability check.\n");
  95. av_log(logctx, AV_LOG_WARNING, "The minimum required version is "
  96. #if defined(_WIN32) || defined(__CYGWIN__)
  97. "378.66"
  98. #else
  99. "378.13"
  100. #endif
  101. ". Continuing blind.\n");
  102. return 0;
  103. }
  104. ret = CHECK_CU(decoder->cvdl->cuvidGetDecoderCaps(&caps));
  105. if (ret < 0)
  106. return ret;
  107. av_log(logctx, AV_LOG_VERBOSE, "NVDEC capabilities:\n");
  108. av_log(logctx, AV_LOG_VERBOSE, "format supported: %s, max_mb_count: %d\n",
  109. caps.bIsSupported ? "yes" : "no", caps.nMaxMBCount);
  110. av_log(logctx, AV_LOG_VERBOSE, "min_width: %d, max_width: %d\n",
  111. caps.nMinWidth, caps.nMaxWidth);
  112. av_log(logctx, AV_LOG_VERBOSE, "min_height: %d, max_height: %d\n",
  113. caps.nMinHeight, caps.nMaxHeight);
  114. if (!caps.bIsSupported) {
  115. av_log(logctx, AV_LOG_ERROR, "Hardware is lacking required capabilities\n");
  116. return AVERROR(EINVAL);
  117. }
  118. if (params->ulWidth > caps.nMaxWidth || params->ulWidth < caps.nMinWidth) {
  119. av_log(logctx, AV_LOG_ERROR, "Video width %d not within range from %d to %d\n",
  120. (int)params->ulWidth, caps.nMinWidth, caps.nMaxWidth);
  121. return AVERROR(EINVAL);
  122. }
  123. if (params->ulHeight > caps.nMaxHeight || params->ulHeight < caps.nMinHeight) {
  124. av_log(logctx, AV_LOG_ERROR, "Video height %d not within range from %d to %d\n",
  125. (int)params->ulHeight, caps.nMinHeight, caps.nMaxHeight);
  126. return AVERROR(EINVAL);
  127. }
  128. if ((params->ulWidth * params->ulHeight) / 256 > caps.nMaxMBCount) {
  129. av_log(logctx, AV_LOG_ERROR, "Video macroblock count %d exceeds maximum of %d\n",
  130. (int)(params->ulWidth * params->ulHeight) / 256, caps.nMaxMBCount);
  131. return AVERROR(EINVAL);
  132. }
  133. return 0;
  134. }
  135. static void nvdec_decoder_free(void *opaque, uint8_t *data)
  136. {
  137. NVDECDecoder *decoder = (NVDECDecoder*)data;
  138. if (decoder->decoder) {
  139. void *logctx = decoder->hw_device_ref->data;
  140. CUcontext dummy;
  141. CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
  142. CHECK_CU(decoder->cvdl->cuvidDestroyDecoder(decoder->decoder));
  143. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  144. }
  145. av_buffer_unref(&decoder->real_hw_frames_ref);
  146. av_buffer_unref(&decoder->hw_device_ref);
  147. cuvid_free_functions(&decoder->cvdl);
  148. av_freep(&decoder);
  149. }
  150. static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref,
  151. CUVIDDECODECREATEINFO *params, void *logctx)
  152. {
  153. AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)hw_device_ref->data;
  154. AVCUDADeviceContext *device_hwctx = hw_device_ctx->hwctx;
  155. AVBufferRef *decoder_ref;
  156. NVDECDecoder *decoder;
  157. CUcontext dummy;
  158. int ret;
  159. decoder = av_mallocz(sizeof(*decoder));
  160. if (!decoder)
  161. return AVERROR(ENOMEM);
  162. decoder_ref = av_buffer_create((uint8_t*)decoder, sizeof(*decoder),
  163. nvdec_decoder_free, NULL, AV_BUFFER_FLAG_READONLY);
  164. if (!decoder_ref) {
  165. av_freep(&decoder);
  166. return AVERROR(ENOMEM);
  167. }
  168. decoder->hw_device_ref = av_buffer_ref(hw_device_ref);
  169. if (!decoder->hw_device_ref) {
  170. ret = AVERROR(ENOMEM);
  171. goto fail;
  172. }
  173. decoder->cuda_ctx = device_hwctx->cuda_ctx;
  174. decoder->cudl = device_hwctx->internal->cuda_dl;
  175. decoder->stream = device_hwctx->stream;
  176. ret = cuvid_load_functions(&decoder->cvdl, logctx);
  177. if (ret < 0) {
  178. av_log(logctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n");
  179. goto fail;
  180. }
  181. ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
  182. if (ret < 0)
  183. goto fail;
  184. ret = nvdec_test_capabilities(decoder, params, logctx);
  185. if (ret < 0) {
  186. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  187. goto fail;
  188. }
  189. ret = CHECK_CU(decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params));
  190. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  191. if (ret < 0) {
  192. goto fail;
  193. }
  194. *out = decoder_ref;
  195. return 0;
  196. fail:
  197. av_buffer_unref(&decoder_ref);
  198. return ret;
  199. }
  200. static AVBufferRef *nvdec_decoder_frame_alloc(void *opaque, buffer_size_t size)
  201. {
  202. NVDECFramePool *pool = opaque;
  203. AVBufferRef *ret;
  204. if (pool->nb_allocated >= pool->dpb_size)
  205. return NULL;
  206. ret = av_buffer_alloc(sizeof(unsigned int));
  207. if (!ret)
  208. return NULL;
  209. *(unsigned int*)ret->data = pool->nb_allocated++;
  210. return ret;
  211. }
  212. int ff_nvdec_decode_uninit(AVCodecContext *avctx)
  213. {
  214. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  215. av_freep(&ctx->bitstream);
  216. av_freep(&ctx->bitstream_internal);
  217. ctx->bitstream_len = 0;
  218. ctx->bitstream_allocated = 0;
  219. av_freep(&ctx->slice_offsets);
  220. ctx->nb_slices = 0;
  221. ctx->slice_offsets_allocated = 0;
  222. av_buffer_unref(&ctx->decoder_ref);
  223. av_buffer_pool_uninit(&ctx->decoder_pool);
  224. return 0;
  225. }
  226. static void nvdec_free_dummy(struct AVHWFramesContext *ctx)
  227. {
  228. av_buffer_pool_uninit(&ctx->pool);
  229. }
  230. static AVBufferRef *nvdec_alloc_dummy(buffer_size_t size)
  231. {
  232. return av_buffer_create(NULL, 0, NULL, NULL, 0);
  233. }
  234. static int nvdec_init_hwframes(AVCodecContext *avctx, AVBufferRef **out_frames_ref, int dummy)
  235. {
  236. AVHWFramesContext *frames_ctx;
  237. int ret;
  238. ret = avcodec_get_hw_frames_parameters(avctx,
  239. avctx->hw_device_ctx,
  240. avctx->hwaccel->pix_fmt,
  241. out_frames_ref);
  242. if (ret < 0)
  243. return ret;
  244. frames_ctx = (AVHWFramesContext*)(*out_frames_ref)->data;
  245. if (dummy) {
  246. // Copied from ff_decode_get_hw_frames_ctx for compatibility
  247. frames_ctx->initial_pool_size += 3;
  248. frames_ctx->free = nvdec_free_dummy;
  249. frames_ctx->pool = av_buffer_pool_init(0, nvdec_alloc_dummy);
  250. if (!frames_ctx->pool) {
  251. av_buffer_unref(out_frames_ref);
  252. return AVERROR(ENOMEM);
  253. }
  254. } else {
  255. // This is normally not used to actually allocate frames from
  256. frames_ctx->initial_pool_size = 0;
  257. }
  258. ret = av_hwframe_ctx_init(*out_frames_ref);
  259. if (ret < 0) {
  260. av_buffer_unref(out_frames_ref);
  261. return ret;
  262. }
  263. return 0;
  264. }
  265. int ff_nvdec_decode_init(AVCodecContext *avctx)
  266. {
  267. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  268. NVDECDecoder *decoder;
  269. AVBufferRef *real_hw_frames_ref;
  270. NVDECFramePool *pool;
  271. AVHWFramesContext *frames_ctx;
  272. const AVPixFmtDescriptor *sw_desc;
  273. CUVIDDECODECREATEINFO params = { 0 };
  274. cudaVideoSurfaceFormat output_format;
  275. int cuvid_codec_type, cuvid_chroma_format, chroma_444;
  276. int ret = 0;
  277. sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  278. if (!sw_desc)
  279. return AVERROR_BUG;
  280. cuvid_codec_type = map_avcodec_id(avctx->codec_id);
  281. if (cuvid_codec_type < 0) {
  282. av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
  283. return AVERROR_BUG;
  284. }
  285. cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
  286. if (cuvid_chroma_format < 0) {
  287. av_log(avctx, AV_LOG_ERROR, "Unsupported chroma format\n");
  288. return AVERROR(ENOSYS);
  289. }
  290. chroma_444 = ctx->supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
  291. if (!avctx->hw_frames_ctx) {
  292. ret = nvdec_init_hwframes(avctx, &avctx->hw_frames_ctx, 1);
  293. if (ret < 0)
  294. return ret;
  295. ret = nvdec_init_hwframes(avctx, &real_hw_frames_ref, 0);
  296. if (ret < 0)
  297. return ret;
  298. } else {
  299. real_hw_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
  300. if (!real_hw_frames_ref)
  301. return AVERROR(ENOMEM);
  302. }
  303. switch (sw_desc->comp[0].depth) {
  304. case 8:
  305. output_format = chroma_444 ? cudaVideoSurfaceFormat_YUV444 :
  306. cudaVideoSurfaceFormat_NV12;
  307. break;
  308. case 10:
  309. case 12:
  310. output_format = chroma_444 ? cudaVideoSurfaceFormat_YUV444_16Bit :
  311. cudaVideoSurfaceFormat_P016;
  312. break;
  313. default:
  314. av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth\n");
  315. av_buffer_unref(&real_hw_frames_ref);
  316. return AVERROR(ENOSYS);
  317. }
  318. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  319. params.ulWidth = avctx->coded_width;
  320. params.ulHeight = avctx->coded_height;
  321. params.ulTargetWidth = avctx->coded_width;
  322. params.ulTargetHeight = avctx->coded_height;
  323. params.bitDepthMinus8 = sw_desc->comp[0].depth - 8;
  324. params.OutputFormat = output_format;
  325. params.CodecType = cuvid_codec_type;
  326. params.ChromaFormat = cuvid_chroma_format;
  327. params.ulNumDecodeSurfaces = frames_ctx->initial_pool_size;
  328. params.ulNumOutputSurfaces = frames_ctx->initial_pool_size;
  329. ret = nvdec_decoder_create(&ctx->decoder_ref, frames_ctx->device_ref, &params, avctx);
  330. if (ret < 0) {
  331. if (params.ulNumDecodeSurfaces > 32) {
  332. av_log(avctx, AV_LOG_WARNING, "Using more than 32 (%d) decode surfaces might cause nvdec to fail.\n",
  333. (int)params.ulNumDecodeSurfaces);
  334. av_log(avctx, AV_LOG_WARNING, "Try lowering the amount of threads. Using %d right now.\n",
  335. avctx->thread_count);
  336. }
  337. av_buffer_unref(&real_hw_frames_ref);
  338. return ret;
  339. }
  340. decoder = (NVDECDecoder*)ctx->decoder_ref->data;
  341. decoder->real_hw_frames_ref = real_hw_frames_ref;
  342. real_hw_frames_ref = NULL;
  343. pool = av_mallocz(sizeof(*pool));
  344. if (!pool) {
  345. ret = AVERROR(ENOMEM);
  346. goto fail;
  347. }
  348. pool->dpb_size = frames_ctx->initial_pool_size;
  349. ctx->decoder_pool = av_buffer_pool_init2(sizeof(int), pool,
  350. nvdec_decoder_frame_alloc, av_free);
  351. if (!ctx->decoder_pool) {
  352. ret = AVERROR(ENOMEM);
  353. goto fail;
  354. }
  355. return 0;
  356. fail:
  357. ff_nvdec_decode_uninit(avctx);
  358. return ret;
  359. }
  360. static void nvdec_fdd_priv_free(void *priv)
  361. {
  362. NVDECFrame *cf = priv;
  363. if (!cf)
  364. return;
  365. av_buffer_unref(&cf->idx_ref);
  366. av_buffer_unref(&cf->decoder_ref);
  367. av_buffer_unref(&cf->ref_idx_ref);
  368. av_freep(&priv);
  369. }
  370. static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
  371. {
  372. NVDECFrame *unmap_data = (NVDECFrame*)data;
  373. NVDECDecoder *decoder = (NVDECDecoder*)unmap_data->decoder_ref->data;
  374. void *logctx = decoder->hw_device_ref->data;
  375. CUdeviceptr devptr = (CUdeviceptr)opaque;
  376. int ret;
  377. CUcontext dummy;
  378. ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
  379. if (ret < 0)
  380. goto finish;
  381. CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
  382. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  383. finish:
  384. av_buffer_unref(&unmap_data->idx_ref);
  385. av_buffer_unref(&unmap_data->decoder_ref);
  386. av_buffer_unref(&unmap_data->ref_idx_ref);
  387. av_free(unmap_data);
  388. }
  389. static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
  390. {
  391. FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
  392. NVDECFrame *cf = (NVDECFrame*)fdd->hwaccel_priv;
  393. NVDECDecoder *decoder = (NVDECDecoder*)cf->decoder_ref->data;
  394. AVHWFramesContext *hwctx = (AVHWFramesContext *)frame->hw_frames_ctx->data;
  395. CUVIDPROCPARAMS vpp = { 0 };
  396. NVDECFrame *unmap_data = NULL;
  397. CUcontext dummy;
  398. CUdeviceptr devptr;
  399. unsigned int pitch, i;
  400. unsigned int offset = 0;
  401. int shift_h = 0, shift_v = 0;
  402. int ret = 0;
  403. vpp.progressive_frame = 1;
  404. vpp.output_stream = decoder->stream;
  405. ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
  406. if (ret < 0)
  407. return ret;
  408. ret = CHECK_CU(decoder->cvdl->cuvidMapVideoFrame(decoder->decoder,
  409. cf->idx, &devptr,
  410. &pitch, &vpp));
  411. if (ret < 0)
  412. goto finish;
  413. unmap_data = av_mallocz(sizeof(*unmap_data));
  414. if (!unmap_data) {
  415. ret = AVERROR(ENOMEM);
  416. goto copy_fail;
  417. }
  418. frame->buf[1] = av_buffer_create((uint8_t *)unmap_data, sizeof(*unmap_data),
  419. nvdec_unmap_mapped_frame, (void*)devptr,
  420. AV_BUFFER_FLAG_READONLY);
  421. if (!frame->buf[1]) {
  422. ret = AVERROR(ENOMEM);
  423. goto copy_fail;
  424. }
  425. av_buffer_unref(&frame->hw_frames_ctx);
  426. frame->hw_frames_ctx = av_buffer_ref(decoder->real_hw_frames_ref);
  427. if (!frame->hw_frames_ctx) {
  428. ret = AVERROR(ENOMEM);
  429. goto copy_fail;
  430. }
  431. unmap_data->idx = cf->idx;
  432. unmap_data->idx_ref = av_buffer_ref(cf->idx_ref);
  433. unmap_data->decoder_ref = av_buffer_ref(cf->decoder_ref);
  434. av_pix_fmt_get_chroma_sub_sample(hwctx->sw_format, &shift_h, &shift_v);
  435. for (i = 0; frame->linesize[i]; i++) {
  436. frame->data[i] = (uint8_t*)(devptr + offset);
  437. frame->linesize[i] = pitch;
  438. offset += pitch * (frame->height >> (i ? shift_v : 0));
  439. }
  440. goto finish;
  441. copy_fail:
  442. if (!frame->buf[1]) {
  443. CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
  444. av_freep(&unmap_data);
  445. } else {
  446. av_buffer_unref(&frame->buf[1]);
  447. }
  448. finish:
  449. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  450. return ret;
  451. }
  452. int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
  453. {
  454. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  455. FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
  456. NVDECFrame *cf = NULL;
  457. int ret;
  458. ctx->bitstream_len = 0;
  459. ctx->nb_slices = 0;
  460. if (fdd->hwaccel_priv)
  461. return 0;
  462. cf = av_mallocz(sizeof(*cf));
  463. if (!cf)
  464. return AVERROR(ENOMEM);
  465. cf->decoder_ref = av_buffer_ref(ctx->decoder_ref);
  466. if (!cf->decoder_ref) {
  467. ret = AVERROR(ENOMEM);
  468. goto fail;
  469. }
  470. cf->idx_ref = av_buffer_pool_get(ctx->decoder_pool);
  471. if (!cf->idx_ref) {
  472. av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
  473. ret = AVERROR(ENOMEM);
  474. goto fail;
  475. }
  476. cf->ref_idx = cf->idx = *(unsigned int*)cf->idx_ref->data;
  477. fdd->hwaccel_priv = cf;
  478. fdd->hwaccel_priv_free = nvdec_fdd_priv_free;
  479. fdd->post_process = nvdec_retrieve_data;
  480. return 0;
  481. fail:
  482. nvdec_fdd_priv_free(cf);
  483. return ret;
  484. }
  485. int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_sep_ref)
  486. {
  487. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  488. FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
  489. NVDECFrame *cf;
  490. int ret;
  491. ret = ff_nvdec_start_frame(avctx, frame);
  492. if (ret < 0)
  493. return ret;
  494. cf = fdd->hwaccel_priv;
  495. if (has_sep_ref) {
  496. if (!cf->ref_idx_ref) {
  497. cf->ref_idx_ref = av_buffer_pool_get(ctx->decoder_pool);
  498. if (!cf->ref_idx_ref) {
  499. av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
  500. ret = AVERROR(ENOMEM);
  501. goto fail;
  502. }
  503. }
  504. cf->ref_idx = *(unsigned int*)cf->ref_idx_ref->data;
  505. } else {
  506. av_buffer_unref(&cf->ref_idx_ref);
  507. cf->ref_idx = cf->idx;
  508. }
  509. return 0;
  510. fail:
  511. nvdec_fdd_priv_free(cf);
  512. return ret;
  513. }
  514. int ff_nvdec_end_frame(AVCodecContext *avctx)
  515. {
  516. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  517. NVDECDecoder *decoder = (NVDECDecoder*)ctx->decoder_ref->data;
  518. void *logctx = avctx;
  519. CUVIDPICPARAMS *pp = &ctx->pic_params;
  520. CUcontext dummy;
  521. int ret = 0;
  522. pp->nBitstreamDataLen = ctx->bitstream_len;
  523. pp->pBitstreamData = ctx->bitstream;
  524. pp->nNumSlices = ctx->nb_slices;
  525. pp->pSliceDataOffsets = ctx->slice_offsets;
  526. ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
  527. if (ret < 0)
  528. return ret;
  529. ret = CHECK_CU(decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params));
  530. if (ret < 0)
  531. goto finish;
  532. finish:
  533. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  534. return ret;
  535. }
  536. int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
  537. {
  538. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  539. int ret = ff_nvdec_end_frame(avctx);
  540. ctx->bitstream = NULL;
  541. return ret;
  542. }
  543. int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer,
  544. uint32_t size)
  545. {
  546. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  547. void *tmp;
  548. tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
  549. (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
  550. if (!tmp)
  551. return AVERROR(ENOMEM);
  552. ctx->slice_offsets = tmp;
  553. if (!ctx->bitstream)
  554. ctx->bitstream = (uint8_t*)buffer;
  555. ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
  556. ctx->bitstream_len += size;
  557. ctx->nb_slices++;
  558. return 0;
  559. }
  560. int ff_nvdec_frame_params(AVCodecContext *avctx,
  561. AVBufferRef *hw_frames_ctx,
  562. int dpb_size,
  563. int supports_444)
  564. {
  565. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
  566. const AVPixFmtDescriptor *sw_desc;
  567. int cuvid_codec_type, cuvid_chroma_format, chroma_444;
  568. sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  569. if (!sw_desc)
  570. return AVERROR_BUG;
  571. cuvid_codec_type = map_avcodec_id(avctx->codec_id);
  572. if (cuvid_codec_type < 0) {
  573. av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
  574. return AVERROR_BUG;
  575. }
  576. cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
  577. if (cuvid_chroma_format < 0) {
  578. av_log(avctx, AV_LOG_VERBOSE, "Unsupported chroma format\n");
  579. return AVERROR(EINVAL);
  580. }
  581. chroma_444 = supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
  582. frames_ctx->format = AV_PIX_FMT_CUDA;
  583. frames_ctx->width = (avctx->coded_width + 1) & ~1;
  584. frames_ctx->height = (avctx->coded_height + 1) & ~1;
  585. /*
  586. * We add two extra frames to the pool to account for deinterlacing filters
  587. * holding onto their frames.
  588. */
  589. frames_ctx->initial_pool_size = dpb_size + 2;
  590. switch (sw_desc->comp[0].depth) {
  591. case 8:
  592. frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_NV12;
  593. break;
  594. case 10:
  595. frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P010;
  596. break;
  597. case 12:
  598. frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P016;
  599. break;
  600. default:
  601. return AVERROR(EINVAL);
  602. }
  603. return 0;
  604. }
  605. int ff_nvdec_get_ref_idx(AVFrame *frame)
  606. {
  607. FrameDecodeData *fdd;
  608. NVDECFrame *cf;
  609. if (!frame || !frame->private_ref)
  610. return -1;
  611. fdd = (FrameDecodeData*)frame->private_ref->data;
  612. cf = (NVDECFrame*)fdd->hwaccel_priv;
  613. if (!cf)
  614. return -1;
  615. return cf->ref_idx;
  616. }