函数调用结构图:
大致函数说明:
1、avcodec_open2
函数原型:intavcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary**options);
函数介绍:
/**
*Initialize the AVCodecContext to use the given AVCodec. Prior to using this
*function the context has to be allocated with avcodec_alloc_context3().
*
*The functions avcodec_find_decoder_by_name(), avcodec_find_encoder_by_name(),
*avcodec_find_decoder() and avcodec_find_encoder() provide an easy way for
*retrieving a codec.
*
*@warning This function is not thread safe!
*
*@note Always call this function before using decoding routines (such as
*@ref avcodec_decode_video2()).
*
*@code
*avcodec_register_all();
*av_dict_set(&opts, "b", "2.5M", 0);
*codec = avcodec_find_decoder(AV_CODEC_ID_H264);
* if(!codec)
* exit(1);
*
*context = avcodec_alloc_context3(codec);
*
* if(avcodec_open2(context, codec, opts) < 0)
* exit(1);
*@endcode
*
*@param avctx The context to initialize.
*@param codec The codec to open this context for. If a non-NULL codec has been
* previously passed to avcodec_alloc_context3() or
* avcodec_get_context_defaults3() for this context, then this
* parameter MUST be either NULL or equal to the previously passed
* codec.
*@param options A dictionary filled with AVCodecContext and codec-privateoptions.
* On return this object will befilled with options that were not found.
*
*@return zero on success, a negative value on error
*@see avcodec_alloc_context3(), avcodec_find_decoder(), avcodec_find_encoder(),
* av_dict_set(), av_opt_find().
*/
说明:为使用给定的AVCodec而初始化AVCodecContext,调用此函数之前AVCodecContext必须调用avcodec_alloc_context3()分配空间。
2、avcodec_copy_context
函数原型:int avcodec_copy_context(AVCodecContext*dest, const AVCodecContext *src);
函数介绍:
/**
*Copy the settings of the source AVCodecContext into the destination
*AVCodecContext. The resulting destination codec context will be
*unopened, i.e. you are required to call avcodec_open2() before you
*can use this AVCodecContext to decode/encode video/audio data.
*
*@param dest target codec context, should be initialized with
* avcodec_alloc_context3(NULL), but otherwise uninitialized
*@param src source codec context
*@return AVERROR() on error (e.g. memory allocation error), 0 on success
*/
说明:拷贝AVCodecContext,目标AVCodecContext的codec是未被打开的,使用codec之前请调用avcodec_open2()。
3、avformat_write_header
函数原型:int avformat_write_header(AVFormatContext*s, AVDictionary **options);
函数介绍:
/**
*Allocate the stream private data and write the stream header to
* anoutput media file.
*
* @params Media file handle, must be allocated with avformat_alloc_context().
* Its oformat field must be set to the desired output format;
* Its pb field must be set to an already opened AVIOContext.
* @paramoptions An AVDictionary filled withAVFormatContext and muxer-private options.
* On return this parameter willbe destroyed and replaced with a dict containing
* options that were not found.May be NULL.
*
*@return 0 on success, negative AVERROR on failure.
*
*@see av_opt_find, av_dict_set, avio_open, av_oformat_next.
*/
说明:为流特有的数据分配空间,并把流的头信息写入output file。
4、av_read_frame
函数原型:int av_read_frame(AVFormatContext *s,AVPacket *pkt)
函数介绍:
/**
*Return the next frame of a stream.
*This function returns what is stored in the file, and does not validate
*that what is there are valid frames for the decoder. It will split what is
*stored in the file into frames and return one for each call. It will not
*omit invalid data between valid frames so as to give the decoder the maximum
*information possible for decoding.
*
* Ifpkt->buf is NULL, then the packet is valid until the next
*av_read_frame() or until avformat_close_input(). Otherwise the packet
* isvalid indefinitely. In both cases the packet must be freed with
*av_free_packet when it is no longer needed. For video, the packet contains
*exactly one frame. For audio, it contains an integer number of frames if each
*frame has a known fixed size (e.g. PCM or ADPCM data). If the audio frames
*have a variable size (e.g. MPEG audio), then it contains one frame.
*
* pkt->pts,pkt->dts and pkt->duration are always set to correct
*values in AVStream.time_base units (and guessed if the format cannot
*provide them). pkt->pts can be AV_NOPTS_VALUE if the videoformat
*has B-frames, so it is better to rely on pkt->dts if you donot
* decompressthe payload.
*
*@return 0 if OK, < 0 on error or end of file
*/
说明:通过av_read_packet(***),读取一个包,需要说明的是此函数必须是包含整数帧的,不存在半帧的情况,以ts流为例,是读取一个完整的PES包(一个完整pes包包含若干视频或音频es包),读取完毕后,通过av_parser_parse2(***)分析出视频一帧(或音频若干帧),返回,下次进入循环的时候,如果上次的数据没有完全取完,则st = s->cur_st;不会是NULL,即再此进入av_parser_parse2(***)流程,而不是下面的av_read_packet(**)流程,这样就保证了,如果读取一次包含了N帧视频数据(以视频为例),则调用av_read_frame(***)N次都不会去读数据,而是返回第一次读取的数据,直到全部解析完毕。
5、avcodec_encode_video2
函数原型:intavcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt,
const AVFrame *frame, int *got_packet_ptr);
函数介绍:
/**
*Encode a frame of video.
*
*Takes input raw video data from frame and writes the next output packet, if
*available, to avpkt. The output packet does not necessarily contain data for
*the most recent frame, as encoders can delay and reorder input frames
*internally as needed.
*
*@param avctx codec context
*@param avpkt output AVPacket.
* The user can supply an outputbuffer by setting
* avpkt->data andavpkt->size prior to calling the
* function, but if the size ofthe user-provided data is not
* large enough, encoding willfail. All other AVPacket fields
* will be reset by the encoderusing av_init_packet(). If
* avpkt->data is NULL, theencoder will allocate it.
* The encoder will setavpkt->size to the size of the
* output packet. The returneddata (if any) belongs to the
* caller, he is responsible forfreeing it.
*
* If this function fails orproduces no output, avpkt will be
* freed using av_free_packet()(i.e. avpkt->destruct will be
* called to free the usersupplied buffer).
*@param[in] frame AVFrame containing the raw video data to be encoded.
* May be NULL when flushing anencoder that has the
* CODEC_CAP_DELAY capabilityset.
*@param[out] got_packet_ptr This field is set to 1 by libavcodec if the
* output packet isnon-empty, and to 0 if it is
* empty. If thefunction returns an error, the
* packet can beassumed to be invalid, and the
* value of got_packet_ptris undefined and should
* not be used.
*@return 0 on success, negativeerror code on failure
*/
说明:压缩一帧数据,如果允许,把数据写入avpkt里面。注意output 包不是每次都有最近的帧数据的,因为编码器有可能缓存起来。
6、av_packet_rescale_ts
函数原型:voidav_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst);
函数介绍:
/**
*Convert valid timing fields (timestamps / durations) in a packet from one
*timebase to another. Timestamps with unknown values (AV_NOPTS_VALUE) will be
*ignored.
*
*@param pkt packet on which the conversion will be performed
*@param tb_src source timebase, in which the timing fields in pkt are
* expressed
*@param tb_dst destination timebase, to which the timing fields will be
* converted
*/
说明:转换时间戳。
7、av_interleaved_write_frame
函数原型:int av_interleaved_write_frame(AVFormatContext*s, AVPacket *pkt)
函数介绍:
/**
*Write a packet to an output media file ensuring correct interleaving.
*
*This function will buffer the packets internally as needed to make sure the
*packets in the output file are properly interleaved in the order of
*increasing dts. Callers doing their own interleaving should call
*av_write_frame() instead of this function.
*
* @params media file handle
* @parampkt The packet containing the data to be written.
* <br>
* If the packet is reference-counted, this function will take
* ownership of this reference and unreference it later when it sees
* fit.
* The caller must not access the data through this reference after
* this function returns. If the packet is not reference-counted,
* libavformat will make a copy.
* <br>
* This parameter can be NULL (at any time, not just at the end), to
* flush the interleaving queues.
* <br>
* Packet's @ref AVPacket.stream_index "stream_index"field must be
* set to the index of the corresponding stream in @ref
* AVFormatContext.streams"s->streams". It is very strongly
* recommended that timing information (@ref AVPacket.pts "pts",@ref
* AVPacket.dts "dts", @ref AVPacket.duration"duration") is set to
* correct values.
*
*@return 0 on success, a negative AVERROR on error. Libavformat willalways
* take care of freeing the packet, even if this function fails.
*
*@see av_write_frame(), AVFormatContext.max_interleave_delta
*/
说明:把输出数据交错写入输出文件。
8、av_frame_unref
函数原型:void av_frame_unref(AVFrame *frame);
函数介绍:
/**
* Unreferenceall the buffers referenced by frame and reset the frame fields.
*/
说明:解除数据绑定。
9、av_frame_ref
函数原型:int av_frame_ref(AVFrame *dst, const AVFrame *src);
函数介绍:
/**
*Set up a new reference to the data described by the source frame.
*
*Copy frame properties from src to dst and create a new referencefor each
*AVBufferRef from src.
*
* Ifsrc is not reference counted, new buffers are allocated and the data is
*copied.
*
*@return 0 on success, a negative AVERROR on error
*/
说明:从src拷贝到dst,并从src建立新的buffers引用。
10、av_init_packet
函数原型:voidav_init_packet(AVPacket *pkt);
函数介绍:
/**
*Initialize optional fields of a packet with default values.
*
*Note, this does not touch the data and size members, which have to be
*initialized separately.
*
*@param pkt packet
*/
说明:用缺省值初始化pkt。并不会为data申请空间,和为size设置有效值,这些要单独设置值。
11、av_write_trailer
函数原型:int av_write_trailer(AVFormatContext *s);
函数介绍:
/**
*Write the stream trailer to an output media file and free the
*file private data.
*
*May only be called after a successful call to avformat_write_header.
*
* @params media file handle
*@return 0 if OK, AVERROR_xxx on error
*/
说明:循环调用interleave_packet()以及write_packet(),将还未输出的AVPacket输出出来。调用AVOutputFormat的write_trailer(),输出文件尾。最后做一些释放动作。
12、interleave_packet
函数原型:static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in,int flush);
函数介绍:
/**
*Interleave an AVPacket correctly so it can be muxed.
* @paramout the interleaved packet will be output here
* @paramin the input packet
* @paramflush 1 if no further packets are available as input and all
* remaining packets should be output
*@return 1 if a packet was output, 0 if no packet could be output,
* < 0 if an error occurred
*/
说明:把缓冲中的数据输出出来。
13、write_packet
函数原型:static int write_packet(AVFormatContext *s, AVPacket *pkt)
函数介绍:
/**
*Make timestamps non negative, move side data from payload tointernal struct, call muxer, and restore
* sidedata.
*
* FIXME: this function should NEVER getundefined pts/dts beside when the
*AVFMT_NOTIMESTAMPS is set.
*Those additional safety checks should be dropped once the correct checks
*are set in the callers.
*/
说明:给时间戳赋值,调用av_packet_split_side_data处理packet,然后调用s->oformat的write_uncoded_frame或者write_packet写packet。
14、avcodec_close
函数原型:int avcodec_close(AVCodecContext *avctx);
函数介绍:
/**
*Close a given AVCodecContext and free all the data associated with it
*(but not the AVCodecContext itself).
*
*Calling this function on an AVCodecContext that hasn't been opened will free
*the codec-specific data allocated in avcodec_alloc_context3() /
*avcodec_get_context_defaults3() with a non-NULL codec. Subsequent calls will
* donothing.
*/
说明:关闭AVCodecContext并释放与之相关的数据(除了AVCodecContext自己)。