aws / dcv-color-primitives

DCV Color Primitives Library
MIT No Attribution
30 stars 22 forks source link

Stack overflow when converting BGRA to RGB #72

Closed averyanalex closed 1 year ago

averyanalex commented 1 year ago

I use this code:

use dcp::{ColorSpace, ImageFormat, PixelFormat};
use dcv_color_primitives as dcp;

const YUV_FORMAT: ImageFormat = ImageFormat {
    pixel_format: PixelFormat::I444,
    color_space: ColorSpace::Bt601,
    num_planes: 3,
};

const BGRA_FORMAT: ImageFormat = ImageFormat {
    pixel_format: PixelFormat::Bgra,
    color_space: ColorSpace::Rgb,
    num_planes: 1,
};

const RGB_FORMAT: ImageFormat = ImageFormat {
    pixel_format: PixelFormat::Rgb,
    color_space: ColorSpace::Rgb,
    num_planes: 1,
};

pub fn yuv_to_bgra(src_yuv_buf: &Vec<&[u8]>, yuv_strides: &[usize; 3]) -> [u8; 640 * 480 * 3] {
    dcp::initialize();

    let mut bgra_buf = [0u8; 640 * 480 * 4];
    let dst_bgra_buf = &mut [&mut bgra_buf[..]];
    let bgra_strides = &[0usize; 1];
    dcp::convert_image(
        640,
        480,
        &YUV_FORMAT,
        Some(yuv_strides),
        src_yuv_buf,
        &BGRA_FORMAT,
        Some(bgra_strides),
        dst_bgra_buf,
    )
    .unwrap();

    let src_bgra_buf = &[&bgra_buf[..]];
    let mut rgb_buf = [0u8; 640 * 480 * 3];
    let dst_rgb_buf = &mut [&mut rgb_buf[..]];
    dcp::convert_image(
        640,
        480,
        &BGRA_FORMAT,
        Some(bgra_strides),
        src_bgra_buf,
        &RGB_FORMAT,
        None,
        dst_rgb_buf,
    )
    .unwrap();
    rgb_buf
}

And I get stackoverflow:

thread 'tokio-runtime-worker' has overflowed its stack
fatal runtime error: stack overflow
[1]    709615 IOT instruction (core dumped)  cargo run --release

YUV to BGRA works fine.

averyanalex commented 1 year ago

Oh, sorry, I changed slices to vectors From

let mut rgb_buf = [0u8; 640 * 480 * 3];

to

let mut rgb_buf: Vec<_> = vec![0u8; 640 * 480 * 3];

and it works now!

Maybe change readme examples to use vecs instead of byte slices?

fabiosky commented 1 year ago

Hi Alexander,

you are right. All documentation examples allocating data on the stack will work just if the linker stack size is sufficiently large.

Addressing it in upcoming PR.

Thanks for the suggestion!

fabiosky commented 1 year ago

Here's the PR: https://github.com/aws/dcv-color-primitives/pull/73