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