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