Pages

Saturday, September 25, 2010

FFMpeg - Using Libavcodec in your program


This will be my second post on ffmpeg. The first post was on building ffmpeg on windows using mingw.
In this post, we will see how you can use those libraries in your application and decode movie files. I am taking dranger's first tutorial as the source and I will build it using Visual C++ 6.0. You can find the source code here.

This post has been published in CodeProject.com. You can view it here. The Codeproject image appearing in this post will indicate that the articles are also present in CodeProject.

Explanation of the code

Opening the video file

// Register all formats and codecs
av_register_all();

// Open video file
if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
return -1; // Couldn't open file

// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
return -1; // Couldn't find stream information

// Dump information about file onto standard error
dump_format(pFormatCtx, 0, argv[1], 0);

First, we initialize ffmpeg by calling 'av_register_all()' at the starting of the program. This registers all supported formats and codecs.
Next we open the video file using av_open_input_file(). The first parameter is the pointer to the 'AVFormatContext' which we will use in our program to refer to the video file. The second parameter is the name of the file to be opened. The last three parameters are for file format, buffer size, and format options. By setting it to NULL and 0, libavformat detects and fills values on it own.
We dump information about the input file using 'dump_format()'

Opening the decoder

videoStream=-1;
for(i=0; inb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
  videoStream=i;
  break;
}
if(videoStream==-1)
return -1;
pCodecCtx=pFormatCtx->streams[videoStream]->codec;

We loop through the list of streams present in the input file to locate the 'video' stream present in it. The file can typically contain one or more audio/video streams in it. Once a video stream is located, we extract the codec type of the video stream. This codec type will be used to initialize the decode as shown below. avcoded_find_decoder accepts the codec id (obtained from the video file) as input and returns a pointer of type 'AVCodec'. The codec is then open by making a call to 'avcodec_open()'

pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1;
}
if(avcodec_open(pCodecCtx, pCodec)<0)
return -1;

Allocating buffers for decoding

numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,  pCodecCtx->width, pCodecCtx->height);

This part of the code is fairly elementary. We calculate the size of the buffer required, allocate a temporary buffer using malloc

Decoding frames

i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
if(packet.stream_index==videoStream) {
  avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, packet.data, packet.size); 
  if(frameFinished) {
  img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24,
                (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width,
                pCodecCtx->height);

if(++i<=5)
  SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height,
        i);
  }
}

Well, this is the part of the code that excites me the most, decoding the frames :) You read frames the file using av_read_frame. This function returns a non-zero positive value if the file has not reached its end. After receiving a packet, we first check if it is a video packet. We pass the CodecContext, Packet data and size as the input to the function. If decoding of the packet is successful, the 'pFrame' pointer points to decoded data. Success or failure of the decoding process is indicated by the 'frameFinished'. If it fails, the value will be '0'. We then convert the data from YUV to RBG format so that we can write to the file. I guess, by default, the decoded data is of type YUV.

Cleanup

av_free(buffer);
av_free(pFrameRGB);
av_free(pFrame);
avcodec_close(pCodecCtx);
av_close_input_file(pFormatCtx);

This is the part of the code i normally hate. But any mistake in this part will result in memory leaks or run time exceptions. So we will release all the allocated buffers and frames using av_free(). We will also close the codecontext and file using avcodec_close() and av_close_input_file() respectively.

Steps to build the application
  1. Create a win32 console application using Visual C++. Let us name it 'ffmpeg'.
  2. Create a folder named 'Libraries' and copy the following library files (avcodec.lib, avformat.lib, avutil.lib) to that folder. 
  3. Add 'tutorial1.c' to the project.
  4. Open the 'Project Settings' dialog box and in the C++ tab, choose the 'Preprocessor' option. In the 'Additional Include Directories', add the folder where the ffmpeg header files are present. (ex: C:\ffmpeg) 
  5. Navigate to the 'Link' tab in the project settings dialog and choose 'Input' as category. Add the following libraries to the 'Object/Library modules' text box : avformat.lib avcodec.lib avutil.lib. Add 'libraries' to the 'Additional Library path' text box.
  6. Build the application and you should be able to build it without any problem.
I hope to write on more post on encoding data using FFmpeg. That will complete the cycle.

9 comments:

Anonymous said...

You did not write this tutorial. Thief

Rmn said...

That's unfair to say. What makes you think that I have not written this? All source codes and snapshots were developed by me. Your argument is baseless.

Temin said...

Hi Rmn

We are doing a project in which one second of data should be extracted from the h.264 stream.
How to know how many frames make up one second of data?

According to our standard One broadcast frame can carry 1s of video and audio data.so while building the header details for the broadcast frame,we have to know the total number of video & audio bytes for that broadcast frame and and the number of frames for that video.

Is it possible to extract these information with this tool.
Kindly give your valuable suggestions

Regards
Temin

Mclovin said...

Why don't you disable anonymous comments to avoid those lousy flaggers?

Anonymous said...

hi RMN

I am trying to encode a directshow buffer stream by using libavcodec. after encoding only green video is coming with some movement. Can you tell me please what will be the reason.
Thanks you!

Imran Khan

ninest123 said...

moncler, moncler, pandora jewelry, pandora jewelry, marc jacobs, ugg,ugg australia,ugg italia, sac louis vuitton pas cher, canada goose, moncler outlet, pandora charms, canada goose outlet, bottes ugg, moncler, canada goose uk, replica watches, hollister, louis vuitton, louis vuitton, swarovski, moncler, swarovski crystal, links of london, ugg boots uk, moncler, louis vuitton, doudoune canada goose, ugg pas cher, coach outlet, louis vuitton, moncler, canada goose, canada goose outlet, canada goose, thomas sabo, canada goose, pandora charms, ugg,uggs,uggs canada, wedding dresses, moncler

ninest123 said...

timberland boots, soccer shoes, nike roshe, asics running shoes, oakley, juicy couture outlet, beats by dre, new balance, jimmy choo shoes, vans, north face outlet, instyler, mcm handbags, abercrombie and fitch, baseball bats, hollister, karen millen, gucci, lancel, bottega veneta, valentino shoes, louboutin, reebok shoes, north face outlet, converse outlet, hollister, herve leger, juicy couture outlet, ralph lauren, ferragamo shoes, insanity workout, ghd, nike air max, mont blanc, chi flat iron, nfl jerseys, toms shoes, supra shoes, mac cosmetics, converse, babyliss, wedding dresses, celine handbags, ray ban, vans shoes, nike air max, montre pas cher, soccer jerseys, p90x workout, birkin bag

ninest123 said...

vans pas cher, michael kors outlet, mulberry, michael kors, sac guess, new balance pas cher, ugg boots, nike air max, true religion outlet, hogan, true religion jeans, burberry, hollister, hollister, oakley pas cher, true religion jeans, coach purses, michael kors outlet, timberland, ray ban uk, converse pas cher, north face, burberry outlet online, longchamp, michael kors, nike huarache, michael kors outlet, tn pas cher, ray ban pas cher, michael kors outlet, michael kors outlet, hermes, air force, lululemon, nike blazer, lacoste pas cher, michael kors, nike trainers, ralph lauren uk, nike air max, nike roshe, nike air max, michael kors, north face, vanessa bruno, ugg boots, true religion jeans, hollister pas cher, nike free run uk, abercrombie and fitch

ninest123 said...

louboutin pas cher, nike air max, ray ban sunglasses, prada handbags, michael kors, coach outlet, coach outlet, nike free, replica watches, kate spade outlet, uggs on sale, louboutin shoes, longchamp outlet, longchamp, burberry, polo ralph lauren outlet, tory burch outlet, nike air max, louboutin, ralph lauren pas cher, louis vuitton, christian louboutin outlet, chanel handbags, prada outlet, tiffany jewelry, ray ban sunglasses, kate spade handbags, sac longchamp, nike outlet, air max, gucci outlet, air jordan pas cher, louboutin outlet, nike roshe run, replica watches, nike free, longchamp pas cher, jordan shoes, polo ralph lauren outlet, tiffany and co, cheap oakley sunglasses, ray ban sunglasses, oakley sunglasses, ugg boots, replica handbags, longchamp outlet, ugg boots