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 
25 #include "libavutil/common.h"
26 #include "libavutil/error.h"
27 #include "libavutil/hwcontext.h"
29 #include "libavutil/cuda_check.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/pixfmt.h"
32 
33 #include "avcodec.h"
34 #include "decode.h"
35 #include "nvdec.h"
36 #include "internal.h"
37 
38 #if !NVDECAPI_CHECK_VERSION(9, 0)
39 #define cudaVideoSurfaceFormat_YUV444 2
40 #define cudaVideoSurfaceFormat_YUV444_16Bit 3
41 #endif
42 
43 typedef struct NVDECDecoder {
44  CUvideodecoder decoder;
45 
48  CUcontext cuda_ctx;
49  CUstream stream;
50 
51  CudaFunctions *cudl;
52  CuvidFunctions *cvdl;
53 } NVDECDecoder;
54 
55 typedef struct NVDECFramePool {
56  unsigned int dpb_size;
57  unsigned int nb_allocated;
59 
60 #define CHECK_CU(x) FF_CUDA_CHECK_DL(logctx, decoder->cudl, x)
61 
62 static int map_avcodec_id(enum AVCodecID id)
63 {
64  switch (id) {
65 #if CONFIG_AV1_NVDEC_HWACCEL
66  case AV_CODEC_ID_AV1: return cudaVideoCodec_AV1;
67 #endif
68  case AV_CODEC_ID_H264: return cudaVideoCodec_H264;
69  case AV_CODEC_ID_HEVC: return cudaVideoCodec_HEVC;
70  case AV_CODEC_ID_MJPEG: return cudaVideoCodec_JPEG;
71  case AV_CODEC_ID_MPEG1VIDEO: return cudaVideoCodec_MPEG1;
72  case AV_CODEC_ID_MPEG2VIDEO: return cudaVideoCodec_MPEG2;
73  case AV_CODEC_ID_MPEG4: return cudaVideoCodec_MPEG4;
74  case AV_CODEC_ID_VC1: return cudaVideoCodec_VC1;
75  case AV_CODEC_ID_VP8: return cudaVideoCodec_VP8;
76  case AV_CODEC_ID_VP9: return cudaVideoCodec_VP9;
77  case AV_CODEC_ID_WMV3: return cudaVideoCodec_VC1;
78  }
79  return -1;
80 }
81 
83 {
84  int shift_h = 0, shift_v = 0;
85 
86  if (av_pix_fmt_count_planes(pix_fmt) == 1)
87  return cudaVideoChromaFormat_Monochrome;
88 
89  av_pix_fmt_get_chroma_sub_sample(pix_fmt, &shift_h, &shift_v);
90 
91  if (shift_h == 1 && shift_v == 1)
92  return cudaVideoChromaFormat_420;
93  else if (shift_h == 1 && shift_v == 0)
94  return cudaVideoChromaFormat_422;
95  else if (shift_h == 0 && shift_v == 0)
96  return cudaVideoChromaFormat_444;
97 
98  return -1;
99 }
100 
102  CUVIDDECODECREATEINFO *params, void *logctx)
103 {
104  int ret;
105  CUVIDDECODECAPS caps = { 0 };
106 
107  caps.eCodecType = params->CodecType;
108  caps.eChromaFormat = params->ChromaFormat;
109  caps.nBitDepthMinus8 = params->bitDepthMinus8;
110 
111  if (!decoder->cvdl->cuvidGetDecoderCaps) {
112  av_log(logctx, AV_LOG_WARNING, "Used Nvidia driver is too old to perform a capability check.\n");
113  av_log(logctx, AV_LOG_WARNING, "The minimum required version is "
114 #if defined(_WIN32) || defined(__CYGWIN__)
115  "378.66"
116 #else
117  "378.13"
118 #endif
119  ". Continuing blind.\n");
120  return 0;
121  }
122 
123  ret = CHECK_CU(decoder->cvdl->cuvidGetDecoderCaps(&caps));
124  if (ret < 0)
125  return ret;
126 
127  av_log(logctx, AV_LOG_VERBOSE, "NVDEC capabilities:\n");
128  av_log(logctx, AV_LOG_VERBOSE, "format supported: %s, max_mb_count: %d\n",
129  caps.bIsSupported ? "yes" : "no", caps.nMaxMBCount);
130  av_log(logctx, AV_LOG_VERBOSE, "min_width: %d, max_width: %d\n",
131  caps.nMinWidth, caps.nMaxWidth);
132  av_log(logctx, AV_LOG_VERBOSE, "min_height: %d, max_height: %d\n",
133  caps.nMinHeight, caps.nMaxHeight);
134 
135  if (!caps.bIsSupported) {
136  av_log(logctx, AV_LOG_ERROR, "Hardware is lacking required capabilities\n");
137  return AVERROR(EINVAL);
138  }
139 
140  if (params->ulWidth > caps.nMaxWidth || params->ulWidth < caps.nMinWidth) {
141  av_log(logctx, AV_LOG_ERROR, "Video width %d not within range from %d to %d\n",
142  (int)params->ulWidth, caps.nMinWidth, caps.nMaxWidth);
143  return AVERROR(EINVAL);
144  }
145 
146  if (params->ulHeight > caps.nMaxHeight || params->ulHeight < caps.nMinHeight) {
147  av_log(logctx, AV_LOG_ERROR, "Video height %d not within range from %d to %d\n",
148  (int)params->ulHeight, caps.nMinHeight, caps.nMaxHeight);
149  return AVERROR(EINVAL);
150  }
151 
152  if ((params->ulWidth * params->ulHeight) / 256 > caps.nMaxMBCount) {
153  av_log(logctx, AV_LOG_ERROR, "Video macroblock count %d exceeds maximum of %d\n",
154  (int)(params->ulWidth * params->ulHeight) / 256, caps.nMaxMBCount);
155  return AVERROR(EINVAL);
156  }
157 
158  return 0;
159 }
160 
161 static void nvdec_decoder_free(void *opaque, uint8_t *data)
162 {
164 
165  if (decoder->decoder) {
166  void *logctx = decoder->hw_device_ref->data;
167  CUcontext dummy;
168  CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
169  CHECK_CU(decoder->cvdl->cuvidDestroyDecoder(decoder->decoder));
170  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
171  }
172 
174  av_buffer_unref(&decoder->hw_device_ref);
175 
176  cuvid_free_functions(&decoder->cvdl);
177 
178  av_freep(&decoder);
179 }
180 
182  CUVIDDECODECREATEINFO *params, void *logctx)
183 {
185  AVCUDADeviceContext *device_hwctx = hw_device_ctx->hwctx;
186 
187  AVBufferRef *decoder_ref;
189 
190  CUcontext dummy;
191  int ret;
192 
193  decoder = av_mallocz(sizeof(*decoder));
194  if (!decoder)
195  return AVERROR(ENOMEM);
196 
197  decoder_ref = av_buffer_create((uint8_t*)decoder, sizeof(*decoder),
199  if (!decoder_ref) {
200  av_freep(&decoder);
201  return AVERROR(ENOMEM);
202  }
203 
204  decoder->hw_device_ref = av_buffer_ref(hw_device_ref);
205  if (!decoder->hw_device_ref) {
206  ret = AVERROR(ENOMEM);
207  goto fail;
208  }
209  decoder->cuda_ctx = device_hwctx->cuda_ctx;
210  decoder->cudl = device_hwctx->internal->cuda_dl;
211  decoder->stream = device_hwctx->stream;
212 
213  ret = cuvid_load_functions(&decoder->cvdl, logctx);
214  if (ret < 0) {
215  av_log(logctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n");
216  goto fail;
217  }
218 
219  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
220  if (ret < 0)
221  goto fail;
222 
223  ret = nvdec_test_capabilities(decoder, params, logctx);
224  if (ret < 0) {
225  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
226  goto fail;
227  }
228 
229  ret = CHECK_CU(decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params));
230 
231  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
232 
233  if (ret < 0) {
234  goto fail;
235  }
236 
237  *out = decoder_ref;
238 
239  return 0;
240 fail:
241  av_buffer_unref(&decoder_ref);
242  return ret;
243 }
244 
246 {
247  NVDECFramePool *pool = opaque;
248  AVBufferRef *ret;
249 
250  if (pool->nb_allocated >= pool->dpb_size)
251  return NULL;
252 
253  ret = av_buffer_alloc(sizeof(unsigned int));
254  if (!ret)
255  return NULL;
256 
257  *(unsigned int*)ret->data = pool->nb_allocated++;
258 
259  return ret;
260 }
261 
263 {
265 
266  av_freep(&ctx->bitstream);
268  ctx->bitstream_len = 0;
269  ctx->bitstream_allocated = 0;
270 
271  av_freep(&ctx->slice_offsets);
272  ctx->nb_slices = 0;
273  ctx->slice_offsets_allocated = 0;
274 
277 
278  return 0;
279 }
280 
282 {
284 }
285 
287 {
288  return av_buffer_create(NULL, 0, NULL, NULL, 0);
289 }
290 
291 static int nvdec_init_hwframes(AVCodecContext *avctx, AVBufferRef **out_frames_ref, int dummy)
292 {
293  AVHWFramesContext *frames_ctx;
294  int ret;
295 
297  avctx->hw_device_ctx,
298  avctx->hwaccel->pix_fmt,
299  out_frames_ref);
300  if (ret < 0)
301  return ret;
302 
303  frames_ctx = (AVHWFramesContext*)(*out_frames_ref)->data;
304 
305  if (dummy) {
306  // Copied from ff_decode_get_hw_frames_ctx for compatibility
307  frames_ctx->initial_pool_size += 3;
308 
309  frames_ctx->free = nvdec_free_dummy;
310  frames_ctx->pool = av_buffer_pool_init(0, nvdec_alloc_dummy);
311 
312  if (!frames_ctx->pool) {
313  av_buffer_unref(out_frames_ref);
314  return AVERROR(ENOMEM);
315  }
316  } else {
317  // This is normally not used to actually allocate frames from
318  frames_ctx->initial_pool_size = 0;
319  }
320 
321  ret = av_hwframe_ctx_init(*out_frames_ref);
322  if (ret < 0) {
323  av_buffer_unref(out_frames_ref);
324  return ret;
325  }
326 
327  return 0;
328 }
329 
331 {
333 
336  NVDECFramePool *pool;
337  AVHWFramesContext *frames_ctx;
338  const AVPixFmtDescriptor *sw_desc;
339 
340  CUVIDDECODECREATEINFO params = { 0 };
341 
342  cudaVideoSurfaceFormat output_format;
343  int cuvid_codec_type, cuvid_chroma_format, chroma_444;
344  int ret = 0;
345 
346  sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
347  if (!sw_desc)
348  return AVERROR_BUG;
349 
350  cuvid_codec_type = map_avcodec_id(avctx->codec_id);
351  if (cuvid_codec_type < 0) {
352  av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
353  return AVERROR_BUG;
354  }
355 
356  cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
357  if (cuvid_chroma_format < 0) {
358  av_log(avctx, AV_LOG_ERROR, "Unsupported chroma format\n");
359  return AVERROR(ENOSYS);
360  }
361  chroma_444 = ctx->supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
362 
363  if (!avctx->hw_frames_ctx) {
364  ret = nvdec_init_hwframes(avctx, &avctx->hw_frames_ctx, 1);
365  if (ret < 0)
366  return ret;
367 
368  ret = nvdec_init_hwframes(avctx, &real_hw_frames_ref, 0);
369  if (ret < 0)
370  return ret;
371  } else {
372  real_hw_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
373  if (!real_hw_frames_ref)
374  return AVERROR(ENOMEM);
375  }
376 
377  switch (sw_desc->comp[0].depth) {
378  case 8:
379  output_format = chroma_444 ? cudaVideoSurfaceFormat_YUV444 :
380  cudaVideoSurfaceFormat_NV12;
381  break;
382  case 10:
383  case 12:
384  output_format = chroma_444 ? cudaVideoSurfaceFormat_YUV444_16Bit :
385  cudaVideoSurfaceFormat_P016;
386  break;
387  default:
388  av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth\n");
389  av_buffer_unref(&real_hw_frames_ref);
390  return AVERROR(ENOSYS);
391  }
392 
393  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
394 
395  params.ulWidth = avctx->coded_width;
396  params.ulHeight = avctx->coded_height;
397  params.ulTargetWidth = avctx->coded_width;
398  params.ulTargetHeight = avctx->coded_height;
399  params.bitDepthMinus8 = sw_desc->comp[0].depth - 8;
400  params.OutputFormat = output_format;
401  params.CodecType = cuvid_codec_type;
402  params.ChromaFormat = cuvid_chroma_format;
403  params.ulNumDecodeSurfaces = frames_ctx->initial_pool_size;
404  params.ulNumOutputSurfaces = frames_ctx->initial_pool_size;
405 
406  ret = nvdec_decoder_create(&ctx->decoder_ref, frames_ctx->device_ref, &params, avctx);
407  if (ret < 0) {
408  if (params.ulNumDecodeSurfaces > 32) {
409  av_log(avctx, AV_LOG_WARNING, "Using more than 32 (%d) decode surfaces might cause nvdec to fail.\n",
410  (int)params.ulNumDecodeSurfaces);
411  av_log(avctx, AV_LOG_WARNING, "Try lowering the amount of threads. Using %d right now.\n",
412  avctx->thread_count);
413  }
414  av_buffer_unref(&real_hw_frames_ref);
415  return ret;
416  }
417 
418  decoder = (NVDECDecoder*)ctx->decoder_ref->data;
419  decoder->real_hw_frames_ref = real_hw_frames_ref;
420  real_hw_frames_ref = NULL;
421 
422  pool = av_mallocz(sizeof(*pool));
423  if (!pool) {
424  ret = AVERROR(ENOMEM);
425  goto fail;
426  }
427  pool->dpb_size = frames_ctx->initial_pool_size;
428 
429  ctx->decoder_pool = av_buffer_pool_init2(sizeof(int), pool,
431  if (!ctx->decoder_pool) {
432  ret = AVERROR(ENOMEM);
433  goto fail;
434  }
435 
436  return 0;
437 fail:
438  ff_nvdec_decode_uninit(avctx);
439  return ret;
440 }
441 
442 static void nvdec_fdd_priv_free(void *priv)
443 {
444  NVDECFrame *cf = priv;
445 
446  if (!cf)
447  return;
448 
449  av_buffer_unref(&cf->idx_ref);
452 
453  av_freep(&priv);
454 }
455 
456 static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
457 {
458  NVDECFrame *unmap_data = (NVDECFrame*)data;
460  void *logctx = decoder->hw_device_ref->data;
461  CUdeviceptr devptr = (CUdeviceptr)opaque;
462  int ret;
463  CUcontext dummy;
464 
465  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
466  if (ret < 0)
467  goto finish;
468 
469  CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
470 
471  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
472 
473 finish:
474  av_buffer_unref(&unmap_data->idx_ref);
475  av_buffer_unref(&unmap_data->decoder_ref);
476  av_buffer_unref(&unmap_data->ref_idx_ref);
477  av_free(unmap_data);
478 }
479 
480 static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
481 {
483  NVDECFrame *cf = (NVDECFrame*)fdd->hwaccel_priv;
484  NVDECDecoder *decoder = (NVDECDecoder*)cf->decoder_ref->data;
485 
487 
488  CUVIDPROCPARAMS vpp = { 0 };
489  NVDECFrame *unmap_data = NULL;
490 
491  CUcontext dummy;
492  CUdeviceptr devptr;
493 
494  unsigned int pitch, i;
495  unsigned int offset = 0;
496  int shift_h = 0, shift_v = 0;
497  int ret = 0;
498 
499  vpp.progressive_frame = 1;
500  vpp.output_stream = decoder->stream;
501 
502  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
503  if (ret < 0)
504  return ret;
505 
506  ret = CHECK_CU(decoder->cvdl->cuvidMapVideoFrame(decoder->decoder,
507  cf->idx, &devptr,
508  &pitch, &vpp));
509  if (ret < 0)
510  goto finish;
511 
512  unmap_data = av_mallocz(sizeof(*unmap_data));
513  if (!unmap_data) {
514  ret = AVERROR(ENOMEM);
515  goto copy_fail;
516  }
517 
518  frame->buf[1] = av_buffer_create((uint8_t *)unmap_data, sizeof(*unmap_data),
519  nvdec_unmap_mapped_frame, (void*)devptr,
521  if (!frame->buf[1]) {
522  ret = AVERROR(ENOMEM);
523  goto copy_fail;
524  }
525 
528  if (!frame->hw_frames_ctx) {
529  ret = AVERROR(ENOMEM);
530  goto copy_fail;
531  }
532 
533  unmap_data->idx = cf->idx;
534  unmap_data->idx_ref = av_buffer_ref(cf->idx_ref);
535  unmap_data->decoder_ref = av_buffer_ref(cf->decoder_ref);
536 
537  av_pix_fmt_get_chroma_sub_sample(hwctx->sw_format, &shift_h, &shift_v);
538  for (i = 0; frame->linesize[i]; i++) {
539  frame->data[i] = (uint8_t*)(devptr + offset);
540  frame->linesize[i] = pitch;
541  offset += pitch * (frame->height >> (i ? shift_v : 0));
542  }
543 
544  goto finish;
545 
546 copy_fail:
547  if (!frame->buf[1]) {
548  CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
549  av_freep(&unmap_data);
550  } else {
551  av_buffer_unref(&frame->buf[1]);
552  }
553 
554 finish:
555  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
556  return ret;
557 }
558 
560 {
563  NVDECFrame *cf = NULL;
564  int ret;
565 
566  ctx->bitstream_len = 0;
567  ctx->nb_slices = 0;
568 
569  if (fdd->hwaccel_priv)
570  return 0;
571 
572  cf = av_mallocz(sizeof(*cf));
573  if (!cf)
574  return AVERROR(ENOMEM);
575 
576  cf->decoder_ref = av_buffer_ref(ctx->decoder_ref);
577  if (!cf->decoder_ref) {
578  ret = AVERROR(ENOMEM);
579  goto fail;
580  }
581 
582  cf->idx_ref = av_buffer_pool_get(ctx->decoder_pool);
583  if (!cf->idx_ref) {
584  av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
585  ret = AVERROR(ENOMEM);
586  goto fail;
587  }
588  cf->ref_idx = cf->idx = *(unsigned int*)cf->idx_ref->data;
589 
590  fdd->hwaccel_priv = cf;
593 
594  return 0;
595 fail:
597  return ret;
598 
599 }
600 
602 {
605  NVDECFrame *cf;
606  int ret;
607 
608  ret = ff_nvdec_start_frame(avctx, frame);
609  if (ret < 0)
610  return ret;
611 
612  cf = fdd->hwaccel_priv;
613 
614  if (has_sep_ref) {
615  if (!cf->ref_idx_ref) {
616  cf->ref_idx_ref = av_buffer_pool_get(ctx->decoder_pool);
617  if (!cf->ref_idx_ref) {
618  av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
619  ret = AVERROR(ENOMEM);
620  goto fail;
621  }
622  }
623  cf->ref_idx = *(unsigned int*)cf->ref_idx_ref->data;
624  } else {
625  av_buffer_unref(&cf->ref_idx_ref);
626  cf->ref_idx = cf->idx;
627  }
628 
629  return 0;
630 fail:
632  return ret;
633 }
634 
636 {
639  void *logctx = avctx;
640  CUVIDPICPARAMS *pp = &ctx->pic_params;
641 
642  CUcontext dummy;
643 
644  int ret = 0;
645 
646  pp->nBitstreamDataLen = ctx->bitstream_len;
647  pp->pBitstreamData = ctx->bitstream;
648  pp->nNumSlices = ctx->nb_slices;
649  pp->pSliceDataOffsets = ctx->slice_offsets;
650 
651  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
652  if (ret < 0)
653  return ret;
654 
655  ret = CHECK_CU(decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params));
656  if (ret < 0)
657  goto finish;
658 
659 finish:
660  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
661 
662  return ret;
663 }
664 
666 {
668  int ret = ff_nvdec_end_frame(avctx);
669  ctx->bitstream = NULL;
670  return ret;
671 }
672 
674  uint32_t size)
675 {
677  void *tmp;
678 
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 
755  fdd = (FrameDecodeData*)frame->private_ref->data;
756  cf = (NVDECFrame*)fdd->hwaccel_priv;
757  if (!cf)
758  return -1;
759 
760  return cf->ref_idx;
761 }
#define cudaVideoSurfaceFormat_YUV444_16Bit
Definition: nvdec.c:40
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:61
#define NULL
Definition: coverity.c:32
AVBufferRef * ref_idx_ref
Definition: nvdec.h:48
static enum AVPixelFormat pix_fmt
AVBufferRef * decoder_ref
Definition: nvdec.h:49
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:125
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_sep_ref)
Definition: nvdec.c:601
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:724
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:509
unsigned * slice_offsets
Definition: nvdec.h:64
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size, int supports_444)
Definition: nvdec.c:695
enum AVPixelFormat pix_fmt
Supported pixel format.
Definition: avcodec.h:2465
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:229
void(* hwaccel_priv_free)(void *priv)
Definition: decode.h:53
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:209
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:665
int nb_slices
Definition: nvdec.h:65
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1680
static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref, CUVIDDECODECREATEINFO *params, void *logctx)
Definition: nvdec.c:181
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Definition: frame.h:657
#define AV_PIX_FMT_P016
Definition: pixfmt.h:449
#define AV_PIX_FMT_P010
Definition: pixfmt.h:448
uint8_t * bitstream
Definition: nvdec.h:59
static AVBufferRef * nvdec_alloc_dummy(buffer_size_t size)
Definition: nvdec.c:286
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
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
CUVIDPICPARAMS pic_params
Definition: nvdec.h:53
static AVBufferRef * nvdec_decoder_frame_alloc(void *opaque, buffer_size_t size)
Definition: nvdec.c:245
unsigned int dpb_size
Definition: nvdec.c:56
AVBufferRef * private_ref
AVBufferRef for internal use by a single libav* library.
Definition: frame.h:697
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec.c:673
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:92
static void finish(void)
Definition: movenc.c:342
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:128
AVBufferRef * decoder_ref
Definition: nvdec.h:57
unsigned int ref_idx
Definition: nvdec.h:46
ptrdiff_t size
Definition: opengl_enc.c:100
CUvideodecoder decoder
Definition: nvdec.c:44
#define av_log(a,...)
static int nvdec_test_capabilities(NVDECDecoder *decoder, CUVIDDECODECREATEINFO *params, void *logctx)
Definition: nvdec.c:101
CUcontext cuda_ctx
Definition: nvdec.c:48
static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
Definition: nvdec.c:456
static void nvdec_free_dummy(struct AVHWFramesContext *ctx)
Definition: nvdec.c:281
static int map_chroma_format(enum AVPixelFormat pix_fmt)
Definition: nvdec.c:82
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
error code definitions
static AVBufferRef * hw_device_ctx
Definition: hw_decode.c:45
int buffer_size_t
Definition: internal.h:306
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:2601
int ff_nvdec_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:635
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: nvdec.c:559
GLenum GLint * params
Definition: opengl_enc.c:113
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
static void nvdec_fdd_priv_free(void *priv)
Definition: nvdec.c:442
AVBufferRef * av_buffer_create(uint8_t *data, buffer_size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:29
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:333
AVBufferRef * real_hw_frames_ref
Definition: nvdec.c:47
int supports_444
Definition: nvdec.h:68
unsigned int idx
Definition: nvdec.h:45
#define fail()
Definition: checkasm.h:133
uint8_t * bitstream_internal
Definition: nvdec.h:62
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition: nvdec.c:330
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:199
int dpb_size
Definition: h264_levels.c:107
CUstream stream
Definition: nvdec.c:49
static int nvdec_init_hwframes(AVCodecContext *avctx, AVBufferRef **out_frames_ref, int dummy)
Definition: nvdec.c:291
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:2218
int bitstream_len
Definition: nvdec.h:60
AVFormatContext * ctx
Definition: movenc.c:48
static void nvdec_decoder_free(void *opaque, uint8_t *data)
Definition: nvdec.c:161
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
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:478
FFmpeg internal API for CUDA.
int dummy
Definition: motion.c:64
HW acceleration through CUDA.
Definition: pixfmt.h:235
AVBufferPool * av_buffer_pool_init2(buffer_size_t size, void *opaque, AVBufferRef *(*alloc)(void *opaque, buffer_size_t size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition: buffer.c:245
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:1773
if(ret)
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:45
CuvidFunctions * cvdl
Definition: nvdec.c:52
void(* free)(struct AVHWFramesContext *ctx)
This field may be set by the caller before calling av_hwframe_ctx_init().
Definition: hwcontext.h:170
Libavcodec external API header.
unsigned int bitstream_allocated
Definition: nvdec.h:61
enum AVCodecID codec_id
Definition: avcodec.h:546
AVBufferRef * av_buffer_alloc(buffer_size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
main external API structure.
Definition: avcodec.h:536
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition: nvdec.c:262
uint8_t * data
The data buffer.
Definition: buffer.h:92
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
This struct is allocated as AVHWDeviceContext.hwctx.
int coded_height
Definition: avcodec.h:724
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:308
unsigned int slice_offsets_allocated
Definition: nvdec.h:66
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
AVBufferRef * idx_ref
Definition: nvdec.h:47
unsigned int nb_allocated
Definition: nvdec.c:57
static int map_avcodec_id(enum AVCodecID id)
Definition: nvdec.c:62
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:141
A reference to a data buffer.
Definition: buffer.h:84
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition: decode.h:34
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:1219
common internal api header.
common internal and external API header
AVBufferPool * av_buffer_pool_init(buffer_size_t size, AVBufferRef *(*alloc)(buffer_size_t size))
Allocate and initialize a buffer pool.
Definition: buffer.c:266
int ff_nvdec_get_ref_idx(AVFrame *frame)
Definition: nvdec.c:747
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:175
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
#define CHECK_CU(x)
Definition: nvdec.c:60
#define cudaVideoSurfaceFormat_YUV444
Definition: nvdec.c:39
pixel format definitions
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Definition: hwcontext.h:190
#define av_free(p)
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:571
CudaFunctions * cudl
Definition: nvdec.c:51
AVBufferPool * decoder_pool
Definition: nvdec.h:55
int height
Definition: frame.h:376
FILE * out
Definition: movenc.c:54
#define av_freep(p)
static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
Definition: nvdec.c:480
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:373
void * hwaccel_priv
Per-frame private data for hwaccels.
Definition: decode.h:52
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
int depth
Number of bits in the component.
Definition: pixdesc.h:58
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:2270
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:222
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:2078
int i
Definition: input.c:407
AVBufferRef * hw_device_ref
Definition: nvdec.c:46
GLuint buffer
Definition: opengl_enc.c:101
static uint8_t tmp[11]
Definition: aes_ctr.c:27