Technology Sharing

FFmpeg reference count data buffer related structures: AVBuffer, AVBufferRef Introduction

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

=================================================================

Source code analysis related to FFmpeg memory management:

Source code related to memory allocation and release in FFmpeg: analysis of av_malloc function, av_mallocz function, av_free function and av_freep function

FFmpeg reference count data buffer related structures: AVBuffer, AVBufferRef Introduction

FFmpeg source code: buffer_create, av_buffer_create, av_buffer_default_free, av_buffer_alloc, av_buffer_allocz function analysis

FFmpeg source code: av_buffer_ref, av_buffer_unref function analysis

=================================================================

I. Introduction

Many structures in FFmpeg source code, such as AVPacket and AVFrame, use AVBufferRef and AVBuffer to manage buffers. AVBuffer uses the member variable refcount to record the number of times resources are used and control the release of resources. This article introduces the AVBuffer and AVBufferRef structures.

2. Declaration of AVBuffer structure

AVBuffer is an application programming interface for reference counted data buffers, which represents the data buffer itself. It is opaque and cannot be directly accessed by users using the FFmpeg API. Users can only access it indirectly through AVBufferRef. However, it is possible to check whether two different references describe the same data buffer by comparing two AVBuffer pointers.

The AVBuffer structure is declared in the header file libavutil/buffer_internal.h of the FFmpeg source code (the FFmpeg source code version used in this article is 5.0.3):

struct AVBuffer {
    uint8_t *data; /**