From 6e622b4ab26f8f94b886d00a3c572c0e03f942aa Mon Sep 17 00:00:00 2001 From: dijunkun Date: Tue, 15 Oct 2024 17:36:18 +0800 Subject: [PATCH] [fix] find out dav1d decode error --- .../video/decode/dav1d/dav1d_av1_decoder.cpp | 80 +++++++++++++------ .../video/encode/aom/aom_av1_encoder.cpp | 5 +- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/media/video/decode/dav1d/dav1d_av1_decoder.cpp b/src/media/video/decode/dav1d/dav1d_av1_decoder.cpp index ffc060b..7bfaca9 100644 --- a/src/media/video/decode/dav1d/dav1d_av1_decoder.cpp +++ b/src/media/video/decode/dav1d/dav1d_av1_decoder.cpp @@ -2,8 +2,8 @@ #include "log.h" -#define SAVE_RECEIVED_AV1_STREAM 1 -#define SAVE_DECODED_NV12_STREAM 0 +#define SAVE_RECEIVED_AV1_STREAM 0 +#define SAVE_DECODED_NV12_STREAM 1 #include "libyuv.h" @@ -31,14 +31,10 @@ class ScopedDav1dData { void NullFreeCallback(const uint8_t *buffer, void *opaque) {} void Yuv420pToNv12(unsigned char *SrcY, unsigned char *SrcU, - unsigned char *SrcV, unsigned char *Dst, int Width, - int Height) { - memcpy(Dst, SrcY, Width * Height); - unsigned char *DstU = Dst + Width * Height; - for (int i = 0; i < Width * Height / 4; i++) { - (*DstU++) = (*SrcU++); - (*DstU++) = (*SrcV++); - } + unsigned char *SrcV, int y_stride, int uv_stride, + unsigned char *Dst, int Width, int Height) { + libyuv::I420ToNV12(SrcY, y_stride, SrcU, uv_stride, SrcV, uv_stride, Dst, + Width, Dst + Width * Height, Width / 2, Width, Height); } Dav1dAv1Decoder::Dav1dAv1Decoder() {} @@ -167,22 +163,58 @@ int Dav1dAv1Decoder::Decode( nv12_frame_->SetHeight(frame_height_); } - if (0) { - Yuv420pToNv12((unsigned char *)dav1d_picture.data[0], - (unsigned char *)dav1d_picture.data[1], - (unsigned char *)dav1d_picture.data[2], - (unsigned char *)nv12_frame_->Buffer(), frame_width_, - frame_height_); - } else { - libyuv::I420ToNV12( - (const uint8_t *)dav1d_picture.data[0], dav1d_picture.p.w, - (const uint8_t *)dav1d_picture.data[1], dav1d_picture.p.w / 2, - (const uint8_t *)dav1d_picture.data[2], dav1d_picture.p.w / 2, - (uint8_t *)nv12_frame_->Buffer(), frame_width_, - (uint8_t *)nv12_frame_->Buffer() + frame_width_ * frame_height_, - frame_width_, frame_width_, frame_height_); + // if (1) { + // Yuv420pToNv12((unsigned char *)dav1d_picture.data[0], + // (unsigned char *)dav1d_picture.data[1], + // (unsigned char *)dav1d_picture.data[2], + // dav1d_picture.stride[0], dav1d_picture.stride[1], + // (unsigned char *)nv12_frame_->Buffer(), frame_width_, + // frame_height_); + // } else { + // libyuv::I420ToNV12( + // (const uint8_t *)dav1d_picture.data[0], dav1d_picture.stride[0], + // (const uint8_t *)dav1d_picture.data[1], dav1d_picture.stride[1] / 2, + // (const uint8_t *)dav1d_picture.data[2], dav1d_picture.stride[1] / 2, + // (uint8_t *)nv12_frame_->Buffer(), frame_width_, + // (uint8_t *)nv12_frame_->Buffer() + frame_width_ * frame_height_, + // frame_width_ / 2, frame_width_, frame_height_); + // } + + size_t y_size = frame_width_ * frame_height_; + size_t u_size = frame_width_ * frame_height_ / 4; + size_t v_size = frame_width_ * frame_height_ / 4; + + for (int i = 0; i < frame_height_; i++) { + memcpy((void *)(nv12_frame_->Buffer() + i * frame_width_), + (uint8_t *)(dav1d_picture.data[0]) + i * dav1d_picture.stride[0], + frame_width_); } + // for (int i = 0; i < frame_height_ / 2; i++) { + // memcpy((void *)(nv12_frame_->Buffer() + y_size + i * (frame_width_ / 2)), + // (uint8_t *)(dav1d_picture.data[1]) + i * dav1d_picture.stride[1], + // frame_width_ / 2); + // } + + uint8_t *uv_plane = (uint8_t *)nv12_frame_->Buffer() + y_size; + for (int i = 0; i < frame_height_ / 2; i++) { + for (int j = 0; j < frame_width_ / 2; j++) { + uv_plane[i * frame_width_ + 2 * j] = + ((uint8_t *)(dav1d_picture.data[1]))[i * dav1d_picture.stride[1] + j]; + uv_plane[i * frame_width_ + 2 * j + 1] = + ((uint8_t *)(dav1d_picture.data[2]))[i * dav1d_picture.stride[1] + j]; + } + } + + for (int i = 0; i < frame_height_ / 2; i++) { + memcpy((void *)(nv12_frame_->Buffer() + y_size + u_size + + i * (frame_width_ / 2)), + (uint8_t *)(dav1d_picture.data[2]) + i * dav1d_picture.stride[1], + frame_width_ / 2); + } + + LOG_ERROR("{}x{}", dav1d_picture.stride[0], dav1d_picture.stride[1]); + on_receive_decoded_frame(*nv12_frame_); if (SAVE_DECODED_NV12_STREAM) { diff --git a/src/media/video/encode/aom/aom_av1_encoder.cpp b/src/media/video/encode/aom/aom_av1_encoder.cpp index 440bfc2..900b5ab 100644 --- a/src/media/video/encode/aom/aom_av1_encoder.cpp +++ b/src/media/video/encode/aom/aom_av1_encoder.cpp @@ -6,7 +6,7 @@ #include "log.h" #define SAVE_RECEIVED_NV12_STREAM 0 -#define SAVE_ENCODED_AV1_STREAM 1 +#define SAVE_ENCODED_AV1_STREAM 0 #define SET_ENCODER_PARAM_OR_RETURN_ERROR(param_id, param_value) \ do { \ @@ -25,8 +25,6 @@ constexpr int kLagInFrames = 0; // No look ahead. constexpr int kRtpTicksPerSecond = 90000; constexpr double kMinimumFrameRate = 1.0; -constexpr uint8_t kObuSizePresentBit = 0b0'0000'010; - static aom_superblock_size_t GetSuperblockSize(int width, int height, int threads) { int resolution = width * height; @@ -341,6 +339,7 @@ int AomAv1Encoder::Encode(const XVideoFrame *video_frame, // LOG_INFO("Encoded frame qp = {}", qp); if (on_encoded_image) { + LOG_ERROR("enc {}x{}", video_frame->width, video_frame->height); on_encoded_image((char *)encoded_frame_, encoded_frame_size_, frame_type); if (SAVE_ENCODED_AV1_STREAM) {