gstbufferlist.c 23.1 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 145 146 147 148 149 150 151 152
 */
struct _GstBufferList
{
  GstMiniObject mini_object;

  GList *buffers;
};

struct _GstBufferListClass
{
  GstMiniObjectClass mini_object_class;
};

153 154 155
/**
 * GstBufferListIterator:
 *
Wim Taymans's avatar
Wim Taymans committed
156
 * Opaque iterator for a #GstBufferList.
157 158
 *
 * Since: 0.10.24
159 160 161 162 163 164 165 166 167
 */
struct _GstBufferListIterator
{
  GstBufferList *list;
  GList *next;
  GList *last_returned;
};

static GType _gst_buffer_list_type = 0;
168 169

G_DEFINE_TYPE (GstBufferList, gst_buffer_list, GST_TYPE_MINI_OBJECT);
170 171 172 173

void
_gst_buffer_list_initialize (void)
{
174 175 176 177
  GType type = gst_buffer_list_get_type ();

  g_type_class_ref (type);
  _gst_buffer_list_type = type;
178 179 180
}

static void
181
gst_buffer_list_init (GstBufferList * list)
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
{
  list->buffers = NULL;

  GST_LOG ("init %p", list);
}

static void
gst_buffer_list_finalize (GstBufferList * list)
{
  GList *tmp;

  g_return_if_fail (list != NULL);

  GST_LOG ("finalize %p", list);

  tmp = list->buffers;
  while (tmp) {
    if (tmp->data != GROUP_START && tmp->data != STOLEN) {
      gst_buffer_unref (GST_BUFFER_CAST (tmp->data));
    }
    tmp = tmp->next;
  }
  g_list_free (list->buffers);

206
/* Not chaining up because GstMiniObject::finalize() does nothing
207
  GST_MINI_OBJECT_CLASS (gst_buffer_list_parent_class)->finalize
208
      (GST_MINI_OBJECT_CAST (list));*/
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
}

static GstBufferList *
_gst_buffer_list_copy (GstBufferList * list)
{
  GstBufferList *list_copy;
  GList *tmp;

  g_return_val_if_fail (list != NULL, NULL);

  list_copy = gst_buffer_list_new ();

  /* shallow copy of list and pointers */
  list_copy->buffers = g_list_copy (list->buffers);

Wim Taymans's avatar
Wim Taymans committed
224
  /* ref all buffers in the list */
225 226 227 228 229 230 231 232 233 234 235 236
  tmp = list_copy->buffers;
  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);
  }

  return list_copy;
}

static void
237
gst_buffer_list_class_init (GstBufferListClass * list_class)
238 239 240 241 242 243 244 245 246 247 248 249 250
{
  list_class->mini_object_class.copy =
      (GstMiniObjectCopyFunction) _gst_buffer_list_copy;
  list_class->mini_object_class.finalize =
      (GstMiniObjectFinalizeFunction) gst_buffer_list_finalize;
}

/**
 * gst_buffer_list_new:
 *
 * Creates a new, empty #GstBufferList. The caller is responsible for unreffing
 * the returned #GstBufferList.
 *
251 252 253 254
 * Free-function: gst_buffer_list_unref
 *
 * Returns: (transfer full): the new #GstBufferList. gst_buffer_list_unref()
 *     after usage.
255 256
 *
 * Since: 0.10.24
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
 */
GstBufferList *
gst_buffer_list_new (void)
{
  GstBufferList *list;

  list = (GstBufferList *) gst_mini_object_new (_gst_buffer_list_type);

  GST_LOG ("new %p", list);

  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
277 278
 *
 * Since: 0.10.24
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
 */
guint
gst_buffer_list_n_groups (GstBufferList * list)
{
  GList *tmp;
  guint n;

  g_return_val_if_fail (list != NULL, 0);

  tmp = list->buffers;
  n = 0;
  while (tmp) {
    if (tmp->data == GROUP_START) {
      n++;
    }
    tmp = g_list_next (tmp);
  }

  return n;
}

Wim Taymans's avatar
Wim Taymans committed
300 301 302 303 304 305 306 307 308 309 310
/**
 * gst_buffer_list_foreach:
 * @list: a #GstBufferList
 * @func: a #GstBufferListFunc to call
 * @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.
311 312
 *
 * Since: 0.10.24
Wim Taymans's avatar
Wim Taymans committed
313 314 315 316 317 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 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 373 374 375 376 377 378 379 380
 */
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);

  next = list->buffers;
  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 */
        list->buffers = g_list_delete_link (list->buffers, tmp);
        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
381
 * Use an iterator or gst_buffer_list_foreach() instead.
Wim Taymans's avatar
Wim Taymans committed
382
 *
383 384
 * 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.
385 386
 *
 * Since: 0.10.24
Wim Taymans's avatar
Wim Taymans committed
387 388 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 417 418 419 420 421 422 423 424
 */
GstBuffer *
gst_buffer_list_get (GstBufferList * list, guint group, guint idx)
{
  GList *tmp;
  guint cgroup, cidx;

  g_return_val_if_fail (list != NULL, NULL);

  tmp = list->buffers;
  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;
}

425 426 427 428 429 430 431
/**
 * 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.
 *
432 433 434 435
 * 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
436 437
 *
 * Since: 0.10.24
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
 */
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;
  it->next = list->buffers;
  it->last_returned = NULL;

  return it;
}

/**
 * gst_buffer_list_iterator_free:
456
 * @it: (transfer full): the #GstBufferListIterator to free
457 458
 *
 * Free the iterator.
459 460
 *
 * Since: 0.10.24
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
 */
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
482 483
 *
 * Since: 0.10.24
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
 */
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
508
 * @buffer: (transfer full): a #GstBuffer
509 510 511 512 513 514 515 516
 *
 * 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.
517 518
 *
 * Since: 0.10.24
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
 */
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 */
  g_return_if_fail (it->next != it->list->buffers);

  /* cheap insert into the GList */
  it->list->buffers = g_list_insert_before (it->list->buffers, it->next,
      buffer);
}

534 535 536
/**
 * gst_buffer_list_iterator_add_list:
 * @it: a #GstBufferListIterator
537
 * @list: (transfer full) (element-type Gst.Buffer): a #GList of buffers
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
 *
 * 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;

  g_return_if_fail (it != NULL);
  g_return_if_fail (it->next != it->list->buffers);

  if (list == NULL)
    return;

  if (it->next) {
    last = list;
    while (last->next)
      last = last->next;

    last->next = it->next;
    list->prev = it->next->prev;
    it->next->prev = last;
    if (list->prev)
      list->prev->next = list;
  } else {
    last = it->list->buffers;
    while (last->next)
      last = last->next;

    last->next = list;
    list->prev = last;
  }
}

580 581 582 583 584 585 586 587 588
/**
 * 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.
589 590
 *
 * Since: 0.10.24
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
 */
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);
  }

  /* cheap insert of a group start into the GList */
  it->list->buffers = g_list_insert_before (it->list->buffers, it->next,
      GROUP_START);
}

/**
 * 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 709 710 711 712 713 714 715
 */
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);
  }
  it->list->buffers = g_list_delete_link (it->list->buffers, it->last_returned);
  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 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
 */
GstBuffer *
gst_buffer_list_iterator_merge_group (const GstBufferListIterator * it)
{
  GList *tmp;
  guint size;
  GstBuffer *buf;
  guint8 *ptr;

  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) {
      size += GST_BUFFER_SIZE (tmp->data);
    }
    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 */
  gst_buffer_copy_metadata (buf, GST_BUFFER_CAST (it->next->data),
      GST_BUFFER_COPY_ALL);

  /* copy data of all buffers before the next group start into the new buffer */
  ptr = GST_BUFFER_DATA (buf);
  tmp = it->next;
  do {
    if (tmp->data != STOLEN) {
      memcpy (ptr, GST_BUFFER_DATA (tmp->data), GST_BUFFER_SIZE (tmp->data));
      ptr += GST_BUFFER_SIZE (tmp->data);
    }
    tmp = g_list_next (tmp);
  } while (tmp && tmp->data != GROUP_START);

  return buf;
}