gstbufferlist.c 6.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* 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
Wim Taymans's avatar
Wim Taymans committed
25
 * @short_description: Lists of buffers for data-passing
26 27
 * @see_also: #GstPad, #GstMiniObject
 *
Wim Taymans's avatar
Wim Taymans committed
28
 * Buffer lists are an object containing a list of buffers.
29 30
 *
 * Buffer lists are created with gst_buffer_list_new() and filled with data
Wim Taymans's avatar
Wim Taymans committed
31
 * using a gst_buffer_list_take().
32 33 34 35 36 37 38 39 40
 *
 */
#include "gst_private.h"

#include "gstbuffer.h"
#include "gstbufferlist.h"

#define GST_CAT_DEFAULT GST_CAT_BUFFER_LIST

Wim Taymans's avatar
Wim Taymans committed
41 42 43 44 45
/**
 * GstBufferList:
 * @mini_object: the parent structure
 *
 * Opaque list of grouped buffers.
46 47
 *
 * Since: 0.10.24
Wim Taymans's avatar
Wim Taymans committed
48 49 50 51 52
 */
struct _GstBufferList
{
  GstMiniObject mini_object;

Wim Taymans's avatar
Wim Taymans committed
53
  GArray *array;
54 55
};

Wim Taymans's avatar
Wim Taymans committed
56
GType _gst_buffer_list_type = 0;
57

58 59 60
void
_gst_buffer_list_initialize (void)
{
Wim Taymans's avatar
Wim Taymans committed
61 62 63
  if (G_LIKELY (_gst_buffer_list_type == 0)) {
    _gst_buffer_list_type = gst_mini_object_register ("GstBufferList");
  }
64 65 66 67 68
}

static GstBufferList *
_gst_buffer_list_copy (GstBufferList * list)
{
Wim Taymans's avatar
Wim Taymans committed
69 70
  GstBufferList *copy;
  guint i, len;
71

Wim Taymans's avatar
Wim Taymans committed
72 73
  len = list->array->len;
  copy = gst_buffer_list_sized_new (len);
74

Wim Taymans's avatar
Wim Taymans committed
75 76 77 78 79 80 81
  /* 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;
82 83 84
}

static void
85
_gst_buffer_list_free (GstBufferList * list)
86
{
Wim Taymans's avatar
Wim Taymans committed
87
  guint i, len;
88 89
  GST_LOG ("free %p", list);

Wim Taymans's avatar
Wim Taymans committed
90 91 92 93 94
  /* 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);
95

Wim Taymans's avatar
Wim Taymans committed
96 97 98 99
  g_slice_free1 (GST_MINI_OBJECT_SIZE (list), list);
}

static void
Wim Taymans's avatar
Wim Taymans committed
100
gst_buffer_list_init (GstBufferList * list, gsize size, guint asize)
Wim Taymans's avatar
Wim Taymans committed
101 102 103 104 105 106
{
  gst_mini_object_init (GST_MINI_OBJECT_CAST (list), _gst_buffer_list_type,
      size);

  list->mini_object.copy = (GstMiniObjectCopyFunction) _gst_buffer_list_copy;
  list->mini_object.free = (GstMiniObjectFreeFunction) _gst_buffer_list_free;
107

Wim Taymans's avatar
Wim Taymans committed
108
  list->array = g_array_sized_new (FALSE, FALSE, sizeof (GstBuffer *), asize);
109 110

  GST_LOG ("init %p", list);
111 112 113
}

/**
Wim Taymans's avatar
Wim Taymans committed
114 115
 * gst_buffer_list_sized_new:
 * @size: an initial reserved size
116 117
 *
 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
Wim Taymans's avatar
Wim Taymans committed
118 119
 * the returned #GstBufferList. The list will have @size space preallocated so
 * that memory reallocations can be avoided.
120
 *
121 122 123 124
 * Free-function: gst_buffer_list_unref
 *
 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
 *     after usage.
125 126
 *
 * Since: 0.10.24
127 128
 */
GstBufferList *
Wim Taymans's avatar
Wim Taymans committed
129
gst_buffer_list_sized_new (guint size)
130 131 132
{
  GstBufferList *list;

133 134
  list = g_slice_new0 (GstBufferList);

135 136
  GST_LOG ("new %p", list);

Wim Taymans's avatar
Wim Taymans committed
137
  gst_buffer_list_init (list, sizeof (GstBufferList), size);
Wim Taymans's avatar
Wim Taymans committed
138

139 140 141 142
  return list;
}

/**
Wim Taymans's avatar
Wim Taymans committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
 * 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.
 *
 * Since: 0.10.24
 */
GstBufferList *
gst_buffer_list_new (void)
{
  return gst_buffer_list_sized_new (8);
}

/**
 * gst_buffer_list_len:
163 164
 * @list: a #GstBufferList
 *
Wim Taymans's avatar
Wim Taymans committed
165
 * Returns the number of buffers in @list.
166
 *
Wim Taymans's avatar
Wim Taymans committed
167
 * Returns: the number of buffers in the buffer list
168 169
 *
 * Since: 0.10.24
170 171
 */
guint
Wim Taymans's avatar
Wim Taymans committed
172
gst_buffer_list_len (GstBufferList * list)
173
{
Wim Taymans's avatar
Wim Taymans committed
174
  g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
175

Wim Taymans's avatar
Wim Taymans committed
176
  return list->array->len;
177 178
}

Wim Taymans's avatar
Wim Taymans committed
179 180 181
/**
 * gst_buffer_list_foreach:
 * @list: a #GstBufferList
182
 * @func: (scope call): a #GstBufferListFunc to call
183
 * @user_data: (closure): user data passed to @func
Wim Taymans's avatar
Wim Taymans committed
184 185 186 187 188 189
 *
 * Call @func with @data for each buffer in @list.
 *
 * @func can modify the passed buffer pointer or its contents. The return value
 * of @func define if this function returns or if the remaining buffers in a
 * group should be skipped.
190 191
 *
 * Since: 0.10.24
Wim Taymans's avatar
Wim Taymans committed
192 193 194 195 196 197 198 199 200 201
 */
void
gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
    gpointer user_data)
{
}

/**
 * gst_buffer_list_get:
 * @list: a #GstBufferList
Wim Taymans's avatar
Wim Taymans committed
202
 * @idx: the index
Wim Taymans's avatar
Wim Taymans committed
203
 *
Wim Taymans's avatar
Wim Taymans committed
204
 * Get the buffer at @idx.
Wim Taymans's avatar
Wim Taymans committed
205
 *
206 207
 * 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.
208 209
 *
 * Since: 0.10.24
Wim Taymans's avatar
Wim Taymans committed
210 211
 */
GstBuffer *
Wim Taymans's avatar
Wim Taymans committed
212
gst_buffer_list_get (GstBufferList * list, guint idx)
Wim Taymans's avatar
Wim Taymans committed
213
{
Wim Taymans's avatar
Wim Taymans committed
214
  GstBuffer *buf;
215

Wim Taymans's avatar
Wim Taymans committed
216 217
  g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
  g_return_val_if_fail (idx < list->array->len, NULL);
218

Wim Taymans's avatar
Wim Taymans committed
219
  buf = g_array_index (list->array, GstBuffer *, idx);
220

Wim Taymans's avatar
Wim Taymans committed
221
  return buf;
222 223 224
}

/**
Wim Taymans's avatar
Wim Taymans committed
225 226 227 228
 * gst_buffer_list_insert:
 * @list: a #GstBufferList
 * @idx: the index
 * @buffer: a #GstBuffer
229
 *
Wim Taymans's avatar
Wim Taymans committed
230 231
 * Insert @buffer at @idx in @list. Other buffers are moved to make room for
 * this new buffer.
232
 *
Wim Taymans's avatar
Wim Taymans committed
233
 * A -1 value for @idx will append the buffer at the end.
234 235
 */
void
Wim Taymans's avatar
Wim Taymans committed
236
gst_buffer_list_insert (GstBufferList * list, guint idx, GstBuffer * buffer)
237
{
Wim Taymans's avatar
Wim Taymans committed
238
  g_return_if_fail (GST_IS_BUFFER_LIST (list));
239 240
  g_return_if_fail (buffer != NULL);

Wim Taymans's avatar
Wim Taymans committed
241 242 243 244 245
  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);
Wim Taymans's avatar
Wim Taymans committed
246
  }
247 248 249
}

void
Wim Taymans's avatar
Wim Taymans committed
250
gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
251
{
Wim Taymans's avatar
Wim Taymans committed
252 253
  g_return_if_fail (GST_IS_BUFFER_LIST (list));
  g_return_if_fail (idx < list->array->len);
254

Wim Taymans's avatar
Wim Taymans committed
255
  g_array_remove_range (list->array, idx, length);
256
}