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.

640 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. decoder->cvdl->cuvidDestroyDecoder(decoder->decoder);
  130. av_buffer_unref(&decoder->hw_device_ref);
  131. cuvid_free_functions(&decoder->cvdl);
  132. av_freep(&decoder);
  133. }
  134. static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref,
  135. CUVIDDECODECREATEINFO *params, void *logctx)
  136. {
  137. AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)hw_device_ref->data;
  138. AVCUDADeviceContext *device_hwctx = hw_device_ctx->hwctx;
  139. AVBufferRef *decoder_ref;
  140. NVDECDecoder *decoder;
  141. CUcontext dummy;
  142. CUresult err;
  143. int ret;
  144. decoder = av_mallocz(sizeof(*decoder));
  145. if (!decoder)
  146. return AVERROR(ENOMEM);
  147. decoder_ref = av_buffer_create((uint8_t*)decoder, sizeof(*decoder),
  148. nvdec_decoder_free, NULL, AV_BUFFER_FLAG_READONLY);
  149. if (!decoder_ref) {
  150. av_freep(&decoder);
  151. return AVERROR(ENOMEM);
  152. }
  153. decoder->hw_device_ref = av_buffer_ref(hw_device_ref);
  154. if (!decoder->hw_device_ref) {
  155. ret = AVERROR(ENOMEM);
  156. goto fail;
  157. }
  158. decoder->cuda_ctx = device_hwctx->cuda_ctx;
  159. decoder->cudl = device_hwctx->internal->cuda_dl;
  160. decoder->stream = device_hwctx->stream;
  161. ret = cuvid_load_functions(&decoder->cvdl, logctx);
  162. if (ret < 0) {
  163. av_log(logctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n");
  164. goto fail;
  165. }
  166. err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx);
  167. if (err != CUDA_SUCCESS) {
  168. ret = AVERROR_UNKNOWN;
  169. goto fail;
  170. }
  171. ret = nvdec_test_capabilities(decoder, params, logctx);
  172. if (ret < 0) {
  173. decoder->cudl->cuCtxPopCurrent(&dummy);
  174. goto fail;
  175. }
  176. err = decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params);
  177. decoder->cudl->cuCtxPopCurrent(&dummy);
  178. if (err != CUDA_SUCCESS) {
  179. av_log(logctx, AV_LOG_ERROR, "Error creating a NVDEC decoder: %d\n", err);
  180. ret = AVERROR_UNKNOWN;
  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. CUdeviceptr devptr = (CUdeviceptr)opaque;
  294. CUresult err;
  295. CUcontext dummy;
  296. err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx);
  297. if (err != CUDA_SUCCESS) {
  298. av_log(NULL, AV_LOG_ERROR, "cuCtxPushCurrent failed\n");
  299. goto finish;
  300. }
  301. err = decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr);
  302. if (err != CUDA_SUCCESS)
  303. av_log(NULL, AV_LOG_ERROR, "cuvidUnmapVideoFrame failed\n");
  304. decoder->cudl->cuCtxPopCurrent(&dummy);
  305. finish:
  306. av_buffer_unref(&unmap_data->idx_ref);
  307. av_buffer_unref(&unmap_data->decoder_ref);
  308. av_free(unmap_data);
  309. }
  310. static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
  311. {
  312. FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
  313. NVDECFrame *cf = (NVDECFrame*)fdd->hwaccel_priv;
  314. NVDECDecoder *decoder = (NVDECDecoder*)cf->decoder_ref->data;
  315. CUVIDPROCPARAMS vpp = { 0 };
  316. NVDECFrame *unmap_data = NULL;
  317. CUresult err;
  318. CUcontext dummy;
  319. CUdeviceptr devptr;
  320. unsigned int pitch, i;
  321. unsigned int offset = 0;
  322. int ret = 0;
  323. vpp.progressive_frame = 1;
  324. vpp.output_stream = decoder->stream;
  325. err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx);
  326. if (err != CUDA_SUCCESS)
  327. return AVERROR_UNKNOWN;
  328. err = decoder->cvdl->cuvidMapVideoFrame(decoder->decoder, cf->idx, &devptr,
  329. &pitch, &vpp);
  330. if (err != CUDA_SUCCESS) {
  331. av_log(logctx, AV_LOG_ERROR, "Error mapping a picture with CUVID: %d\n",
  332. err);
  333. ret = AVERROR_UNKNOWN;
  334. goto finish;
  335. }
  336. unmap_data = av_mallocz(sizeof(*unmap_data));
  337. if (!unmap_data) {
  338. ret = AVERROR(ENOMEM);
  339. goto copy_fail;
  340. }
  341. frame->buf[1] = av_buffer_create((uint8_t *)unmap_data, sizeof(*unmap_data),
  342. nvdec_unmap_mapped_frame, (void*)devptr,
  343. AV_BUFFER_FLAG_READONLY);
  344. if (!frame->buf[1]) {
  345. ret = AVERROR(ENOMEM);
  346. goto copy_fail;
  347. }
  348. unmap_data->idx = cf->idx;
  349. unmap_data->idx_ref = av_buffer_ref(cf->idx_ref);
  350. unmap_data->decoder_ref = av_buffer_ref(cf->decoder_ref);
  351. for (i = 0; frame->linesize[i]; i++) {
  352. frame->data[i] = (uint8_t*)(devptr + offset);
  353. frame->linesize[i] = pitch;
  354. offset += pitch * (frame->height >> (i ? 1 : 0));
  355. }
  356. goto finish;
  357. copy_fail:
  358. if (!frame->buf[1]) {
  359. decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr);
  360. av_freep(&unmap_data);
  361. } else {
  362. av_buffer_unref(&frame->buf[1]);
  363. }
  364. finish:
  365. decoder->cudl->cuCtxPopCurrent(&dummy);
  366. return ret;
  367. }
  368. int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
  369. {
  370. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  371. FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
  372. NVDECFrame *cf = NULL;
  373. int ret;
  374. ctx->bitstream_len = 0;
  375. ctx->nb_slices = 0;
  376. if (fdd->hwaccel_priv)
  377. return 0;
  378. cf = av_mallocz(sizeof(*cf));
  379. if (!cf)
  380. return AVERROR(ENOMEM);
  381. cf->decoder_ref = av_buffer_ref(ctx->decoder_ref);
  382. if (!cf->decoder_ref) {
  383. ret = AVERROR(ENOMEM);
  384. goto fail;
  385. }
  386. cf->idx_ref = av_buffer_pool_get(ctx->decoder_pool);
  387. if (!cf->idx_ref) {
  388. av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
  389. ret = AVERROR(ENOMEM);
  390. goto fail;
  391. }
  392. cf->idx = *(unsigned int*)cf->idx_ref->data;
  393. fdd->hwaccel_priv = cf;
  394. fdd->hwaccel_priv_free = nvdec_fdd_priv_free;
  395. fdd->post_process = nvdec_retrieve_data;
  396. return 0;
  397. fail:
  398. nvdec_fdd_priv_free(cf);
  399. return ret;
  400. }
  401. int ff_nvdec_end_frame(AVCodecContext *avctx)
  402. {
  403. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  404. NVDECDecoder *decoder = (NVDECDecoder*)ctx->decoder_ref->data;
  405. CUVIDPICPARAMS *pp = &ctx->pic_params;
  406. CUresult err;
  407. CUcontext dummy;
  408. int ret = 0;
  409. pp->nBitstreamDataLen = ctx->bitstream_len;
  410. pp->pBitstreamData = ctx->bitstream;
  411. pp->nNumSlices = ctx->nb_slices;
  412. pp->pSliceDataOffsets = ctx->slice_offsets;
  413. err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx);
  414. if (err != CUDA_SUCCESS)
  415. return AVERROR_UNKNOWN;
  416. err = decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params);
  417. if (err != CUDA_SUCCESS) {
  418. av_log(avctx, AV_LOG_ERROR, "Error decoding a picture with NVDEC: %d\n",
  419. err);
  420. ret = AVERROR_UNKNOWN;
  421. goto finish;
  422. }
  423. finish:
  424. decoder->cudl->cuCtxPopCurrent(&dummy);
  425. return ret;
  426. }
  427. int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
  428. {
  429. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  430. int ret = ff_nvdec_end_frame(avctx);
  431. ctx->bitstream = NULL;
  432. return ret;
  433. }
  434. int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer,
  435. uint32_t size)
  436. {
  437. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  438. void *tmp;
  439. tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
  440. (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
  441. if (!tmp)
  442. return AVERROR(ENOMEM);
  443. ctx->slice_offsets = tmp;
  444. if (!ctx->bitstream)
  445. ctx->bitstream = (uint8_t*)buffer;
  446. ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
  447. ctx->bitstream_len += size;
  448. ctx->nb_slices++;
  449. return 0;
  450. }
  451. static void nvdec_free_dummy(struct AVHWFramesContext *ctx)
  452. {
  453. av_buffer_pool_uninit(&ctx->pool);
  454. }
  455. static AVBufferRef *nvdec_alloc_dummy(int size)
  456. {
  457. return av_buffer_create(NULL, 0, NULL, NULL, 0);
  458. }
  459. int ff_nvdec_frame_params(AVCodecContext *avctx,
  460. AVBufferRef *hw_frames_ctx,
  461. int dpb_size)
  462. {
  463. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
  464. const AVPixFmtDescriptor *sw_desc;
  465. int cuvid_codec_type, cuvid_chroma_format;
  466. sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  467. if (!sw_desc)
  468. return AVERROR_BUG;
  469. cuvid_codec_type = map_avcodec_id(avctx->codec_id);
  470. if (cuvid_codec_type < 0) {
  471. av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
  472. return AVERROR_BUG;
  473. }
  474. cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
  475. if (cuvid_chroma_format < 0) {
  476. av_log(avctx, AV_LOG_VERBOSE, "Unsupported chroma format\n");
  477. return AVERROR(EINVAL);
  478. }
  479. frames_ctx->format = AV_PIX_FMT_CUDA;
  480. frames_ctx->width = (avctx->coded_width + 1) & ~1;
  481. frames_ctx->height = (avctx->coded_height + 1) & ~1;
  482. frames_ctx->initial_pool_size = dpb_size;
  483. frames_ctx->free = nvdec_free_dummy;
  484. frames_ctx->pool = av_buffer_pool_init(0, nvdec_alloc_dummy);
  485. if (!frames_ctx->pool)
  486. return AVERROR(ENOMEM);
  487. switch (sw_desc->comp[0].depth) {
  488. case 8:
  489. frames_ctx->sw_format = AV_PIX_FMT_NV12;
  490. break;
  491. case 10:
  492. frames_ctx->sw_format = AV_PIX_FMT_P010;
  493. break;
  494. case 12:
  495. frames_ctx->sw_format = AV_PIX_FMT_P016;
  496. break;
  497. default:
  498. return AVERROR(EINVAL);
  499. }
  500. return 0;
  501. }
  502. int ff_nvdec_get_ref_idx(AVFrame *frame)
  503. {
  504. FrameDecodeData *fdd;
  505. NVDECFrame *cf;
  506. if (!frame || !frame->private_ref)
  507. return -1;
  508. fdd = (FrameDecodeData*)frame->private_ref->data;
  509. cf = (NVDECFrame*)fdd->hwaccel_priv;
  510. if (!cf)
  511. return -1;
  512. return cf->idx;
  513. }