gstbufferlist.c 7.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
21 22 23 24
 */

/**
 * 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_insert().
32
 *
Wim Taymans's avatar
Wim Taymans committed
33 34 35
 * 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.
36 37 38 39 40 41 42 43
 */
#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
44 45 46 47 48 49 50 51 52
/**
 * GstBufferList:
 *
 * Opaque list of grouped buffers.
 */
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
GST_DEFINE_MINI_OBJECT_TYPE (GstBufferList, gst_buffer_list);
Wim Taymans's avatar
Wim Taymans committed
59

60
void
61
_priv_gst_buffer_list_initialize (void)
62
{
Wim Taymans's avatar
Wim Taymans committed
63
  _gst_buffer_list_type = gst_buffer_list_get_type ();
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
  len = list->array->len;
Wim Taymans's avatar
Wim Taymans committed
73
  copy = gst_buffer_list_new_sized (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

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

static void
100
gst_buffer_list_init (GstBufferList * list, guint asize)
Wim Taymans's avatar
Wim Taymans committed
101
{
102
  gst_mini_object_init (GST_MINI_OBJECT_CAST (list), 0, _gst_buffer_list_type,
103 104
      (GstMiniObjectCopyFunction) _gst_buffer_list_copy, NULL,
      (GstMiniObjectFreeFunction) _gst_buffer_list_free);
105

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

  GST_LOG ("init %p", list);
109 110 111
}

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

129 130
  list = g_slice_new0 (GstBufferList);

131 132
  GST_LOG ("new %p", list);

133
  gst_buffer_list_init (list, size);
Wim Taymans's avatar
Wim Taymans committed
134

135 136 137 138
  return list;
}

/**
Wim Taymans's avatar
Wim Taymans committed
139 140 141 142 143 144 145 146 147 148 149 150 151
 * 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)
{
Wim Taymans's avatar
Wim Taymans committed
152
  return gst_buffer_list_new_sized (8);
Wim Taymans's avatar
Wim Taymans committed
153 154 155
}

/**
Wim Taymans's avatar
Wim Taymans committed
156
 * gst_buffer_list_length:
157 158
 * @list: a #GstBufferList
 *
Wim Taymans's avatar
Wim Taymans committed
159
 * Returns the number of buffers in @list.
160
 *
Wim Taymans's avatar
Wim Taymans committed
161
 * Returns: the number of buffers in the buffer list
162 163
 */
guint
Wim Taymans's avatar
Wim Taymans committed
164
gst_buffer_list_length (GstBufferList * list)
165
{
Wim Taymans's avatar
Wim Taymans committed
166
  g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
167

Wim Taymans's avatar
Wim Taymans committed
168
  return list->array->len;
169 170
}

Wim Taymans's avatar
Wim Taymans committed
171 172 173
/**
 * gst_buffer_list_foreach:
 * @list: a #GstBufferList
174
 * @func: (scope call): a #GstBufferListFunc to call
175
 * @user_data: (closure): user data passed to @func
Wim Taymans's avatar
Wim Taymans committed
176 177 178 179
 *
 * Call @func with @data for each buffer in @list.
 *
 * @func can modify the passed buffer pointer or its contents. The return value
Wim Taymans's avatar
Wim Taymans committed
180 181
 * of @func define if this function returns or if the remaining buffers in
 * the list should be skipped.
182 183 184
 *
 * Returns: %TRUE when @func returned %TRUE for each buffer in @list or when
 * @list is empty.
Wim Taymans's avatar
Wim Taymans committed
185
 */
186
gboolean
Wim Taymans's avatar
Wim Taymans committed
187 188 189
gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
    gpointer user_data)
{
190
  guint i, len;
191
  gboolean ret = TRUE;
192

193 194
  g_return_val_if_fail (GST_IS_BUFFER_LIST (list), FALSE);
  g_return_val_if_fail (func != NULL, FALSE);
195 196 197 198 199 200 201 202 203 204 205 206

  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);
207
        len--;
208 209 210 211 212 213 214 215 216 217 218 219
      } 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++;
  }
220
  return ret;
Wim Taymans's avatar
Wim Taymans committed
221 222 223 224 225
}

/**
 * gst_buffer_list_get:
 * @list: a #GstBufferList
Wim Taymans's avatar
Wim Taymans committed
226
 * @idx: the index
Wim Taymans's avatar
Wim Taymans committed
227
 *
Wim Taymans's avatar
Wim Taymans committed
228
 * Get the buffer at @idx.
Wim Taymans's avatar
Wim Taymans committed
229
 *
230
 * Returns: (transfer none): the buffer at @idx in @group or %NULL when there
231
 *     is no buffer. The buffer remains valid as long as @list is valid.
Wim Taymans's avatar
Wim Taymans committed
232 233
 */
GstBuffer *
Wim Taymans's avatar
Wim Taymans committed
234
gst_buffer_list_get (GstBufferList * list, guint idx)
Wim Taymans's avatar
Wim Taymans committed
235
{
Wim Taymans's avatar
Wim Taymans committed
236
  GstBuffer *buf;
237

Wim Taymans's avatar
Wim Taymans committed
238 239
  g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
  g_return_val_if_fail (idx < list->array->len, NULL);
240

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

Wim Taymans's avatar
Wim Taymans committed
243
  return buf;
244 245
}

Wim Taymans's avatar
Wim Taymans committed
246 247 248 249 250 251 252
/**
 * gst_buffer_list_add:
 * @l: a #GstBufferList
 * @b: a #GstBuffer
 *
 * Append @b at the end of @l.
 */
253
/**
Wim Taymans's avatar
Wim Taymans committed
254 255 256
 * gst_buffer_list_insert:
 * @list: a #GstBufferList
 * @idx: the index
257
 * @buffer: (transfer full): a #GstBuffer
258
 *
Wim Taymans's avatar
Wim Taymans committed
259 260
 * Insert @buffer at @idx in @list. Other buffers are moved to make room for
 * this new buffer.
261
 *
Wim Taymans's avatar
Wim Taymans committed
262
 * A -1 value for @idx will append the buffer at the end.
263 264
 */
void
265
gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
266
{
Wim Taymans's avatar
Wim Taymans committed
267
  g_return_if_fail (GST_IS_BUFFER_LIST (list));
268 269
  g_return_if_fail (buffer != NULL);

Wim Taymans's avatar
Wim Taymans committed
270 271 272 273 274
  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
275
  }
276 277
}

Wim Taymans's avatar
Wim Taymans committed
278 279 280 281 282 283 284 285 286
/**
 * 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.
 */
287
void
Wim Taymans's avatar
Wim Taymans committed
288
gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
289
{
Wim Taymans's avatar
Wim Taymans committed
290 291
  g_return_if_fail (GST_IS_BUFFER_LIST (list));
  g_return_if_fail (idx < list->array->len);
292

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