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.

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