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