f64aef8169f43115c98c4053ba4e4372296d5544
[platform/upstream/gstreamer.git] / gst / gstmemory.h
1 /* GStreamer
2  * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.be>
3  *
4  * gstmemory.h: Header for memory blocks
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #ifndef __GST_MEMORY_H__
24 #define __GST_MEMORY_H__
25
26 #include <gst/gstconfig.h>
27
28 #include <glib-object.h>
29
30 G_BEGIN_DECLS
31
32 #define GST_TYPE_MEMORY (gst_memory_get_type())
33 GType gst_memory_get_type(void);
34
35 #define GST_TYPE_ALLOCATOR (gst_allocator_get_type())
36 GType gst_allocator_get_type(void);
37
38 typedef struct _GstMemory GstMemory;
39 typedef struct _GstMemoryInfo GstMemoryInfo;
40 typedef struct _GstAllocator GstAllocator;
41
42 GST_EXPORT gsize gst_memory_alignment;
43
44 #define GST_MEMORY_CAST(mem)   ((GstMemory *)(mem))
45
46 /**
47  * GstMemoryFlags:
48  * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
49  * memory with #GST_MAP_WRITE.
50  * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
51  * made when this memory needs to be shared between buffers.
52  * @GST_MEMORY_FLAG_ZERO_PREFIXED: the memory prefix is filled with 0 bytes
53  * @GST_MEMORY_FLAG_ZERO_PADDED: the memory padding is filled with 0 bytes
54  * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
55  *
56  * Flags for wrapped memory.
57  */
58 typedef enum {
59   GST_MEMORY_FLAG_READONLY      = (1 << 0),
60   GST_MEMORY_FLAG_NO_SHARE      = (1 << 1),
61   GST_MEMORY_FLAG_ZERO_PREFIXED = (1 << 2),
62   GST_MEMORY_FLAG_ZERO_PADDED   = (1 << 3),
63
64   GST_MEMORY_FLAG_LAST          = (1 << 16)
65 } GstMemoryFlags;
66
67 /**
68  * GST_MEMORY_FLAGS:
69  * @mem: a #GstMemory.
70  *
71  * A flags word containing #GstMemoryFlags flags set on @mem
72  */
73 #define GST_MEMORY_FLAGS(mem)  (GST_MEMORY_CAST (mem)->flags)
74 /**
75  * GST_MEMORY_FLAG_IS_SET:
76  * @mem: a #GstMemory.
77  * @flag: the #GstMemoryFlags to check.
78  *
79  * Gives the status of a specific flag on a @mem.
80  */
81 #define GST_MEMORY_FLAG_IS_SET(mem,flag)   !!(GST_MEMORY_FLAGS (mem) & (flag))
82 /**
83  * GST_MEMORY_FLAG_UNSET:
84  * @mem: a #GstMemory.
85  * @flag: the #GstMemoryFlags to clear.
86  *
87  * Clear a specific flag on a @mem.
88  */
89 #define GST_MEMORY_FLAG_UNSET(mem,flag)   (GST_MEMORY_FLAGS (mem) &= ~(flag))
90
91 /**
92  * GST_MEMORY_IS_READONLY:
93  * @mem: a #GstMemory.
94  *
95  * Check if @mem is readonly.
96  */
97 #define GST_MEMORY_IS_READONLY(mem)        GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
98 /**
99  * GST_MEMORY_IS_ZERO_PREFIXED:
100  * @mem: a #GstMemory.
101  *
102  * Check if the prefix in @mem is 0 filled.
103  */
104 #define GST_MEMORY_IS_ZERO_PREFIXED(mem)   GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
105 /**
106  * GST_MEMORY_IS_ZERO_PADDED:
107  * @mem: a #GstMemory.
108  *
109  * Check if the padding in @mem is 0 filled.
110  */
111 #define GST_MEMORY_IS_ZERO_PADDED(mem)     GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
112
113
114 /**
115  * GstMemory:
116  * @allocator: pointer to the #GstAllocator
117  * @flags: memory flags
118  * @refcount: refcount
119  * @parent: parent memory block
120  * @state: private state
121  * @maxsize: the maximum size allocated
122  * @align: the alignment of the memory
123  * @offset: the offset where valid data starts
124  * @size: the size of valid data
125  *
126  * Base structure for memory implementations. Custom memory will put this structure
127  * as the first member of their structure.
128  */
129 struct _GstMemory {
130   GstAllocator   *allocator;
131
132   GstMemoryFlags  flags;
133   gint            refcount;
134   GstMemory      *parent;
135   volatile gint   state;
136   gsize           maxsize;
137   gsize           align;
138   gsize           offset;
139   gsize           size;
140 };
141
142 /**
143  * GstMapFlags:
144  * @GST_MAP_READ: map for read access
145  * @GST_MAP_WRITE: map for write access
146  * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
147  *
148  * Flags used when mapping memory
149  */
150 typedef enum {
151   GST_MAP_READ      = (1 << 0),
152   GST_MAP_WRITE     = (1 << 1),
153
154   GST_MAP_FLAG_LAST = (1 << 16)
155 } GstMapFlags;
156
157 /**
158  * GstMapInfo:
159  * @memory: a pointer to the mapped memory
160  * @flags: flags used when mapping the memory
161  * @data: a pointer to the mapped data
162  * @size: the valid size in @data
163  * @maxsize: the maximum bytes in @data
164  * @user_data: extra private user_data that the implementation of the memory
165  *             can use to store extra info.
166  *
167  * A structure containing the result of a map operation such as
168  * gst_memory_map(). It contains the data and size.
169  */
170 typedef struct {
171   GstMemory *memory;
172   GstMapFlags flags;
173   guint8 *data;
174   gsize size;
175   gsize maxsize;
176   /*< private >*/
177   gpointer user_data[4];
178 } GstMapInfo;
179
180 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0, }
181
182 /**
183  * GST_MAP_READWRITE:
184  *
185  * Map for readwrite access
186  */
187 #define GST_MAP_READWRITE      (GST_MAP_READ | GST_MAP_WRITE)
188
189 /**
190  * GST_ALLOCATOR_SYSMEM:
191  *
192  * The allocator name for the default system memory allocator
193  */
194 #define GST_ALLOCATOR_SYSMEM   "SystemMemory"
195
196 /**
197  * GstAllocatorAllocFunction:
198  * @allocator: a #GstAllocator
199  * @flags: the flags
200  * @maxsize: the maxsize
201  * @offset: the offset
202  * @size: the size
203  * @align: the alignment
204  * @user_data: user data
205  *
206  * Allocate a new #GstMemory from @allocator that can hold at least @maxsize bytes
207  * and is aligned to (@align + 1) bytes.
208  *
209  * The offset and size of the memory should be set and the prefix/padding must
210  * be filled with 0 if @flags contains #GST_MEMORY_FLAG_ZERO_PREFIXED and
211  * #GST_MEMORY_FLAG_ZERO_PADDED respectively.
212  *
213  * @user_data is the data that was used when creating @allocator.
214  *
215  * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
216  */
217 typedef GstMemory *  (*GstAllocatorAllocFunction)  (GstAllocator *allocator,
218                                                     GstMemoryFlags flags,
219                                                     gsize maxsize, gsize offset,
220                                                     gsize size, gsize align,
221                                                     gpointer user_data);
222
223 /**
224  * GstMemoryMapFunction:
225  * @mem: a #GstMemory
226  * @maxsize: size to map
227  * @flags: access mode for the memory
228  *
229  * Get the memory of @mem that can be accessed according to the mode specified
230  * in @flags. The function should return a pointer that contains at least
231  * @maxsize bytes.
232  *
233  * Returns: a pointer to memory of which at least @maxsize bytes can be
234  * accessed according to the access pattern in @flags.
235  */
236 typedef gpointer    (*GstMemoryMapFunction)       (GstMemory *mem, gsize maxsize, GstMapFlags flags);
237
238 /**
239  * GstMemoryUnmapFunction:
240  * @mem: a #GstMemory
241  *
242  * Return the pointer previously retrieved with gst_memory_map().
243  *
244  * Returns: %TRUE on success.
245  */
246 typedef void        (*GstMemoryUnmapFunction)     (GstMemory *mem);
247
248 /**
249  * GstMemoryFreeFunction:
250  * @mem: a #GstMemory
251  *
252  * Free the memory used by @mem. This function is usually called when the
253  * refcount of the @mem has reached 0.
254  */
255 typedef void        (*GstMemoryFreeFunction)      (GstMemory *mem);
256
257 /**
258  * GstMemoryCopyFunction:
259  * @mem: a #GstMemory
260  * @offset: an offset
261  * @size: a size or -1
262  *
263  * Copy @size bytes from @mem starting at @offset and return them wrapped in a
264  * new GstMemory object.
265  * If @size is set to -1, all bytes starting at @offset are copied.
266  *
267  * Returns: a new #GstMemory object wrapping a copy of the requested region in
268  * @mem.
269  */
270 typedef GstMemory * (*GstMemoryCopyFunction)      (GstMemory *mem, gssize offset, gssize size);
271
272 /**
273  * GstMemoryShareFunction:
274  * @mem: a #GstMemory
275  * @offset: an offset
276  * @size: a size or -1
277  *
278  * Share @size bytes from @mem starting at @offset and return them wrapped in a
279  * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
280  * shared. This function does not make a copy of the bytes in @mem.
281  *
282  * Returns: a new #GstMemory object sharing the requested region in @mem.
283  */
284 typedef GstMemory * (*GstMemoryShareFunction)     (GstMemory *mem, gssize offset, gssize size);
285
286 /**
287  * GstMemoryIsSpanFunction:
288  * @mem1: a #GstMemory
289  * @mem2: a #GstMemory
290  * @offset: a result offset
291  *
292  * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
293  * @mem1 in the parent buffer in @offset.
294  *
295  * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
296  */
297 typedef gboolean    (*GstMemoryIsSpanFunction)    (GstMemory *mem1, GstMemory *mem2, gsize *offset);
298
299 /**
300  * GstMemoryInfo:
301  * @mem_type: the memory type this allocator provides
302  * @alloc: the implementation of the GstAllocatorAllocFunction
303  * @mem_map: the implementation of the GstMemoryMapFunction
304  * @mem_unmap: the implementation of the GstMemoryUnmapFunction
305  * @mem_free: the implementation of the GstMemoryFreeFunction
306  * @mem_copy: the implementation of the GstMemoryCopyFunction
307  * @mem_share: the implementation of the GstMemoryShareFunction
308  * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
309  *
310  * The #GstMemoryInfo is used to register new memory allocators and contain
311  * the implementations for various memory operations.
312  */
313 struct _GstMemoryInfo {
314   const gchar              *mem_type;
315
316   GstAllocatorAllocFunction alloc;
317
318   GstMemoryMapFunction      mem_map;
319   GstMemoryUnmapFunction    mem_unmap;
320   GstMemoryFreeFunction     mem_free;
321
322   GstMemoryCopyFunction     mem_copy;
323   GstMemoryShareFunction    mem_share;
324   GstMemoryIsSpanFunction   mem_is_span;
325
326   /*< private >*/
327   gpointer _gst_reserved[GST_PADDING];
328 };
329
330 /* allocators */
331 GstAllocator * gst_allocator_new             (const GstMemoryInfo * info,
332                                               gpointer user_data, GDestroyNotify notify);
333 const gchar *  gst_allocator_get_memory_type (GstAllocator * allocator);
334
335 GstAllocator * gst_allocator_ref             (GstAllocator * allocator);
336 void           gst_allocator_unref           (GstAllocator * allocator);
337
338 void           gst_allocator_register        (const gchar *name, GstAllocator *allocator);
339 GstAllocator * gst_allocator_find            (const gchar *name);
340
341 void           gst_allocator_set_default     (GstAllocator * allocator);
342
343 /* allocating memory blocks */
344 GstMemory *    gst_allocator_alloc           (GstAllocator * allocator, GstMemoryFlags flags,
345                                               gsize maxsize, gsize offset, gsize size,
346                                               gsize align);
347
348 GstMemory *    gst_memory_new_wrapped  (GstMemoryFlags flags, gpointer data, gsize maxsize,
349                                         gsize offset, gsize size, gpointer user_data,
350                                         GDestroyNotify notify);
351
352 /* refcounting */
353 GstMemory *    gst_memory_ref          (GstMemory *mem);
354 void           gst_memory_unref        (GstMemory *mem);
355
356 gboolean       gst_memory_is_exclusive (GstMemory *mem);
357
358 /* getting/setting memory properties */
359 gsize          gst_memory_get_sizes    (GstMemory *mem, gsize *offset, gsize *maxsize);
360 void           gst_memory_resize       (GstMemory *mem, gssize offset, gsize size);
361
362 /* retrieving data */
363 GstMemory *    gst_memory_make_mapped  (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
364 gboolean       gst_memory_map          (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
365 void           gst_memory_unmap        (GstMemory *mem, GstMapInfo *info);
366
367 /* copy and subregions */
368 GstMemory *    gst_memory_copy         (GstMemory *mem, gssize offset, gssize size);
369 GstMemory *    gst_memory_share        (GstMemory *mem, gssize offset, gssize size);
370
371 /* span memory */
372 gboolean       gst_memory_is_span      (GstMemory *mem1, GstMemory *mem2, gsize *offset);
373
374 G_END_DECLS
375
376 #endif /* __GST_MEMORY_H__ */