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