gstbufferlist.c 9.32 KB
Newer Older
1 2 3
/* GStreamer
 * Copyright (C) 2009 Axis Communications <dev-gstreamer at axis dot com>
 * @author Jonas Holmberg <jonas dot holmberg at axis dot com>
4
 * Copyright (C) 2014 Tim-Philipp Müller <tim at centricular dot com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20 21
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
22 23 24 25
 */

/**
 * SECTION:gstbufferlist
Wim Taymans's avatar
Wim Taymans committed
26
 * @short_description: Lists of buffers for data-passing
27 28
 * @see_also: #GstPad, #GstMiniObject
 *
Wim Taymans's avatar
Wim Taymans committed
29
 * Buffer lists are an object containing a list of buffers.
30 31
 *
 * Buffer lists are created with gst_buffer_list_new() and filled with data
Wim Taymans's avatar
Wim Taymans committed
32
 * using a gst_buffer_list_insert().
33
 *
Wim Taymans's avatar
Wim Taymans committed
34 35 36
 * 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.
37 38 39 40 41
 */
#include "gst_private.h"

#include "gstbuffer.h"
#include "gstbufferlist.h"
42
#include "gstutils.h"
43 44 45

#define GST_CAT_DEFAULT GST_CAT_BUFFER_LIST

46 47 48
#define GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY(list) \
    ((list)->buffers != &(list)->arr[0])

Wim Taymans's avatar
Wim Taymans committed
49 50 51 52 53 54 55 56 57
/**
 * GstBufferList:
 *
 * Opaque list of grouped buffers.
 */
struct _GstBufferList
{
  GstMiniObject mini_object;

58 59 60 61 62 63 64 65 66 67
  GstBuffer **buffers;
  guint n_buffers;
  guint n_allocated;

  gsize slice_size;

  /* one-item array, in reality more items are pre-allocated
   * as part of the GstBufferList structure, and that
   * pre-allocated array extends beyond the declared struct */
  GstBuffer *arr[1];
68 69
};

Wim Taymans's avatar
Wim Taymans committed
70
GType _gst_buffer_list_type = 0;
71

72
GST_DEFINE_MINI_OBJECT_TYPE (GstBufferList, gst_buffer_list);
Wim Taymans's avatar
Wim Taymans committed
73

74
void
75
_priv_gst_buffer_list_initialize (void)
76
{
Wim Taymans's avatar
Wim Taymans committed
77
  _gst_buffer_list_type = gst_buffer_list_get_type ();
78 79 80 81 82
}

static GstBufferList *
_gst_buffer_list_copy (GstBufferList * list)
{
Wim Taymans's avatar
Wim Taymans committed
83 84
  GstBufferList *copy;
  guint i, len;
85

86 87
  len = list->n_buffers;
  copy = gst_buffer_list_new_sized (list->n_allocated);
88

Wim Taymans's avatar
Wim Taymans committed
89
  /* add and ref all buffers in the array */
90 91 92 93 94
  for (i = 0; i < len; i++)
    copy->buffers[i] = gst_buffer_ref (list->buffers[i]);

  copy->n_buffers = len;

Wim Taymans's avatar
Wim Taymans committed
95
  return copy;
96 97 98
}

static void
99
_gst_buffer_list_free (GstBufferList * list)
100
{
Wim Taymans's avatar
Wim Taymans committed
101
  guint i, len;
102

103 104
  GST_LOG ("free %p", list);

Wim Taymans's avatar
Wim Taymans committed
105
  /* unrefs all buffers too */
106
  len = list->n_buffers;
Wim Taymans's avatar
Wim Taymans committed
107
  for (i = 0; i < len; i++)
108 109 110 111
    gst_buffer_unref (list->buffers[i]);

  if (GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY (list))
    g_free (list->buffers);
112

113
  g_slice_free1 (list->slice_size, list);
Wim Taymans's avatar
Wim Taymans committed
114 115 116
}

static void
117
gst_buffer_list_init (GstBufferList * list, guint n_allocated, gsize slice_size)
Wim Taymans's avatar
Wim Taymans committed
118
{
119
  gst_mini_object_init (GST_MINI_OBJECT_CAST (list), 0, _gst_buffer_list_type,
120 121
      (GstMiniObjectCopyFunction) _gst_buffer_list_copy, NULL,
      (GstMiniObjectFreeFunction) _gst_buffer_list_free);
122

123 124 125 126
  list->buffers = &list->arr[0];
  list->n_buffers = 0;
  list->n_allocated = n_allocated;
  list->slice_size = slice_size;
127 128

  GST_LOG ("init %p", list);
129 130 131
}

/**
Wim Taymans's avatar
Wim Taymans committed
132
 * gst_buffer_list_new_sized:
Wim Taymans's avatar
Wim Taymans committed
133
 * @size: an initial reserved size
134 135
 *
 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
Wim Taymans's avatar
Wim Taymans committed
136 137
 * the returned #GstBufferList. The list will have @size space preallocated so
 * that memory reallocations can be avoided.
138
 *
139 140 141 142
 * Free-function: gst_buffer_list_unref
 *
 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
 *     after usage.
143 144
 */
GstBufferList *
Wim Taymans's avatar
Wim Taymans committed
145
gst_buffer_list_new_sized (guint size)
146 147
{
  GstBufferList *list;
148 149
  gsize slice_size;
  guint n_allocated;
150

151 152 153 154 155
  n_allocated = GST_ROUND_UP_16 (size);

  slice_size = sizeof (GstBufferList) + (n_allocated - 1) * sizeof (gpointer);

  list = g_slice_alloc0 (slice_size);
156

157 158
  GST_LOG ("new %p", list);

159
  gst_buffer_list_init (list, n_allocated, slice_size);
Wim Taymans's avatar
Wim Taymans committed
160

161 162 163 164
  return list;
}

/**
Wim Taymans's avatar
Wim Taymans committed
165 166 167 168 169 170 171 172 173 174 175 176 177
 * 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
178
  return gst_buffer_list_new_sized (8);
Wim Taymans's avatar
Wim Taymans committed
179 180 181
}

/**
Wim Taymans's avatar
Wim Taymans committed
182
 * gst_buffer_list_length:
183 184
 * @list: a #GstBufferList
 *
Wim Taymans's avatar
Wim Taymans committed
185
 * Returns the number of buffers in @list.
186
 *
Wim Taymans's avatar
Wim Taymans committed
187
 * Returns: the number of buffers in the buffer list
188 189
 */
guint
Wim Taymans's avatar
Wim Taymans committed
190
gst_buffer_list_length (GstBufferList * list)
191
{
Wim Taymans's avatar
Wim Taymans committed
192
  g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  return list->n_buffers;
}

static inline void
gst_buffer_list_remove_range_internal (GstBufferList * list, guint idx,
    guint length, gboolean unref_old)
{
  guint i;

  if (unref_old) {
    for (i = idx; i < idx + length; ++i)
      gst_buffer_unref (list->buffers[i]);
  }

  if (idx + length != list->n_buffers) {
    memmove (&list->buffers[idx], &list->buffers[idx + length],
        (list->n_buffers - (idx + length)) * sizeof (void *));
  }

  list->n_buffers -= length;
214 215
}

Wim Taymans's avatar
Wim Taymans committed
216 217 218
/**
 * gst_buffer_list_foreach:
 * @list: a #GstBufferList
219
 * @func: (scope call): a #GstBufferListFunc to call
220
 * @user_data: (closure): user data passed to @func
Wim Taymans's avatar
Wim Taymans committed
221 222 223 224
 *
 * 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
225 226
 * of @func define if this function returns or if the remaining buffers in
 * the list should be skipped.
227 228 229
 *
 * Returns: %TRUE when @func returned %TRUE for each buffer in @list or when
 * @list is empty.
Wim Taymans's avatar
Wim Taymans committed
230
 */
231
gboolean
Wim Taymans's avatar
Wim Taymans committed
232 233 234
gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
    gpointer user_data)
{
235
  guint i, len;
236
  gboolean ret = TRUE;
237

238 239
  g_return_val_if_fail (GST_IS_BUFFER_LIST (list), FALSE);
  g_return_val_if_fail (func != NULL, FALSE);
240

241
  len = list->n_buffers;
242 243 244
  for (i = 0; i < len;) {
    GstBuffer *buf, *buf_ret;

245
    buf = buf_ret = list->buffers[i];
246 247 248 249 250
    ret = func (&buf_ret, i, user_data);

    /* Check if the function changed the buffer */
    if (buf != buf_ret) {
      if (buf_ret == NULL) {
251 252
        gst_buffer_list_remove_range_internal (list, i, 1, FALSE);
        --len;
253
      } else {
254
        list->buffers[i] = buf_ret;
255 256 257 258 259 260 261 262 263 264
      }
    }

    if (!ret)
      break;

    /* If the buffer was not removed by func go to the next buffer */
    if (buf_ret != NULL)
      i++;
  }
265
  return ret;
Wim Taymans's avatar
Wim Taymans committed
266 267 268 269 270
}

/**
 * gst_buffer_list_get:
 * @list: a #GstBufferList
Wim Taymans's avatar
Wim Taymans committed
271
 * @idx: the index
Wim Taymans's avatar
Wim Taymans committed
272
 *
Wim Taymans's avatar
Wim Taymans committed
273
 * Get the buffer at @idx.
Wim Taymans's avatar
Wim Taymans committed
274
 *
275 276
 * Returns: (transfer none) (nullable): the buffer at @idx in @group
 *     or %NULL when there is no buffer. The buffer remains valid as
277
 *     long as @list is valid and buffer is not removed from the list.
Wim Taymans's avatar
Wim Taymans committed
278 279
 */
GstBuffer *
Wim Taymans's avatar
Wim Taymans committed
280
gst_buffer_list_get (GstBufferList * list, guint idx)
Wim Taymans's avatar
Wim Taymans committed
281
{
Wim Taymans's avatar
Wim Taymans committed
282
  g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
283
  g_return_val_if_fail (idx < list->n_buffers, NULL);
284

285
  return list->buffers[idx];
286 287
}

Wim Taymans's avatar
Wim Taymans committed
288 289 290 291 292 293 294
/**
 * gst_buffer_list_add:
 * @l: a #GstBufferList
 * @b: a #GstBuffer
 *
 * Append @b at the end of @l.
 */
295
/**
Wim Taymans's avatar
Wim Taymans committed
296 297 298
 * gst_buffer_list_insert:
 * @list: a #GstBufferList
 * @idx: the index
299
 * @buffer: (transfer full): a #GstBuffer
300
 *
Wim Taymans's avatar
Wim Taymans committed
301 302
 * Insert @buffer at @idx in @list. Other buffers are moved to make room for
 * this new buffer.
303
 *
Wim Taymans's avatar
Wim Taymans committed
304
 * A -1 value for @idx will append the buffer at the end.
305 306
 */
void
307
gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
308
{
309 310
  guint want_alloc;

Wim Taymans's avatar
Wim Taymans committed
311
  g_return_if_fail (GST_IS_BUFFER_LIST (list));
312
  g_return_if_fail (buffer != NULL);
313
  g_return_if_fail (gst_buffer_list_is_writable (list));
314

315 316 317
  if (idx == -1 && list->n_buffers < list->n_allocated) {
    list->buffers[list->n_buffers++] = buffer;
    return;
Wim Taymans's avatar
Wim Taymans committed
318
  }
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

  if (idx == -1 || idx > list->n_buffers)
    idx = list->n_buffers;

  want_alloc = list->n_buffers + 1;

  if (want_alloc > list->n_allocated) {
    want_alloc = MAX (GST_ROUND_UP_16 (want_alloc), list->n_allocated * 2);

    if (GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY (list)) {
      list->buffers = g_renew (GstBuffer *, list->buffers, want_alloc);
    } else {
      list->buffers = g_new0 (GstBuffer *, want_alloc);
      memcpy (list->buffers, &list->arr[0], list->n_buffers * sizeof (void *));
      GST_CAT_LOG (GST_CAT_PERFORMANCE, "exceeding pre-alloced array");
    }

    list->n_allocated = want_alloc;
  }

  if (idx < list->n_buffers) {
    memmove (&list->buffers[idx + 1], &list->buffers[idx],
        (list->n_buffers - idx) * sizeof (void *));
  }

  ++list->n_buffers;
  list->buffers[idx] = buffer;
346 347
}

Wim Taymans's avatar
Wim Taymans committed
348 349 350 351 352 353
/**
 * gst_buffer_list_remove:
 * @list: a #GstBufferList
 * @idx: the index
 * @length: the amount to remove
 *
354 355
 * Remove @length buffers starting from @idx in @list. The following buffers
 * are moved to close the gap.
Wim Taymans's avatar
Wim Taymans committed
356
 */
357
void
Wim Taymans's avatar
Wim Taymans committed
358
gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
359
{
Wim Taymans's avatar
Wim Taymans committed
360
  g_return_if_fail (GST_IS_BUFFER_LIST (list));
361 362
  g_return_if_fail (idx < list->n_buffers);
  g_return_if_fail (idx + length <= list->n_buffers);
363
  g_return_if_fail (gst_buffer_list_is_writable (list));
364

365
  gst_buffer_list_remove_range_internal (list, idx, length, TRUE);
366
}