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 
90  int64_t prev_pts;
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 fifo_size_inc;
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  // target width/height need to be multiples of two
168  cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1;
169  cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1;
170 
171  // aspect ratio conversion, 1:1, depends on scaled resolution
172  cuinfo.target_rect.left = 0;
173  cuinfo.target_rect.top = 0;
174  cuinfo.target_rect.right = cuinfo.ulTargetWidth;
175  cuinfo.target_rect.bottom = cuinfo.ulTargetHeight;
176 
177  chroma_444 = format->chroma_format == cudaVideoChromaFormat_444;
178 
179  switch (format->bit_depth_luma_minus8) {
180  case 0: // 8-bit
181  pix_fmts[1] = chroma_444 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_NV12;
182  caps = &ctx->caps8;
183  break;
184  case 2: // 10-bit
185  pix_fmts[1] = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P010;
186  caps = &ctx->caps10;
187  break;
188  case 4: // 12-bit
189  pix_fmts[1] = chroma_444 ? AV_PIX_FMT_YUV444P16 : AV_PIX_FMT_P016;
190  caps = &ctx->caps12;
191  break;
192  default:
193  break;
194  }
195 
196  if (!caps || !caps->bIsSupported) {
197  av_log(avctx, AV_LOG_ERROR, "unsupported bit depth: %d\n",
198  format->bit_depth_luma_minus8 + 8);
199  ctx->internal_error = AVERROR(EINVAL);
200  return 0;
201  }
202 
203  surface_fmt = ff_get_format(avctx, pix_fmts);
204  if (surface_fmt < 0) {
205  av_log(avctx, AV_LOG_ERROR, "ff_get_format failed: %d\n", surface_fmt);
206  ctx->internal_error = AVERROR(EINVAL);
207  return 0;
208  }
209 
210  av_log(avctx, AV_LOG_VERBOSE, "Formats: Original: %s | HW: %s | SW: %s\n",
211  av_get_pix_fmt_name(avctx->pix_fmt),
212  av_get_pix_fmt_name(surface_fmt),
213  av_get_pix_fmt_name(avctx->sw_pix_fmt));
214 
215  avctx->pix_fmt = surface_fmt;
216 
217  // Update our hwframe ctx, as the get_format callback might have refreshed it!
218  if (avctx->hw_frames_ctx) {
219  av_buffer_unref(&ctx->hwframe);
220 
221  ctx->hwframe = av_buffer_ref(avctx->hw_frames_ctx);
222  if (!ctx->hwframe) {
223  ctx->internal_error = AVERROR(ENOMEM);
224  return 0;
225  }
226 
227  hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
228  }
229 
230  ff_set_sar(avctx, av_div_q(
231  (AVRational){ format->display_aspect_ratio.x, format->display_aspect_ratio.y },
232  (AVRational){ avctx->width, avctx->height }));
233 
234  ctx->deint_mode_current = format->progressive_sequence
235  ? cudaVideoDeinterlaceMode_Weave
236  : ctx->deint_mode;
237 
238  ctx->progressive_sequence = format->progressive_sequence;
239 
240  if (!format->progressive_sequence && ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave)
242  else
243  avctx->flags &= ~AV_CODEC_FLAG_INTERLACED_DCT;
244 
245  if (format->video_signal_description.video_full_range_flag)
246  avctx->color_range = AVCOL_RANGE_JPEG;
247  else
248  avctx->color_range = AVCOL_RANGE_MPEG;
249 
250  avctx->color_primaries = format->video_signal_description.color_primaries;
251  avctx->color_trc = format->video_signal_description.transfer_characteristics;
252  avctx->colorspace = format->video_signal_description.matrix_coefficients;
253 
254  if (format->bitrate)
255  avctx->bit_rate = format->bitrate;
256 
257  if (format->frame_rate.numerator && format->frame_rate.denominator) {
258  avctx->framerate.num = format->frame_rate.numerator;
259  avctx->framerate.den = format->frame_rate.denominator;
260  }
261 
262  if (ctx->cudecoder
263  && avctx->coded_width == format->coded_width
264  && avctx->coded_height == format->coded_height
265  && avctx->width == old_width
266  && avctx->height == old_height
267  && ctx->chroma_format == format->chroma_format
268  && ctx->codec_type == format->codec)
269  return 1;
270 
271  if (ctx->cudecoder) {
272  av_log(avctx, AV_LOG_TRACE, "Re-initializing decoder\n");
273  ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder));
274  if (ctx->internal_error < 0)
275  return 0;
276  ctx->cudecoder = NULL;
277  }
278 
279  if (hwframe_ctx->pool && (
280  hwframe_ctx->width < avctx->width ||
281  hwframe_ctx->height < avctx->height ||
282  hwframe_ctx->format != AV_PIX_FMT_CUDA ||
283  hwframe_ctx->sw_format != avctx->sw_pix_fmt)) {
284  av_log(avctx, AV_LOG_ERROR, "AVHWFramesContext is already initialized with incompatible parameters\n");
285  av_log(avctx, AV_LOG_DEBUG, "width: %d <-> %d\n", hwframe_ctx->width, avctx->width);
286  av_log(avctx, AV_LOG_DEBUG, "height: %d <-> %d\n", hwframe_ctx->height, avctx->height);
287  av_log(avctx, AV_LOG_DEBUG, "format: %s <-> cuda\n", av_get_pix_fmt_name(hwframe_ctx->format));
288  av_log(avctx, AV_LOG_DEBUG, "sw_format: %s <-> %s\n",
289  av_get_pix_fmt_name(hwframe_ctx->sw_format), av_get_pix_fmt_name(avctx->sw_pix_fmt));
290  ctx->internal_error = AVERROR(EINVAL);
291  return 0;
292  }
293 
294  ctx->chroma_format = format->chroma_format;
295 
296  cuinfo.CodecType = ctx->codec_type = format->codec;
297  cuinfo.ChromaFormat = format->chroma_format;
298 
299  switch (avctx->sw_pix_fmt) {
300  case AV_PIX_FMT_NV12:
301  cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
302  break;
303  case AV_PIX_FMT_P010:
304  case AV_PIX_FMT_P016:
305  cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016;
306  break;
307  case AV_PIX_FMT_YUV444P:
308  cuinfo.OutputFormat = cudaVideoSurfaceFormat_YUV444;
309  break;
311  cuinfo.OutputFormat = cudaVideoSurfaceFormat_YUV444_16Bit;
312  break;
313  default:
314  av_log(avctx, AV_LOG_ERROR, "Unsupported output format: %s\n",
315  av_get_pix_fmt_name(avctx->sw_pix_fmt));
316  ctx->internal_error = AVERROR(EINVAL);
317  return 0;
318  }
319 
320  fifo_size_inc = ctx->nb_surfaces;
321  ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, format->min_num_decode_surfaces + 3);
322 
323  if (avctx->extra_hw_frames > 0)
324  ctx->nb_surfaces += avctx->extra_hw_frames;
325 
326  fifo_size_inc = ctx->nb_surfaces - fifo_size_inc;
327  if (fifo_size_inc > 0 && av_fifo_grow2(ctx->frame_queue, fifo_size_inc) < 0) {
328  av_log(avctx, AV_LOG_ERROR, "Failed to grow frame queue on video sequence callback\n");
329  ctx->internal_error = AVERROR(ENOMEM);
330  return 0;
331  }
332 
333  if (fifo_size_inc > 0 && av_reallocp_array(&ctx->key_frame, ctx->nb_surfaces, sizeof(int)) < 0) {
334  av_log(avctx, AV_LOG_ERROR, "Failed to grow key frame array on video sequence callback\n");
335  ctx->internal_error = AVERROR(ENOMEM);
336  return 0;
337  }
338 
339  cuinfo.ulNumDecodeSurfaces = ctx->nb_surfaces;
340  cuinfo.ulNumOutputSurfaces = 1;
341  cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
342  cuinfo.bitDepthMinus8 = format->bit_depth_luma_minus8;
343  cuinfo.DeinterlaceMode = ctx->deint_mode_current;
344 
345  if (ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field)
346  avctx->framerate = av_mul_q(avctx->framerate, (AVRational){2, 1});
347 
348  ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidCreateDecoder(&ctx->cudecoder, &cuinfo));
349  if (ctx->internal_error < 0)
350  return 0;
351 
352  if (!hwframe_ctx->pool) {
353  hwframe_ctx->format = AV_PIX_FMT_CUDA;
354  hwframe_ctx->sw_format = avctx->sw_pix_fmt;
355  hwframe_ctx->width = avctx->width;
356  hwframe_ctx->height = avctx->height;
357 
358  if ((ctx->internal_error = av_hwframe_ctx_init(ctx->hwframe)) < 0) {
359  av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_init failed\n");
360  return 0;
361  }
362  }
363 
364  if(ctx->cuparseinfo.ulMaxNumDecodeSurfaces != cuinfo.ulNumDecodeSurfaces) {
365  ctx->cuparseinfo.ulMaxNumDecodeSurfaces = cuinfo.ulNumDecodeSurfaces;
366  return cuinfo.ulNumDecodeSurfaces;
367  }
368 
369  return 1;
370 }
371 
372 static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS* picparams)
373 {
374  AVCodecContext *avctx = opaque;
375  CuvidContext *ctx = avctx->priv_data;
376 
377  av_log(avctx, AV_LOG_TRACE, "pfnDecodePicture\n");
378 
379  if(picparams->intra_pic_flag)
380  ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag;
381 
382  ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidDecodePicture(ctx->cudecoder, picparams));
383  if (ctx->internal_error < 0)
384  return 0;
385 
386  return 1;
387 }
388 
389 static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO* dispinfo)
390 {
391  AVCodecContext *avctx = opaque;
392  CuvidContext *ctx = avctx->priv_data;
393  CuvidParsedFrame parsed_frame = { { 0 } };
394 
395  parsed_frame.dispinfo = *dispinfo;
396  ctx->internal_error = 0;
397 
398  // For some reason, dispinfo->progressive_frame is sometimes wrong.
399  parsed_frame.dispinfo.progressive_frame = ctx->progressive_sequence;
400 
401  if (ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
402  av_fifo_write(ctx->frame_queue, &parsed_frame, 1);
403  } else {
404  parsed_frame.is_deinterlacing = 1;
405  av_fifo_write(ctx->frame_queue, &parsed_frame, 1);
406  if (!ctx->drop_second_field) {
407  parsed_frame.second_field = 1;
408  av_fifo_write(ctx->frame_queue, &parsed_frame, 1);
409  }
410  }
411 
412  return 1;
413 }
414 
416 {
417  CuvidContext *ctx = avctx->priv_data;
418 
419  int delay = ctx->cuparseinfo.ulMaxDisplayDelay;
420  if (ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field)
421  delay *= 2;
422 
423  return av_fifo_can_read(ctx->frame_queue) + delay >= ctx->nb_surfaces;
424 }
425 
426 static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
427 {
428  CuvidContext *ctx = avctx->priv_data;
429  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
430  AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
431  CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
432  CUVIDSOURCEDATAPACKET cupkt;
433  int ret = 0, eret = 0, is_flush = ctx->decoder_flushing;
434 
435  av_log(avctx, AV_LOG_TRACE, "cuvid_decode_packet\n");
436 
437  if (is_flush && avpkt && avpkt->size)
438  return AVERROR_EOF;
439 
440  if (cuvid_is_buffer_full(avctx) && avpkt && avpkt->size)
441  return AVERROR(EAGAIN);
442 
443  ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
444  if (ret < 0) {
445  return ret;
446  }
447 
448  memset(&cupkt, 0, sizeof(cupkt));
449 
450  if (avpkt && avpkt->size) {
451  cupkt.payload_size = avpkt->size;
452  cupkt.payload = avpkt->data;
453 
454  if (avpkt->pts != AV_NOPTS_VALUE) {
455  cupkt.flags = CUVID_PKT_TIMESTAMP;
456  if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
457  cupkt.timestamp = av_rescale_q(avpkt->pts, avctx->pkt_timebase, (AVRational){1, 10000000});
458  else
459  cupkt.timestamp = avpkt->pts;
460  }
461  } else {
462  cupkt.flags = CUVID_PKT_ENDOFSTREAM;
463  ctx->decoder_flushing = 1;
464  }
465 
466  ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &cupkt));
467 
468  if (ret < 0)
469  goto error;
470 
471  // cuvidParseVideoData doesn't return an error just because stuff failed...
472  if (ctx->internal_error) {
473  av_log(avctx, AV_LOG_ERROR, "cuvid decode callback error\n");
474  ret = ctx->internal_error;
475  goto error;
476  }
477 
478 error:
479  eret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
480 
481  if (eret < 0)
482  return eret;
483  else if (ret < 0)
484  return ret;
485  else if (is_flush)
486  return AVERROR_EOF;
487  else
488  return 0;
489 }
490 
492 {
493  CuvidContext *ctx = avctx->priv_data;
494  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
495  AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
496  CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
497  CuvidParsedFrame parsed_frame;
498  CUdeviceptr mapped_frame = 0;
499  int ret = 0, eret = 0;
500 
501  av_log(avctx, AV_LOG_TRACE, "cuvid_output_frame\n");
502 
503  if (ctx->decoder_flushing) {
504  ret = cuvid_decode_packet(avctx, NULL);
505  if (ret < 0 && ret != AVERROR_EOF)
506  return ret;
507  }
508 
509  if (!cuvid_is_buffer_full(avctx)) {
510  AVPacket *const pkt = ctx->pkt;
511  ret = ff_decode_get_packet(avctx, pkt);
512  if (ret < 0 && ret != AVERROR_EOF)
513  return ret;
514  ret = cuvid_decode_packet(avctx, pkt);
516  // cuvid_is_buffer_full() should avoid this.
517  if (ret == AVERROR(EAGAIN))
519  if (ret < 0 && ret != AVERROR_EOF)
520  return ret;
521  }
522 
523  ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
524  if (ret < 0)
525  return ret;
526 
527  if (av_fifo_read(ctx->frame_queue, &parsed_frame, 1) >= 0) {
528  const AVPixFmtDescriptor *pixdesc;
529  CUVIDPROCPARAMS params;
530  unsigned int pitch = 0;
531  int offset = 0;
532  int i;
533 
534  memset(&params, 0, sizeof(params));
535  params.progressive_frame = parsed_frame.dispinfo.progressive_frame;
536  params.second_field = parsed_frame.second_field;
537  params.top_field_first = parsed_frame.dispinfo.top_field_first;
538 
539  ret = CHECK_CU(ctx->cvdl->cuvidMapVideoFrame(ctx->cudecoder, parsed_frame.dispinfo.picture_index, &mapped_frame, &pitch, &params));
540  if (ret < 0)
541  goto error;
542 
543  if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
544  ret = av_hwframe_get_buffer(ctx->hwframe, frame, 0);
545  if (ret < 0) {
546  av_log(avctx, AV_LOG_ERROR, "av_hwframe_get_buffer failed\n");
547  goto error;
548  }
549 
550  ret = ff_decode_frame_props(avctx, frame);
551  if (ret < 0) {
552  av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props failed\n");
553  goto error;
554  }
555 
556  pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
557 
558  for (i = 0; i < pixdesc->nb_components; i++) {
559  int height = avctx->height >> (i ? pixdesc->log2_chroma_h : 0);
560  CUDA_MEMCPY2D cpy = {
561  .srcMemoryType = CU_MEMORYTYPE_DEVICE,
562  .dstMemoryType = CU_MEMORYTYPE_DEVICE,
563  .srcDevice = mapped_frame,
564  .dstDevice = (CUdeviceptr)frame->data[i],
565  .srcPitch = pitch,
566  .dstPitch = frame->linesize[i],
567  .srcY = offset,
568  .WidthInBytes = FFMIN(pitch, frame->linesize[i]),
569  .Height = height,
570  };
571 
572  ret = CHECK_CU(ctx->cudl->cuMemcpy2DAsync(&cpy, device_hwctx->stream));
573  if (ret < 0)
574  goto error;
575 
576  offset += height;
577  }
578  } else if (avctx->pix_fmt == AV_PIX_FMT_NV12 ||
579  avctx->pix_fmt == AV_PIX_FMT_P010 ||
580  avctx->pix_fmt == AV_PIX_FMT_P016 ||
581  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
582  avctx->pix_fmt == AV_PIX_FMT_YUV444P16) {
583  unsigned int offset = 0;
584  AVFrame *tmp_frame = av_frame_alloc();
585  if (!tmp_frame) {
586  av_log(avctx, AV_LOG_ERROR, "av_frame_alloc failed\n");
587  ret = AVERROR(ENOMEM);
588  goto error;
589  }
590 
591  pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
592 
593  tmp_frame->format = AV_PIX_FMT_CUDA;
594  tmp_frame->hw_frames_ctx = av_buffer_ref(ctx->hwframe);
595  if (!tmp_frame->hw_frames_ctx) {
596  ret = AVERROR(ENOMEM);
597  av_frame_free(&tmp_frame);
598  goto error;
599  }
600 
601  tmp_frame->width = avctx->width;
602  tmp_frame->height = avctx->height;
603 
604  /*
605  * Note that the following logic would not work for three plane
606  * YUV420 because the pitch value is different for the chroma
607  * planes.
608  */
609  for (i = 0; i < pixdesc->nb_components; i++) {
610  tmp_frame->data[i] = (uint8_t*)mapped_frame + offset;
611  tmp_frame->linesize[i] = pitch;
612  offset += pitch * (avctx->height >> (i ? pixdesc->log2_chroma_h : 0));
613  }
614 
615  ret = ff_get_buffer(avctx, frame, 0);
616  if (ret < 0) {
617  av_log(avctx, AV_LOG_ERROR, "ff_get_buffer failed\n");
618  av_frame_free(&tmp_frame);
619  goto error;
620  }
621 
622  ret = av_hwframe_transfer_data(frame, tmp_frame, 0);
623  if (ret) {
624  av_log(avctx, AV_LOG_ERROR, "av_hwframe_transfer_data failed\n");
625  av_frame_free(&tmp_frame);
626  goto error;
627  }
628  av_frame_free(&tmp_frame);
629  } else {
630  ret = AVERROR_BUG;
631  goto error;
632  }
633 
634  if (ctx->key_frame[parsed_frame.dispinfo.picture_index])
635  frame->flags |= AV_FRAME_FLAG_KEY;
636  else
637  frame->flags &= ~AV_FRAME_FLAG_KEY;
638  ctx->key_frame[parsed_frame.dispinfo.picture_index] = 0;
639 
640  frame->width = avctx->width;
641  frame->height = avctx->height;
642  if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
643  frame->pts = av_rescale_q(parsed_frame.dispinfo.timestamp, (AVRational){1, 10000000}, avctx->pkt_timebase);
644  else
645  frame->pts = parsed_frame.dispinfo.timestamp;
646 
647  if (parsed_frame.second_field) {
648  if (ctx->prev_pts == INT64_MIN) {
649  ctx->prev_pts = frame->pts;
650  frame->pts += (avctx->pkt_timebase.den * avctx->framerate.den) / (avctx->pkt_timebase.num * avctx->framerate.num);
651  } else {
652  int pts_diff = (frame->pts - ctx->prev_pts) / 2;
653  ctx->prev_pts = frame->pts;
654  frame->pts += pts_diff;
655  }
656  }
657 
658  /* CUVIDs opaque reordering breaks the internal pkt logic.
659  * So set pkt_pts and clear all the other pkt_ fields.
660  */
661  frame->duration = 0;
662 #if FF_API_FRAME_PKT
664  frame->pkt_pos = -1;
665  frame->pkt_size = -1;
667 #endif
668 
669  if (!parsed_frame.is_deinterlacing && !parsed_frame.dispinfo.progressive_frame)
671 
672  if ((frame->flags & AV_FRAME_FLAG_INTERLACED) && parsed_frame.dispinfo.top_field_first)
674  } else if (ctx->decoder_flushing) {
675  ret = AVERROR_EOF;
676  } else {
677  ret = AVERROR(EAGAIN);
678  }
679 
680 error:
681  if (ret < 0)
683 
684  if (mapped_frame)
685  eret = CHECK_CU(ctx->cvdl->cuvidUnmapVideoFrame(ctx->cudecoder, mapped_frame));
686 
687  eret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
688 
689  if (eret < 0)
690  return eret;
691  else
692  return ret;
693 }
694 
696 {
697  CuvidContext *ctx = avctx->priv_data;
698  AVHWDeviceContext *device_ctx = ctx->hwdevice ? (AVHWDeviceContext *)ctx->hwdevice->data : NULL;
699  AVCUDADeviceContext *device_hwctx = device_ctx ? device_ctx->hwctx : NULL;
700  CUcontext dummy, cuda_ctx = device_hwctx ? device_hwctx->cuda_ctx : NULL;
701 
702  av_fifo_freep2(&ctx->frame_queue);
703 
704  if (cuda_ctx) {
705  ctx->cudl->cuCtxPushCurrent(cuda_ctx);
706 
707  if (ctx->cuparser)
708  ctx->cvdl->cuvidDestroyVideoParser(ctx->cuparser);
709 
710  if (ctx->cudecoder)
711  ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder);
712 
713  ctx->cudl->cuCtxPopCurrent(&dummy);
714  }
715 
716  ctx->cudl = NULL;
717 
718  av_buffer_unref(&ctx->hwframe);
719  av_buffer_unref(&ctx->hwdevice);
720 
721  av_freep(&ctx->key_frame);
722  av_freep(&ctx->cuparse_ext);
723 
724  cuvid_free_functions(&ctx->cvdl);
725 
726  return 0;
727 }
728 
730  const CUVIDPARSERPARAMS *cuparseinfo,
731  int probed_width,
732  int probed_height,
733  int bit_depth)
734 {
735  CuvidContext *ctx = avctx->priv_data;
736  CUVIDDECODECAPS *caps;
737  int res8 = 0, res10 = 0, res12 = 0;
738 
739  if (!ctx->cvdl->cuvidGetDecoderCaps) {
740  av_log(avctx, AV_LOG_WARNING, "Used Nvidia driver is too old to perform a capability check.\n");
741  av_log(avctx, AV_LOG_WARNING, "The minimum required version is "
742 #if defined(_WIN32) || defined(__CYGWIN__)
743  "378.66"
744 #else
745  "378.13"
746 #endif
747  ". Continuing blind.\n");
748  ctx->caps8.bIsSupported = ctx->caps10.bIsSupported = 1;
749  // 12 bit was not supported before the capability check was introduced, so disable it.
750  ctx->caps12.bIsSupported = 0;
751  return 0;
752  }
753 
754  ctx->caps8.eCodecType = ctx->caps10.eCodecType = ctx->caps12.eCodecType
755  = cuparseinfo->CodecType;
756  ctx->caps8.eChromaFormat = ctx->caps10.eChromaFormat = ctx->caps12.eChromaFormat
757  = cudaVideoChromaFormat_420;
758 
759  ctx->caps8.nBitDepthMinus8 = 0;
760  ctx->caps10.nBitDepthMinus8 = 2;
761  ctx->caps12.nBitDepthMinus8 = 4;
762 
763  res8 = CHECK_CU(ctx->cvdl->cuvidGetDecoderCaps(&ctx->caps8));
764  res10 = CHECK_CU(ctx->cvdl->cuvidGetDecoderCaps(&ctx->caps10));
765  res12 = CHECK_CU(ctx->cvdl->cuvidGetDecoderCaps(&ctx->caps12));
766 
767  av_log(avctx, AV_LOG_VERBOSE, "CUVID capabilities for %s:\n", avctx->codec->name);
768  av_log(avctx, AV_LOG_VERBOSE, "8 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
769  ctx->caps8.bIsSupported, ctx->caps8.nMinWidth, ctx->caps8.nMaxWidth, ctx->caps8.nMinHeight, ctx->caps8.nMaxHeight);
770  av_log(avctx, AV_LOG_VERBOSE, "10 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
771  ctx->caps10.bIsSupported, ctx->caps10.nMinWidth, ctx->caps10.nMaxWidth, ctx->caps10.nMinHeight, ctx->caps10.nMaxHeight);
772  av_log(avctx, AV_LOG_VERBOSE, "12 bit: supported: %d, min_width: %d, max_width: %d, min_height: %d, max_height: %d\n",
773  ctx->caps12.bIsSupported, ctx->caps12.nMinWidth, ctx->caps12.nMaxWidth, ctx->caps12.nMinHeight, ctx->caps12.nMaxHeight);
774 
775  switch (bit_depth) {
776  case 10:
777  caps = &ctx->caps10;
778  if (res10 < 0)
779  return res10;
780  break;
781  case 12:
782  caps = &ctx->caps12;
783  if (res12 < 0)
784  return res12;
785  break;
786  default:
787  caps = &ctx->caps8;
788  if (res8 < 0)
789  return res8;
790  }
791 
792  if (!ctx->caps8.bIsSupported) {
793  av_log(avctx, AV_LOG_ERROR, "Codec %s is not supported.\n", avctx->codec->name);
794  return AVERROR(EINVAL);
795  }
796 
797  if (!caps->bIsSupported) {
798  av_log(avctx, AV_LOG_ERROR, "Bit depth %d is not supported.\n", bit_depth);
799  return AVERROR(EINVAL);
800  }
801 
802  if (probed_width > caps->nMaxWidth || probed_width < caps->nMinWidth) {
803  av_log(avctx, AV_LOG_ERROR, "Video width %d not within range from %d to %d\n",
804  probed_width, caps->nMinWidth, caps->nMaxWidth);
805  return AVERROR(EINVAL);
806  }
807 
808  if (probed_height > caps->nMaxHeight || probed_height < caps->nMinHeight) {
809  av_log(avctx, AV_LOG_ERROR, "Video height %d not within range from %d to %d\n",
810  probed_height, caps->nMinHeight, caps->nMaxHeight);
811  return AVERROR(EINVAL);
812  }
813 
814  if ((probed_width * probed_height) / 256 > caps->nMaxMBCount) {
815  av_log(avctx, AV_LOG_ERROR, "Video macroblock count %d exceeds maximum of %d\n",
816  (int)(probed_width * probed_height) / 256, caps->nMaxMBCount);
817  return AVERROR(EINVAL);
818  }
819 
820  return 0;
821 }
822 
824 {
825  CuvidContext *ctx = avctx->priv_data;
826  AVCUDADeviceContext *device_hwctx;
827  AVHWDeviceContext *device_ctx;
828  AVHWFramesContext *hwframe_ctx;
829  CUVIDSOURCEDATAPACKET seq_pkt;
830  CUcontext cuda_ctx = NULL;
831  CUcontext dummy;
832  uint8_t *extradata;
833  int extradata_size;
834  int ret = 0;
835 
838  AV_PIX_FMT_NONE };
839 
840  int probed_width = avctx->coded_width ? avctx->coded_width : 1280;
841  int probed_height = avctx->coded_height ? avctx->coded_height : 720;
842  int probed_bit_depth = 8;
843 
844  const AVPixFmtDescriptor *probe_desc = av_pix_fmt_desc_get(avctx->pix_fmt);
845  if (probe_desc && probe_desc->nb_components)
846  probed_bit_depth = probe_desc->comp[0].depth;
847 
848  ctx->pkt = avctx->internal->in_pkt;
849  // Accelerated transcoding scenarios with 'ffmpeg' require that the
850  // pix_fmt be set to AV_PIX_FMT_CUDA early. The sw_pix_fmt, and the
851  // pix_fmt for non-accelerated transcoding, do not need to be correct
852  // but need to be set to something. We arbitrarily pick NV12.
853  ret = ff_get_format(avctx, pix_fmts);
854  if (ret < 0) {
855  av_log(avctx, AV_LOG_ERROR, "ff_get_format failed: %d\n", ret);
856  return ret;
857  }
858  avctx->pix_fmt = ret;
859 
860  if (ctx->resize_expr && sscanf(ctx->resize_expr, "%dx%d",
861  &ctx->resize.width, &ctx->resize.height) != 2) {
862  av_log(avctx, AV_LOG_ERROR, "Invalid resize expressions\n");
863  ret = AVERROR(EINVAL);
864  goto error;
865  }
866 
867  if (ctx->crop_expr && sscanf(ctx->crop_expr, "%dx%dx%dx%d",
868  &ctx->crop.top, &ctx->crop.bottom,
869  &ctx->crop.left, &ctx->crop.right) != 4) {
870  av_log(avctx, AV_LOG_ERROR, "Invalid cropping expressions\n");
871  ret = AVERROR(EINVAL);
872  goto error;
873  }
874 
875  ret = cuvid_load_functions(&ctx->cvdl, avctx);
876  if (ret < 0) {
877  av_log(avctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n");
878  goto error;
879  }
880 
881  // respect the deprecated "surfaces" option if non-default value is given by user;
882  if(ctx->nb_surfaces < 0)
883  ctx->nb_surfaces = CUVID_DEFAULT_NUM_SURFACES;
884 
885  ctx->frame_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(CuvidParsedFrame), 0);
886  if (!ctx->frame_queue) {
887  ret = AVERROR(ENOMEM);
888  goto error;
889  }
890 
891  if (avctx->hw_frames_ctx) {
892  ctx->hwframe = av_buffer_ref(avctx->hw_frames_ctx);
893  if (!ctx->hwframe) {
894  ret = AVERROR(ENOMEM);
895  goto error;
896  }
897 
898  hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
899 
900  ctx->hwdevice = av_buffer_ref(hwframe_ctx->device_ref);
901  if (!ctx->hwdevice) {
902  ret = AVERROR(ENOMEM);
903  goto error;
904  }
905  } else {
906  if (avctx->hw_device_ctx) {
907  ctx->hwdevice = av_buffer_ref(avctx->hw_device_ctx);
908  if (!ctx->hwdevice) {
909  ret = AVERROR(ENOMEM);
910  goto error;
911  }
912  } else {
913  ret = av_hwdevice_ctx_create(&ctx->hwdevice, AV_HWDEVICE_TYPE_CUDA, ctx->cu_gpu, NULL, 0);
914  if (ret < 0)
915  goto error;
916  }
917 
918  ctx->hwframe = av_hwframe_ctx_alloc(ctx->hwdevice);
919  if (!ctx->hwframe) {
920  av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n");
921  ret = AVERROR(ENOMEM);
922  goto error;
923  }
924 
925  hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
926  }
927 
928  device_ctx = hwframe_ctx->device_ctx;
929  device_hwctx = device_ctx->hwctx;
930 
931  cuda_ctx = device_hwctx->cuda_ctx;
932  ctx->cudl = device_hwctx->internal->cuda_dl;
933 
934  memset(&ctx->cuparseinfo, 0, sizeof(ctx->cuparseinfo));
935  memset(&seq_pkt, 0, sizeof(seq_pkt));
936 
937  switch (avctx->codec->id) {
938 #if CONFIG_H264_CUVID_DECODER
939  case AV_CODEC_ID_H264:
940  ctx->cuparseinfo.CodecType = cudaVideoCodec_H264;
941  break;
942 #endif
943 #if CONFIG_HEVC_CUVID_DECODER
944  case AV_CODEC_ID_HEVC:
945  ctx->cuparseinfo.CodecType = cudaVideoCodec_HEVC;
946  break;
947 #endif
948 #if CONFIG_MJPEG_CUVID_DECODER
949  case AV_CODEC_ID_MJPEG:
950  ctx->cuparseinfo.CodecType = cudaVideoCodec_JPEG;
951  break;
952 #endif
953 #if CONFIG_MPEG1_CUVID_DECODER
955  ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG1;
956  break;
957 #endif
958 #if CONFIG_MPEG2_CUVID_DECODER
960  ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG2;
961  break;
962 #endif
963 #if CONFIG_MPEG4_CUVID_DECODER
964  case AV_CODEC_ID_MPEG4:
965  ctx->cuparseinfo.CodecType = cudaVideoCodec_MPEG4;
966  break;
967 #endif
968 #if CONFIG_VP8_CUVID_DECODER
969  case AV_CODEC_ID_VP8:
970  ctx->cuparseinfo.CodecType = cudaVideoCodec_VP8;
971  break;
972 #endif
973 #if CONFIG_VP9_CUVID_DECODER
974  case AV_CODEC_ID_VP9:
975  ctx->cuparseinfo.CodecType = cudaVideoCodec_VP9;
976  break;
977 #endif
978 #if CONFIG_VC1_CUVID_DECODER
979  case AV_CODEC_ID_VC1:
980  ctx->cuparseinfo.CodecType = cudaVideoCodec_VC1;
981  break;
982 #endif
983 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
984  case AV_CODEC_ID_AV1:
985  ctx->cuparseinfo.CodecType = cudaVideoCodec_AV1;
986  break;
987 #endif
988  default:
989  av_log(avctx, AV_LOG_ERROR, "Invalid CUVID codec!\n");
990  return AVERROR_BUG;
991  }
992 
993  if (ffcodec(avctx->codec)->bsfs) {
994  const AVCodecParameters *par = avctx->internal->bsf->par_out;
995  extradata = par->extradata;
996  extradata_size = par->extradata_size;
997  } else {
998  extradata = avctx->extradata;
999  extradata_size = avctx->extradata_size;
1000  }
1001 
1002  // Check first bit to determine whether it's AV1CodecConfigurationRecord.
1003  // Skip first 4 bytes of AV1CodecConfigurationRecord to keep configOBUs
1004  // only, otherwise cuvidParseVideoData report unknown error.
1005  if (avctx->codec->id == AV_CODEC_ID_AV1 &&
1006  extradata_size > 4 &&
1007  extradata[0] & 0x80) {
1008  extradata += 4;
1009  extradata_size -= 4;
1010  }
1011 
1012  ctx->cuparse_ext = av_mallocz(sizeof(*ctx->cuparse_ext)
1013  + FFMAX(extradata_size - (int)sizeof(ctx->cuparse_ext->raw_seqhdr_data), 0));
1014  if (!ctx->cuparse_ext) {
1015  ret = AVERROR(ENOMEM);
1016  goto error;
1017  }
1018 
1019  if (extradata_size > 0)
1020  memcpy(ctx->cuparse_ext->raw_seqhdr_data, extradata, extradata_size);
1021  ctx->cuparse_ext->format.seqhdr_data_length = extradata_size;
1022 
1023  ctx->cuparseinfo.pExtVideoInfo = ctx->cuparse_ext;
1024 
1025  ctx->key_frame = av_mallocz(ctx->nb_surfaces * sizeof(int));
1026  if (!ctx->key_frame) {
1027  ret = AVERROR(ENOMEM);
1028  goto error;
1029  }
1030 
1031  ctx->cuparseinfo.ulMaxNumDecodeSurfaces = 1;
1032  ctx->cuparseinfo.ulMaxDisplayDelay = (avctx->flags & AV_CODEC_FLAG_LOW_DELAY) ? 0 : CUVID_MAX_DISPLAY_DELAY;
1033  ctx->cuparseinfo.pUserData = avctx;
1034  ctx->cuparseinfo.pfnSequenceCallback = cuvid_handle_video_sequence;
1035  ctx->cuparseinfo.pfnDecodePicture = cuvid_handle_picture_decode;
1036  ctx->cuparseinfo.pfnDisplayPicture = cuvid_handle_picture_display;
1037 
1038  ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
1039  if (ret < 0)
1040  goto error;
1041 
1042  ret = cuvid_test_capabilities(avctx, &ctx->cuparseinfo,
1043  probed_width,
1044  probed_height,
1045  probed_bit_depth);
1046  if (ret < 0)
1047  goto error;
1048 
1049  ret = CHECK_CU(ctx->cvdl->cuvidCreateVideoParser(&ctx->cuparser, &ctx->cuparseinfo));
1050  if (ret < 0)
1051  goto error;
1052 
1053  seq_pkt.payload = ctx->cuparse_ext->raw_seqhdr_data;
1054  seq_pkt.payload_size = ctx->cuparse_ext->format.seqhdr_data_length;
1055 
1056  if (seq_pkt.payload && seq_pkt.payload_size) {
1057  ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &seq_pkt));
1058  if (ret < 0)
1059  goto error;
1060  }
1061 
1062  ret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
1063  if (ret < 0)
1064  goto error;
1065 
1066  ctx->prev_pts = INT64_MIN;
1067 
1068  if (!avctx->pkt_timebase.num || !avctx->pkt_timebase.den)
1069  av_log(avctx, AV_LOG_WARNING, "Invalid pkt_timebase, passing timestamps as-is.\n");
1070 
1071  return 0;
1072 
1073 error:
1074  cuvid_decode_end(avctx);
1075  return ret;
1076 }
1077 
1078 static void cuvid_flush(AVCodecContext *avctx)
1079 {
1080  CuvidContext *ctx = avctx->priv_data;
1081  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
1082  AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
1083  CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
1084  CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
1085  int ret;
1086 
1087  ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx));
1088  if (ret < 0)
1089  goto error;
1090 
1091  av_fifo_reset2(ctx->frame_queue);
1092 
1093  if (ctx->cudecoder) {
1094  ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder);
1095  ctx->cudecoder = NULL;
1096  }
1097 
1098  if (ctx->cuparser) {
1099  ctx->cvdl->cuvidDestroyVideoParser(ctx->cuparser);
1100  ctx->cuparser = NULL;
1101  }
1102 
1103  ret = CHECK_CU(ctx->cvdl->cuvidCreateVideoParser(&ctx->cuparser, &ctx->cuparseinfo));
1104  if (ret < 0)
1105  goto error;
1106 
1107  seq_pkt.payload = ctx->cuparse_ext->raw_seqhdr_data;
1108  seq_pkt.payload_size = ctx->cuparse_ext->format.seqhdr_data_length;
1109 
1110  if (seq_pkt.payload && seq_pkt.payload_size) {
1111  ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &seq_pkt));
1112  if (ret < 0)
1113  goto error;
1114  }
1115 
1116  ret = CHECK_CU(ctx->cudl->cuCtxPopCurrent(&dummy));
1117  if (ret < 0)
1118  goto error;
1119 
1120  ctx->prev_pts = INT64_MIN;
1121  ctx->decoder_flushing = 0;
1122 
1123  return;
1124  error:
1125  av_log(avctx, AV_LOG_ERROR, "CUDA reinit on flush failed\n");
1126 }
1127 
1128 #define OFFSET(x) offsetof(CuvidContext, x)
1129 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1130 static const AVOption options[] = {
1131  { "deint", "Set deinterlacing mode", OFFSET(deint_mode), AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive, VD, .unit = "deint" },
1132  { "weave", "Weave deinterlacing (do nothing)", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0, VD, .unit = "deint" },
1133  { "bob", "Bob deinterlacing", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0, VD, .unit = "deint" },
1134  { "adaptive", "Adaptive deinterlacing", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0, VD, .unit = "deint" },
1135  { "gpu", "GPU to be used for decoding", OFFSET(cu_gpu), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
1136  { "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 },
1137  { "drop_second_field", "Drop second field when deinterlacing", OFFSET(drop_second_field), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1138  { "crop", "Crop (top)x(bottom)x(left)x(right)", OFFSET(crop_expr), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
1139  { "resize", "Resize (width)x(height)", OFFSET(resize_expr), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
1140  { NULL }
1141 };
1142 
1144  &(const AVCodecHWConfigInternal) {
1145  .public = {
1149  .device_type = AV_HWDEVICE_TYPE_CUDA
1150  },
1151  .hwaccel = NULL,
1152  },
1153  NULL
1154 };
1155 
1156 #define DEFINE_CUVID_CODEC(x, X, bsf_name) \
1157  static const AVClass x##_cuvid_class = { \
1158  .class_name = #x "_cuvid", \
1159  .item_name = av_default_item_name, \
1160  .option = options, \
1161  .version = LIBAVUTIL_VERSION_INT, \
1162  }; \
1163  const FFCodec ff_##x##_cuvid_decoder = { \
1164  .p.name = #x "_cuvid", \
1165  CODEC_LONG_NAME("Nvidia CUVID " #X " decoder"), \
1166  .p.type = AVMEDIA_TYPE_VIDEO, \
1167  .p.id = AV_CODEC_ID_##X, \
1168  .priv_data_size = sizeof(CuvidContext), \
1169  .p.priv_class = &x##_cuvid_class, \
1170  .init = cuvid_decode_init, \
1171  .close = cuvid_decode_end, \
1172  FF_CODEC_RECEIVE_FRAME_CB(cuvid_output_frame), \
1173  .flush = cuvid_flush, \
1174  .bsfs = bsf_name, \
1175  .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
1176  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | \
1177  FF_CODEC_CAP_SETS_FRAME_PROPS, \
1178  .hw_configs = cuvid_hw_configs, \
1179  .p.wrapper_name = "cuvid", \
1180  };
1181 
1182 #if CONFIG_AV1_CUVID_DECODER && defined(CUVID_HAS_AV1_SUPPORT)
1183 DEFINE_CUVID_CODEC(av1, AV1, NULL)
1184 #endif
1185 
1186 #if CONFIG_HEVC_CUVID_DECODER
1187 DEFINE_CUVID_CODEC(hevc, HEVC, "hevc_mp4toannexb")
1188 #endif
1189 
1190 #if CONFIG_H264_CUVID_DECODER
1191 DEFINE_CUVID_CODEC(h264, H264, "h264_mp4toannexb")
1192 #endif
1193 
1194 #if CONFIG_MJPEG_CUVID_DECODER
1195 DEFINE_CUVID_CODEC(mjpeg, MJPEG, NULL)
1196 #endif
1197 
1198 #if CONFIG_MPEG1_CUVID_DECODER
1199 DEFINE_CUVID_CODEC(mpeg1, MPEG1VIDEO, NULL)
1200 #endif
1201 
1202 #if CONFIG_MPEG2_CUVID_DECODER
1203 DEFINE_CUVID_CODEC(mpeg2, MPEG2VIDEO, NULL)
1204 #endif
1205 
1206 #if CONFIG_MPEG4_CUVID_DECODER
1207 DEFINE_CUVID_CODEC(mpeg4, MPEG4, NULL)
1208 #endif
1209 
1210 #if CONFIG_VP8_CUVID_DECODER
1211 DEFINE_CUVID_CODEC(vp8, VP8, NULL)
1212 #endif
1213 
1214 #if CONFIG_VP9_CUVID_DECODER
1215 DEFINE_CUVID_CODEC(vp9, VP9, NULL)
1216 #endif
1217 
1218 #if CONFIG_VC1_CUVID_DECODER
1219 DEFINE_CUVID_CODEC(vc1, VC1, NULL)
1220 #endif
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
hwconfig.h
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:302
AVHWDeviceContext::hwctx
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:85
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:427
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
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:223
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:69
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
cuvid_handle_picture_display
static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO *dispinfo)
Definition: cuviddec.c:389
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:1222
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
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
AVFrame::color_primaries
enum AVColorPrimaries color_primaries
Definition: frame.h:655
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:160
CuvidParsedFrame::is_deinterlacing
int is_deinterlacing
Definition: cuviddec.c:114
AVFrame::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: frame.h:664
av_hwframe_ctx_init
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:322
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
pixdesc.h
AVFrame::width
int width
Definition: frame.h:446
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:686
cuvid_output_frame
static int cuvid_output_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: cuviddec.c:491
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:248
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:524
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:346
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:196
options
static const AVOption options[]
Definition: cuviddec.c:1130
mathematics.h
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:646
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
CuvidContext::caps12
CUVIDDECODECAPS caps12
Definition: cuviddec.c:101
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:395
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:638
AV_HWDEVICE_TYPE_CUDA
@ AV_HWDEVICE_TYPE_CUDA
Definition: hwcontext.h:30
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:560
fifo.h
bsf.h
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:454
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
dummy
int dummy
Definition: motion.c:66
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:502
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:334
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:633
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:330
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:60
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:148
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:206
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
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:625
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
CuvidContext::right
int right
Definition: cuviddec.c:74
cuvid_hw_configs
static const AVCodecHWConfigInternal *const cuvid_hw_configs[]
Definition: cuviddec.c:1143
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:491
format
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 format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
cuvid_handle_picture_decode
static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS *picparams)
Definition: cuviddec.c:372
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:220
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:1156
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:304
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
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:343
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:426
CuvidContext::codec_type
cudaVideoCodec codec_type
Definition: cuviddec.c:98
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
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:280
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:1129
cuvid_test_capabilities
static int cuvid_test_capabilities(AVCodecContext *avctx, const CUVIDPARSERPARAMS *cuparseinfo, int probed_width, int probed_height, int bit_depth)
Definition: cuviddec.c:729
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:126
CuvidContext::internal_error
int internal_error
Definition: cuviddec.c:93
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:480
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:109
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:415
AV_CODEC_HW_CONFIG_METHOD_INTERNAL
@ AV_CODEC_HW_CONFIG_METHOD_INTERNAL
The codec supports this format by some internal method.
Definition: codec.h:322
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:73
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:823
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:1556
AVPacket::size
int size
Definition: packet.h:525
AVFifo
Definition: fifo.c:35
codec_internal.h
AVCodecInternal::bsf
struct AVBSFContext * bsf
Definition: internal.h:77
AVCodecContext::pkt_timebase
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
Definition: avcodec.h:551
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:695
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
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:305
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:461
AVCodecHWConfigInternal
Definition: hwconfig.h:25
buffer.h
height
#define height
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
CHECK_CU
#define CHECK_CU(x)
Definition: cuviddec.c:117
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
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:303
AVCUDADeviceContextInternal::cuda_dl
CudaFunctions * cuda_dl
Definition: hwcontext_cuda_internal.h:32
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:517
CuvidContext::top
int top
Definition: cuviddec.c:73
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
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:76
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
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:606
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:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
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:1497
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:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
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:633
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:669
CuvidContext::resize
struct CuvidContext::@81 resize
AV_PIX_FMT_P016
#define AV_PIX_FMT_P016
Definition: pixfmt.h:530
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:1475
avcodec.h
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
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:264
AVHWFramesContext::device_ctx
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:134
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:600
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:433
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:725
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:1448
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVFrame::height
int height
Definition: frame.h:446
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
Definition: opt.h:235
CuvidContext::hwdevice
AVBufferRef * hwdevice
Definition: cuviddec.c:83
CuvidContext::crop
struct CuvidContext::@80 crop
AVPixFmtDescriptor::comp
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:105
CuvidParsedFrame::second_field
int second_field
Definition: cuviddec.c:113
OFFSET
#define OFFSET(x)
Definition: cuviddec.c:1128
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:528
FFCodec::bsfs
const char * bsfs
Decoding only, a comma-separated list of bitstream filters to apply to packets before decoding.
Definition: codec_internal.h:251
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:633
CuvidContext::hwframe
AVBufferRef * hwframe
Definition: cuviddec.c:84
cuvid_flush
static void cuvid_flush(AVCodecContext *avctx)
Definition: cuviddec.c:1078
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:472
AVPacket
This structure stores compressed data.
Definition: packet.h:501
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
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:419
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:664
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:239
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:491
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:244
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:2885
cuvid_handle_video_sequence
static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT *format)
Definition: cuviddec.c:125