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 
47  CUcontext cuda_ctx;
48  CUstream stream;
49 
50  CudaFunctions *cudl;
51  CuvidFunctions *cvdl;
52 } NVDECDecoder;
53 
54 typedef struct NVDECFramePool {
55  unsigned int dpb_size;
56  unsigned int nb_allocated;
58 
59 #define CHECK_CU(x) FF_CUDA_CHECK_DL(logctx, decoder->cudl, x)
60 
61 static int map_avcodec_id(enum AVCodecID id)
62 {
63  switch (id) {
64  case AV_CODEC_ID_H264: return cudaVideoCodec_H264;
65  case AV_CODEC_ID_HEVC: return cudaVideoCodec_HEVC;
66  case AV_CODEC_ID_MJPEG: return cudaVideoCodec_JPEG;
67  case AV_CODEC_ID_MPEG1VIDEO: return cudaVideoCodec_MPEG1;
68  case AV_CODEC_ID_MPEG2VIDEO: return cudaVideoCodec_MPEG2;
69  case AV_CODEC_ID_MPEG4: return cudaVideoCodec_MPEG4;
70  case AV_CODEC_ID_VC1: return cudaVideoCodec_VC1;
71  case AV_CODEC_ID_VP8: return cudaVideoCodec_VP8;
72  case AV_CODEC_ID_VP9: return cudaVideoCodec_VP9;
73  case AV_CODEC_ID_WMV3: return cudaVideoCodec_VC1;
74  }
75  return -1;
76 }
77 
79 {
80  int shift_h = 0, shift_v = 0;
81 
82  av_pix_fmt_get_chroma_sub_sample(pix_fmt, &shift_h, &shift_v);
83 
84  if (shift_h == 1 && shift_v == 1)
85  return cudaVideoChromaFormat_420;
86  else if (shift_h == 1 && shift_v == 0)
87  return cudaVideoChromaFormat_422;
88  else if (shift_h == 0 && shift_v == 0)
89  return cudaVideoChromaFormat_444;
90 
91  return -1;
92 }
93 
95  CUVIDDECODECREATEINFO *params, void *logctx)
96 {
97  int ret;
98  CUVIDDECODECAPS caps = { 0 };
99 
100  caps.eCodecType = params->CodecType;
101  caps.eChromaFormat = params->ChromaFormat;
102  caps.nBitDepthMinus8 = params->bitDepthMinus8;
103 
104  if (!decoder->cvdl->cuvidGetDecoderCaps) {
105  av_log(logctx, AV_LOG_WARNING, "Used Nvidia driver is too old to perform a capability check.\n");
106  av_log(logctx, AV_LOG_WARNING, "The minimum required version is "
107 #if defined(_WIN32) || defined(__CYGWIN__)
108  "378.66"
109 #else
110  "378.13"
111 #endif
112  ". Continuing blind.\n");
113  return 0;
114  }
115 
116  ret = CHECK_CU(decoder->cvdl->cuvidGetDecoderCaps(&caps));
117  if (ret < 0)
118  return ret;
119 
120  av_log(logctx, AV_LOG_VERBOSE, "NVDEC capabilities:\n");
121  av_log(logctx, AV_LOG_VERBOSE, "format supported: %s, max_mb_count: %d\n",
122  caps.bIsSupported ? "yes" : "no", caps.nMaxMBCount);
123  av_log(logctx, AV_LOG_VERBOSE, "min_width: %d, max_width: %d\n",
124  caps.nMinWidth, caps.nMaxWidth);
125  av_log(logctx, AV_LOG_VERBOSE, "min_height: %d, max_height: %d\n",
126  caps.nMinHeight, caps.nMaxHeight);
127 
128  if (!caps.bIsSupported) {
129  av_log(logctx, AV_LOG_ERROR, "Hardware is lacking required capabilities\n");
130  return AVERROR(EINVAL);
131  }
132 
133  if (params->ulWidth > caps.nMaxWidth || params->ulWidth < caps.nMinWidth) {
134  av_log(logctx, AV_LOG_ERROR, "Video width %d not within range from %d to %d\n",
135  (int)params->ulWidth, caps.nMinWidth, caps.nMaxWidth);
136  return AVERROR(EINVAL);
137  }
138 
139  if (params->ulHeight > caps.nMaxHeight || params->ulHeight < caps.nMinHeight) {
140  av_log(logctx, AV_LOG_ERROR, "Video height %d not within range from %d to %d\n",
141  (int)params->ulHeight, caps.nMinHeight, caps.nMaxHeight);
142  return AVERROR(EINVAL);
143  }
144 
145  if ((params->ulWidth * params->ulHeight) / 256 > caps.nMaxMBCount) {
146  av_log(logctx, AV_LOG_ERROR, "Video macroblock count %d exceeds maximum of %d\n",
147  (int)(params->ulWidth * params->ulHeight) / 256, caps.nMaxMBCount);
148  return AVERROR(EINVAL);
149  }
150 
151  return 0;
152 }
153 
154 static void nvdec_decoder_free(void *opaque, uint8_t *data)
155 {
157 
158  if (decoder->decoder) {
159  void *logctx = decoder->hw_device_ref->data;
160  CUcontext dummy;
161  CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
162  CHECK_CU(decoder->cvdl->cuvidDestroyDecoder(decoder->decoder));
163  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
164  }
165 
166  av_buffer_unref(&decoder->hw_device_ref);
167 
168  cuvid_free_functions(&decoder->cvdl);
169 
170  av_freep(&decoder);
171 }
172 
173 static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref,
174  CUVIDDECODECREATEINFO *params, void *logctx)
175 {
177  AVCUDADeviceContext *device_hwctx = hw_device_ctx->hwctx;
178 
179  AVBufferRef *decoder_ref;
181 
182  CUcontext dummy;
183  int ret;
184 
185  decoder = av_mallocz(sizeof(*decoder));
186  if (!decoder)
187  return AVERROR(ENOMEM);
188 
189  decoder_ref = av_buffer_create((uint8_t*)decoder, sizeof(*decoder),
191  if (!decoder_ref) {
192  av_freep(&decoder);
193  return AVERROR(ENOMEM);
194  }
195 
196  decoder->hw_device_ref = av_buffer_ref(hw_device_ref);
197  if (!decoder->hw_device_ref) {
198  ret = AVERROR(ENOMEM);
199  goto fail;
200  }
201  decoder->cuda_ctx = device_hwctx->cuda_ctx;
202  decoder->cudl = device_hwctx->internal->cuda_dl;
203  decoder->stream = device_hwctx->stream;
204 
205  ret = cuvid_load_functions(&decoder->cvdl, logctx);
206  if (ret < 0) {
207  av_log(logctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n");
208  goto fail;
209  }
210 
211  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
212  if (ret < 0)
213  goto fail;
214 
216  if (ret < 0) {
217  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
218  goto fail;
219  }
220 
221  ret = CHECK_CU(decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params));
222 
223  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
224 
225  if (ret < 0) {
226  goto fail;
227  }
228 
229  *out = decoder_ref;
230 
231  return 0;
232 fail:
233  av_buffer_unref(&decoder_ref);
234  return ret;
235 }
236 
237 static AVBufferRef *nvdec_decoder_frame_alloc(void *opaque, int size)
238 {
239  NVDECFramePool *pool = opaque;
240  AVBufferRef *ret;
241 
242  if (pool->nb_allocated >= pool->dpb_size)
243  return NULL;
244 
245  ret = av_buffer_alloc(sizeof(unsigned int));
246  if (!ret)
247  return NULL;
248 
249  *(unsigned int*)ret->data = pool->nb_allocated++;
250 
251  return ret;
252 }
253 
255 {
257 
258  av_freep(&ctx->bitstream);
259  ctx->bitstream_len = 0;
260  ctx->bitstream_allocated = 0;
261 
262  av_freep(&ctx->slice_offsets);
263  ctx->nb_slices = 0;
264  ctx->slice_offsets_allocated = 0;
265 
266  av_buffer_unref(&ctx->decoder_ref);
267  av_buffer_pool_uninit(&ctx->decoder_pool);
268 
269  return 0;
270 }
271 
273 {
275 
276  NVDECFramePool *pool;
277  AVHWFramesContext *frames_ctx;
278  const AVPixFmtDescriptor *sw_desc;
279 
280  CUVIDDECODECREATEINFO params = { 0 };
281 
282  cudaVideoSurfaceFormat output_format;
283  int cuvid_codec_type, cuvid_chroma_format, chroma_444;
284  int ret = 0;
285 
286  sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
287  if (!sw_desc)
288  return AVERROR_BUG;
289 
290  cuvid_codec_type = map_avcodec_id(avctx->codec_id);
291  if (cuvid_codec_type < 0) {
292  av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
293  return AVERROR_BUG;
294  }
295 
296  cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
297  if (cuvid_chroma_format < 0) {
298  av_log(avctx, AV_LOG_ERROR, "Unsupported chroma format\n");
299  return AVERROR(ENOSYS);
300  }
301  chroma_444 = ctx->supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
302 
303  if (!avctx->hw_frames_ctx) {
305  if (ret < 0)
306  return ret;
307  }
308 
309  switch (sw_desc->comp[0].depth) {
310  case 8:
311  output_format = chroma_444 ? cudaVideoSurfaceFormat_YUV444 :
312  cudaVideoSurfaceFormat_NV12;
313  break;
314  case 10:
315  case 12:
316  output_format = chroma_444 ? cudaVideoSurfaceFormat_YUV444_16Bit :
317  cudaVideoSurfaceFormat_P016;
318  break;
319  default:
320  av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth\n");
321  return AVERROR(ENOSYS);
322  }
323 
324  frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
325 
326  params.ulWidth = avctx->coded_width;
327  params.ulHeight = avctx->coded_height;
328  params.ulTargetWidth = avctx->coded_width;
329  params.ulTargetHeight = avctx->coded_height;
330  params.bitDepthMinus8 = sw_desc->comp[0].depth - 8;
331  params.OutputFormat = output_format;
332  params.CodecType = cuvid_codec_type;
333  params.ChromaFormat = cuvid_chroma_format;
334  params.ulNumDecodeSurfaces = frames_ctx->initial_pool_size;
335  params.ulNumOutputSurfaces = frames_ctx->initial_pool_size;
336 
337  ret = nvdec_decoder_create(&ctx->decoder_ref, frames_ctx->device_ref, &params, avctx);
338  if (ret < 0) {
339  if (params.ulNumDecodeSurfaces > 32) {
340  av_log(avctx, AV_LOG_WARNING, "Using more than 32 (%d) decode surfaces might cause nvdec to fail.\n",
341  (int)params.ulNumDecodeSurfaces);
342  av_log(avctx, AV_LOG_WARNING, "Try lowering the amount of threads. Using %d right now.\n",
343  avctx->thread_count);
344  }
345  return ret;
346  }
347 
348  pool = av_mallocz(sizeof(*pool));
349  if (!pool) {
350  ret = AVERROR(ENOMEM);
351  goto fail;
352  }
353  pool->dpb_size = frames_ctx->initial_pool_size;
354 
355  ctx->decoder_pool = av_buffer_pool_init2(sizeof(int), pool,
357  if (!ctx->decoder_pool) {
358  ret = AVERROR(ENOMEM);
359  goto fail;
360  }
361 
362  return 0;
363 fail:
364  ff_nvdec_decode_uninit(avctx);
365  return ret;
366 }
367 
368 static void nvdec_fdd_priv_free(void *priv)
369 {
370  NVDECFrame *cf = priv;
371 
372  if (!cf)
373  return;
374 
375  av_buffer_unref(&cf->idx_ref);
377 
378  av_freep(&priv);
379 }
380 
381 static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
382 {
383  NVDECFrame *unmap_data = (NVDECFrame*)data;
385  void *logctx = decoder->hw_device_ref->data;
386  CUdeviceptr devptr = (CUdeviceptr)opaque;
387  int ret;
388  CUcontext dummy;
389 
390  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
391  if (ret < 0)
392  goto finish;
393 
394  CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
395 
396  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
397 
398 finish:
399  av_buffer_unref(&unmap_data->idx_ref);
400  av_buffer_unref(&unmap_data->decoder_ref);
401  av_free(unmap_data);
402 }
403 
404 static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
405 {
406  FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
407  NVDECFrame *cf = (NVDECFrame*)fdd->hwaccel_priv;
408  NVDECDecoder *decoder = (NVDECDecoder*)cf->decoder_ref->data;
409 
410  AVHWFramesContext *hwctx = (AVHWFramesContext *)frame->hw_frames_ctx->data;
411 
412  CUVIDPROCPARAMS vpp = { 0 };
413  NVDECFrame *unmap_data = NULL;
414 
415  CUcontext dummy;
416  CUdeviceptr devptr;
417 
418  unsigned int pitch, i;
419  unsigned int offset = 0;
420  int shift_h = 0, shift_v = 0;
421  int ret = 0;
422 
423  vpp.progressive_frame = 1;
424  vpp.output_stream = decoder->stream;
425 
426  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
427  if (ret < 0)
428  return ret;
429 
430  ret = CHECK_CU(decoder->cvdl->cuvidMapVideoFrame(decoder->decoder,
431  cf->idx, &devptr,
432  &pitch, &vpp));
433  if (ret < 0)
434  goto finish;
435 
436  unmap_data = av_mallocz(sizeof(*unmap_data));
437  if (!unmap_data) {
438  ret = AVERROR(ENOMEM);
439  goto copy_fail;
440  }
441 
442  frame->buf[1] = av_buffer_create((uint8_t *)unmap_data, sizeof(*unmap_data),
443  nvdec_unmap_mapped_frame, (void*)devptr,
445  if (!frame->buf[1]) {
446  ret = AVERROR(ENOMEM);
447  goto copy_fail;
448  }
449 
450  unmap_data->idx = cf->idx;
451  unmap_data->idx_ref = av_buffer_ref(cf->idx_ref);
452  unmap_data->decoder_ref = av_buffer_ref(cf->decoder_ref);
453 
454  av_pix_fmt_get_chroma_sub_sample(hwctx->sw_format, &shift_h, &shift_v);
455  for (i = 0; frame->linesize[i]; i++) {
456  frame->data[i] = (uint8_t*)(devptr + offset);
457  frame->linesize[i] = pitch;
458  offset += pitch * (frame->height >> (i ? shift_v : 0));
459  }
460 
461  goto finish;
462 
463 copy_fail:
464  if (!frame->buf[1]) {
465  CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
466  av_freep(&unmap_data);
467  } else {
468  av_buffer_unref(&frame->buf[1]);
469  }
470 
471 finish:
472  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
473  return ret;
474 }
475 
477 {
479  FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
480  NVDECFrame *cf = NULL;
481  int ret;
482 
483  ctx->bitstream_len = 0;
484  ctx->nb_slices = 0;
485 
486  if (fdd->hwaccel_priv)
487  return 0;
488 
489  cf = av_mallocz(sizeof(*cf));
490  if (!cf)
491  return AVERROR(ENOMEM);
492 
493  cf->decoder_ref = av_buffer_ref(ctx->decoder_ref);
494  if (!cf->decoder_ref) {
495  ret = AVERROR(ENOMEM);
496  goto fail;
497  }
498 
499  cf->idx_ref = av_buffer_pool_get(ctx->decoder_pool);
500  if (!cf->idx_ref) {
501  av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
502  ret = AVERROR(ENOMEM);
503  goto fail;
504  }
505  cf->idx = *(unsigned int*)cf->idx_ref->data;
506 
507  fdd->hwaccel_priv = cf;
510 
511  return 0;
512 fail:
514  return ret;
515 
516 }
517 
519 {
521  NVDECDecoder *decoder = (NVDECDecoder*)ctx->decoder_ref->data;
522  void *logctx = avctx;
523  CUVIDPICPARAMS *pp = &ctx->pic_params;
524 
525  CUcontext dummy;
526 
527  int ret = 0;
528 
529  pp->nBitstreamDataLen = ctx->bitstream_len;
530  pp->pBitstreamData = ctx->bitstream;
531  pp->nNumSlices = ctx->nb_slices;
532  pp->pSliceDataOffsets = ctx->slice_offsets;
533 
534  ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
535  if (ret < 0)
536  return ret;
537 
538  ret = CHECK_CU(decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params));
539  if (ret < 0)
540  goto finish;
541 
542 finish:
543  CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
544 
545  return ret;
546 }
547 
549 {
551  int ret = ff_nvdec_end_frame(avctx);
552  ctx->bitstream = NULL;
553  return ret;
554 }
555 
557  uint32_t size)
558 {
560  void *tmp;
561 
562  tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
563  (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
564  if (!tmp)
565  return AVERROR(ENOMEM);
566  ctx->slice_offsets = tmp;
567 
568  if (!ctx->bitstream)
569  ctx->bitstream = (uint8_t*)buffer;
570 
571  ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
572  ctx->bitstream_len += size;
573  ctx->nb_slices++;
574 
575  return 0;
576 }
577 
579 {
580  av_buffer_pool_uninit(&ctx->pool);
581 }
582 
584 {
585  return av_buffer_create(NULL, 0, NULL, NULL, 0);
586 }
587 
589  AVBufferRef *hw_frames_ctx,
590  int dpb_size,
591  int supports_444)
592 {
593  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
594  const AVPixFmtDescriptor *sw_desc;
595  int cuvid_codec_type, cuvid_chroma_format, chroma_444;
596 
597  sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
598  if (!sw_desc)
599  return AVERROR_BUG;
600 
601  cuvid_codec_type = map_avcodec_id(avctx->codec_id);
602  if (cuvid_codec_type < 0) {
603  av_log(avctx, AV_LOG_ERROR, "Unsupported codec ID\n");
604  return AVERROR_BUG;
605  }
606 
607  cuvid_chroma_format = map_chroma_format(avctx->sw_pix_fmt);
608  if (cuvid_chroma_format < 0) {
609  av_log(avctx, AV_LOG_VERBOSE, "Unsupported chroma format\n");
610  return AVERROR(EINVAL);
611  }
612  chroma_444 = supports_444 && cuvid_chroma_format == cudaVideoChromaFormat_444;
613 
614  frames_ctx->format = AV_PIX_FMT_CUDA;
615  frames_ctx->width = (avctx->coded_width + 1) & ~1;
616  frames_ctx->height = (avctx->coded_height + 1) & ~1;
617  /*
618  * We add two extra frames to the pool to account for deinterlacing filters
619  * holding onto their frames.
620  */
621  frames_ctx->initial_pool_size = dpb_size + 2;
622 
623  frames_ctx->free = nvdec_free_dummy;
624  frames_ctx->pool = av_buffer_pool_init(0, nvdec_alloc_dummy);
625 
626  if (!frames_ctx->pool)
627  return AVERROR(ENOMEM);
628 
629  switch (sw_desc->comp[0].depth) {
630  case 8:
631  frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_NV12;
632  break;
633  case 10:
634  frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P010;
635  break;
636  case 12:
637  frames_ctx->sw_format = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P016;
638  break;
639  default:
640  return AVERROR(EINVAL);
641  }
642 
643  return 0;
644 }
645 
647 {
648  FrameDecodeData *fdd;
649  NVDECFrame *cf;
650 
651  if (!frame || !frame->private_ref)
652  return -1;
653 
654  fdd = (FrameDecodeData*)frame->private_ref->data;
655  cf = (NVDECFrame*)fdd->hwaccel_priv;
656  if (!cf)
657  return -1;
658 
659  return cf->idx;
660 }
nvdec_decoder_frame_alloc
static AVBufferRef * nvdec_decoder_frame_alloc(void *opaque, int size)
Definition: nvdec.c:237
nvdec_decoder_create
static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref, CUVIDDECODECREATEINFO *params, void *logctx)
Definition: nvdec.c:173
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:235
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
av_buffer_alloc
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
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:61
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:2522
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:89
map_chroma_format
static int map_chroma_format(enum AVPixelFormat pix_fmt)
Definition: nvdec.c:78
NVDECFramePool
Definition: nvdec.c:54
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:208
NVDECDecoder::stream
CUstream stream
Definition: nvdec.c:48
ff_nvdec_get_ref_idx
int ff_nvdec_get_ref_idx(AVFrame *frame)
Definition: nvdec.c:646
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: avcodec.h:230
FrameDecodeData
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition: decode.h:34
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
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:169
AVComponentDescriptor::depth
int depth
Number of bits in the component.
Definition: pixdesc.h:58
data
const char data[16]
Definition: mxf.c:91
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
av_buffer_create
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
FrameDecodeData::hwaccel_priv_free
void(* hwaccel_priv_free)(void *priv)
Definition: decode.h:53
NVDECDecoder::decoder
CUvideodecoder decoder
Definition: nvdec.c:44
AVHWFramesContext::width
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:228
NVDECDecoder::cvdl
CuvidFunctions * cvdl
Definition: nvdec.c:51
AV_HWDEVICE_TYPE_CUDA
@ AV_HWDEVICE_TYPE_CUDA
Definition: hwcontext.h:30
av_buffer_pool_init
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
Definition: buffer.c:238
decoder
static const chunk_decoder decoder[8]
Definition: dfa.c:330
finish
static void finish(void)
Definition: movenc.c:345
ff_nvdec_start_frame
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: nvdec.c:476
fail
#define fail()
Definition: checkasm.h:120
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:2824
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:2550
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:1753
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
cudaVideoSurfaceFormat_YUV444
#define cudaVideoSurfaceFormat_YUV444
Definition: nvdec.c:39
AVHWFramesContext::height
int height
Definition: hwcontext.h:228
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:334
AVHWFramesContext::pool
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Definition: hwcontext.h:189
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:476
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:400
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:113
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: avcodec.h:386
nvdec_decoder_free
static void nvdec_decoder_free(void *opaque, uint8_t *data)
Definition: nvdec.c:154
NVDECFrame
Definition: nvdec.h:44
ctx
AVFormatContext * ctx
Definition: movenc.c:48
decode.h
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demuxing_decoding.c:40
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: avcodec.h:245
ff_decode_get_hw_frames_ctx
int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx, enum AVHWDeviceType dev_type)
Make sure avctx.hw_frames_ctx is set.
Definition: decode.c:1222
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:1575
NVDECDecoder::cudl
CudaFunctions * cudl
Definition: nvdec.c:50
dpb_size
int dpb_size
Definition: h264_levels.c:107
if
if(ret)
Definition: filter_design.txt:179
AV_CODEC_ID_WMV3
@ AV_CODEC_ID_WMV3
Definition: avcodec.h:289
ff_nvdec_simple_end_frame
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:548
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:221
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:125
ff_nvdec_decode_init
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition: nvdec.c:272
AVHWFramesContext::device_ref
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:140
NVDECDecoder::hw_device_ref
AVBufferRef * hw_device_ref
Definition: nvdec.c:46
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1600
av_buffer_pool_uninit
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:275
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: avcodec.h:219
error.h
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
cudaVideoSurfaceFormat_YUV444_16Bit
#define cudaVideoSurfaceFormat_YUV444_16Bit
Definition: nvdec.c:40
CHECK_CU
#define CHECK_CU(x)
Definition: nvdec.c:59
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:45
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:190
NVDECFrame::idx
unsigned int idx
Definition: nvdec.h:45
size
int size
Definition: twinvq_data.h:11134
nvdec_test_capabilities
static int nvdec_test_capabilities(NVDECDecoder *decoder, CUVIDDECODECREATEINFO *params, void *logctx)
Definition: nvdec.c:94
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
nvdec_free_dummy
static void nvdec_free_dummy(struct AVHWFramesContext *ctx)
Definition: nvdec.c:578
ff_nvdec_decode_uninit
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition: nvdec.c:254
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: avcodec.h:225
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
hw_device_ctx
static AVBufferRef * hw_device_ctx
Definition: hw_decode.c:45
NVDECDecoder::cuda_ctx
CUcontext cuda_ctx
Definition: nvdec.c:47
nvdec_alloc_dummy
static AVBufferRef * nvdec_alloc_dummy(int size)
Definition: nvdec.c:583
nvdec_fdd_priv_free
static void nvdec_fdd_priv_free(void *priv)
Definition: nvdec.c:368
ff_nvdec_end_frame
int ff_nvdec_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:518
common.h
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: avcodec.h:392
uint8_t
uint8_t
Definition: audio_convert.c:194
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:236
AV_CODEC_ID_VC1
@ AV_CODEC_ID_VC1
Definition: avcodec.h:288
av_buffer_pool_init2
AVBufferPool * av_buffer_pool_init2(int size, void *opaque, AVBufferRef *(*alloc)(void *opaque, int size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition: buffer.c:218
NVDECDecoder
Definition: nvdec.c:43
AV_PIX_FMT_P016
#define AV_PIX_FMT_P016
Definition: pixfmt.h:437
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:3262
avcodec.h
nvdec_retrieve_data
static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
Definition: nvdec.c:404
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:123
AVCUDADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_cuda.h:42
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
frame
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
Definition: filter_design.txt:264
cuda_check.h
params
const char const char * params
Definition: avisynth_c.h:867
NVDECFramePool::dpb_size
unsigned int dpb_size
Definition: nvdec.c:55
NVDECFramePool::nb_allocated
unsigned int nb_allocated
Definition: nvdec.c:56
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
ff_nvdec_simple_decode_slice
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec.c:556
nvdec_unmap_mapped_frame
static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
Definition: nvdec.c:381
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
config.h
dummy
int dummy
Definition: motion.c:64
ff_nvdec_frame_params
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size, int supports_444)
Definition: nvdec.c:588
AVPixFmtDescriptor::comp
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
av_buffer_ref
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
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:46
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:436
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1753
AVHWFramesContext::initial_pool_size
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:198
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:81
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
FrameDecodeData::hwaccel_priv
void * hwaccel_priv
Per-frame private data for hwaccels.
Definition: decode.h:52
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: avcodec.h:358
hwcontext.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:3112
NVDECContext
Definition: nvdec.h:50
NVDECFrame::decoder_ref
AVBufferRef * decoder_ref
Definition: nvdec.h:47
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220