MIMXRT1060-EVK encoding JPEG to microSD card

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MIMXRT1060-EVK encoding JPEG to microSD card

720 Views
pagecache
Contributor I

Hello,

I'm trying to make a program where a camera image is saved onto a microSD card plugged into the EVK.  I've been using the csi_565 and sd_jpeg demos and the "Making a Camera Application" demo for the RT1050 as reference, and the program is running into insufficient memory issues when it tries to start the JPEG compressor.  I've tried reducing the image size from 720p to 240p and the OUTPUT_BUF_SIZE in libjpeg's jdatadst.c, but that only postpones the program from running out of memory to allocate to later in the compressor startup.

EDIT: Right after I posted this I realized that heap and stack were set to default sizes.  Manually set their sizes and fixed the memory issues, but now I'm getting " Improper call to JPEG library in state 0" errors.

EDIT 2: Improper library calls resolved by updating FREAD and FWRITE in jconfig.h to the read_file and write_file in jconfig.h in sd_jpeg demo.

Here's the main function:

 

 

 

 

int main(void)
{
	int j = 0;
	uint32_t csiFrameBufPtr1;

    BOARD_ConfigMPU();
    BOARD_InitPins();
    BOARD_InitDEBUG_UARTPins();
    BOARD_InitSDRAMPins();
    BOARD_EarlyPrepareCamera();
    BOARD_InitCSIPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    PRINTF("CSI RGB565 example start...\r\n");

    APP_InitCamera(); //from csi_rgb565

    CAMERA_RECEIVER_Start(&cameraReceiver);
    if (0 != MOUNT_SDCard()) //Mount_SDCard() from sd_jpeg
	{
		PRINTF("SD card mount error. Demo stopped!");
		return -1;
	}
	while (kStatus_Success != CAMERA_RECEIVER_GetFullBuffer(&cameraReceiver, &csiFrameBufPtr1))
	{
	}
	CAMERA_RECEIVER_Stop(&cameraReceiver);
	JpegCompress(csiFrameBufPtr1);
	return 0;
}

 

 

 

 

 and here's the JpegCompress function, which is from the "Making A Camera App." demo:

 

 

 

 

static status_t JpegCompress(uint32_t buffer){
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	uint16_t (*scanlines)[IMAGE_WIDTH] = (uint16_t (*)[IMAGE_WIDTH])buffer;
	JSAMPROW row_pointer;

	FILE fJpeg;
	char JpegFilename[20] = "/IMG_000.jpg";
	static uint32_t JpegFilenameIndex = 0u;

	PRINTF("Start of JPEG Compression... ");

	if(JpegFilenameIndex > 9999u)
	{
		JpegFilenameIndex = 0u;
	}

	//sprintf(JpegFilename, "/IMG_%04d.jpg", ++JpegFilenameIndex);

	/* Open the output file. */
	if(f_open(&fJpeg, _T(JpegFilename), (FA_WRITE | FA_CREATE_ALWAYS)))
	{
		PRINTF("ERROR: can't open %s\n", JpegFilename);
		return kStatus_Fail;
	}

	/* Initialize the JPEG compression object with default error handling. */
	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

	/* Initialize JPEG parameters.*/
	cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
	jpeg_set_defaults(&cinfo);

	/* Specify the source format. */
	cinfo.in_color_space = JCS_GRAYSCALE;
	cinfo.input_components = 1u;
	cinfo.data_precision = 8u;
	cinfo.image_width = (JDIMENSION)IMAGE_WIDTH;
	cinfo.image_height = (JDIMENSION)IMAGE_HEIGHT;

	/* Now that we know input colorspace, fix colorspace-dependent defaults */
	jpeg_default_colorspace(&cinfo);

	/* Specify data destination for compression */
	jpeg_stdio_dest(&cinfo, &fJpeg);

	jpeg_set_quality(&cinfo, 50u, true);

	/* Start compressor */
	jpeg_start_compress(&cinfo, TRUE);

	/* Process data */
	while (cinfo.next_scanline < cinfo.image_height)
	{
		rgb565_2_rgb888((uint8_t *)(scanlines + cinfo.next_scanline), (uint8_t *)(JpegScanlines), IMAGE_WIDTH);
		row_pointer = (JSAMPROW)JpegScanlines;
		jpeg_write_scanlines(&cinfo, &row_pointer, COMPRESS_LINES);
	}

	/* Finish compression and release memory */
	jpeg_finish_compress(&cinfo);
	jpeg_destroy_compress(&cinfo);

	f_close(&fJpeg);

	PRINTF("Done: %s is saved. \r\n", JpegFilename);

	return kStatus_Success;
}

 

 

 

 

 My hypothesis is that there's a project setting that's limiting memory access, although I'm not sure where I would find those settings.

Labels (1)
0 Replies