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