views:

188

answers:

3

I recently wrote C programs for image processing of BMP images, I had to read the pixel values and process them, it was very simple I followed the BMP header contents and I could get most of the information of the BMP image.

Now the challenge is to process videos (Frame by Frame), how can I do it? How will I be able to read the headers of continuous streams of image frames in a video clip? Or else, is it like, for example, the mpeg format will also have universal header, upon reading which I can get the information about the entire video and after the header, all the data are only pixels.

I hope I could convey.

Has anyone got experience with processing videos?

Any books or links to tutorials will be very helpful.

+2  A: 

A video stream, like MPEG, is composed by a number of frames dependent from (obviously) its duration and its frame-rate. To read a pixel you must start from what is called an Intra frame, which is not dependent from a previous frame into the stream. Any successive frame is a frame which is temporally dependent from its previous frame, so to obtain its pixel, you have to decode the stream from the Intra to the frame you want.
Note that, tipically, an intra frame is inserted periodically to give to the decoder a way to synchronize with the stream. This is very useful in a context where errors can occur.
What you want to do isn't an easy work. You have to use an MPEG decoder and then modify the frame before diplaying it, if you want to do post processing, like a filter or other.
I suggest you to study video coding, and you can find a lot of material on that, starting from the standard MPEG.

Maurizio Reginelli
Thanks Maurizio for you suggestions. I just need few more clarifications, the mpeg is a compressed format like jpeg I guess, is there any video format which is raw uncompressed (on the lines of BMP image) in nature? I just want to avoid video decoders. Also, please suggest if I'm right/wrong with the following statement - RAW video formats don't need a decoder.
vikramtheone
I never worked with a RAW decoder, but I suppose that your statement is right. I'm sorry, I cannot suggest you any of that.
Maurizio Reginelli
Hi Maurizio, I have posted a new question few days ago could please take a look at it once. Its titled: Webcam video stream processing. It has got only 9 views and no answers yet.
vikramtheone
+1  A: 

I would recommend looking into FFMpeg. It has a command line utility that can grab frames from a movie and dump them to an image like JPG. You can then modify your existing reader to handle JPG files (just use something like libjpeg to decode the JPG to a raw pixel buffer).

Alternatively, you can use the FFMpeg APIs (C, Python, other), and do the frame capture programatically and look at the pixels as you move through the video. Video formats are complex, so unless you want to start understanding all of the different codecs, you might want to grab a library to do the decode to raw pixel buffer for you.

SB
Thanks SB. Can you please take time to answer few questions I have posted to Jason and Maurizio?
vikramtheone
You won't find many videos out there that are uncompressed. I don't even know how the RAW format is laid out for videos. For images, it's almost like a BMP, but it can be in a different colorspace. You may need to do colorspace conversion. I really think you should either use the ffmpeg command line tool to spit out frames of the video to bmp and then use your program, or work with ffmpeg libs. Jason basically said the same thing I suggested in my answer.
SB
A: 

MPEG 1/2/4 videos are much more difficult to handle than bitmaps because they are compressed. With the bitmap data, you have actual color values stored directly to the file. In MPEG, or JPEG for that matter, the color information goes through a number transformations before being written to the file. These include

  • RGB -> YUV 420P (sub-sampled chrominance)
  • Discrete Cosine Transform
  • Weighted quantization
  • zig-zag ordering
  • differential encoding
  • variable-length encoding (Huffman-like)

All of this means that there is no simple way to parse pixel data out of the file. You either have to study every minute detail of the standard and write your own decoder, or use some video decoding library like ffmpeg to do the work for you. ffmpeg can convert your video to still images (see answers this recent question). Also, you could interface directly to the ffmpeg libraries (libavformat and libavcodec). See the answers to this question for good tutorials.

Jason
I don't agree with you. It depends on the type of processing you have to do. If you make a simple blurring, for example, you can do it in a copy of the frame to be displayed, without change the decoder. Note that changing a decoder may involve serious implication regarding the decoding process: the decoder must be standard.
Maurizio Reginelli
I was just making the point that you can't easily write a program to get pixel data out of a video file without the use of complex decoding software. It is true that once the frame is decoded, the processing is exactly the same as it is with the bitmap because at that point you've converted it to raw RGB image data.
Jason
Thanks Maurizio (again) and Jason for you suggestions. Jason I want to know your opinion on the following - The mpeg is a compressed format like jpeg I guess, is there any video format which is raw uncompressed in nature (on the lines of BMP image)? I just want to avoid video decoders. Also, please tell me if I'm right/wrong with the following statement - RAW video formats don't need a decoder.
vikramtheone
Yeah, there are uncompressed formats. I'm not sure how standard they are (I don't know if they'll work with a wide variety of players). I know you can create uncompressed video files with ffmpeg by using `-vcodec rawvideo` with your command line. It will let you put that video into any of the container formats it has like AVI, or you can just output it without a container.
Jason
Uncompressed video is not very popular because of the space it requires and the quality you can still get out of compression. I just tried converting a 12 MB mpeg-4 sample clip into an uncompressed version and it ended up 684 MB.
Jason
Oh, and you are right that raw formats don't need a decoder. The pixel data should be stored directly, but you may have to parse out some headers if it is in a container like AVI.
Jason