ffmpeg的内存使用注意事项

AVBuffer

struct AVBuffer {
    uint8_t *data; /**< data described by this buffer */
    int      size; /**< size of data in bytes */
    /**
     *  number of existing AVBufferRef instances referring to this buffer
     */
    atomic_uint refcount;


    /**
     * a callback for freeing the data
     */
    void (*free)(void *opaque, uint8_t *data);


    /**
     * an opaque pointer, to be used by the freeing callback
     */
    void *opaque;


    /**
     * A combination of BUFFER_FLAG_*
     */
    int flags;
};

AVBufferRef

/**
* A reference to a data buffer.
*
* The size of this struct is not a part of the public ABI and it is not meant
* to be allocated directly.
*/
typedef struct AVBufferRef {
    AVBuffer *buffer;

    /**
     * The data buffer. It is considered writable if and only if
     * this is the only reference to the buffer, in which case
     * av_buffer_is_writable() returns 1.
     */
    uint8_t *data;

    /**
     * Size of data in bytes.
     */
    int      size;
} AVBufferRef;

AVPacket

/**
* This structure stores compressed data. It is typically exported by demuxers
* and then passed as input to decoders, or received as output from encoders and
* then passed to muxers.
*
* For video, it should typically contain one compressed frame. For audio it may
* contain several compressed frames. Encoders are allowed to output empty
* packets, with no compressed data, containing only side data
* (e.g. to update some stream parameters at the end of encoding).
*
* AVPacket is one of the few structs in FFmpeg, whose size is a part of public
* ABI. Thus it may be allocated on stack and no new fields can be added to it
* without libavcodec and libavformat major bump.
*
* The semantics of data ownership depends on the buf field.
* If it is set, the packet data is dynamically allocated and is
* valid indefinitely until a call to av_packet_unref() reduces the
* reference count to 0.
*
* If the buf field is not set av_packet_ref() would make a copy instead
* of increasing the reference count.
*
* The side data is always allocated with av_malloc(), copied by
* av_packet_ref() and freed by av_packet_unref().
*
* @see av_packet_ref
* @see av_packet_unref
*/
typedef struct AVPacket {

    /**
     * A reference to the reference-counted buffer where the packet data is
     * stored.
     * May be NULL, then the packet data is not reference-counted.
     */
    AVBufferRef *buf;

    /**
     * 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;

    /**
     * A combination of AV_PKT_FLAG values
     */
    int   flags;

    /*
     * Additional packet data that can be provided by the container.
     * Packet can contain several types of side information.
     */
    AVPacketSideData *side_data;
    int side_data_elems;

    /**
     * Duration of this packet in AVStream->time_base units, 0 if unknown.
     * Equals next_pts - this_pts in presentation order.
     */
    int64_t duration;
    int64_t pos;                            ///< byte position in stream, -1 if unknown

#if FF_API_CONVERGENCE_DURATION
    /**
     * @deprecated Same as the duration field, but as int64_t. This was required
     * for Matroska subtitles, whose duration values could overflow when the
     * duration field was still an int.
     */
    attribute_deprecated
    int64_t convergence_duration;
#endif

} AVPacket;

调用函数:

AVPacket *av_packet_alloc():分配一个AVPacket指针,AVBufferRef为空

av_packet_free(AVPacket *):释放AVPacket指针空间

av_init_packet:只是初始化AVPacket

av_new_packet(AVPacket* ,int size):创建一个AVPacket,并创建size大小的AVBufferRef,引用计数等于1

av_packet_ref(AVPacket *dst,AVPacket *src):自己要创建AVPacket *dst,并引用计数+1

av_packet_unref:引用计数-1

av_packet_move_ref:转移引用计数

av_packet_clone:相当于av_packet_alloc和av_packet_ref AVPacket是新的,但内容是老的

AVPaket的使用要求:

猜你喜欢

转载自blog.csdn.net/cai742925624/article/details/126887276
今日推荐