[fix] fix the function which converts yuv420p into nv12

This commit is contained in:
dijunkun
2024-10-16 09:48:33 +08:00
parent 6e622b4ab2
commit b10e41ccab
2 changed files with 27 additions and 54 deletions

View File

@@ -3,7 +3,7 @@
#include "log.h" #include "log.h"
#define SAVE_RECEIVED_AV1_STREAM 0 #define SAVE_RECEIVED_AV1_STREAM 0
#define SAVE_DECODED_NV12_STREAM 1 #define SAVE_DECODED_NV12_STREAM 0
#include "libyuv.h" #include "libyuv.h"
@@ -33,8 +33,17 @@ void NullFreeCallback(const uint8_t *buffer, void *opaque) {}
void Yuv420pToNv12(unsigned char *SrcY, unsigned char *SrcU, void Yuv420pToNv12(unsigned char *SrcY, unsigned char *SrcU,
unsigned char *SrcV, int y_stride, int uv_stride, unsigned char *SrcV, int y_stride, int uv_stride,
unsigned char *Dst, int Width, int Height) { unsigned char *Dst, int Width, int Height) {
libyuv::I420ToNV12(SrcY, y_stride, SrcU, uv_stride, SrcV, uv_stride, Dst, for (int i = 0; i < Height; i++) {
Width, Dst + Width * Height, Width / 2, Width, Height); memcpy(Dst + i * Width, SrcY + i * y_stride, Width);
}
unsigned char *DstUV = Dst + Width * Height;
for (int i = 0; i < Height / 2; i++) {
for (int j = 0; j < Width / 2; j++) {
DstUV[i * Width + 2 * j] = SrcU[i * uv_stride + j];
DstUV[i * Width + 2 * j + 1] = SrcV[i * uv_stride + j];
}
}
} }
Dav1dAv1Decoder::Dav1dAv1Decoder() {} Dav1dAv1Decoder::Dav1dAv1Decoder() {}
@@ -163,58 +172,23 @@ int Dav1dAv1Decoder::Decode(
nv12_frame_->SetHeight(frame_height_); nv12_frame_->SetHeight(frame_height_);
} }
// if (1) { if (0) {
// Yuv420pToNv12((unsigned char *)dav1d_picture.data[0], Yuv420pToNv12((unsigned char *)dav1d_picture.data[0],
// (unsigned char *)dav1d_picture.data[1], (unsigned char *)dav1d_picture.data[1],
// (unsigned char *)dav1d_picture.data[2], (unsigned char *)dav1d_picture.data[2],
// dav1d_picture.stride[0], dav1d_picture.stride[1], dav1d_picture.stride[0], dav1d_picture.stride[1],
// (unsigned char *)nv12_frame_->Buffer(), frame_width_, (unsigned char *)nv12_frame_->Buffer(), frame_width_,
// frame_height_); frame_height_);
// } else { } else {
// libyuv::I420ToNV12( libyuv::I420ToNV12(
// (const uint8_t *)dav1d_picture.data[0], dav1d_picture.stride[0], (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[1], dav1d_picture.stride[1],
// (const uint8_t *)dav1d_picture.data[2], dav1d_picture.stride[1] / 2, (const uint8_t *)dav1d_picture.data[2], dav1d_picture.stride[1],
// (uint8_t *)nv12_frame_->Buffer(), frame_width_, (uint8_t *)nv12_frame_->Buffer(), frame_width_,
// (uint8_t *)nv12_frame_->Buffer() + frame_width_ * frame_height_, (uint8_t *)nv12_frame_->Buffer() + frame_width_ * frame_height_,
// frame_width_ / 2, frame_width_, frame_height_); frame_width_, 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_); on_receive_decoded_frame(*nv12_frame_);
if (SAVE_DECODED_NV12_STREAM) { if (SAVE_DECODED_NV12_STREAM) {

View File

@@ -339,7 +339,6 @@ int AomAv1Encoder::Encode(const XVideoFrame *video_frame,
// LOG_INFO("Encoded frame qp = {}", qp); // LOG_INFO("Encoded frame qp = {}", qp);
if (on_encoded_image) { if (on_encoded_image) {
LOG_ERROR("enc {}x{}", video_frame->width, video_frame->height);
on_encoded_image((char *)encoded_frame_, encoded_frame_size_, on_encoded_image((char *)encoded_frame_, encoded_frame_size_,
frame_type); frame_type);
if (SAVE_ENCODED_AV1_STREAM) { if (SAVE_ENCODED_AV1_STREAM) {