mjpeg video code

Discussion related to "under the hood" OpenMV topics.
ragbagger
Posts: 20
Joined: Wed Aug 09, 2017 5:37 pm

mjpeg video code

Postby ragbagger » Tue Jan 08, 2019 9:51 pm

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?
User avatar
kwagyeman
Posts: 3132
Joined: Sun May 24, 2015 2:10 pm

Re: mjpeg video code

Postby kwagyeman » Tue Jan 08, 2019 11:35 pm

Ha, that codes is wrong 3 years ago. I don't know who wrote that... wait, I did :).

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.
Nyamekye,
ragbagger
Posts: 20
Joined: Wed Aug 09, 2017 5:37 pm

Re: mjpeg video code

Postby ragbagger » Wed Jan 09, 2019 1:58 pm

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.
User avatar
kwagyeman
Posts: 3132
Joined: Sun May 24, 2015 2:10 pm

Re: mjpeg video code

Postby kwagyeman » Wed Jan 09, 2019 2:29 pm

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.
Nyamekye,
ragbagger
Posts: 20
Joined: Wed Aug 09, 2017 5:37 pm

Re: mjpeg video code

Postby ragbagger » Wed Jan 09, 2019 3:53 pm

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.
ragbagger
Posts: 20
Joined: Wed Aug 09, 2017 5:37 pm

Re: mjpeg video code

Postby ragbagger » Wed Jan 09, 2019 9:04 pm

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.
ragbagger
Posts: 20
Joined: Wed Aug 09, 2017 5:37 pm

Re: mjpeg video code

Postby ragbagger » Wed Jan 09, 2019 9:33 pm

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 :)
User avatar
kwagyeman
Posts: 3132
Joined: Sun May 24, 2015 2:10 pm

Re: mjpeg video code

Postby kwagyeman » Thu Jan 10, 2019 12:04 pm

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.
Nyamekye,
ragbagger
Posts: 20
Joined: Wed Aug 09, 2017 5:37 pm

Re: mjpeg video code

Postby ragbagger » Thu Jan 10, 2019 1:46 pm

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?
User avatar
kwagyeman
Posts: 3132
Joined: Sun May 24, 2015 2:10 pm

Re: mjpeg video code

Postby kwagyeman » Thu Jan 10, 2019 3:09 pm

We had to read the spec and then figure it out...
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 5 guests