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 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 #define GST_DISABLE_MINIOBJECT_INLINE_FUNCTIONS
40 #include "gst_private.h"
42 #include "gstbuffer.h"
43 #include "gstbufferlist.h"
46 #define GST_CAT_DEFAULT GST_CAT_BUFFER_LIST
48 #define GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY(list) \
49 ((list)->buffers != &(list)->arr[0])
54 * Opaque list of grouped buffers.
58 GstMiniObject mini_object;
66 /* one-item array, in reality more items are pre-allocated
67 * as part of the GstBufferList structure, and that
68 * pre-allocated array extends beyond the declared struct */
72 GType _gst_buffer_list_type = 0;
74 GST_DEFINE_MINI_OBJECT_TYPE (GstBufferList, gst_buffer_list);
77 _priv_gst_buffer_list_initialize (void)
79 _gst_buffer_list_type = gst_buffer_list_get_type ();
82 static GstBufferList *
83 _gst_buffer_list_copy (GstBufferList * list)
88 len = list->n_buffers;
89 copy = gst_buffer_list_new_sized (list->n_allocated);
91 /* add and ref all buffers in the array */
92 for (i = 0; i < len; i++) {
93 copy->buffers[i] = gst_buffer_ref (list->buffers[i]);
94 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (copy->buffers[i]),
95 GST_MINI_OBJECT_CAST (copy));
98 copy->n_buffers = len;
104 _gst_buffer_list_free (GstBufferList * list)
109 GST_LOG ("free %p", list);
111 /* unrefs all buffers too */
112 len = list->n_buffers;
113 for (i = 0; i < len; i++) {
114 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (list->buffers[i]),
115 GST_MINI_OBJECT_CAST (list));
116 gst_buffer_unref (list->buffers[i]);
119 if (GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY (list))
120 g_free (list->buffers);
122 slice_size = list->slice_size;
125 memset (list, 0xff, slice_size);
128 g_slice_free1 (slice_size, list);
132 gst_buffer_list_init (GstBufferList * list, guint n_allocated, gsize slice_size)
134 gst_mini_object_init (GST_MINI_OBJECT_CAST (list), 0, _gst_buffer_list_type,
135 (GstMiniObjectCopyFunction) _gst_buffer_list_copy, NULL,
136 (GstMiniObjectFreeFunction) _gst_buffer_list_free);
138 list->buffers = &list->arr[0];
140 list->n_allocated = n_allocated;
141 list->slice_size = slice_size;
143 GST_LOG ("init %p", list);
147 * gst_buffer_list_new_sized:
148 * @size: an initial reserved size
150 * Creates a new, empty #GstBufferList. The list will have @size space
151 * preallocated so that memory reallocations can be avoided.
153 * Returns: (transfer full): the new #GstBufferList.
156 gst_buffer_list_new_sized (guint size)
165 n_allocated = GST_ROUND_UP_16 (size);
167 slice_size = sizeof (GstBufferList) + (n_allocated - 1) * sizeof (gpointer);
169 list = g_slice_alloc0 (slice_size);
171 GST_LOG ("new %p", list);
173 gst_buffer_list_init (list, n_allocated, slice_size);
179 * gst_buffer_list_new:
181 * Creates a new, empty #GstBufferList.
183 * Returns: (transfer full): the new #GstBufferList.
186 gst_buffer_list_new (void)
188 return gst_buffer_list_new_sized (8);
192 * gst_buffer_list_length:
193 * @list: a #GstBufferList
195 * Returns the number of buffers in @list.
197 * Returns: the number of buffers in the buffer list
200 gst_buffer_list_length (GstBufferList * list)
202 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
204 return list->n_buffers;
208 gst_buffer_list_remove_range_internal (GstBufferList * list, guint idx,
209 guint length, gboolean unref_old)
214 for (i = idx; i < idx + length; ++i) {
215 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (list->buffers[i]),
216 GST_MINI_OBJECT_CAST (list));
217 gst_buffer_unref (list->buffers[i]);
221 if (idx + length != list->n_buffers) {
222 memmove (&list->buffers[idx], &list->buffers[idx + length],
223 (list->n_buffers - (idx + length)) * sizeof (void *));
226 list->n_buffers -= length;
230 * gst_buffer_list_foreach:
231 * @list: a #GstBufferList
232 * @func: (scope call): a #GstBufferListFunc to call
233 * @user_data: (closure): user data passed to @func
235 * Calls @func with @data for each buffer in @list.
237 * @func can modify the passed buffer pointer or its contents. The return value
238 * of @func defines if this function returns or if the remaining buffers in
239 * the list should be skipped.
241 * Returns: %TRUE when @func returned %TRUE for each buffer in @list or when
245 gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
250 gboolean list_was_writable, first_warning = TRUE;
252 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), FALSE);
253 g_return_val_if_fail (func != NULL, FALSE);
255 list_was_writable = gst_buffer_list_is_writable (list);
257 len = list->n_buffers;
258 for (i = 0; i < len;) {
259 GstBuffer *buf, *buf_ret;
260 gboolean was_writable;
262 buf = buf_ret = list->buffers[i];
264 /* If the buffer is writable, we remove us as parent for now to
265 * allow the callback to destroy the buffer. If we get the buffer
266 * back, we add ourselves as parent again.
268 * Non-writable buffers just get another reference as they were not
269 * writable to begin with, and they would possibly become writable
270 * by removing ourselves as parent
272 was_writable = list_was_writable && gst_buffer_is_writable (buf);
275 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (buf),
276 GST_MINI_OBJECT_CAST (list));
278 gst_buffer_ref (buf);
280 ret = func (&buf_ret, i, user_data);
282 /* Check if the function changed the buffer */
283 if (buf != buf_ret) {
284 /* If the list was not writable but the callback was actually changing
285 * our buffer, then it wouldn't have been allowed to do so.
287 * Fortunately we still have a reference to the old buffer in that case
288 * and just not modify the list, unref the new buffer (if any) and warn
290 if (!list_was_writable) {
293 ("gst_buffer_list_foreach: non-writable list %p was changed from callback",
295 first_warning = FALSE;
298 gst_buffer_unref (buf_ret);
299 } else if (buf_ret == NULL) {
300 gst_buffer_list_remove_range_internal (list, i, 1, !was_writable);
304 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (buf),
305 GST_MINI_OBJECT_CAST (list));
306 gst_buffer_unref (buf);
309 list->buffers[i] = buf_ret;
310 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buf_ret),
311 GST_MINI_OBJECT_CAST (list));
315 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buf),
316 GST_MINI_OBJECT_CAST (list));
318 gst_buffer_unref (buf);
324 /* If the buffer was not removed by func go to the next buffer */
332 * gst_buffer_list_get:
333 * @list: a #GstBufferList
336 * Gets the buffer at @idx.
338 * You must make sure that @idx does not exceed the number of
341 * Returns: (transfer none) (nullable): the buffer at @idx in @group
342 * or %NULL when there is no buffer. The buffer remains valid as
343 * long as @list is valid and buffer is not removed from the list.
346 gst_buffer_list_get (GstBufferList * list, guint idx)
348 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
349 g_return_val_if_fail (idx < list->n_buffers, NULL);
351 return list->buffers[idx];
355 * gst_buffer_list_get_writable:
356 * @list: a (writable) #GstBufferList
359 * Gets the buffer at @idx, ensuring it is a writable buffer.
361 * You must make sure that @idx does not exceed the number of
364 * Returns: (transfer none) (nullable): the buffer at @idx in @group.
365 * The returned buffer remains valid as long as @list is valid and
366 * the buffer is not removed from the list.
371 gst_buffer_list_get_writable (GstBufferList * list, guint idx)
375 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
376 g_return_val_if_fail (gst_buffer_list_is_writable (list), NULL);
377 g_return_val_if_fail (idx < list->n_buffers, NULL);
379 /* We have to implement this manually here to correctly add/remove the
381 if (gst_buffer_is_writable (list->buffers[idx]))
382 return list->buffers[idx];
384 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (list->buffers[idx]),
385 GST_MINI_OBJECT_CAST (list));
386 new_buf = gst_buffer_copy (list->buffers[idx]);
387 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (new_buf),
388 GST_MINI_OBJECT_CAST (list));
389 gst_buffer_unref (list->buffers[idx]);
390 list->buffers[idx] = new_buf;
396 * gst_buffer_list_add:
397 * @l: a #GstBufferList
400 * Append @b at the end of @l.
403 * gst_buffer_list_insert:
404 * @list: a #GstBufferList
406 * @buffer: (transfer full): a #GstBuffer
408 * Inserts @buffer at @idx in @list. Other buffers are moved to make room for
411 * A -1 value for @idx will append the buffer at the end.
414 gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
418 g_return_if_fail (GST_IS_BUFFER_LIST (list));
419 g_return_if_fail (buffer != NULL);
420 g_return_if_fail (gst_buffer_list_is_writable (list));
422 if (idx == -1 && list->n_buffers < list->n_allocated) {
423 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buffer),
424 GST_MINI_OBJECT_CAST (list));
425 list->buffers[list->n_buffers++] = buffer;
429 if (idx == -1 || idx > list->n_buffers)
430 idx = list->n_buffers;
432 want_alloc = list->n_buffers + 1;
434 if (want_alloc > list->n_allocated) {
435 if (G_UNLIKELY (list->n_allocated > (G_MAXUINT / 2)))
436 g_error ("Growing GstBufferList would result in overflow");
438 want_alloc = MAX (GST_ROUND_UP_16 (want_alloc), list->n_allocated * 2);
440 if (GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY (list)) {
441 list->buffers = g_renew (GstBuffer *, list->buffers, want_alloc);
443 list->buffers = g_new0 (GstBuffer *, want_alloc);
444 memcpy (list->buffers, &list->arr[0], list->n_buffers * sizeof (void *));
445 GST_CAT_LOG (GST_CAT_PERFORMANCE, "exceeding pre-alloced array");
448 list->n_allocated = want_alloc;
451 if (idx < list->n_buffers) {
452 memmove (&list->buffers[idx + 1], &list->buffers[idx],
453 (list->n_buffers - idx) * sizeof (void *));
457 list->buffers[idx] = buffer;
458 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (buffer),
459 GST_MINI_OBJECT_CAST (list));
463 * gst_buffer_list_remove:
464 * @list: a #GstBufferList
466 * @length: the amount to remove
468 * Removes @length buffers starting from @idx in @list. The following buffers
469 * are moved to close the gap.
472 gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
474 g_return_if_fail (GST_IS_BUFFER_LIST (list));
475 g_return_if_fail (idx < list->n_buffers);
476 g_return_if_fail (idx + length <= list->n_buffers);
477 g_return_if_fail (gst_buffer_list_is_writable (list));
479 gst_buffer_list_remove_range_internal (list, idx, length, TRUE);
483 * gst_buffer_list_copy_deep:
484 * @list: a #GstBufferList
486 * Creates a copy of the given buffer list. This will make a newly allocated
487 * copy of the buffers that the source buffer list contains.
489 * Returns: (transfer full): a new copy of @list.
494 gst_buffer_list_copy_deep (const GstBufferList * list)
497 GstBufferList *result = NULL;
499 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
501 result = gst_buffer_list_new ();
503 len = list->n_buffers;
504 for (i = 0; i < len; i++) {
505 GstBuffer *old = list->buffers[i];
506 GstBuffer *new = gst_buffer_copy_deep (old);
508 if (G_LIKELY (new)) {
509 gst_buffer_list_insert (result, i, new);
512 ("Failed to deep copy buffer %p while deep "
513 "copying buffer list %p. Buffer list copy "
514 "will be incomplete", old, list);
522 * gst_buffer_list_calculate_size:
523 * @list: a #GstBufferList
525 * Calculates the size of the data contained in @list by adding the
526 * size of all buffers.
528 * Returns: the size of the data contained in @list in bytes.
533 gst_buffer_list_calculate_size (GstBufferList * list)
539 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
542 buffers = list->buffers;
544 for (i = 0; i < n; ++i)
545 size += gst_buffer_get_size (buffers[i]);
551 * gst_buffer_list_ref: (skip)
552 * @list: a #GstBufferList
554 * Increases the refcount of the given buffer list by one.
556 * Note that the refcount affects the writability of @list and its data, see
557 * gst_buffer_list_make_writable(). It is important to note that keeping
558 * additional references to GstBufferList instances can potentially increase
559 * the number of memcpy operations in a pipeline.
561 * Returns: (transfer full): @list
564 gst_buffer_list_ref (GstBufferList * list)
567 GST_BUFFER_LIST_CAST (gst_mini_object_ref (GST_MINI_OBJECT_CAST (list)));
571 * gst_buffer_list_unref: (skip)
572 * @list: (transfer full): a #GstBufferList
574 * Decreases the refcount of the buffer list. If the refcount reaches 0, the
575 * buffer list will be freed.
578 gst_buffer_list_unref (GstBufferList * list)
580 gst_mini_object_unref (GST_MINI_OBJECT_CAST (list));
584 * gst_clear_buffer_list: (skip)
585 * @list_ptr: a pointer to a #GstBufferList reference
587 * Clears a reference to a #GstBufferList.
589 * @list_ptr must not be %NULL.
591 * If the reference is %NULL then this function does nothing. Otherwise, the
592 * reference count of the list is decreased and the pointer is set to %NULL.
597 gst_clear_buffer_list (GstBufferList ** list_ptr)
599 gst_clear_mini_object ((GstMiniObject **) list_ptr);
603 * gst_buffer_list_copy: (skip)
604 * @list: a #GstBufferList
606 * Creates a shallow copy of the given buffer list. This will make a newly
607 * allocated copy of the source list with copies of buffer pointers. The
608 * refcount of buffers pointed to will be increased by one.
610 * Returns: (transfer full): a new copy of @list.
613 gst_buffer_list_copy (const GstBufferList * list)
616 GST_BUFFER_LIST_CAST (gst_mini_object_copy (GST_MINI_OBJECT_CONST_CAST
621 * gst_buffer_list_replace:
622 * @old_list: (inout) (transfer full) (nullable): pointer to a pointer to a
623 * #GstBufferList to be replaced.
624 * @new_list: (transfer none) (allow-none): pointer to a #GstBufferList that
625 * will replace the buffer list pointed to by @old_list.
627 * Modifies a pointer to a #GstBufferList to point to a different
628 * #GstBufferList. The modification is done atomically (so this is useful for
629 * ensuring thread safety in some cases), and the reference counts are updated
630 * appropriately (the old buffer list is unreffed, the new is reffed).
632 * Either @new_list or the #GstBufferList pointed to by @old_list may be %NULL.
634 * Returns: %TRUE if @new_list was different from @old_list
639 gst_buffer_list_replace (GstBufferList ** old_list, GstBufferList * new_list)
641 return gst_mini_object_replace ((GstMiniObject **) old_list,
642 (GstMiniObject *) new_list);
646 * gst_buffer_list_take:
647 * @old_list: (inout) (transfer full): pointer to a pointer to a #GstBufferList
649 * @new_list: (transfer full) (allow-none): pointer to a #GstBufferList
650 * that will replace the bufferlist pointed to by @old_list.
652 * Modifies a pointer to a #GstBufferList to point to a different
653 * #GstBufferList. This function is similar to gst_buffer_list_replace() except
654 * that it takes ownership of @new_list.
656 * Returns: %TRUE if @new_list was different from @old_list
661 gst_buffer_list_take (GstBufferList ** old_list, GstBufferList * new_list)
663 return gst_mini_object_take ((GstMiniObject **) old_list,
664 (GstMiniObject *) new_list);