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