FFmpeg
nvdec.c
Go to the documentation of this file.
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 #include "config_components.h"
25 
26 #include "libavutil/common.h"
27 #include "libavutil/error.h"
28 #include "libavutil/hwcontext.h"
30 #include "libavutil/cuda_check.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/pixfmt.h"
33 
34 #include "avcodec.h"
35 #include "decode.h"
36 #include "nvdec.h"
37 #include "internal.h"
38 #include "refstruct.h"
39 
40 #if !NVDECAPI_CHECK_VERSION(9, 0)
41 #define cudaVideoSurfaceFormat_YUV444 2
42 #define cudaVideoSurfaceFormat_YUV444_16Bit 3
43 #endif
44 
45 typedef struct NVDECDecoder {
46  CUvideodecoder decoder;
47 
50  CUcontext cuda_ctx;
51  CUstream stream;
52 
53  CudaFunctions *cudl;
54  CuvidFunctions *cvdl;
55 
57 } NVDECDecoder;
58 
59 typedef struct NVDECFramePool {
60  unsigned int dpb_size;
61  unsigned int nb_allocated;
63 
64 #define CHECK_CU(x) FF_CUDA_CHECK_DL(logctx, decoder->cudl, x)
65 
66 static int map_avcodec_id(enum AVCodecID id)
67 {
68  switch (id) {
69 #if CONFIG_AV1_NVDEC_HWACCEL
70  case AV_CODEC_ID_AV1: return cudaVideoCodec_AV1;
71 #endif
72  case AV_CODEC_ID_H264: return cudaVideoCodec_H264;
73  case AV_CODEC_ID_HEVC: return cudaVideoCodec_HEVC;
74  case AV_CODEC_ID_MJPEG: return cudaVideoCodec_JPEG;
75  case AV_CODEC_ID_MPEG1VIDEO: return cudaVideoCodec_MPEG1;
76  case AV_CODEC_ID_MPEG2VIDEO: return cudaVideoCodec_MPEG2;
77  case AV_CODEC_ID_MPEG4: return cudaVideoCodec_MPEG4;
78  case AV_CODEC_ID_VC1: return cudaVideoCodec_VC1;
79  case AV_CODEC_ID_VP8: return cudaVideoCodec_VP8;
80  case AV_CODEC_ID_VP9: return cudaVideoCodec_VP9;
81  case AV_CODEC_ID_WMV3: return cudaVideoCodec_VC1;
82  }
83  return -1;
84 }
85 
87 {
88  int shift_h = 0, shift_v = 0;
89 
91  return cudaVideoChromaFormat_Monochrome;
92 
93  av_pix_fmt_get_chroma_sub_sample(pix_fmt, &shift_h, &shift_v);
94 
95  if (shift_h == 1 && shift_v == 1)
96  return cudaVideoChromaFormat_420;
97  else if (shift_h == 1 && shift_v == 0)
98  return cudaVideoChromaFormat_422;
99  else if (shift_h == 0 && shift_v == 0)
100  return cudaVideoChromaFormat_444;
101 
102  return -1;
103 }
104 
106  CUVIDDECODECREATEINFO *params, void *logctx)
107 {
108  int ret;
109  CUVIDDECODECAPS caps = { 0 };
110 
111  caps.eCodecType = params->CodecType;
112  caps.eChromaFormat = params->ChromaFormat;
113  caps.nBitDepthMinus8 = params->bitDepthMinus8;
114 
115  if (!decoder->cvdl->cuvidGetDecoderCaps) {
116  av_log(logctx, AV_LOG_WARNING, "Used Nvidia driver is too old to perform a capability check.\n");
117  av_log(logctx, AV_LOG_WARNING, "The minimum required version is "
118 #if defined(_WIN32) || defined(__CYGWIN__)
119  "378.66"
120 #else
121  "378.13"
122 #endif
123  ". Continuing blind.\n");
124  return 0;
125  }
126 
127  ret = CHECK_CU(decoder->cvdl->cuvidGetDecoderCaps(&caps));
128  if (ret < 0)
129  return ret;
130 
131  av_log(logctx, AV_LOG_VERBOSE, "NVDEC capabilities:\n");
132  av_log(logctx, AV_LOG_VERBOSE, "format supported: %s, max_mb_count: %d\n",
133  caps.bIsSupported ? "yes" : "no", caps.nMaxMBCount);
134  av_log(logctx, AV_LOG_VERBOSE, "min_width: %d, max_width: %d\n",
135  caps.nMinWidth, caps.nMaxWidth);
136  av_log(logctx, AV_LOG_VERBOSE, "min_height: %d, max_height: %d\n",
137  caps.nMinHeight, caps.nMaxHeight);
138 
139  if (!caps.bIsSupported) {
140  av_log(logctx, AV_LOG_ERROR, "Hardware is lacking required capabilities\n");
141  return AVERROR(EINVAL);
142  }
143 
144  if (params->ulWidth > caps.nMaxWidth || params->ulWidth < caps.nMinWidth) {
145  av_log(logctx, AV_LOG_ERROR, "Video width %d not within range from %d to %d\n",
146  (int)params->ulWidth, caps.nMinWidth, caps.nMaxWidth);
147  return AVERROR(EINVAL);
148  }
149 
150  if (params->ulHeight > caps.nMaxHeight || params->ulHeight < caps.nMinHeight) {
151  av_log(logctx, AV_LOG_ERROR, "Video height %d not within range from %d to %d\n",
152  (int)params->ulHeight, caps.nMinHeight, caps.nMaxHeight);
153  return AVERROR(EINVAL);
154  }
155 
156  if ((params->ulWidth * params->ulHeight) / 256 > caps.nMaxMBCount) {
157  av_log(logctx, AV_LOG_ERROR, "Video macroblock count %d exceeds maximum of %d\n",
158  (int)(params->ulWidth * params->ulHeight) / 256, caps.nMaxMBCount);
159  return AVERROR(EINVAL);
160  }
161 
162  return 0;
163 }
164 
165 static void nvdec_decoder_free(FFRefStructOpaque unused, void *obj)
166 {
167  NVDECDecoder *decoder = obj;
168 
169  if (decoder->decoder) {
170  void *logctx = decoder->hw_device_ref->data;
171  CUcontext dummy;
172  CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
173  CHECK_CU(decoder->cvdl->cuvidDestroyDecoder(decoder->decoder));
174  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
175  }
176 
177  av_buffer_unref(&decoder->real_hw_frames_ref);
178  av_buffer_unref(&decoder->hw_device_ref);
179 
180  cuvid_free_functions(&decoder->cvdl);
181 }
182 
183 static int nvdec_decoder_create(NVDECDecoder **out, AVBufferRef *hw_device_ref,
184  CUVIDDECODECREATEINFO *params, void *logctx)
185 {
187  AVCUDADeviceContext *device_hwctx = hw_device_ctx->hwctx;
188 
190 
191  CUcontext dummy;
192  int ret;
193 
194  decoder = ff_refstruct_alloc_ext(sizeof(*decoder), 0,
196  if (!decoder)
197  return AVERROR(ENOMEM);
198 
199  decoder->hw_device_ref = av_buffer_ref(hw_device_ref);
200  if (!decoder->hw_device_ref) {
201  ret = AVERROR(ENOMEM);
202  goto fail;
203  }
204  decoder->cuda_ctx = device_hwctx->cuda_ctx;
205  decoder->cudl = device_hwctx->internal->cuda_dl;
206  decoder->stream = device_hwctx->stream;
207 
208  ret = cuvid_load_functions(&decoder->cvdl, logctx);
209  if (ret < 0) {
210  av_log(logctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n");
211  goto fail;
212  }
213 
214  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
215  if (ret < 0)
216  goto fail;
217 
218  ret = nvdec_test_capabilities(decoder, params, logctx);
219  if (ret < 0) {
220  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
221  goto fail;
222  }
223 
224  ret = CHECK_CU(decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params));
225 
226  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
227 
228  if (ret < 0) {
229  goto fail;
230  }
231 
232  *out = decoder;
233 
234  return 0;
235 fail:
237  return ret;
238 }
239 
240 static AVBufferRef *nvdec_decoder_frame_alloc(void *opaque, size_t size)
241 {
242  NVDECFramePool *pool = opaque;
243  AVBufferRef *ret;
244 
245  if (pool->nb_allocated >= pool->dpb_size)
246  return NULL;
247 
248  ret = av_buffer_alloc(sizeof(unsigned int));
249  if (!ret)
250  return NULL;
251 
252  *(unsigned int*)ret->data = pool->nb_allocated++;
253 
254  return ret;
255 }
256 
258 {
260 
261  av_freep(&ctx->bitstream_internal);
262  ctx->bitstream = NULL;
263  ctx->bitstream_len = 0;
264  ctx->bitstream_allocated = 0;
265 
266  av_freep(&ctx->slice_offsets);
267  ctx->nb_slices = 0;
268  ctx->slice_offsets_allocated = 0;
269 
270  ff_refstruct_unref(&ctx->decoder);
271  av_buffer_pool_uninit(&ctx->decoder_pool);
272 
273  return 0;
274 }
275 
277 {
278  av_buffer_pool_uninit(&ctx->pool);
279 }
280 
282 {
283  return av_buffer_create(NULL, 0, NULL, NULL, 0);
284 }
285 
286 static int nvdec_init_hwframes(AVCodecContext *avctx, AVBufferRef **out_frames_ref, int dummy)
287 {
288  AVHWFramesContext *frames_ctx;
289  int ret;
290 
292  avctx->hw_device_ctx,
293  avctx->hwaccel->pix_fmt,
294  out_frames_ref);
295  if (ret < 0)
296  return ret;
297 
298  frames_ctx = (AVHWFramesContext*)(*out_frames_ref)->data;
299 
300  if (dummy) {
301  // Copied from ff_decode_get_hw_frames_ctx for compatibility
302  frames_ctx->initial_pool_size += 3;
303 
304  frames_ctx->free = nvdec_free_dummy;
305  frames_ctx->pool = av_buffer_pool_init(0, nvdec_alloc_dummy);
306 
307  if (!frames_ctx->pool) {
308  av_buffer_unref(out_frames_ref);
309  return AVERROR(ENOMEM);
310  }
311  } else {
312  // This is normally not used to actually allocate frames from
313  frames_ctx->initial_pool_size = 0;
314  }
315 
316  ret = av_hwframe_ctx_init(*out_frames_ref);
317  if (ret < 0) {
318  av_buffer_unref(out_frames_ref);
319  return ret;
320  }
321 
322  return 0;
323 }
324 
326 {
328 
330  AVBufferRef *real_hw_frames_ref;
331  NVDECFramePool *pool;
332  AVHWFramesContext *frames_ctx;
333  const AVPixFmtDescriptor *sw_desc;
334 
335  CUVIDDECODECREATEINFO params = { 0 };
336 
337  cudaVideoSurfaceFormat output_format;
338  int cuvid_codec_type, cuvid_chroma_format, chroma_444;
339  int ret = 0;
340 
341  int unsafe_output = !!(avctx->hwaccel_flags & AV_HWACCEL_FLAG_UNSAFE_OUTPUT);
342 
343  sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
344  if (!sw_desc)
345  return AVERROR_BUG;
346 
347  cuvid_codec_type = map_avcodec_id(avctx->codec_id);
348  if (cuvid_codec_type < 0) {
349  av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
350  return AVERROR_BUG;
351  }
352 
353  cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
354  if (cuvid_chroma_format < 0) {
355  av_log(avctx, AV_LOG_ERROR, "Unsupported chroma format\n");
356  return AVERROR(ENOSYS);
357  }
358  chroma_444 = ctx->supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
359 
360  if (!avctx->hw_frames_ctx) {
361  ret = nvdec_init_hwframes(avctx, &avctx->hw_frames_ctx, 1);
362  if (ret < 0)
363  return ret;
364 
365  ret = nvdec_init_hwframes(avctx, &real_hw_frames_ref, 0);
366  if (ret < 0)
367  return ret;
368  } else {
369  real_hw_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
370  if (!real_hw_frames_ref)
371  return AVERROR(ENOMEM);
372  }
373 
374  switch (sw_desc->comp[0].depth) {
375  case 8:
377  cudaVideoSurfaceFormat_NV12;
378  break;
379  case 10:
380  case 12:
382  cudaVideoSurfaceFormat_P016;
383  break;
384  default:
385  av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth\n");
386  av_buffer_unref(&real_hw_frames_ref);
387  return AVERROR(ENOSYS);
388  }
389 
390  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
391 
392  params.ulWidth = avctx->coded_width;
393  params.ulHeight = avctx->coded_height;
394  params.ulTargetWidth = avctx->coded_width;
395  params.ulTargetHeight = avctx->coded_height;
396  params.bitDepthMinus8 = sw_desc->comp[0].depth - 8;
397  params.OutputFormat = output_format;
398  params.CodecType = cuvid_codec_type;
399  params.ChromaFormat = cuvid_chroma_format;
400  params.ulNumDecodeSurfaces = frames_ctx->initial_pool_size;
401  params.ulNumOutputSurfaces = unsafe_output ? frames_ctx->initial_pool_size : 1;
402 
403  ret = nvdec_decoder_create(&ctx->decoder, frames_ctx->device_ref, &params, avctx);
404  if (ret < 0) {
405  if (params.ulNumDecodeSurfaces > 32) {
406  av_log(avctx, AV_LOG_WARNING, "Using more than 32 (%d) decode surfaces might cause nvdec to fail.\n",
407  (int)params.ulNumDecodeSurfaces);
408  av_log(avctx, AV_LOG_WARNING, "Try lowering the amount of threads. Using %d right now.\n",
409  avctx->thread_count);
410  }
411  av_buffer_unref(&real_hw_frames_ref);
412  return ret;
413  }
414 
415  decoder = ctx->decoder;
416  decoder->unsafe_output = unsafe_output;
417  decoder->real_hw_frames_ref = real_hw_frames_ref;
418  real_hw_frames_ref = NULL;
419 
420  pool = av_mallocz(sizeof(*pool));
421  if (!pool) {
422  ret = AVERROR(ENOMEM);
423  goto fail;
424  }
425  pool->dpb_size = frames_ctx->initial_pool_size;
426 
427  ctx->decoder_pool = av_buffer_pool_init2(sizeof(int), pool,
429  if (!ctx->decoder_pool) {
430  ret = AVERROR(ENOMEM);
431  goto fail;
432  }
433 
434  return 0;
435 fail:
436  ff_nvdec_decode_uninit(avctx);
437  return ret;
438 }
439 
440 static void nvdec_fdd_priv_free(void *priv)
441 {
442  NVDECFrame *cf = priv;
443 
444  if (!cf)
445  return;
446 
447  av_buffer_unref(&cf->idx_ref);
450 
451  av_freep(&priv);
452 }
453 
454 static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
455 {
456  NVDECFrame *unmap_data = (NVDECFrame*)data;
457  NVDECDecoder *decoder = unmap_data->decoder;
458  void *logctx = decoder->hw_device_ref->data;
459  CUdeviceptr devptr = (CUdeviceptr)opaque;
460  int ret;
461  CUcontext dummy;
462 
463  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
464  if (ret < 0)
465  goto finish;
466 
467  CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
468 
469  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
470 
471 finish:
472  av_buffer_unref(&unmap_data->idx_ref);
473  av_buffer_unref(&unmap_data->ref_idx_ref);
474  ff_refstruct_unref(&unmap_data->decoder);
475  av_free(unmap_data);
476 }
477 
478 static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
479 {
481  NVDECFrame *cf = (NVDECFrame*)fdd->hwaccel_priv;
482  NVDECDecoder *decoder = cf->decoder;
483 
485 
486  CUVIDPROCPARAMS vpp = { 0 };
487  NVDECFrame *unmap_data = NULL;
488 
489  CUcontext dummy;
490  CUdeviceptr devptr;
491 
492  unsigned int pitch, i;
493  unsigned int offset = 0;
494  int shift_h = 0, shift_v = 0;
495  int ret = 0;
496 
497  vpp.progressive_frame = 1;
498  vpp.output_stream = decoder->stream;
499 
500  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
501  if (ret < 0)
502  return ret;
503 
504  ret = CHECK_CU(decoder->cvdl->cuvidMapVideoFrame(decoder->decoder,
505  cf->idx, &devptr,
506  &pitch, &vpp));
507  if (ret < 0)
508  goto finish;
509 
510  unmap_data = av_mallocz(sizeof(*unmap_data));
511  if (!unmap_data) {
512  ret = AVERROR(ENOMEM);
513  goto copy_fail;
514  }
515 
516  frame->buf[1] = av_buffer_create((uint8_t *)unmap_data, sizeof(*unmap_data),
517  nvdec_unmap_mapped_frame, (void*)devptr,
519  if (!frame->buf[1]) {
520  ret = AVERROR(ENOMEM);
521  goto copy_fail;
522  }
523 
524  ret = av_buffer_replace(&frame->hw_frames_ctx, decoder->real_hw_frames_ref);
525  if (ret < 0)
526  goto copy_fail;
527 
528  unmap_data->idx = cf->idx;
529  if (!(unmap_data->idx_ref = av_buffer_ref(cf->idx_ref))) {
530  ret = AVERROR(ENOMEM);
531  goto copy_fail;
532  }
533  unmap_data->decoder = ff_refstruct_ref(cf->decoder);
534 
535  av_pix_fmt_get_chroma_sub_sample(hwctx->sw_format, &shift_h, &shift_v);
536  for (i = 0; frame->linesize[i]; i++) {
537  frame->data[i] = (uint8_t*)(devptr + offset);
538  frame->linesize[i] = pitch;
539  offset += pitch * (frame->height >> (i ? shift_v : 0));
540  }
541 
542  goto finish;
543 
544 copy_fail:
545  if (!frame->buf[1]) {
546  CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
547  av_freep(&unmap_data);
548  } else {
549  av_buffer_unref(&frame->buf[1]);
550  }
551 
552 finish:
553  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
554 
555  if (ret < 0 || decoder->unsafe_output)
556  return ret;
557 
559 }
560 
562 {
565  NVDECFrame *cf = NULL;
566  int ret;
567 
568  ctx->bitstream_len = 0;
569  ctx->nb_slices = 0;
570 
571  if (fdd->hwaccel_priv)
572  return 0;
573 
574  cf = av_mallocz(sizeof(*cf));
575  if (!cf)
576  return AVERROR(ENOMEM);
577 
578  cf->decoder = ff_refstruct_ref(ctx->decoder);
579 
580  cf->idx_ref = av_buffer_pool_get(ctx->decoder_pool);
581  if (!cf->idx_ref) {
582  av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
583  ret = AVERROR(ENOMEM);
584  goto fail;
585  }
586  cf->ref_idx = cf->idx = *(unsigned int*)cf->idx_ref->data;
587 
588  fdd->hwaccel_priv = cf;
591 
592  return 0;
593 fail:
595  return ret;
596 
597 }
598 
600 {
603  NVDECFrame *cf;
604  int ret;
605 
606  ret = ff_nvdec_start_frame(avctx, frame);
607  if (ret < 0)
608  return ret;
609 
610  cf = fdd->hwaccel_priv;
611 
612  if (has_sep_ref) {
613  if (!cf->ref_idx_ref) {
614  cf->ref_idx_ref = av_buffer_pool_get(ctx->decoder_pool);
615  if (!cf->ref_idx_ref) {
616  av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
617  ret = AVERROR(ENOMEM);
618  goto fail;
619  }
620  }
621  cf->ref_idx = *(unsigned int*)cf->ref_idx_ref->data;
622  } else {
623  av_buffer_unref(&cf->ref_idx_ref);
624  cf->ref_idx = cf->idx;
625  }
626 
627  return 0;
628 fail:
630  return ret;
631 }
632 
634 {
636  NVDECDecoder *decoder = ctx->decoder;
637  void *logctx = avctx;
638  CUVIDPICPARAMS *pp = &ctx->pic_params;
639 
640  CUcontext dummy;
641 
642  int ret = 0;
643 
644  pp->nBitstreamDataLen = ctx->bitstream_len;
645  pp->pBitstreamData = ctx->bitstream;
646  pp->nNumSlices = ctx->nb_slices;
647  pp->pSliceDataOffsets = ctx->slice_offsets;
648 
649  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
650  if (ret < 0)
651  return ret;
652 
653  ret = CHECK_CU(decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params));
654  if (ret < 0)
655  goto finish;
656 
657 finish:
658  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
659 
660  return ret;
661 }
662 
664 {
666  int ret = ff_nvdec_end_frame(avctx);
667  ctx->bitstream = NULL;
668  ctx->bitstream_len = 0;
669  ctx->nb_slices = 0;
670  return ret;
671 }
672 
674  uint32_t size)
675 {
677  void *tmp;
678 
679  tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
680  (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
681  if (!tmp)
682  return AVERROR(ENOMEM);
683  ctx->slice_offsets = tmp;
684 
685  if (!ctx->bitstream)
686  ctx->bitstream = (uint8_t*)buffer;
687 
688  ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
689  ctx->bitstream_len += size;
690  ctx->nb_slices++;
691 
692  return 0;
693 }
694 
696  AVBufferRef *hw_frames_ctx,
697  int dpb_size,
698  int supports_444)
699 {
700  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
701  const AVPixFmtDescriptor *sw_desc;
702  int cuvid_codec_type, cuvid_chroma_format, chroma_444;
703 
704  sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
705  if (!sw_desc)
706  return AVERROR_BUG;
707 
708  cuvid_codec_type = map_avcodec_id(avctx->codec_id);
709  if (cuvid_codec_type < 0) {
710  av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
711  return AVERROR_BUG;
712  }
713 
714  cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
715  if (cuvid_chroma_format < 0) {
716  av_log(avctx, AV_LOG_VERBOSE, "Unsupported chroma format\n");
717  return AVERROR(EINVAL);
718  }
719  chroma_444 = supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
720 
721  frames_ctx->format = AV_PIX_FMT_CUDA;
722  frames_ctx->width = (avctx->coded_width + 1) & ~1;
723  frames_ctx->height = (avctx->coded_height + 1) & ~1;
724  /*
725  * We add two extra frames to the pool to account for deinterlacing filters
726  * holding onto their frames.
727  */
728  frames_ctx->initial_pool_size = dpb_size + 2;
729 
730  switch (sw_desc->comp[0].depth) {
731  case 8:
732  frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_NV12;
733  break;
734  case 10:
735  frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P010;
736  break;
737  case 12:
738  frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P016;
739  break;
740  default:
741  return AVERROR(EINVAL);
742  }
743 
744  return 0;
745 }
746 
748 {
749  FrameDecodeData *fdd;
750  NVDECFrame *cf;
751 
752  if (!frame || !frame->private_ref)
753  return -1;
754 
756  cf = (NVDECFrame*)fdd->hwaccel_priv;
757  if (!cf)
758  return -1;
759 
760  return cf->ref_idx;
761 }
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1435
av_buffer_pool_init
AVBufferPool * av_buffer_pool_init(size_t size, AVBufferRef *(*alloc)(size_t size))
Allocate and initialize a buffer pool.
Definition: buffer.c:280
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:253
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
ff_refstruct_ref
void * ff_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition: refstruct.c:136
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
map_avcodec_id
static int map_avcodec_id(enum AVCodecID id)
Definition: nvdec.c:66
hwcontext_cuda_internal.h
out
FILE * out
Definition: movenc.c:54
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2964
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
map_chroma_format
static int map_chroma_format(enum AVPixelFormat pix_fmt)
Definition: nvdec.c:86
NVDECFramePool
Definition: nvdec.c:59
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:209
NVDECDecoder::stream
CUstream stream
Definition: nvdec.c:51
ff_refstruct_alloc_ext
static void * ff_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(FFRefStructOpaque opaque, void *obj))
A wrapper around ff_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition: refstruct.h:94
ff_nvdec_get_ref_idx
int ff_nvdec_get_ref_idx(AVFrame *frame)
Definition: nvdec.c:747
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
FrameDecodeData
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition: decode.h:33
av_hwframe_ctx_init
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:334
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
av_frame_make_writable
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:683
pixdesc.h
internal.h
AVHWFramesContext::free
void(* free)(struct AVHWFramesContext *ctx)
This field may be set by the caller before calling av_hwframe_ctx_init().
Definition: hwcontext.h:170
AVComponentDescriptor::depth
int depth
Number of bits in the component.
Definition: pixdesc.h:57
NVDECDecoder::unsafe_output
int unsafe_output
Definition: nvdec.c:56
data
const char data[16]
Definition: mxf.c:148
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
FrameDecodeData::hwaccel_priv_free
void(* hwaccel_priv_free)(void *priv)
Definition: decode.h:52
FFRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
NVDECDecoder::decoder
CUvideodecoder decoder
Definition: nvdec.c:46
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVHWFramesContext::width
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:229
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:590
NVDECDecoder::cvdl
CuvidFunctions * cvdl
Definition: nvdec.c:54
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3004
decoder
static const chunk_decoder decoder[8]
Definition: dfa.c:331
finish
static void finish(void)
Definition: movenc.c:342
ff_nvdec_start_frame
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: nvdec.c:561
NVDECFrame::ref_idx
unsigned int ref_idx
Definition: nvdec.h:46
fail
#define fail()
Definition: checkasm.h:138
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1532
dummy
int dummy
Definition: motion.c:66
av_buffer_pool_init2
AVBufferPool * av_buffer_pool_init2(size_t size, void *opaque, AVBufferRef *(*alloc)(void *opaque, size_t size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition: buffer.c:259
av_pix_fmt_get_chroma_sub_sample
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2992
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:636
refstruct.h
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:61
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
cudaVideoSurfaceFormat_YUV444
#define cudaVideoSurfaceFormat_YUV444
Definition: nvdec.c:41
AV_HWACCEL_FLAG_UNSAFE_OUTPUT
#define AV_HWACCEL_FLAG_UNSAFE_OUTPUT
Some hardware decoders (namely nvdec) can either output direct decoder surfaces, or make an on-device...
Definition: avcodec.h:2215
AVHWFramesContext::height
int height
Definition: hwcontext.h:229
av_buffer_pool_get
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:384
AVHWFramesContext::pool
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Definition: hwcontext.h:190
ff_nvdec_start_frame_sep_ref
int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_sep_ref)
Definition: nvdec.c:599
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:495
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:481
AV_BUFFER_FLAG_READONLY
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:114
nvdec_decoder_free
static void nvdec_decoder_free(FFRefStructOpaque unused, void *obj)
Definition: nvdec.c:165
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:220
NVDECFrame
Definition: nvdec.h:44
ctx
AVFormatContext * ctx
Definition: movenc.c:48
decode.h
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:451
NVDECDecoder::cudl
CudaFunctions * cudl
Definition: nvdec.c:53
dpb_size
int dpb_size
Definition: h264_levels.c:111
if
if(ret)
Definition: filter_design.txt:179
AV_CODEC_ID_WMV3
@ AV_CODEC_ID_WMV3
Definition: codec_id.h:123
ff_nvdec_simple_end_frame
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:663
NULL
#define NULL
Definition: coverity.c:32
AVHWFramesContext::sw_format
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:222
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:283
ff_nvdec_decode_init
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition: nvdec.c:325
AVHWFramesContext::device_ref
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:141
NVDECDecoder::hw_device_ref
AVBufferRef * hw_device_ref
Definition: nvdec.c:48
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:476
av_buffer_pool_uninit
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:322
nvdec_decoder_create
static int nvdec_decoder_create(NVDECDecoder **out, AVBufferRef *hw_device_ref, CUVIDDECODECREATEINFO *params, void *logctx)
Definition: nvdec.c:183
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:53
av_buffer_create
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:55
error.h
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
cudaVideoSurfaceFormat_YUV444_16Bit
#define cudaVideoSurfaceFormat_YUV444_16Bit
Definition: nvdec.c:42
nvdec_alloc_dummy
static AVBufferRef * nvdec_alloc_dummy(size_t size)
Definition: nvdec.c:281
CHECK_CU
#define CHECK_CU(x)
Definition: nvdec.c:64
NVDECDecoder::real_hw_frames_ref
AVBufferRef * real_hw_frames_ref
Definition: nvdec.c:49
FrameDecodeData::post_process
int(* post_process)(void *logctx, AVFrame *frame)
The callback to perform some delayed processing on the frame right before it is returned to the calle...
Definition: decode.h:44
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:124
size
int size
Definition: twinvq_data.h:10344
nvdec_decoder_frame_alloc
static AVBufferRef * nvdec_decoder_frame_alloc(void *opaque, size_t size)
Definition: nvdec.c:240
nvdec_test_capabilities
static int nvdec_test_capabilities(NVDECDecoder *decoder, CUVIDDECODECREATEINFO *params, void *logctx)
Definition: nvdec.c:105
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
nvdec.h
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
nvdec_free_dummy
static void nvdec_free_dummy(struct AVHWFramesContext *ctx)
Definition: nvdec.c:276
ff_nvdec_decode_uninit
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition: nvdec.c:257
AVFrame::private_ref
AVBufferRef * private_ref
AVBufferRef for internal use by a single libav* library.
Definition: frame.h:797
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
NVDECFrame::decoder
struct NVDECDecoder * decoder
RefStruct reference.
Definition: nvdec.h:49
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
hw_device_ctx
static AVBufferRef * hw_device_ctx
Definition: hw_decode.c:44
NVDECDecoder::cuda_ctx
CUcontext cuda_ctx
Definition: nvdec.c:50
nvdec_fdd_priv_free
static void nvdec_fdd_priv_free(void *priv)
Definition: nvdec.c:440
ff_nvdec_end_frame
int ff_nvdec_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:633
common.h
AVCodecContext::hwaccel_flags
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active).
Definition: avcodec.h:1990
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
AVCodecContext::hw_device_ctx
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:1981
AV_CODEC_ID_VC1
@ AV_CODEC_ID_VC1
Definition: codec_id.h:122
av_buffer_replace
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:233
NVDECDecoder
Definition: nvdec.c:45
AV_PIX_FMT_P016
#define AV_PIX_FMT_P016
Definition: pixfmt.h:520
AVCodecContext::hw_frames_ctx
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:1940
avcodec.h
nvdec_retrieve_data
static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
Definition: nvdec.c:478
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
AVCUDADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_cuda.h:42
avcodec_get_hw_frames_parameters
int avcodec_get_hw_frames_parameters(AVCodecContext *avctx, AVBufferRef *device_ref, enum AVPixelFormat hw_pix_fmt, AVBufferRef **out_frames_ref)
Create and return a AVHWFramesContext with values adequate for hardware decoding.
Definition: decode.c:1159
ret
ret
Definition: filter_design.txt:187
pixfmt.h
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
cuda_check.h
NVDECFramePool::dpb_size
unsigned int dpb_size
Definition: nvdec.c:60
nvdec_init_hwframes
static int nvdec_init_hwframes(AVCodecContext *avctx, AVBufferRef **out_frames_ref, int dummy)
Definition: nvdec.c:286
AVFrame::hw_frames_ctx
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition: frame.h:752
NVDECFramePool::nb_allocated
unsigned int nb_allocated
Definition: nvdec.c:61
AVCodecContext
main external API structure.
Definition: avcodec.h:441
AVFrame::height
int height
Definition: frame.h:412
ff_nvdec_simple_decode_slice
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec.c:673
nvdec_unmap_mapped_frame
static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
Definition: nvdec.c:454
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
ff_nvdec_frame_params
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size, int supports_444)
Definition: nvdec.c:695
output_format
static char * output_format
Definition: ffprobe.c:143
AVPixFmtDescriptor::comp
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:105
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
NVDECFrame::idx_ref
AVBufferRef * idx_ref
Definition: nvdec.h:47
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:518
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:636
AVHWFramesContext::initial_pool_size
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:199
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FrameDecodeData::hwaccel_priv
void * hwaccel_priv
Per-frame private data for hwaccels.
Definition: decode.h:51
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: codec_id.h:192
hwcontext.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1810
NVDECContext
Definition: nvdec.h:52
NVDECFrame::ref_idx_ref
AVBufferRef * ref_idx_ref
Definition: nvdec.h:48
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
AVHWAccel::pix_fmt
enum AVPixelFormat pix_fmt
Supported pixel format.
Definition: avcodec.h:2156
ff_refstruct_unref
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:116