framereader: fix crash after fallback to cpu decoding (#23055)

old-commit-hash: a449b856cac746647ad994eb511600929d529498
This commit is contained in:
Dean Lee
2021-11-29 05:29:40 +08:00
committed by GitHub
parent 70e2e78f52
commit 4ac4b61272
2 changed files with 7 additions and 3 deletions
+6 -3
View File
@@ -27,8 +27,9 @@ enum AVPixelFormat get_hw_format(AVCodecContext *ctx, const enum AVPixelFormat *
if (*p == *hw_pix_fmt) return *p;
}
printf("Please run replay with the --no-cuda flag!\n");
assert(0);
return AV_PIX_FMT_NONE;
// fallback to YUV420p
*hw_pix_fmt = AV_PIX_FMT_NONE;
return AV_PIX_FMT_YUV420P;
}
} // namespace
@@ -94,7 +95,7 @@ bool FrameReader::load(const std::string &url, bool no_cuda, std::atomic<bool> *
width = (decoder_ctx->width + 3) & ~3;
height = decoder_ctx->height;
if (!no_cuda) {
if (has_cuda_device && !no_cuda) {
if (!initHardwareDecoder(AV_HWDEVICE_TYPE_CUDA)) {
printf("No CUDA capable device was found. fallback to CPU decoding.\n");
} else {
@@ -136,6 +137,8 @@ bool FrameReader::initHardwareDecoder(AVHWDeviceType hw_device_type) {
int ret = av_hwdevice_ctx_create(&hw_device_ctx, hw_device_type, nullptr, nullptr, 0);
if (ret < 0) {
hw_pix_fmt = AV_PIX_FMT_NONE;
has_cuda_device = false;
printf("Failed to create specified HW device %d.\n", ret);
return false;
}
+1
View File
@@ -50,4 +50,5 @@ private:
AVPixelFormat hw_pix_fmt = AV_PIX_FMT_NONE;
AVBufferRef *hw_device_ctx = nullptr;
std::vector<uint8_t> nv12toyuv_buffer;
inline static std::atomic<bool> has_cuda_device = true;
};