0e1cd7577c918008546abccdb597776d7476c535
[platform/upstream/gstreamer.git] / gst / gstbufferlist.c
1 /* GStreamer
2  * Copyright (C) 2009 Axis Communications <dev-gstreamer at axis dot com>
3  * @author Jonas Holmberg <jonas dot holmberg at axis dot com>
4  *
5  * gstbufferlist.c: Buffer list
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:gstbufferlist
25  * @short_description: Lists of buffers for data-passing
26  * @see_also: #GstPad, #GstMiniObject
27  *
28  * Buffer lists are an object containing a list of buffers.
29  *
30  * Buffer lists are created with gst_buffer_list_new() and filled with data
31  * using a gst_buffer_list_insert().
32  *
33  * Buffer lists can be pushed on a srcpad with gst_pad_push_list(). This is
34  * interesting when multiple buffers need to be pushed in one go because it
35  * can reduce the amount of overhead for pushing each buffer individually.
36  *
37  * Last reviewed on 2012-03-28 (0.11.3)
38  */
39 #include "gst_private.h"
40
41 #include "gstbuffer.h"
42 #include "gstbufferlist.h"
43
44 #define GST_CAT_DEFAULT GST_CAT_BUFFER_LIST
45
46 /**
47  * GstBufferList:
48  *
49  * Opaque list of grouped buffers.
50  *
51  * Since: 0.10.24
52  */
53 struct _GstBufferList
54 {
55   GstMiniObject mini_object;
56
57   gsize slice_size;
58
59   GArray *array;
60 };
61
62 GType _gst_buffer_list_type = 0;
63
64 GST_DEFINE_MINI_OBJECT_TYPE (GstBufferList, gst_buffer_list);
65
66 void
67 _priv_gst_buffer_list_initialize (void)
68 {
69   _gst_buffer_list_type = gst_buffer_list_get_type ();
70 }
71
72 static GstBufferList *
73 _gst_buffer_list_copy (GstBufferList * list)
74 {
75   GstBufferList *copy;
76   guint i, len;
77
78   len = list->array->len;
79   copy = gst_buffer_list_new_sized (len);
80
81   /* add and ref all buffers in the array */
82   for (i = 0; i < len; i++) {
83     GstBuffer *buf = g_array_index (list->array, GstBuffer *, i);
84     buf = gst_buffer_ref (buf);
85     g_array_append_val (copy->array, buf);
86   }
87   return copy;
88 }
89
90 static void
91 _gst_buffer_list_free (GstBufferList * list)
92 {
93   guint i, len;
94   GST_LOG ("free %p", list);
95
96   /* unrefs all buffers too */
97   len = list->array->len;
98   for (i = 0; i < len; i++)
99     gst_buffer_unref (g_array_index (list->array, GstBuffer *, i));
100   g_array_free (list->array, TRUE);
101
102   g_slice_free1 (list->slice_size, list);
103 }
104
105 static void
106 gst_buffer_list_init (GstBufferList * list, gsize size, guint asize)
107 {
108   gst_mini_object_init (GST_MINI_OBJECT_CAST (list), _gst_buffer_list_type);
109
110   list->mini_object.copy = (GstMiniObjectCopyFunction) _gst_buffer_list_copy;
111   list->mini_object.free = (GstMiniObjectFreeFunction) _gst_buffer_list_free;
112
113   list->slice_size = size;
114
115   list->array = g_array_sized_new (FALSE, FALSE, sizeof (GstBuffer *), asize);
116
117   GST_LOG ("init %p", list);
118 }
119
120 /**
121  * gst_buffer_list_new_sized:
122  * @size: an initial reserved size
123  *
124  * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
125  * the returned #GstBufferList. The list will have @size space preallocated so
126  * that memory reallocations can be avoided.
127  *
128  * Free-function: gst_buffer_list_unref
129  *
130  * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
131  *     after usage.
132  *
133  * Since: 0.10.24
134  */
135 GstBufferList *
136 gst_buffer_list_new_sized (guint size)
137 {
138   GstBufferList *list;
139
140   list = g_slice_new0 (GstBufferList);
141
142   GST_LOG ("new %p", list);
143
144   gst_buffer_list_init (list, sizeof (GstBufferList), size);
145
146   return list;
147 }
148
149 /**
150  * gst_buffer_list_new:
151  *
152  * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
153  * the returned #GstBufferList.
154  *
155  * Free-function: gst_buffer_list_unref
156  *
157  * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
158  *     after usage.
159  *
160  * Since: 0.10.24
161  */
162 GstBufferList *
163 gst_buffer_list_new (void)
164 {
165   return gst_buffer_list_new_sized (8);
166 }
167
168 /**
169  * gst_buffer_list_length:
170  * @list: a #GstBufferList
171  *
172  * Returns the number of buffers in @list.
173  *
174  * Returns: the number of buffers in the buffer list
175  *
176  * Since: 0.10.24
177  */
178 guint
179 gst_buffer_list_length (GstBufferList * list)
180 {
181   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
182
183   return list->array->len;
184 }
185
186 /**
187  * gst_buffer_list_foreach:
188  * @list: a #GstBufferList
189  * @func: (scope call): a #GstBufferListFunc to call
190  * @user_data: (closure): user data passed to @func
191  *
192  * Call @func with @data for each buffer in @list.
193  *
194  * @func can modify the passed buffer pointer or its contents. The return value
195  * of @func define if this function returns or if the remaining buffers in
196  * the list should be skipped.
197  *
198  * Since: 0.10.24
199  */
200 void
201 gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
202     gpointer user_data)
203 {
204   guint i, len;
205
206   g_return_if_fail (GST_IS_BUFFER_LIST (list));
207   g_return_if_fail (func != NULL);
208
209   len = list->array->len;
210   for (i = 0; i < len;) {
211     GstBuffer *buf, *buf_ret;
212     gboolean ret;
213
214     buf = buf_ret = g_array_index (list->array, GstBuffer *, i);
215     ret = func (&buf_ret, i, user_data);
216
217     /* Check if the function changed the buffer */
218     if (buf != buf_ret) {
219       if (buf_ret == NULL) {
220         g_array_remove_index (list->array, i);
221         len--;
222       } else {
223         g_array_index (list->array, GstBuffer *, i) = buf_ret;
224       }
225     }
226
227     if (!ret)
228       break;
229
230     /* If the buffer was not removed by func go to the next buffer */
231     if (buf_ret != NULL)
232       i++;
233   }
234 }
235
236 /**
237  * gst_buffer_list_get:
238  * @list: a #GstBufferList
239  * @idx: the index
240  *
241  * Get the buffer at @idx.
242  *
243  * Returns: (transfer none): the buffer at @idx in @group or NULL when there
244  *     is no buffer. The buffer remains valid as long as @list is valid.
245  *
246  * Since: 0.10.24
247  */
248 GstBuffer *
249 gst_buffer_list_get (GstBufferList * list, guint idx)
250 {
251   GstBuffer *buf;
252
253   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
254   g_return_val_if_fail (idx < list->array->len, NULL);
255
256   buf = g_array_index (list->array, GstBuffer *, idx);
257
258   return buf;
259 }
260
261 /**
262  * gst_buffer_list_insert:
263  * @list: a #GstBufferList
264  * @idx: the index
265  * @buffer: a #GstBuffer
266  *
267  * Insert @buffer at @idx in @list. Other buffers are moved to make room for
268  * this new buffer.
269  *
270  * A -1 value for @idx will append the buffer at the end.
271  */
272 void
273 gst_buffer_list_insert (GstBufferList * list, guint idx, GstBuffer * buffer)
274 {
275   g_return_if_fail (GST_IS_BUFFER_LIST (list));
276   g_return_if_fail (buffer != NULL);
277
278   if (idx == -1)
279     g_array_append_val (list->array, buffer);
280   else {
281     g_return_if_fail (idx < list->array->len);
282     g_array_insert_val (list->array, idx, buffer);
283   }
284 }
285
286 /**
287  * gst_buffer_list_remove:
288  * @list: a #GstBufferList
289  * @idx: the index
290  * @length: the amount to remove
291  *
292  * Remove @length buffers starting from @idx in @list. The following buffers are
293  * moved to close the gap.
294  */
295 void
296 gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
297 {
298   g_return_if_fail (GST_IS_BUFFER_LIST (list));
299   g_return_if_fail (idx < list->array->len);
300
301   g_array_remove_range (list->array, idx, length);
302 }