2 * Copyright (C) 2009 Axis Communications <dev-gstreamer at axis dot com>
3 * @author Jonas Holmberg <jonas dot holmberg at axis dot com>
4 * Copyright (C) 2014 Tim-Philipp Müller <tim at centricular dot com>
6 * gstbufferlist.c: Buffer list
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:gstbufferlist
26 * @title: GstBufferList
27 * @short_description: Lists of buffers for data-passing
28 * @see_also: #GstPad, #GstMiniObject
30 * Buffer lists are an object containing a list of buffers.
32 * Buffer lists are created with gst_buffer_list_new() and filled with data
33 * using a gst_buffer_list_insert().
35 * Buffer lists can be pushed on a srcpad with gst_pad_push_list(). This is
36 * interesting when multiple buffers need to be pushed in one go because it
37 * can reduce the amount of overhead for pushing each buffer individually.
39 #include "gst_private.h"
41 #include "gstbuffer.h"
42 #include "gstbufferlist.h"
45 #define GST_CAT_DEFAULT GST_CAT_BUFFER_LIST
47 #define GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY(list) \
48 ((list)->buffers != &(list)->arr[0])
53 * Opaque list of grouped buffers.
57 GstMiniObject mini_object;
65 /* one-item array, in reality more items are pre-allocated
66 * as part of the GstBufferList structure, and that
67 * pre-allocated array extends beyond the declared struct */
71 GType _gst_buffer_list_type = 0;
73 GST_DEFINE_MINI_OBJECT_TYPE (GstBufferList, gst_buffer_list);
76 _priv_gst_buffer_list_initialize (void)
78 _gst_buffer_list_type = gst_buffer_list_get_type ();
81 static GstBufferList *
82 _gst_buffer_list_copy (GstBufferList * list)
87 len = list->n_buffers;
88 copy = gst_buffer_list_new_sized (list->n_allocated);
90 /* add and ref all buffers in the array */
91 for (i = 0; i < len; i++) {
92 copy->buffers[i] = gst_buffer_ref (list->buffers[i]);
93 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (copy->buffers[i]),
94 GST_MINI_OBJECT_CAST (copy));
97 copy->n_buffers = len;
103 _gst_buffer_list_free (GstBufferList * list)
108 GST_LOG ("free %p", list);
110 /* unrefs all buffers too */
111 len = list->n_buffers;
112 for (i = 0; i < len; i++) {
113 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (list->buffers[i]),
114 GST_MINI_OBJECT_CAST (list));
115 gst_buffer_unref (list->buffers[i]);
118 if (GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY (list))
119 g_free (list->buffers);
121 slice_size = list->slice_size;
124 memset (list, 0xff, slice_size);
127 g_slice_free1 (slice_size, list);
131 gst_buffer_list_init (GstBufferList * list, guint n_allocated, gsize slice_size)
133 gst_mini_object_init (GST_MINI_OBJECT_CAST (list), 0, _gst_buffer_list_type,
134 (GstMiniObjectCopyFunction) _gst_buffer_list_copy, NULL,
135 (GstMiniObjectFreeFunction) _gst_buffer_list_free);
137 list->buffers = &list->arr[0];
139 list->n_allocated = n_allocated;
140 list->slice_size = slice_size;
142 GST_LOG ("init %p", list);
146 * gst_buffer_list_new_sized:
147 * @size: an initial reserved size
149 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
150 * the returned #GstBufferList. The list will have @size space preallocated so
151 * that memory reallocations can be avoided.
153 * Free-function: gst_buffer_list_unref
155 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
159 gst_buffer_list_new_sized (guint size)
168 n_allocated = GST_ROUND_UP_16 (size);
170 slice_size = sizeof (GstBufferList) + (n_allocated - 1) * sizeof (gpointer);
172 list = g_slice_alloc0 (slice_size);
174 GST_LOG ("new %p", list);
176 gst_buffer_list_init (list, n_allocated, slice_size);
182 * gst_buffer_list_new:
184 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
185 * the returned #GstBufferList.
187 * Free-function: gst_buffer_list_unref
189 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
193 gst_buffer_list_new (void)
195 return gst_buffer_list_new_sized (8);
199 * gst_buffer_list_length:
200 * @list: a #GstBufferList
202 * Returns the number of buffers in @list.
204 * Returns: the number of buffers in the buffer list
207 gst_buffer_list_length (GstBufferList * list)
209 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
211 return list->n_buffers;
215 gst_buffer_list_remove_range_internal (GstBufferList * list, guint idx,
216 guint length, gboolean unref_old)
221 for (i = idx; i < idx + length; ++i) {
222 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (list->buffers[i]),
223 GST_MINI_OBJECT_CAST (list));
224 gst_buffer_unref (list->buffers[i]);
228 if (idx + length != list->n_buffers) {
229 memmove (&list->buffers[idx], &list->buffers[idx + length],
230 (list->n_buffers - (idx + length)) * sizeof (void *));
233 list->n_buffers -= length;
237 * gst_buffer_list_foreach:
238 * @list: a #GstBufferList
239 * @func: (scope call): a #GstBufferListFunc to call
240 * @user_data: (closure): user data passed to @func
242 * Call @func with @data for each buffer in @list.
244 * @func can modify the passed buffer pointer or its contents. The return value
245 * of @func define if this function returns or if the remaining buffers in
246 * the list should be skipped.
248 * Returns: %TRUE when @func returned %TRUE for each buffer in @list or when
252 gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
257 gboolean list_was_writable, first_warning = TRUE;
259 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), FALSE);
260 g_return_val_if_fail (func != NULL, FALSE);
262 list_was_writable = gst_buffer_list_is_writable (list);
264 len = list->n_buffers;
265 for (i = 0; i < len;) {
266 GstBuffer *buf, *buf_ret;
267 gboolean was_writable;
269 buf = buf_ret = list->buffers[i];
271 /* If the buffer is writable, we remove us as parent for now to
272 * allow the callback to destroy the buffer. If we get the buffer
273 * back, we add ourselves as parent again.
275 * Non-writable buffers just get another reference as they were not
276 * writable to begin with, and they would possibly become writable
277 * by removing ourselves as parent
279 was_writable = list_was_writable && gst_buffer_is_writable (buf);
282 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (buf),
283 GST_MINI_OBJECT_CAST (list));
285 gst_buffer_ref (buf);
287 ret = func (&buf_ret, i, user_data);
289 /* Check if the function changed the buffer */
290 if (buf != buf_ret) {
291 /* If the list was not writable but the callback was actually changing
292 * our buffer, then it wouldn't have been allowed to do so.
294 * Fortunately we still have a reference to the old buffer in that case
295 * and just not modify the list, unref the new buffer (if any) and warn
297 if (!list_was_writable) {
300 ("gst_buffer_list_foreach: non-writable list %p was changed from callback",
302 first_warning = FALSE;
305 gst_buffer_unref (buf_ret);
306 } else if (buf_ret == NULL) {
307 gst_buffer_list_remove_range_internal (list, i, 1, !was_writable);
311 gst_buffer_unref (buf);
313 list->buffers[i] = buf_ret;
314 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buf_ret),
315 GST_MINI_OBJECT_CAST (list));
319 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buf),
320 GST_MINI_OBJECT_CAST (list));
322 gst_buffer_unref (buf);
328 /* If the buffer was not removed by func go to the next buffer */
336 * gst_buffer_list_get:
337 * @list: a #GstBufferList
340 * Get the buffer at @idx.
342 * You must make sure that @idx does not exceed the number of
345 * Returns: (transfer none) (nullable): the buffer at @idx in @group
346 * or %NULL when there is no buffer. The buffer remains valid as
347 * long as @list is valid and buffer is not removed from the list.
350 gst_buffer_list_get (GstBufferList * list, guint idx)
352 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
353 g_return_val_if_fail (idx < list->n_buffers, NULL);
355 return list->buffers[idx];
359 * gst_buffer_list_get_writable:
360 * @list: a (writable) #GstBufferList
363 * Gets the buffer at @idx, ensuring it is a writable buffer.
365 * You must make sure that @idx does not exceed the number of
368 * Returns: (transfer none) (nullable): the buffer at @idx in @group.
369 * The returned buffer remains valid as long as @list is valid and
370 * the buffer is not removed from the list.
375 gst_buffer_list_get_writable (GstBufferList * list, guint idx)
379 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
380 g_return_val_if_fail (gst_buffer_list_is_writable (list), NULL);
381 g_return_val_if_fail (idx < list->n_buffers, NULL);
383 /* We have to implement this manually here to correctly add/remove the
385 if (gst_buffer_is_writable (list->buffers[idx]))
386 return list->buffers[idx];
388 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (list->buffers[idx]),
389 GST_MINI_OBJECT_CAST (list));
390 new_buf = gst_buffer_copy (list->buffers[idx]);
391 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (new_buf),
392 GST_MINI_OBJECT_CAST (list));
393 gst_buffer_unref (list->buffers[idx]);
394 list->buffers[idx] = new_buf;
400 * gst_buffer_list_add:
401 * @l: a #GstBufferList
404 * Append @b at the end of @l.
407 * gst_buffer_list_insert:
408 * @list: a #GstBufferList
410 * @buffer: (transfer full): a #GstBuffer
412 * Insert @buffer at @idx in @list. Other buffers are moved to make room for
415 * A -1 value for @idx will append the buffer at the end.
418 gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
422 g_return_if_fail (GST_IS_BUFFER_LIST (list));
423 g_return_if_fail (buffer != NULL);
424 g_return_if_fail (gst_buffer_list_is_writable (list));
426 if (idx == -1 && list->n_buffers < list->n_allocated) {
427 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buffer),
428 GST_MINI_OBJECT_CAST (list));
429 list->buffers[list->n_buffers++] = buffer;
433 if (idx == -1 || idx > list->n_buffers)
434 idx = list->n_buffers;
436 want_alloc = list->n_buffers + 1;
438 if (want_alloc > list->n_allocated) {
439 want_alloc = MAX (GST_ROUND_UP_16 (want_alloc), list->n_allocated * 2);
441 if (GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY (list)) {
442 list->buffers = g_renew (GstBuffer *, list->buffers, want_alloc);
444 list->buffers = g_new0 (GstBuffer *, want_alloc);
445 memcpy (list->buffers, &list->arr[0], list->n_buffers * sizeof (void *));
446 GST_CAT_LOG (GST_CAT_PERFORMANCE, "exceeding pre-alloced array");
449 list->n_allocated = want_alloc;
452 if (idx < list->n_buffers) {
453 memmove (&list->buffers[idx + 1], &list->buffers[idx],
454 (list->n_buffers - idx) * sizeof (void *));
458 list->buffers[idx] = buffer;
459 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buffer),
460 GST_MINI_OBJECT_CAST (list));
464 * gst_buffer_list_remove:
465 * @list: a #GstBufferList
467 * @length: the amount to remove
469 * Remove @length buffers starting from @idx in @list. The following buffers
470 * are moved to close the gap.
473 gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
475 g_return_if_fail (GST_IS_BUFFER_LIST (list));
476 g_return_if_fail (idx < list->n_buffers);
477 g_return_if_fail (idx + length <= list->n_buffers);
478 g_return_if_fail (gst_buffer_list_is_writable (list));
480 gst_buffer_list_remove_range_internal (list, idx, length, TRUE);
484 * gst_buffer_list_copy_deep:
485 * @list: a #GstBufferList
487 * Create a copy of the given buffer list. This will make a newly allocated
488 * copy of the buffer that the source buffer list contains.
490 * Returns: (transfer full): a new copy of @list.
495 gst_buffer_list_copy_deep (const GstBufferList * list)
498 GstBufferList *result = NULL;
500 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
502 result = gst_buffer_list_new ();
504 len = list->n_buffers;
505 for (i = 0; i < len; i++) {
506 GstBuffer *old = list->buffers[i];
507 GstBuffer *new = gst_buffer_copy_deep (old);
509 if (G_LIKELY (new)) {
510 gst_buffer_list_insert (result, i, new);
513 ("Failed to deep copy buffer %p while deep "
514 "copying buffer list %p. Buffer list copy "
515 "will be incomplete", old, list);
523 * gst_buffer_list_calculate_size:
524 * @list: a #GstBufferList
526 * Calculates the size of the data contained in buffer list by adding the
527 * size of all buffers.
529 * Returns: the size of the data contained in buffer list in bytes.
534 gst_buffer_list_calculate_size (GstBufferList * list)
540 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
543 buffers = list->buffers;
545 for (i = 0; i < n; ++i)
546 size += gst_buffer_get_size (buffers[i]);