v4l2 video capture example

Rehab11 picture Rehab11 · Feb 25, 2012 · Viewed 12.4k times · Source

this is my first post here and I hope I can find some help

I'm working on embedded linux on samsung ok6410 board and ov9650 cmos camera

I have to capture a video and save it on a SD-card

I want to use a circular buffer that captures video and, when it is full, allow new data will overwrite the old.

There is a flag that, when raised, capturing continues for 10 seconds then stops. The video is saved to sd-card should contain the 10 seconds before raising the flag and 10 seconds after raising.

I read the capture example on the official site of v4l2 API specifications here

http://free-electrons.com/kerneldoc/latest/video4linux/API.html

but there are some points that I can't understand or don't know if i understand it correctly

  1. In this example,there are 4 frame buffers,each buffer can hold one frame. Is this right?

  2. There is a variable (frame_count)that is initialized to 70,does this mean that by finishing this program I will have a video that contains 70 frames?

  3. What do the fns (main loop) and (read frame) do? I know what they should do but can't understand the written code ,the loops..etc

  4. How can I adjust the fps in this code ? or I should write the value in the camera registers?

  5. After capturing the video I will have a buffer that is filled with raw data, I want to compress it or use codecs to save it as MPEG , can I change the pixel format to be compressed (MPEG for example) instead of YUYV? or what should I do to compress the video? codecs or what?

  6. Can I write these raw data in the buffer to a file .yuv? I found some software that can play videos of this format.

  7. Is it possible to use circular buffers instead of linear buffers to hold the captured video?

I know that my post is too long but I'm a newbie and can't find any tutorials that can help me to write my code.

thanks in advance

Answer

Ottavio Campana picture Ottavio Campana · Mar 7, 2012

The page that you're linking returns 404, but if you're looking to the documentation, you should check this https://linuxtv.org/downloads/v4l-dvb-apis/ , particularly check this example, which is a good starting point to do what you want.

Since I think that I guessed the code you're referring to, I try to answer your questions

  1. the 4 frame buffers are used to do double-buffering, i.e. you dequeue a frame and operate on it, while the others get filled by the video input
  2. If it is as in the example I linked, no it just grabs 70 frames from the input and then exits.
  3. read_frame dequeues a frame from the buffer so that you can operate on that and re-adds it to the queue when you're done.
  4. to adjust fps, you need to dequeue and requeue frames to the drivers without doing anything. Modifying the fps is done by frame skipping.
  5. generally spoken, it depends on your driver and your encoder. You need to find a video format (such as for example V4L2_PIX_FMT_YUYV) that is supported both by the driver and by the encoder. If it does not exist, then you need to write your own colorspace conversion routine.
  6. Sure! I often do this.
  7. I don't understand the question.