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 "internal.h"
26 #include "buffer.h"
27 #include "thread.h"
28 
29 /**
30  * The buffer was av_realloc()ed, so it is reallocatable.
31  */
32 #define BUFFER_FLAG_REALLOCATABLE (1 << 0)
33 
34 struct AVBuffer {
35     uint8_t *data; /**< data described by this buffer */
36     buffer_size_t size; /**< size of data in bytes */
37 
38     /**
39      *  number of existing AVBufferRef instances referring to this buffer
40      */
41     atomic_uint refcount;
42 
43     /**
44      * a callback for freeing the data
45      */
46     void (*free)(void *opaque, uint8_t *data);
47 
48     /**
49      * an opaque pointer, to be used by the freeing callback
50      */
51     void *opaque;
52 
53     /**
54      * A combination of AV_BUFFER_FLAG_*
55      */
56     int flags;
57 
58     /**
59      * A combination of BUFFER_FLAG_*
60      */
61     int flags_internal;
62 };
63 
64 typedef struct BufferPoolEntry {
65     uint8_t *data;
66 
67     /*
68      * Backups of the original opaque/free of the AVBuffer corresponding to
69      * data. They will be used to free the buffer when the pool is freed.
70      */
71     void *opaque;
72     void (*free)(void *opaque, uint8_t *data);
73 
74     AVBufferPool *pool;
75     struct BufferPoolEntry *next;
76 } BufferPoolEntry;
77 
78 struct AVBufferPool {
79     AVMutex mutex;
80     BufferPoolEntry *pool;
81 
82     /*
83      * This is used to track when the pool is to be freed.
84      * The pointer to the pool itself held by the caller is considered to
85      * be one reference. Each buffer requested by the caller increases refcount
86      * by one, returning the buffer to the pool decreases it by one.
87      * refcount reaches zero when the buffer has been uninited AND all the
88      * buffers have been released, then it's safe to free the pool and all
89      * the buffers in it.
90      */
91     atomic_uint refcount;
92 
93     buffer_size_t size;
94     void *opaque;
95     AVBufferRef* (*alloc)(buffer_size_t size);
96     AVBufferRef* (*alloc2)(void *opaque, buffer_size_t size);
97     void         (*pool_free)(void *opaque);
98 };
99 
100 #endif /* AVUTIL_BUFFER_INTERNAL_H */
101