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