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