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