/* * Nvidia CUVID decoder * Copyright (c) 2016 Timo Rothenpieler <timo@rothenpieler.org> * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "config_components.h" #include "compat/cuda/dynlink_loader.h" #include "libavutil/buffer.h"#include "libavutil/mathematics.h"#include "libavutil/hwcontext.h"#include "libavutil/hwcontext_cuda_internal.h"#include "libavutil/cuda_check.h"#include "libavutil/fifo.h"#include "libavutil/log.h"#include "libavutil/mem.h"#include "libavutil/opt.h"#include "libavutil/pixdesc.h" #include "avcodec.h"#include "bsf.h"#include "codec_internal.h"#include "decode.h"#include "hwconfig.h"#include "nvdec.h"#include "internal.h" #if !NVDECAPI_CHECK_VERSION(9, 0)#define cudaVideoSurfaceFormat_YUV444 2#define cudaVideoSurfaceFormat_YUV444_16Bit 3#endif #if NVDECAPI_CHECK_VERSION(11, 0)#define CUVID_HAS_AV1_SUPPORT#endif typedef struct CuvidContext{ AVClass *avclass; CUvideodecoder cudecoder; CUvideoparser cuparser; /* This packet coincides with AVCodecInternal.in_pkt * and is not owned by us. */ AVPacket *pkt; char *cu_gpu; int nb_surfaces; int drop_second_field; char *crop_expr; char *resize_expr; struct { int left; int top; int right; int bottom; } crop; struct { int width; int height; } resize; AVBufferRef *hwdevice; AVBufferRef *hwframe; AVFifo *frame_queue; int deint_mode; int deint_mode_current; int64_t prev_pts; int progressive_sequence; int internal_error; int decoder_flushing; int *key_frame; cudaVideoCodec codec_type; cudaVideoChromaFormat chroma_format; CUVIDDECODECAPS caps8, caps10, caps12; CUVIDPARSERPARAMS cuparseinfo; CUVIDEOFORMATEX *cuparse_ext; CudaFunctions *cudl; CuvidFunctions *cvdl;} CuvidContext; typedef struct CuvidParsedFrame{ CUVIDPARSERDISPINFO dispinfo; int second_field; int is_deinterlacing;} CuvidParsedFrame; #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x) // NV recommends [2;4] range#define CUVID_MAX_DISPLAY_DELAY (4) // Actual pool size will be determined by parser.#define CUVID_DEFAULT_NUM_SURFACES (CUVID_MAX_DISPLAY_DELAY + 1) static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* format){ AVCodecContext *avctx = opaque; CuvidContext *ctx = avctx->priv_data; AVHWFramesContext *hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data; CUVIDDECODECAPS *caps = NULL; CUVIDDECODECREATEINFO cuinfo; int surface_fmt; int chroma_444; int fifo_size_inc; int old_width = avctx->width; int old_height = avctx->height; enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE, // Will be updated below AV_PIX_FMT_NONE }; av_log(avctx, AV_LOG_TRACE, "pfnSequenceCallback, progressive_sequence=%d\n", format->progressive_sequence); memset(&cuinfo, 0, sizeof(cuinfo)); ctx->internal_error = 0; avctx->coded_width = cuinfo.ulWidth = format->coded_width; avctx->coded_height = cuinfo.ulHeight = format->coded_height; // apply cropping cuinfo.display_area.left = format->display_area.left + ctx->crop.left; cuinfo.display_area.top = format->display_area.top + ctx->crop.top; cuinfo.display_area.right = format->display_area.right - ctx->crop.right; cuinfo.display_area.bottom = format->display_area.bottom - ctx->crop.bottom; // width and height need to be set before calling ff_get_format if (ctx->resize_expr) { avctx->width = ctx->resize.width; avctx->height = ctx->resize.height; } else { avctx->width = cuinfo.display_area.right - cuinfo.display_area.left; avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top; } // target width/height need to be multiples of two cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1; cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1; // aspect ratio conversion, 1:1, depends on scaled resolution cuinfo.target_rect.left = 0; cuinfo.target_rect.top = 0; cuinfo.target_rect.right = cuinfo.ulTargetWidth; cuinfo.target_rect.bottom = cuinfo.ulTargetHeight; chroma_444 = format->chroma_format == cudaVideoChromaFormat_444; switch (format->bit_depth_luma_minus8) { case 0: // 8-bit if (chroma_444) { pix_fmts[1] = AV_PIX_FMT_YUV444P;#ifdef NVDEC_HAVE_422_SUPPORT } else if (format->chroma_format == cudaVideoChromaFormat_422) { pix_fmts[1] = AV_PIX_FMT_NV16;#endif } else { pix_fmts[1] = AV_PIX_FMT_NV12; } caps = &ctx->caps8; break; case 2: // 10-bit if (chroma_444) { pix_fmts[1] = AV_PIX_FMT_YUV444P16;#ifdef NVDEC_HAVE_422_SUPPORT } else if (format->chroma_format == cudaVideoChromaFormat_422) { pix_fmts[1] = AV_PIX_FMT_P216LE;#endif } else { pix_fmts[1] = AV_PIX_FMT_P016; } caps = &ctx->caps10; break; case 4: // 12-bit if (chroma_444) { pix_fmts[1] = AV_PIX_FMT_YUV444P16;#ifdef NVDEC_HAVE_422_SUPPORT } else if (format->chroma_format == cudaVideoChromaFormat_422) { pix_fmts[1] = AV_PIX_FMT_P216LE;#endif } else { pix_fmts[1] = AV_PIX_FMT_P016; } caps = &ctx->caps12; break; default: break; } if (!caps || !caps->bIsSupported) { av_log(avctx, AV_LOG_ERROR, "unsupported bit depth: %d\n", format->bit_depth_luma_minus8 + 8); ctx->internal_error = AVERROR(EINVAL); return 0; } surface_fmt = ff_get_format(avctx, pix_fmts); if (surface_fmt < 0) { av_log(avctx, AV_LOG_ERROR, "ff_get_format failed: %d\n", surface_fmt); ctx->internal_error = AVERROR(EINVAL); return 0; } av_log(avctx, AV_LOG_VERBOSE, "Formats: Original: %s | HW: %s | SW: %s\n", av_get_pix_fmt_name(avctx->pix_fmt), av_get_pix_fmt_name(surface_fmt), av_get_pix_fmt_name(avctx->sw_pix_fmt)); avctx->pix_fmt = surface_fmt; // Update our hwframe ctx, as the get_format callback might have refreshed it! if (avctx->hw_frames_ctx) { av_buffer_unref(&ctx->hwframe); ctx->hwframe = av_buffer_ref(avctx->hw_frames_ctx); if (!ctx->hwframe) { ctx->internal_error = AVERROR(ENOMEM); return 0; } hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data; } ff_set_sar(avctx, av_div_q( (AVRational){ format->display_aspect_ratio.x, format->display_aspect_ratio.y }, (AVRational){ avctx->width, avctx->height })); ctx->deint_mode_current = format->progressive_sequence ? cudaVideoDeinterlaceMode_Weave : ctx->deint_mode; ctx->progressive_sequence = format->progressive_sequence; if (!format->progressive_sequence && ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) avctx->flags |= AV_CODEC_FLAG_INTERLACED_DCT; else avctx->flags &= ~AV_CODEC_FLAG_INTERLACED_DCT; if (format->video_signal_description.video_full_range_flag) avctx->color_range = AVCOL_RANGE_JPEG; else avctx->color_range = AVCOL_RANGE_MPEG; avctx->color_primaries = format->video_signal_description.color_primaries; avctx->color_trc = format->video_signal_description.transfer_characteristics; avctx->colorspace = format->video_signal_description.matrix_coefficients; if (format->bitrate) avctx->bit_rate = format->bitrate; if (format->frame_rate.numerator && format->frame_rate.denominator) { avctx->framerate.num = format->frame_rate.numerator; avctx->framerate.den = format->frame_rate.denominator; } if (ctx->cudecoder && avctx->coded_width == format->coded_width && avctx->coded_height == format->coded_height && avctx->width == old_width && avctx->height == old_height && ctx->chroma_format == format->chroma_format && ctx->codec_type == format->codec) return 1; if (ctx->cudecoder) { av_log(avctx, AV_LOG_TRACE, "Re-initializing decoder\n"); ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder)); if (ctx->internal_error < 0) return 0; ctx->cudecoder = NULL; } if (hwframe_ctx->pool && ( hwframe_ctx->width < avctx->width || hwframe_ctx->height < avctx->height || hwframe_ctx->format != AV_PIX_FMT_CUDA || hwframe_ctx->sw_format != avctx->sw_pix_fmt)) { av_log(avctx, AV_LOG_ERROR, "AVHWFramesContext is already initialized with incompatible parameters\n"); av_log(avctx, AV_LOG_DEBUG, "width: %d <-> %d\n", hwframe_ctx->width, avctx->width); av_log(avctx, AV_LOG_DEBUG, "height: %d <-> %d\n", hwframe_ctx->height, avctx->height); av_log(avctx, AV_LOG_DEBUG, "format: %s <-> cuda\n", av_get_pix_fmt_name(hwframe_ctx->format)); av_log(avctx, AV_LOG_DEBUG, "sw_format: %s <-> %s\n", av_get_pix_fmt_name(hwframe_ctx->sw_format), av_get_pix_fmt_name(avctx->sw_pix_fmt)); ctx->internal_error = AVERROR(EINVAL); return 0; } ctx->chroma_format = format->chroma_format; cuinfo.CodecType = ctx->codec_type = format->codec; cuinfo.ChromaFormat = format->chroma_format; switch (avctx->sw_pix_fmt) { case AV_PIX_FMT_NV12: cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12; break; case AV_PIX_FMT_P010: case AV_PIX_FMT_P016: cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016; break;#ifdef NVDEC_HAVE_422_SUPPORT case AV_PIX_FMT_NV16: cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV16; break; case AV_PIX_FMT_P216: cuinfo.OutputFormat = cudaVideoSurfaceFormat_P216; break;#endif case AV_PIX_FMT_YUV444P: cuinfo.OutputFormat = cudaVideoSurfaceFormat_YUV444; break; case AV_PIX_FMT_YUV444P16: cuinfo.OutputFormat = cudaVideoSurfaceFormat_YUV444_16Bit; break; default: av_log(avctx, AV_LOG_ERROR, "Unsupported output format: %s\n", av_get_pix_fmt_name(avctx->sw_pix_fmt)); ctx->internal_error = AVERROR(EINVAL); return 0; } fifo_size_inc = ctx->nb_surfaces; ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, format->min_num_decode_surfaces + 3); if (avctx->extra_hw_frames > 0) ctx->nb_surfaces += avctx->extra_hw_frames; fifo_size_inc = ctx->nb_surfaces - fifo_size_inc; if (fifo_size_inc > 0 && av_fifo_grow2(ctx->frame_queue, fifo_size_inc) < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to grow frame queue on video sequence callback\n"); ctx->internal_error = AVERROR(ENOMEM); return 0; } if (fifo_size_inc > 0 && av_reallocp_array(&ctx->key_frame, ctx->nb_surfaces, sizeof(int)) < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to grow key frame array on video sequence callback\n"); ctx->internal_error = AVERROR(ENOMEM); return 0; } cuinfo.ulNumDecodeSurfaces = ctx->nb_surfaces; cuinfo.ulNumOutputSurfaces = 1; cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID; cuinfo.bitDepthMinus8 = format->bit_depth_luma_minus8; cuinfo.DeinterlaceMode = ctx->deint_mode_current; if (ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field) avctx->framerate = av_mul_q(avctx->framerate, (AVRational){2, 1}); ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidCreateDecoder(&ctx->cudecoder, &cuinfo)); if (ctx->internal_error < 0) return 0; if (!hwframe_ctx->pool) { hwframe_ctx->format = AV_PIX_FMT_CUDA; hwframe_ctx->sw_format = avctx->sw_pix_fmt; hwframe_ctx->width = avctx->width; hwframe_ctx->height = avctx->height; if ((ctx->internal_error = av_hwframe_ctx_init(ctx->hwframe)) < 0) { av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_init failed\n"); return 0; } } if(ctx->cuparseinfo.ulMaxNumDecodeSurfaces != cuinfo.ulNumDecodeSurfaces) { ctx->cuparseinfo.ulMaxNumDecodeSurfaces = cuinfo.ulNumDecodeSurfaces; return cuinfo.ulNumDecodeSurfaces; } return 1;} static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS* picparams){ AVCodecContext *avctx = opaque; CuvidContext *ctx = avctx->priv_data; av_log(avctx, AV_LOG_TRACE, "pfnDecodePicture\n"); if(picparams->intra_pic_flag) ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag; ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidDecodePicture(ctx->cudecoder, picparams)); if (ctx->internal_error < 0) return 0; return 1;} static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO* dispinfo){ AVCodecContext *avctx = opaque; CuvidContext *ctx = avctx->priv_data; CuvidParsedFrame parsed_frame = { { 0 } }; parsed_frame.dispinfo = *dispinfo; ctx->internal_error = 0; // For some reason, dispinfo->progressive_frame is sometimes wrong. parsed_frame.dispinfo.progressive_frame = ctx->progressive_sequence; if (ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) { av_fifo_write(ctx->frame_queue, &parsed_frame, 1); } else { parsed_frame.is_deinterlacing = 1; av_fifo_write(ctx->frame_queue, &parsed_frame, 1); if (!ctx->drop_second_field) { parsed_frame.second_field = 1; av_fifo_write(ctx->frame_queue, &parsed_frame, 1); } } return 1;} static int cuvid_is_buffer_full(AVCodecContext *avctx){ CuvidContext *ctx = avctx->priv_data; int delay = ctx->cuparseinfo.ulMaxDisplayDelay; if (ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field) delay *= 2; return av_fifo_can_read(ctx->frame_queue) + delay >= ctx->nb_surfaces;} static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt){ CuvidContext *ctx = avctx->priv_data; AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data; AVCUDADeviceContext *device_hwctx = device_ctx->hwctx; CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx; CUVIDSOURCEDATAPACKET cupkt; int ret = 0, eret = 0, is_flush = ctx->decoder_flushing; av_log(avctx, AV_LOG_TRACE, "cuvid_decode_packet\n"); if (is_flush && avpkt && avpkt->size) return AVERROR_EOF; if (cuvid_is_buffer_full(avctx) && avpkt && avpkt->size) return AVERROR(EAGAIN); ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx)); if (ret < 0) { return ret; } memset(&cupkt, 0, sizeof(cupkt)); if (avpkt && avpkt->size) { cupkt.payload_size = avpkt->size; cupkt.payload = avpkt->data; if (avpkt->pts != AV_NOPTS_VALUE) { cupkt.flags = CUVID_PKT_TIMESTAMP; if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) cupkt.timestamp = av_rescale_q(avpkt->pts, avctx->pkt_timebase, (AVRational){1, 10000000}); else cupkt.timestamp = avpkt->pts; } } else { cupkt.flags = CUVID_PKT_ENDOFSTREAM; ctx->decoder_flushing = 1; } ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &cupkt)); if (ret < 0)Lines 1–500 of 1299. Load more
/* * Nvidia CUVID decoder * Copyright (c) 2016 Timo Rothenpieler <timo@rothenpieler.org> * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "config_components.h" #include "compat/cuda/dynlink_loader.h" #include "libavutil/buffer.h" #include "libavutil/mathematics.h" #include "libavutil/hwcontext.h" #include "libavutil/hwcontext_cuda_internal.h" #include "libavutil/cuda_check.h" #include "libavutil/fifo.h" #include "libavutil/log.h" #include "libavutil/mem.h" #include "libavutil/opt.h" #include "libavutil/pixdesc.h" #include "avcodec.h" #include "bsf.h" #include "codec_internal.h" #include "decode.h" #include "hwconfig.h" #include "nvdec.h" #include "internal.h" #if !NVDECAPI_CHECK_VERSION(9, 0) #define cudaVideoSurfaceFormat_YUV444 2 #define cudaVideoSurfaceFormat_YUV444_16Bit 3 #endif #if NVDECAPI_CHECK_VERSION(11, 0) #define CUVID_HAS_AV1_SUPPORT #endif typedef struct CuvidContext { AVClass *avclass; CUvideodecoder cudecoder; CUvideoparser cuparser; /* This packet coincides with AVCodecInternal.in_pkt * and is not owned by us. */ AVPacket *pkt; char *cu_gpu; int nb_surfaces; int drop_second_field; char *crop_expr; char *resize_expr; struct { int left; int top; int right; int bottom; } crop; struct { int width; int height; } resize; AVBufferRef *hwdevice; AVBufferRef *hwframe; AVFifo *frame_queue; int deint_mode; int deint_mode_current; int64_t prev_pts; int progressive_sequence; int internal_error; int decoder_flushing; int *key_frame; cudaVideoCodec codec_type; cudaVideoChromaFormat chroma_format; CUVIDDECODECAPS caps8, caps10, caps12; CUVIDPARSERPARAMS cuparseinfo; CUVIDEOFORMATEX *cuparse_ext; CudaFunctions *cudl; CuvidFunctions *cvdl; } CuvidContext; typedef struct CuvidParsedFrame { CUVIDPARSERDISPINFO dispinfo; int second_field; int is_deinterlacing; } CuvidParsedFrame; #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, ctx->cudl, x) // NV recommends [2;4] range #define CUVID_MAX_DISPLAY_DELAY (4) // Actual pool size will be determined by parser. #define CUVID_DEFAULT_NUM_SURFACES (CUVID_MAX_DISPLAY_DELAY + 1) static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* format) { AVCodecContext *avctx = opaque; CuvidContext *ctx = avctx->priv_data; AVHWFramesContext *hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data; CUVIDDECODECAPS *caps = NULL; CUVIDDECODECREATEINFO cuinfo; int surface_fmt; int chroma_444; int fifo_size_inc; int old_width = avctx->width; int old_height = avctx->height; enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE, // Will be updated below AV_PIX_FMT_NONE }; av_log(avctx, AV_LOG_TRACE, "pfnSequenceCallback, progressive_sequence=%d\n", format->progressive_sequence); memset(&cuinfo, 0, sizeof(cuinfo)); ctx->internal_error = 0; avctx->coded_width = cuinfo.ulWidth = format->coded_width; avctx->coded_height = cuinfo.ulHeight = format->coded_height; // apply cropping cuinfo.display_area.left = format->display_area.left + ctx->crop.left; cuinfo.display_area.top = format->display_area.top + ctx->crop.top; cuinfo.display_area.right = format->display_area.right - ctx->crop.right; cuinfo.display_area.bottom = format->display_area.bottom - ctx->crop.bottom; // width and height need to be set before calling ff_get_format if (ctx->resize_expr) { avctx->width = ctx->resize.width; avctx->height = ctx->resize.height; } else { avctx->width = cuinfo.display_area.right - cuinfo.display_area.left; avctx->height = cuinfo.display_area.bottom - cuinfo.display_area.top; } // target width/height need to be multiples of two cuinfo.ulTargetWidth = avctx->width = (avctx->width + 1) & ~1; cuinfo.ulTargetHeight = avctx->height = (avctx->height + 1) & ~1; // aspect ratio conversion, 1:1, depends on scaled resolution cuinfo.target_rect.left = 0; cuinfo.target_rect.top = 0; cuinfo.target_rect.right = cuinfo.ulTargetWidth; cuinfo.target_rect.bottom = cuinfo.ulTargetHeight; chroma_444 = format->chroma_format == cudaVideoChromaFormat_444; switch (format->bit_depth_luma_minus8) { case 0: // 8-bit if (chroma_444) { pix_fmts[1] = AV_PIX_FMT_YUV444P; #ifdef NVDEC_HAVE_422_SUPPORT } else if (format->chroma_format == cudaVideoChromaFormat_422) { pix_fmts[1] = AV_PIX_FMT_NV16; #endif } else { pix_fmts[1] = AV_PIX_FMT_NV12; } caps = &ctx->caps8; break; case 2: // 10-bit if (chroma_444) { pix_fmts[1] = AV_PIX_FMT_YUV444P16; #ifdef NVDEC_HAVE_422_SUPPORT } else if (format->chroma_format == cudaVideoChromaFormat_422) { pix_fmts[1] = AV_PIX_FMT_P216LE; #endif } else { pix_fmts[1] = AV_PIX_FMT_P016; } caps = &ctx->caps10; break; case 4: // 12-bit if (chroma_444) { pix_fmts[1] = AV_PIX_FMT_YUV444P16; #ifdef NVDEC_HAVE_422_SUPPORT } else if (format->chroma_format == cudaVideoChromaFormat_422) { pix_fmts[1] = AV_PIX_FMT_P216LE; #endif } else { pix_fmts[1] = AV_PIX_FMT_P016; } caps = &ctx->caps12; break; default: break; } if (!caps || !caps->bIsSupported) { av_log(avctx, AV_LOG_ERROR, "unsupported bit depth: %d\n", format->bit_depth_luma_minus8 + 8); ctx->internal_error = AVERROR(EINVAL); return 0; } surface_fmt = ff_get_format(avctx, pix_fmts); if (surface_fmt < 0) { av_log(avctx, AV_LOG_ERROR, "ff_get_format failed: %d\n", surface_fmt); ctx->internal_error = AVERROR(EINVAL); return 0; } av_log(avctx, AV_LOG_VERBOSE, "Formats: Original: %s | HW: %s | SW: %s\n", av_get_pix_fmt_name(avctx->pix_fmt), av_get_pix_fmt_name(surface_fmt), av_get_pix_fmt_name(avctx->sw_pix_fmt)); avctx->pix_fmt = surface_fmt; // Update our hwframe ctx, as the get_format callback might have refreshed it! if (avctx->hw_frames_ctx) { av_buffer_unref(&ctx->hwframe); ctx->hwframe = av_buffer_ref(avctx->hw_frames_ctx); if (!ctx->hwframe) { ctx->internal_error = AVERROR(ENOMEM); return 0; } hwframe_ctx = (AVHWFramesContext*)ctx->hwframe->data; } ff_set_sar(avctx, av_div_q( (AVRational){ format->display_aspect_ratio.x, format->display_aspect_ratio.y }, (AVRational){ avctx->width, avctx->height })); ctx->deint_mode_current = format->progressive_sequence ? cudaVideoDeinterlaceMode_Weave : ctx->deint_mode; ctx->progressive_sequence = format->progressive_sequence; if (!format->progressive_sequence && ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) avctx->flags |= AV_CODEC_FLAG_INTERLACED_DCT; else avctx->flags &= ~AV_CODEC_FLAG_INTERLACED_DCT; if (format->video_signal_description.video_full_range_flag) avctx->color_range = AVCOL_RANGE_JPEG; else avctx->color_range = AVCOL_RANGE_MPEG; avctx->color_primaries = format->video_signal_description.color_primaries; avctx->color_trc = format->video_signal_description.transfer_characteristics; avctx->colorspace = format->video_signal_description.matrix_coefficients; if (format->bitrate) avctx->bit_rate = format->bitrate; if (format->frame_rate.numerator && format->frame_rate.denominator) { avctx->framerate.num = format->frame_rate.numerator; avctx->framerate.den = format->frame_rate.denominator; } if (ctx->cudecoder && avctx->coded_width == format->coded_width && avctx->coded_height == format->coded_height && avctx->width == old_width && avctx->height == old_height && ctx->chroma_format == format->chroma_format && ctx->codec_type == format->codec) return 1; if (ctx->cudecoder) { av_log(avctx, AV_LOG_TRACE, "Re-initializing decoder\n"); ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder)); if (ctx->internal_error < 0) return 0; ctx->cudecoder = NULL; } if (hwframe_ctx->pool && ( hwframe_ctx->width < avctx->width || hwframe_ctx->height < avctx->height || hwframe_ctx->format != AV_PIX_FMT_CUDA || hwframe_ctx->sw_format != avctx->sw_pix_fmt)) { av_log(avctx, AV_LOG_ERROR, "AVHWFramesContext is already initialized with incompatible parameters\n"); av_log(avctx, AV_LOG_DEBUG, "width: %d <-> %d\n", hwframe_ctx->width, avctx->width); av_log(avctx, AV_LOG_DEBUG, "height: %d <-> %d\n", hwframe_ctx->height, avctx->height); av_log(avctx, AV_LOG_DEBUG, "format: %s <-> cuda\n", av_get_pix_fmt_name(hwframe_ctx->format)); av_log(avctx, AV_LOG_DEBUG, "sw_format: %s <-> %s\n", av_get_pix_fmt_name(hwframe_ctx->sw_format), av_get_pix_fmt_name(avctx->sw_pix_fmt)); ctx->internal_error = AVERROR(EINVAL); return 0; } ctx->chroma_format = format->chroma_format; cuinfo.CodecType = ctx->codec_type = format->codec; cuinfo.ChromaFormat = format->chroma_format; switch (avctx->sw_pix_fmt) { case AV_PIX_FMT_NV12: cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV12; break; case AV_PIX_FMT_P010: case AV_PIX_FMT_P016: cuinfo.OutputFormat = cudaVideoSurfaceFormat_P016; break; #ifdef NVDEC_HAVE_422_SUPPORT case AV_PIX_FMT_NV16: cuinfo.OutputFormat = cudaVideoSurfaceFormat_NV16; break; case AV_PIX_FMT_P216: cuinfo.OutputFormat = cudaVideoSurfaceFormat_P216; break; #endif case AV_PIX_FMT_YUV444P: cuinfo.OutputFormat = cudaVideoSurfaceFormat_YUV444; break; case AV_PIX_FMT_YUV444P16: cuinfo.OutputFormat = cudaVideoSurfaceFormat_YUV444_16Bit; break; default: av_log(avctx, AV_LOG_ERROR, "Unsupported output format: %s\n", av_get_pix_fmt_name(avctx->sw_pix_fmt)); ctx->internal_error = AVERROR(EINVAL); return 0; } fifo_size_inc = ctx->nb_surfaces; ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, format->min_num_decode_surfaces + 3); if (avctx->extra_hw_frames > 0) ctx->nb_surfaces += avctx->extra_hw_frames; fifo_size_inc = ctx->nb_surfaces - fifo_size_inc; if (fifo_size_inc > 0 && av_fifo_grow2(ctx->frame_queue, fifo_size_inc) < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to grow frame queue on video sequence callback\n"); ctx->internal_error = AVERROR(ENOMEM); return 0; } if (fifo_size_inc > 0 && av_reallocp_array(&ctx->key_frame, ctx->nb_surfaces, sizeof(int)) < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to grow key frame array on video sequence callback\n"); ctx->internal_error = AVERROR(ENOMEM); return 0; } cuinfo.ulNumDecodeSurfaces = ctx->nb_surfaces; cuinfo.ulNumOutputSurfaces = 1; cuinfo.ulCreationFlags = cudaVideoCreate_PreferCUVID; cuinfo.bitDepthMinus8 = format->bit_depth_luma_minus8; cuinfo.DeinterlaceMode = ctx->deint_mode_current; if (ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field) avctx->framerate = av_mul_q(avctx->framerate, (AVRational){2, 1}); ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidCreateDecoder(&ctx->cudecoder, &cuinfo)); if (ctx->internal_error < 0) return 0; if (!hwframe_ctx->pool) { hwframe_ctx->format = AV_PIX_FMT_CUDA; hwframe_ctx->sw_format = avctx->sw_pix_fmt; hwframe_ctx->width = avctx->width; hwframe_ctx->height = avctx->height; if ((ctx->internal_error = av_hwframe_ctx_init(ctx->hwframe)) < 0) { av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_init failed\n"); return 0; } } if(ctx->cuparseinfo.ulMaxNumDecodeSurfaces != cuinfo.ulNumDecodeSurfaces) { ctx->cuparseinfo.ulMaxNumDecodeSurfaces = cuinfo.ulNumDecodeSurfaces; return cuinfo.ulNumDecodeSurfaces; } return 1; } static int CUDAAPI cuvid_handle_picture_decode(void *opaque, CUVIDPICPARAMS* picparams) { AVCodecContext *avctx = opaque; CuvidContext *ctx = avctx->priv_data; av_log(avctx, AV_LOG_TRACE, "pfnDecodePicture\n"); if(picparams->intra_pic_flag) ctx->key_frame[picparams->CurrPicIdx] = picparams->intra_pic_flag; ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidDecodePicture(ctx->cudecoder, picparams)); if (ctx->internal_error < 0) return 0; return 1; } static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO* dispinfo) { AVCodecContext *avctx = opaque; CuvidContext *ctx = avctx->priv_data; CuvidParsedFrame parsed_frame = { { 0 } }; parsed_frame.dispinfo = *dispinfo; ctx->internal_error = 0; // For some reason, dispinfo->progressive_frame is sometimes wrong. parsed_frame.dispinfo.progressive_frame = ctx->progressive_sequence; if (ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) { av_fifo_write(ctx->frame_queue, &parsed_frame, 1); } else { parsed_frame.is_deinterlacing = 1; av_fifo_write(ctx->frame_queue, &parsed_frame, 1); if (!ctx->drop_second_field) { parsed_frame.second_field = 1; av_fifo_write(ctx->frame_queue, &parsed_frame, 1); } } return 1; } static int cuvid_is_buffer_full(AVCodecContext *avctx) { CuvidContext *ctx = avctx->priv_data; int delay = ctx->cuparseinfo.ulMaxDisplayDelay; if (ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field) delay *= 2; return av_fifo_can_read(ctx->frame_queue) + delay >= ctx->nb_surfaces; } static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt) { CuvidContext *ctx = avctx->priv_data; AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hwdevice->data; AVCUDADeviceContext *device_hwctx = device_ctx->hwctx; CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx; CUVIDSOURCEDATAPACKET cupkt; int ret = 0, eret = 0, is_flush = ctx->decoder_flushing; av_log(avctx, AV_LOG_TRACE, "cuvid_decode_packet\n"); if (is_flush && avpkt && avpkt->size) return AVERROR_EOF; if (cuvid_is_buffer_full(avctx) && avpkt && avpkt->size) return AVERROR(EAGAIN); ret = CHECK_CU(ctx->cudl->cuCtxPushCurrent(cuda_ctx)); if (ret < 0) { return ret; } memset(&cupkt, 0, sizeof(cupkt)); if (avpkt && avpkt->size) { cupkt.payload_size = avpkt->size; cupkt.payload = avpkt->data; if (avpkt->pts != AV_NOPTS_VALUE) { cupkt.flags = CUVID_PKT_TIMESTAMP; if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) cupkt.timestamp = av_rescale_q(avpkt->pts, avctx->pkt_timebase, (AVRational){1, 10000000}); else cupkt.timestamp = avpkt->pts; } } else { cupkt.flags = CUVID_PKT_ENDOFSTREAM; ctx->decoder_flushing = 1; } ret = CHECK_CU(ctx->cvdl->cuvidParseVideoData(ctx->cuparser, &cupkt)); if (ret < 0)