Make JPEG buffer update use image source arg.

This commit is contained in:
iabdalkader 2021-04-03 20:37:34 +02:00
parent f0903dd67f
commit d28b14e175
9 changed files with 33 additions and 28 deletions

View File

@ -113,24 +113,27 @@ static void initialize_jpeg_buf_from_image(image_t *img)
}
}
void fb_update_jpeg_buffer()
void framebuffer_update_jpeg_buffer(image_t *src)
{
image_t main_fb_src;
static int overflow_count = 0;
image_t src;
framebuffer_initialize_image(&src);
if (src == NULL) {
framebuffer_initialize_image(&main_fb_src);
src = &main_fb_src;
}
if (framebuffer->streaming_enabled && jpeg_framebuffer->enabled) {
if (src.bpp > 3) {
if (src->bpp > 3) {
bool does_not_fit = false;
if (mutex_try_lock(&jpeg_framebuffer->lock, MUTEX_TID_OMV)) {
if(CONSERVATIVE_JPEG_BUF_SIZE < src.bpp) {
if(CONSERVATIVE_JPEG_BUF_SIZE < src->bpp) {
initialize_jpeg_buf_from_image(NULL);
does_not_fit = true;
} else {
initialize_jpeg_buf_from_image(&src);
memcpy(jpeg_framebuffer->pixels, src.pixels, src.bpp);
initialize_jpeg_buf_from_image(src);
memcpy(jpeg_framebuffer->pixels, src->pixels, src->bpp);
}
mutex_unlock(&jpeg_framebuffer->lock, MUTEX_TID_OMV);
@ -138,18 +141,18 @@ void fb_update_jpeg_buffer()
if (does_not_fit) {
printf("Warning: JPEG too big! Trying framebuffer transfer using fallback method!\n");
int new_size = fb_encode_for_ide_new_size(&src);
int new_size = fb_encode_for_ide_new_size(src);
fb_alloc_mark();
uint8_t *temp = fb_alloc(new_size, FB_ALLOC_NO_HINT);
fb_encode_for_ide(temp, &src);
fb_encode_for_ide(temp, src);
(MP_PYTHON_PRINTER)->print_strn((MP_PYTHON_PRINTER)->data, (const char *) temp, new_size);
fb_alloc_free_till_mark();
}
} else if (src.bpp >= 0) {
} else if (src->bpp >= 0) {
if (mutex_try_lock(&jpeg_framebuffer->lock, MUTEX_TID_OMV)) {
image_t dst = {.w=src.w, .h=src.h, .bpp=CONSERVATIVE_JPEG_BUF_SIZE, .pixels=jpeg_framebuffer->pixels};
image_t dst = {.w=src->w, .h=src->h, .bpp=CONSERVATIVE_JPEG_BUF_SIZE, .pixels=jpeg_framebuffer->pixels};
// Note: lower quality saves USB bandwidth and results in a faster IDE FPS.
bool overflow = jpeg_compress(&src, &dst, jpeg_framebuffer->quality, false);
bool overflow = jpeg_compress(src, &dst, jpeg_framebuffer->quality, false);
if (overflow) {
// JPEG buffer overflowed, reduce JPEG quality for the next frame
@ -167,7 +170,7 @@ void fb_update_jpeg_buffer()
}
// Dynamically adjust our quality if the image is huge.
bool big_frame_buffer = image_size(&src) > JPEG_QUALITY_THRESH;
bool big_frame_buffer = image_size(src) > JPEG_QUALITY_THRESH;
int jpeg_quality_max = big_frame_buffer ? JPEG_QUALITY_LOW : JPEG_QUALITY_HIGH;
// No buffer overflow, increase quality up to max quality based on frame size...

View File

@ -49,12 +49,6 @@ void fb_encode_for_ide(uint8_t *ptr, image_t *img);
void framebuffer_init0();
// Initializes an image_t struct with the frame buffer.
void framebuffer_initialize_image(image_t *img);
// Transfers the frame buffer to the jpeg frame buffer if not locked.
void fb_update_jpeg_buffer();
int32_t framebuffer_get_x();
int32_t framebuffer_get_y();
int32_t framebuffer_get_u();
@ -75,6 +69,14 @@ uint32_t framebuffer_get_buffer_size();
// Return the current buffer address.
uint8_t *framebuffer_get_buffer();
// Initializes an image_t struct with the frame buffer.
void framebuffer_initialize_image(image_t *img);
// Compress src image to the JPEG buffer if src is mutable, otherwise copy src to the JPEG buffer
// if the src is JPEG and fits in the JPEG buffer, or encode and stream src image to the IDE if not.
// If src == NULL use main framebuffer as source image.
void framebuffer_update_jpeg_buffer(image_t *src);
// Set the framebuffer w, h and bpp.
void framebuffer_set(int32_t w, int32_t h, int32_t bpp);

View File

@ -1088,7 +1088,7 @@ mp_obj_t py_fir_snapshot(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
}
if (copy_to_fb) {
fb_update_jpeg_buffer();
framebuffer_update_jpeg_buffer(NULL);
}
image_t dst_img;

View File

@ -940,7 +940,7 @@ static mp_obj_t py_image_to(int bpp, const uint16_t *default_color_palette, bool
}
if (copy_to_fb && copy) {
fb_update_jpeg_buffer();
framebuffer_update_jpeg_buffer(NULL);
}
image_t dst_img;
@ -1205,7 +1205,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_save_obj, 2, py_image_save);
static mp_obj_t py_image_flush(mp_obj_t img_obj)
{
fb_update_jpeg_buffer();
framebuffer_update_jpeg_buffer(NULL);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_image_flush_obj, py_image_flush);
@ -6574,7 +6574,7 @@ mp_obj_t py_image_load_image(uint n_args, const mp_obj_t *args, mp_map_t *kw_arg
}
if (copy_to_fb) {
fb_update_jpeg_buffer();
framebuffer_update_jpeg_buffer(NULL);
}
return py_image_from_struct(&image);

View File

@ -156,7 +156,7 @@ mp_obj_t py_imageio_read(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
}
if (copy_to_fb) {
fb_update_jpeg_buffer();
framebuffer_update_jpeg_buffer(NULL);
}
if (0) {

View File

@ -100,7 +100,7 @@ static mp_obj_t py_sensor_shutdown(mp_obj_t enable)
static mp_obj_t py_sensor_flush()
{
fb_update_jpeg_buffer();
framebuffer_update_jpeg_buffer(NULL);
return mp_const_none;
}

View File

@ -998,7 +998,7 @@ int sensor_snapshot(sensor_t *sensor, image_t *image, uint32_t flags)
// Compress the framebuffer for the IDE preview, only if it's not the first frame,
// the framebuffer is enabled and the image sensor does not support JPEG encoding.
// Note: This doesn't run unless the IDE is connected and the framebuffer is enabled.
fb_update_jpeg_buffer();
framebuffer_update_jpeg_buffer(NULL);
noInterrupts();

View File

@ -1282,7 +1282,7 @@ int sensor_snapshot(sensor_t *sensor, image_t *image, uint32_t flags)
// Compress the framebuffer for the IDE preview, only if it's not the first frame,
// the framebuffer is enabled and the image sensor does not support JPEG encoding.
// Note: This doesn't run unless the IDE is connected and the framebuffer is enabled.
fb_update_jpeg_buffer();
framebuffer_update_jpeg_buffer(NULL);
// Make sure the raw frame fits into the FB. It will be switched from RGB565 to BAYER
// first to save space before being cropped until it fits.

View File

@ -484,7 +484,7 @@ static int sensor_check_buffsize(sensor_t *sensor)
static int snapshot(sensor_t *sensor, image_t *image, uint32_t flags)
{
fb_update_jpeg_buffer();
framebuffer_update_jpeg_buffer(NULL);
if (sensor_check_buffsize(sensor) == -1) {
return -1;