2009-05-12 09:51:37 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2009 Axis Communications <dev-gstreamer at axis dot com>
|
|
|
|
* @author Jonas Holmberg <jonas dot holmberg at axis dot com>
|
|
|
|
*
|
|
|
|
* gstbufferlist.c: Buffer list
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gstbufferlist
|
2011-03-31 15:51:02 +00:00
|
|
|
* @short_description: Lists of buffers for data-passing
|
2009-05-12 09:51:37 +00:00
|
|
|
* @see_also: #GstPad, #GstMiniObject
|
|
|
|
*
|
2011-03-31 15:51:02 +00:00
|
|
|
* Buffer lists are an object containing a list of buffers.
|
2009-05-12 09:51:37 +00:00
|
|
|
*
|
|
|
|
* Buffer lists are created with gst_buffer_list_new() and filled with data
|
2011-09-26 18:47:35 +00:00
|
|
|
* using a gst_buffer_list_insert().
|
2009-05-12 09:51:37 +00:00
|
|
|
*
|
2012-03-28 16:12:23 +00:00
|
|
|
* Buffer lists can be pushed on a srcpad with gst_pad_push_list(). This is
|
|
|
|
* interesting when multiple buffers need to be pushed in one go because it
|
|
|
|
* can reduce the amount of overhead for pushing each buffer individually.
|
|
|
|
*
|
|
|
|
* Last reviewed on 2012-03-28 (0.11.3)
|
2009-05-12 09:51:37 +00:00
|
|
|
*/
|
|
|
|
#include "gst_private.h"
|
|
|
|
|
|
|
|
#include "gstbuffer.h"
|
|
|
|
#include "gstbufferlist.h"
|
|
|
|
|
|
|
|
#define GST_CAT_DEFAULT GST_CAT_BUFFER_LIST
|
|
|
|
|
2009-05-12 14:18:48 +00:00
|
|
|
/**
|
|
|
|
* GstBufferList:
|
|
|
|
*
|
|
|
|
* Opaque list of grouped buffers.
|
|
|
|
*/
|
|
|
|
struct _GstBufferList
|
|
|
|
{
|
|
|
|
GstMiniObject mini_object;
|
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
GArray *array;
|
2009-05-12 09:51:37 +00:00
|
|
|
};
|
|
|
|
|
2010-11-02 12:31:25 +00:00
|
|
|
GType _gst_buffer_list_type = 0;
|
2010-07-20 14:23:11 +00:00
|
|
|
|
2011-08-29 15:06:18 +00:00
|
|
|
GST_DEFINE_MINI_OBJECT_TYPE (GstBufferList, gst_buffer_list);
|
2011-08-29 13:34:30 +00:00
|
|
|
|
2009-05-12 09:51:37 +00:00
|
|
|
void
|
2011-08-29 11:27:26 +00:00
|
|
|
_priv_gst_buffer_list_initialize (void)
|
2009-05-12 09:51:37 +00:00
|
|
|
{
|
2011-08-29 13:34:30 +00:00
|
|
|
_gst_buffer_list_type = gst_buffer_list_get_type ();
|
2009-05-12 09:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstBufferList *
|
|
|
|
_gst_buffer_list_copy (GstBufferList * list)
|
|
|
|
{
|
2011-03-31 15:51:02 +00:00
|
|
|
GstBufferList *copy;
|
|
|
|
guint i, len;
|
2009-05-12 09:51:37 +00:00
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
len = list->array->len;
|
2011-10-29 06:24:12 +00:00
|
|
|
copy = gst_buffer_list_new_sized (len);
|
2011-02-25 15:20:49 +00:00
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
/* add and ref all buffers in the array */
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
GstBuffer *buf = g_array_index (list->array, GstBuffer *, i);
|
|
|
|
buf = gst_buffer_ref (buf);
|
|
|
|
g_array_append_val (copy->array, buf);
|
|
|
|
}
|
|
|
|
return copy;
|
2009-05-12 09:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-12-02 20:21:48 +00:00
|
|
|
_gst_buffer_list_free (GstBufferList * list)
|
2009-05-12 09:51:37 +00:00
|
|
|
{
|
2011-03-31 15:51:02 +00:00
|
|
|
guint i, len;
|
2009-12-02 20:21:48 +00:00
|
|
|
GST_LOG ("free %p", list);
|
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
/* unrefs all buffers too */
|
|
|
|
len = list->array->len;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
gst_buffer_unref (g_array_index (list->array, GstBuffer *, i));
|
|
|
|
g_array_free (list->array, TRUE);
|
2009-12-02 20:21:48 +00:00
|
|
|
|
2012-06-14 22:49:10 +00:00
|
|
|
g_slice_free1 (sizeof (GstBufferList), list);
|
2011-02-23 15:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-06-14 22:49:10 +00:00
|
|
|
gst_buffer_list_init (GstBufferList * list, guint asize)
|
2011-02-23 15:48:00 +00:00
|
|
|
{
|
2012-07-04 14:38:15 +00:00
|
|
|
gst_mini_object_init (GST_MINI_OBJECT_CAST (list), 0, _gst_buffer_list_type,
|
2012-06-23 18:56:12 +00:00
|
|
|
(GstMiniObjectCopyFunction) _gst_buffer_list_copy, NULL,
|
|
|
|
(GstMiniObjectFreeFunction) _gst_buffer_list_free);
|
2011-03-17 09:50:43 +00:00
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
list->array = g_array_sized_new (FALSE, FALSE, sizeof (GstBuffer *), asize);
|
2011-03-17 09:50:43 +00:00
|
|
|
|
|
|
|
GST_LOG ("init %p", list);
|
2009-05-12 09:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-29 06:24:12 +00:00
|
|
|
* gst_buffer_list_new_sized:
|
2011-03-31 15:51:02 +00:00
|
|
|
* @size: an initial reserved size
|
2009-05-12 09:51:37 +00:00
|
|
|
*
|
|
|
|
* Creates a new, empty #GstBufferList. The caller is responsible for unreffing
|
2011-03-31 15:51:02 +00:00
|
|
|
* the returned #GstBufferList. The list will have @size space preallocated so
|
|
|
|
* that memory reallocations can be avoided.
|
2009-05-12 09:51:37 +00:00
|
|
|
*
|
2010-12-07 18:35:04 +00:00
|
|
|
* Free-function: gst_buffer_list_unref
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
|
|
|
|
* after usage.
|
2009-05-12 09:51:37 +00:00
|
|
|
*/
|
|
|
|
GstBufferList *
|
2011-10-29 06:24:12 +00:00
|
|
|
gst_buffer_list_new_sized (guint size)
|
2009-05-12 09:51:37 +00:00
|
|
|
{
|
|
|
|
GstBufferList *list;
|
|
|
|
|
2009-12-02 20:21:48 +00:00
|
|
|
list = g_slice_new0 (GstBufferList);
|
|
|
|
|
2009-05-12 09:51:37 +00:00
|
|
|
GST_LOG ("new %p", list);
|
|
|
|
|
2012-06-14 22:49:10 +00:00
|
|
|
gst_buffer_list_init (list, size);
|
2011-02-23 15:48:00 +00:00
|
|
|
|
2009-05-12 09:51:37 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-31 15:51:02 +00:00
|
|
|
* gst_buffer_list_new:
|
|
|
|
*
|
|
|
|
* Creates a new, empty #GstBufferList. The caller is responsible for unreffing
|
|
|
|
* the returned #GstBufferList.
|
|
|
|
*
|
|
|
|
* Free-function: gst_buffer_list_unref
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
|
|
|
|
* after usage.
|
|
|
|
*/
|
|
|
|
GstBufferList *
|
|
|
|
gst_buffer_list_new (void)
|
|
|
|
{
|
2011-10-29 06:24:12 +00:00
|
|
|
return gst_buffer_list_new_sized (8);
|
2011-03-31 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-29 06:24:12 +00:00
|
|
|
* gst_buffer_list_length:
|
2009-05-12 09:51:37 +00:00
|
|
|
* @list: a #GstBufferList
|
|
|
|
*
|
2011-03-31 15:51:02 +00:00
|
|
|
* Returns the number of buffers in @list.
|
2009-05-12 09:51:37 +00:00
|
|
|
*
|
2011-03-31 15:51:02 +00:00
|
|
|
* Returns: the number of buffers in the buffer list
|
2009-05-12 09:51:37 +00:00
|
|
|
*/
|
|
|
|
guint
|
2011-10-29 06:24:12 +00:00
|
|
|
gst_buffer_list_length (GstBufferList * list)
|
2009-05-12 09:51:37 +00:00
|
|
|
{
|
2011-03-31 15:51:02 +00:00
|
|
|
g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
|
2009-05-12 09:51:37 +00:00
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
return list->array->len;
|
2009-05-12 09:51:37 +00:00
|
|
|
}
|
|
|
|
|
2009-06-19 13:29:14 +00:00
|
|
|
/**
|
|
|
|
* gst_buffer_list_foreach:
|
|
|
|
* @list: a #GstBufferList
|
2010-12-17 18:14:41 +00:00
|
|
|
* @func: (scope call): a #GstBufferListFunc to call
|
2011-04-14 13:51:24 +00:00
|
|
|
* @user_data: (closure): user data passed to @func
|
2009-06-19 13:29:14 +00:00
|
|
|
*
|
|
|
|
* Call @func with @data for each buffer in @list.
|
|
|
|
*
|
|
|
|
* @func can modify the passed buffer pointer or its contents. The return value
|
2011-12-22 14:53:19 +00:00
|
|
|
* of @func define if this function returns or if the remaining buffers in
|
|
|
|
* the list should be skipped.
|
2012-07-17 10:52:59 +00:00
|
|
|
*
|
|
|
|
* Returns: %TRUE when @func returned %TRUE for each buffer in @list or when
|
|
|
|
* @list is empty.
|
2009-06-19 13:29:14 +00:00
|
|
|
*/
|
2012-07-17 10:52:59 +00:00
|
|
|
gboolean
|
2009-06-19 13:29:14 +00:00
|
|
|
gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2011-04-19 18:52:05 +00:00
|
|
|
guint i, len;
|
2012-07-17 10:52:59 +00:00
|
|
|
gboolean ret = TRUE;
|
2011-04-19 18:52:05 +00:00
|
|
|
|
2012-07-17 10:52:59 +00:00
|
|
|
g_return_val_if_fail (GST_IS_BUFFER_LIST (list), FALSE);
|
|
|
|
g_return_val_if_fail (func != NULL, FALSE);
|
2011-04-19 18:52:05 +00:00
|
|
|
|
|
|
|
len = list->array->len;
|
|
|
|
for (i = 0; i < len;) {
|
|
|
|
GstBuffer *buf, *buf_ret;
|
|
|
|
|
|
|
|
buf = buf_ret = g_array_index (list->array, GstBuffer *, i);
|
|
|
|
ret = func (&buf_ret, i, user_data);
|
|
|
|
|
|
|
|
/* Check if the function changed the buffer */
|
|
|
|
if (buf != buf_ret) {
|
|
|
|
if (buf_ret == NULL) {
|
|
|
|
g_array_remove_index (list->array, i);
|
2011-11-23 16:38:24 +00:00
|
|
|
len--;
|
2011-04-19 18:52:05 +00:00
|
|
|
} else {
|
|
|
|
g_array_index (list->array, GstBuffer *, i) = buf_ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* If the buffer was not removed by func go to the next buffer */
|
|
|
|
if (buf_ret != NULL)
|
|
|
|
i++;
|
|
|
|
}
|
2012-07-17 10:52:59 +00:00
|
|
|
return ret;
|
2009-06-19 13:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_buffer_list_get:
|
|
|
|
* @list: a #GstBufferList
|
2011-03-31 15:51:02 +00:00
|
|
|
* @idx: the index
|
2009-06-19 13:29:14 +00:00
|
|
|
*
|
2011-03-31 15:51:02 +00:00
|
|
|
* Get the buffer at @idx.
|
2009-06-19 13:29:14 +00:00
|
|
|
*
|
2010-12-07 18:35:04 +00:00
|
|
|
* Returns: (transfer none): the buffer at @idx in @group or NULL when there
|
|
|
|
* is no buffer. The buffer remains valid as long as @list is valid.
|
2009-06-19 13:29:14 +00:00
|
|
|
*/
|
|
|
|
GstBuffer *
|
2011-03-31 15:51:02 +00:00
|
|
|
gst_buffer_list_get (GstBufferList * list, guint idx)
|
2009-06-19 13:29:14 +00:00
|
|
|
{
|
2011-03-31 15:51:02 +00:00
|
|
|
GstBuffer *buf;
|
2009-05-12 09:51:37 +00:00
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
|
|
|
|
g_return_val_if_fail (idx < list->array->len, NULL);
|
2009-05-12 09:51:37 +00:00
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
buf = g_array_index (list->array, GstBuffer *, idx);
|
2009-05-12 09:51:37 +00:00
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
return buf;
|
2009-05-12 09:51:37 +00:00
|
|
|
}
|
|
|
|
|
2012-06-15 10:55:20 +00:00
|
|
|
/**
|
|
|
|
* gst_buffer_list_add:
|
|
|
|
* @l: a #GstBufferList
|
|
|
|
* @b: a #GstBuffer
|
|
|
|
*
|
|
|
|
* Append @b at the end of @l.
|
|
|
|
*/
|
2009-05-12 09:51:37 +00:00
|
|
|
/**
|
2011-03-31 15:51:02 +00:00
|
|
|
* gst_buffer_list_insert:
|
|
|
|
* @list: a #GstBufferList
|
|
|
|
* @idx: the index
|
2012-07-23 14:27:34 +00:00
|
|
|
* @buffer: (transfer full): a #GstBuffer
|
2009-05-12 09:51:37 +00:00
|
|
|
*
|
2011-03-31 15:51:02 +00:00
|
|
|
* Insert @buffer at @idx in @list. Other buffers are moved to make room for
|
|
|
|
* this new buffer.
|
2010-01-31 17:30:54 +00:00
|
|
|
*
|
2011-03-31 15:51:02 +00:00
|
|
|
* A -1 value for @idx will append the buffer at the end.
|
2009-05-12 09:51:37 +00:00
|
|
|
*/
|
|
|
|
void
|
2012-07-23 14:27:34 +00:00
|
|
|
gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
|
2009-05-12 09:51:37 +00:00
|
|
|
{
|
2011-03-31 15:51:02 +00:00
|
|
|
g_return_if_fail (GST_IS_BUFFER_LIST (list));
|
2009-05-12 09:51:37 +00:00
|
|
|
g_return_if_fail (buffer != NULL);
|
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
if (idx == -1)
|
|
|
|
g_array_append_val (list->array, buffer);
|
|
|
|
else {
|
|
|
|
g_return_if_fail (idx < list->array->len);
|
|
|
|
g_array_insert_val (list->array, idx, buffer);
|
2009-06-19 13:29:14 +00:00
|
|
|
}
|
2009-05-12 09:51:37 +00:00
|
|
|
}
|
|
|
|
|
2012-03-29 11:34:50 +00:00
|
|
|
/**
|
|
|
|
* gst_buffer_list_remove:
|
|
|
|
* @list: a #GstBufferList
|
|
|
|
* @idx: the index
|
|
|
|
* @length: the amount to remove
|
|
|
|
*
|
|
|
|
* Remove @length buffers starting from @idx in @list. The following buffers are
|
|
|
|
* moved to close the gap.
|
|
|
|
*/
|
2009-05-12 09:51:37 +00:00
|
|
|
void
|
2011-03-31 15:51:02 +00:00
|
|
|
gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
|
2009-05-12 09:51:37 +00:00
|
|
|
{
|
2011-03-31 15:51:02 +00:00
|
|
|
g_return_if_fail (GST_IS_BUFFER_LIST (list));
|
|
|
|
g_return_if_fail (idx < list->array->len);
|
2009-05-12 09:51:37 +00:00
|
|
|
|
2011-03-31 15:51:02 +00:00
|
|
|
g_array_remove_range (list->array, idx, length);
|
2009-05-12 09:51:37 +00:00
|
|
|
}
|