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