X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fnvdec.c;h=c7d5379770320bb9b1ce32570e280562bd780432;hb=b429c86d84cfc964c84bc68ec56799802de2b25a;hp=20d7c3db275cfa6ec716de2c3ce84fce106c5a0e;hpb=c7812b3bcffaa3fddd141cfa59a41af589766b01;p=ffmpeg diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c index 20d7c3db275..c7d53797703 100644 --- a/libavcodec/nvdec.c +++ b/libavcodec/nvdec.c @@ -26,6 +26,7 @@ #include "libavutil/error.h" #include "libavutil/hwcontext.h" #include "libavutil/hwcontext_cuda_internal.h" +#include "libavutil/cuda_check.h" #include "libavutil/pixdesc.h" #include "libavutil/pixfmt.h" @@ -39,6 +40,7 @@ typedef struct NVDECDecoder { AVBufferRef *hw_device_ref; CUcontext cuda_ctx; + CUstream stream; CudaFunctions *cudl; CuvidFunctions *cvdl; @@ -49,14 +51,21 @@ typedef struct NVDECFramePool { unsigned int nb_allocated; } NVDECFramePool; +#define CHECK_CU(x) FF_CUDA_CHECK_DL(logctx, decoder->cudl, x) + static int map_avcodec_id(enum AVCodecID id) { switch (id) { - case AV_CODEC_ID_H264: return cudaVideoCodec_H264; - case AV_CODEC_ID_HEVC: return cudaVideoCodec_HEVC; - case AV_CODEC_ID_VC1: return cudaVideoCodec_VC1; - case AV_CODEC_ID_VP9: return cudaVideoCodec_VP9; - case AV_CODEC_ID_WMV3: return cudaVideoCodec_VC1; + case AV_CODEC_ID_H264: return cudaVideoCodec_H264; + case AV_CODEC_ID_HEVC: return cudaVideoCodec_HEVC; + case AV_CODEC_ID_MJPEG: return cudaVideoCodec_JPEG; + case AV_CODEC_ID_MPEG1VIDEO: return cudaVideoCodec_MPEG1; + case AV_CODEC_ID_MPEG2VIDEO: return cudaVideoCodec_MPEG2; + case AV_CODEC_ID_MPEG4: return cudaVideoCodec_MPEG4; + case AV_CODEC_ID_VC1: return cudaVideoCodec_VC1; + case AV_CODEC_ID_VP8: return cudaVideoCodec_VP8; + case AV_CODEC_ID_VP9: return cudaVideoCodec_VP9; + case AV_CODEC_ID_WMV3: return cudaVideoCodec_VC1; } return -1; } @@ -80,19 +89,29 @@ static int map_chroma_format(enum AVPixelFormat pix_fmt) static int nvdec_test_capabilities(NVDECDecoder *decoder, CUVIDDECODECREATEINFO *params, void *logctx) { - CUresult err; + int ret; CUVIDDECODECAPS caps = { 0 }; caps.eCodecType = params->CodecType; caps.eChromaFormat = params->ChromaFormat; caps.nBitDepthMinus8 = params->bitDepthMinus8; - err = decoder->cvdl->cuvidGetDecoderCaps(&caps); - if (err != CUDA_SUCCESS) { - av_log(logctx, AV_LOG_ERROR, "Failed querying decoder capabilities\n"); - return AVERROR_UNKNOWN; + if (!decoder->cvdl->cuvidGetDecoderCaps) { + av_log(logctx, AV_LOG_WARNING, "Used Nvidia driver is too old to perform a capability check.\n"); + av_log(logctx, AV_LOG_WARNING, "The minimum required version is " +#if defined(_WIN32) || defined(__CYGWIN__) + "378.66" +#else + "378.13" +#endif + ". Continuing blind.\n"); + return 0; } + ret = CHECK_CU(decoder->cvdl->cuvidGetDecoderCaps(&caps)); + if (ret < 0) + return ret; + av_log(logctx, AV_LOG_VERBOSE, "NVDEC capabilities:\n"); av_log(logctx, AV_LOG_VERBOSE, "format supported: %s, max_mb_count: %d\n", caps.bIsSupported ? "yes" : "no", caps.nMaxMBCount); @@ -131,8 +150,13 @@ static void nvdec_decoder_free(void *opaque, uint8_t *data) { NVDECDecoder *decoder = (NVDECDecoder*)data; - if (decoder->decoder) - decoder->cvdl->cuvidDestroyDecoder(decoder->decoder); + if (decoder->decoder) { + void *logctx = decoder->hw_device_ref->data; + CUcontext dummy; + CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx)); + CHECK_CU(decoder->cvdl->cuvidDestroyDecoder(decoder->decoder)); + CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy)); + } av_buffer_unref(&decoder->hw_device_ref); @@ -151,7 +175,6 @@ static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref, NVDECDecoder *decoder; CUcontext dummy; - CUresult err; int ret; decoder = av_mallocz(sizeof(*decoder)); @@ -172,32 +195,29 @@ static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref, } decoder->cuda_ctx = device_hwctx->cuda_ctx; decoder->cudl = device_hwctx->internal->cuda_dl; + decoder->stream = device_hwctx->stream; - ret = cuvid_load_functions(&decoder->cvdl); + ret = cuvid_load_functions(&decoder->cvdl, logctx); if (ret < 0) { av_log(logctx, AV_LOG_ERROR, "Failed loading nvcuvid.\n"); goto fail; } - err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx); - if (err != CUDA_SUCCESS) { - ret = AVERROR_UNKNOWN; + ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx)); + if (ret < 0) goto fail; - } ret = nvdec_test_capabilities(decoder, params, logctx); if (ret < 0) { - decoder->cudl->cuCtxPopCurrent(&dummy); + CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy)); goto fail; } - err = decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params); + ret = CHECK_CU(decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params)); - decoder->cudl->cuCtxPopCurrent(&dummy); + CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy)); - if (err != CUDA_SUCCESS) { - av_log(logctx, AV_LOG_ERROR, "Error creating a NVDEC decoder: %d\n", err); - ret = AVERROR_UNKNOWN; + if (ret < 0) { goto fail; } @@ -291,7 +311,7 @@ int ff_nvdec_decode_init(AVCodecContext *avctx) params.CodecType = cuvid_codec_type; params.ChromaFormat = cuvid_chroma_format; params.ulNumDecodeSurfaces = frames_ctx->initial_pool_size; - params.ulNumOutputSurfaces = 1; + params.ulNumOutputSurfaces = frames_ctx->initial_pool_size; ret = nvdec_decoder_create(&ctx->decoder_ref, frames_ctx->device_ref, ¶ms, avctx); if (ret < 0) { @@ -337,15 +357,38 @@ static void nvdec_fdd_priv_free(void *priv) av_freep(&priv); } +static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data) +{ + NVDECFrame *unmap_data = (NVDECFrame*)data; + NVDECDecoder *decoder = (NVDECDecoder*)unmap_data->decoder_ref->data; + void *logctx = decoder->hw_device_ref->data; + CUdeviceptr devptr = (CUdeviceptr)opaque; + int ret; + CUcontext dummy; + + ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx)); + if (ret < 0) + goto finish; + + CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr)); + + CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy)); + +finish: + av_buffer_unref(&unmap_data->idx_ref); + av_buffer_unref(&unmap_data->decoder_ref); + av_free(unmap_data); +} + static int nvdec_retrieve_data(void *logctx, AVFrame *frame) { FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data; NVDECFrame *cf = (NVDECFrame*)fdd->hwaccel_priv; NVDECDecoder *decoder = (NVDECDecoder*)cf->decoder_ref->data; - CUVIDPROCPARAMS vpp = { .progressive_frame = 1 }; + CUVIDPROCPARAMS vpp = { 0 }; + NVDECFrame *unmap_data = NULL; - CUresult err; CUcontext dummy; CUdeviceptr devptr; @@ -353,48 +396,55 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame) unsigned int offset = 0; int ret = 0; - err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx); - if (err != CUDA_SUCCESS) - return AVERROR_UNKNOWN; + vpp.progressive_frame = 1; + vpp.output_stream = decoder->stream; - err = decoder->cvdl->cuvidMapVideoFrame(decoder->decoder, cf->idx, &devptr, - &pitch, &vpp); - if (err != CUDA_SUCCESS) { - av_log(logctx, AV_LOG_ERROR, "Error mapping a picture with CUVID: %d\n", - err); - ret = AVERROR_UNKNOWN; + ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx)); + if (ret < 0) + return ret; + + ret = CHECK_CU(decoder->cvdl->cuvidMapVideoFrame(decoder->decoder, + cf->idx, &devptr, + &pitch, &vpp)); + if (ret < 0) goto finish; + + unmap_data = av_mallocz(sizeof(*unmap_data)); + if (!unmap_data) { + ret = AVERROR(ENOMEM); + goto copy_fail; } - for (i = 0; frame->data[i]; i++) { - CUDA_MEMCPY2D cpy = { - .srcMemoryType = CU_MEMORYTYPE_DEVICE, - .dstMemoryType = CU_MEMORYTYPE_DEVICE, - .srcDevice = devptr, - .dstDevice = (CUdeviceptr)frame->data[i], - .srcPitch = pitch, - .dstPitch = frame->linesize[i], - .srcY = offset, - .WidthInBytes = FFMIN(pitch, frame->linesize[i]), - .Height = frame->height >> (i ? 1 : 0), - }; - - err = decoder->cudl->cuMemcpy2D(&cpy); - if (err != CUDA_SUCCESS) { - av_log(logctx, AV_LOG_ERROR, "Error copying decoded frame: %d\n", - err); - ret = AVERROR_UNKNOWN; - goto copy_fail; - } + frame->buf[1] = av_buffer_create((uint8_t *)unmap_data, sizeof(*unmap_data), + nvdec_unmap_mapped_frame, (void*)devptr, + AV_BUFFER_FLAG_READONLY); + if (!frame->buf[1]) { + ret = AVERROR(ENOMEM); + goto copy_fail; + } + + unmap_data->idx = cf->idx; + unmap_data->idx_ref = av_buffer_ref(cf->idx_ref); + unmap_data->decoder_ref = av_buffer_ref(cf->decoder_ref); - offset += cpy.Height; + for (i = 0; frame->linesize[i]; i++) { + frame->data[i] = (uint8_t*)(devptr + offset); + frame->linesize[i] = pitch; + offset += pitch * (frame->height >> (i ? 1 : 0)); } + goto finish; + copy_fail: - decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr); + if (!frame->buf[1]) { + CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr)); + av_freep(&unmap_data); + } else { + av_buffer_unref(&frame->buf[1]); + } finish: - decoder->cudl->cuCtxPopCurrent(&dummy); + CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy)); return ret; } @@ -444,9 +494,9 @@ int ff_nvdec_end_frame(AVCodecContext *avctx) { NVDECContext *ctx = avctx->internal->hwaccel_priv_data; NVDECDecoder *decoder = (NVDECDecoder*)ctx->decoder_ref->data; + void *logctx = avctx; CUVIDPICPARAMS *pp = &ctx->pic_params; - CUresult err; CUcontext dummy; int ret = 0; @@ -456,24 +506,60 @@ int ff_nvdec_end_frame(AVCodecContext *avctx) pp->nNumSlices = ctx->nb_slices; pp->pSliceDataOffsets = ctx->slice_offsets; - err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx); - if (err != CUDA_SUCCESS) - return AVERROR_UNKNOWN; + ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx)); + if (ret < 0) + return ret; - err = decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params); - if (err != CUDA_SUCCESS) { - av_log(avctx, AV_LOG_ERROR, "Error decoding a picture with NVDEC: %d\n", - err); - ret = AVERROR_UNKNOWN; + ret = CHECK_CU(decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params)); + if (ret < 0) goto finish; - } finish: - decoder->cudl->cuCtxPopCurrent(&dummy); + CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy)); + + return ret; +} +int ff_nvdec_simple_end_frame(AVCodecContext *avctx) +{ + NVDECContext *ctx = avctx->internal->hwaccel_priv_data; + int ret = ff_nvdec_end_frame(avctx); + ctx->bitstream = NULL; return ret; } +int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, + uint32_t size) +{ + NVDECContext *ctx = avctx->internal->hwaccel_priv_data; + void *tmp; + + tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated, + (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets)); + if (!tmp) + return AVERROR(ENOMEM); + ctx->slice_offsets = tmp; + + if (!ctx->bitstream) + ctx->bitstream = (uint8_t*)buffer; + + ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream; + ctx->bitstream_len += size; + ctx->nb_slices++; + + return 0; +} + +static void nvdec_free_dummy(struct AVHWFramesContext *ctx) +{ + av_buffer_pool_uninit(&ctx->pool); +} + +static AVBufferRef *nvdec_alloc_dummy(int size) +{ + return av_buffer_create(NULL, 0, NULL, NULL, 0); +} + int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size) @@ -499,9 +585,19 @@ int ff_nvdec_frame_params(AVCodecContext *avctx, } frames_ctx->format = AV_PIX_FMT_CUDA; - frames_ctx->width = avctx->coded_width; - frames_ctx->height = avctx->coded_height; - frames_ctx->initial_pool_size = dpb_size; + frames_ctx->width = (avctx->coded_width + 1) & ~1; + frames_ctx->height = (avctx->coded_height + 1) & ~1; + /* + * We add two extra frames to the pool to account for deinterlacing filters + * holding onto their frames. + */ + frames_ctx->initial_pool_size = dpb_size + 2; + + frames_ctx->free = nvdec_free_dummy; + frames_ctx->pool = av_buffer_pool_init(0, nvdec_alloc_dummy); + + if (!frames_ctx->pool) + return AVERROR(ENOMEM); switch (sw_desc->comp[0].depth) { case 8: @@ -519,3 +615,19 @@ int ff_nvdec_frame_params(AVCodecContext *avctx, return 0; } + +int ff_nvdec_get_ref_idx(AVFrame *frame) +{ + FrameDecodeData *fdd; + NVDECFrame *cf; + + if (!frame || !frame->private_ref) + return -1; + + fdd = (FrameDecodeData*)frame->private_ref->data; + cf = (NVDECFrame*)fdd->hwaccel_priv; + if (!cf) + return -1; + + return cf->idx; +}