ffmpeg函数介绍

FFmpeg API 之 libavcodec库 libavformat 库负责封装和解封装,而 libavcodec 则用于解码和编码。 类型 AVPacket 表示编码后的数据,其中包含一个或多个编码后的帧数据。类型 AVFrame 表示解码后,或者说原始的帧数据。编码和解码在某种程度来说,就是两者之间的互相转换。 1、编解码概述 FFmpeg 提供的 encode/decode API 有如下四个函数 avcodec_send_packet () / avcodec_receive_frame () / avcodec_send_fram. 阅读详情

本文对在使用ffmpeg进行音视频编解码时使用到的一些函数做一个简单介绍,我当前使用的ffmpeg版本为:0.8.5,因为本人发现在不同的版本中,有些函数名称会有点小改动,所以在此有必要说明下ffmpeg的版本号。

ffmpeg本人也是刚接触,本文将采用累加的方法逐个介绍我使用到的函数,如有不妥之处,还望谅解!

 

头文件引入方法:

extern "C" {

#include "libavcodec/avcodec.h"

#include "libavformat/avformat.h"

#include "libavutil/avutil.h"

#include "libavutil/mem.h"

#include "libavutil/fifo.h"

#include "libswscale/swscale.h"

};

 

1 avcodec_init()

/**

 * Initialize libavcodec.

 * If called more than once, does nothing.

 *

 * @warning This function must be called before any other libavcodec

 * function.

 *

 * @warning This function is not thread-safe.

 */

void avcodec_init(void);

// 初始化libavcodec,一般最先调用该函数

// 引入头文件: #include "libavcodec/avcodec.h"

// 实现在: \ffmpeg\libavcodec\utils.c

// 该函数必须在调用libavcodec里的其它函数前调用,一般在程序启动或模块初始化时调用,如果你调用了多次也无所谓,因为后面的调用不会做任何事情.从函数的实现里你可以发现,代码中对多次调用进行了控制.

// 该函数是非线程安全的

 

2 av_register_all()

/**

 * Initialize libavformat and register all the muxers, demuxers and

 * protocols. If you do not call this function, then you can select

 * exactly which formats you want to support.

 *

 * @see av_register_input_format()

 * @see av_register_output_format()

 * @see av_register_protocol()

 */

void av_register_all(void);

// 初始化 libavformat和注册所有的muxers、demuxers和protocols,

// 一般在调用avcodec_init后调用该方法

// 引入头文件:#include "libavformat/avformat.h"

// 实现在:\ffmpeg\libavformat\allformats.c

// 其中会调用avcodec_register_all()注册多种音视频格式的编解码器,并注册各种文件的编解复用器

// 当然,你也可以不调用该函数,而通过选择调用特定的方法来提供支持

 

3 avformat_alloc_context()

/**

 * Allocate an AVFormatContext.

 * avformat_free_context() can be used to free the context and everything

 * allocated by the framework within it.

 */

AVFormatContext *avformat_alloc_context(void);

// 分配一个AVFormatContext结构

// 引入头文件:#include "libavformat/avformat.h"

// 实现在:\ffmpeg\libavformat\options.c

// 其中负责申请一个AVFormatContext结构的内存,并进行简单初始化

// avformat_free_context()可以用来释放该结构里的所有东西以及该结构本身

// 也是就说使用 avformat_alloc_context()分配的结构,需要使用avformat_free_context()来释放

// 有些版本中函数名可能为: av_alloc_format_context();

 

4 avformat_free_context()

/**

 * Free an AVFormatContext and all its streams.

 * @param s context to free

 */

void avformat_free_context(AVFormatContext *s);

// 释放一个AVFormatContext结构

// 引入头文件:#include "libavformat/avformat.h"

// 实现在:\ffmpeg\libavformat\utils.c

// 使用 avformat_alloc_context()分配的结构,采用该函数进行释放,除释放AVFormatContext结构本身内存之外,AVFormatContext中指针所指向的内存也会一并释放

// 有些版本中函数名猜测可能为: av_free_format_context();

 

5 AVFormatContext 结构

/**

 * Format I/O context.

 * New fields can be added to the end with minor version bumps.

 * Removal, reordering and changes to existing fields require a major

 * version bump.

 * sizeof(AVFormatContext) must not be used outside libav*.

 */

typedef struct AVFormatContext {

    struct AVInputFormat *iformat;

    struct AVOutputFormat *oformat;

    AVIOContext *pb;

    unsigned int nb_streams;

    AVStream **streams;

    char filename[1024]; /**< input or output filename */

    ....

} AVFormatContext;

// AVFormatContext在FFMpeg里是一个非常重要的的结构,是其它输入、输出相关信息的一个容器

// 引入头文件:#include "libavformat/avformat.h"

// 以上只列出了其中的部分成员

// 作为输入容器时 struct AVInputFormat *iformat; 不能为空, 其中包含了输入文件的音视频流信息,程序从输入容器从读出音视频包进行解码处理

// 作为输出容器时 struct AVOutputFormat *oformat; 不能为空, 程序把编码好的音视频包写入到输出容器中

// AVIOContext *pb: I/O上下文,通过对该变量赋值可以改变输入源或输出目的

// unsigned int nb_streams; 音视频流数量

// AVStream **streams; 音视频流

 

6 AVIOContext 结构

/**

 * Bytestream IO Context.

 * New fields can be added to the end with minor version bumps.

 * Removal, reordering and changes to existing fields require a major

 * version bump.

 * sizeof(AVIOContext) must not be used outside libav*.

 *

 * @note None of the function pointers in AVIOContext should be called

 *       directly, they should only be set by the client application

 *       when implementing custom I/O. Normally these are set to the

 *       function pointers specified in avio_alloc_context()

 */

typedef struct {

    unsigned char *buffer;  /**< Start of the buffer. */

    int buffer_size;        /**< Maximum buffer size */

    unsigned char *buf_ptr; /**< Current position in the buffer */

    unsigned char *buf_end; /**< End of the data, may be less than

                                 buffer+buffer_size if the read function returned

                                 less data than requested, e.g. for streams where

                                 no more data has been received yet. */

    void *opaque;           /**< A private pointer, passed to the read/write/seek/...

                                 functions. */

    int (*read_packet)(void *opaque, uint8_t *buf,int buf_size);

    int (*write_packet)(void *opaque, uint8_t *buf,int buf_size);

    int64_t (*seek)(void *opaque, int64_t offset,int whence);

    int64_t pos;            /**< position in the file of the current buffer */

    int must_flush;         /**< true if the next seek should flush */

    int eof_reached;        /**< true if eof reached */

    int write_flag;         /**< true if open for writing */

#if FF_API_OLD_AVIO

    attribute_deprecated int is_streamed;

#endif

    int max_packet_size;

    unsigned long checksum;

    unsigned char *checksum_ptr;

    unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);

    int error;              /**< contains the error code or 0 if no error happened */

    /**

     * Pause or resume playback for network streaming protocols - e.g. MMS.

     */

    int (*read_pause)(void *opaque,int pause);

    /**

     * Seek to a given timestamp in stream with the specified stream_index.

     * Needed for some network streaming protocols which don't support seeking

     * to byte position.

     */

    int64_t (*read_seek)(void *opaque,int stream_index,

                         int64_t timestamp, int flags);

    /**

     * A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.

     */

    int seekable;

} AVIOContext;

// 字节流 I/O 上下文

// 在结构的尾部增加变量可以减少版本冲突

// 移除、排序和修改已经存在的变量将会导致较大的版本冲突

// sizeof(AVIOContext)在libav*.外部不可使用

// AVIOContext里的函数指针不能直接调用,通常使用avio_alloc_context()函数来设置其中的函数指针

// unsigned char *buffer: 缓存的起始指针

// int buffer_size: 缓存的最大值

// void *opaque: 在回调函数中使用的指针

// int (*read_packet)(void *opaque, uint8_t *buf,int buf_size): 读文件回调方法

// int (*write_packet)(void *opaque, uint8_t *buf,int buf_size): 写文件回调方法

// int64_t (*seek)(void *opaque, int64_t offset,int whence): seek文件回调方法

 

7 avio_alloc_context()

/**

 * Allocate and initialize an AVIOContext for buffered I/O. It must be later

 * freed with av_free().

 *

 * @param buffer Memory block for input/output operations via AVIOContext.

 *        The buffer must be allocated with av_malloc() and friends.

 * @param buffer_size The buffer size is very important for performance.

 *        For protocols with fixed blocksize it should be set to this blocksize.

 *        For others a typical size is a cache page, e.g. 4kb.

 * @param write_flag Set to 1 if the buffer should be writable, 0 otherwise.

 * @param opaque An opaque pointer to user-specific data.

 * @param read_packet  A function for refilling the buffer, may be NULL.

 * @param write_packet A function for writing the buffer contents, may be NULL.

 * @param seek A function for seeking to specified byte position, may be NULL.

 *

 * @return Allocated AVIOContext or NULL on failure.

 */

AVIOContext *avio_alloc_context(

                  unsigned char *buffer,

                  int buffer_size,

                  int write_flag,

                  void *opaque,

                  int (*read_packet)(void *opaque, uint8_t *buf,int buf_size),

                  int (*write_packet)(void *opaque, uint8_t *buf,int buf_size),

                  int64_t (*seek)(void *opaque, int64_t offset,int whence));

// 为I/0缓存申请并初始化一个AVIOContext结构,结束使用时必须使用av_free()进行释放

// unsigned char *buffer: 输入/输出缓存内存块,必须是使用av_malloc()分配的

// int buffer_size: 缓存大小是非常重要的

// int write_flag: 如果缓存为可写则设置为1,否则设置为0

// void *opaque: 指针,用于回调时使用

// int (*read_packet): 读包函数指针

// int (*write_packet): 写包函数指针

// int64_t (*seek): seek文件函数指针

8 av_open_input_file()

/**

 * Open a media file as input. The codecs are not opened. Only the file

 * header (if present) is read.

 *

 * @param ic_ptr The opened media file handle is put here.

 * @param filename filename to open

 * @param fmt If non-NULL, force the file format to use.

 * @param buf_size optional buffer size (zero if default is OK)

 * @param ap Additional parameters needed when opening the file

 *           (NULL if default).

 * @return 0 if OK, AVERROR_xxx otherwise

 *

 * @deprecated use avformat_open_input instead.

 */

attribute_deprecated int av_open_input_file(AVFormatContext **ic_ptr,const char *filename,

                       AVInputFormat *fmt,

                       int buf_size,

                       AVFormatParameters *ap);

// 以输入方式打开一个媒体文件,也即源文件,codecs并没有打开,只读取了文件的头信息.

// 引入头文件:#include "libavformat/avformat.h"

// AVFormatContext **ic_ptr 输入文件容器

//const char *filename 输入文件名,全路径,并且保证文件存在

// AVInputFormat *fmt 输入文件格式,填NULL即可

//int buf_size,缓冲区大小,直接填0即可

// AVFormatParameters *ap, 格式参数,添NULL即可

// 成功返回0,其它失败

// 不赞成使用 avformat_open_input 代替

 

9 av_close_input_file()

/**

 * @deprecated use avformat_close_input()

 * Close a media file (but not its codecs).

 * @param s media file handle

 */

void av_close_input_file(AVFormatContext *s);

// 关闭使用avformat_close_input()打开的输入文件容器,但并不关系它的codecs

// 引入头文件:#include "libavformat/avformat.h"

// 使用av_open_input_file 打开的文件容器,可以使用该函数关闭

// 使用 av_close_input_file 关闭后,就不再需要使用avformat_free_context 进行释放了

 

10 av_find_stream_info()

/**

 * Read packets of a media file to get stream information. This

 * is useful for file formats with no headers such as MPEG. This

 * function also computes the real framerate in case of MPEG-2 repeat

 * frame mode.

 * The logical file position is not changed by this function;

 * examined packets may be buffered for later processing.

 *

 * @param ic media file handle

 * @return >=0 if OK, AVERROR_xxx on error

 * @todo Let the user decide somehow what information is needed so that

 *       we do not waste time getting stuff the user does not need.

 */

int av_find_stream_info(AVFormatContext *ic);

// 通过读取媒体文件的中的包来获取媒体文件中的流信息,对于没有头信息的文件如(mpeg)是非常有用的,

// 该函数通常重算类似mpeg-2帧模式的真实帧率,该函数并未改变逻辑文件的position.

// 引入头文件:#include "libavformat/avformat.h"

// 也就是把媒体文件中的音视频流等信息读出来,保存在容器中,以便解码时使用

// 返回>=0时成功,否则失败

/***********************************************************/

1 avcodec_find_decoder()

/**

 * Find a registered decoder with a matching codec ID.

 *

 * @param id CodecID of the requested decoder

 * @return A decoder if one was found, NULL otherwise.

 */

AVCodec *avcodec_find_decoder(enum CodecID id);

// 通过code ID查找一个已经注册的音视频解码器

// 引入 #include "libavcodec/avcodec.h"

// 实现在: \ffmpeg\libavcodec\utils.c

// 查找解码器之前,必须先调用av_register_all注册所有支持的解码器

// 查找成功返回解码器指针,否则返回NULL

// 音视频解码器保存在一个链表中,查找过程中,函数从头到尾遍历链表,通过比较解码器的ID来查找

 

2 avcodec_find_decoder_by_name()

/**

 * Find a registered decoder with the specified name.

 *

 * @param name name of the requested decoder

 * @return A decoder if one was found, NULL otherwise.

 */

AVCodec *avcodec_find_decoder_by_name(constchar *name);

// 通过一个指定的名称查找一个已经注册的音视频解码器

// 引入 #include "libavcodec/avcodec.h"

// 实现在: \ffmpeg\libavcodec\utils.c

// 查找解码器之前,必须先调用av_register_all注册所有支持的解码器

// 查找成功返回解码器指针,否则返回NULL

// 音视频解码器保存在一个链表中,查找过程中,函数从头到尾遍历链表,通过比较解码器的name来查找

 

3 avcodec_find_encoder()

/**

 * Find a registered encoder with a matching codec ID.

 *

 * @param id CodecID of the requested encoder

 * @return An encoder if one was found, NULL otherwise.

 */

AVCodec *avcodec_find_encoder(enum CodecID id);

// 通过code ID查找一个已经注册的音视频编码器

// 引入 #include "libavcodec/avcodec.h"

// 实现在: \ffmpeg\libavcodec\utils.c

// 查找编码器之前,必须先调用av_register_all注册所有支持的编码器

// 查找成功返回编码器指针,否则返回NULL

// 音视频编码器保存在一个链表中,查找过程中,函数从头到尾遍历链表,通过比较编码器的ID来查找

 

4 avcodec_find_encoder_by_name()

/**

 * Find a registered encoder with the specified name.

 *

 * @param name name of the requested encoder

 * @return An encoder if one was found, NULL otherwise.

 */

AVCodec *avcodec_find_encoder_by_name(constchar *name);

// 通过一个指定的名称查找一个已经注册的音视频编码器

// 引入 #include "libavcodec/avcodec.h"

// 实现在: \ffmpeg\libavcodec\utils.c

// 查找编码器之前,必须先调用av_register_all注册所有支持的编码器

// 查找成功返回编码器指针,否则返回NULL

// 音视频编码器保存在一个链表中,查找过程中,函数从头到尾遍历链表,通过比较编码器的名称来查找

 

5 avcodec_open()

/**

 * Initialize the AVCodecContext to use the given AVCodec. Prior to using this

 * function the context has to be allocated.

 *

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

 *

 * @code

 * avcodec_register_all();

 * codec = avcodec_find_decoder(CODEC_ID_H264);

 * if (!codec)

 *     exit(1);

 *

 * context = avcodec_alloc_context();

 *

 * if (avcodec_open(context, codec) < 0)

 *     exit(1);

 * @endcode

 *

 * @param avctx The context which will be set up to use the given codec.

 * @param codec The codec to use within the context.

 * @return zero on success, a negative value on error

 * @see avcodec_alloc_context, avcodec_find_decoder, avcodec_find_encoder, avcodec_close

 */

int avcodec_open(AVCodecContext *avctx, AVCodec *codec);

// 使用给定的AVCodec初始化AVCodecContext

// 引入#include "libavcodec/avcodec.h"

// 方法: avcodec_find_decoder_by_name(), avcodec_find_encoder_by_name(), avcodec_find_decoder() and avcodec_find_encoder() 提供了快速获取一个codec的途径

// 该方法在编码和解码时都会用到

// 返回0时成功,打开作为输出时,参数设置不对的话,调用会失败

 
6 av_guess_format()

/**

 * Return the output format in the list of registered output formats

 * which best matches the provided parameters, or return NULL if

 * there is no match.

 *

 * @param short_name if non-NULL checks if short_name matches with the

 * names of the registered formats

 * @param filename if non-NULL checks if filename terminates with the

 * extensions of the registered formats

 * @param mime_type if non-NULL checks if mime_type matches with the

 * MIME type of the registered formats

 */

AVOutputFormat *av_guess_format(constchar *short_name,

                                constchar *filename,

                                constchar *mime_type);

// 返回一个已经注册的最合适的输出格式

// 引入#include "libavformat/avformat.h"

// 可以通过 const char *short_name 获取,如"mpeg"

// 也可以通过 const char *filename 获取,如"E:\a.mp4"

 

7 av_new_stream()

/**

 * Add a new stream to a media file.

 *

 * Can only be called in the read_header() function. If the flag

 * AVFMTCTX_NOHEADER is in the format context, then new streams

 * can be added in read_packet too.

 *

 * @param s media file handle

 * @param id file-format-dependent stream ID

 */

AVStream *av_new_stream(AVFormatContext *s, int id);

// 为媒体文件添加一个流,一般为作为输出的媒体文件容器添加音视频流

// 引入 #include "libavformat/avformat.h"

// 再打开源文件时用户一般不需要直接调用该方法

 
8 dump_format()

#if FF_API_DUMP_FORMAT

/**

 * @deprecated Deprecated in favor of av_dump_format().

 */

attribute_deprecated void dump_format(AVFormatContext *ic,

                                      int index,

                                      constchar *url,

                                      int is_output);

#endif

// 该函数的作用就是检查下初始化过程中设置的参数是否符合规范
// 有些版本中为 av_dump_format
 
9 av_set_parameters()

#if FF_API_FORMAT_PARAMETERS

/**

 * @deprecated pass the options to avformat_write_header directly.

 */

attribute_deprecated int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);

#endif
// 设置初始化参数
// 不赞成跳过该方法,直接调用 avformat_write_header/av_write_header
 
10 av_write_header()

#if FF_API_FORMAT_PARAMETERS

/**

 * Allocate the stream private data and write the stream header to an

 * output media file.

 * @note: this sets stream time-bases, if possible to stream->codec->time_base

 * but for some formats it might also be some other time base

 *

 * @param s media file handle

 * @return 0 if OK, AVERROR_xxx on error

 *

 * @deprecated use avformat_write_header.

 */

attribute_deprecated int av_write_header(AVFormatContext *s);

#endif

// 把流头信息写入到媒体文件中
// 返回0成功
 

/**********************************************************/

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.

     * Can be AV_NOPTS_VALUE if it is not stored in the file.

     */

    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.

 * Some decoders may support multiple frames in a single AVPacket, such

 * 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 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 avpkt->data 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 and output buffer

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

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

 * (AltiVec and SSE do).

 *

 * @param avctx the codec context

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

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

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

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

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

 * 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读取媒体流后需要进行判断,如果为音频流则调用该函数解码
// 返回结果<0时失败,此时程序应该退出检查原因
// 返回>=0时正常,假设 读取包为:AVPacket vPacket 返回值为 int vLen; 每次解码正常时,对vPacket做
// 如下处理:
//   vPacket.size -= vLen;
//   vPacket.data += vLen;
// 如果 vPacket.size==0,则继续读下一流包,否则继续调度该方法进行解码,直到vPacket.size==0
FFmpeg实现音视频同步的精准片段拼接 音视频开发过程中,经常会遇到多个片段(或者称之为“分镜头”)拼接的问题。下面将列举若干实例,来分别说明。本文中列举的例子必须满足前提条件:1、每个片段自身是音视频同步的,只是在拼接后产生了不同步的问题;2、每个片段的视频流都具有相同的:画面宽度、画面高度、像素宽高比、帧率和时间基;/3、每个片段的音频流都具有相同的:采样率、时间基;/4、每个片段都具有视频流和音频流。直接concatFFmpeg为我们提供了concat滤镜,该滤镜不仅可以拼接视频,还可以拼接音频。就像这样:这里主要关注字段。 阅读详情

相关推荐

FFmpeg —— FFmpeg常用API函数介绍

目录 1.常用API 1.1 通用API 1.1.1 av_register_all()——弃用 1.1.2 内存的分配和释放(av_malloc()、av_free()等) 1.1.3 常见结构体的初始化和销毁(AVFormatContext、AVFrame) 1.1.4 avio_open2() 1.1.5 avcodec_find_encod...

天空的博客 2万+

av_read_frame获取到AVPacket数据包AV_INPUT_BUFFER_PADDING_SIZE值和输入文件AVCodecContext的extradata值研究

AVPacket pkt; av_init_packet(&pkt); // 初始化pkt pkt.data = NULL; pkt.size = 0; //fmt_ctx为输入文件的AVFormatContext指针 av_read_frame(fmt_ctx, &pkt); if (pkt.stream_index == video_idx) { // 为包数...

weixin_42717961的博客 1858

ffmpeg 注册函数

看完了av_log_set_flags(AV_LOG_SKIP_REPEATED);  parse_loglevel(argc, argv, options);这两个函数接下来就是一系列的注册函数 1. avcodec_register_all(); 该函数在allcodecs.c ,allcodecs.c在libavcodec包中     该函数的注释为: /**  * Registe

cugkuan的专栏 1973

FFmpeg库常用函数介绍(三)

作用:将二进制数据作为值设置给对象。obj:要设置选项的对象;name:选项名称;val:要设置的值;size:值的大小;search_flags:搜索选项的方式。例如,AV_OPT_SEARCH_CHILDREN指的是首先搜索给定对象的子对象。返回值:负值表示失败。

m0_51496461的博客 1668

(三)ffmpeg 解码流程以及函数介绍

在调用该函数时,需要先确定源图像和目标图像的尺寸、像素格式以及是否需要进行某些特定的处理(例如缩放、裁剪、旋转等),然后根据这些信息来设置相应的参数。这个函数是FFmpeg多媒体处理流程的起点,通常用于设置后续的解码、编码或其他处理步骤。:可选参数,指定与要查找的流相关的流的索引号。例如,如果要查找的视频流与音频流相关联,可以设置此参数为音频流的索引号。这些选项可以影响编解码器的打开过程或被用来设置特定格式的私有选项。:如果多个资源之间存在依赖关系,应按照正确的顺序释放资源,以避免未定义的行为。

m0_67254672的博客 2797

【FFmpeg】ffmpeg中函数返回的错误码:AVERROR及AVERROR_*

1、AVERROR FFmpeg的错误码大部分使用的PIOSIX标准中错误码的负值。 AVERROR定义在文件 FFmpeg-n4.2.1/libavutil/error.h 中 #define AVERROR(e) (-(e)) // Returns a negative error code from a POSIX error code, to return from library functions. //FFmpeg库的错误码是POSIX错误码的负值。 2、av_strerror 将错误

郭老二 7627

ffmpeg 函数介绍

本文对在使用ffmpeg进行音视频编解码时使用到的一些函数做一个简单介绍,我当前使用的ffmpeg版本为:0.8.5,因为本人发现在不同的版本中,有些函数名称会有点小改动,所以在此有必要说明下ffmpeg的版本号。     ffmpeg本人也是刚接触,本文将采用累加的方法逐个介绍我使用到的函数,如有不妥之处,还望谅解!     头文件引入方法: extern "C" { #inc

u011244446的专栏 570

ffmpeg函数简介(封装格式相关)

读取一个包(packet),即从文件或流中读取一帧音视频数据。创建的 AVFormatContext,那么用它来释放。从媒体文件中读取流信息,比如视频流、音频流、字幕流等。,包含了解码器、流信息、文件路径、数据缓冲等信息。几乎所有操作媒体文件的函数都围绕这个结构体展开。获取流信息,比如帧率、分辨率、码率、时长等。这个函数更灵活,允许设置查找时间戳范围。但通常我们更常用的是。(即打开文件并准备读取媒体信息)。打开一个输入媒体文件,并填充。关闭输入文件并释放相关资源。,适合更复杂的跳转需求。

m0_49476241的博客 1829

ffmpeg结构体以及函数介绍

转自:http://blog.chinaunix.net/uid-20718335-id-3024436.html 本文对在使用ffmpeg进行音视频编解码时使用到的一些函数做一个简单介绍,我当前使用的ffmpeg版本为:0.8.5,因为本人发现在不同的版本中,有些函数名称会有点小改动,所以在此有必要说明下ffmpeg的版本号。     ffmpeg本人也是刚接触,本文将采用累加的方法逐个介绍

liuhongxian的专栏 2379

STM32F4串口接收ATGM336H GPS数据,手把手教你解析NMEA协议并转换经纬度格式

本文详细介绍了如何使用STM32F4开发板通过串口接收ATGM336H GPS模块的NMEA协议数据,并解析GNRMC语句获取经纬度信息。内容涵盖硬件连接、NMEA协议深度解析、数据校验实现以及经纬度格式转换的数学原理与代码实现,为嵌入式开发者提供完整的GPS数据处理解决方案。

weixin_42522575的博客 505

springboot源码 红色J_通达信副图指标源码之,出手就赢

今天跟大家分享一个通达信副图指标源码,本指标没有未来函数,因为本指标没有卖点提示,所以本指标需配合主图指标使用效果会更好,简单易懂,很容易明白,所以就不过多介绍使用方法了,以下是指标的贴图。风险提示!股市有风险买卖需谨慎,本指标只做参考建议,不做买卖交易结果,自己要对自己交易结果的盈亏负责,以下是指标的源码。VAR2:=REF(LOW,1);VAR3:=SMA(ABS(LOW-VAR2),3,1)...

weixin_39859909的博客 2863

ffmpeg函数使用

一. av_guess_format()函数 原型 AVOutputFormat *av_guess_format(const char *short_name, const char *filename, const char *mime_type); av_guess_format中支持的short_name格式可以通过下面命令获取 [root@node_94 cmake-

tttoooz的博客 828

华为数通---配置风暴控制案例

风暴控制可以通过阻塞报文或关闭端口来阻断广播、未知组播和未知单播报文的流量。

2301_77081516的博客 1633

FFmpeg库常用函数介绍(一)

作用:打开媒体文件并获取媒体文件信息,可以是本地文件,也可以时网络流。ps:输入文件的AVFormatContext,会将获取的信息填充进去;url:流地址,可以是本地文件、RTMP、RTP协议流地址;fmt:一般为NULL,表示自动检测流格式,否则会使用提供的值作为流格式;options:一般为NULL;返回值:0表示成功,负值表示失败。注意:需要调用avformat_close_input关闭AVFormatContext。

m0_51496461的博客 2449

YOLOv11改进 | 引入SFSConv空间频率选择卷积(含SFSC3k2二次创新),空间频率特征协同作用,检测性能全面提升

本文提出在YOLOv11中引入SFSConv空间频率选择卷积模块,通过显式分离和动态融合空间域与频率域特征,解决传统卷积对多尺度目标检测的局限性。SFSConv包含空间频率分离、动态权重融合和协同特征重建三个核心机制,并进一步创新提出SFSC3k2结构,通过双路频率分支增强特征协同作用。该改进显著提升了YOLOv11在遥感、医学等多尺度复杂场景下的检测性能,实验显示在微小目标检测任务中mAP提升5%-10%。代码实现展示了SFSConv模块的核心逻辑及其在PyTorch中的高效实现。

走向CTO的路上... 772

PrimeTime low power-SMVA分析(4)

这些功能只有在将timing_enable_cross_voltage_domain_analysis变量设置为true时才能使用。DVFS场景与在分布式多场景分析(DMSA)中分析的运行条件场景无关。- check_timing 和 report_exceptions 命令通过将检查或报告应用于所有可能的场景和电压组合来支持SMVA。- 相位锁定环生成的时钟(create_generated_clock -pll_feedback 和 -pll_output)在多个场景的情况下,返回最差情况的值。

QRBQ 650

ffmpeg重要函数和结构体整理

参考:https://blog.csdn.net/leixiaohua1020/article/details/14215369 自己学习记录,方便以后查看。 ======================================================================================================================= ffmpeg关键结构体类型: 1)解协议 AVIOContext,URLProtocol,URLCo.

时间流浪者 672

从入门到精通!Arduino 全栈开发深度指南

本文围绕 Arduino 开发撰写全攻略,从基础入门到高级应用层层递进。开篇介绍 Arduino 概念、优势及硬件家族,引导读者完成开发环境搭建与首个 LED 闪烁项目;深入讲解编程语法、I/O 控制及通信协议;详细阐述传感器、模块应用与调试优化技巧;最后以自定义库开发、低功耗设计等高级内容收尾,并给出综合项目案例与产品化思路。无论新手入门,还是开发者进阶,均可从中系统学习 Arduino 知识,掌握从理论到实践的全流程开发技能。

邓邓子的博客 2769

ffmpeg函数、结构体介绍

函数或结构体目录1 avcodec_init()函数2 av_register_all()函数3 avformat_alloc_context()函数4 avformat_free_context()函数5 AVFormatContext 结构体6 AVIOContext 结构体7 avio_alloc_context()函数8 av_open_input_file()函数9 av_close_input_file()函数10 av_find_stream_info()函数11 avcodec_find_d

weixin_42887343的博客 998

Python之ae-toolbox包语法、参数和实际应用案例

ae-toolbox(全称 after-effects-toolbox)是 Python 生态中专门用于 Adobe After Effects(AE)自动化操作 的工具包,基于 AE 的 ExtendScript(JSX)桥接技术,实现 Python 与 AE 的跨语言通信。

王国平的博客 1181

杰理接入第三方升级协议OTA教程

本教程介绍了使用dual_bank_updata_api.h接口接入第三方协议进行固件升级的方法。主要内容包括:INI配置要求(需启用双备份并强制4K对齐)、7个核心API接口功能说明及参数详解(初始化、空间检查、数据写入、校验等)、标准升级流程步骤(初始化→检查→写入→校验→引导→清理)。特别强调了4K对齐配置的重要性以避免数据丢失,并提供了被动升级流程图。该方案支持用户自定义协议接入,需严格按照流程调用接口完成升级。

qq_42014563的博客 1438
上一篇: RPC 编程
下一篇: FFMpeg介绍
lp2007
博客等级 码龄20年 15粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值