FFmpeg Structure and function introduction

Source: Internet
Author: User
Tags deprecated error code int size require versions
Turn from: http://blog.chinaunix.net/uid-20718335-id-3024436.html This article is a brief introduction to some of the functions used in audio and video codec using FFmpeg, and the ffmpeg version I am currently using is     : 0.8.5, because I found in different versions, some function names will be a little change, so it is necessary to explain the FFmpeg version number.     FFmpeg himself is also just contact, this article will use the cumulative method to describe the function I use, if there is something wrong, but also hope understanding. Header File Introduction method:

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 is called before any other libavcodec

* function.

*

* @warning This function was not thread-safe.

*/

void Avcodec_init (void);

Initializes the LIBAVCODEC, which is typically the first call to the function

Introduction of header file: #include "libavcodec/avcodec.h"

Implementation in: \FFMPEG\LIBAVCODEC\UTILS.C

The function must be called before other functions in the call Libavcodec, typically called when the program starts or when the module is initialized, if you call it multiple times, because the subsequent call will not do anything. From the implementation of the function you can see that the code has control over multiple calls.

The function is non-thread safe

2 Av_register_all ()

/**

* Initialize Libavformat and register all the muxers, Demuxers and

* Protocols. If You don't call the This function and then you can select

* Exactly which formats want to support.

*

* @see Av_register_input_format ()

* @see Av_register_output_format ()

* @see Av_register_protocol ()

*/

void Av_register_all (void);

Initialize the Libavformat and register all muxers, Demuxers, and protocols,

This method is typically called after calling Avcodec_init

Introduction of header file: #include "libavformat/avformat.h"

Implementation in: \FFMPEG\LIBAVFORMAT\ALLFORMATS.C

It calls Avcodec_register_all () to register codecs for multiple audio and video formats, and to register various file codecs

Of course, you can also do this without calling the function, but by choosing to invoke a specific method to provide support

3 Avformat_alloc_context ()

/**

* Allocate an Avformatcontext.

* Avformat_free_context () can used to free the context and everything

* Allocated by the framework within it.

*/

Avformatcontext *avformat_alloc_context (void);

Assigning a AVFORMATCONTEXT structure

Introduction of header file: #include "libavformat/avformat.h"

Implementation in: \FFMPEG\LIBAVFORMAT\OPTIONS.C

It is responsible for requesting a avformatcontext structure of memory, and for simple initialization

Avformat_free_context () can be used to release everything in the structure and the structure itself.

It is also said that using the Avformat_alloc_context () allocation structure, you need to use Avformat_free_context () to release

In some versions, the function name may be: Av_alloc_format_context ();

4 Avformat_free_context ()

/**

* Free a avformatcontext and all its streams.

* @param s context to free

*/

void Avformat_free_context (Avformatcontext *s);

Releasing a AVFORMATCONTEXT structure

Introduction of header file: #include "libavformat/avformat.h"

Implementation in: \FFMPEG\LIBAVFORMAT\UTILS.C

Using the structure allocated by Avformat_alloc_context (), the function is released, and the memory pointed to by the pointer in the Avformatcontext is released in addition to the AVFORMATCONTEXT structure itself.

Some versions of the function name guess may be: av_free_format_context ();

5 Avformatcontext Structure

/**

* Format I/o context.

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

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

* Version bump.

* sizeof (AVFORMATCONTEXT) must not being 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 is a very important structure in ffmpeg, which is a container for other input and output related information.

Introduction of header file: #include "libavformat/avformat.h"

Only some of these members are listed above

As input container, the struct Avinputformat *iformat; cannot be empty, it contains the audio and video stream information of the input file, the program is decoded from the input container from the readout audio and video packet

As the output container, the struct Avoutputformat *oformat; cannot be null, the program writes encoded audio and video packets to the output container

Aviocontext *PB:I/O Context, you can change the input source or output purpose by assigning a value to the variable

unsigned int nb_streams; Number of audio and video streams

Avstream **streams; Audio and video streaming

6 Aviocontext Structure

/**

* ByteStream IO Context.

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

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

* Version bump.

* sizeof (AVIOCONTEXT) must not being used outside libav*.

*

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

* Directly, they should only is set by the client application

* When implementing custom I/O. Normally these is 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 is 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 on 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 was not seekable.

*/

int seekable;

} Aviocontext;

byte stream I/O context

Adding variables at the end of a structure can reduce version conflicts

removing, sorting, and modifying existing variables will result in a larger version conflict

sizeof (Aviocontext) in libav*. External not available

function pointers in Aviocontext cannot be called directly, usually using the Avio_alloc_context () function to set their function pointers

unsigned char *buffer: starting pointer for cache

int buffer_size: The maximum value of the cache

void *opaque: A pointer used in a callback function

Int (*read_packet) (void *opaque, uint8_t *buf, int buf_size): Read File callback method

Int (*write_packet) (void *opaque, uint8_t *buf, int buf_size): Write File callback method

int64_t (*seek) (void *opaque, int64_t offset, int whence): Seek file callback method

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 is allocated with Av_malloc () and friends.

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

* For protocols with fixed blocksize it should is 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 is writable, 0 otherwise.

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

* @param read_packet A function for refilling the buffer, May is NULL.

* @param write_packet A function for writing the buffer contents, May is NULL.

* @param seek A function for seeking to specified byte position, could 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));

Request and initialize a AVIOCONTEXT structure for the i/0 cache, which must be released using Av_free () when it is finished

unsigned char *buffer: input/output cache memory block, must be assigned using Av_malloc ()

int buffer_size: Cache size is very important

int Write_flag: Set to 1 if the cache is writable, otherwise set to 0

void *opaque: pointer, used for callback

Int (*read_packet): Read packet function pointer

Int (*write_packet): Write packet function pointer

int64_t (*seek): Seek file function pointer

8 Av_open_input_file ()

/**

* Open a media file as input. The codecs is 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);

Open a media file as input, also known as the source file, codecs does not open and only reads the header information of the file.

Introduction of header file: #include "libavformat/avformat.h"

Avformatcontext **ic_ptr Input File container

const char *filename input file name, full path, and guaranteed file presence

Avinputformat *fmt Input file format, fill null

int buf_size, buffer size, fill 0 directly

Avformatparameters *ap, format parameter, add NULL to

Successful return 0, other failure

Do not favour the use of avformat_open_input instead

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);

Closes the input file container that is opened with Avformat_close_input (), but does not relate to its codecs

Introduction of header file: #include "libavformat/avformat.h"

Using the Av_open_input_file Open file container, you can use this function to close the

With Av_close_input_file off, it is no longer necessary to use Avformat_free_context to release the

ten Av_find_stream_info ()

/**

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

* is useful to 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 a changed by this function;

* Examined packets May is 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 are needed so

* We don't waste time getting stuff the user does not need.

*/

int Av_find_stream_info (Avformatcontext *ic);

Getting the stream information in a media file by reading a package in a media file is very useful for files that have no header information, such as (MPEG).

This function usually mpeg-2 the real frame rate like the frame pattern of the position, which does not change the logical file's size.

Introduction of header file: #include "libavformat/avformat.h"

That is, the audio and video streams in the media files are read out, stored in the container, so that the use of decoding

Return >=0 succeeded, otherwise failed



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);

Find a registered audio and video decoder with code ID

Introduction of #include "libavcodec/avcodec.h"

Implementation in: \FFMPEG\LIBAVCODEC\UTILS.C

Before locating the decoder, you must first call Av_register_all to register all supported decoders

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.