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