mjpeg video code

Hi guys
I have a question about adding a frame your mjpeg code. Specifically from line 99 in function
void mjpeg_add_frame(FIL *fp, uint32_t *frames, uint32_t *bytes, image_t *img, int quality)

I am confused about the line
write_data(fp, img->pixels, img->bpp + pad); // reading past okay".

Hoping you could shed some insight.

My understanding is as follows

fp - fatfs file pointer. so you are writing directly to SD card
img->pixels- the jpeg pixel data
img->bpp +pad …assuming bpp =2, this comes to be 2 + 4 = 6

now the 3rd argument for function write_data() is the size of data to be written for the new frame. Why are you only writing 6 bytes. Shouldn’t this be the size of the entire jpeg file or jpeg frame ? What am I missing here?

Ha, that codes is wrong 3 years ago. I don’t know who wrote that… wait, I did :slight_smile:.

Um, the bpp field is the length of the jpeg file in bytes when >= 4. It’s a rather abused value. Anyway, the jpeg data byte stream must be a multiple of 4. The reading past okay comment there notes that I’m writing garbage data to the disk past the end of the valid jpeg data. But, it’s okay because it’s padding.

haha… 3 years is a long time. So I appreciate the explanation.

Still do not understand one thing though… If that line is just adding the padding for a multiple of 4, where are you actually writing in the frame data. I do not see any line for that in the jpeg_add_frame function. I would assume that for each frame you would also write the actual jpeg data.

The write data method writes the data. It’s a byte blob so you just give the starting address and length. As for the jpeg compression that’s done in the jpeg compression call.

right…but the length that is given to the write data method is “out.bpp + pad”

write_data(fp, out.pixels, out.bpp + pad);


out.bpp + pad = 2+4 = 6

I’m assuming bpp is 2 bytes for RGB565.
Shouldn’t the length be some kilobytes in size? My jpeg image size is around 10KB.

wait… I just re- read your reply a few times. Are you saying the bpp = jpeg file size when bpp> 4? I couldn’t get that from the code, but ill take your word for it. Must have missed it.

update: I was able to get video working. As usual thanks a ton for your replies. Apologies for not reading your reply with more attention before :slight_smile:

Yeah, the bbp means different things at different times. If the value is >= 4 then it’s actually the file length in bytes of a jpg image. Yes, this is poor design but it works and it’s so built-into the logic now that its a bear to change.

yes. Interesting. And it does work pretty well.
Also, where did you get the skeleton for the mjpeg headers. I scoured the internet to find a source for a mjpeg header when initially thinking of creating a video with the jpeg files and came up with nothing.
So very very curious to how/where you got the header info for the mjpeg_open, jpeg_close and mjpeg_add_frame functions?

We had to read the spec and then figure it out…