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;
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
- Create a win32 console application using Visual C++. Let us name it 'ffmpeg'.
- Create a folder named 'Libraries' and copy the following library files (avcodec.lib, avformat.lib, avutil.lib) to that folder.
- Add 'tutorial1.c' to the project.
- 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)
- 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.
- Build the application and you should be able to build it without any problem.