gstbufferlist.c 23.4 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/* 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
 * @short_description: Grouped scatter data buffer type for data-passing
 * @see_also: #GstPad, #GstMiniObject
 *
 * Buffer lists are units of grouped scatter/gather data transfer in
 * GStreamer.
 *
 * Buffer lists are created with gst_buffer_list_new() and filled with data
 * using a #GstBufferListIterator. The iterator has no current buffer; its
 * cursor position lies between buffers, immediately before the buffer that
 * would be returned by gst_buffer_list_iterator_next(). After iterating to the
 * end of a group the iterator must be advanced to the next group by a call to
 * gst_buffer_list_iterator_next_group() before any further calls to
 * gst_buffer_list_iterator_next() can return buffers again. The cursor position
 * of a newly created iterator lies before the first group; a call to
 * gst_buffer_list_iterator_next_group() is necessary before calls to
 * gst_buffer_list_iterator_next() can return buffers.
 *
42
 * <informalfigure>
43 44 45
 *   <programlisting>
 *      +--- group0 ----------------------+--- group1 ------------+
 *      |   buffer0   buffer1   buffer2   |   buffer3   buffer4   |
46 47
 *    ^   ^         ^         ^         ^   ^         ^         ^
 *    Iterator positions between buffers
48
 *   </programlisting>
49
 * </informalfigure>
50 51
 *
 * The gst_buffer_list_iterator_remove(), gst_buffer_list_iterator_steal(),
Jonas Holmberg's avatar
Jonas Holmberg committed
52 53 54
 * gst_buffer_list_iterator_take() and gst_buffer_list_iterator_do() functions
 * are not defined in terms of the cursor position; they operate on the last
 * element returned from gst_buffer_list_iterator_next().
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
 *
 * The basic use pattern of creating a buffer list with an iterator is as
 * follows:
 *
 * <example>
 * <title>Creating a buffer list</title>
 *   <programlisting>
 *    GstBufferList *list;
 *    GstBufferListIterator *it;
 *
 *    list = gst_buffer_list_new ();
 *    it = gst_buffer_list_iterate (list);
 *    gst_buffer_list_iterator_add_group (it);
 *    gst_buffer_list_iterator_add (it, header1);
 *    gst_buffer_list_iterator_add (it, data1);
 *    gst_buffer_list_iterator_add_group (it);
 *    gst_buffer_list_iterator_add (it, header2);
 *    gst_buffer_list_iterator_add (it, data2);
 *    gst_buffer_list_iterator_add_group (it);
 *    gst_buffer_list_iterator_add (it, header3);
 *    gst_buffer_list_iterator_add (it, data3);
 *    ...
 *    gst_buffer_list_iterator_free (it);
 *   </programlisting>
 * </example>
 *
 * The basic use pattern of iterating over a buffer list is as follows:
 *
 * <example>
 * <title>Iterating a buffer list</title>
 *   <programlisting>
 *    GstBufferListIterator *it;
 *
 *    it = gst_buffer_list_iterate (list);
 *    while (gst_buffer_list_iterator_next_group (it)) {
 *      while ((buffer = gst_buffer_list_iterator_next (it)) != NULL) {
 *        do_something_with_buffer (buffer);
 *      }
 *    }
 *    gst_buffer_list_iterator_free (it);
 *   </programlisting>
 * </example>
 *
 * The basic use pattern of modifying a buffer in a list is as follows:
 *
 * <example>
 * <title>Modifying the data of the first buffer in a list</title>
 *   <programlisting>
 *    GstBufferListIterator *it;
 *
 *    list = gst_buffer_list_make_writable (list);
 *    it = gst_buffer_list_iterate (list);
 *    if (gst_buffer_list_iterator_next_group (it)) {
 *      GstBuffer *buf
 *
 *      buf = gst_buffer_list_iterator_next (it);
 *      if (buf != NULL) {
 *        buf = gst_buffer_list_iterator_do (it,
Wim Taymans's avatar
Wim Taymans committed
113
 *            (GstBufferListDoFunction) gst_mini_object_make_writable, NULL);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
 *        modify_data (GST_BUFFER_DATA (buf));
 *      }
 *    }
 *    gst_buffer_list_iterator_free (it);
 *   </programlisting>
 * </example>
 *
 * Since: 0.10.24
 */
#include "gst_private.h"

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

#define GST_CAT_DEFAULT GST_CAT_BUFFER_LIST

#define GROUP_START NULL
Benjamin Otte's avatar
Benjamin Otte committed
131
static gconstpointer STOLEN = "";
132

Wim Taymans's avatar
Wim Taymans committed
133 134 135 136 137
/**
 * GstBufferList:
 * @mini_object: the parent structure
 *
 * Opaque list of grouped buffers.
138 139
 *
 * Since: 0.10.24
Wim Taymans's avatar
Wim Taymans committed
140 141 142 143 144
 */
struct _GstBufferList
{
  GstMiniObject mini_object;

145
  GQueue *buffers;
Wim Taymans's avatar
Wim Taymans committed
146 147
};

148 149 150
/**
 * GstBufferListIterator:
 *
Wim Taymans's avatar
Wim Taymans committed
151
 * Opaque iterator for a #GstBufferList.
152 153
 *
 * Since: 0.10.24
154 155 156 157 158 159 160 161
 */
struct _GstBufferListIterator
{
  GstBufferList *list;
  GList *next;
  GList *last_returned;
};

Wim Taymans's avatar
Wim Taymans committed
162
GType _gst_buffer_list_type = 0;
163

164 165 166
void
_gst_buffer_list_initialize (void)
{
Wim Taymans's avatar
Wim Taymans committed
167 168 169
  if (G_LIKELY (_gst_buffer_list_type == 0)) {
    _gst_buffer_list_type = gst_mini_object_register ("GstBufferList");
  }
170 171 172 173 174 175
}

static GstBufferList *
_gst_buffer_list_copy (GstBufferList * list)
{
  GstBufferList *list_copy;
176
  GQueue *buffers_copy;
177 178 179 180 181
  GList *tmp;

  g_return_val_if_fail (list != NULL, NULL);

  /* shallow copy of list and pointers */
182
  buffers_copy = g_queue_copy (list->buffers);
183

Wim Taymans's avatar
Wim Taymans committed
184
  /* ref all buffers in the list */
185
  tmp = list->buffers->head;
186 187 188 189 190 191 192
  while (tmp) {
    if (tmp->data != GROUP_START && tmp->data != STOLEN) {
      tmp->data = gst_buffer_ref (GST_BUFFER_CAST (tmp->data));
    }
    tmp = g_list_next (tmp);
  }

193 194 195 196
  list_copy = gst_buffer_list_new ();
  g_queue_free (list_copy->buffers);
  list_copy->buffers = buffers_copy;

197 198 199 200
  return list_copy;
}

static void
201
_gst_buffer_list_free (GstBufferList * list)
202
{
203 204 205 206 207 208
  GList *tmp;

  g_return_if_fail (list != NULL);

  GST_LOG ("free %p", list);

209
  tmp = list->buffers->head;
210 211 212 213 214 215
  while (tmp) {
    if (tmp->data != GROUP_START && tmp->data != STOLEN) {
      gst_buffer_unref (GST_BUFFER_CAST (tmp->data));
    }
    tmp = tmp->next;
  }
216
  g_queue_free (list->buffers);
217

Wim Taymans's avatar
Wim Taymans committed
218 219 220 221 222 223 224 225 226 227 228
  g_slice_free1 (GST_MINI_OBJECT_SIZE (list), list);
}

static void
gst_buffer_list_init (GstBufferList * list, gsize size)
{
  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;
229 230 231 232

  list->buffers = g_queue_new ();

  GST_LOG ("init %p", list);
233 234 235 236 237 238 239 240
}

/**
 * gst_buffer_list_new:
 *
 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
 * the returned #GstBufferList.
 *
241 242 243 244
 * Free-function: gst_buffer_list_unref
 *
 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
 *     after usage.
245 246
 *
 * Since: 0.10.24
247 248 249 250 251 252
 */
GstBufferList *
gst_buffer_list_new (void)
{
  GstBufferList *list;

253 254
  list = g_slice_new0 (GstBufferList);

255 256
  GST_LOG ("new %p", list);

Wim Taymans's avatar
Wim Taymans committed
257 258
  gst_buffer_list_init (list, sizeof (GstBufferList));

259 260 261 262 263 264 265 266 267 268
  return list;
}

/**
 * gst_buffer_list_n_groups:
 * @list: a #GstBufferList
 *
 * Returns the number of groups in @list.
 *
 * Returns: the number of groups in the buffer list
269 270
 *
 * Since: 0.10.24
271 272 273 274 275 276 277 278 279
 */
guint
gst_buffer_list_n_groups (GstBufferList * list)
{
  GList *tmp;
  guint n;

  g_return_val_if_fail (list != NULL, 0);

280
  tmp = list->buffers->head;
281 282 283 284 285 286 287 288 289 290 291
  n = 0;
  while (tmp) {
    if (tmp->data == GROUP_START) {
      n++;
    }
    tmp = g_list_next (tmp);
  }

  return n;
}

Wim Taymans's avatar
Wim Taymans committed
292 293 294
/**
 * gst_buffer_list_foreach:
 * @list: a #GstBufferList
295
 * @func: (scope call): a #GstBufferListFunc to call
Wim Taymans's avatar
Wim Taymans committed
296 297 298 299 300 301 302
 * @user_data: user data passed to @func
 *
 * 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.
303 304
 *
 * Since: 0.10.24
Wim Taymans's avatar
Wim Taymans committed
305 306 307 308 309 310 311 312 313 314 315 316
 */
void
gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
    gpointer user_data)
{
  GList *tmp, *next;
  guint group, idx;
  GstBufferListItem res;

  g_return_if_fail (list != NULL);
  g_return_if_fail (func != NULL);

317
  next = list->buffers->head;
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
  group = idx = 0;
  while (next) {
    GstBuffer *buffer;

    tmp = next;
    next = g_list_next (tmp);

    buffer = tmp->data;

    if (buffer == GROUP_START) {
      group++;
      idx = 0;
      continue;
    } else if (buffer == STOLEN)
      continue;
    else
      idx++;

    /* need to decrement the indices */
    res = func (&buffer, group - 1, idx - 1, user_data);

    if (G_UNLIKELY (buffer != tmp->data)) {
      /* the function changed the buffer */
      if (buffer == NULL) {
        /* we were asked to remove the item */
343
        g_queue_delete_link (list->buffers, tmp);
Wim Taymans's avatar
Wim Taymans committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
        idx--;
      } else {
        /* change the buffer */
        tmp->data = buffer;
      }
    }

    switch (res) {
      case GST_BUFFER_LIST_CONTINUE:
        break;
      case GST_BUFFER_LIST_SKIP_GROUP:
        while (next && next->data != GROUP_START)
          next = g_list_next (next);
        break;
      case GST_BUFFER_LIST_END:
        return;
    }
  }
}

/**
 * gst_buffer_list_get:
 * @list: a #GstBufferList
 * @group: the group
 * @idx: the index in @group
 *
 * Get the buffer at @idx in @group.
 *
 * Note that this function is not efficient for iterating over the entire list.
Jonas Holmberg's avatar
Jonas Holmberg committed
373
 * Use an iterator or gst_buffer_list_foreach() instead.
Wim Taymans's avatar
Wim Taymans committed
374
 *
375 376
 * 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.
377 378
 *
 * Since: 0.10.24
Wim Taymans's avatar
Wim Taymans committed
379 380 381 382 383 384 385 386 387
 */
GstBuffer *
gst_buffer_list_get (GstBufferList * list, guint group, guint idx)
{
  GList *tmp;
  guint cgroup, cidx;

  g_return_val_if_fail (list != NULL, NULL);

388
  tmp = list->buffers->head;
Wim Taymans's avatar
Wim Taymans committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
  cgroup = 0;
  while (tmp) {
    if (tmp->data == GROUP_START) {
      if (cgroup == group) {
        /* we found the group */
        tmp = g_list_next (tmp);
        cidx = 0;
        while (tmp && tmp->data != GROUP_START) {
          if (tmp->data != STOLEN) {
            if (cidx == idx)
              return GST_BUFFER_CAST (tmp->data);
            else
              cidx++;
          }
          tmp = g_list_next (tmp);
        }
        break;
      } else {
        cgroup++;
        if (cgroup > group)
          break;
      }
    }
    tmp = g_list_next (tmp);
  }
  return NULL;
}

417 418 419 420 421 422 423
/**
 * gst_buffer_list_iterate:
 * @list: a #GstBufferList
 *
 * Iterate the buffers in @list. The owner of the iterator must also be the
 * owner of a reference to @list while the returned iterator is in use.
 *
424 425 426 427
 * Free-function: gst_buffer_list_iterator_free
 *
 * Returns: (transfer full): a new #GstBufferListIterator of the buffers in
 *     @list. gst_buffer_list_iterator_free() after usage
428 429
 *
 * Since: 0.10.24
430 431 432 433 434 435 436 437 438 439
 */
GstBufferListIterator *
gst_buffer_list_iterate (GstBufferList * list)
{
  GstBufferListIterator *it;

  g_return_val_if_fail (list != NULL, NULL);

  it = g_slice_new (GstBufferListIterator);
  it->list = list;
440
  it->next = list->buffers->head;
441 442 443 444 445 446 447
  it->last_returned = NULL;

  return it;
}

/**
 * gst_buffer_list_iterator_free:
448
 * @it: (transfer full): the #GstBufferListIterator to free
449 450
 *
 * Free the iterator.
451 452
 *
 * Since: 0.10.24
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
 */
void
gst_buffer_list_iterator_free (GstBufferListIterator * it)
{
  g_return_if_fail (it != NULL);

  g_slice_free (GstBufferListIterator, it);
}

/**
 * gst_buffer_list_iterator_n_buffers:
 * @it: a #GstBufferListIterator
 *
 * Returns the number of buffers left to iterate in the current group. I.e. the
 * number of calls that can be made to gst_buffer_list_iterator_next() before
 * it returns NULL.
 *
 * This function will not move the implicit cursor or in any other way affect
 * the state of the iterator @it.
 *
 * Returns: the number of buffers left to iterate in the current group
474 475
 *
 * Since: 0.10.24
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
 */
guint
gst_buffer_list_iterator_n_buffers (const GstBufferListIterator * it)
{
  GList *tmp;
  guint n;

  g_return_val_if_fail (it != NULL, 0);

  tmp = it->next;
  n = 0;
  while (tmp && tmp->data != GROUP_START) {
    if (tmp->data != STOLEN) {
      n++;
    }
    tmp = g_list_next (tmp);
  }

  return n;
}

/**
 * gst_buffer_list_iterator_add:
 * @it: a #GstBufferListIterator
500
 * @buffer: (transfer full): a #GstBuffer
501 502 503 504 505 506 507 508
 *
 * Inserts @buffer into the #GstBufferList iterated with @it. The buffer is
 * inserted into the current group, immediately before the buffer that would be
 * returned by gst_buffer_list_iterator_next(). The buffer is inserted before
 * the implicit cursor, a subsequent call to gst_buffer_list_iterator_next()
 * will return the buffer after the inserted buffer, if any.
 *
 * This function takes ownership of @buffer.
509 510
 *
 * Since: 0.10.24
511 512 513 514 515 516 517 518
 */
void
gst_buffer_list_iterator_add (GstBufferListIterator * it, GstBuffer * buffer)
{
  g_return_if_fail (it != NULL);
  g_return_if_fail (buffer != NULL);

  /* adding before the first group start is not allowed */
519
  g_return_if_fail (it->next != it->list->buffers->head);
520

521 522 523 524 525 526
  /* cheap insert into the GQueue */
  if (it->next != NULL) {
    g_queue_insert_before (it->list->buffers, it->next, buffer);
  } else {
    g_queue_push_tail (it->list->buffers, buffer);
  }
527 528
}

529 530 531
/**
 * gst_buffer_list_iterator_add_list:
 * @it: a #GstBufferListIterator
532
 * @list: (transfer full) (element-type Gst.Buffer): a #GList of buffers
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
 *
 * Inserts @list of buffers into the #GstBufferList iterated with @it. The list is
 * inserted into the current group, immediately before the buffer that would be
 * returned by gst_buffer_list_iterator_next(). The list is inserted before
 * the implicit cursor, a subsequent call to gst_buffer_list_iterator_next()
 * will return the buffer after the last buffer of the inserted list, if any.
 *
 * This function takes ownership of @list and all its buffers.
 *
 * Since: 0.10.31
 */
void
gst_buffer_list_iterator_add_list (GstBufferListIterator * it, GList * list)
{
  GList *last;
548
  guint len;
549 550

  g_return_if_fail (it != NULL);
551
  g_return_if_fail (it->next != it->list->buffers->head);
552 553 554 555

  if (list == NULL)
    return;

556 557 558 559 560 561
  last = list;
  len = 1;
  while (last->next) {
    last = last->next;
    len++;
  }
562

563
  if (it->next) {
564 565 566 567 568 569
    last->next = it->next;
    list->prev = it->next->prev;
    it->next->prev = last;
    if (list->prev)
      list->prev->next = list;
  } else {
570 571 572
    it->list->buffers->tail->next = list;
    list->prev = it->list->buffers->tail;
    it->list->buffers->tail = last;
573
  }
574
  it->list->buffers->length += len;
575 576
}

577 578 579 580 581 582 583 584 585
/**
 * gst_buffer_list_iterator_add_group:
 * @it: a #GstBufferListIterator
 *
 * Inserts a new, empty group into the #GstBufferList iterated with @it. The
 * group is inserted immediately before the group that would be returned by
 * gst_buffer_list_iterator_next_group(). A subsequent call to
 * gst_buffer_list_iterator_next_group() will advance the iterator to the group
 * after the inserted group, if any.
586 587
 *
 * Since: 0.10.24
588 589 590 591 592 593 594 595 596 597 598
 */
void
gst_buffer_list_iterator_add_group (GstBufferListIterator * it)
{
  g_return_if_fail (it != NULL);

  /* advance iterator to next group start */
  while (it->next != NULL && it->next->data != GROUP_START) {
    it->next = g_list_next (it->next);
  }

599 600 601 602 603 604
  /* cheap insert of a group start into the GQueue */
  if (it->next != NULL) {
    g_queue_insert_before (it->list->buffers, it->next, GROUP_START);
  } else {
    g_queue_push_tail (it->list->buffers, GROUP_START);
  }
605 606 607 608 609 610 611 612 613 614 615 616 617
}

/**
 * gst_buffer_list_iterator_next:
 * @it: a #GstBufferListIterator
 *
 * Returns the next buffer in the list iterated with @it. If the iterator is at
 * the end of a group, NULL will be returned. This function may be called
 * repeatedly to iterate through the current group.
 *
 * The caller will not get a new ref to the returned #GstBuffer and must not
 * unref it.
 *
618 619
 * Returns: (transfer none): the next buffer in the current group of the
 *     buffer list, or NULL
620 621
 *
 * Since: 0.10.24
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
 */
GstBuffer *
gst_buffer_list_iterator_next (GstBufferListIterator * it)
{
  GstBuffer *buffer;

  g_return_val_if_fail (it != NULL, NULL);

  while (it->next != NULL && it->next->data != GROUP_START &&
      it->next->data == STOLEN) {
    it->next = g_list_next (it->next);
  }

  if (it->next == NULL || it->next->data == GROUP_START) {
    goto no_buffer;
  }

  buffer = GST_BUFFER_CAST (it->next->data);

  it->last_returned = it->next;
  it->next = g_list_next (it->next);

  return buffer;

no_buffer:
Wim Taymans's avatar
Wim Taymans committed
647 648 649 650
  {
    it->last_returned = NULL;
    return NULL;
  }
651 652 653 654 655 656 657 658 659 660 661 662
}

/**
 * gst_buffer_list_iterator_next_group:
 * @it: a #GstBufferListIterator
 *
 * Advance the iterator @it to the first buffer in the next group. If the
 * iterator is at the last group, FALSE will be returned. This function may be
 * called repeatedly to iterate through the groups in a buffer list.
 *
 * Returns: TRUE if the iterator could be advanced to the next group, FALSE if
 * the iterator was already at the last group
663 664
 *
 * Since: 0.10.24
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
 */
gboolean
gst_buffer_list_iterator_next_group (GstBufferListIterator * it)
{
  g_return_val_if_fail (it != NULL, FALSE);

  /* advance iterator to next group start */
  while (it->next != NULL && it->next->data != GROUP_START) {
    it->next = g_list_next (it->next);
  }

  if (it->next) {
    /* move one step beyond the group start */
    it->next = g_list_next (it->next);
  }

  it->last_returned = NULL;

  return (it->next != NULL);
}

/**
 * gst_buffer_list_iterator_remove:
 * @it: a #GstBufferListIterator
 *
 * Removes the last buffer returned by gst_buffer_list_iterator_next() from
 * the #GstBufferList iterated with @it. gst_buffer_list_iterator_next() must
 * have been called on @it before this function is called. This function can
 * only be called once per call to gst_buffer_list_iterator_next().
 *
 * The removed buffer is unreffed.
696 697
 *
 * Since: 0.10.24
698 699 700 701 702 703 704 705 706 707 708
 */
void
gst_buffer_list_iterator_remove (GstBufferListIterator * it)
{
  g_return_if_fail (it != NULL);
  g_return_if_fail (it->last_returned != NULL);
  g_assert (it->last_returned->data != GROUP_START);

  if (it->last_returned->data != STOLEN) {
    gst_buffer_unref (it->last_returned->data);
  }
709
  g_queue_delete_link (it->list->buffers, it->last_returned);
710 711 712 713 714 715
  it->last_returned = NULL;
}

/**
 * gst_buffer_list_iterator_take:
 * @it: a #GstBufferListIterator
716
 * @buffer: (transfer full): a #GstBuffer
717 718 719 720 721 722 723 724 725 726
 *
 * Replaces the last buffer returned by gst_buffer_list_iterator_next() with
 * @buffer in the #GstBufferList iterated with @it and takes ownership of
 * @buffer. gst_buffer_list_iterator_next() must have been called on @it before
 * this function is called. gst_buffer_list_iterator_remove() must not have been
 * called since the last call to gst_buffer_list_iterator_next().
 *
 * This function unrefs the replaced buffer if it has not been stolen with
 * gst_buffer_list_iterator_steal() and takes ownership of @buffer (i.e. the
 * refcount of @buffer is not increased).
727
 *
728 729
 * FIXME 0.11: this conditional taking-ownership is not good for bindings
 *
730
 * Since: 0.10.24
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
 */
void
gst_buffer_list_iterator_take (GstBufferListIterator * it, GstBuffer * buffer)
{
  g_return_if_fail (it != NULL);
  g_return_if_fail (it->last_returned != NULL);
  g_return_if_fail (buffer != NULL);
  g_assert (it->last_returned->data != GROUP_START);

  if (it->last_returned->data != STOLEN) {
    gst_buffer_unref (it->last_returned->data);
  }
  it->last_returned->data = buffer;
}

/**
 * gst_buffer_list_iterator_steal:
 * @it: a #GstBufferListIterator
 *
 * Returns the last buffer returned by gst_buffer_list_iterator_next() without
 * modifying the refcount of the buffer.
 *
753 754
 * Returns: (transfer none): the last buffer returned by
 *     gst_buffer_list_iterator_next()
755 756
 *
 * Since: 0.10.24
757 758 759 760 761 762 763 764 765 766 767 768
 */
GstBuffer *
gst_buffer_list_iterator_steal (GstBufferListIterator * it)
{
  GstBuffer *buffer;

  g_return_val_if_fail (it != NULL, NULL);
  g_return_val_if_fail (it->last_returned != NULL, NULL);
  g_return_val_if_fail (it->last_returned->data != STOLEN, NULL);
  g_assert (it->last_returned->data != GROUP_START);

  buffer = it->last_returned->data;
Benjamin Otte's avatar
Benjamin Otte committed
769
  it->last_returned->data = (gpointer) STOLEN;
770 771 772 773 774

  return buffer;
}

/**
Wim Taymans's avatar
Wim Taymans committed
775
 * gst_buffer_list_iterator_do:
776 777
 * @it: a #GstBufferListIterator
 * @do_func: the function to be called
Wim Taymans's avatar
Wim Taymans committed
778
 * @user_data: the gpointer to optional user data.
779 780 781 782 783 784 785 786 787
 *
 * Calls the given function for the last buffer returned by
 * gst_buffer_list_iterator_next(). gst_buffer_list_iterator_next() must have
 * been called on @it before this function is called.
 * gst_buffer_list_iterator_remove() and gst_buffer_list_iterator_steal() must
 * not have been called since the last call to gst_buffer_list_iterator_next().
 *
 * See #GstBufferListDoFunction for more details.
 *
788
 * Returns: (transfer none): the return value from @do_func
789 790
 *
 * Since: 0.10.24
791 792
 */
GstBuffer *
Wim Taymans's avatar
Wim Taymans committed
793 794
gst_buffer_list_iterator_do (GstBufferListIterator * it,
    GstBufferListDoFunction do_func, gpointer user_data)
795 796 797 798 799 800 801 802 803 804 805
{
  GstBuffer *buffer;

  g_return_val_if_fail (it != NULL, NULL);
  g_return_val_if_fail (it->last_returned != NULL, NULL);
  g_return_val_if_fail (it->last_returned->data != STOLEN, NULL);
  g_return_val_if_fail (do_func != NULL, NULL);
  g_return_val_if_fail (gst_buffer_list_is_writable (it->list), NULL);
  g_assert (it->last_returned->data != GROUP_START);

  buffer = gst_buffer_list_iterator_steal (it);
Wim Taymans's avatar
Wim Taymans committed
806
  buffer = do_func (buffer, user_data);
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
  if (buffer == NULL) {
    gst_buffer_list_iterator_remove (it);
  } else {
    gst_buffer_list_iterator_take (it, buffer);
  }

  return buffer;
}

/**
 * gst_buffer_list_iterator_merge_group:
 * @it: a #GstBufferListIterator
 *
 * Merge a buffer list group into a normal #GstBuffer by copying its metadata
 * and memcpying its data into consecutive memory. All buffers in the current
 * group after the implicit cursor will be merged into one new buffer. The
 * metadata of the new buffer will be a copy of the metadata of the buffer that
 * would be returned by gst_buffer_list_iterator_next(). If there is no buffer
 * in the current group after the implicit cursor, NULL will be returned.
 *
 * This function will not move the implicit cursor or in any other way affect
 * the state of the iterator @it or the list.
 *
830 831
 * Returns: (transfer full): a new #GstBuffer, gst_buffer_unref() after usage,
 *     or NULL
832 833
 *
 * Since: 0.10.24
834 835 836 837 838
 */
GstBuffer *
gst_buffer_list_iterator_merge_group (const GstBufferListIterator * it)
{
  GList *tmp;
Wim Taymans's avatar
Wim Taymans committed
839
  gsize size, bsize;
840
  GstBuffer *buf;
Wim Taymans's avatar
Wim Taymans committed
841
  guint8 *dest, *ptr, *bdata;
842 843 844 845 846 847 848 849

  g_return_val_if_fail (it != NULL, NULL);

  /* calculate size of merged buffer */
  size = 0;
  tmp = it->next;
  while (tmp && tmp->data != GROUP_START) {
    if (tmp->data != STOLEN) {
Wim Taymans's avatar
Wim Taymans committed
850
      size += gst_buffer_get_size (tmp->data);
851 852 853 854 855 856 857 858 859 860 861 862
    }
    tmp = g_list_next (tmp);
  }

  if (size == 0) {
    return NULL;
  }

  /* allocate a new buffer */
  buf = gst_buffer_new_and_alloc (size);

  /* copy metadata from the next buffer after the implicit cursor */
Wim Taymans's avatar
Wim Taymans committed
863 864
  gst_buffer_copy_into (buf, GST_BUFFER_CAST (it->next->data),
      GST_BUFFER_COPY_METADATA, 0, 0);
865 866

  /* copy data of all buffers before the next group start into the new buffer */
Wim Taymans's avatar
Wim Taymans committed
867
  dest = ptr = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
868 869 870
  tmp = it->next;
  do {
    if (tmp->data != STOLEN) {
Wim Taymans's avatar
Wim Taymans committed
871 872 873 874 875 876 877

      bdata =
          gst_buffer_map (GST_BUFFER_CAST (tmp->data), &bsize, NULL,
          GST_MAP_READ);
      memcpy (ptr, bdata, bsize);
      gst_buffer_unmap (GST_BUFFER_CAST (tmp->data), bdata, bsize);
      ptr += bsize;
878 879 880 881
    }
    tmp = g_list_next (tmp);
  } while (tmp && tmp->data != GROUP_START);

Wim Taymans's avatar
Wim Taymans committed
882 883
  gst_buffer_unmap (buf, dest, size);

884 885
  return buf;
}