[fix] fix crash due to yuv420p convert to nv12

This commit is contained in:
dijunkun
2024-08-02 16:13:49 +08:00
parent 66fc4d3f95
commit 0edeec3d16
3 changed files with 36 additions and 9 deletions

View File

@@ -39,6 +39,22 @@ void CopyYUVWithStride(uint8_t *srcY, uint8_t *srcU, uint8_t *srcV, int width,
}
}
void ConvertYUV420toNV12(const unsigned char *yuvData, unsigned char *nv12Data,
int width, int height) {
int ySize = width * height;
int uvSize = ySize / 4;
const unsigned char *yData = yuvData;
const unsigned char *uData = yData + ySize;
const unsigned char *vData = uData + uvSize;
std::memcpy(nv12Data, yData, ySize);
for (int i = 0; i < uvSize; i++) {
nv12Data[ySize + i * 2] = uData[i];
nv12Data[ySize + i * 2 + 1] = vData[i];
}
}
OpenH264Decoder::OpenH264Decoder() {}
OpenH264Decoder::~OpenH264Decoder() {
if (openh264_decoder_) {
@@ -138,15 +154,20 @@ int OpenH264Decoder::Decode(
frame_width_ * frame_height_ * 3 / 2, nv12_stream_);
}
if (0) {
ConvertYUV420toNV12(decoded_frame_, nv12_frame_, frame_width_,
frame_height_);
} else {
libyuv::I420ToNV12(
(const uint8_t *)decoded_frame_, frame_width_,
(const uint8_t *)decoded_frame_ + frame_width_ * frame_height_,
frame_width_ / 2,
(const uint8_t *)decoded_frame_ +
frame_width_ * frame_height_ * 3 / 2,
frame_width_ * frame_height_ * 5 / 4,
frame_width_ / 2, nv12_frame_, frame_width_,
nv12_frame_ + frame_width_ * frame_height_, frame_width_,
frame_width_, frame_height_);
}
VideoFrame decoded_frame(nv12_frame_,
frame_width_ * frame_height_ * 3 / 2,

View File

@@ -18,6 +18,7 @@ VideoDecoderFactory::~VideoDecoderFactory() {}
std::unique_ptr<VideoDecoder> VideoDecoderFactory::CreateVideoDecoder(
bool hardware_acceleration, bool av1_encoding) {
if (av1_encoding) {
LOG_INFO("Use dav1d decoder");
return std::make_unique<Dav1dAv1Decoder>(Dav1dAv1Decoder());
} else {
#if __APPLE__
@@ -25,11 +26,13 @@ std::unique_ptr<VideoDecoder> VideoDecoderFactory::CreateVideoDecoder(
#else
if (hardware_acceleration) {
if (CheckIsHardwareAccerlerationSupported()) {
LOG_INFO("Use nvidia decoder");
return std::make_unique<NvidiaVideoDecoder>(NvidiaVideoDecoder());
} else {
return nullptr;
}
} else {
LOG_INFO("Use openh264 decoder");
return std::make_unique<OpenH264Decoder>(OpenH264Decoder());
}
#endif

View File

@@ -18,6 +18,7 @@ VideoEncoderFactory::~VideoEncoderFactory() {}
std::unique_ptr<VideoEncoder> VideoEncoderFactory::CreateVideoEncoder(
bool hardware_acceleration, bool av1_encoding) {
if (av1_encoding) {
LOG_INFO("Use AOM encoder");
return std::make_unique<AomAv1Encoder>(AomAv1Encoder());
} else {
#if __APPLE__
@@ -25,11 +26,13 @@ std::unique_ptr<VideoEncoder> VideoEncoderFactory::CreateVideoEncoder(
#else
if (hardware_acceleration) {
if (CheckIsHardwareAccerlerationSupported()) {
LOG_INFO("Use Nvidia encoder");
return std::make_unique<NvidiaVideoEncoder>(NvidiaVideoEncoder());
} else {
return nullptr;
}
} else {
LOG_INFO("Use OpenH264 encoder");
return std::make_unique<OpenH264Encoder>(OpenH264Encoder());
}
#endif