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