FFmpeg
cuviddec.c
Go to the documentation of this file.
1 /*
2  * Nvidia CUVID decoder
3  * Copyright (c) 2016 Timo Rothenpieler <timo@rothenpieler.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config_components.h"
23 
25 
26 #include "libavutil/buffer.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/hwcontext.h"
30 #include "libavutil/cuda_check.h"
31 #include "libavutil/fifo.h"
32 #include "libavutil/log.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 
37 #include "avcodec.h"
38 #include "bsf.h"
39 #include "codec_internal.h"
40 #include "decode.h"
41 #include "hwconfig.h"
42 #include "nvdec.h"
43 #include "internal.h"
44 
45 #if !NVDECAPI_CHECK_VERSION(9, 0)
46 #define cudaVideoSurfaceFormat_YUV444 2
47 #define cudaVideoSurfaceFormat_YUV444_16Bit 3
48 #endif
49 
50 #if NVDECAPI_CHECK_VERSION(11, 0)
51 #define CUVID_HAS_AV1_SUPPORT
52 #endif
53 
54 typedef struct CuvidContext
55 {
57 
58  CUvideodecoder cudecoder;
59  CUvideoparser cuparser;
60 
61  /* This packet coincides with AVCodecInternal.in_pkt
62  * and is not owned by us. */
64 
65  char *cu_gpu;
68  char *crop_expr;
69  char *resize_expr;
70 
71  struct {
72  int left;
73  int top;
74  int right;
75  int bottom;
76  } crop;
77 
78  struct {
79  int width;
80  int height;
81  } resize;
82 
85 
87 
92 
95 
96  int *key_frame;
97 
98  cudaVideoCodec codec_type;
99  cudaVideoChromaFormat chroma_format;
100 
101  CUVIDDECODECAPS caps8, caps10, caps12;
102 
103  CUVIDPARSERPARAMS cuparseinfo;
104  CUVIDEOFORMATEX *cuparse_ext;
105 
106  CudaFunctions *cudl;
107  CuvidFunctions *cvdl;
108 } CuvidContext;
109 
110 typedef struct CuvidParsedFrame
111 {
112  CUVIDPARSERDISPINFO dispinfo;
116 
117 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x)
118 
119 // NV recommends [2;4] range
120 #define CUVID_MAX_DISPLAY_DELAY (4)
121 
122 // Actual pool size will be determined by parser.
123 #define CUVID_DEFAULT_NUM_SURFACES (CUVID_MAX_DISPLAY_DELAY + 1)
124 
125 static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* format)
126 {
127  AVCodecContext *avctx = opaque;
128  CuvidContext *ctx = avctx->priv_data;
129  AVHWFramesContext *hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
130  CUVIDDECODECAPS *caps = NULL;
131  CUVIDDECODECREATEINFO cuinfo;
132  int surface_fmt;
133  int chroma_444;
134  int old_nb_surfaces, fifo_size_inc, fifo_size_mul = 1;
135 
136  int old_width = avctx->width;
137  int old_height = avctx->height;
138 
140  AV_PIX_FMT_NONE, // Will be updated below
141  AV_PIX_FMT_NONE };
142 
143  av_log(avctx, AV_LOG_TRACE, "pfnSequenceCallback, progressive_sequence=%d\n", format->progressive_sequence);
144 
145  memset(&cuinfo, 0, sizeof(cuinfo));
146 
147  ctx->internal_error = 0;
148 
149  avctx->coded_width = cuinfo.ulWidth = format->coded_width;
150  avctx->coded_height = cuinfo.ulHeight = format->coded_height;
151 
152  // apply cropping
153  cuinfo.display_area.left = format->display_area.left + ctx->crop.left;
154  cuinfo.display_area.top = format->display_area.top + ctx->crop.top;
155  cuinfo.display_area.right = format->display_area.right - ctx->crop.right;
156  cuinfo.display_area.bottom = format->display_area.bottom - ctx->crop.bottom;
157 
158  // width and height need to be set before calling ff_get_format
159  if (ctx->resize_expr) {
160  avctx->width = ctx->resize.width;
161  avctx->height = ctx->resize.height;
162  } else {
163  avctx->width = cuinfo.display_area.right - cuinfo.display_area.left;
164  avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top;
165  }
166 
167  // NVDEC target dimensions must be even-aligned for internal surface allocation.
168  // For chroma-subsampled formats (420/422), the output dimensions must also be
169  // even. For monochrome/444, keep the original output dimensions and only
170  // even-align the NVDEC target — the frame copy will crop to avctx dimensions.
171  cuinfo.ulTargetWidth = (avctx->width + 1) & ~1;
172  cuinfo.ulTargetHeight = (avctx->height + 1) & ~1;
173  if (format->chroma_format == cudaVideoChromaFormat_420 ||
174  format->chroma_format == cudaVideoChromaFormat_422) {
175  avctx->width = cuinfo.ulTargetWidth;
176  avctx->height = cuinfo.ulTargetHeight;
177  }
178 
179  // aspect ratio conversion, 1:1, depends on scaled resolution
180  cuinfo.target_rect.left = 0;
181  cuinfo.target_rect.top = 0;
182  cuinfo.target_rect.right = cuinfo.ulTargetWidth;
183  cuinfo.target_rect.bottom = cuinfo.ulTargetHeight;
184 
185  chroma_444 = format->chroma_format == cudaVideoChromaFormat_444;
186 
187  switch (format->bit_depth_luma_minus8) {
188  case 0: // 8-bit
189  if (chroma_444) {
191 #ifdef NVDEC_HAVE_422_SUPPORT
192  } else if (format->chroma_format == cudaVideoChromaFormat_422) {
194 #endif
195  } else {
197  }
198  caps = &ctx->caps8;
199  break;
200  case 2: // 10-bit
201  if (chroma_444) {
202 #if FF_API_NVDEC_OLD_PIX_FMTS
204 #else
206 #endif
207 #ifdef NVDEC_HAVE_422_SUPPORT
208  } else if (format->chroma_format == cudaVideoChromaFormat_422) {
209 #if FF_API_NVDEC_OLD_PIX_FMTS
211 #else
213 #endif
214 #endif
215  } else {
217  }
218  caps = &ctx->caps10;
219  break;
220  case 4: // 12-bit
221  if (chroma_444) {
222 #if FF_API_NVDEC_OLD_PIX_FMTS
224 #else
226 #endif
227 #ifdef NVDEC_HAVE_422_SUPPORT
228  } else if (format->chroma_format == cudaVideoChromaFormat_422) {
229 #if FF_API_NVDEC_OLD_PIX_FMTS
231 #else
233 #endif
234 #endif
235  } else {
236 #if FF_API_NVDEC_OLD_PIX_FMTS
238 #else
240 #endif
241  }
242  caps = &ctx->caps12;
243  break;
244  default:
245  break;
246  }
247 
248  if (!caps || !caps->bIsSupported) {
249  av_log(avctx, AV_LOG_ERROR, "unsupported bit depth: %d\n",
250  format->bit_depth_luma_minus8 + 8);
251  ctx->internal_error = AVERROR(EINVAL);
252  return 0;
253  }
254 
255  surface_fmt = ff_get_format(avctx, pix_fmts);
256  if (surface_fmt < 0) {
257  av_log(avctx, AV_LOG_ERROR, "ff_get_format failed: %d\n", surface_fmt);
258  ctx->internal_error = AVERROR(EINVAL);
259  return 0;
260  }
261 
262  av_log(avctx, AV_LOG_VERBOSE, "Formats: Original: %s | HW: %s | SW: %s\n",
263  av_get_pix_fmt_name(avctx->pix_fmt),
264  av_get_pix_fmt_name(surface_fmt),
265  av_get_pix_fmt_name(avctx->sw_pix_fmt));
266 
267  avctx->pix_fmt = surface_fmt;
268 
269  // Update our hwframe ctx, as the get_format callback might have refreshed it!
270  if (avctx->hw_frames_ctx) {
271  av_buffer_unref(&ctx->hwframe);
272 
273  ctx->hwframe = av_buffer_ref(avctx->hw_frames_ctx);
274  if (!ctx->hwframe) {
275  ctx->internal_error = AVERROR(ENOMEM);
276  return 0;
277  }
278 
279  hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
280  }
281 
282  ff_set_sar(avctx, av_div_q(
283  (AVRational){ format->display_aspect_ratio.x, format->display_aspect_ratio.y },
284  (AVRational){ avctx->width, avctx->height }));
285 
286  ctx->deint_mode_current = format->progressive_sequence
287  ? cudaVideoDeinterlaceMode_Weave
288  : ctx->deint_mode;
289 
290  ctx->progressive_sequence = format->progressive_sequence;
291 
292  if (!format->progressive_sequence && ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
293  avctx->flags |= AV_CODEC_FLAG_INTERLACED_DCT;
294  else
295  avctx->flags &= ~AV_CODEC_FLAG_INTERLACED_DCT;
296 
297  if (format->video_signal_description.video_full_range_flag)
298  avctx->color_range = AVCOL_RANGE_JPEG;
299  else
300  avctx->color_range = AVCOL_RANGE_MPEG;
301 
302  avctx->color_primaries = format->video_signal_description.color_primaries;
303  avctx->color_trc = format->video_signal_description.transfer_characteristics;
304  avctx->colorspace = format->video_signal_description.matrix_coefficients;
305 
306  if (format->bitrate)
307  avctx->bit_rate = format->bitrate;
308 
309  if (format->frame_rate.numerator && format->frame_rate.denominator) {
310  avctx->framerate.num = format->frame_rate.numerator;
311  avctx->framerate.den = format->frame_rate.denominator;
312  }
313 
314  if (ctx->cudecoder
315  && avctx->coded_width == format->coded_width
316  && avctx->coded_height == format->coded_height
317  && avctx->width == old_width
318  && avctx->height == old_height
319  && ctx->chroma_format == format->chroma_format
320  && ctx->codec_type == format->codec)
321  return 1;
322 
323  if (ctx->cudecoder) {
324  av_log(avctx, AV_LOG_TRACE, "Re-initializing decoder\n");
325  ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder));
326  if (ctx->internal_error < 0)
327  return 0;
328  ctx->cudecoder = NULL;
329  }
330 
331  if (hwframe_ctx->pool && (
332  hwframe_ctx->width < avctx->width ||
333  hwframe_ctx->height < avctx->height ||
334  hwframe_ctx->format != AV_PIX_FMT_CUDA ||
335  hwframe_ctx->sw_format != avctx->sw_pix_fmt)) {
336  av_log(avctx, AV_LOG_ERROR, "AVHWFramesContext is already initialized with incompatible parameters\n");
337  av_log(avctx, AV_LOG_DEBUG, "width: %d <-> %d\n", hwframe_ctx->width, avctx->width);
338  av_log(avctx, AV_LOG_DEBUG, "height: %d <-> %d\n", hwframe_ctx->height, avctx->height);
339  av_log(avctx, AV_LOG_DEBUG, "format: %s <-> cuda\n", av_get_pix_fmt_name(hwframe_ctx->format));
340  av_log(avctx, AV_LOG_DEBUG, "sw_format: %s <-> %s\n",
341  av_get_pix_fmt_name(hwframe_ctx->sw_format), av_get_pix_fmt_name(avctx->sw_pix_fmt));
342  ctx->internal_error = AVERROR(EINVAL);
343  return 0;
344  }
345 
346  ctx->chroma_format = format->chroma_format;
347 
348  cuinfo.CodecType = ctx->codec_type = format->codec;
349  cuinfo.ChromaFormat = format->chroma_format;
350 
351  switch (avctx->sw_pix_fmt) {
352  case AV_PIX_FMT_NV12:
353  cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
354  break;
355  case AV_PIX_FMT_P010:
356  case AV_PIX_FMT_P016:
357  cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
358  break;
359 #ifdef NVDEC_HAVE_422_SUPPORT
360  case AV_PIX_FMT_NV16:
361  cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV16;
362  break;
363  case AV_PIX_FMT_P216:
364  cuinfo.OutputFormat = cudaVideoSurfaceFormat_P216;
365  break;
366 #endif
367  case AV_PIX_FMT_YUV444P:
368  cuinfo.OutputFormat = cudaVideoSurfaceFormat_YUV444;
369  break;
371  cuinfo.OutputFormat = cudaVideoSurfaceFormat_YUV444_16Bit;
372  break;
373  default:
374  av_log(avctx, AV_LOG_ERROR, "Unsupported output format: %s\n",
375  av_get_pix_fmt_name(avctx->sw_pix_fmt));
376  ctx->internal_error = AVERROR(EINVAL);
377  return 0;
378  }
379 
380  if (ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field) {
381  avctx->framerate = av_mul_q(avctx->framerate, (AVRational){2, 1});
382  fifo_size_mul = 2;
383  }
384 
385  old_nb_surfaces = ctx->nb_surfaces;
386  ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, format->min_num_decode_surfaces + 3);
387  if (avctx->extra_hw_frames > 0)
388  ctx->nb_surfaces += avctx->extra_hw_frames;
389 
390  fifo_size_inc = ctx->nb_surfaces * fifo_size_mul - av_fifo_can_read(ctx->frame_queue) - av_fifo_can_write(ctx->frame_queue);
391  if (fifo_size_inc > 0 && av_fifo_grow2(ctx->frame_queue, fifo_size_inc) < 0) {
392  av_log(avctx, AV_LOG_ERROR, "Failed to grow frame queue on video sequence callback\n");
393  ctx->internal_error = AVERROR(ENOMEM);
394  return 0;
395  }
396 
397  if (ctx->nb_surfaces > old_nb_surfaces && av_reallocp_array(&ctx->key_frame, ctx->nb_surfaces, sizeof(int)) < 0) {
398  av_log(avctx, AV_LOG_ERROR, "Failed to grow key frame array on video sequence callback\n");
399  ctx->internal_error = AVERROR(ENOMEM);
400  return 0;
401  }
402 
403  cuinfo.ulNumDecodeSurfaces = ctx->nb_surfaces;
404  cuinfo.ulNumOutputSurfaces = 1;
405  cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
406  cuinfo.bitDepthMinus8 = format->bit_depth_luma_minus8;
407  cuinfo.DeinterlaceMode = ctx->deint_mode_current;
408 
409  ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidCreateDecoder(&ctx->cudecoder, &cuinfo));
410  if (ctx->internal_error < 0)
411  return 0;
412 
413  if (!hwframe_ctx->pool) {
414  hwframe_ctx->format = AV_PIX_FMT_CUDA;
415  hwframe_ctx->sw_format = avctx->sw_pix_fmt;
416  hwframe_ctx->width = avctx->width;
417  hwframe_ctx->height = avctx->height;
418 
419  if ((ctx->internal_error = av_hwframe_ctx_init(ctx->hwframe)) < 0) {
420  av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_init failed\n");
421  return 0;
422  }
423  }
424 
425  if(ctx->cuparseinfo.ulMaxNumDecodeSurfaces != cuinfo.ulNumDecodeSurfaces) {
426  ctx->cuparseinfo.ulMaxNumDecodeSurfaces = cuinfo.ulNumDecodeSurfaces;
427  return cuinfo.ulNumDecodeSurfaces;
428  }
429 
430  return 1;
431 }
432 
433 static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS* picparams)
434 {
435  AVCodecContext *avctx = opaque;
436  CuvidContext *ctx = avctx->priv_data;
437 
438  av_log(avctx, AV_LOG_TRACE, "pfnDecodePicture\n");
439 
440  if(picparams->intra_pic_flag)
441  ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
442 
443  ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidDecodePicture(ctx->cudecoder, picparams));
444  if (ctx->internal_error < 0)
445  return 0;
446 
447  return 1;
448 }
449 
450 static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO* dispinfo)
451 {
452  AVCodecContext *avctx = opaque;
453  CuvidContext *ctx = avctx->priv_data;
454  CuvidParsedFrame parsed_frame = { { 0 } };
455  int ret;
456 
457  parsed_frame.dispinfo = *dispinfo;
458  ctx->internal_error = 0;
459 
460  // For some reason, dispinfo->progressive_frame is sometimes wrong.
461  parsed_frame.dispinfo.progressive_frame = ctx->progressive_sequence;
462 
463  if (ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
464  ret = av_fifo_write(ctx->frame_queue, &parsed_frame, 1);
465  if (ret < 0)
466  av_log(avctx, AV_LOG_ERROR, "Writing frame to fifo failed!\n");
467  } else {
468  parsed_frame.is_deinterlacing = 1;
469  ret = av_fifo_write(ctx->frame_queue, &parsed_frame, 1);
470  if (ret < 0)
471  av_log(avctx, AV_LOG_ERROR, "Writing first frame to fifo failed!\n");
472 
473  if (!ctx->drop_second_field) {
474  parsed_frame.second_field = 1;
475  ret = av_fifo_write(ctx->frame_queue, &parsed_frame, 1);
476  if (ret < 0)
477  av_log(avctx, AV_LOG_ERROR, "Writing second frame to fifo failed!\n");
478  }
479  }
480 
481  return 1;
482 }
483 
485 {
486  CuvidContext *ctx = avctx->priv_data;
487 
488  int shift = 0;
489  if (ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field)
490  shift = 1;
491 
492  // shift/divide frame count to ensure the buffer is still signalled full if one half-frame has already been returned when deinterlacing.
493  return ((av_fifo_can_read(ctx->frame_queue) + shift) >> shift) + ctx->cuparseinfo.ulMaxDisplayDelay >= ctx->nb_surfaces;
494 }
495 
496 static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
497 {
498  CuvidContext *ctx = avctx->priv_data;
499  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
500  AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
501  CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
502  CUVIDSOURCEDATAPACKET cupkt;
503  int ret = 0, eret = 0, is_flush = ctx->decoder_flushing;
504 
505  av_log(avctx, AV_LOG_TRACE, "cuvid_decode_packet\n");
506 
507  if (is_flush && avpkt && avpkt->size)
508  return AVERROR_EOF;
509 
510  if (cuvid_is_buffer_full(avctx) && avpkt && avpkt->size)
511  return AVERROR(EAGAIN);
512 
513  ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
514  if (ret < 0) {
515  return ret;
516  }
517 
518  memset(&cupkt, 0, sizeof(cupkt));
519 
520  if (avpkt && avpkt->size) {
521  cupkt.payload_size = avpkt->size;
522  cupkt.payload = avpkt->data;
523 
524  if (avpkt->pts != AV_NOPTS_VALUE) {
525  cupkt.flags = CUVID_PKT_TIMESTAMP;
526  if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
527  cupkt.timestamp = av_rescale_q(avpkt->pts, avctx->pkt_timebase, (AVRational){1, 10000000});
528  else
529  cupkt.timestamp = avpkt->pts;
530  }
531  } else {
532  cupkt.flags = CUVID_PKT_ENDOFSTREAM;
533  ctx->decoder_flushing = 1;
534  }
535 
536  // When flushing, only actually flush cuvid when the output buffer has been fully emptied.
537  // CUVID happily dumps out a ton of frames with no regard for its own available surfaces.
538  if (!ctx->decoder_flushing || (ctx->decoder_flushing && !av_fifo_can_read(ctx->frame_queue)))
539  ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &cupkt));
540  else
541  ret = 0;
542 
543  if (ret < 0)
544  goto error;
545 
546  // cuvidParseVideoData doesn't return an error just because stuff failed...
547  if (ctx->internal_error) {
548  av_log(avctx, AV_LOG_ERROR, "cuvid decode callback error\n");
549  ret = ctx->internal_error;
550  goto error;
551  }
552 
553 error:
554  eret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
555 
556  if (eret < 0)
557  return eret;
558  else if (ret < 0)
559  return ret;
560  else if (is_flush)
561  return AVERROR_EOF;
562  else
563  return 0;
564 }
565 
567 {
568  CuvidContext *ctx = avctx->priv_data;
569  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
570  AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
571  CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
572  CuvidParsedFrame parsed_frame;
573  CUdeviceptr mapped_frame = 0;
574  int ret = 0, eret = 0;
575 
576  av_log(avctx, AV_LOG_TRACE, "cuvid_output_frame\n");
577 
578  if (ctx->decoder_flushing) {
579  ret = cuvid_decode_packet(avctx, NULL);
580  if (ret < 0 && ret != AVERROR_EOF)
581  return ret;
582  }
583 
584  if (!cuvid_is_buffer_full(avctx)) {
585  AVPacket *const pkt = ctx->pkt;
586  ret = ff_decode_get_packet(avctx, pkt);
587  if (ret < 0 && ret != AVERROR_EOF)
588  return ret;
589  ret = cuvid_decode_packet(avctx, pkt);
591  // cuvid_is_buffer_full() should avoid this.
592  if (ret == AVERROR(EAGAIN))
594  if (ret < 0 && ret != AVERROR_EOF)
595  return ret;
596  }
597 
598  ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
599  if (ret < 0)
600  return ret;
601 
602  if (av_fifo_read(ctx->frame_queue, &parsed_frame, 1) >= 0) {
603  const AVPixFmtDescriptor *pixdesc;
604  CUVIDPROCPARAMS params;
605  unsigned int pitch = 0;
606  int offset = 0;
607  int i;
608 
609  memset(&params, 0, sizeof(params));
610  params.progressive_frame = parsed_frame.dispinfo.progressive_frame;
611  params.second_field = parsed_frame.second_field;
612  params.top_field_first = parsed_frame.dispinfo.top_field_first;
613 
614  ret = CHECK_CU(ctx->cvdl->cuvidMapVideoFrame(ctx->cudecoder, parsed_frame.dispinfo.picture_index, &mapped_frame, &pitch, &params));
615  if (ret < 0)
616  goto error;
617 
618  if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
619  ret = av_hwframe_get_buffer(ctx->hwframe, frame, 0);
620  if (ret < 0) {
621  av_log(avctx, AV_LOG_ERROR, "av_hwframe_get_buffer failed\n");
622  goto error;
623  }
624 
625  ret = ff_decode_frame_props(avctx, frame);
626  if (ret < 0) {
627  av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props failed\n");
628  goto error;
629  }
630 
631  pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
632 
633  for (i = 0; i < pixdesc->nb_components; i++) {
634  int height = avctx->height >> (i ? pixdesc->log2_chroma_h : 0);
635  CUDA_MEMCPY2D cpy = {
636  .srcMemoryType = CU_MEMORYTYPE_DEVICE,
637  .dstMemoryType = CU_MEMORYTYPE_DEVICE,
638  .srcDevice = mapped_frame,
639  .dstDevice = (CUdeviceptr)frame->data[i],
640  .srcPitch = pitch,
641  .dstPitch = frame->linesize[i],
642  .srcY = offset,
643  .WidthInBytes = FFMIN(pitch, frame->linesize[i]),
644  .Height = height,
645  };
646 
647  ret = CHECK_CU(ctx->cudl->cuMemcpy2DAsync(&cpy, device_hwctx->stream));
648  if (ret < 0)
649  goto error;
650 
651  offset += height;
652  }
653  } else if (avctx->pix_fmt == AV_PIX_FMT_NV12 ||
654  avctx->pix_fmt == AV_PIX_FMT_P010 ||
655  avctx->pix_fmt == AV_PIX_FMT_P016 ||
656 #ifdef NVDEC_HAVE_422_SUPPORT
657  avctx->pix_fmt == AV_PIX_FMT_NV16 ||
658  avctx->pix_fmt == AV_PIX_FMT_P216 ||
659 #endif
660  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
661  avctx->pix_fmt == AV_PIX_FMT_YUV444P16) {
662  unsigned int offset = 0;
663  AVFrame *tmp_frame = av_frame_alloc();
664  if (!tmp_frame) {
665  av_log(avctx, AV_LOG_ERROR, "av_frame_alloc failed\n");
666  ret = AVERROR(ENOMEM);
667  goto error;
668  }
669 
670  pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
671 
672  tmp_frame->format = AV_PIX_FMT_CUDA;
673  tmp_frame->hw_frames_ctx = av_buffer_ref(ctx->hwframe);
674  if (!tmp_frame->hw_frames_ctx) {
675  ret = AVERROR(ENOMEM);
676  av_frame_free(&tmp_frame);
677  goto error;
678  }
679 
680  tmp_frame->width = avctx->width;
681  tmp_frame->height = avctx->height;
682 
683  /*
684  * Note that the following logic would not work for three plane
685  * YUV420 because the pitch value is different for the chroma
686  * planes.
687  */
688  for (i = 0; i < pixdesc->nb_components; i++) {
689  tmp_frame->data[i] = (uint8_t*)mapped_frame + offset;
690  tmp_frame->linesize[i] = pitch;
691  offset += pitch * (avctx->height >> (i ? pixdesc->log2_chroma_h : 0));
692  }
693 
694  ret = ff_get_buffer(avctx, frame, 0);
695  if (ret < 0) {
696  av_log(avctx, AV_LOG_ERROR, "ff_get_buffer failed\n");
697  av_frame_free(&tmp_frame);
698  goto error;
699  }
700 
701  ret = av_hwframe_transfer_data(frame, tmp_frame, 0);
702  if (ret) {
703  av_log(avctx, AV_LOG_ERROR, "av_hwframe_transfer_data failed\n");
704  av_frame_free(&tmp_frame);
705  goto error;
706  }
707  av_frame_free(&tmp_frame);
708  } else {
709  ret = AVERROR_BUG;
710  goto error;
711  }
712 
713  if (ctx->key_frame[parsed_frame.dispinfo.picture_index])
714  frame->flags |= AV_FRAME_FLAG_KEY;
715  else
716  frame->flags &= ~AV_FRAME_FLAG_KEY;
717  ctx->key_frame[parsed_frame.dispinfo.picture_index] = 0;
718 
719  frame->width = avctx->width;
720  frame->height = avctx->height;
721  if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
722  frame->pts = av_rescale_q(parsed_frame.dispinfo.timestamp, (AVRational){1, 10000000}, avctx->pkt_timebase);
723  else
724  frame->pts = parsed_frame.dispinfo.timestamp;
725 
726  if (parsed_frame.second_field) {
727  if (ctx->prev_pts == INT64_MIN) {
728  ctx->prev_pts = frame->pts;
729  frame->pts += (avctx->pkt_timebase.den * avctx->framerate.den) / (avctx->pkt_timebase.num * avctx->framerate.num);
730  } else {
731  int pts_diff = (frame->pts - ctx->prev_pts) / 2;
732  ctx->prev_pts = frame->pts;
733  frame->pts += pts_diff;
734  }
735  }
736 
737  /* CUVIDs opaque reordering breaks the internal pkt logic.
738  * So set pkt_pts and clear all the other pkt_ fields.
739  */
740  frame->duration = 0;
741 
742  if (!parsed_frame.is_deinterlacing && !parsed_frame.dispinfo.progressive_frame)
744 
745  if ((frame->flags & AV_FRAME_FLAG_INTERLACED) && parsed_frame.dispinfo.top_field_first)
747  } else if (ctx->decoder_flushing) {
748  ret = AVERROR_EOF;
749  } else {
750  ret = AVERROR(EAGAIN);
751  }
752 
753 error:
754  if (ret < 0)
756 
757  if (mapped_frame)
758  eret = CHECK_CU(ctx->cvdl->cuvidUnmapVideoFrame(ctx->cudecoder, mapped_frame));
759 
760  eret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
761 
762  if (eret < 0)
763  return eret;
764  else
765  return ret;
766 }
767 
769 {
770  CuvidContext *ctx = avctx->priv_data;
771  AVHWDeviceContext *device_ctx = ctx->hwdevice ? (AVHWDeviceContext *)ctx->hwdevice->data : NULL;
772  AVCUDADeviceContext *device_hwctx = device_ctx ? device_ctx->hwctx : NULL;
773  CUcontext dummy, cuda_ctx = device_hwctx ? device_hwctx->cuda_ctx : NULL;
774 
775  av_fifo_freep2(&ctx->frame_queue);
776 
777  if (cuda_ctx) {
778  ctx->cudl->cuCtxPushCurrent(cuda_ctx);
779 
780  if (ctx->cuparser)
781  ctx->cvdl->cuvidDestroyVideoParser(ctx->cuparser);
782 
783  if (ctx->cudecoder)
784  ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder);
785 
786  ctx->cudl->cuCtxPopCurrent(&dummy);
787  }
788 
789  ctx->cudl = NULL;
790 
791  av_buffer_unref(&ctx->hwframe);
792  av_buffer_unref(&ctx->hwdevice);
793 
794  av_freep(&ctx->key_frame);
795  av_freep(&ctx->cuparse_ext);
796 
797  cuvid_free_functions(&ctx->cvdl);
798 
799  return 0;
800 }
801 
803  const CUVIDPARSERPARAMS *cuparseinfo,
804  int probed_width,
805  int probed_height,
806  int bit_depth, int is_yuv422, int is_yuv444)
807 {
808  CuvidContext *ctx = avctx->priv_data;
809  CUVIDDECODECAPS *caps;
810  int res8 = 0, res10 = 0, res12 = 0;
811 
812  if (!ctx->cvdl->cuvidGetDecoderCaps) {
813  av_log(avctx, AV_LOG_WARNING, "Used Nvidia driver is too old to perform a capability check.\n");
814  av_log(avctx, AV_LOG_WARNING, "The minimum required version is "
815 #if defined(_WIN32) || defined(__CYGWIN__)
816  "378.66"
817 #else
818  "378.13"
819 #endif
820  ". Continuing blind.\n");
821  ctx->caps8.bIsSupported = ctx->caps10.bIsSupported = 1;
822  // 12 bit was not supported before the capability check was introduced, so disable it.
823  ctx->caps12.bIsSupported = 0;
824  return 0;
825  }
826 
827  ctx->caps8.eCodecType = ctx->caps10.eCodecType = ctx->caps12.eCodecType
828  = cuparseinfo->CodecType;
829 
830  ctx->caps8.eChromaFormat = ctx->caps10.eChromaFormat = ctx->caps12.eChromaFormat
831  = is_yuv444 ? cudaVideoChromaFormat_444 :
832 #ifdef NVDEC_HAVE_422_SUPPORT
833  (is_yuv422 ? cudaVideoChromaFormat_422 : cudaVideoChromaFormat_420);
834 #else
835  cudaVideoChromaFormat_420;
836 #endif
837 
838  ctx->caps8.nBitDepthMinus8 = 0;
839  ctx->caps10.nBitDepthMinus8 = 2;
840  ctx->caps12.nBitDepthMinus8 = 4;
841 
842  res8 = CHECK_CU(ctx->cvdl->cuvidGetDecoderCaps(&ctx->caps8));
843  res10 = CHECK_CU(ctx->cvdl->cuvidGetDecoderCaps(&ctx->caps10));
844  res12 = CHECK_CU(ctx->cvdl->cuvidGetDecoderCaps(&ctx->caps12));
845 
846  av_log(avctx, AV_LOG_VERBOSE, "CUVID capabilities for %s:\n", avctx->codec->name);
847  av_log(avctx, AV_LOG_VERBOSE, "8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
848  ctx->caps8.bIsSupported, ctx->caps8.nMinWidth, ctx->caps8.nMaxWidth, ctx->caps8.nMinHeight, ctx->caps8.nMaxHeight);
849  av_log(avctx, AV_LOG_VERBOSE, "10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
850  ctx->caps10.bIsSupported, ctx->caps10.nMinWidth, ctx->caps10.nMaxWidth, ctx->caps10.nMinHeight, ctx->caps10.nMaxHeight);
851  av_log(avctx, AV_LOG_VERBOSE, "12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
852  ctx->caps12.bIsSupported, ctx->caps12.nMinWidth, ctx->caps12.nMaxWidth, ctx->caps12.nMinHeight, ctx->caps12.nMaxHeight);
853 
854  switch (bit_depth) {
855  case 10:
856  caps = &ctx->caps10;
857  if (res10 < 0)
858  return res10;
859  break;
860  case 12:
861  caps = &ctx->caps12;
862  if (res12 < 0)
863  return res12;
864  break;
865  default:
866  caps = &ctx->caps8;
867  if (res8 < 0)
868  return res8;
869  }
870 
871  if (!ctx->caps8.bIsSupported) {
872  av_log(avctx, AV_LOG_ERROR, "Codec %s is not supported with this chroma format.\n", avctx->codec->name);
873  return AVERROR(EINVAL);
874  }
875 
876  if (!caps->bIsSupported) {
877  av_log(avctx, AV_LOG_ERROR, "Bit depth %d with this chroma format is not supported.\n", bit_depth);
878  return AVERROR(EINVAL);
879  }
880 
881  if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
882  av_log(avctx, AV_LOG_ERROR, "Video width %d not within range from %d to %d\n",
883  probed_width, caps->nMinWidth, caps->nMaxWidth);
884  return AVERROR(EINVAL);
885  }
886 
887  if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
888  av_log(avctx, AV_LOG_ERROR, "Video height %d not within range from %d to %d\n",
889  probed_height, caps->nMinHeight, caps->nMaxHeight);
890  return AVERROR(EINVAL);
891  }
892 
893  if ((probed_width * probed_height) / 256 > caps->nMaxMBCount) {
894  av_log(avctx, AV_LOG_ERROR, "Video macroblock count %d exceeds maximum of %d\n",
895  (int)(probed_width * probed_height) / 256, caps->nMaxMBCount);
896  return AVERROR(EINVAL);
897  }
898 
899  return 0;
900 }
901 
903 {
904  CuvidContext *ctx = avctx->priv_data;
905  AVCUDADeviceContext *device_hwctx;
906  AVHWDeviceContext *device_ctx;
907  AVHWFramesContext *hwframe_ctx;
908  CUVIDSOURCEDATAPACKET seq_pkt;
909  CUcontext cuda_ctx = NULL;
910  CUcontext dummy;
911  uint8_t *extradata;
912  int extradata_size;
913  int ret = 0;
914 
917  AV_PIX_FMT_NONE };
918 
919  int probed_width = avctx->coded_width ? avctx->coded_width : 1280;
920  int probed_height = avctx->coded_height ? avctx->coded_height : 720;
921  int probed_bit_depth = 8, is_yuv444 = 0, is_yuv422 = 0;
922 
923  const AVPixFmtDescriptor *probe_desc = av_pix_fmt_desc_get(avctx->pix_fmt);
924  if (probe_desc && probe_desc->nb_components)
925  probed_bit_depth = probe_desc->comp[0].depth;
926 
927  if (probe_desc && probe_desc->nb_components > 1 && !probe_desc->log2_chroma_w && !probe_desc->log2_chroma_h)
928  is_yuv444 = 1;
929 
930 #ifdef NVDEC_HAVE_422_SUPPORT
931  if (probe_desc && probe_desc->log2_chroma_w && !probe_desc->log2_chroma_h)
932  is_yuv422 = 1;
933 #endif
934 
935  // Pick pixel format based on bit depth and chroma sampling.
936  switch (probed_bit_depth) {
937  case 10:
938 #if FF_API_NVDEC_OLD_PIX_FMTS
939  pix_fmts[1] = is_yuv444 ? AV_PIX_FMT_YUV444P16 : (is_yuv422 ? AV_PIX_FMT_P216 : AV_PIX_FMT_P010);
940 #else
941  pix_fmts[1] = is_yuv444 ? AV_PIX_FMT_YUV444P10MSB : (is_yuv422 ? AV_PIX_FMT_P210 : AV_PIX_FMT_P010);
942 #endif
943  break;
944  case 12:
945 #if FF_API_NVDEC_OLD_PIX_FMTS
946  pix_fmts[1] = is_yuv444 ? AV_PIX_FMT_YUV444P16 : (is_yuv422 ? AV_PIX_FMT_P216 : AV_PIX_FMT_P016);
947 #else
948  pix_fmts[1] = is_yuv444 ? AV_PIX_FMT_YUV444P12MSB : (is_yuv422 ? AV_PIX_FMT_P212 : AV_PIX_FMT_P012);
949 #endif
950  break;
951  default:
952  pix_fmts[1] = is_yuv444 ? AV_PIX_FMT_YUV444P : (is_yuv422 ? AV_PIX_FMT_NV16 : AV_PIX_FMT_NV12);
953  break;
954  }
955 
956  ctx->pkt = avctx->internal->in_pkt;
957  // Accelerated transcoding scenarios with 'ffmpeg' require that the
958  // pix_fmt be set to AV_PIX_FMT_CUDA early. The sw_pix_fmt, and the
959  // pix_fmt for non-accelerated transcoding, do not need to be correct
960  // but need to be set to something.
961  ret = ff_get_format(avctx, pix_fmts);
962  if (ret < 0) {
963  av_log(avctx, AV_LOG_ERROR, "ff_get_format failed: %d\n", ret);
964  return ret;
965  }
966  avctx->pix_fmt = ret;
967 
968  if (ctx->resize_expr && sscanf(ctx->resize_expr, "%dx%d",
969  &ctx->resize.width, &ctx->resize.height) != 2) {
970  av_log(avctx, AV_LOG_ERROR, "Invalid resize expressions\n");
971  ret = AVERROR(EINVAL);
972  goto error;
973  }
974 
975  if (ctx->crop_expr && sscanf(ctx->crop_expr, "%dx%dx%dx%d",
976  &ctx->crop.top, &ctx->crop.bottom,
977  &ctx->crop.left, &ctx->crop.right) != 4) {
978  av_log(avctx, AV_LOG_ERROR, "Invalid cropping expressions\n");
979  ret = AVERROR(EINVAL);
980  goto error;
981  }
982 
983  ret = cuvid_load_functions(&ctx->cvdl, avctx);
984  if (ret < 0) {
985  av_log(avctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n");
986  goto error;
987  }
988 
989  // respect the deprecated "surfaces" option if non-default value is given by user;
990  if(ctx->nb_surfaces < 0)
991  ctx->nb_surfaces = CUVID_DEFAULT_NUM_SURFACES;
992 
993  ctx->frame_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(CuvidParsedFrame), 0);
994  if (!ctx->frame_queue) {
995  ret = AVERROR(ENOMEM);
996  goto error;
997  }
998 
999  if (avctx->hw_frames_ctx) {
1000  ctx->hwframe = av_buffer_ref(avctx->hw_frames_ctx);
1001  if (!ctx->hwframe) {
1002  ret = AVERROR(ENOMEM);
1003  goto error;
1004  }
1005 
1006  hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
1007 
1008  ctx->hwdevice = av_buffer_ref(hwframe_ctx->device_ref);
1009  if (!ctx->hwdevice) {
1010  ret = AVERROR(ENOMEM);
1011  goto error;
1012  }
1013  } else {
1014  if (avctx->hw_device_ctx) {
1015  ctx->hwdevice = av_buffer_ref(avctx->hw_device_ctx);
1016  if (!ctx->hwdevice) {
1017  ret = AVERROR(ENOMEM);
1018  goto error;
1019  }
1020  } else {
1021  ret = av_hwdevice_ctx_create(&ctx->hwdevice, AV_HWDEVICE_TYPE_CUDA, ctx->cu_gpu, NULL, 0);
1022  if (ret < 0)
1023  goto error;
1024  }
1025 
1026  ctx->hwframe = av_hwframe_ctx_alloc(ctx->hwdevice);
1027  if (!ctx->hwframe) {
1028  av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n");
1029  ret = AVERROR(ENOMEM);
1030  goto error;
1031  }
1032 
1033  hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
1034  }
1035 
1036  device_ctx = hwframe_ctx->device_ctx;
1037  device_hwctx = device_ctx->hwctx;
1038 
1039  cuda_ctx = device_hwctx->cuda_ctx;
1040  ctx->cudl = device_hwctx->internal->cuda_dl;
1041 
1042  memset(&ctx->cuparseinfo, 0, sizeof(ctx->cuparseinfo));
1043  memset(&seq_pkt, 0, sizeof(seq_pkt));
1044 
1045  switch (avctx->codec->id) {
1046 #if CONFIG_H264_CUVID_DECODER
1047  case AV_CODEC_ID_H264:
1048  ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
1049  break;
1050 #endif
1051 #if CONFIG_HEVC_CUVID_DECODER
1052  case AV_CODEC_ID_HEVC:
1053  ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
1054  break;
1055 #endif
1056 #if CONFIG_MJPEG_CUVID_DECODER
1057  case AV_CODEC_ID_MJPEG:
1058  ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
1059  break;
1060 #endif
1061 #if CONFIG_MPEG1_CUVID_DECODER
1063  ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
1064  break;
1065 #endif
1066 #if CONFIG_MPEG2_CUVID_DECODER
1068  ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
1069  break;
1070 #endif
1071 #if CONFIG_MPEG4_CUVID_DECODER
1072  case AV_CODEC_ID_MPEG4:
1073  ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
1074  break;
1075 #endif
1076 #if CONFIG_VP8_CUVID_DECODER
1077  case AV_CODEC_ID_VP8:
1078  ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
1079  break;
1080 #endif
1081 #if CONFIG_VP9_CUVID_DECODER
1082  case AV_CODEC_ID_VP9:
1083  ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
1084  break;
1085 #endif
1086 #if CONFIG_VC1_CUVID_DECODER
1087  case AV_CODEC_ID_VC1:
1088  ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
1089  break;
1090 #endif
1091 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1092  case AV_CODEC_ID_AV1:
1093  ctx->cuparseinfo.CodecType = cudaVideoCodec_AV1;
1094  break;
1095 #endif
1096  default:
1097  av_log(avctx, AV_LOG_ERROR, "Invalid CUVID codec!\n");
1098  return AVERROR_BUG;
1099  }
1100 
1101  if (ffcodec(avctx->codec)->bsfs) {
1102  const AVCodecParameters *par = avctx->internal->bsf->par_out;
1103  extradata = par->extradata;
1104  extradata_size = par->extradata_size;
1105  } else {
1106  extradata = avctx->extradata;
1107  extradata_size = avctx->extradata_size;
1108  }
1109 
1110  // Check first bit to determine whether it's AV1CodecConfigurationRecord.
1111  // Skip first 4 bytes of AV1CodecConfigurationRecord to keep configOBUs
1112  // only, otherwise cuvidParseVideoData report unknown error.
1113  if (avctx->codec->id == AV_CODEC_ID_AV1 &&
1114  extradata_size >= 4 &&
1115  extradata[0] & 0x80) {
1116  extradata += 4;
1117  extradata_size -= 4;
1118  }
1119 
1120  ctx->cuparse_ext = av_mallocz(sizeof(*ctx->cuparse_ext)
1121  + FFMAX(extradata_size - (int)sizeof(ctx->cuparse_ext->raw_seqhdr_data), 0));
1122  if (!ctx->cuparse_ext) {
1123  ret = AVERROR(ENOMEM);
1124  goto error;
1125  }
1126 
1127  if (extradata_size > 0)
1128  memcpy(ctx->cuparse_ext->raw_seqhdr_data, extradata, extradata_size);
1129  ctx->cuparse_ext->format.seqhdr_data_length = extradata_size;
1130 
1131  ctx->cuparseinfo.pExtVideoInfo = ctx->cuparse_ext;
1132 
1133  ctx->key_frame = av_mallocz(ctx->nb_surfaces * sizeof(int));
1134  if (!ctx->key_frame) {
1135  ret = AVERROR(ENOMEM);
1136  goto error;
1137  }
1138 
1139  ctx->cuparseinfo.ulMaxNumDecodeSurfaces = 1;
1140  ctx->cuparseinfo.ulMaxDisplayDelay = (avctx->flags & AV_CODEC_FLAG_LOW_DELAY) ? 0 : CUVID_MAX_DISPLAY_DELAY;
1141  ctx->cuparseinfo.pUserData = avctx;
1142  ctx->cuparseinfo.pfnSequenceCallback = cuvid_handle_video_sequence;
1143  ctx->cuparseinfo.pfnDecodePicture = cuvid_handle_picture_decode;
1144  ctx->cuparseinfo.pfnDisplayPicture = cuvid_handle_picture_display;
1145 
1146  ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
1147  if (ret < 0)
1148  goto error;
1149 
1150  ret = cuvid_test_capabilities(avctx, &ctx->cuparseinfo,
1151  probed_width,
1152  probed_height,
1153  probed_bit_depth, is_yuv422, is_yuv444);
1154  if (ret < 0)
1155  goto error;
1156 
1157  ret = CHECK_CU(ctx->cvdl->cuvidCreateVideoParser(&ctx->cuparser, &ctx->cuparseinfo));
1158  if (ret < 0)
1159  goto error;
1160 
1161  seq_pkt.payload = ctx->cuparse_ext->raw_seqhdr_data;
1162  seq_pkt.payload_size = ctx->cuparse_ext->format.seqhdr_data_length;
1163 
1164  if (seq_pkt.payload && seq_pkt.payload_size) {
1165  ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &seq_pkt));
1166  if (ret < 0)
1167  goto error;
1168  }
1169 
1170  ret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
1171  if (ret < 0)
1172  goto error;
1173 
1174  ctx->prev_pts = INT64_MIN;
1175 
1176  if (!avctx->pkt_timebase.num || !avctx->pkt_timebase.den)
1177  av_log(avctx, AV_LOG_WARNING, "Invalid pkt_timebase, passing timestamps as-is.\n");
1178 
1179  return 0;
1180 
1181 error:
1182  cuvid_decode_end(avctx);
1183  return ret;
1184 }
1185 
1186 static void cuvid_flush(AVCodecContext *avctx)
1187 {
1188  CuvidContext *ctx = avctx->priv_data;
1189  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
1190  AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
1191  CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
1192  CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
1193  int ret;
1194 
1195  ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
1196  if (ret < 0)
1197  goto error;
1198 
1199  av_fifo_reset2(ctx->frame_queue);
1200 
1201  if (ctx->cudecoder) {
1202  ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder);
1203  ctx->cudecoder = NULL;
1204  }
1205 
1206  if (ctx->cuparser) {
1207  ctx->cvdl->cuvidDestroyVideoParser(ctx->cuparser);
1208  ctx->cuparser = NULL;
1209  }
1210 
1211  ret = CHECK_CU(ctx->cvdl->cuvidCreateVideoParser(&ctx->cuparser, &ctx->cuparseinfo));
1212  if (ret < 0)
1213  goto error;
1214 
1215  seq_pkt.payload = ctx->cuparse_ext->raw_seqhdr_data;
1216  seq_pkt.payload_size = ctx->cuparse_ext->format.seqhdr_data_length;
1217 
1218  if (seq_pkt.payload && seq_pkt.payload_size) {
1219  ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &seq_pkt));
1220  if (ret < 0)
1221  goto error;
1222  }
1223 
1224  ret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
1225  if (ret < 0)
1226  goto error;
1227 
1228  ctx->prev_pts = INT64_MIN;
1229  ctx->decoder_flushing = 0;
1230 
1231  return;
1232  error:
1233  av_log(avctx, AV_LOG_ERROR, "CUDA reinit on flush failed\n");
1234 }
1235 
1236 #define OFFSET(x) offsetof(CuvidContext, x)
1237 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1238 static const AVOption options[] = {
1239  { "deint", "Set deinterlacing mode", OFFSET(deint_mode), AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive, VD, .unit = "deint" },
1240  { "weave", "Weave deinterlacing (do nothing)", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0, VD, .unit = "deint" },
1241  { "bob", "Bob deinterlacing", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0, VD, .unit = "deint" },
1242  { "adaptive", "Adaptive deinterlacing", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0, VD, .unit = "deint" },
1243  { "gpu", "GPU to be used for decoding", OFFSET(cu_gpu), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
1244  { "surfaces", "Maximum surfaces to be used for decoding", OFFSET(nb_surfaces), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VD | AV_OPT_FLAG_DEPRECATED },
1245  { "drop_second_field", "Drop second field when deinterlacing", OFFSET(drop_second_field), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1246  { "crop", "Crop (top)x(bottom)x(left)x(right)", OFFSET(crop_expr), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
1247  { "resize", "Resize (width)x(height)", OFFSET(resize_expr), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
1248  { NULL }
1249 };
1250 
1252  &(const AVCodecHWConfigInternal) {
1253  .public = {
1258  .device_type = AV_HWDEVICE_TYPE_CUDA
1259  },
1260  .hwaccel = NULL,
1261  },
1262  NULL
1263 };
1264 
1265 #define DEFINE_CUVID_CODEC(x, X, bsf_name) \
1266  static const AVClass x##_cuvid_class = { \
1267  .class_name = #x "_cuvid", \
1268  .item_name = av_default_item_name, \
1269  .option = options, \
1270  .version = LIBAVUTIL_VERSION_INT, \
1271  }; \
1272  const FFCodec ff_##x##_cuvid_decoder = { \
1273  .p.name = #x "_cuvid", \
1274  CODEC_LONG_NAME("Nvidia CUVID " #X " decoder"), \
1275  .p.type = AVMEDIA_TYPE_VIDEO, \
1276  .p.id = AV_CODEC_ID_##X, \
1277  .priv_data_size = sizeof(CuvidContext), \
1278  .p.priv_class = &x##_cuvid_class, \
1279  .init = cuvid_decode_init, \
1280  .close = cuvid_decode_end, \
1281  FF_CODEC_RECEIVE_FRAME_CB(cuvid_output_frame), \
1282  .flush = cuvid_flush, \
1283  .bsfs = bsf_name, \
1284  .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
1285  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | \
1286  FF_CODEC_CAP_SETS_FRAME_PROPS, \
1287  .hw_configs = cuvid_hw_configs, \
1288  .p.wrapper_name = "cuvid", \
1289  };
1290 
1291 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1292 DEFINE_CUVID_CODEC(av1, AV1, NULL)
1293 #endif
1294 
1295 #if CONFIG_HEVC_CUVID_DECODER
1296 DEFINE_CUVID_CODEC(hevc, HEVC, "hevc_mp4toannexb")
1297 #endif
1298 
1299 #if CONFIG_H264_CUVID_DECODER
1300 DEFINE_CUVID_CODEC(h264, H264, "h264_mp4toannexb")
1301 #endif
1302 
1303 #if CONFIG_MJPEG_CUVID_DECODER
1304 DEFINE_CUVID_CODEC(mjpeg, MJPEG, NULL)
1305 #endif
1306 
1307 #if CONFIG_MPEG1_CUVID_DECODER
1308 DEFINE_CUVID_CODEC(mpeg1, MPEG1VIDEO, NULL)
1309 #endif
1310 
1311 #if CONFIG_MPEG2_CUVID_DECODER
1312 DEFINE_CUVID_CODEC(mpeg2, MPEG2VIDEO, NULL)
1313 #endif
1314 
1315 #if CONFIG_MPEG4_CUVID_DECODER
1316 DEFINE_CUVID_CODEC(mpeg4, MPEG4, NULL)
1317 #endif
1318 
1319 #if CONFIG_VP8_CUVID_DECODER
1320 DEFINE_CUVID_CODEC(vp8, VP8, NULL)
1321 #endif
1322 
1323 #if CONFIG_VP9_CUVID_DECODER
1324 DEFINE_CUVID_CODEC(vp9, VP9, NULL)
1325 #endif
1326 
1327 #if CONFIG_VC1_CUVID_DECODER
1328 DEFINE_CUVID_CODEC(vc1, VC1, NULL)
1329 #endif
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
hwconfig.h
AVHWDeviceContext::hwctx
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:88
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:433
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
ff_decode_get_packet
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition: decode.c:253
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:260
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:71
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
av_fifo_can_write
size_t av_fifo_can_write(const AVFifo *f)
Definition: fifo.c:94
AV_CODEC_HW_CONFIG_METHOD_INTERNAL
@ AV_CODEC_HW_CONFIG_METHOD_INTERNAL
The codec supports this format by some internal method.
Definition: codec.h:318
cuvid_handle_picture_display
static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO *dispinfo)
Definition: cuviddec.c:450
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
opt.h
CuvidContext::bottom
int bottom
Definition: cuviddec.c:75
hwcontext_cuda_internal.h
CuvidContext::decoder_flushing
int decoder_flushing
Definition: cuviddec.c:94
ff_get_format
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1220
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:49
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3456
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
av_div_q
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
int64_t
long long int64_t
Definition: coverity.c:34
AV_PIX_FMT_YUV444P10MSB
#define AV_PIX_FMT_YUV444P10MSB
Definition: pixfmt.h:554
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
CuvidParsedFrame::is_deinterlacing
int is_deinterlacing
Definition: cuviddec.c:114
av_hwframe_ctx_init
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:337
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:459
pixdesc.h
AVFrame::width
int width
Definition: frame.h:531
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:777
cuvid_output_frame
static int cuvid_output_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: cuviddec.c:566
av_hwframe_ctx_alloc
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:263
internal.h
AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
@ AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
The codec supports this format via the hw_frames_ctx interface.
Definition: codec.h:311
AVPacket::data
uint8_t * data
Definition: packet.h:595
AVComponentDescriptor::depth
int depth
Number of bits in the component.
Definition: pixdesc.h:57
CuvidContext::avclass
AVClass * avclass
Definition: cuviddec.c:56
AVOption
AVOption.
Definition: opt.h:429
CuvidContext::chroma_format
cudaVideoChromaFormat chroma_format
Definition: cuviddec.c:99
CuvidContext::progressive_sequence
int progressive_sequence
Definition: cuviddec.c:91
CuvidContext::cudl
CudaFunctions * cudl
Definition: cuviddec.c:106
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
mathematics.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
bit_depth
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition: af_astats.c:246
dummy
static int dummy
Definition: ffplay.c:3751
CuvidContext::caps12
CUVIDDECODECAPS caps12
Definition: cuviddec.c:101
AV_PIX_FMT_P212
#define AV_PIX_FMT_P212
Definition: pixfmt.h:618
AV_PIX_FMT_YUV444P12MSB
#define AV_PIX_FMT_YUV444P12MSB
Definition: pixfmt.h:555
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:480
cuvid_test_capabilities
static int cuvid_test_capabilities(AVCodecContext *avctx, const CUVIDPARSERPARAMS *cuparseinfo, int probed_width, int probed_height, int bit_depth, int is_yuv422, int is_yuv444)
Definition: cuviddec.c:802
AV_FRAME_FLAG_TOP_FIELD_FIRST
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition: frame.h:687
AV_HWDEVICE_TYPE_CUDA
@ AV_HWDEVICE_TYPE_CUDA
Definition: hwcontext.h:30
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:563
fifo.h
bsf.h
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:452
av_fifo_write
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition: fifo.c:188
CuvidParsedFrame
Definition: cuviddec.c:110
av_fifo_grow2
int av_fifo_grow2(AVFifo *f, size_t inc)
Enlarge an AVFifo.
Definition: fifo.c:99
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:500
CuvidContext::height
int height
Definition: cuviddec.c:80
AV_CODEC_FLAG_LOW_DELAY
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:314
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:619
CuvidContext::nb_surfaces
int nb_surfaces
Definition: cuviddec.c:66
AVBSFContext::par_out
AVCodecParameters * par_out
Parameters of the output stream.
Definition: bsf.h:96
AVCUDADeviceContext::cuda_ctx
CUcontext cuda_ctx
Definition: hwcontext_cuda.h:43
AVRational::num
int num
Numerator.
Definition: rational.h:59
CuvidContext::frame_queue
AVFifo * frame_queue
Definition: cuviddec.c:86
AV_CODEC_FLAG_INTERLACED_DCT
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:310
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:63
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
CuvidContext
Definition: cuviddec.c:54
CuvidContext::cuparse_ext
CUVIDEOFORMATEX * cuparse_ext
Definition: cuviddec.c:104
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:236
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:119
av_fifo_read
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition: fifo.c:240
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:674
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:527
CuvidContext::right
int right
Definition: cuviddec.c:74
cuvid_hw_configs
static const AVCodecHWConfigInternal *const cuvid_hw_configs[]
Definition: cuviddec.c:1251
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:552
cuvid_handle_picture_decode
static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS *picparams)
Definition: cuviddec.c:433
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:222
CuvidContext::caps10
CUVIDDECODECAPS caps10
Definition: cuviddec.c:101
CUVID_MAX_DISPLAY_DELAY
#define CUVID_MAX_DISPLAY_DELAY
Definition: cuviddec.c:120
DEFINE_CUVID_CODEC
#define DEFINE_CUVID_CODEC(x, X, bsf_name)
Definition: cuviddec.c:1265
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
decode.h
CuvidContext::cuparseinfo
CUVIDPARSERPARAMS cuparseinfo
Definition: cuviddec.c:103
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
AVCodecHWConfig::pix_fmt
enum AVPixelFormat pix_fmt
For decoders, a hardware pixel format which that decoder may be able to decode to if suitable hardwar...
Definition: codec.h:339
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
cuvid_decode_packet
static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Definition: cuviddec.c:496
CuvidContext::codec_type
cudaVideoCodec codec_type
Definition: cuviddec.c:98
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
format
New swscale design to change SwsGraph is what coordinates multiple passes These can include cascaded scaling error diffusion and so on Or we could have separate passes for the vertical and horizontal scaling In between each SwsPass lies a fully allocated image buffer Graph passes may have different levels of e g we can have a single threaded error diffusion pass following a multi threaded scaling pass SwsGraph is internally recreated whenever the image format
Definition: swscale-v2.txt:14
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:284
AVPixFmtDescriptor::nb_components
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
CuvidContext::crop_expr
char * crop_expr
Definition: cuviddec.c:68
VD
#define VD
Definition: cuviddec.c:1237
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVHWFramesContext::device_ref
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:129
CuvidContext::internal_error
int internal_error
Definition: cuviddec.c:93
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:478
av_fifo_can_read
size_t av_fifo_can_read(const AVFifo *f)
Definition: fifo.c:87
ff_set_sar
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:106
options
Definition: swscale.c:45
CuvidContext::width
int width
Definition: cuviddec.c:79
CuvidContext::cvdl
CuvidFunctions * cvdl
Definition: cuviddec.c:107
cuvid_is_buffer_full
static int cuvid_is_buffer_full(AVCodecContext *avctx)
Definition: cuviddec.c:484
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:53
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:75
av_fifo_reset2
void av_fifo_reset2(AVFifo *f)
Definition: fifo.c:280
cuvid_decode_init
static av_cold int cuvid_decode_init(AVCodecContext *avctx)
Definition: cuviddec.c:902
CuvidContext::crop
struct CuvidContext::@107 crop
AVCUDADeviceContext::internal
AVCUDADeviceContextInternal * internal
Definition: hwcontext_cuda.h:45
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1768
AVPacket::size
int size
Definition: packet.h:596
AVFifo
Definition: fifo.c:35
height
#define height
Definition: dsp.h:89
codec_internal.h
AV_PIX_FMT_P012
#define AV_PIX_FMT_P012
Definition: pixfmt.h:603
shift
static int shift(int a, int b)
Definition: bonk.c:261
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
AVCodecInternal::bsf
struct AVBSFContext * bsf
Definition: internal.h:84
AVCodecContext::pkt_timebase
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
Definition: avcodec.h:554
CuvidContext::drop_second_field
int drop_second_field
Definition: cuviddec.c:67
cuvid_decode_end
static av_cold int cuvid_decode_end(AVCodecContext *avctx)
Definition: cuviddec.c:768
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:247
CuvidContext::prev_pts
int64_t prev_pts
Definition: cuviddec.c:90
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:290
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:546
AVCodecHWConfigInternal
Definition: hwconfig.h:25
AV_PIX_FMT_NV16
@ AV_PIX_FMT_NV16
interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:198
buffer.h
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition: mem.c:225
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
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
cudaVideoSurfaceFormat_YUV444_16Bit
#define cudaVideoSurfaceFormat_YUV444_16Bit
Definition: cuviddec.c:47
nvdec.h
AV_PIX_FMT_P216
#define AV_PIX_FMT_P216
Definition: pixfmt.h:620
CHECK_CU
#define CHECK_CU(x)
Definition: cuviddec.c:117
AV_PIX_FMT_P210
#define AV_PIX_FMT_P210
Definition: pixfmt.h:616
AVCodec::id
enum AVCodecID id
Definition: codec.h:186
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
CuvidContext::cuparser
CUvideoparser cuparser
Definition: cuviddec.c:59
AV_OPT_FLAG_DEPRECATED
#define AV_OPT_FLAG_DEPRECATED
Set if option is deprecated, users should refer to AVOption.help text for more information.
Definition: opt.h:386
AVCUDADeviceContextInternal::cuda_dl
CudaFunctions * cuda_dl
Definition: hwcontext_cuda_internal.h:32
log.h
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:588
CuvidContext::top
int top
Definition: cuviddec.c:73
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:526
CuvidContext::resize
struct CuvidContext::@108 resize
CuvidParsedFrame::dispinfo
CUVIDPARSERDISPINFO dispinfo
Definition: cuviddec.c:112
AVCodecInternal::in_pkt
AVPacket * in_pkt
This packet is used to hold the packet given to decoders implementing the .decode API; it is unused b...
Definition: internal.h:83
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:228
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:496
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
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:1493
AV_CODEC_ID_VC1
@ AV_CODEC_ID_VC1
Definition: codec_id.h:122
CuvidContext::resize_expr
char * resize_expr
Definition: cuviddec.c:69
AVCodecContext::height
int height
Definition: avcodec.h:604
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:643
CuvidContext::left
int left
Definition: cuviddec.c:72
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:682
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:760
AV_PIX_FMT_P016
#define AV_PIX_FMT_P016
Definition: pixfmt.h:604
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:1471
avcodec.h
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:118
AVCUDADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_cuda.h:42
ret
ret
Definition: filter_design.txt:187
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:96
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:265
AVHWFramesContext::device_ctx
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:137
cuda_check.h
CuvidContext::deint_mode_current
int deint_mode_current
Definition: cuviddec.c:89
av_hwdevice_ctx_create
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
Definition: hwcontext.c:615
av_fifo_alloc2
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition: fifo.c:47
av_hwframe_transfer_data
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:448
cudaVideoSurfaceFormat_YUV444
#define cudaVideoSurfaceFormat_YUV444
Definition: cuviddec.c:46
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:756
ff_decode_frame_props
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data.
Definition: decode.c:1589
AVCodecContext
main external API structure.
Definition: avcodec.h:443
AVFrame::height
int height
Definition: frame.h:531
CuvidContext::cudecoder
CUvideodecoder cudecoder
Definition: cuviddec.c:58
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
CuvidContext::hwdevice
AVBufferRef * hwdevice
Definition: cuviddec.c:83
AVPixFmtDescriptor::comp
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:105
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
Definition: codec.h:298
CuvidParsedFrame::second_field
int second_field
Definition: cuviddec.c:113
OFFSET
#define OFFSET(x)
Definition: cuviddec.c:1236
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
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:78
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:602
FFCodec::bsfs
const char * bsfs
Decoding only, a comma-separated list of bitstream filters to apply to packets before decoding.
Definition: codec_internal.h:261
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:619
CuvidContext::hwframe
AVBufferRef * hwframe
Definition: cuviddec.c:84
cuvid_flush
static void cuvid_flush(AVCodecContext *avctx)
Definition: cuviddec.c:1186
mem.h
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
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:470
AVPacket
This structure stores compressed data.
Definition: packet.h:572
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:604
CuvidContext::caps8
CUVIDDECODECAPS caps8
Definition: cuviddec.c:101
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:504
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
av_fifo_freep2
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition: fifo.c:286
CuvidContext::pkt
AVPacket * pkt
Definition: cuviddec.c:63
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:650
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276
AVCodecHWConfigInternal::public
AVCodecHWConfig public
This is the structure which will be returned to the user by avcodec_get_hw_config().
Definition: hwconfig.h:30
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
CuvidContext::deint_mode
int deint_mode
Definition: cuviddec.c:88
av_hwframe_get_buffer
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:506
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
CuvidContext::key_frame
int * key_frame
Definition: cuviddec.c:96
CuvidContext::cu_gpu
char * cu_gpu
Definition: cuviddec.c:65
AVPixFmtDescriptor::log2_chroma_h
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
CUVID_DEFAULT_NUM_SURFACES
#define CUVID_DEFAULT_NUM_SURFACES
Definition: cuviddec.c:123
av_get_pix_fmt_name
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:3376
cuvid_handle_video_sequence
static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT *format)
Definition: cuviddec.c:125