MJPEG from camera

80 views
Skip to first unread message

Eduardo

unread,
Jun 28, 2013, 5:25:49 PM6/28/13
to v4...@googlegroups.com
Hi, I am using a camera that supports the following formats:

YUYV - 18 - 640x480 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 160x120 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 176x144 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 320x176 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 320x240 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 352x288 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 432x240 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 544x288 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 640x360 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 752x416 (1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 800x448 (1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 800x600 (1/20 - 1/15 - 1/10 - 1/5 - ) - 864x480 (1/20 - 1/15 - 1/10 - 1/5 - ) - 960x544 (1/15 - 1/10 - 1/5 - ) - 960x720 (1/10 - 1/5 - ) - 1024x576 (1/10 - 1/5 - ) - 1184x656 (1/10 - 1/5 - ) - 1280x720 (1/10 - 1/5 - ) - 1280x960 (2/15 - 1/5 - ) -
  
MJPEG - 48 - 640x480 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 160x120 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 176x144 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 320x176 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 320x240 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 352x288 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 432x240 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 544x288 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 640x360 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 752x416 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 800x448 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 800x600 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 864x480 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 960x544 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 960x720 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 1024x576 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 1184x656 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 1280x720 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) - 1280x960 (1/30 - 1/25 - 1/20 - 1/15 - 1/10 - 1/5 - ) -

I noticed that I'm receiving only YUYV from the camera, and could not find a way to set the format to MJPEG. Is it possible to do that?
Thanks in advance!
Eduardo

Gilles Gigan

unread,
Jun 30, 2013, 7:52:48 PM6/30/13
to v4...@googlegroups.com
Hi Eduardo,
you have two options:
- you can get a proper JPEG stream by creating a JPEG frame grabber using VideoDevice.getJPEGFrameGrabber()  (see http://v4l4j.googlecode.com/svn/www/v4l4j-api/au/edu/jcu/v4l4j/VideoDevice.html#getJPEGFrameGrabber(int,%20int,%20int,%20int,%20int) )
- or you can get access to the raw MJPEG stream by creating a raw frame grabber and setting the image format to MJPEG:
  get the ImageFormat object representing MJPEG using ImageFormatList.getNativeFormat(V4L4JConstants.IMG_MJPEG) ( http://v4l4j.googlecode.com/svn/www/v4l4j-api/au/edu/jcu/v4l4j/ImageFormatList.html#getNativeFormat(int) )
  and pass that image format object to VideoDevice.getRawFrameGrabber() to get a frame grabber that will deliver MJPEG frames. Note that these frames are usually missing the Huffman tables, which you will have to add manually. (If you ask for a JPEG frame grabber, v4l4j will do that for you)

Check au.edu.jcu.v4l4j.examples.SimpleViewer for a concrete example on how to get a JPEG frame grabber (check initFrameGrabber() ) and display these frames, or au.edu.jcu.v4l4j.examples.server.CamServer to see how to stream frames to VLC.

Gilles




Eduardo

--
You received this message because you are subscribed to the Google Groups "v4l4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v4l4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Eduardo

unread,
Jul 1, 2013, 10:31:36 AM7/1/13
to v4...@googlegroups.com
I have been using the JPEGFrameGrabber, but since I have four cameras, and the frames were being compressed, the CPU usage was very high, so I wanted to reduce it by directly saving frames from the camera without compressing them, but I don't how exactly how to add the Huffman tables to the received raw frames. Is there any other solution to avoid compressing frames?
Thanks in advance!

Gilles Gigan

unread,
Jul 2, 2013, 1:42:30 AM7/2/13
to v4...@googlegroups.com
Hi Eduardo,
Because your camera supports MJPEG, when you use a JPEG frame grabber, the JPEG compression should be done by the camera, and v4l4j simply adds missing tables in each of the mjpeg frame it receives. I can confirm that for you if you uninstall v4l4j, rebuild it in debug mode with "ant all-debug", install the debug version, and then run "ant test-fps test.outFormat=1".
This will run a dummy JPEG capture for 10 seconds and print a lot of stuff on the terminal. Can you copy the output of that command, so I can confirm what's happening.

Gilles

Eduardo

unread,
Jul 4, 2013, 11:35:24 PM7/4/13
to v4...@googlegroups.com
Hi Gilles,
thanks for the quick response. I thought I was receiving raw data from the camera and processing it in order to obtain the jpeg frames, but after your explanation it is clear that the all the compression is done by the camera (I also figured out what was the reason for the high CPU usage). Now I would like to save these jpeg frames to the disk in the most efficient way possible. Do you have any recommendation on how to achieve that? Thanks in advance! 
Eduardo

Gilles Gigan

unread,
Jul 5, 2013, 12:01:14 AM7/5/13
to v4...@googlegroups.com
Hi Eduardo,
great to hear the high cpu usage is sorted out. unfortunately, I dont have an efficient solution for saving jpeg files, other than the usual:
FileOutputStream jpegFile = new FileOutputStream("file.jpg");
jpegFile.write(videoFrame.getBytes());
jpegFile.close();
I think the above code should be handled by a separate thread, not by the v4l4j callback thread (ie. not in CaptureCallback.nextFrame()).
So something along the lines of:

class SaveJPEGFiles implement CaptureCallback, Runnable
{

...
BlockingQueue queue;
Thread t;

SaveJPEGFiles()
{
    ...
    queue = new LinkedBlockingQueue();
    t = new Thread(this);
    t.start();
    ...
    // initialise and start a JPEG frame grabber
}

nextFrame(VideoFrame newFrame)
{
       queue.put(newFrame);
       // dont recycle the frame here as the file writing thread is going to use it.
}


void run()
{
    // Do the jpeg file write here:
    while(! exitLoop)
    {
          VideoFrame f = queue.take();
          FileOutputStream jpegFile = new FileOutputStream("file.jpg");
          jpegFile.write(videoFrame.getBytes());
          jpegFile.close();
          f.recycle();
    }
}


}

Hope this makes sense
Gilles
Reply all
Reply to author
Forward
0 new messages