mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Fix openh264 decoder frame loss
This commit is contained in:
@@ -26,7 +26,7 @@ static const int YUV420P_BUFFER_SIZE = 1280 * 720 * 3 / 2;
|
|||||||
|
|
||||||
void CopyYUVWithStride(uint8_t *srcY, uint8_t *srcU, uint8_t *srcV, int width,
|
void CopyYUVWithStride(uint8_t *srcY, uint8_t *srcU, uint8_t *srcV, int width,
|
||||||
int height, int strideY, int strideU, int strideV,
|
int height, int strideY, int strideU, int strideV,
|
||||||
uint8_t *dst) {
|
uint8_t *yuv_data_) {
|
||||||
int actualWidth = width;
|
int actualWidth = width;
|
||||||
int actualHeight = height;
|
int actualHeight = height;
|
||||||
|
|
||||||
@@ -35,21 +35,21 @@ void CopyYUVWithStride(uint8_t *srcY, uint8_t *srcU, uint8_t *srcV, int width,
|
|||||||
int actualStrideV = actualWidth / 2;
|
int actualStrideV = actualWidth / 2;
|
||||||
|
|
||||||
for (int row = 0; row < actualHeight; row++) {
|
for (int row = 0; row < actualHeight; row++) {
|
||||||
memcpy(dst, srcY, actualStrideY);
|
memcpy(yuv_data_, srcY, actualStrideY);
|
||||||
srcY += strideY;
|
srcY += strideY;
|
||||||
dst += actualStrideY;
|
yuv_data_ += actualStrideY;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int row = 0; row < actualHeight / 2; row++) {
|
for (int row = 0; row < actualHeight / 2; row++) {
|
||||||
memcpy(dst, srcU, actualStrideU);
|
memcpy(yuv_data_, srcU, actualStrideU);
|
||||||
srcU += strideU;
|
srcU += strideU;
|
||||||
dst += actualStrideU;
|
yuv_data_ += actualStrideU;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int row = 0; row < actualHeight / 2; row++) {
|
for (int row = 0; row < actualHeight / 2; row++) {
|
||||||
memcpy(dst, srcV, actualStrideV);
|
memcpy(yuv_data_, srcV, actualStrideV);
|
||||||
srcV += strideV;
|
srcV += strideV;
|
||||||
dst += actualStrideV;
|
yuv_data_ += actualStrideV;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,23 +88,6 @@ OpenH264Decoder::~OpenH264Decoder() {
|
|||||||
delete nv12_frame_;
|
delete nv12_frame_;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pData[0]) {
|
|
||||||
delete pData[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pData[1]) {
|
|
||||||
delete pData[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pData[2]) {
|
|
||||||
delete pData[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pData_tmp) {
|
|
||||||
delete pData_tmp;
|
|
||||||
pData_tmp = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SAVE_H264_STREAM && h264_stream_) {
|
if (SAVE_H264_STREAM && h264_stream_) {
|
||||||
fflush(h264_stream_);
|
fflush(h264_stream_);
|
||||||
h264_stream_ = nullptr;
|
h264_stream_ = nullptr;
|
||||||
@@ -137,14 +120,6 @@ int OpenH264Decoder::Init() {
|
|||||||
decoded_frame_size_ = YUV420P_BUFFER_SIZE;
|
decoded_frame_size_ = YUV420P_BUFFER_SIZE;
|
||||||
decoded_frame_ = new uint8_t[YUV420P_BUFFER_SIZE];
|
decoded_frame_ = new uint8_t[YUV420P_BUFFER_SIZE];
|
||||||
nv12_frame_ = new uint8_t[YUV420P_BUFFER_SIZE];
|
nv12_frame_ = new uint8_t[YUV420P_BUFFER_SIZE];
|
||||||
// pData[0] = new uint8_t[1280 * 720];
|
|
||||||
// pData[1] = new uint8_t[1280 * 720 / 4];
|
|
||||||
// pData[2] = new uint8_t[1280 * 720 / 4];
|
|
||||||
|
|
||||||
pData_tmp = new uint8_t[frame_width_ * frame_height_ * 3 / 2];
|
|
||||||
// *pData = pData_tmp;
|
|
||||||
// *(pData + 1) = pData_tmp + frame_width_ * frame_height_;
|
|
||||||
// *(pData + 2) = pData_tmp + (frame_width_ * frame_height_ * 5) / 4;
|
|
||||||
|
|
||||||
if (WelsCreateDecoder(&openh264_decoder_) != 0) {
|
if (WelsCreateDecoder(&openh264_decoder_) != 0) {
|
||||||
LOG_ERROR("Failed to create OpenH264 decoder");
|
LOG_ERROR("Failed to create OpenH264 decoder");
|
||||||
@@ -160,11 +135,9 @@ int OpenH264Decoder::Init() {
|
|||||||
|
|
||||||
int32_t iRet = openh264_decoder_->Initialize(&sDecParam);
|
int32_t iRet = openh264_decoder_->Initialize(&sDecParam);
|
||||||
|
|
||||||
int trace_level = WELS_LOG_WARNING;
|
int trace_level = WELS_LOG_QUIET;
|
||||||
openh264_decoder_->SetOption(DECODER_OPTION_TRACE_LEVEL, &trace_level);
|
openh264_decoder_->SetOption(DECODER_OPTION_TRACE_LEVEL, &trace_level);
|
||||||
|
|
||||||
LOG_ERROR("inited");
|
|
||||||
printf("1 this is %p\n", this);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,36 +155,18 @@ int OpenH264Decoder::Decode(
|
|||||||
SBufferInfo sDstBufInfo = {0};
|
SBufferInfo sDstBufInfo = {0};
|
||||||
memset(&sDstBufInfo, 0, sizeof(SBufferInfo));
|
memset(&sDstBufInfo, 0, sizeof(SBufferInfo));
|
||||||
|
|
||||||
unsigned char *dst[3];
|
int iRet = openh264_decoder_->DecodeFrameNoDelay(data, size, yuv_data_,
|
||||||
|
&sDstBufInfo);
|
||||||
|
|
||||||
int iRet = openh264_decoder_->DecodeFrame2(data, size, dst, &sDstBufInfo);
|
if (sDstBufInfo.iBufferStatus == 1) {
|
||||||
// int iRet =
|
|
||||||
// openh264_decoder_->DecodeFrameNoDelay(data, size, dst, &sDstBufInfo);
|
|
||||||
|
|
||||||
if (iRet != 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// int num_of_buffer = 0;
|
|
||||||
// iRet = openh264_decoder_->GetOption(
|
|
||||||
// DECODER_OPTION_NUM_OF_FRAMES_REMAINING_IN_BUFFER, &num_of_buffer);
|
|
||||||
|
|
||||||
// LOG_ERROR("Number of buffer {} {}", num_of_buffer, iRet);
|
|
||||||
|
|
||||||
// iRet = openh264_decoder_->FlushFrame(dst, &sDstBufInfo);
|
|
||||||
// if (iRet != 0) {
|
|
||||||
// LOG_ERROR("FlushFrame state: {}", iRet);
|
|
||||||
// return -1;
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (1) {
|
|
||||||
if (on_receive_decoded_frame) {
|
if (on_receive_decoded_frame) {
|
||||||
CopyYUVWithStride(
|
CopyYUVWithStride(yuv_data_[0], yuv_data_[1], yuv_data_[2],
|
||||||
dst[0], dst[1], dst[2], sDstBufInfo.UsrData.sSystemBuffer.iWidth,
|
sDstBufInfo.UsrData.sSystemBuffer.iWidth,
|
||||||
sDstBufInfo.UsrData.sSystemBuffer.iHeight,
|
sDstBufInfo.UsrData.sSystemBuffer.iHeight,
|
||||||
sDstBufInfo.UsrData.sSystemBuffer.iStride[0],
|
sDstBufInfo.UsrData.sSystemBuffer.iStride[0],
|
||||||
sDstBufInfo.UsrData.sSystemBuffer.iStride[1],
|
sDstBufInfo.UsrData.sSystemBuffer.iStride[1],
|
||||||
sDstBufInfo.UsrData.sSystemBuffer.iStride[1], decoded_frame_);
|
sDstBufInfo.UsrData.sSystemBuffer.iStride[1],
|
||||||
|
decoded_frame_);
|
||||||
|
|
||||||
if (SAVE_NV12_STREAM) {
|
if (SAVE_NV12_STREAM) {
|
||||||
fwrite((unsigned char *)decoded_frame_, 1,
|
fwrite((unsigned char *)decoded_frame_, 1,
|
||||||
|
|||||||
@@ -35,12 +35,9 @@ class OpenH264Decoder : public VideoDecoder {
|
|||||||
uint8_t* decoded_frame_ = nullptr;
|
uint8_t* decoded_frame_ = nullptr;
|
||||||
int decoded_frame_size_ = 0;
|
int decoded_frame_size_ = 0;
|
||||||
uint8_t* nv12_frame_ = nullptr;
|
uint8_t* nv12_frame_ = nullptr;
|
||||||
unsigned char* pData[3] = {};
|
|
||||||
int frame_width_ = 1280;
|
int frame_width_ = 1280;
|
||||||
int frame_height_ = 720;
|
int frame_height_ = 720;
|
||||||
|
unsigned char* yuv_data_[3];
|
||||||
uint8_t* pData_tmp = nullptr;
|
|
||||||
uint8_t* pData_tmp_2 = nullptr;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -26,8 +26,8 @@ std::unique_ptr<VideoDecoder> VideoDecoderFactory::CreateVideoDecoder(
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return std::make_unique<FfmpegVideoDecoder>(FfmpegVideoDecoder());
|
// return std::make_unique<FfmpegVideoDecoder>(FfmpegVideoDecoder());
|
||||||
// return std::make_unique<OpenH264Decoder>(OpenH264Decoder());
|
return std::make_unique<OpenH264Decoder>(OpenH264Decoder());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,8 +120,8 @@ int OpenH264Encoder::Init() {
|
|||||||
|
|
||||||
encoded_frame_ = new uint8_t[YUV420P_BUFFER_SIZE];
|
encoded_frame_ = new uint8_t[YUV420P_BUFFER_SIZE];
|
||||||
|
|
||||||
// int trace_level = WELS_LOG_WARNING;
|
int trace_level = WELS_LOG_QUIET;
|
||||||
// openh264_encoder_->SetOption(ENCODER_OPTION_TRACE_LEVEL, &trace_level);
|
openh264_encoder_->SetOption(ENCODER_OPTION_TRACE_LEVEL, &trace_level);
|
||||||
|
|
||||||
// Create encoder parameters based on the layer configuration.
|
// Create encoder parameters based on the layer configuration.
|
||||||
SEncParamExt encoder_params = CreateEncoderParams();
|
SEncParamExt encoder_params = CreateEncoderParams();
|
||||||
@@ -196,7 +196,7 @@ int OpenH264Encoder::Encode(
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
#if 1
|
||||||
size_t required_capacity = 0;
|
size_t required_capacity = 0;
|
||||||
size_t fragments_count = 0;
|
size_t fragments_count = 0;
|
||||||
for (int layer = 0; layer < info.iLayerNum; ++layer) {
|
for (int layer = 0; layer < info.iLayerNum; ++layer) {
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ std::unique_ptr<VideoEncoder> VideoEncoderFactory::CreateVideoEncoder(
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return std::make_unique<FFmpegVideoEncoder>(FFmpegVideoEncoder());
|
// return std::make_unique<FFmpegVideoEncoder>(FFmpegVideoEncoder());
|
||||||
// return std::make_unique<OpenH264Encoder>(OpenH264Encoder());
|
return std::make_unique<OpenH264Encoder>(OpenH264Encoder());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ if is_os("windows") then
|
|||||||
add_packages("vcpkg::libnice")
|
add_packages("vcpkg::libnice")
|
||||||
add_requires("openh264 2.1.1", {configs = {shared = false}})
|
add_requires("openh264 2.1.1", {configs = {shared = false}})
|
||||||
elseif is_os("linux") then
|
elseif is_os("linux") then
|
||||||
add_requireconfs("ffmpeg.x264", {configs = {pic = true}})
|
|
||||||
add_requires("ffmpeg 5.1.2")
|
add_requires("ffmpeg 5.1.2")
|
||||||
add_requires("glib", {system = true})
|
add_requires("glib", {system = true})
|
||||||
add_requires("vcpkg::libnice 0.1.21")
|
add_requires("vcpkg::libnice 0.1.21")
|
||||||
|
|||||||
Reference in New Issue
Block a user