1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * This file is part of FFmpeg.
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
5cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
6cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
7cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
8cabdff1aSopenharmony_ci *
9cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
10cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
11cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12cabdff1aSopenharmony_ci * Lesser General Public License for more details.
13cabdff1aSopenharmony_ci *
14cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
15cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
16cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17cabdff1aSopenharmony_ci */
18cabdff1aSopenharmony_ci
19cabdff1aSopenharmony_ci#ifndef AVUTIL_BUFFER_INTERNAL_H
20cabdff1aSopenharmony_ci#define AVUTIL_BUFFER_INTERNAL_H
21cabdff1aSopenharmony_ci
22cabdff1aSopenharmony_ci#include <stdatomic.h>
23cabdff1aSopenharmony_ci#include <stdint.h>
24cabdff1aSopenharmony_ci
25cabdff1aSopenharmony_ci#include "buffer.h"
26cabdff1aSopenharmony_ci#include "thread.h"
27cabdff1aSopenharmony_ci
28cabdff1aSopenharmony_ci/**
29cabdff1aSopenharmony_ci * The buffer was av_realloc()ed, so it is reallocatable.
30cabdff1aSopenharmony_ci */
31cabdff1aSopenharmony_ci#define BUFFER_FLAG_REALLOCATABLE (1 << 0)
32cabdff1aSopenharmony_ci/**
33cabdff1aSopenharmony_ci * The AVBuffer structure is part of a larger structure
34cabdff1aSopenharmony_ci * and should not be freed.
35cabdff1aSopenharmony_ci */
36cabdff1aSopenharmony_ci#define BUFFER_FLAG_NO_FREE       (1 << 1)
37cabdff1aSopenharmony_ci
38cabdff1aSopenharmony_cistruct AVBuffer {
39cabdff1aSopenharmony_ci    uint8_t *data; /**< data described by this buffer */
40cabdff1aSopenharmony_ci    size_t size; /**< size of data in bytes */
41cabdff1aSopenharmony_ci
42cabdff1aSopenharmony_ci    /**
43cabdff1aSopenharmony_ci     *  number of existing AVBufferRef instances referring to this buffer
44cabdff1aSopenharmony_ci     */
45cabdff1aSopenharmony_ci    atomic_uint refcount;
46cabdff1aSopenharmony_ci
47cabdff1aSopenharmony_ci    /**
48cabdff1aSopenharmony_ci     * a callback for freeing the data
49cabdff1aSopenharmony_ci     */
50cabdff1aSopenharmony_ci    void (*free)(void *opaque, uint8_t *data);
51cabdff1aSopenharmony_ci
52cabdff1aSopenharmony_ci    /**
53cabdff1aSopenharmony_ci     * an opaque pointer, to be used by the freeing callback
54cabdff1aSopenharmony_ci     */
55cabdff1aSopenharmony_ci    void *opaque;
56cabdff1aSopenharmony_ci
57cabdff1aSopenharmony_ci    /**
58cabdff1aSopenharmony_ci     * A combination of AV_BUFFER_FLAG_*
59cabdff1aSopenharmony_ci     */
60cabdff1aSopenharmony_ci    int flags;
61cabdff1aSopenharmony_ci
62cabdff1aSopenharmony_ci    /**
63cabdff1aSopenharmony_ci     * A combination of BUFFER_FLAG_*
64cabdff1aSopenharmony_ci     */
65cabdff1aSopenharmony_ci    int flags_internal;
66cabdff1aSopenharmony_ci};
67cabdff1aSopenharmony_ci
68cabdff1aSopenharmony_citypedef struct BufferPoolEntry {
69cabdff1aSopenharmony_ci    uint8_t *data;
70cabdff1aSopenharmony_ci
71cabdff1aSopenharmony_ci    /*
72cabdff1aSopenharmony_ci     * Backups of the original opaque/free of the AVBuffer corresponding to
73cabdff1aSopenharmony_ci     * data. They will be used to free the buffer when the pool is freed.
74cabdff1aSopenharmony_ci     */
75cabdff1aSopenharmony_ci    void *opaque;
76cabdff1aSopenharmony_ci    void (*free)(void *opaque, uint8_t *data);
77cabdff1aSopenharmony_ci
78cabdff1aSopenharmony_ci    AVBufferPool *pool;
79cabdff1aSopenharmony_ci    struct BufferPoolEntry *next;
80cabdff1aSopenharmony_ci
81cabdff1aSopenharmony_ci    /*
82cabdff1aSopenharmony_ci     * An AVBuffer structure to (re)use as AVBuffer for subsequent uses
83cabdff1aSopenharmony_ci     * of this BufferPoolEntry.
84cabdff1aSopenharmony_ci     */
85cabdff1aSopenharmony_ci    AVBuffer buffer;
86cabdff1aSopenharmony_ci} BufferPoolEntry;
87cabdff1aSopenharmony_ci
88cabdff1aSopenharmony_cistruct AVBufferPool {
89cabdff1aSopenharmony_ci    AVMutex mutex;
90cabdff1aSopenharmony_ci    BufferPoolEntry *pool;
91cabdff1aSopenharmony_ci
92cabdff1aSopenharmony_ci    /*
93cabdff1aSopenharmony_ci     * This is used to track when the pool is to be freed.
94cabdff1aSopenharmony_ci     * The pointer to the pool itself held by the caller is considered to
95cabdff1aSopenharmony_ci     * be one reference. Each buffer requested by the caller increases refcount
96cabdff1aSopenharmony_ci     * by one, returning the buffer to the pool decreases it by one.
97cabdff1aSopenharmony_ci     * refcount reaches zero when the buffer has been uninited AND all the
98cabdff1aSopenharmony_ci     * buffers have been released, then it's safe to free the pool and all
99cabdff1aSopenharmony_ci     * the buffers in it.
100cabdff1aSopenharmony_ci     */
101cabdff1aSopenharmony_ci    atomic_uint refcount;
102cabdff1aSopenharmony_ci
103cabdff1aSopenharmony_ci    size_t size;
104cabdff1aSopenharmony_ci    void *opaque;
105cabdff1aSopenharmony_ci    AVBufferRef* (*alloc)(size_t size);
106cabdff1aSopenharmony_ci    AVBufferRef* (*alloc2)(void *opaque, size_t size);
107cabdff1aSopenharmony_ci    void         (*pool_free)(void *opaque);
108cabdff1aSopenharmony_ci};
109cabdff1aSopenharmony_ci
110cabdff1aSopenharmony_ci#endif /* AVUTIL_BUFFER_INTERNAL_H */
111