From 075a7ced7fa2a4f118df4e2a2e2e335ecd55d80c Mon Sep 17 00:00:00 2001 From: wang-bin Date: Thu, 3 Apr 2014 01:48:14 +0800 Subject: [PATCH] cuda: fix crash if no cuda loaded. --- src/VideoDecoderCUDA.cpp | 6 ++++++ src/cuda/cuda_api.cpp | 22 +++++++++++++++++++++- src/cuda/cuda_api.h | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/VideoDecoderCUDA.cpp b/src/VideoDecoderCUDA.cpp index 0f1d5342..a9cc51a9 100644 --- a/src/VideoDecoderCUDA.cpp +++ b/src/VideoDecoderCUDA.cpp @@ -130,6 +130,8 @@ public: nb_dec_surface = 20; if (!can_load) return; + if (!isLoaded()) //cuda_api + return; bitstream_filter_ctx = av_bitstream_filter_init("h264_mp4toannexb"); Q_ASSERT_X(bitstream_filter_ctx, "av_bitstream_filter_init", "Unknown bitstream filter"); initCuda(); @@ -137,6 +139,8 @@ public: ~VideoDecoderCUDAPrivate() { if (!can_load) return; + if (!isLoaded()) //cuda_api + return; av_bitstream_filter_close(bitstream_filter_ctx); releaseCuda(); } @@ -267,6 +271,8 @@ bool VideoDecoderCUDA::prepare() qWarning("VideoDecoderCUDA::prepare(): CUVID library not available"); return false; } + if (!d.isLoaded()) //cuda_api + return false; // max decoder surfaces is computed in createCUVIDDecoder. createCUVIDParser use the value return d.createCUVIDDecoder(mapCodecFromFFmpeg(d.codec_ctx->codec_id), d.codec_ctx->coded_width, d.codec_ctx->coded_height) && d.createCUVIDParser(); diff --git a/src/cuda/cuda_api.cpp b/src/cuda/cuda_api.cpp index 04c53c39..ce2c22b2 100644 --- a/src/cuda/cuda_api.cpp +++ b/src/cuda/cuda_api.cpp @@ -6,8 +6,10 @@ class cuda_api::context { public: + context() + : loaded(true) { #if !NV_CONFIG(DLLAPI_CUDA) && !defined(CUDA_LINK) - context() { + loaded = false; cuda_dll.setFileName("cuda"); if (!cuda_dll.isLoaded()) cuda_dll.load(); @@ -15,10 +17,22 @@ public: cuda_dll.setFileName("nvcuda"); cuda_dll.load(); } + if (!cuda_dll.isLoaded()) { + qWarning("can not load cuda!"); + return; + } cuvid_dll.setFileName("nvcuvid"); cuvid_dll.load(); + if (!cuvid_dll.isLoaded()) { + qWarning("can not load nvcuvid!"); + return; + } + loaded = true; +#endif // !NV_CONFIG(DLLAPI_CUDA) && !defined(CUDA_LINK) } +#if !NV_CONFIG(DLLAPI_CUDA) && !defined(CUDA_LINK) ~context() { + loaded = false; cuvid_dll.unload(); cuda_dll.unload(); } @@ -26,6 +40,7 @@ public: QLibrary cuda_dll; QLibrary cuvid_dll; #endif // !NV_CONFIG(DLLAPI_CUDA) && !defined(CUDA_LINK) + bool loaded; }; cuda_api::cuda_api() @@ -38,6 +53,11 @@ cuda_api::~cuda_api() delete ctx; } +bool cuda_api::isLoaded() const +{ + return ctx->loaded; +} + #if !NV_CONFIG(DLLAPI_CUDA) && !defined(CUDA_LINK) //////////////////////////////////////////////////// /// CUDA functions diff --git a/src/cuda/cuda_api.h b/src/cuda/cuda_api.h index 5864fb06..af5910c4 100644 --- a/src/cuda/cuda_api.h +++ b/src/cuda/cuda_api.h @@ -28,6 +28,7 @@ class cuda_api { public: cuda_api(); ~cuda_api(); + bool isLoaded() const; #if !NV_CONFIG(DLLAPI_CUDA) && !defined(CUDA_LINK) //////////////////////////////////////////////////// /// CUDA functions