jpers
Posts: 24
Joined: Mon May 23, 2016 6:06 am

Will this simple patch enable raspivid to record raw RGB?

Sun Jan 22, 2017 6:31 pm

Hello,

We use Raspberry pi for computer vision / machine vision solutions. We developed our stuff offline using videos recorded using raspivid, but for real-time processing we are using video4linux 2 and gstreamer-1.0 with v4l2 input module.

Now I have discovered that the raw video using v4l2 markedly differs from the video recorded using raspivid, especially gain control is totally off, so it messes up our application, we would need to re-tune the parameters, which is something we really don't want to do. (I checked the video by using v4l2 to capture video and then encoded into .h264 using omxh264 gstreamer module).

The best way of fixing this would be forcing raspivid to provide raw RGB output, which we will pipe into our app (we used FIFOs before to get raw video from gstreamer to our app, so no, this is not an issue). So, hopefully all other nice settings that raspivid sends to the camera will remain exactly the same.

I identified the relevant portion of the code at https://github.com/raspberrypi/userland ... RaspiVid.c, starting at line 836:

Code: Select all

           if (len==4 && !strncmp("H264", argv[i+1], 4))
               state->encoding = MMAL_ENCODING_H264;
            else  if (len==5 && !strncmp("MJPEG", argv[i+1], 5))
               state->encoding = MMAL_ENCODING_MJPEG;
            else
               valid = 0;
I also found a list of MMAL encodings here http://www.jvcref.com/files/PI/document ... dings.html, and the one I like is MMAL_ENCODING_RGB24.

So I thought of patching the code as follows (see additional two lines!)

Code: Select all

           if (len==4 && !strncmp("H264", argv[i+1], 4))
               state->encoding = MMAL_ENCODING_H264;
            else  if (len==5 && !strncmp("MJPEG", argv[i+1], 5))
               state->encoding = MMAL_ENCODING_MJPEG;
            else  if (len==4 && !strncmp("RGB3", argv[i+1], 4))
               state->encoding = MMAL_ENCODING_RGB24;
            else
               valid = 0;
Will this work out of the box without breaking anything important related to video capture (since I would like everything else in raspivid preserved)? I am afraid that it will work, but will crash and burn unpredictably sometimes later due to some unforeseen reason?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8924
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Will this simple patch enable raspivid to record raw RGB

Sun Jan 22, 2017 7:38 pm

Your proposed change won't work as the video_encode component has no codec for RGB24 as it isn't a compressed format.

However the job has already been done for you with commit https://github.com/raspberrypi/userland ... 0d113d934c

Code: Select all

 RaspiVid: Add raw (YUV420, RGB or grayscale) video output

The raw video output is useful for image processing applications where
both motion vectors from encoder and image data are needed. This commit
adds a new --raw option which inserts a video splitter component between
camera's preview output and preview's input, which allows us to set
callback to capture raw video data into a file.

Another option --raw-format is introduced which allows us to choose
YUV420, RGB or grayscale format.

Parts of the code (video output format configuration and
splitter_buffer_callback()) come from RaspiYUV.c.

The splitter component is created only if the --raw option is specified.
So make your raspivid command line include something like:

Code: Select all

raspivid --raw-format rgb --raw rgb.bin 
should save rgb images to rgb.bin. Using "-" as a destination sends the data to stdout which can then be used for piping into other things.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

jpers
Posts: 24
Joined: Mon May 23, 2016 6:06 am

Re: Will this simple patch enable raspivid to record raw RGB

Wed Mar 01, 2017 5:42 pm

6by9 wrote:Your proposed change won't work as the video_encode component has no codec for RGB24 as it isn't a compressed format.

However the job has already been done for you with commit https://github.com/raspberrypi/userland ... 0d113d934c

Code: Select all

 RaspiVid: Add raw (YUV420, RGB or grayscale) video output[/quote]

Thank you for pointing us into the right direction -- at the end, we used RaspiVidYUV: 

https://github.com/raspberrypi/userland/blob/master/host_applications/linux/apps/raspicam/RaspiVidYUV.c

Return to “Advanced users”