2 * Copyright (C) 2009 Axis Communications <dev-gstreamer at axis dot com>
3 * @author Jonas Holmberg <jonas dot holmberg at axis dot com>
5 * gstbufferlist.c: Buffer list
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.
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.
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.
24 * SECTION:gstbufferlist
25 * @short_description: Lists of buffers for data-passing
26 * @see_also: #GstPad, #GstMiniObject
28 * Buffer lists are an object containing a list of buffers.
30 * Buffer lists are created with gst_buffer_list_new() and filled with data
31 * using a gst_buffer_list_insert().
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.
37 #include "gst_private.h"
39 #include "gstbuffer.h"
40 #include "gstbufferlist.h"
42 #define GST_CAT_DEFAULT GST_CAT_BUFFER_LIST
47 * Opaque list of grouped buffers.
51 GstMiniObject mini_object;
56 GType _gst_buffer_list_type = 0;
58 GST_DEFINE_MINI_OBJECT_TYPE (GstBufferList, gst_buffer_list);
61 _priv_gst_buffer_list_initialize (void)
63 _gst_buffer_list_type = gst_buffer_list_get_type ();
66 static GstBufferList *
67 _gst_buffer_list_copy (GstBufferList * list)
72 len = list->array->len;
73 copy = gst_buffer_list_new_sized (len);
75 /* add and ref all buffers in the array */
76 for (i = 0; i < len; i++) {
77 GstBuffer *buf = g_array_index (list->array, GstBuffer *, i);
78 buf = gst_buffer_ref (buf);
79 g_array_append_val (copy->array, buf);
85 _gst_buffer_list_free (GstBufferList * list)
88 GST_LOG ("free %p", list);
90 /* unrefs all buffers too */
91 len = list->array->len;
92 for (i = 0; i < len; i++)
93 gst_buffer_unref (g_array_index (list->array, GstBuffer *, i));
94 g_array_free (list->array, TRUE);
96 g_slice_free1 (sizeof (GstBufferList), list);
100 gst_buffer_list_init (GstBufferList * list, guint asize)
102 gst_mini_object_init (GST_MINI_OBJECT_CAST (list), 0, _gst_buffer_list_type,
103 (GstMiniObjectCopyFunction) _gst_buffer_list_copy, NULL,
104 (GstMiniObjectFreeFunction) _gst_buffer_list_free);
106 list->array = g_array_sized_new (FALSE, FALSE, sizeof (GstBuffer *), asize);
108 GST_LOG ("init %p", list);
112 * gst_buffer_list_new_sized:
113 * @size: an initial reserved size
115 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
116 * the returned #GstBufferList. The list will have @size space preallocated so
117 * that memory reallocations can be avoided.
119 * Free-function: gst_buffer_list_unref
121 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
125 gst_buffer_list_new_sized (guint size)
129 list = g_slice_new0 (GstBufferList);
131 GST_LOG ("new %p", list);
133 gst_buffer_list_init (list, size);
139 * gst_buffer_list_new:
141 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
142 * the returned #GstBufferList.
144 * Free-function: gst_buffer_list_unref
146 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
150 gst_buffer_list_new (void)
152 return gst_buffer_list_new_sized (8);
156 * gst_buffer_list_length:
157 * @list: a #GstBufferList
159 * Returns the number of buffers in @list.
161 * Returns: the number of buffers in the buffer list
164 gst_buffer_list_length (GstBufferList * list)
166 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
168 return list->array->len;
172 * gst_buffer_list_foreach:
173 * @list: a #GstBufferList
174 * @func: (scope call): a #GstBufferListFunc to call
175 * @user_data: (closure): user data passed to @func
177 * Call @func with @data for each buffer in @list.
179 * @func can modify the passed buffer pointer or its contents. The return value
180 * of @func define if this function returns or if the remaining buffers in
181 * the list should be skipped.
183 * Returns: %TRUE when @func returned %TRUE for each buffer in @list or when
187 gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
193 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), FALSE);
194 g_return_val_if_fail (func != NULL, FALSE);
196 len = list->array->len;
197 for (i = 0; i < len;) {
198 GstBuffer *buf, *buf_ret;
200 buf = buf_ret = g_array_index (list->array, GstBuffer *, i);
201 ret = func (&buf_ret, i, user_data);
203 /* Check if the function changed the buffer */
204 if (buf != buf_ret) {
205 if (buf_ret == NULL) {
206 g_array_remove_index (list->array, i);
209 g_array_index (list->array, GstBuffer *, i) = buf_ret;
216 /* If the buffer was not removed by func go to the next buffer */
224 * gst_buffer_list_get:
225 * @list: a #GstBufferList
228 * Get the buffer at @idx.
230 * Returns: (transfer none) (nullable): the buffer at @idx in @group
231 * or %NULL when there is no buffer. The buffer remains valid as
232 * long as @list is valid.
235 gst_buffer_list_get (GstBufferList * list, guint idx)
239 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
240 g_return_val_if_fail (idx < list->array->len, NULL);
242 buf = g_array_index (list->array, GstBuffer *, idx);
248 * gst_buffer_list_add:
249 * @l: a #GstBufferList
252 * Append @b at the end of @l.
255 * gst_buffer_list_insert:
256 * @list: a #GstBufferList
258 * @buffer: (transfer full): a #GstBuffer
260 * Insert @buffer at @idx in @list. Other buffers are moved to make room for
263 * A -1 value for @idx will append the buffer at the end.
266 gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
268 g_return_if_fail (GST_IS_BUFFER_LIST (list));
269 g_return_if_fail (buffer != NULL);
272 g_array_append_val (list->array, buffer);
274 g_return_if_fail (idx < list->array->len);
275 g_array_insert_val (list->array, idx, buffer);
280 * gst_buffer_list_remove:
281 * @list: a #GstBufferList
283 * @length: the amount to remove
285 * Remove @length buffers starting from @idx in @list. The following buffers
286 * are moved to close the gap.
289 gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
294 g_return_if_fail (GST_IS_BUFFER_LIST (list));
295 g_return_if_fail (idx < list->array->len);
297 for (i = idx; i < idx + length; ++i) {
298 buf = g_array_index (list->array, GstBuffer *, i);
299 gst_buffer_unref (buf);
301 g_array_remove_range (list->array, idx, length);