Http://www.cnblogs.com/tanlon/p/3879081.html
Learn about the more important functions and data structures in FFmpeg before you begin the decoding exercise.
1. Data structure:
(1) Avformatcontext
Avformatcontextis an always-on data structure, and many functions use it as a parameter.ffmpegformat i/o context avinputformat ( or avoutputformat same time avformatcontextavstream, avpacket These important data structures and some other pertinent information, such as title,author,copyright ET. There are also some information that may be used in decoding, such as: duration, file_size, bit_rate
Initialization: Because the avformatconext structure contains a lot of information, the initialization process is step-through, and some variables are not initialized if no value is available. However, because the general declaration is using pointers, a process of allocating memory is not limited:
Avformatcontext *pformatctx;pformatctx = Avformat_alloc_context ();
(2) Avoutputformat
The " indication" of which codec the codec will use.
Avoutputformat *fmt; FMT = Guess_format (null, filename, null);
The file format is judged by filename, and what encoder is initialized. Of course, if is using Avinputformat *fmt , is fix with what decoder. (Specify output sequence ->fix encoder, specify input sequence ->fix decoder)
(3) Avinputformat
Get Avinputformat from Avformatcontext
Avinputformat *Inputformat;inputformat = pformatctx->iformat;
(4) Avcodeccontext
The comment in this structure in the Ffmpeg SDK is that the main external API structure its importance. And in avcodec its definition, it gives a very detailed introduction to each of its member variables. It should be said that the initialization of Avcodeccontext is the most important link in Codec use, and Avcodeccontext is a member structure of Avstream.
(5) Avcodec
StructureAvcodecThere are fewer member variables and member functions, but it is important. He contained aCodecid, that is, with whichCodec、 pixel format information. There are also the aforementioned 5 a function ( init encode close decoder flushavcodeccontexavcodeccontext after initialization ,avcodec
(6) Avframe
Avframe is described as a " RAW Image "(i.e. YUV or RGB ...). is there anything else? The structure of his first two member data,uint8_t
*data[4],int linesize[4]< Span style= "font-family: the song Body;" > The first deposit is y cr (yuvlinesize What is it? The two data can also be extracted at a different data structure: avpicture.
In addition,Avframe also contains some other member data, such as. Whether key_frame, encoded image book Coded_picture_number, whether as reference frame reference, macro block type *mb_type, and so on .
the initialization of the avframe is not as simple as it looks on his structure. Because Avframe also has a task that hosts image data (data[4]) Therefore, allocating memory to him should be done with care.
(7) avpacket
avpacketavpacket structure. In my opinion, such coding settings are very necessary, especially in the video real-time transmission, synchronization, boundary problems can be through avpacket to solve. avpacket member data has two timestamps, data data (usually encoded data), size size et cetera (see line). Speaking avpacket" will have to mention the codec function, because avpacket
(8) avpicture
Avpictureavpicture< Span style= "font-family: the song Body;" > will picture The concept from framepicture (picture) itself information, brightness, chroma and row size. and framekey frame and such information. Such a similar graded " Span style= "font-family: the song Body;" > is the whole concept clearer. avpacketavpacket
(9) Avstream
Avstream is justified as the second cross-cutting structure following avformatcontext. There are avcodeccontext in his member data, which is basically set up for the parameters of the Video Codec that are used (including important information such as bit rate, resolution, etc.). Also, as a "stream" , it contains the " stream "
Some of the data in this concept, such as: Frame rate (r_frame_rate), Basic Time measurement unit (time_base), first frame position (start_time), Duration ( Duration), the number of frames (nb_frames), and some IP information. Of course, some of this information is not required to be initialized, but Avcodeccontex must be initialized , and is the most important part of initializing Avstream.
These are the more important data structures in FFMpeg. The following generation relationship has a rationale: (- )
Avformatcontext->avstream->avcodeccontext->avcodec
|
Avoutputformat or Avinputformat
Avframe->avpicture....>avpacket
functions in 2.FFMpeg:
FFMpeg SDKA number of initialization and encoding functions are provided. All we have to do is initialize the main data structures correctly, and use the corresponding codec functions correctly and read and write (/ o) operation function. As a holistic code sdkffmpeg There are some of his own standardized use processes. such as function av_register_all (); is one of the first to call " register function he initialized the Span style= "font-family:arial;" >libavcodec," All the and Video file format (format ).
(1). Av_register_all ();
Usage:initialize ibavcoded, and register all codecs and formats
Every project that uses the FFMpeg SDK must call a function. codec and format are registered before they can be used.
(2). Avformatcontext *avformat_alloc_context (void);
Usage:allocate the output media context. is actually initializing the Avformatcontext member data avclass:
(3). void Av_dump_format (avformatcontext *ic, int index, const char *url, int is_output);
Usage: This step fills the Avformatcontext watershed (streams field) with useful information. As a debug diagnostic, we will output this information to the standard error output in its entirety, but you do not use it in the product of an application:
(4). int Avformat_open_input (Avformatcontext **ps, const char *filename, Avinputformat *fmt, Avdictionary **options);
Usage: Open a video file
(5). int Avformat_find_stream_info (Avformatcontext *ic, avdictionary **options);
Usage: Get stream information for a video file
(6). Avcodec *avcodec_find_decoder (enum avcodecid ID);
Usage: Get video encoding format
(7). int Avcodec_open2 (Avcodeccontext *avctx, const AVCODEC *CODEC, avdictionary **options);
Usage: Open an encoded file in an encoded format
(8). int Av_read_frame (Avformatcontext *s, Avpacket *pkt);
Usage: reading from frame packet
(9). int Avcodec_decode_audio4 (Avcodeccontext *avctx, avframe *frame, int *got_frame_ptr, const avpacket *AVPKT);
Usage: decoding sounds
So the decoding process is as follows:
So the decoding process is: Register all format---Initialize avformatcontext-> open a video file----Get the video file stream information----Get the original video stream, get the video stream encoded content, and get the audio stream encoded content- > Get video encoding Format--Get audio encoding format--open an encoded file in an encoded format--read packet-> decode video from frame, decode audio--release packet-> off decoder- > Close Avformatcontext
Decoding process function: Av_register_all ()->avformat_alloc_context ()->avformat_open_input ()->avformat_find_stream_info ( )->avcodec_find_decoder ()->avcodec_open2 ()->av_new_packet ()->av_read_frame ()->avcodec_decode_ Video2 ()-->avcodec_decode_audio4 ()->av_free_packet ()->avcodec_close ()->avformat_close_input ()
0 Basic Learning Video decoding the more important functions and data structures in the FFmpeg