Fix stride align

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-03-03 09:27:51 +08:00
parent 42c95f71f6
commit 5aa97faedd
6 changed files with 25 additions and 18 deletions

View File

@ -380,6 +380,7 @@ impl Decoder {
fn handle_hw_video_frame(
decoder: &mut HwDecoder,
frames: &EncodedVideoFrames,
stride_align: usize,
fmt: ImageFormat,
raw: &mut Vec<u8>,
i420: &mut Vec<u8>,
@ -388,7 +389,7 @@ impl Decoder {
for h264 in frames.frames.iter() {
for image in decoder.decode(&h264.data)? {
// TODO: just process the last frame
if image.to_fmt(fmt, raw, i420).is_ok() {
if image.to_fmt(stride_align, fmt, raw, i420).is_ok() {
ret = true;
}
}
@ -400,12 +401,13 @@ impl Decoder {
fn handle_mediacodec_video_frame(
decoder: &mut MediaCodecDecoder,
frames: &EncodedVideoFrames,
stride_align: usize,
fmt: ImageFormat,
raw: &mut Vec<u8>,
) -> ResultType<bool> {
let mut ret = false;
for h264 in frames.frames.iter() {
return decoder.decode(&h264.data, fmt, raw);
return decoder.decode(&h264.data, stride_align, fmt, raw);
}
return Ok(false);
}

View File

@ -236,7 +236,7 @@ pub struct HwDecoderImage<'a> {
}
impl HwDecoderImage<'_> {
pub fn to_fmt(&self, fmt: ImageFormat, fmt_data: &mut Vec<u8>, i420: &mut Vec<u8>) -> ResultType<()> {
pub fn to_fmt(&self, stride_align: usize, fmt: ImageFormat, fmt_data: &mut Vec<u8>, i420: &mut Vec<u8>) -> ResultType<()> {
let frame = self.frame;
match frame.pixfmt {
AVPixelFormat::AV_PIX_FMT_NV12 => hw::hw_nv12_to(
@ -269,12 +269,12 @@ impl HwDecoderImage<'_> {
}
}
pub fn bgra(&self, bgra: &mut Vec<u8>, i420: &mut Vec<u8>) -> ResultType<()> {
self.to_fmt(ImageFormat::ARGB, bgra, i420)
pub fn bgra(&self, stride_align: usize, bgra: &mut Vec<u8>, i420: &mut Vec<u8>) -> ResultType<()> {
self.to_fmt(stride_align, ImageFormat::ARGB, bgra, i420)
}
pub fn rgba(&self, rgba: &mut Vec<u8>, i420: &mut Vec<u8>) -> ResultType<()> {
self.to_fmt(ImageFormat::ABGR, rgba, i420)
pub fn rgba(&self, stride_align: usize, rgba: &mut Vec<u8>, i420: &mut Vec<u8>) -> ResultType<()> {
self.to_fmt(stride_align, ImageFormat::ABGR, rgba, i420)
}
}

View File

@ -50,7 +50,8 @@ impl MediaCodecDecoder {
MediaCodecDecoders { h264, h265 }
}
pub fn decode(&mut self, data: &[u8], fmt: ImageFormat, raw: &mut Vec<u8>) -> ResultType<bool> {
// to-do: apply stride_align to raw output data
pub fn decode(&mut self, data: &[u8], stride_align: usize, fmt: ImageFormat, raw: &mut Vec<u8>) -> ResultType<bool> {
match self.dequeue_input_buffer(Duration::from_millis(10))? {
Some(mut input_buffer) => {
let mut buf = input_buffer.buffer_mut();

View File

@ -541,13 +541,15 @@ impl Image {
pub fn to(&self, fmt: ImageFormat, stride_align: usize, dst: &mut Vec<u8>) {
let h = self.height();
let mut w = self.width();
let bps = match fmt {
let w = self.width();
let bytes_per_pixel = match fmt {
ImageFormat::Raw => 3,
ImageFormat::ARGB | ImageFormat::ABGR => 4,
};
w = (w + stride_align - 1) & !(stride_align - 1);
dst.resize(h * w * bps, 0);
// https://github.com/lemenkov/libyuv/blob/6900494d90ae095d44405cd4cc3f346971fa69c9/source/convert_argb.cc#L128
// https://github.com/lemenkov/libyuv/blob/6900494d90ae095d44405cd4cc3f346971fa69c9/source/convert_argb.cc#L129
let bytes_per_row = (w * bytes_per_pixel + stride_align - 1) & !(stride_align - 1);
dst.resize(h * bytes_per_row, 0);
let img = self.inner();
unsafe {
match fmt {
@ -560,7 +562,7 @@ impl Image {
img.planes[2],
img.stride[2],
dst.as_mut_ptr(),
(w * bps) as _,
bytes_per_row as _,
self.width() as _,
self.height() as _,
);
@ -574,7 +576,7 @@ impl Image {
img.planes[2],
img.stride[2],
dst.as_mut_ptr(),
(w * bps) as _,
bytes_per_row as _,
self.width() as _,
self.height() as _,
);
@ -588,7 +590,7 @@ impl Image {
img.planes[2],
img.stride[2],
dst.as_mut_ptr(),
(w * bps) as _,
bytes_per_row as _,
self.width() as _,
self.height() as _,
);

View File

@ -40,7 +40,9 @@ pub const CLIPBOARD_INTERVAL: u64 = 333;
pub const SYNC_PEER_INFO_DISPLAYS: i32 = 1;
#[cfg(all(target_os = "macos", feature = "flutter_texture_render"))]
pub const STRIDE_ALIGN: usize = 16;
// https://developer.apple.com/forums/thread/712709
// Memory alignment should be multiple of 64.
pub const STRIDE_ALIGN: usize = 64;
#[cfg(not(all(target_os = "macos", feature = "flutter_texture_render")))]
pub const STRIDE_ALIGN: usize = 1;

View File

@ -207,8 +207,8 @@ impl VideoRenderer {
self.height = height;
self.data_len = if width > 0 && height > 0 {
let sa1 = crate::common::STRIDE_ALIGN - 1;
let w = (width as usize + sa1) & !sa1;
w * (height as usize) * 4
let row_bytes = (width as usize * 4 + sa1) & !sa1;
row_bytes * height as usize
} else {
0
};