Saturday, May 24, 2008

FFmpeg: An open source video codec

First of all this may be considered a research post but I think that you may find something interesting in this post as a developer.

Have you ever watched a movie…oh yeah…ok have you ever imagined that won’t be able to watch this movie without a video codec?!

Yeah… imagine that you have a movie with these features indicated in the figure bellow and suppose that this move is stored in your HD without any codec as just raw image frames in RGB format? Do you know how much space it will take from HD? It will take about 120 Giga Bytes



Yes its true; verify [720*300*3*25*(60*(60*2+13) +7)]/2^30 = 120.5 GB

But actually the file is stored at my PC using only 742 MB and so it seems that the codec made a compression ratio 1 : 166, isn’t great?

What does this means? This means that you could never store more than a single movie at your 160 GB HD, It also means that you could never burn a movie on a DVD and shares it with your friends, Ask yourself another question…What if this video was stored in the original desktop digestions e.g.; 800 X 600 pixels..It will overflow your weak HD :P.

But thanks God … this scenario doesn’t happen and the favor returns to video codec

For a second trial imagine that you view this movie on the internet for a 10 minutes, then your internet connection needs to transfer [425*355*3*25*10*60]/2^30 = 6.2 GB in a raw RGB video format

You may need to buffer for many weeks or even months to be able to view it on your strong ADSL link…What a disaster if the internet congested by millions of users like you…Internet might die.

But actually this live movie is about 20-30 MB only

So in simple definitions a video codec is a lossy compression technique used to compress/decompress multimedia to be suitable for storage and transmission

Video codec consist of encoder and decoder and there are many of them some are commercial and others are open source and a lot of them are under progress, for more information you can visit the multimedia home page.

As an example of a codec is the MPEG-4 which is implemented in the open source project ffmpeg, MPEG-4 codec has the facility that it can produce different quality stream output from the same original video for different quantizer scales, In MPEG-4, the valid quantizer scale values are in the range 1 to 31, with 1 producing the highest quality and bit rate

So let’s encode a video file with different quantizer scales (1-31) and compare the visual appearance and output stream size

You can see in the image bellow that the left half of the lady is after using Fair & Lovely lotion and the right is not.




This is not true; the left half is the output of the encoded video file after using a quantizer scale = 1, and the right is using 31…So which quality or appearance you prefer? I know the left… but to be fair who specify that is not your wish, but it depends for example on the available resources … you can’t view a live movie with the good quality one a dialup network connection instead for low speed connection you should use less good quality one

By numbers, let’s see the compression ratio for each quality value (1-32), Original raw file size is 44550 KB

Quality value from best to worst

1

2

3

15

16

30

31

Decoded steam size in KB

995

992

627

135

129

83

83

Compression ratio to raw file 1 :

44

44

71

330

345

536

536

Isn’t amazing to compress a file with a compression ratio of 44 for best quality with tha same appearance like the original file and 536 for worst quality?

You can transfer this 10 seconds file over any network starting from 995 KB ~= 1 MB for high speed network or 135 KB for medium speed network or 83 KB for slow Dial Up connection

Let’s take a running example using ffmpeg :

Download demo 2MB on Windows ; Just run the encode.bat & decode.bat after downloading akiyo_cif300.yuv into your working folder

Download demo 2MB on Linux ; Just run the encode.sh & decode.sh after downloading akiyo_cif300.yuv into your working folder

Download demo output raw video file click here (44 MB) and to play YUV file please refer to my previous post

Also you can download coded video files High 995 KB, Medium 135 KB, Low 83 KB Quality, You may need k-Lite codec to play these files or run the decoder to return them to YUV format

It is very interesting that ffmpeg source code is available for public, it is cross-platform and this another advantage, you can also enhance it and build over what the community did, and your product will be used by Video LAN or a lot of other projects

5 comments:

Mahmoud said...

The motive to write this post was my talks with dr Mohamed Sami about research groups and what should be from a researcher to continually speak with the others about his research interests, also my talks with my “anteem” friend Ahmed Isameil about motivating young students to compile large open source projects like ffmpeg to be strong developers

TeCNoYoTTa said...

شكرا يا دكتور
انا استفدت من الموضوع المفيد ده

Mohammad Fouad said...

"about motivating young students to compile large open source projects"

aho ana mesh young wala 7aga we ba2aly 2 weeks ba compile wa7ed menhom we lessa mesh 3aref

AlaaShaker said...

Nice post .. I really liked the "Fair & Lovely" part :D

Mahmoud said...

@mahmoud zidan
thanks for ur comment ya 7oda

@tecno
Thanks ya man ... w Rabena yeg3alo 3amer

@foash
3ady gedan bet7sl fe a7san el 3a2lat ..an a3raf wa7ed 3agooz a3ad kaza shahr y3ml keda ..nafs el error -> google-> nfs el pages -> la7ad mayla2y forum post or wiki te7l el moshkla
At this time ya man you owe the community and you should pay a post explaining the solution

@Alaa
Thanks ya man ... w Rabena yag3l el post 3and 7osn zanokm and to be "Fair & Lovely"