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