天天看點

ffmpeg結構體以及函數介紹(三)

1 avpacket

typedef

struct avpacket {

    /**

     * presentation timestamp in avstream->time_base units; the time at which

     * the decompressed packet will be presented to the user.

     * can be av_nopts_value if it is not stored in the file.

     * pts must be larger or equal to dts as presentation cannot happen before

     * decompression, unless one wants to view hex dumps. some formats misuse

     * the terms dts and pts/cts to mean something different. such timestamps

     * must be converted to true pts/dts before they are stored in avpacket.

     */

    int64_t pts;

     * decompression timestamp in avstream->time_base units; the time at which

     * the packet is decompressed.

    int64_t dts;

    uint8_t *data;

    int   size;

    int   stream_index;

    int   flags;

int   duration;

.

} avpacket

// avpacket是個很重要的結構,該結構在讀媒體源檔案和寫輸出檔案時都需要用到

// int64_t pts; 顯示時間戳

// int64_t dts; 解碼時間戳

// uint8_t *data; 包資料

// int   size; 包資料長度

// int   stream_index; 包所屬流序号

// int   duration; 時長

// 以上資訊,如果是在讀媒體源檔案那麼avcodec會初始化,如果是輸出檔案,使用者需要對以上資訊指派

2

av_init_packet()

/**

 * initialize optional fields of a packet with default values.

 *

 * @param pkt packet

 */

void av_init_packet(avpacket *pkt);

// 使用預設值初始化avpacket

// 定義avpacket對象後,請使用av_init_packet進行初始化

3

av_free_packet()

 * free a packet.

 * @param pkt packet to free

void av_free_packet(avpacket *pkt);

// 釋放avpacket對象

4

av_read_frame()

 * 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.

 * the returned packet is valid

 * until the next av_read_frame() or until av_close_input_file() and

 * must be freed with av_free_packet. 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 video format

 * has b-frames, so it is better to rely on pkt->dts if you do not

 * decompress the payload.

 * @return 0 if ok, < 0 on error or end of file

int av_read_frame(avformatcontext *s, avpacket *pkt);

// 從輸入源檔案容器中讀取一個avpacket資料包

// 該函數讀出的包并不每次都是有效的,對于讀出的包我們都應該進行相應的解碼(視訊解碼/音頻解碼),

// 在傳回值>=0時,循環調用該函數進行讀取,循環調用之前請調用av_free_packet函數清理avpacket

5

avcodec_decode_video2()

 * decode the video frame of size avpkt->size from avpkt->data into picture.

 * some decoders may support multiple frames in a single avpacket, such

 * decoders would then just decode the first frame.

 * @warning the input buffer must be ff_input_buffer_padding_size larger than

 * the actual read bytes because some optimized bitstream readers read 32 or 64

 * bits at once and could read over the end.

 * @warning the end of the input buffer buf should be set to 0 to ensure that

 * no overreading happens for damaged mpeg streams.

 * @note you might have to align the input buffer avpkt->data.

 * the alignment requirements depend on the cpu: on some cpus it isn't

 * necessary at all, on others it won't work at all if not aligned and on others

 * it will work but it will have an impact on performance.

 * in practice, avpkt->data should have 4 byte alignment at minimum.

 * @note some codecs have a delay between input and output, these need to be

 * fed with avpkt->data=null, avpkt->size=0 at the end to return the remaining frames.

 * @param avctx the codec context

 * @param[out] picture the avframe in which the decoded video frame will be stored.

 *             use avcodec_alloc_frame to get an avframe, the codec will

 *             allocate memory for the actual bitmap.

 *             with default get/release_buffer(), the decoder frees/reuses the bitmap as it sees fit.

 *             with overridden get/release_buffer() (needs codec_cap_dr1) the user decides into what buffer the decoder

 *                   decodes and the decoder tells the user once it does not need the data anymore,

 *                   the user app can at this point free/reuse/keep the memory as it sees fit.

 * @param[in] avpkt the input avpacket containing the input buffer.

 *            you can create such packet with av_init_packet() and by then setting

 *            data and size, some decoders might in addition need other fields like

 *            flags&av_pkt_flag_key. all decoders are designed to use the least

 *            fields possible.

 * @param[in,out] got_picture_ptr zero if no frame could be decompressed, otherwise, it is nonzero.

 * @return on error a negative value is returned, otherwise the number of bytes

 * used or zero if no frame could be decompressed.

int avcodec_decode_video2(avcodeccontext *avctx, avframe *picture,

                         int *got_picture_ptr,

                         avpacket *avpkt);

// 解碼視訊流avpacket

// 使用av_read_frame讀取媒體流後需要進行判斷,如果為視訊流則調用該函數解碼

// 傳回結果<0時失敗,此時程式應該退出檢查原因

// 傳回>=0時正常,假設 讀取包為:avpacket vpacket 傳回值為 int vlen; 每次解碼正常時,對vpacket做

// 如下處理:

//   vpacket.size -= vlen;

//   vpacket.data += vlen;

// 如果 vpacket.size==0,則繼續讀下一流包,否則繼續排程該方法進行解碼,直到vpacket.size==0

// 傳回 got_picture_ptr > 0 時,表示解碼到了avframe *picture,其後可以對picture程序處理

6

avcodec_decode_audio3()

 * decode the audio frame of size avpkt->size from avpkt->data into samples.

 * decoders would then just decode the first frame. in this case,

 * avcodec_decode_audio3 has to be called again with an avpacket that contains

 * the remaining data in order to decode the second frame etc.

 * if no frame

 * could be outputted, frame_size_ptr is zero. otherwise, it is the

 * decompressed frame size in bytes.

 * @warning you must set frame_size_ptr to the allocated size of the

 * output buffer before calling avcodec_decode_audio3().

 * @warning the end of the input buffer avpkt->data should be set to 0 to ensure that

 * @note you might have to align the input buffer avpkt->data and output buffer

 * samples. the alignment requirements depend on the cpu: on some cpus it isn't

 * in practice, avpkt->data should have 4 byte alignment at minimum and

 * samples should be 16 byte aligned unless the cpu doesn't need it

 * (altivec and sse do).

 * @param[out] samples the output buffer, sample type in avctx->sample_fmt

 * @param[in,out] frame_size_ptr the output buffer size in bytes

 *            data and size, some decoders might in addition need other fields.

 *            all decoders are designed to use the least fields possible though.

 * used or zero if no frame data was decompressed (used) from the input avpacket.

int avcodec_decode_audio3(avcodeccontext *avctx, int16_t *samples,

                         int *frame_size_ptr,

                         avpacket *avpkt);

// 解碼音頻流avpacket

// 使用av_read_frame讀取媒體流後需要進行判斷,如果為音頻流則調用該函數解碼

//

繼續閱讀