gst/parse: Also pass -DGST_EXPORTS here
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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 #include <gst/gstminiobject.h>
30
31 G_BEGIN_DECLS
32
33 GST_EXPORT GType _gst_memory_type;
34 #define GST_TYPE_MEMORY (_gst_memory_type)
35 GType gst_memory_get_type(void);
36
37 typedef struct _GstMemory GstMemory;
38 typedef struct _GstAllocator GstAllocator;
39
40 #define GST_MEMORY_CAST(mem)   ((GstMemory *)(mem))
41
42 /**
43  * GstMemoryFlags:
44  * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
45  * memory with #GST_MAP_WRITE.
46  * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
47  * made when this memory needs to be shared between buffers.
48  * @GST_MEMORY_FLAG_ZERO_PREFIXED: the memory prefix is filled with 0 bytes
49  * @GST_MEMORY_FLAG_ZERO_PADDED: the memory padding is filled with 0 bytes
50  * @GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS: the memory is physically contiguous. (Since 1.2)
51  * @GST_MEMORY_FLAG_NOT_MAPPABLE: the memory can't be mapped via gst_memory_map() without any preconditions. (Since 1.2)
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      = GST_MINI_OBJECT_FLAG_LOCK_READONLY,
58   GST_MEMORY_FLAG_NO_SHARE      = (GST_MINI_OBJECT_FLAG_LAST << 0),
59   GST_MEMORY_FLAG_ZERO_PREFIXED = (GST_MINI_OBJECT_FLAG_LAST << 1),
60   GST_MEMORY_FLAG_ZERO_PADDED   = (GST_MINI_OBJECT_FLAG_LAST << 2),
61   GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS = (GST_MINI_OBJECT_FLAG_LAST << 3),
62   GST_MEMORY_FLAG_NOT_MAPPABLE  = (GST_MINI_OBJECT_FLAG_LAST << 4),
63
64   GST_MEMORY_FLAG_LAST          = (GST_MINI_OBJECT_FLAG_LAST << 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_MINI_OBJECT_FLAGS (mem)
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_MINI_OBJECT_FLAG_IS_SET (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_MINI_OBJECT_FLAG_UNSET (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_NO_SHARE:
100  * @mem: a #GstMemory.
101  *
102  * Check if @mem cannot be shared between buffers
103  */
104 #define GST_MEMORY_IS_NO_SHARE(mem)        GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NO_SHARE)
105 /**
106  * GST_MEMORY_IS_ZERO_PREFIXED:
107  * @mem: a #GstMemory.
108  *
109  * Check if the prefix in @mem is 0 filled.
110  */
111 #define GST_MEMORY_IS_ZERO_PREFIXED(mem)   GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
112 /**
113  * GST_MEMORY_IS_ZERO_PADDED:
114  * @mem: a #GstMemory.
115  *
116  * Check if the padding in @mem is 0 filled.
117  */
118 #define GST_MEMORY_IS_ZERO_PADDED(mem)     GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
119
120 /**
121  * GST_MEMORY_IS_PHYSICALLY_CONTIGUOUS:
122  * @mem: a #GstMemory.
123  *
124  * Check if @mem is physically contiguous.
125  *
126  * Since: 1.2
127  */
128 #define GST_MEMORY_IS_PHYSICALLY_CONTIGUOUS(mem)     GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS)
129
130 /**
131  * GST_MEMORY_IS_NOT_MAPPABLE:
132  * @mem: a #GstMemory.
133  *
134  * Check if @mem can't be mapped via gst_memory_map() without any preconditions
135  *
136  * Since: 1.2
137  */
138 #define GST_MEMORY_IS_NOT_MAPPABLE(mem)     GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NOT_MAPPABLE)
139
140 /**
141  * GstMemory:
142  * @mini_object: parent structure
143  * @allocator: pointer to the #GstAllocator
144  * @parent: parent memory block
145  * @maxsize: the maximum size allocated
146  * @align: the alignment of the memory
147  * @offset: the offset where valid data starts
148  * @size: the size of valid data
149  *
150  * Base structure for memory implementations. Custom memory will put this structure
151  * as the first member of their structure.
152  */
153 struct _GstMemory {
154   GstMiniObject   mini_object;
155
156   GstAllocator   *allocator;
157
158   GstMemory      *parent;
159   gsize           maxsize;
160   gsize           align;
161   gsize           offset;
162   gsize           size;
163 };
164
165 /**
166  * GstMapFlags:
167  * @GST_MAP_READ: map for read access
168  * @GST_MAP_WRITE: map for write access
169  * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
170  *
171  * Flags used when mapping memory
172  */
173 typedef enum {
174   GST_MAP_READ      = GST_LOCK_FLAG_READ,
175   GST_MAP_WRITE     = GST_LOCK_FLAG_WRITE,
176
177   GST_MAP_FLAG_LAST = (1 << 16)
178 } GstMapFlags;
179
180 /**
181  * GST_MAP_READWRITE:
182  *
183  * GstMapFlags value alias for GST_MAP_READ | GST_MAP_WRITE
184  */
185 #define GST_MAP_READWRITE      (GST_MAP_READ | GST_MAP_WRITE)
186
187
188 /**
189  * GstMapInfo:
190  * @memory: a pointer to the mapped memory
191  * @flags: flags used when mapping the memory
192  * @data: (array length=size): a pointer to the mapped data
193  * @size: the valid size in @data
194  * @maxsize: the maximum bytes in @data
195  * @user_data: extra private user_data that the implementation of the memory
196  *             can use to store extra info.
197  *
198  * A structure containing the result of a map operation such as
199  * gst_memory_map(). It contains the data and size.
200  */
201 typedef struct {
202   GstMemory *memory;
203   GstMapFlags flags;
204   guint8 *data;
205   gsize size;
206   gsize maxsize;
207   /*< protected >*/
208   gpointer user_data[4];
209
210   /*< private >*/
211   gpointer _gst_reserved[GST_PADDING];
212 } GstMapInfo;
213
214 /**
215  * GST_MAP_INFO_INIT:
216  *
217  * Initializer for #GstMapInfo
218  */
219 #define GST_MAP_INFO_INIT { NULL, (GstMapFlags) 0, NULL, 0, 0, { NULL, NULL, NULL, NULL}, {NULL, NULL, NULL, NULL}}
220
221 /**
222  * GstMemoryMapFunction:
223  * @mem: a #GstMemory
224  * @maxsize: size to map
225  * @flags: access mode for the memory
226  *
227  * Get the memory of @mem that can be accessed according to the mode specified
228  * in @flags. The function should return a pointer that contains at least
229  * @maxsize bytes.
230  *
231  * Returns: a pointer to memory of which at least @maxsize bytes can be
232  * accessed according to the access pattern in @flags.
233  */
234 typedef gpointer    (*GstMemoryMapFunction)       (GstMemory *mem, gsize maxsize, GstMapFlags flags);
235
236 /**
237  * GstMemoryMapFullFunction:
238  * @mem: a #GstMemory
239  * @info: the #GstMapInfo to map with
240  * @maxsize: size to map
241  *
242  * Get the memory of @mem that can be accessed according to the mode specified
243  * in @info's flags. The function should return a pointer that contains at least
244  * @maxsize bytes.
245  *
246  * Returns: a pointer to memory of which at least @maxsize bytes can be
247  * accessed according to the access pattern in @info's flags.
248  */
249 typedef gpointer    (*GstMemoryMapFullFunction)       (GstMemory *mem, GstMapInfo * info, gsize maxsize);
250
251 /**
252  * GstMemoryUnmapFunction:
253  * @mem: a #GstMemory
254  *
255  * Return the pointer previously retrieved with gst_memory_map().
256  */
257 typedef void        (*GstMemoryUnmapFunction)     (GstMemory *mem);
258
259 /**
260  * GstMemoryUnmapFullFunction:
261  * @mem: a #GstMemory
262  * @info: a #GstMapInfo
263  *
264  * Return the pointer previously retrieved with gst_memory_map() with @info.
265  */
266 typedef void        (*GstMemoryUnmapFullFunction)     (GstMemory *mem, GstMapInfo * info);
267
268 /**
269  * GstMemoryCopyFunction:
270  * @mem: a #GstMemory
271  * @offset: an offset
272  * @size: a size or -1
273  *
274  * Copy @size bytes from @mem starting at @offset and return them wrapped in a
275  * new GstMemory object.
276  * If @size is set to -1, all bytes starting at @offset are copied.
277  *
278  * Returns: a new #GstMemory object wrapping a copy of the requested region in
279  * @mem.
280  */
281 typedef GstMemory * (*GstMemoryCopyFunction)      (GstMemory *mem, gssize offset, gssize size);
282
283 /**
284  * GstMemoryShareFunction:
285  * @mem: a #GstMemory
286  * @offset: an offset
287  * @size: a size or -1
288  *
289  * Share @size bytes from @mem starting at @offset and return them wrapped in a
290  * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
291  * shared. This function does not make a copy of the bytes in @mem.
292  *
293  * Returns: a new #GstMemory object sharing the requested region in @mem.
294  */
295 typedef GstMemory * (*GstMemoryShareFunction)     (GstMemory *mem, gssize offset, gssize size);
296
297 /**
298  * GstMemoryIsSpanFunction:
299  * @mem1: a #GstMemory
300  * @mem2: a #GstMemory
301  * @offset: a result offset
302  *
303  * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
304  * @mem1 in the parent buffer in @offset.
305  *
306  * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
307  */
308 typedef gboolean    (*GstMemoryIsSpanFunction)    (GstMemory *mem1, GstMemory *mem2, gsize *offset);
309
310 void           gst_memory_init         (GstMemory *mem, GstMemoryFlags flags,
311                                         GstAllocator *allocator, GstMemory *parent,
312                                         gsize maxsize, gsize align,
313                                         gsize offset, gsize size);
314
315 gboolean       gst_memory_is_type      (GstMemory *mem, const gchar *mem_type);
316
317 /* refcounting */
318 /**
319  * gst_memory_ref:
320  * @memory: The memory to refcount
321  *
322  * Increase the refcount of this memory.
323  *
324  * Returns: (transfer full): @memory (for convenience when doing assignments)
325  */
326 static inline GstMemory *
327 gst_memory_ref (GstMemory * memory)
328 {
329   return (GstMemory *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (memory));
330 }
331
332 /**
333  * gst_memory_unref:
334  * @memory: (transfer full): the memory to refcount
335  *
336  * Decrease the refcount of an memory, freeing it if the refcount reaches 0.
337  */
338 static inline void
339 gst_memory_unref (GstMemory * memory)
340 {
341   gst_mini_object_unref (GST_MINI_OBJECT_CAST (memory));
342 }
343
344 /* getting/setting memory properties */
345 gsize          gst_memory_get_sizes    (GstMemory *mem, gsize *offset, gsize *maxsize);
346 void           gst_memory_resize       (GstMemory *mem, gssize offset, gsize size);
347
348 #define        gst_memory_lock(m,f)        gst_mini_object_lock (GST_MINI_OBJECT_CAST (m), (f))
349 #define        gst_memory_unlock(m,f)      gst_mini_object_unlock (GST_MINI_OBJECT_CAST (m), (f))
350 #define        gst_memory_is_writable(m)   gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (m))
351 #define        gst_memory_make_writable(m) GST_MEMORY_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (m)))
352
353 /* retrieving data */
354 GstMemory *    gst_memory_make_mapped  (GstMemory *mem, GstMapInfo *info, GstMapFlags flags) G_GNUC_WARN_UNUSED_RESULT;
355 gboolean       gst_memory_map          (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
356 void           gst_memory_unmap        (GstMemory *mem, GstMapInfo *info);
357
358 /* copy and subregions */
359 GstMemory *    gst_memory_copy         (GstMemory *mem, gssize offset, gssize size) G_GNUC_WARN_UNUSED_RESULT;
360 GstMemory *    gst_memory_share        (GstMemory *mem, gssize offset, gssize size) G_GNUC_WARN_UNUSED_RESULT;
361
362 /* span memory */
363 gboolean       gst_memory_is_span      (GstMemory *mem1, GstMemory *mem2, gsize *offset);
364
365 #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
366 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstMemory, gst_memory_unref)
367 #endif
368
369 #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
370 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstAllocator, gst_object_unref)
371 #endif
372
373 G_END_DECLS
374
375 #endif /* __GST_MEMORY_H__ */