35 #define MAX_FRAME_COUNT 25 
   76     const char *err_string;
 
   83     cuGetErrorName(err, &err_name);
 
   84     cuGetErrorString(err, &err_string);
 
   87     if (err_name && err_string)
 
   94 #define CHECK_CU(x) check_cu(avctx, (x), #x) 
  151     if (hwframe_ctx->
pool && (
 
  156         av_log(avctx, 
AV_LOG_ERROR, 
"AVHWFramesContext is already initialized with incompatible parameters\n");
 
  172     memset(&cuinfo, 0, 
sizeof(cuinfo));
 
  180     cuinfo.ulTargetWidth = cuinfo.ulWidth;
 
  181     cuinfo.ulTargetHeight = cuinfo.ulHeight;
 
  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;
 
  189     cuinfo.ulNumOutputSurfaces = 1;
 
  203     if (
ctx->internal_error < 0)
 
  206     if (!hwframe_ctx->pool) {
 
  209         hwframe_ctx->width = avctx->width;
 
  210         hwframe_ctx->height = avctx->height;
 
  264     int ret = 0, eret = 0, is_flush = 
ctx->decoder_flushing;
 
  268     if (is_flush && avpkt && avpkt->size)
 
  274     if (
ctx->bsf && avpkt && avpkt->size) {
 
  291         avpkt = &filtered_packet;
 
  294     ret = 
CHECK_CU(cuCtxPushCurrent(cuda_ctx));
 
  300     memset(&cupkt, 0, 
sizeof(cupkt));
 
  302     if (avpkt && avpkt->size) {
 
  303         cupkt.payload_size = avpkt->size;
 
  304         cupkt.payload = avpkt->data;
 
  308             if (avctx->pkt_timebase.num && avctx->pkt_timebase.den)
 
  311                 cupkt.timestamp = avpkt->pts;
 
  315         ctx->decoder_flushing = 1;
 
  326     if (
ctx->internal_error) {
 
  328         ret = 
ctx->internal_error;
 
  352     int ret = 0, eret = 0;
 
  362     ret = 
CHECK_CU(cuCtxPushCurrent(cuda_ctx));
 
  369         unsigned int pitch = 0;
 
  375         memset(¶ms, 0, 
sizeof(params));
 
  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,
 
  407                     .Height        = avctx->
height >> (i ? 1 : 0),
 
  467                 frame->
pts += pts_diff;
 
  497     eret = 
CHECK_CU(cuCtxPopCurrent(&dummy));
 
  527     } 
else if (ret < 0) {
 
  563     memset(&cuinfo, 0, 
sizeof(cuinfo));
 
  657     device_hwctx = device_ctx->
hwctx;
 
  662     memset(&seq_pkt, 0, 
sizeof(seq_pkt));
 
  667 #if CONFIG_H263_CUVID_DECODER 
  672 #if CONFIG_H264_CUVID_DECODER 
  677 #if CONFIG_HEVC_CUVID_DECODER 
  682 #if CONFIG_MJPEG_CUVID_DECODER 
  687 #if CONFIG_MPEG1_CUVID_DECODER 
  692 #if CONFIG_MPEG2_CUVID_DECODER 
  697 #if CONFIG_MPEG4_CUVID_DECODER 
  702 #if CONFIG_VP8_CUVID_DECODER 
  707 #if CONFIG_VP9_CUVID_DECODER 
  712 #if CONFIG_VC1_CUVID_DECODER 
  758     ret = 
CHECK_CU(cuCtxPushCurrent(cuda_ctx));
 
  779     ret = 
CHECK_CU(cuCtxPopCurrent(&dummy));
 
  804     ret = 
CHECK_CU(cuCtxPushCurrent(cuda_ctx));
 
  811     if (!
ctx->frame_queue) {
 
  816     if (
ctx->cudecoder) {
 
  830     seq_pkt.payload = 
ctx->cuparse_ext.raw_seqhdr_data;
 
  831     seq_pkt.payload_size = 
ctx->cuparse_ext.format.seqhdr_data_length;
 
  833     if (seq_pkt.payload && seq_pkt.payload_size) {
 
  843     ctx->prev_pts = INT64_MIN;
 
  844     ctx->decoder_flushing = 0;
 
  851 #define OFFSET(x) offsetof(CuvidContext, x) 
  852 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM 
  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, \ 
  867         .version = LIBAVUTIL_VERSION_INT, \ 
  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, \ 
  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, \ 
  894 #if CONFIG_HEVC_CUVID_DECODER 
  898 #if CONFIG_H263_CUVID_DECODER 
  902 #if CONFIG_H264_CUVID_DECODER 
  906 #if CONFIG_MJPEG_CUVID_DECODER 
  910 #if CONFIG_MPEG1_CUVID_DECODER 
  914 #if CONFIG_MPEG2_CUVID_DECODER 
  918 #if CONFIG_MPEG4_CUVID_DECODER 
  922 #if CONFIG_VP8_CUVID_DECODER 
  926 #if CONFIG_VP9_CUVID_DECODER 
  930 #if CONFIG_VC1_CUVID_DECODER 
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e. 
void av_bsf_free(AVBSFContext **ctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
const struct AVCodec * codec
AVCodecParameters * par_out
Parameters of the output stream. 
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another. 
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
This structure describes decoded (raw) audio or video data. 
ptrdiff_t const GLvoid * data
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT. 
int coded_width
Bitstream width / height, may be different from width/height e.g. 
#define AV_LOG_WARNING
Something somehow does not look correct. 
int64_t bit_rate
the average bitrate 
enum AVColorRange color_range
MPEG vs JPEG YUV range. 
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. 
The bitstream filter state. 
static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT *format)
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
int width
The allocated dimensions of the frames in this pool. 
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx. 
void av_frame_set_pkt_duration(AVFrame *frame, int64_t val)
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type. 
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)
CUVIDPARSERPARAMS cuparseinfo
Use dedicated video engines directly. 
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. 
CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj)
static int check_cu(AVCodecContext *avctx, CUresult err, const char *func)
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. 
cudaVideoDeinterlaceMode DeinterlaceMode
cudaVideoDeinterlaceMode_XXX 
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Set when this is the last packet for this stream. 
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter. 
static void cuvid_flush(AVCodecContext *avctx)
cudaVideoCodec CodecType
cudaVideoCodec_XXX 
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet. 
cudaVideoSurfaceFormat OutputFormat
cudaVideoSurfaceFormat_XXX 
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values. 
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development. 
static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data. 
unsigned long ulCreationFlags
Decoder creation flags (cudaVideoCreateFlags_XXX) 
static av_cold int cuvid_decode_init(AVCodecContext *avctx)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user). 
AVFifoBuffer * frame_queue
uint8_t * extradata
some codecs need / can use extradata like Huffman tables. 
CUresult CUDAAPI cuvidDecodePicture(CUvideodecoder hDecoder, CUVIDPICPARAMS *pPicParams)
Decode a single picture (field or frame) 
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context. 
CUVIDEOFORMATEX * pExtVideoInfo
[Optional] sequence header data from system layer 
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. 
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)
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context. 
#define AVERROR_EOF
End of file. 
int interlaced_frame
The content of the picture is interlaced. 
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet. 
An API-specific header for AV_HWDEVICE_TYPE_CUDA. 
Weave both fields (no deinterlacing) 
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers. 
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are. 
cudaVideoChromaFormat chroma_format
int width
width and height of the video frame 
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered. 
cudaVideoCodec codec_type
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g. 
unsigned long ulNumDecodeSurfaces
Maximum number of internal decode surfaces. 
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. 
unsigned long ulNumOutputSurfaces
Maximum number of output surfaces simultaneously mapped. 
int flags
AV_CODEC_FLAG_*. 
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use. 
static const uint8_t offset[127][2]
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext. 
unsigned long payload_size
number of bytes in the payload (may be zero if EOS flag is set) 
int extradata_size
Size of the extradata content in bytes. 
static int cuvid_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface. 
int progressive_frame
Input is progressive (deinterlace_mode will be ignored) 
unsigned long ulTargetHeight
Post-processed Output Height (Should be aligbed to 2) 
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext. 
int second_field
Output the second field (ignored if deinterlace mode is Weave) 
int width
picture width / height. 
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
int top_field_first
Input frame is top field first (1st field is top, 2nd field is bottom) 
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering. 
void av_frame_set_pkt_pos(AVFrame *frame, int64_t val)
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries. 
#define AVERROR_BSF_NOT_FOUND
Bitstream filter not found. 
static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO *dispinfo)
CUresult CUDAAPI cuvidCreateVideoParser(CUvideoparser *pObj, CUVIDPARSERPARAMS *pParams)
HW acceleration through CUDA. 
preferred ID for MPEG-1/2 video decoding 
the normal 2^n-1 "JPEG" YUV ranges 
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format. 
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
CUresult CUDAAPI cuvidDestroyDecoder(CUvideodecoder hDecoder)
Destroy the decoder object. 
static int cuvid_test_dummy_decoder(AVCodecContext *avctx, CUVIDPARSERPARAMS *cuparseinfo)
Libavcodec external API header. 
PFNVIDSEQUENCECALLBACK pfnSequenceCallback
Called before decoding frames and/or whenever there is a format 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 ...
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line. 
main external API structure. 
void av_packet_unref(AVPacket *pkt)
Wipe the packet. 
uint8_t * data
The data buffer. 
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame. 
a very simple circular buffer FIFO implementation 
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2. 
This struct is allocated as AVHWDeviceContext.hwctx. 
static const char * format
Describe the class of an AVClass context structure. 
const unsigned char * payload
Pointer to packet payload data (may be NULL if EOS flag is set) 
enum AVColorSpace colorspace
YUV colorspace type. 
Rational number (pair of numerator and denominator). 
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic. 
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context. 
This struct describes a set or pool of "hardware" frames (i.e. 
CUvideotimestamp timestamp
static const AVOption options[]
int(* func)(AVBPrint *dst, const char *in, const char *arg)
refcounted data buffer API 
static enum AVPixelFormat pix_fmts[]
cudaVideoCodec CodecType
cudaVideoCodec_XXX 
CUVIDPARSERDISPINFO dispinfo
static int cuvid_output_frame(AVCodecContext *avctx, AVFrame *frame)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes. 
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext. 
attribute_deprecated int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame. 
the normal 219*2^(n-8) "MPEG" YUV ranges 
A reference to a data buffer. 
NvCuvid API provides Video Decoding interface to NVIDIA GPU devices. 
PFNVIDDECODECALLBACK pfnDecodePicture
Called when a picture is ready to be decoded (decode order) 
void * pUserData
User data for callbacks. 
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header. 
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context. 
NV12 (currently the only supported output format) 
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer. 
static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS *picparams)
unsigned long bitDepthMinus8
unsigned int ulMaxNumDecodeSurfaces
Max # of decode surfaces (parser will cycle through these) 
unsigned long ulHeight
Coded Sequence Height. 
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer(). 
unsigned long ulWidth
Coded Sequence Width. 
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer. 
#define FF_ENABLE_DEPRECATION_WARNINGS
int top_field_first
If the content is interlaced, is top field displayed first. 
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) 
unsigned long ulTargetWidth
Post-processed Output Width (Should be aligned to 2) 
PFNVIDDISPLAYCALLBACK pfnDisplayPicture
Called whenever a picture is ready to be displayed (display order) 
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent. 
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals. 
unsigned int ulMaxDisplayDelay
Max display queue delay (improves pipelining of decode with display) - 0=no delay (recommended values...
CUVIDEOFORMATEX cuparse_ext
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL. 
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames. 
#define AVERROR_EXTERNAL
Generic error in an external library. 
AVPixelFormat
Pixel format. 
This structure stores compressed data. 
AVCodecParameters * par_in
Parameters of the input stream. 
CUresult CUDAAPI cuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET *pPacket)
#define AV_NOPTS_VALUE
Undefined timestamp value.