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.

648 lines
18KB

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