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 
22 #include "libavutil/buffer.h"
23 #include "libavutil/mathematics.h"
24 #include "libavutil/hwcontext.h"
26 #include "libavutil/fifo.h"
27 #include "libavutil/log.h"
28 #include "libavutil/opt.h"
29 
30 #include "avcodec.h"
31 #include "internal.h"
32 
33 #include "compat/cuda/nvcuvid.h"
34 
35 #define MAX_FRAME_COUNT 25
36 
37 typedef struct CuvidContext
38 {
40 
43 
44  char *cu_gpu;
45 
48 
50 
52 
54  int64_t prev_pts;
55 
58 
61 
64 } CuvidContext;
65 
66 typedef struct CuvidParsedFrame
67 {
72 
73 static int check_cu(AVCodecContext *avctx, CUresult err, const char *func)
74 {
75  const char *err_name;
76  const char *err_string;
77 
78  av_log(avctx, AV_LOG_TRACE, "Calling %s\n", func);
79 
80  if (err == CUDA_SUCCESS)
81  return 0;
82 
83  cuGetErrorName(err, &err_name);
84  cuGetErrorString(err, &err_string);
85 
86  av_log(avctx, AV_LOG_ERROR, "%s failed", func);
87  if (err_name && err_string)
88  av_log(avctx, AV_LOG_ERROR, " -> %s: %s", err_name, err_string);
89  av_log(avctx, AV_LOG_ERROR, "\n");
90 
91  return AVERROR_EXTERNAL;
92 }
93 
94 #define CHECK_CU(x) check_cu(avctx, (x), #x)
95 
97 {
98  AVCodecContext *avctx = opaque;
99  CuvidContext *ctx = avctx->priv_data;
100  AVHWFramesContext *hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
101  CUVIDDECODECREATEINFO cuinfo;
102 
103  av_log(avctx, AV_LOG_TRACE, "pfnSequenceCallback, progressive_sequence=%d\n", format->progressive_sequence);
104 
105  ctx->internal_error = 0;
106 
107  avctx->width = format->display_area.right;
108  avctx->height = format->display_area.bottom;
109 
110  ff_set_sar(avctx, av_div_q(
112  (AVRational){ avctx->width, avctx->height }));
113 
116  else
118 
120  avctx->color_range = AVCOL_RANGE_JPEG;
121  else
122  avctx->color_range = AVCOL_RANGE_MPEG;
123 
127 
128  if (format->bitrate)
129  avctx->bit_rate = format->bitrate;
130 
131  if (format->frame_rate.numerator && format->frame_rate.denominator) {
132  avctx->framerate.num = format->frame_rate.numerator;
133  avctx->framerate.den = format->frame_rate.denominator;
134  }
135 
136  if (ctx->cudecoder
137  && avctx->coded_width == format->coded_width
138  && avctx->coded_height == format->coded_height
139  && ctx->chroma_format == format->chroma_format
140  && ctx->codec_type == format->codec)
141  return 1;
142 
143  if (ctx->cudecoder) {
144  av_log(avctx, AV_LOG_TRACE, "Re-initializing decoder\n");
146  if (ctx->internal_error < 0)
147  return 0;
148  ctx->cudecoder = NULL;
149  }
150 
151  if (hwframe_ctx->pool && (
152  hwframe_ctx->width < avctx->width ||
153  hwframe_ctx->height < avctx->height ||
154  hwframe_ctx->format != AV_PIX_FMT_CUDA ||
155  hwframe_ctx->sw_format != AV_PIX_FMT_NV12)) {
156  av_log(avctx, AV_LOG_ERROR, "AVHWFramesContext is already initialized with incompatible parameters\n");
157  ctx->internal_error = AVERROR(EINVAL);
158  return 0;
159  }
160 
161  if (format->chroma_format != cudaVideoChromaFormat_420) {
162  av_log(avctx, AV_LOG_ERROR, "Chroma formats other than 420 are not supported\n");
163  ctx->internal_error = AVERROR(EINVAL);
164  return 0;
165  }
166 
167  avctx->coded_width = format->coded_width;
168  avctx->coded_height = format->coded_height;
169 
170  ctx->chroma_format = format->chroma_format;
171 
172  memset(&cuinfo, 0, sizeof(cuinfo));
173 
174  cuinfo.CodecType = ctx->codec_type = format->codec;
175  cuinfo.ChromaFormat = format->chroma_format;
176  cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12;
177 
178  cuinfo.ulWidth = avctx->coded_width;
179  cuinfo.ulHeight = avctx->coded_height;
180  cuinfo.ulTargetWidth = cuinfo.ulWidth;
181  cuinfo.ulTargetHeight = cuinfo.ulHeight;
182 
183  cuinfo.target_rect.left = 0;
184  cuinfo.target_rect.top = 0;
185  cuinfo.target_rect.right = cuinfo.ulWidth;
186  cuinfo.target_rect.bottom = cuinfo.ulHeight;
187 
188  cuinfo.ulNumDecodeSurfaces = MAX_FRAME_COUNT;
189  cuinfo.ulNumOutputSurfaces = 1;
190  cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID;
191  cuinfo.bitDepthMinus8 = format->bit_depth_luma_minus8;
192 
193  if (format->progressive_sequence) {
194  ctx->deint_mode = cuinfo.DeinterlaceMode = cudaVideoDeinterlaceMode_Weave;
195  } else {
196  cuinfo.DeinterlaceMode = ctx->deint_mode;
197  }
198 
200  avctx->framerate = av_mul_q(avctx->framerate, (AVRational){2, 1});
201 
202  ctx->internal_error = CHECK_CU(cuvidCreateDecoder(&ctx->cudecoder, &cuinfo));
203  if (ctx->internal_error < 0)
204  return 0;
205 
206  if (!hwframe_ctx->pool) {
207  hwframe_ctx->format = AV_PIX_FMT_CUDA;
208  hwframe_ctx->sw_format = AV_PIX_FMT_NV12;
209  hwframe_ctx->width = avctx->width;
210  hwframe_ctx->height = avctx->height;
211 
212  if ((ctx->internal_error = av_hwframe_ctx_init(ctx->hwframe)) < 0) {
213  av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_init failed\n");
214  return 0;
215  }
216  }
217 
218  return 1;
219 }
220 
221 static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS* picparams)
222 {
223  AVCodecContext *avctx = opaque;
224  CuvidContext *ctx = avctx->priv_data;
225 
226  av_log(avctx, AV_LOG_TRACE, "pfnDecodePicture\n");
227 
228  ctx->internal_error = CHECK_CU(cuvidDecodePicture(ctx->cudecoder, picparams));
229  if (ctx->internal_error < 0)
230  return 0;
231 
232  return 1;
233 }
234 
235 static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO* dispinfo)
236 {
237  AVCodecContext *avctx = opaque;
238  CuvidContext *ctx = avctx->priv_data;
239  CuvidParsedFrame parsed_frame = { *dispinfo, 0, 0 };
240 
241  ctx->internal_error = 0;
242 
244  av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
245  } else {
246  parsed_frame.is_deinterlacing = 1;
247  av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
248  parsed_frame.second_field = 1;
249  av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
250  }
251 
252  return 1;
253 }
254 
255 static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
256 {
257  CuvidContext *ctx = avctx->priv_data;
258  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
259  AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
260  CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
261  CUVIDSOURCEDATAPACKET cupkt;
262  AVPacket filter_packet = { 0 };
263  AVPacket filtered_packet = { 0 };
264  int ret = 0, eret = 0, is_flush = ctx->decoder_flushing;
265 
266  av_log(avctx, AV_LOG_TRACE, "cuvid_decode_packet\n");
267 
268  if (is_flush && avpkt && avpkt->size)
269  return AVERROR_EOF;
270 
271  if (av_fifo_size(ctx->frame_queue) / sizeof(CuvidParsedFrame) > MAX_FRAME_COUNT - 2 && avpkt && avpkt->size)
272  return AVERROR(EAGAIN);
273 
274  if (ctx->bsf && avpkt && avpkt->size) {
275  if ((ret = av_packet_ref(&filter_packet, avpkt)) < 0) {
276  av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed\n");
277  return ret;
278  }
279 
280  if ((ret = av_bsf_send_packet(ctx->bsf, &filter_packet)) < 0) {
281  av_log(avctx, AV_LOG_ERROR, "av_bsf_send_packet failed\n");
283  return ret;
284  }
285 
286  if ((ret = av_bsf_receive_packet(ctx->bsf, &filtered_packet)) < 0) {
287  av_log(avctx, AV_LOG_ERROR, "av_bsf_receive_packet failed\n");
288  return ret;
289  }
290 
291  avpkt = &filtered_packet;
292  }
293 
294  ret = CHECK_CU(cuCtxPushCurrent(cuda_ctx));
295  if (ret < 0) {
296  av_packet_unref(&filtered_packet);
297  return ret;
298  }
299 
300  memset(&cupkt, 0, sizeof(cupkt));
301 
302  if (avpkt && avpkt->size) {
303  cupkt.payload_size = avpkt->size;
304  cupkt.payload = avpkt->data;
305 
306  if (avpkt->pts != AV_NOPTS_VALUE) {
307  cupkt.flags = CUVID_PKT_TIMESTAMP;
308  if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
309  cupkt.timestamp = av_rescale_q(avpkt->pts, avctx->pkt_timebase, (AVRational){1, 10000000});
310  else
311  cupkt.timestamp = avpkt->pts;
312  }
313  } else {
314  cupkt.flags = CUVID_PKT_ENDOFSTREAM;
315  ctx->decoder_flushing = 1;
316  }
317 
318  ret = CHECK_CU(cuvidParseVideoData(ctx->cuparser, &cupkt));
319 
320  av_packet_unref(&filtered_packet);
321 
322  if (ret < 0)
323  goto error;
324 
325  // cuvidParseVideoData doesn't return an error just because stuff failed...
326  if (ctx->internal_error) {
327  av_log(avctx, AV_LOG_ERROR, "cuvid decode callback error\n");
328  ret = ctx->internal_error;
329  goto error;
330  }
331 
332 error:
333  eret = CHECK_CU(cuCtxPopCurrent(&dummy));
334 
335  if (eret < 0)
336  return eret;
337  else if (ret < 0)
338  return ret;
339  else if (is_flush)
340  return AVERROR_EOF;
341  else
342  return 0;
343 }
344 
346 {
347  CuvidContext *ctx = avctx->priv_data;
348  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
349  AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
350  CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
351  CUdeviceptr mapped_frame = 0;
352  int ret = 0, eret = 0;
353 
354  av_log(avctx, AV_LOG_TRACE, "cuvid_output_frame\n");
355 
356  if (ctx->decoder_flushing) {
357  ret = cuvid_decode_packet(avctx, NULL);
358  if (ret < 0 && ret != AVERROR_EOF)
359  return ret;
360  }
361 
362  ret = CHECK_CU(cuCtxPushCurrent(cuda_ctx));
363  if (ret < 0)
364  return ret;
365 
366  if (av_fifo_size(ctx->frame_queue)) {
367  CuvidParsedFrame parsed_frame;
369  unsigned int pitch = 0;
370  int offset = 0;
371  int i;
372 
373  av_fifo_generic_read(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
374 
375  memset(&params, 0, sizeof(params));
376  params.progressive_frame = parsed_frame.dispinfo.progressive_frame;
377  params.second_field = parsed_frame.second_field;
378  params.top_field_first = parsed_frame.dispinfo.top_field_first;
379 
380  ret = CHECK_CU(cuvidMapVideoFrame(ctx->cudecoder, parsed_frame.dispinfo.picture_index, &mapped_frame, &pitch, &params));
381  if (ret < 0)
382  goto error;
383 
384  if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
385  ret = av_hwframe_get_buffer(ctx->hwframe, frame, 0);
386  if (ret < 0) {
387  av_log(avctx, AV_LOG_ERROR, "av_hwframe_get_buffer failed\n");
388  goto error;
389  }
390 
391  ret = ff_decode_frame_props(avctx, frame);
392  if (ret < 0) {
393  av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props failed\n");
394  goto error;
395  }
396 
397  for (i = 0; i < 2; i++) {
398  CUDA_MEMCPY2D cpy = {
399  .srcMemoryType = CU_MEMORYTYPE_DEVICE,
400  .dstMemoryType = CU_MEMORYTYPE_DEVICE,
401  .srcDevice = mapped_frame,
402  .dstDevice = (CUdeviceptr)frame->data[i],
403  .srcPitch = pitch,
404  .dstPitch = frame->linesize[i],
405  .srcY = offset,
406  .WidthInBytes = FFMIN(pitch, frame->linesize[i]),
407  .Height = avctx->height >> (i ? 1 : 0),
408  };
409 
410  ret = CHECK_CU(cuMemcpy2D(&cpy));
411  if (ret < 0)
412  goto error;
413 
414  offset += avctx->coded_height;
415  }
416  } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
417  AVFrame *tmp_frame = av_frame_alloc();
418  if (!tmp_frame) {
419  av_log(avctx, AV_LOG_ERROR, "av_frame_alloc failed\n");
420  ret = AVERROR(ENOMEM);
421  goto error;
422  }
423 
424  tmp_frame->format = AV_PIX_FMT_CUDA;
425  tmp_frame->hw_frames_ctx = av_buffer_ref(ctx->hwframe);
426  tmp_frame->data[0] = (uint8_t*)mapped_frame;
427  tmp_frame->linesize[0] = pitch;
428  tmp_frame->data[1] = (uint8_t*)(mapped_frame + avctx->coded_height * pitch);
429  tmp_frame->linesize[1] = pitch;
430  tmp_frame->width = avctx->width;
431  tmp_frame->height = avctx->height;
432 
433  ret = ff_get_buffer(avctx, frame, 0);
434  if (ret < 0) {
435  av_log(avctx, AV_LOG_ERROR, "ff_get_buffer failed\n");
436  av_frame_free(&tmp_frame);
437  goto error;
438  }
439 
440  ret = av_hwframe_transfer_data(frame, tmp_frame, 0);
441  if (ret) {
442  av_log(avctx, AV_LOG_ERROR, "av_hwframe_transfer_data failed\n");
443  av_frame_free(&tmp_frame);
444  goto error;
445  }
446 
447  av_frame_free(&tmp_frame);
448  } else {
449  ret = AVERROR_BUG;
450  goto error;
451  }
452 
453  frame->width = avctx->width;
454  frame->height = avctx->height;
455  if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
456  frame->pts = av_rescale_q(parsed_frame.dispinfo.timestamp, (AVRational){1, 10000000}, avctx->pkt_timebase);
457  else
458  frame->pts = parsed_frame.dispinfo.timestamp;
459 
460  if (parsed_frame.second_field) {
461  if (ctx->prev_pts == INT64_MIN) {
462  ctx->prev_pts = frame->pts;
463  frame->pts += (avctx->pkt_timebase.den * avctx->framerate.den) / (avctx->pkt_timebase.num * avctx->framerate.num);
464  } else {
465  int pts_diff = (frame->pts - ctx->prev_pts) / 2;
466  ctx->prev_pts = frame->pts;
467  frame->pts += pts_diff;
468  }
469  }
470 
471  /* CUVIDs opaque reordering breaks the internal pkt logic.
472  * So set pkt_pts and clear all the other pkt_ fields.
473  */
474 #if FF_API_PKT_PTS
476  frame->pkt_pts = frame->pts;
478 #endif
479  av_frame_set_pkt_pos(frame, -1);
480  av_frame_set_pkt_duration(frame, 0);
481  av_frame_set_pkt_size(frame, -1);
482 
483  frame->interlaced_frame = !parsed_frame.is_deinterlacing && !parsed_frame.dispinfo.progressive_frame;
484 
485  if (frame->interlaced_frame)
486  frame->top_field_first = parsed_frame.dispinfo.top_field_first;
487  } else if (ctx->decoder_flushing) {
488  ret = AVERROR_EOF;
489  } else {
490  ret = AVERROR(EAGAIN);
491  }
492 
493 error:
494  if (mapped_frame)
495  eret = CHECK_CU(cuvidUnmapVideoFrame(ctx->cudecoder, mapped_frame));
496 
497  eret = CHECK_CU(cuCtxPopCurrent(&dummy));
498 
499  if (eret < 0)
500  return eret;
501  else
502  return ret;
503 }
504 
505 static int cuvid_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
506 {
507  CuvidContext *ctx = avctx->priv_data;
508  AVFrame *frame = data;
509  int ret = 0;
510 
511  av_log(avctx, AV_LOG_TRACE, "cuvid_decode_frame\n");
512 
514  av_log(avctx, AV_LOG_ERROR, "Deinterlacing is not supported via the old API\n");
515  return AVERROR(EINVAL);
516  }
517 
518  if (!ctx->decoder_flushing) {
519  ret = cuvid_decode_packet(avctx, avpkt);
520  if (ret < 0)
521  return ret;
522  }
523 
524  ret = cuvid_output_frame(avctx, frame);
525  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
526  *got_frame = 0;
527  } else if (ret < 0) {
528  return ret;
529  } else {
530  *got_frame = 1;
531  }
532 
533  return 0;
534 }
535 
537 {
538  CuvidContext *ctx = avctx->priv_data;
539 
540  av_fifo_freep(&ctx->frame_queue);
541 
542  if (ctx->bsf)
543  av_bsf_free(&ctx->bsf);
544 
545  if (ctx->cuparser)
547 
548  if (ctx->cudecoder)
550 
551  av_buffer_unref(&ctx->hwframe);
552  av_buffer_unref(&ctx->hwdevice);
553 
554  return 0;
555 }
556 
558 {
559  CUVIDDECODECREATEINFO cuinfo;
560  CUvideodecoder cudec = 0;
561  int ret = 0;
562 
563  memset(&cuinfo, 0, sizeof(cuinfo));
564 
565  cuinfo.CodecType = cuparseinfo->CodecType;
568 
569  cuinfo.ulWidth = 1280;
570  cuinfo.ulHeight = 720;
571  cuinfo.ulTargetWidth = cuinfo.ulWidth;
572  cuinfo.ulTargetHeight = cuinfo.ulHeight;
573 
574  cuinfo.target_rect.left = 0;
575  cuinfo.target_rect.top = 0;
576  cuinfo.target_rect.right = cuinfo.ulWidth;
577  cuinfo.target_rect.bottom = cuinfo.ulHeight;
578 
580  cuinfo.ulNumOutputSurfaces = 1;
582  cuinfo.bitDepthMinus8 = 0;
583 
585 
586  ret = CHECK_CU(cuvidCreateDecoder(&cudec, &cuinfo));
587  if (ret < 0)
588  return ret;
589 
590  ret = CHECK_CU(cuvidDestroyDecoder(cudec));
591  if (ret < 0)
592  return ret;
593 
594  return 0;
595 }
596 
598 {
599  CuvidContext *ctx = avctx->priv_data;
600  AVCUDADeviceContext *device_hwctx;
601  AVHWDeviceContext *device_ctx;
602  AVHWFramesContext *hwframe_ctx;
603  CUVIDSOURCEDATAPACKET seq_pkt;
604  CUcontext cuda_ctx = NULL;
606  const AVBitStreamFilter *bsf;
607  int ret = 0;
608 
611  AV_PIX_FMT_NONE };
612 
613  ret = ff_get_format(avctx, pix_fmts);
614  if (ret < 0) {
615  av_log(avctx, AV_LOG_ERROR, "ff_get_format failed: %d\n", ret);
616  return ret;
617  }
618 
620  if (!ctx->frame_queue) {
621  ret = AVERROR(ENOMEM);
622  goto error;
623  }
624 
625  avctx->pix_fmt = ret;
626 
627  if (avctx->hw_frames_ctx) {
628  ctx->hwframe = av_buffer_ref(avctx->hw_frames_ctx);
629  if (!ctx->hwframe) {
630  ret = AVERROR(ENOMEM);
631  goto error;
632  }
633 
634  hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
635 
636  ctx->hwdevice = av_buffer_ref(hwframe_ctx->device_ref);
637  if (!ctx->hwdevice) {
638  ret = AVERROR(ENOMEM);
639  goto error;
640  }
641  } else {
643  if (ret < 0)
644  goto error;
645 
647  if (!ctx->hwframe) {
648  av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n");
649  ret = AVERROR(ENOMEM);
650  goto error;
651  }
652 
653  hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data;
654  }
655 
656  device_ctx = hwframe_ctx->device_ctx;
657  device_hwctx = device_ctx->hwctx;
658  cuda_ctx = device_hwctx->cuda_ctx;
659 
660  memset(&ctx->cuparseinfo, 0, sizeof(ctx->cuparseinfo));
661  memset(&ctx->cuparse_ext, 0, sizeof(ctx->cuparse_ext));
662  memset(&seq_pkt, 0, sizeof(seq_pkt));
663 
665 
666  switch (avctx->codec->id) {
667 #if CONFIG_H263_CUVID_DECODER
668  case AV_CODEC_ID_H263:
670  break;
671 #endif
672 #if CONFIG_H264_CUVID_DECODER
673  case AV_CODEC_ID_H264:
675  break;
676 #endif
677 #if CONFIG_HEVC_CUVID_DECODER
678  case AV_CODEC_ID_HEVC:
680  break;
681 #endif
682 #if CONFIG_MJPEG_CUVID_DECODER
683  case AV_CODEC_ID_MJPEG:
685  break;
686 #endif
687 #if CONFIG_MPEG1_CUVID_DECODER
690  break;
691 #endif
692 #if CONFIG_MPEG2_CUVID_DECODER
695  break;
696 #endif
697 #if CONFIG_MPEG4_CUVID_DECODER
698  case AV_CODEC_ID_MPEG4:
700  break;
701 #endif
702 #if CONFIG_VP8_CUVID_DECODER
703  case AV_CODEC_ID_VP8:
705  break;
706 #endif
707 #if CONFIG_VP9_CUVID_DECODER
708  case AV_CODEC_ID_VP9:
710  break;
711 #endif
712 #if CONFIG_VC1_CUVID_DECODER
713  case AV_CODEC_ID_VC1:
715  break;
716 #endif
717  default:
718  av_log(avctx, AV_LOG_ERROR, "Invalid CUVID codec!\n");
719  return AVERROR_BUG;
720  }
721 
722  if (avctx->codec->id == AV_CODEC_ID_H264 || avctx->codec->id == AV_CODEC_ID_HEVC) {
723  if (avctx->codec->id == AV_CODEC_ID_H264)
724  bsf = av_bsf_get_by_name("h264_mp4toannexb");
725  else
726  bsf = av_bsf_get_by_name("hevc_mp4toannexb");
727 
728  if (!bsf) {
729  ret = AVERROR_BSF_NOT_FOUND;
730  goto error;
731  }
732  if (ret = av_bsf_alloc(bsf, &ctx->bsf)) {
733  goto error;
734  }
735  if (((ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx)) < 0) || ((ret = av_bsf_init(ctx->bsf)) < 0)) {
736  av_bsf_free(&ctx->bsf);
737  goto error;
738  }
739 
741  memcpy(ctx->cuparse_ext.raw_seqhdr_data,
742  ctx->bsf->par_out->extradata,
744  } else if (avctx->extradata_size > 0) {
746  memcpy(ctx->cuparse_ext.raw_seqhdr_data,
747  avctx->extradata,
748  FFMIN(sizeof(ctx->cuparse_ext.raw_seqhdr_data), avctx->extradata_size));
749  }
750 
753  ctx->cuparseinfo.pUserData = avctx;
757 
758  ret = CHECK_CU(cuCtxPushCurrent(cuda_ctx));
759  if (ret < 0)
760  goto error;
761 
762  ret = cuvid_test_dummy_decoder(avctx, &ctx->cuparseinfo);
763  if (ret < 0)
764  goto error;
765 
767  if (ret < 0)
768  goto error;
769 
770  seq_pkt.payload = ctx->cuparse_ext.raw_seqhdr_data;
772 
773  if (seq_pkt.payload && seq_pkt.payload_size) {
774  ret = CHECK_CU(cuvidParseVideoData(ctx->cuparser, &seq_pkt));
775  if (ret < 0)
776  goto error;
777  }
778 
779  ret = CHECK_CU(cuCtxPopCurrent(&dummy));
780  if (ret < 0)
781  goto error;
782 
783  ctx->prev_pts = INT64_MIN;
784 
785  if (!avctx->pkt_timebase.num || !avctx->pkt_timebase.den)
786  av_log(avctx, AV_LOG_WARNING, "Invalid pkt_timebase, passing timestamps as-is.\n");
787 
788  return 0;
789 
790 error:
791  cuvid_decode_end(avctx);
792  return ret;
793 }
794 
795 static void cuvid_flush(AVCodecContext *avctx)
796 {
797  CuvidContext *ctx = avctx->priv_data;
798  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data;
799  AVCUDADeviceContext *device_hwctx = device_ctx->hwctx;
800  CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
801  CUVIDSOURCEDATAPACKET seq_pkt = { 0 };
802  int ret;
803 
804  ret = CHECK_CU(cuCtxPushCurrent(cuda_ctx));
805  if (ret < 0)
806  goto error;
807 
808  av_fifo_freep(&ctx->frame_queue);
809 
810  ctx->frame_queue = av_fifo_alloc(MAX_FRAME_COUNT * sizeof(CuvidParsedFrame));
811  if (!ctx->frame_queue) {
812  av_log(avctx, AV_LOG_ERROR, "Failed to recreate frame queue on flush\n");
813  return;
814  }
815 
816  if (ctx->cudecoder) {
817  cuvidDestroyDecoder(ctx->cudecoder);
818  ctx->cudecoder = NULL;
819  }
820 
821  if (ctx->cuparser) {
822  cuvidDestroyVideoParser(ctx->cuparser);
823  ctx->cuparser = NULL;
824  }
825 
826  ret = CHECK_CU(cuvidCreateVideoParser(&ctx->cuparser, &ctx->cuparseinfo));
827  if (ret < 0)
828  goto error;
829 
830  seq_pkt.payload = ctx->cuparse_ext.raw_seqhdr_data;
831  seq_pkt.payload_size = ctx->cuparse_ext.format.seqhdr_data_length;
832 
833  if (seq_pkt.payload && seq_pkt.payload_size) {
834  ret = CHECK_CU(cuvidParseVideoData(ctx->cuparser, &seq_pkt));
835  if (ret < 0)
836  goto error;
837  }
838 
839  ret = CHECK_CU(cuCtxPopCurrent(&dummy));
840  if (ret < 0)
841  goto error;
842 
843  ctx->prev_pts = INT64_MIN;
844  ctx->decoder_flushing = 0;
845 
846  return;
847  error:
848  av_log(avctx, AV_LOG_ERROR, "CUDA reinit on flush failed\n");
849 }
850 
851 #define OFFSET(x) offsetof(CuvidContext, x)
852 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
853 static const AVOption options[] = {
854  { "deint", "Set deinterlacing mode", OFFSET(deint_mode), AV_OPT_TYPE_INT, { .i64 = cudaVideoDeinterlaceMode_Weave }, cudaVideoDeinterlaceMode_Weave, cudaVideoDeinterlaceMode_Adaptive, VD, "deint" },
855  { "weave", "Weave deinterlacing (do nothing)", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Weave }, 0, 0, VD, "deint" },
856  { "bob", "Bob deinterlacing", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Bob }, 0, 0, VD, "deint" },
857  { "adaptive", "Adaptive deinterlacing", 0, AV_OPT_TYPE_CONST, { .i64 = cudaVideoDeinterlaceMode_Adaptive }, 0, 0, VD, "deint" },
858  { "gpu", "GPU to be used for decoding", OFFSET(cu_gpu), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
859  { NULL }
860 };
861 
862 #define DEFINE_CUVID_CODEC(x, X) \
863  static const AVClass x##_cuvid_class = { \
864  .class_name = #x "_cuvid", \
865  .item_name = av_default_item_name, \
866  .option = options, \
867  .version = LIBAVUTIL_VERSION_INT, \
868  }; \
869  AVHWAccel ff_##x##_cuvid_hwaccel = { \
870  .name = #x "_cuvid", \
871  .type = AVMEDIA_TYPE_VIDEO, \
872  .id = AV_CODEC_ID_##X, \
873  .pix_fmt = AV_PIX_FMT_CUDA, \
874  }; \
875  AVCodec ff_##x##_cuvid_decoder = { \
876  .name = #x "_cuvid", \
877  .long_name = NULL_IF_CONFIG_SMALL("Nvidia CUVID " #X " decoder"), \
878  .type = AVMEDIA_TYPE_VIDEO, \
879  .id = AV_CODEC_ID_##X, \
880  .priv_data_size = sizeof(CuvidContext), \
881  .priv_class = &x##_cuvid_class, \
882  .init = cuvid_decode_init, \
883  .close = cuvid_decode_end, \
884  .decode = cuvid_decode_frame, \
885  .send_packet = cuvid_decode_packet, \
886  .receive_frame = cuvid_output_frame, \
887  .flush = cuvid_flush, \
888  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
889  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, \
890  AV_PIX_FMT_NV12, \
891  AV_PIX_FMT_NONE }, \
892  };
893 
894 #if CONFIG_HEVC_CUVID_DECODER
895 DEFINE_CUVID_CODEC(hevc, HEVC)
896 #endif
897 
898 #if CONFIG_H263_CUVID_DECODER
899 DEFINE_CUVID_CODEC(h263, H263)
900 #endif
901 
902 #if CONFIG_H264_CUVID_DECODER
903 DEFINE_CUVID_CODEC(h264, H264)
904 #endif
905 
906 #if CONFIG_MJPEG_CUVID_DECODER
907 DEFINE_CUVID_CODEC(mjpeg, MJPEG)
908 #endif
909 
910 #if CONFIG_MPEG1_CUVID_DECODER
911 DEFINE_CUVID_CODEC(mpeg1, MPEG1VIDEO)
912 #endif
913 
914 #if CONFIG_MPEG2_CUVID_DECODER
915 DEFINE_CUVID_CODEC(mpeg2, MPEG2VIDEO)
916 #endif
917 
918 #if CONFIG_MPEG4_CUVID_DECODER
919 DEFINE_CUVID_CODEC(mpeg4, MPEG4)
920 #endif
921 
922 #if CONFIG_VP8_CUVID_DECODER
923 DEFINE_CUVID_CODEC(vp8, VP8)
924 #endif
925 
926 #if CONFIG_VP9_CUVID_DECODER
927 DEFINE_CUVID_CODEC(vp9, VP9)
928 #endif
929 
930 #if CONFIG_VC1_CUVID_DECODER
931 DEFINE_CUVID_CODEC(vc1, VC1)
932 #endif
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:54
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:36
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1685
AVRational framerate
Definition: avcodec.h:3375
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5762
unsigned char raw_seqhdr_data[1024]
Definition: nvcuvid.h:147
int decoder_flushing
Definition: cuvid.c:57
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:124
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVBufferRef * hwdevice
Definition: cuvid.c:46
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
Definition: avcodec.h:874
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1878
int right
right position of display rect
Definition: nvcuvid.h:114
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1741
#define MAX_FRAME_COUNT
Definition: cuvid.c:35
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2413
CUresult CUDAAPI cuvidMapVideoFrame(CUvideodecoder hDecoder, int nPicIdx, unsigned int *pDevPtr, unsigned int *pPitch, CUVIDPROCPARAMS *pVPP)
Post-process and map a video frame for use in cuda.
int num
Numerator.
Definition: rational.h:59
The bitstream filter state.
Definition: avcodec.h:5731
static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT *format)
Definition: cuvid.c:96
int deint_mode
Definition: cuvid.c:53
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:222
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1904
void av_frame_set_pkt_duration(AVFrame *frame, int64_t val)
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:202
struct CUVIDDECODECREATEINFO::@25 target_rect
target rectangle in the output frame (for aspect ratio conversion) if a null rectangle is specified...
#define DEFINE_CUVID_CODEC(x, X)
Definition: cuvid.c:862
CUVIDPARSERPARAMS cuparseinfo
Definition: cuvid.c:62
Use dedicated video engines directly.
Definition: cuviddec.h:122
void av_frame_set_pkt_size(AVFrame *frame, int val)
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:135
unsigned int seqhdr_data_length
Additional bytes following (CUVIDEOFORMATEX)
Definition: nvcuvid.h:137
CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj)
static int check_cu(AVCodecContext *avctx, CUresult err, const char *func)
Definition: cuvid.c:73
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
cudaVideoDeinterlaceMode DeinterlaceMode
cudaVideoDeinterlaceMode_XXX
Definition: cuviddec.h:150
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Definition: frame.h:534
Set when this is the last packet for this stream.
Definition: nvcuvid.h:170
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:82
static void cuvid_flush(AVCodecContext *avctx)
Definition: cuvid.c:795
cudaVideoCodec CodecType
cudaVideoCodec_XXX
Definition: cuviddec.h:134
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:199
Drop one field.
Definition: cuviddec.h:99
cudaVideoSurfaceFormat OutputFormat
cudaVideoSurfaceFormat_XXX
Definition: cuviddec.h:149
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:145
AVOptions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Definition: cuvid.c:255
int internal_error
Definition: cuvid.c:56
CUvideoparser cuparser
Definition: cuvid.c:42
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data.
Definition: utils.c:870
unsigned long ulCreationFlags
Decoder creation flags (cudaVideoCreateFlags_XXX)
Definition: cuviddec.h:136
unsigned int numerator
frame rate numerator (0 = unspecified or variable frame rate)
Definition: nvcuvid.h:96
static av_cold int cuvid_decode_init(AVCodecContext *avctx)
Definition: cuvid.c:597
unsigned char color_primaries
Definition: nvcuvid.h:133
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
int is_deinterlacing
Definition: cuvid.c:70
AVFifoBuffer * frame_queue
Definition: cuvid.c:51
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1791
CUresult CUDAAPI cuvidDecodePicture(CUvideodecoder hDecoder, CUVIDPICPARAMS *pPicParams)
Decode a single picture (field or frame)
static AVFrame * frame
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:85
CUVIDEOFORMATEX * pExtVideoInfo
[Optional] sequence header data from system layer
Definition: nvcuvid.h:294
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:459
CUresult CUDAAPI cuvidCreateDecoder(CUvideodecoder *phDecoder, CUVIDDECODECREATEINFO *pdci)
In order to minimize decode latencies, there should be always at least 2 pictures in the decode queue...
static av_cold int cuvid_decode_end(AVCodecContext *avctx)
Definition: cuvid.c:536
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:225
#define AVERROR_EOF
End of file.
Definition: error.h:55
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:322
cudaVideoChromaFormat
Definition: cuviddec.h:107
#define CUDAAPI
Definition: nvenc.h:38
unsigned char transfer_characteristics
Definition: nvcuvid.h:134
#define av_log(a,...)
void * CUvideodecoder
Definition: cuviddec.h:52
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:576
An API-specific header for AV_HWDEVICE_TYPE_CUDA.
Weave both fields (no deinterlacing)
Definition: cuviddec.h:98
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
unsigned char bit_depth_luma_minus8
high bit depth Luma
Definition: nvcuvid.h:100
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are.
Definition: avcodec.h:3391
enum AVCodecID id
Definition: avcodec.h:3614
cudaVideoChromaFormat chroma_format
Definition: cuvid.c:60
int width
width and height of the video frame
Definition: frame.h:236
#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:59
#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:158
unsigned long ulNumDecodeSurfaces
Maximum number of internal decode surfaces.
Definition: cuviddec.h:133
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
unsigned long ulNumOutputSurfaces
Maximum number of output surfaces simultaneously mapped.
Definition: cuviddec.h:153
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1771
GLenum GLint * params
Definition: opengl_enc.c:114
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
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:263
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:390
unsigned long payload_size
number of bytes in the payload (may be zero if EOS flag is set)
Definition: nvcuvid.h:182
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3998
static int cuvid_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cuvid.c:505
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:364
int bottom
bottom position of display rect
Definition: nvcuvid.h:115
int progressive_frame
Input is progressive (deinterlace_mode will be ignored)
Definition: cuviddec.h:672
#define CHECK_CU(x)
Definition: cuvid.c:94
unsigned long ulTargetHeight
Post-processed Output Height (Should be aligbed to 2)
Definition: cuviddec.h:152
Adaptive deinterlacing.
Definition: cuviddec.h:100
#define FFMIN(a, b)
Definition: common.h:96
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:142
int second_field
Output the second field (ignored if deinterlace mode is Weave)
Definition: cuviddec.h:673
int width
picture width / height.
Definition: avcodec.h:1863
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
Definition: concatdec.c:521
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:3542
int top_field_first
Input frame is top field first (1st field is top, 2nd field is bottom)
Definition: cuviddec.h:674
AVBufferRef * hwframe
Definition: cuvid.c:47
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:176
cudaVideoCodec codec
Compression format.
Definition: nvcuvid.h:91
AVFormatContext * ctx
Definition: movenc.c:48
void * CUvideoparser
Definition: nvcuvid.h:55
void av_frame_set_pkt_pos(AVFrame *frame, int64_t val)
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2392
unsigned int coded_width
coded frame width
Definition: nvcuvid.h:103
unsigned int denominator
frame rate denominator (0 = unspecified or variable frame rate)
Definition: nvcuvid.h:97
#define AVERROR_BSF_NOT_FOUND
Bitstream filter not found.
Definition: error.h:49
CUresult
Definition: nvenc.h:41
int dummy
Definition: motion.c:64
static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO *dispinfo)
Definition: cuvid.c:235
unsigned char video_full_range_flag
Definition: nvcuvid.h:131
CUresult CUDAAPI cuvidCreateVideoParser(CUvideoparser *pObj, CUVIDPARSERPARAMS *pParams)
HW acceleration through CUDA.
Definition: pixfmt.h:249
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:196
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:460
CUvideodecoder cudecoder
Definition: cuvid.c:41
unsigned char progressive_sequence
0=interlaced, 1=progressive
Definition: nvcuvid.h:99
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: utils.c:1111
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:248
struct CUVIDEOFORMAT::@34 frame_rate
frame rate = numerator / denominator (for example: 30000/1001)
CUresult CUDAAPI cuvidDestroyDecoder(CUvideodecoder hDecoder)
Destroy the decoder object.
static int cuvid_test_dummy_decoder(AVCodecContext *avctx, CUVIDPARSERPARAMS *cuparseinfo)
Definition: cuvid.c:557
Libavcodec external API header.
unsigned char matrix_coefficients
Definition: nvcuvid.h:135
PFNVIDSEQUENCECALLBACK pfnSequenceCallback
Called before decoding frames and/or whenever there is a format change.
Definition: nvcuvid.h:290
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:215
main external API structure.
Definition: avcodec.h:1676
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:567
uint8_t * data
The data buffer.
Definition: buffer.h:89
#define VD
Definition: cuvid.c:852
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:947
a very simple circular buffer FIFO implementation
AVBSFContext * bsf
Definition: cuvid.c:49
int extradata_size
Definition: avcodec.h:1792
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
void * CUdeviceptr
Definition: nvenc.h:46
This struct is allocated as AVHWDeviceContext.hwctx.
int coded_height
Definition: avcodec.h:1878
static const char * format
Definition: movenc.c:47
Describe the class of an AVClass context structure.
Definition: log.h:67
const unsigned char * payload
Pointer to packet payload data (may be NULL if EOS flag is set)
Definition: nvcuvid.h:183
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2406
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2399
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:4166
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:117
CUvideotimestamp timestamp
Definition: nvcuvid.h:266
static const AVOption options[]
Definition: cuvid.c:853
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:67
refcounted data buffer API
#define OFFSET(x)
Definition: cuvid.c:851
unsigned int coded_height
coded frame height
Definition: nvcuvid.h:104
char * cu_gpu
Definition: cuvid.c:44
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
cudaVideoCodec CodecType
cudaVideoCodec_XXX
Definition: nvcuvid.h:283
CUVIDPARSERDISPINFO dispinfo
Definition: cuvid.c:68
static int cuvid_output_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: cuvid.c:345
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:134
attribute_deprecated int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
Definition: frame.h:276
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:459
int second_field
Definition: cuvid.c:69
A reference to a data buffer.
Definition: buffer.h:81
NvCuvid API provides Video Decoding interface to NVIDIA GPU devices.
PFNVIDDECODECALLBACK pfnDecodePicture
Called when a picture is ready to be decoded (decode order)
Definition: nvcuvid.h:291
void * pUserData
User data for callbacks.
Definition: nvcuvid.h:289
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
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:177
NV12 (currently the only supported output format)
Definition: cuviddec.h:90
int64_t prev_pts
Definition: cuvid.c:54
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS *picparams)
Definition: cuvid.c:221
unsigned long bitDepthMinus8
Definition: cuviddec.h:137
int den
Denominator.
Definition: rational.h:60
CUVIDEOFORMAT format
Definition: nvcuvid.h:146
unsigned int ulMaxNumDecodeSurfaces
Max # of decode surfaces (parser will cycle through these)
Definition: nvcuvid.h:284
unsigned long ulHeight
Coded Sequence Height.
Definition: cuviddec.h:132
void * priv_data
Definition: avcodec.h:1718
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Definition: hwcontext.h:183
unsigned long ulWidth
Coded Sequence Width.
Definition: cuviddec.h:131
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:327
CUresult CUDAAPI cuvidUnmapVideoFrame(CUvideodecoder hDecoder, unsigned int DevPtr)
Unmap a previously mapped video frame.
cudaVideoChromaFormat ChromaFormat
cudaVideoChromaFormat_XXX (only 4:2:0 is currently supported)
Definition: cuviddec.h:135
unsigned int bitrate
video bitrate (bps, 0=unknown)
Definition: nvcuvid.h:118
unsigned long ulTargetWidth
Post-processed Output Width (Should be aligned to 2)
Definition: cuviddec.h:151
PFNVIDDISPLAYCALLBACK pfnDisplayPicture
Called whenever a picture is ready to be displayed (display order)
Definition: nvcuvid.h:292
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3994
cudaVideoCodec
Definition: cuviddec.h:64
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
int height
Definition: frame.h:236
unsigned int ulMaxDisplayDelay
Max display queue delay (improves pipelining of decode with display) - 0=no delay (recommended values...
Definition: nvcuvid.h:287
CUVIDEOFORMATEX cuparse_ext
Definition: cuvid.c:63
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
Definition: fifo.c:63
cudaVideoChromaFormat chroma_format
Chroma format.
Definition: nvcuvid.h:117
struct CUVIDEOFORMAT::@35 display_area
area of the frame that should be displayed typical example: coded_width = 1920, coded_height = 1088 d...
void * CUcontext
Definition: nvenc.h:45
Timestamp is valid.
Definition: nvcuvid.h:171
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:215
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
struct CUVIDEOFORMAT::@37 video_signal_description
Video Signal Description.
This structure stores compressed data.
Definition: avcodec.h:1578
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5757
CUresult CUDAAPI cuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET *pPacket)
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:242
struct CUVIDEOFORMAT::@36 display_aspect_ratio
Display Aspect Ratio = x:y (4:3, 16:9, etc)