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.

661 lines
19KB

  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. CUcontext cuda_ctx;
  42. CUstream stream;
  43. CudaFunctions *cudl;
  44. CuvidFunctions *cvdl;
  45. } NVDECDecoder;
  46. typedef struct NVDECFramePool {
  47. unsigned int dpb_size;
  48. unsigned int nb_allocated;
  49. } NVDECFramePool;
  50. #define CHECK_CU(x) FF_CUDA_CHECK_DL(logctx, decoder->cudl, x)
  51. static int map_avcodec_id(enum AVCodecID id)
  52. {
  53. switch (id) {
  54. case AV_CODEC_ID_H264: return cudaVideoCodec_H264;
  55. case AV_CODEC_ID_HEVC: return cudaVideoCodec_HEVC;
  56. case AV_CODEC_ID_MJPEG: return cudaVideoCodec_JPEG;
  57. case AV_CODEC_ID_MPEG1VIDEO: return cudaVideoCodec_MPEG1;
  58. case AV_CODEC_ID_MPEG2VIDEO: return cudaVideoCodec_MPEG2;
  59. case AV_CODEC_ID_MPEG4: return cudaVideoCodec_MPEG4;
  60. case AV_CODEC_ID_VC1: return cudaVideoCodec_VC1;
  61. case AV_CODEC_ID_VP8: return cudaVideoCodec_VP8;
  62. case AV_CODEC_ID_VP9: return cudaVideoCodec_VP9;
  63. case AV_CODEC_ID_WMV3: return cudaVideoCodec_VC1;
  64. }
  65. return -1;
  66. }
  67. static int map_chroma_format(enum AVPixelFormat pix_fmt)
  68. {
  69. int shift_h = 0, shift_v = 0;
  70. av_pix_fmt_get_chroma_sub_sample(pix_fmt, &shift_h, &shift_v);
  71. if (shift_h == 1 && shift_v == 1)
  72. return cudaVideoChromaFormat_420;
  73. else if (shift_h == 1 && shift_v == 0)
  74. return cudaVideoChromaFormat_422;
  75. else if (shift_h == 0 && shift_v == 0)
  76. return cudaVideoChromaFormat_444;
  77. return -1;
  78. }
  79. static int nvdec_test_capabilities(NVDECDecoder *decoder,
  80. CUVIDDECODECREATEINFO *params, void *logctx)
  81. {
  82. int ret;
  83. CUVIDDECODECAPS caps = { 0 };
  84. caps.eCodecType = params->CodecType;
  85. caps.eChromaFormat = params->ChromaFormat;
  86. caps.nBitDepthMinus8 = params->bitDepthMinus8;
  87. if (!decoder->cvdl->cuvidGetDecoderCaps) {
  88. av_log(logctx, AV_LOG_WARNING, "Used Nvidia driver is too old to perform a capability check.\n");
  89. av_log(logctx, AV_LOG_WARNING, "The minimum required version is "
  90. #if defined(_WIN32) || defined(__CYGWIN__)
  91. "378.66"
  92. #else
  93. "378.13"
  94. #endif
  95. ". Continuing blind.\n");
  96. return 0;
  97. }
  98. ret = CHECK_CU(decoder->cvdl->cuvidGetDecoderCaps(&caps));
  99. if (ret < 0)
  100. return ret;
  101. av_log(logctx, AV_LOG_VERBOSE, "NVDEC capabilities:\n");
  102. av_log(logctx, AV_LOG_VERBOSE, "format supported: %s, max_mb_count: %d\n",
  103. caps.bIsSupported ? "yes" : "no", caps.nMaxMBCount);
  104. av_log(logctx, AV_LOG_VERBOSE, "min_width: %d, max_width: %d\n",
  105. caps.nMinWidth, caps.nMaxWidth);
  106. av_log(logctx, AV_LOG_VERBOSE, "min_height: %d, max_height: %d\n",
  107. caps.nMinHeight, caps.nMaxHeight);
  108. if (!caps.bIsSupported) {
  109. av_log(logctx, AV_LOG_ERROR, "Hardware is lacking required capabilities\n");
  110. return AVERROR(EINVAL);
  111. }
  112. if (params->ulWidth > caps.nMaxWidth || params->ulWidth < caps.nMinWidth) {
  113. av_log(logctx, AV_LOG_ERROR, "Video width %d not within range from %d to %d\n",
  114. (int)params->ulWidth, caps.nMinWidth, caps.nMaxWidth);
  115. return AVERROR(EINVAL);
  116. }
  117. if (params->ulHeight > caps.nMaxHeight || params->ulHeight < caps.nMinHeight) {
  118. av_log(logctx, AV_LOG_ERROR, "Video height %d not within range from %d to %d\n",
  119. (int)params->ulHeight, caps.nMinHeight, caps.nMaxHeight);
  120. return AVERROR(EINVAL);
  121. }
  122. if ((params->ulWidth * params->ulHeight) / 256 > caps.nMaxMBCount) {
  123. av_log(logctx, AV_LOG_ERROR, "Video macroblock count %d exceeds maximum of %d\n",
  124. (int)(params->ulWidth * params->ulHeight) / 256, caps.nMaxMBCount);
  125. return AVERROR(EINVAL);
  126. }
  127. return 0;
  128. }
  129. static void nvdec_decoder_free(void *opaque, uint8_t *data)
  130. {
  131. NVDECDecoder *decoder = (NVDECDecoder*)data;
  132. if (decoder->decoder) {
  133. void *logctx = decoder->hw_device_ref->data;
  134. CUcontext dummy;
  135. CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
  136. CHECK_CU(decoder->cvdl->cuvidDestroyDecoder(decoder->decoder));
  137. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  138. }
  139. av_buffer_unref(&decoder->hw_device_ref);
  140. cuvid_free_functions(&decoder->cvdl);
  141. av_freep(&decoder);
  142. }
  143. static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref,
  144. CUVIDDECODECREATEINFO *params, void *logctx)
  145. {
  146. AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)hw_device_ref->data;
  147. AVCUDADeviceContext *device_hwctx = hw_device_ctx->hwctx;
  148. AVBufferRef *decoder_ref;
  149. NVDECDecoder *decoder;
  150. CUcontext dummy;
  151. int ret;
  152. decoder = av_mallocz(sizeof(*decoder));
  153. if (!decoder)
  154. return AVERROR(ENOMEM);
  155. decoder_ref = av_buffer_create((uint8_t*)decoder, sizeof(*decoder),
  156. nvdec_decoder_free, NULL, AV_BUFFER_FLAG_READONLY);
  157. if (!decoder_ref) {
  158. av_freep(&decoder);
  159. return AVERROR(ENOMEM);
  160. }
  161. decoder->hw_device_ref = av_buffer_ref(hw_device_ref);
  162. if (!decoder->hw_device_ref) {
  163. ret = AVERROR(ENOMEM);
  164. goto fail;
  165. }
  166. decoder->cuda_ctx = device_hwctx->cuda_ctx;
  167. decoder->cudl = device_hwctx->internal->cuda_dl;
  168. decoder->stream = device_hwctx->stream;
  169. ret = cuvid_load_functions(&decoder->cvdl, logctx);
  170. if (ret < 0) {
  171. av_log(logctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n");
  172. goto fail;
  173. }
  174. ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
  175. if (ret < 0)
  176. goto fail;
  177. ret = nvdec_test_capabilities(decoder, params, logctx);
  178. if (ret < 0) {
  179. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  180. goto fail;
  181. }
  182. ret = CHECK_CU(decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params));
  183. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  184. if (ret < 0) {
  185. goto fail;
  186. }
  187. *out = decoder_ref;
  188. return 0;
  189. fail:
  190. av_buffer_unref(&decoder_ref);
  191. return ret;
  192. }
  193. static AVBufferRef *nvdec_decoder_frame_alloc(void *opaque, int size)
  194. {
  195. NVDECFramePool *pool = opaque;
  196. AVBufferRef *ret;
  197. if (pool->nb_allocated >= pool->dpb_size)
  198. return NULL;
  199. ret = av_buffer_alloc(sizeof(unsigned int));
  200. if (!ret)
  201. return NULL;
  202. *(unsigned int*)ret->data = pool->nb_allocated++;
  203. return ret;
  204. }
  205. int ff_nvdec_decode_uninit(AVCodecContext *avctx)
  206. {
  207. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  208. av_freep(&ctx->bitstream);
  209. ctx->bitstream_len = 0;
  210. ctx->bitstream_allocated = 0;
  211. av_freep(&ctx->slice_offsets);
  212. ctx->nb_slices = 0;
  213. ctx->slice_offsets_allocated = 0;
  214. av_buffer_unref(&ctx->decoder_ref);
  215. av_buffer_pool_uninit(&ctx->decoder_pool);
  216. return 0;
  217. }
  218. int ff_nvdec_decode_init(AVCodecContext *avctx)
  219. {
  220. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  221. NVDECFramePool *pool;
  222. AVHWFramesContext *frames_ctx;
  223. const AVPixFmtDescriptor *sw_desc;
  224. CUVIDDECODECREATEINFO params = { 0 };
  225. cudaVideoSurfaceFormat output_format;
  226. int cuvid_codec_type, cuvid_chroma_format, chroma_444;
  227. int ret = 0;
  228. sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  229. if (!sw_desc)
  230. return AVERROR_BUG;
  231. cuvid_codec_type = map_avcodec_id(avctx->codec_id);
  232. if (cuvid_codec_type < 0) {
  233. av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
  234. return AVERROR_BUG;
  235. }
  236. cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
  237. if (cuvid_chroma_format < 0) {
  238. av_log(avctx, AV_LOG_ERROR, "Unsupported chroma format\n");
  239. return AVERROR(ENOSYS);
  240. }
  241. chroma_444 = ctx->supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
  242. if (!avctx->hw_frames_ctx) {
  243. ret = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_CUDA);
  244. if (ret < 0)
  245. return ret;
  246. }
  247. switch (sw_desc->comp[0].depth) {
  248. case 8:
  249. output_format = chroma_444 ? cudaVideoSurfaceFormat_YUV444 :
  250. cudaVideoSurfaceFormat_NV12;
  251. break;
  252. case 10:
  253. case 12:
  254. output_format = chroma_444 ? cudaVideoSurfaceFormat_YUV444_16Bit :
  255. cudaVideoSurfaceFormat_P016;
  256. break;
  257. default:
  258. av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth\n");
  259. return AVERROR(ENOSYS);
  260. }
  261. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  262. params.ulWidth = avctx->coded_width;
  263. params.ulHeight = avctx->coded_height;
  264. params.ulTargetWidth = avctx->coded_width;
  265. params.ulTargetHeight = avctx->coded_height;
  266. params.bitDepthMinus8 = sw_desc->comp[0].depth - 8;
  267. params.OutputFormat = output_format;
  268. params.CodecType = cuvid_codec_type;
  269. params.ChromaFormat = cuvid_chroma_format;
  270. params.ulNumDecodeSurfaces = frames_ctx->initial_pool_size;
  271. params.ulNumOutputSurfaces = frames_ctx->initial_pool_size;
  272. ret = nvdec_decoder_create(&ctx->decoder_ref, frames_ctx->device_ref, &params, avctx);
  273. if (ret < 0) {
  274. if (params.ulNumDecodeSurfaces > 32) {
  275. av_log(avctx, AV_LOG_WARNING, "Using more than 32 (%d) decode surfaces might cause nvdec to fail.\n",
  276. (int)params.ulNumDecodeSurfaces);
  277. av_log(avctx, AV_LOG_WARNING, "Try lowering the amount of threads. Using %d right now.\n",
  278. avctx->thread_count);
  279. }
  280. return ret;
  281. }
  282. pool = av_mallocz(sizeof(*pool));
  283. if (!pool) {
  284. ret = AVERROR(ENOMEM);
  285. goto fail;
  286. }
  287. pool->dpb_size = frames_ctx->initial_pool_size;
  288. ctx->decoder_pool = av_buffer_pool_init2(sizeof(int), pool,
  289. nvdec_decoder_frame_alloc, av_free);
  290. if (!ctx->decoder_pool) {
  291. ret = AVERROR(ENOMEM);
  292. goto fail;
  293. }
  294. return 0;
  295. fail:
  296. ff_nvdec_decode_uninit(avctx);
  297. return ret;
  298. }
  299. static void nvdec_fdd_priv_free(void *priv)
  300. {
  301. NVDECFrame *cf = priv;
  302. if (!cf)
  303. return;
  304. av_buffer_unref(&cf->idx_ref);
  305. av_buffer_unref(&cf->decoder_ref);
  306. av_freep(&priv);
  307. }
  308. static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
  309. {
  310. NVDECFrame *unmap_data = (NVDECFrame*)data;
  311. NVDECDecoder *decoder = (NVDECDecoder*)unmap_data->decoder_ref->data;
  312. void *logctx = decoder->hw_device_ref->data;
  313. CUdeviceptr devptr = (CUdeviceptr)opaque;
  314. int ret;
  315. CUcontext dummy;
  316. ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
  317. if (ret < 0)
  318. goto finish;
  319. CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
  320. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  321. finish:
  322. av_buffer_unref(&unmap_data->idx_ref);
  323. av_buffer_unref(&unmap_data->decoder_ref);
  324. av_free(unmap_data);
  325. }
  326. static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
  327. {
  328. FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
  329. NVDECFrame *cf = (NVDECFrame*)fdd->hwaccel_priv;
  330. NVDECDecoder *decoder = (NVDECDecoder*)cf->decoder_ref->data;
  331. AVHWFramesContext *hwctx = (AVHWFramesContext *)frame->hw_frames_ctx->data;
  332. CUVIDPROCPARAMS vpp = { 0 };
  333. NVDECFrame *unmap_data = NULL;
  334. CUcontext dummy;
  335. CUdeviceptr devptr;
  336. unsigned int pitch, i;
  337. unsigned int offset = 0;
  338. int shift_h = 0, shift_v = 0;
  339. int ret = 0;
  340. vpp.progressive_frame = 1;
  341. vpp.output_stream = decoder->stream;
  342. ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
  343. if (ret < 0)
  344. return ret;
  345. ret = CHECK_CU(decoder->cvdl->cuvidMapVideoFrame(decoder->decoder,
  346. cf->idx, &devptr,
  347. &pitch, &vpp));
  348. if (ret < 0)
  349. goto finish;
  350. unmap_data = av_mallocz(sizeof(*unmap_data));
  351. if (!unmap_data) {
  352. ret = AVERROR(ENOMEM);
  353. goto copy_fail;
  354. }
  355. frame->buf[1] = av_buffer_create((uint8_t *)unmap_data, sizeof(*unmap_data),
  356. nvdec_unmap_mapped_frame, (void*)devptr,
  357. AV_BUFFER_FLAG_READONLY);
  358. if (!frame->buf[1]) {
  359. ret = AVERROR(ENOMEM);
  360. goto copy_fail;
  361. }
  362. unmap_data->idx = cf->idx;
  363. unmap_data->idx_ref = av_buffer_ref(cf->idx_ref);
  364. unmap_data->decoder_ref = av_buffer_ref(cf->decoder_ref);
  365. av_pix_fmt_get_chroma_sub_sample(hwctx->sw_format, &shift_h, &shift_v);
  366. for (i = 0; frame->linesize[i]; i++) {
  367. frame->data[i] = (uint8_t*)(devptr + offset);
  368. frame->linesize[i] = pitch;
  369. offset += pitch * (frame->height >> (i ? shift_v : 0));
  370. }
  371. goto finish;
  372. copy_fail:
  373. if (!frame->buf[1]) {
  374. CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
  375. av_freep(&unmap_data);
  376. } else {
  377. av_buffer_unref(&frame->buf[1]);
  378. }
  379. finish:
  380. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  381. return ret;
  382. }
  383. int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
  384. {
  385. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  386. FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
  387. NVDECFrame *cf = NULL;
  388. int ret;
  389. ctx->bitstream_len = 0;
  390. ctx->nb_slices = 0;
  391. if (fdd->hwaccel_priv)
  392. return 0;
  393. cf = av_mallocz(sizeof(*cf));
  394. if (!cf)
  395. return AVERROR(ENOMEM);
  396. cf->decoder_ref = av_buffer_ref(ctx->decoder_ref);
  397. if (!cf->decoder_ref) {
  398. ret = AVERROR(ENOMEM);
  399. goto fail;
  400. }
  401. cf->idx_ref = av_buffer_pool_get(ctx->decoder_pool);
  402. if (!cf->idx_ref) {
  403. av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
  404. ret = AVERROR(ENOMEM);
  405. goto fail;
  406. }
  407. cf->idx = *(unsigned int*)cf->idx_ref->data;
  408. fdd->hwaccel_priv = cf;
  409. fdd->hwaccel_priv_free = nvdec_fdd_priv_free;
  410. fdd->post_process = nvdec_retrieve_data;
  411. return 0;
  412. fail:
  413. nvdec_fdd_priv_free(cf);
  414. return ret;
  415. }
  416. int ff_nvdec_end_frame(AVCodecContext *avctx)
  417. {
  418. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  419. NVDECDecoder *decoder = (NVDECDecoder*)ctx->decoder_ref->data;
  420. void *logctx = avctx;
  421. CUVIDPICPARAMS *pp = &ctx->pic_params;
  422. CUcontext dummy;
  423. int ret = 0;
  424. pp->nBitstreamDataLen = ctx->bitstream_len;
  425. pp->pBitstreamData = ctx->bitstream;
  426. pp->nNumSlices = ctx->nb_slices;
  427. pp->pSliceDataOffsets = ctx->slice_offsets;
  428. ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
  429. if (ret < 0)
  430. return ret;
  431. ret = CHECK_CU(decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params));
  432. if (ret < 0)
  433. goto finish;
  434. finish:
  435. CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
  436. return ret;
  437. }
  438. int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
  439. {
  440. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  441. int ret = ff_nvdec_end_frame(avctx);
  442. ctx->bitstream = NULL;
  443. return ret;
  444. }
  445. int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer,
  446. uint32_t size)
  447. {
  448. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  449. void *tmp;
  450. tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
  451. (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
  452. if (!tmp)
  453. return AVERROR(ENOMEM);
  454. ctx->slice_offsets = tmp;
  455. if (!ctx->bitstream)
  456. ctx->bitstream = (uint8_t*)buffer;
  457. ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
  458. ctx->bitstream_len += size;
  459. ctx->nb_slices++;
  460. return 0;
  461. }
  462. static void nvdec_free_dummy(struct AVHWFramesContext *ctx)
  463. {
  464. av_buffer_pool_uninit(&ctx->pool);
  465. }
  466. static AVBufferRef *nvdec_alloc_dummy(int size)
  467. {
  468. return av_buffer_create(NULL, 0, NULL, NULL, 0);
  469. }
  470. int ff_nvdec_frame_params(AVCodecContext *avctx,
  471. AVBufferRef *hw_frames_ctx,
  472. int dpb_size,
  473. int supports_444)
  474. {
  475. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
  476. const AVPixFmtDescriptor *sw_desc;
  477. int cuvid_codec_type, cuvid_chroma_format, chroma_444;
  478. sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  479. if (!sw_desc)
  480. return AVERROR_BUG;
  481. cuvid_codec_type = map_avcodec_id(avctx->codec_id);
  482. if (cuvid_codec_type < 0) {
  483. av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
  484. return AVERROR_BUG;
  485. }
  486. cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
  487. if (cuvid_chroma_format < 0) {
  488. av_log(avctx, AV_LOG_VERBOSE, "Unsupported chroma format\n");
  489. return AVERROR(EINVAL);
  490. }
  491. chroma_444 = supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
  492. frames_ctx->format = AV_PIX_FMT_CUDA;
  493. frames_ctx->width = (avctx->coded_width + 1) & ~1;
  494. frames_ctx->height = (avctx->coded_height + 1) & ~1;
  495. /*
  496. * We add two extra frames to the pool to account for deinterlacing filters
  497. * holding onto their frames.
  498. */
  499. frames_ctx->initial_pool_size = dpb_size + 2;
  500. frames_ctx->free = nvdec_free_dummy;
  501. frames_ctx->pool = av_buffer_pool_init(0, nvdec_alloc_dummy);
  502. if (!frames_ctx->pool)
  503. return AVERROR(ENOMEM);
  504. switch (sw_desc->comp[0].depth) {
  505. case 8:
  506. frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_NV12;
  507. break;
  508. case 10:
  509. frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P010;
  510. break;
  511. case 12:
  512. frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P016;
  513. break;
  514. default:
  515. return AVERROR(EINVAL);
  516. }
  517. return 0;
  518. }
  519. int ff_nvdec_get_ref_idx(AVFrame *frame)
  520. {
  521. FrameDecodeData *fdd;
  522. NVDECFrame *cf;
  523. if (!frame || !frame->private_ref)
  524. return -1;
  525. fdd = (FrameDecodeData*)frame->private_ref->data;
  526. cf = (NVDECFrame*)fdd->hwaccel_priv;
  527. if (!cf)
  528. return -1;
  529. return cf->idx;
  530. }