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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, 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_take().
34 #include "gst_private.h"
36 #include "gstbuffer.h"
37 #include "gstbufferlist.h"
39 #define GST_CAT_DEFAULT GST_CAT_BUFFER_LIST
44 * Opaque list of grouped buffers.
50 GstMiniObject mini_object;
55 GType _gst_buffer_list_type = 0;
58 _gst_buffer_list_initialize (void)
60 if (G_LIKELY (_gst_buffer_list_type == 0)) {
61 _gst_buffer_list_type = gst_mini_object_register ("GstBufferList");
65 static GstBufferList *
66 _gst_buffer_list_copy (GstBufferList * list)
71 len = list->array->len;
72 copy = gst_buffer_list_sized_new (len);
74 /* add and ref all buffers in the array */
75 for (i = 0; i < len; i++) {
76 GstBuffer *buf = g_array_index (list->array, GstBuffer *, i);
77 buf = gst_buffer_ref (buf);
78 g_array_append_val (copy->array, buf);
84 _gst_buffer_list_free (GstBufferList * list)
87 GST_LOG ("free %p", list);
89 /* unrefs all buffers too */
90 len = list->array->len;
91 for (i = 0; i < len; i++)
92 gst_buffer_unref (g_array_index (list->array, GstBuffer *, i));
93 g_array_free (list->array, TRUE);
95 g_slice_free1 (GST_MINI_OBJECT_SIZE (list), list);
99 gst_buffer_list_init (GstBufferList * list, gsize size, guint asize)
101 gst_mini_object_init (GST_MINI_OBJECT_CAST (list), _gst_buffer_list_type,
104 list->mini_object.copy = (GstMiniObjectCopyFunction) _gst_buffer_list_copy;
105 list->mini_object.free = (GstMiniObjectFreeFunction) _gst_buffer_list_free;
107 list->array = g_array_sized_new (FALSE, FALSE, sizeof (GstBuffer *), asize);
109 GST_LOG ("init %p", list);
113 * gst_buffer_list_sized_new:
114 * @size: an initial reserved size
116 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
117 * the returned #GstBufferList. The list will have @size space preallocated so
118 * that memory reallocations can be avoided.
120 * Free-function: gst_buffer_list_unref
122 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
128 gst_buffer_list_sized_new (guint size)
132 list = g_slice_new0 (GstBufferList);
134 GST_LOG ("new %p", list);
136 gst_buffer_list_init (list, sizeof (GstBufferList), size);
142 * gst_buffer_list_new:
144 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
145 * the returned #GstBufferList.
147 * Free-function: gst_buffer_list_unref
149 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
155 gst_buffer_list_new (void)
157 return gst_buffer_list_sized_new (8);
161 * gst_buffer_list_len:
162 * @list: a #GstBufferList
164 * Returns the number of buffers in @list.
166 * Returns: the number of buffers in the buffer list
171 gst_buffer_list_len (GstBufferList * list)
173 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
175 return list->array->len;
179 * gst_buffer_list_foreach:
180 * @list: a #GstBufferList
181 * @func: (scope call): a #GstBufferListFunc to call
182 * @user_data: (closure): user data passed to @func
184 * Call @func with @data for each buffer in @list.
186 * @func can modify the passed buffer pointer or its contents. The return value
187 * of @func define if this function returns or if the remaining buffers in a
188 * group should be skipped.
193 gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
198 g_return_if_fail (GST_IS_BUFFER_LIST (list));
199 g_return_if_fail (func != NULL);
201 len = list->array->len;
202 for (i = 0; i < len;) {
203 GstBuffer *buf, *buf_ret;
206 buf = buf_ret = g_array_index (list->array, GstBuffer *, i);
207 ret = func (&buf_ret, i, user_data);
209 /* Check if the function changed the buffer */
210 if (buf != buf_ret) {
211 if (buf_ret == NULL) {
212 g_array_remove_index (list->array, i);
214 g_array_index (list->array, GstBuffer *, i) = buf_ret;
221 /* If the buffer was not removed by func go to the next buffer */
228 * gst_buffer_list_get:
229 * @list: a #GstBufferList
232 * Get the buffer at @idx.
234 * Returns: (transfer none): the buffer at @idx in @group or NULL when there
235 * is no buffer. The buffer remains valid as long as @list is valid.
240 gst_buffer_list_get (GstBufferList * list, guint idx)
244 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
245 g_return_val_if_fail (idx < list->array->len, NULL);
247 buf = g_array_index (list->array, GstBuffer *, idx);
253 * gst_buffer_list_insert:
254 * @list: a #GstBufferList
256 * @buffer: a #GstBuffer
258 * Insert @buffer at @idx in @list. Other buffers are moved to make room for
261 * A -1 value for @idx will append the buffer at the end.
264 gst_buffer_list_insert (GstBufferList * list, guint idx, GstBuffer * buffer)
266 g_return_if_fail (GST_IS_BUFFER_LIST (list));
267 g_return_if_fail (buffer != NULL);
270 g_array_append_val (list->array, buffer);
272 g_return_if_fail (idx < list->array->len);
273 g_array_insert_val (list->array, idx, buffer);
278 gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
280 g_return_if_fail (GST_IS_BUFFER_LIST (list));
281 g_return_if_fail (idx < list->array->len);
283 g_array_remove_range (list->array, idx, length);