标签:
First things first - let‘s look at how to open a video file and get at the streams contained in it. The first thing we need to do is to initialize libavformat/libavcodec:
1 av_register_all();
This registers all available file formats and codecs with the library so they will be used automatically when a file with the corresponding format/codec is opened. Note that you only need to call av_register_all() once, so it‘s probably best to do this somewhere in your startup code. If you like, it‘s possible to register only certain individual file formats and codecs, but there‘s usually no reason why you would have to do that.
Next off, opening the file:
1 AVFormatContext *pFormatCtx; 2 const char *filename="myvideo.mpg"; // Open video file 3 if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) 4 handle_error(); //Couldn‘t open file
The last three parameters specify the file format, buffer size and format parameters; by simply specifying NULL or 0 we ask libavformat to auto-detect the format and use a default buffer size. Replace handle_error() with appropriate error handling code for your application.
Next, we need to retrieve information about the streams contained in the file: // Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0) handle_error();// Couldn‘t find stream information
This fills the streams field of the AVFormatContext with valid information. As a debugging aid, we‘ll dump this information onto standard error, but of course you don‘t have to do this in a production application:
1 dump_format(pFormatCtx, 0, filename, false);
As mentioned in the introduction, we‘ll handle only video streams, not audio streams. To make things nice and easy, we simply use the first video stream we find:
1 int i, videoStream; 2 AVCodecContext *pCodecCtx; // Find the first video stream 3 videoStream=-1; 4 for(i=0; i<pFormatCtx->nb_streams; i++) { 5 if(pFormatCtx->streams[i]->codec.codec_type==CODEC_TYPE_VIDEO) { 6 videoStream=i; 7 break; 8 } 9 if(videoStream==-1) 10 handle_error(); // Didn‘t find a video stream 11 } // Get a pointer to the codec context for the video stream pCodecCtx=&pFormatCtx->streams[videoStream]->codec;
OK, so now we‘ve got a pointer to the so-called codec context for our video stream, but we still have to find the actual codec and open it: AVCodec *pCodec;
1 // Find the decoder for the video stream 2 pCodec=avcodec_find_decoder(pCodecCtx->codec_id); 3 if(pCodec==NULL) 4 handle_error(); // Codec not found
Inform the codec that we can handle truncated bitstreams -- i.e., bitstreams where frame boundaries can fall in the middle of packets
1 if(pCodec->capabilities & CODEC_CAP_TRUNCATED) 2 pCodecCtx->flags|=CODEC_FLAG_TRUNCATED; // Open codec 3 if(avcodec_open(pCodecCtx, pCodec)<0) 4 handle_error(); // Could not open codec
So what‘s up with those "truncated bitstreams"? Well, as we‘ll see in a moment, the data in a video stream is split up into packets. Since the amount of data per video frame can vary, the boundary between two video frames need not coincide with a packet boundary. Here, we‘re telling the codec that we can handle this situation.
One important piece of information that is stored in the AVCodecContext structure is the frame rate of the video. To allow for non-integer frame rates (like NTSC‘s 29.97 fps), the rate is stored as a fraction, with the numerator in pCodecCtx->frame_rate and the denominator in pCodecCtx->frame_rate_base. While testing the library with different video files, I noticed that some codecs (notably ASF) seem to fill these fields incorrectly (frame_rate_base contains 1 instead of 1000). The following hack fixes this:
1 // Hack to correct wrong frame rates that seem to be generated by some // codecs 2 if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1) 3 pCodecCtx->frame_rate_base=1000;
Note that it shouldn‘t be a problem to leave this fix in place even if the bug is corrected some day - it‘s unlikely that a video would have a frame rate of more than 1000 fps.
One more thing left to do:
Allocate a video frame to store the decoded images in:
1 AVFrame *pFrame; 2 pFrame=avcodec_alloc_frame();
That‘s it! Now let‘s start decoding some video.
标签:
原文地址:http://www.cnblogs.com/solo-heart/p/4210964.html