Fix FSF address
[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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, 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 struct _GstBufferList
52 {
53   GstMiniObject mini_object;
54
55   GArray *array;
56 };
57
58 GType _gst_buffer_list_type = 0;
59
60 GST_DEFINE_MINI_OBJECT_TYPE (GstBufferList, gst_buffer_list);
61
62 void
63 _priv_gst_buffer_list_initialize (void)
64 {
65   _gst_buffer_list_type = gst_buffer_list_get_type ();
66 }
67
68 static GstBufferList *
69 _gst_buffer_list_copy (GstBufferList * list)
70 {
71   GstBufferList *copy;
72   guint i, len;
73
74   len = list->array->len;
75   copy = gst_buffer_list_new_sized (len);
76
77   /* add and ref all buffers in the array */
78   for (i = 0; i < len; i++) {
79     GstBuffer *buf = g_array_index (list->array, GstBuffer *, i);
80     buf = gst_buffer_ref (buf);
81     g_array_append_val (copy->array, buf);
82   }
83   return copy;
84 }
85
86 static void
87 _gst_buffer_list_free (GstBufferList * list)
88 {
89   guint i, len;
90   GST_LOG ("free %p", list);
91
92   /* unrefs all buffers too */
93   len = list->array->len;
94   for (i = 0; i < len; i++)
95     gst_buffer_unref (g_array_index (list->array, GstBuffer *, i));
96   g_array_free (list->array, TRUE);
97
98   g_slice_free1 (sizeof (GstBufferList), list);
99 }
100
101 static void
102 gst_buffer_list_init (GstBufferList * list, guint asize)
103 {
104   gst_mini_object_init (GST_MINI_OBJECT_CAST (list), 0, _gst_buffer_list_type,
105       (GstMiniObjectCopyFunction) _gst_buffer_list_copy, NULL,
106       (GstMiniObjectFreeFunction) _gst_buffer_list_free);
107
108   list->array = g_array_sized_new (FALSE, FALSE, sizeof (GstBuffer *), asize);
109
110   GST_LOG ("init %p", list);
111 }
112
113 /**
114  * gst_buffer_list_new_sized:
115  * @size: an initial reserved size
116  *
117  * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
118  * the returned #GstBufferList. The list will have @size space preallocated so
119  * that memory reallocations can be avoided.
120  *
121  * Free-function: gst_buffer_list_unref
122  *
123  * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
124  *     after usage.
125  */
126 GstBufferList *
127 gst_buffer_list_new_sized (guint size)
128 {
129   GstBufferList *list;
130
131   list = g_slice_new0 (GstBufferList);
132
133   GST_LOG ("new %p", list);
134
135   gst_buffer_list_init (list, size);
136
137   return list;
138 }
139
140 /**
141  * gst_buffer_list_new:
142  *
143  * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
144  * the returned #GstBufferList.
145  *
146  * Free-function: gst_buffer_list_unref
147  *
148  * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
149  *     after usage.
150  */
151 GstBufferList *
152 gst_buffer_list_new (void)
153 {
154   return gst_buffer_list_new_sized (8);
155 }
156
157 /**
158  * gst_buffer_list_length:
159  * @list: a #GstBufferList
160  *
161  * Returns the number of buffers in @list.
162  *
163  * Returns: the number of buffers in the buffer list
164  */
165 guint
166 gst_buffer_list_length (GstBufferList * list)
167 {
168   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
169
170   return list->array->len;
171 }
172
173 /**
174  * gst_buffer_list_foreach:
175  * @list: a #GstBufferList
176  * @func: (scope call): a #GstBufferListFunc to call
177  * @user_data: (closure): user data passed to @func
178  *
179  * Call @func with @data for each buffer in @list.
180  *
181  * @func can modify the passed buffer pointer or its contents. The return value
182  * of @func define if this function returns or if the remaining buffers in
183  * the list should be skipped.
184  *
185  * Returns: %TRUE when @func returned %TRUE for each buffer in @list or when
186  * @list is empty.
187  */
188 gboolean
189 gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
190     gpointer user_data)
191 {
192   guint i, len;
193   gboolean ret = TRUE;
194
195   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), FALSE);
196   g_return_val_if_fail (func != NULL, FALSE);
197
198   len = list->array->len;
199   for (i = 0; i < len;) {
200     GstBuffer *buf, *buf_ret;
201
202     buf = buf_ret = g_array_index (list->array, GstBuffer *, i);
203     ret = func (&buf_ret, i, user_data);
204
205     /* Check if the function changed the buffer */
206     if (buf != buf_ret) {
207       if (buf_ret == NULL) {
208         g_array_remove_index (list->array, i);
209         len--;
210       } else {
211         g_array_index (list->array, GstBuffer *, i) = buf_ret;
212       }
213     }
214
215     if (!ret)
216       break;
217
218     /* If the buffer was not removed by func go to the next buffer */
219     if (buf_ret != NULL)
220       i++;
221   }
222   return ret;
223 }
224
225 /**
226  * gst_buffer_list_get:
227  * @list: a #GstBufferList
228  * @idx: the index
229  *
230  * Get the buffer at @idx.
231  *
232  * Returns: (transfer none): the buffer at @idx in @group or NULL when there
233  *     is no buffer. The buffer remains valid as long as @list is valid.
234  */
235 GstBuffer *
236 gst_buffer_list_get (GstBufferList * list, guint idx)
237 {
238   GstBuffer *buf;
239
240   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
241   g_return_val_if_fail (idx < list->array->len, NULL);
242
243   buf = g_array_index (list->array, GstBuffer *, idx);
244
245   return buf;
246 }
247
248 /**
249  * gst_buffer_list_add:
250  * @l: a #GstBufferList
251  * @b: a #GstBuffer
252  *
253  * Append @b at the end of @l.
254  */
255 /**
256  * gst_buffer_list_insert:
257  * @list: a #GstBufferList
258  * @idx: the index
259  * @buffer: (transfer full): a #GstBuffer
260  *
261  * Insert @buffer at @idx in @list. Other buffers are moved to make room for
262  * this new buffer.
263  *
264  * A -1 value for @idx will append the buffer at the end.
265  */
266 void
267 gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
268 {
269   g_return_if_fail (GST_IS_BUFFER_LIST (list));
270   g_return_if_fail (buffer != NULL);
271
272   if (idx == -1)
273     g_array_append_val (list->array, buffer);
274   else {
275     g_return_if_fail (idx < list->array->len);
276     g_array_insert_val (list->array, idx, buffer);
277   }
278 }
279
280 /**
281  * gst_buffer_list_remove:
282  * @list: a #GstBufferList
283  * @idx: the index
284  * @length: the amount to remove
285  *
286  * Remove @length buffers starting from @idx in @list. The following buffers are
287  * moved to close the gap.
288  */
289 void
290 gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
291 {
292   g_return_if_fail (GST_IS_BUFFER_LIST (list));
293   g_return_if_fail (idx < list->array->len);
294
295   g_array_remove_range (list->array, idx, length);
296 }