I'm using it to capture an image which gets converted to raw rgb colour data for processing, before being converted back into a jpg.
I'm using the 'fmt2jpg' method from the 'esp_camera' library to do the conversion back into a jpg. It works, except that once its done its job, the available psram seems to have lost 128k.
My code stores the raw rgb pixel data in psram, but this gets fully released at the end of the process. I can account for all the psram that gets allocated & released, except for this 128k
My initial thought was that the 128k would be the size of the generated jpg image, but no, that's only around 20k, and anyway, this would be in the internal memory.
I plan to call this capture & process code repeatedly, but if it loses 128k each time, i'll only get 31 iterations ...
My code is basically this:
Code: Select all
void processImage(){
camera_fb_t * initial_fb = NULL;
initial_fb = esp_camera_fb_get();
Serial.println("Start free psram = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024) + "K");
uint32_t ARRAY_LENGTH = initial_fb->width * initial_fb->height * 3;
void *initial_ptrVal = NULL;
initial_ptrVal = heap_caps_malloc(ARRAY_LENGTH, MALLOC_CAP_SPIRAM);
Serial.println("After allocation free psram = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024) + "K");
uint8_t *initial_rgb = (uint8_t *)initial_ptrVal;
bool jpeg_converted = fmt2rgb888(initial_fb->buf, initial_fb->len, PIXFORMAT_JPEG, initial_rgb);
.... do stuff with the raw RGB data ....
size_t jpg_size = 0;
uint8_t * jpg_buf = NULL;
fmt2jpg(initial_rgb, ARRAY_LENGTH, 800, 600, PIXFORMAT_RGB888, 31, &jpg_buf, &jpg_size);
heap_caps_free(initial_rgb);
.... save the JPG to the SD card ....
jpg_buf = NULL;
Serial.println("End free psram = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024) + "K");
}
Start free psram = 3999K
After allocation free psram = 2593K
End free psram = 3871K
I was expecting the method to end with the full 3999k of psram available for use next time...
Any thoughts?! TBH I'm extremely rusty as regards c++. I learned it 30years ago, but gave been writing Java since, so I wouldn't be surprised to discover I'mmaking an onvious error!
cheers
Paul