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