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