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
43 * @mini_object: the parent structure
45 * Opaque list of grouped buffers.
51 GstMiniObject mini_object;
56 GType _gst_buffer_list_type = 0;
59 _gst_buffer_list_initialize (void)
61 if (G_LIKELY (_gst_buffer_list_type == 0)) {
62 _gst_buffer_list_type = gst_mini_object_register ("GstBufferList");
66 static GstBufferList *
67 _gst_buffer_list_copy (GstBufferList * list)
72 len = list->array->len;
73 copy = gst_buffer_list_sized_new (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 (GST_MINI_OBJECT_SIZE (list), list);
100 gst_buffer_list_init (GstBufferList * list, gsize size, guint asize)
102 gst_mini_object_init (GST_MINI_OBJECT_CAST (list), _gst_buffer_list_type,
105 list->mini_object.copy = (GstMiniObjectCopyFunction) _gst_buffer_list_copy;
106 list->mini_object.free = (GstMiniObjectFreeFunction) _gst_buffer_list_free;
108 list->array = g_array_sized_new (FALSE, FALSE, sizeof (GstBuffer *), asize);
110 GST_LOG ("init %p", list);
114 * gst_buffer_list_sized_new:
115 * @size: an initial reserved size
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.
121 * Free-function: gst_buffer_list_unref
123 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
129 gst_buffer_list_sized_new (guint size)
133 list = g_slice_new0 (GstBufferList);
135 GST_LOG ("new %p", list);
137 gst_buffer_list_init (list, sizeof (GstBufferList), size);
143 * gst_buffer_list_new:
145 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
146 * the returned #GstBufferList.
148 * Free-function: gst_buffer_list_unref
150 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
156 gst_buffer_list_new (void)
158 return gst_buffer_list_sized_new (8);
162 * gst_buffer_list_len:
163 * @list: a #GstBufferList
165 * Returns the number of buffers in @list.
167 * Returns: the number of buffers in the buffer list
172 gst_buffer_list_len (GstBufferList * list)
174 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
176 return list->array->len;
180 * gst_buffer_list_foreach:
181 * @list: a #GstBufferList
182 * @func: (scope call): a #GstBufferListFunc to call
183 * @user_data: user data passed to @func
185 * Call @func with @data for each buffer in @list.
187 * @func can modify the passed buffer pointer or its contents. The return value
188 * of @func define if this function returns or if the remaining buffers in a
189 * group should be skipped.
194 gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
200 * gst_buffer_list_get:
201 * @list: a #GstBufferList
204 * Get the buffer at @idx.
206 * Returns: (transfer none): the buffer at @idx in @group or NULL when there
207 * is no buffer. The buffer remains valid as long as @list is valid.
212 gst_buffer_list_get (GstBufferList * list, guint idx)
216 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
217 g_return_val_if_fail (idx < list->array->len, NULL);
219 buf = g_array_index (list->array, GstBuffer *, idx);
225 * gst_buffer_list_insert:
226 * @list: a #GstBufferList
228 * @buffer: a #GstBuffer
230 * Insert @buffer at @idx in @list. Other buffers are moved to make room for
233 * A -1 value for @idx will append the buffer at the end.
236 gst_buffer_list_insert (GstBufferList * list, guint idx, GstBuffer * buffer)
238 g_return_if_fail (GST_IS_BUFFER_LIST (list));
239 g_return_if_fail (buffer != NULL);
242 g_array_append_val (list->array, buffer);
244 g_return_if_fail (idx < list->array->len);
245 g_array_insert_val (list->array, idx, buffer);
250 gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
252 g_return_if_fail (GST_IS_BUFFER_LIST (list));
253 g_return_if_fail (idx < list->array->len);
255 g_array_remove_range (list->array, idx, length);