Debian Codegraph @

libs/gst/base/gstadapter.c

raw
1 /* GStreamer
2 * Copyright (C) 2004 Benjamin Otte <[email protected]>
3 * 2005 Wim Taymans <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * SECTION:gstadapter
23 * @title: GstAdapter
24 * @short_description: adapts incoming data on a sink pad into chunks of N bytes
25 *
26 * This class is for elements that receive buffers in an undesired size.
27 * While for example raw video contains one image per buffer, the same is not
28 * true for a lot of other formats, especially those that come directly from
29 * a file. So if you have undefined buffer sizes and require a specific size,
30 * this object is for you.
31 *
32 * An adapter is created with gst_adapter_new(). It can be freed again with
33 * g_object_unref().
34 *
35 * The theory of operation is like this: All buffers received are put
36 * into the adapter using gst_adapter_push() and the data is then read back
37 * in chunks of the desired size using gst_adapter_map()/gst_adapter_unmap()
38 * and/or gst_adapter_copy(). After the data has been processed, it is freed
39 * using gst_adapter_unmap().
40 *
41 * Other methods such as gst_adapter_take() and gst_adapter_take_buffer()
42 * combine gst_adapter_map() and gst_adapter_unmap() in one method and are
43 * potentially more convenient for some use cases.
44 *
45 * For example, a sink pad's chain function that needs to pass data to a library
46 * in 512-byte chunks could be implemented like this:
47 * |[<!-- language="C" -->
48 * static GstFlowReturn
49 * sink_pad_chain (GstPad *pad, GstObject *parent, GstBuffer *buffer)
50 * {
51 * MyElement *this;
52 * GstAdapter *adapter;
53 * GstFlowReturn ret = GST_FLOW_OK;
54 *
55 * this = MY_ELEMENT (parent);
56 *
57 * adapter = this->adapter;
58 *
59 * // put buffer into adapter
60 * gst_adapter_push (adapter, buffer);
61 *
62 * // while we can read out 512 bytes, process them
63 * while (gst_adapter_available (adapter) >= 512 && ret == GST_FLOW_OK) {
64 * const guint8 *data = gst_adapter_map (adapter, 512);
65 * // use flowreturn as an error value
66 * ret = my_library_foo (data);
67 * gst_adapter_unmap (adapter);
68 * gst_adapter_flush (adapter, 512);
69 * }
70 * return ret;
71 * }
72 * ]|
73 *
74 * For another example, a simple element inside GStreamer that uses #GstAdapter
75 * is the libvisual element.
76 *
77 * An element using #GstAdapter in its sink pad chain function should ensure that
78 * when the FLUSH_STOP event is received, that any queued data is cleared using
79 * gst_adapter_clear(). Data should also be cleared or processed on EOS and
80 * when changing state from %GST_STATE_PAUSED to %GST_STATE_READY.
81 *
82 * Also check the GST_BUFFER_FLAG_DISCONT flag on the buffer. Some elements might
83 * need to clear the adapter after a discontinuity.
84 *
85 * The adapter will keep track of the timestamps of the buffers
86 * that were pushed. The last seen timestamp before the current position
87 * can be queried with gst_adapter_prev_pts(). This function can
88 * optionally return the number of bytes between the start of the buffer that
89 * carried the timestamp and the current adapter position. The distance is
90 * useful when dealing with, for example, raw audio samples because it allows
91 * you to calculate the timestamp of the current adapter position by using the
92 * last seen timestamp and the amount of bytes since. Additionally, the
93 * gst_adapter_prev_pts_at_offset() can be used to determine the last
94 * seen timestamp at a particular offset in the adapter.
95 *
96 * The adapter will also keep track of the offset of the buffers
97 * (#GST_BUFFER_OFFSET) that were pushed. The last seen offset before the
98 * current position can be queried with gst_adapter_prev_offset(). This function
99 * can optionally return the number of bytes between the start of the buffer
100 * that carried the offset and the current adapter position.
101 *
102 * Additionally the adapter also keeps track of the PTS, DTS and buffer offset
103 * at the last discontinuity, which can be retrieved with
104 * gst_adapter_pts_at_discont(), gst_adapter_dts_at_discont() and
105 * gst_adapter_offset_at_discont(). The number of bytes that were consumed
106 * since then can be queried with gst_adapter_distance_from_discont().
107 *
108 * A last thing to note is that while #GstAdapter is pretty optimized,
109 * merging buffers still might be an operation that requires a `malloc()` and
110 * `memcpy()` operation, and these operations are not the fastest. Because of
111 * this, some functions like gst_adapter_available_fast() are provided to help
112 * speed up such cases should you want to. To avoid repeated memory allocations,
113 * gst_adapter_copy() can be used to copy data into a (statically allocated)
114 * user provided buffer.
115 *
116 * #GstAdapter is not MT safe. All operations on an adapter must be serialized by
117 * the caller. This is not normally a problem, however, as the normal use case
118 * of #GstAdapter is inside one pad's chain function, in which case access is
119 * serialized via the pad's STREAM_LOCK.
120 *
121 * Note that gst_adapter_push() takes ownership of the buffer passed. Use
122 * gst_buffer_ref() before pushing it into the adapter if you still want to
123 * access the buffer later. The adapter will never modify the data in the
124 * buffer pushed in it.
125 */
126
127 #include <gst/gst_private.h>
128 #include "gstadapter.h"
129 #include <string.h>
130 #include <gst/base/gstqueuearray.h>
131
132 /* default size for the assembled data buffer */
133 #define DEFAULT_SIZE 4096
134
135 static void gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush);
136
137 GST_DEBUG_CATEGORY_STATIC (gst_adapter_debug);
138 #define GST_CAT_DEFAULT gst_adapter_debug
139
140 struct _GstAdapter
141 {
142 GObject object;
143
144 /*< private > */
145 GstVecDeque *bufqueue;
146 gsize size;
147 gsize skip;
148 guint count;
149
150 /* we keep state of assembled pieces */
151 gpointer assembled_data;
152 gsize assembled_size;
153 gsize assembled_len;
154
155 GstClockTime pts;
156 guint64 pts_distance;
157 GstClockTime dts;
158 guint64 dts_distance;
159 guint64 offset;
160 guint64 offset_distance;
161
162 gsize scan_offset;
163 /* G_MAXUINT when unset */
164 guint scan_entry_idx;
165
166 GstClockTime pts_at_discont;
167 GstClockTime dts_at_discont;
168 guint64 offset_at_discont;
169
170 guint64 distance_from_discont;
171
172 GstMapInfo info;
173 };
174
175 struct _GstAdapterClass
176 {
177 GObjectClass parent_class;
178 };
179
180 #define _do_init \
181 GST_DEBUG_CATEGORY_INIT (gst_adapter_debug, "adapter", 0, "object to splice and merge buffers to desired size")
182 #define gst_adapter_parent_class parent_class
183 G_DEFINE_TYPE_WITH_CODE (GstAdapter, gst_adapter, G_TYPE_OBJECT, _do_init);
184
185 static void gst_adapter_dispose (GObject * object);
186 static void gst_adapter_finalize (GObject * object);
187
188 static void
189 gst_adapter_class_init (GstAdapterClass * klass)
190 {
191 GObjectClass *object = G_OBJECT_CLASS (klass);
192
193 object->dispose = gst_adapter_dispose;
194 object->finalize = gst_adapter_finalize;
195 }
196
197 static void
198 gst_adapter_init (GstAdapter * adapter)
199 {
200 adapter->assembled_data = g_malloc (DEFAULT_SIZE);
201 adapter->assembled_size = DEFAULT_SIZE;
202 adapter->pts = GST_CLOCK_TIME_NONE;
203 adapter->pts_distance = 0;
204 adapter->dts = GST_CLOCK_TIME_NONE;
205 adapter->dts_distance = 0;
206 adapter->offset = GST_BUFFER_OFFSET_NONE;
207 adapter->offset_distance = 0;
208 adapter->pts_at_discont = GST_CLOCK_TIME_NONE;
209 adapter->dts_at_discont = GST_CLOCK_TIME_NONE;
210 adapter->offset_at_discont = GST_BUFFER_OFFSET_NONE;
211 adapter->distance_from_discont = 0;
212 adapter->bufqueue = gst_vec_deque_new (10);
213 }
214
215 static void
216 gst_adapter_dispose (GObject * object)
217 {
218 GstAdapter *adapter = GST_ADAPTER (object);
219
220 gst_adapter_clear (adapter);
221
222 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
223 }
224
225 static void
226 gst_adapter_finalize (GObject * object)
227 {
228 GstAdapter *adapter = GST_ADAPTER (object);
229
230 g_free (adapter->assembled_data);
231
232 gst_vec_deque_free (adapter->bufqueue);
233
234 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
235 }
236
237 /**
238 * gst_adapter_new:
239 *
240 * Creates a new #GstAdapter. Free with g_object_unref().
241 *
242 * Returns: (transfer full): a new #GstAdapter
243 */
244 GstAdapter *
245 gst_adapter_new (void)
246 {
247 return g_object_new (GST_TYPE_ADAPTER, NULL);
248 }
249
250 /**
251 * gst_adapter_clear:
252 * @adapter: a #GstAdapter
253 *
254 * Removes all buffers from @adapter.
255 */
256 void
257 gst_adapter_clear (GstAdapter * adapter)
258 {
259 GstMiniObject *obj;
260 g_return_if_fail (GST_IS_ADAPTER (adapter));
261
262 if (adapter->info.memory)
263 gst_adapter_unmap (adapter);
264
265 while ((obj = gst_vec_deque_pop_head (adapter->bufqueue)))
266 gst_mini_object_unref (obj);
267
268 adapter->count = 0;
269 adapter->size = 0;
270 adapter->skip = 0;
271 adapter->assembled_len = 0;
272 adapter->pts = GST_CLOCK_TIME_NONE;
273 adapter->pts_distance = 0;
274 adapter->dts = GST_CLOCK_TIME_NONE;
275 adapter->dts_distance = 0;
276 adapter->offset = GST_BUFFER_OFFSET_NONE;
277 adapter->offset_distance = 0;
278 adapter->pts_at_discont = GST_CLOCK_TIME_NONE;
279 adapter->dts_at_discont = GST_CLOCK_TIME_NONE;
280 adapter->offset_at_discont = GST_BUFFER_OFFSET_NONE;
281 adapter->distance_from_discont = 0;
282 adapter->scan_offset = 0;
283 adapter->scan_entry_idx = G_MAXUINT;
284 }
285
286 static inline void
287 update_timestamps_and_offset (GstAdapter * adapter, GstBuffer * buf)
288 {
289 GstClockTime pts, dts;
290 guint64 offset;
291
292 pts = GST_BUFFER_PTS (buf);
293 if (GST_CLOCK_TIME_IS_VALID (pts)) {
294 GST_LOG_OBJECT (adapter, "new pts %" GST_TIME_FORMAT, GST_TIME_ARGS (pts));
295 adapter->pts = pts;
296 adapter->pts_distance = 0;
297 }
298 dts = GST_BUFFER_DTS (buf);
299 if (GST_CLOCK_TIME_IS_VALID (dts)) {
300 GST_LOG_OBJECT (adapter, "new dts %" GST_TIME_FORMAT, GST_TIME_ARGS (dts));
301 adapter->dts = dts;
302 adapter->dts_distance = 0;
303 }
304 offset = GST_BUFFER_OFFSET (buf);
305 if (offset != GST_BUFFER_OFFSET_NONE) {
306 GST_LOG_OBJECT (adapter, "new offset %" G_GUINT64_FORMAT, offset);
307 adapter->offset = offset;
308 adapter->offset_distance = 0;
309 }
310
311 if (GST_BUFFER_IS_DISCONT (buf)) {
312 /* Take values as-is (might be NONE) */
313 adapter->pts_at_discont = pts;
314 adapter->dts_at_discont = dts;
315 adapter->offset_at_discont = offset;
316 adapter->distance_from_discont = 0;
317 }
318 }
319
320 /* copy data into @dest, skipping @skip bytes from the head buffers */
321 static void
322 copy_into_unchecked (GstAdapter * adapter, guint8 * dest, gsize skip,
323 gsize size)
324 {
325 GstBuffer *buf;
326 gsize bsize, csize;
327 guint idx = 0;
328
329 /* first step, do skipping */
330 /* we might well be copying where we were scanning */
331 if (adapter->scan_entry_idx != G_MAXUINT && (adapter->scan_offset <= skip)) {
332 idx = adapter->scan_entry_idx;
333 skip -= adapter->scan_offset;
334 } else {
335 idx = 0;
336 }
337 buf = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
338 bsize = gst_buffer_get_size (buf);
339 while (G_UNLIKELY (skip >= bsize)) {
340 skip -= bsize;
341 buf = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
342 bsize = gst_buffer_get_size (buf);
343 }
344 /* copy partial buffer */
345 csize = MIN (bsize - skip, size);
346 GST_DEBUG ("bsize %" G_GSIZE_FORMAT ", skip %" G_GSIZE_FORMAT ", csize %"
347 G_GSIZE_FORMAT, bsize, skip, csize);
348 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter, "extract %" G_GSIZE_FORMAT
349 " bytes", csize);
350 gst_buffer_extract (buf, skip, dest, csize);
351 size -= csize;
352 dest += csize;
353
354 /* second step, copy remainder */
355 while (size > 0) {
356 buf = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
357 bsize = gst_buffer_get_size (buf);
358 if (G_LIKELY (bsize > 0)) {
359 csize = MIN (bsize, size);
360 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
361 "extract %" G_GSIZE_FORMAT " bytes", csize);
362 gst_buffer_extract (buf, 0, dest, csize);
363 size -= csize;
364 dest += csize;
365 }
366 }
367 }
368
369 /**
370 * gst_adapter_push:
371 * @adapter: a #GstAdapter
372 * @buf: (transfer full): a #GstBuffer to add to queue in the adapter
373 *
374 * Adds the data from @buf to the data stored inside @adapter and takes
375 * ownership of the buffer.
376 */
377 void
378 gst_adapter_push (GstAdapter * adapter, GstBuffer * buf)
379 {
380 gsize size;
381
382 g_return_if_fail (GST_IS_ADAPTER (adapter));
383 g_return_if_fail (GST_IS_BUFFER (buf));
384
385 size = gst_buffer_get_size (buf);
386 adapter->size += size;
387
388 /* Note: merging buffers at this point is premature. */
389 if (gst_vec_deque_is_empty (adapter->bufqueue)) {
390 GST_LOG_OBJECT (adapter, "pushing %p first %" G_GSIZE_FORMAT " bytes",
391 buf, size);
392 gst_vec_deque_push_tail (adapter->bufqueue, buf);
393 update_timestamps_and_offset (adapter, buf);
394 } else {
395 /* Otherwise append to the end, and advance our end pointer */
396 GST_LOG_OBJECT (adapter, "pushing %p %" G_GSIZE_FORMAT " bytes at end, "
397 "size now %" G_GSIZE_FORMAT, buf, size, adapter->size);
398 gst_vec_deque_push_tail (adapter->bufqueue, buf);
399 }
400 ++adapter->count;
401 }
402
403 #if 0
404 /* Internal method only. Tries to merge buffers at the head of the queue
405 * to form a single larger buffer of size 'size'.
406 *
407 * Returns %TRUE if it managed to merge anything.
408 */
409 static gboolean
410 gst_adapter_try_to_merge_up (GstAdapter * adapter, gsize size)
411 {
412 GstBuffer *cur, *head;
413 GSList *g;
414 gboolean ret = FALSE;
415 gsize hsize;
416
417 g = adapter->buflist;
418 if (g == NULL)
419 return FALSE;
420
421 head = g->data;
422
423 hsize = gst_buffer_get_size (head);
424
425 /* Remove skipped part from the buffer (otherwise the buffer might grow indefinitely) */
426 head = gst_buffer_make_writable (head);
427 gst_buffer_resize (head, adapter->skip, hsize - adapter->skip);
428 hsize -= adapter->skip;
429 adapter->skip = 0;
430 g->data = head;
431
432 g = g_slist_next (g);
433
434 while (g != NULL && hsize < size) {
435 cur = g->data;
436 /* Merge the head buffer and the next in line */
437 GST_LOG_OBJECT (adapter, "Merging buffers of size %" G_GSIZE_FORMAT " & %"
438 G_GSIZE_FORMAT " in search of target %" G_GSIZE_FORMAT,
439 hsize, gst_buffer_get_size (cur), size);
440
441 head = gst_buffer_append (head, cur);
442 hsize = gst_buffer_get_size (head);
443 ret = TRUE;
444
445 /* Delete the front list item, and store our new buffer in the 2nd list
446 * item */
447 adapter->buflist = g_slist_delete_link (adapter->buflist, adapter->buflist);
448 g->data = head;
449
450 /* invalidate scan position */
451 adapter->scan_offset = 0;
452 adapter->scan_entry = NULL;
453
454 g = g_slist_next (g);
455 }
456
457 return ret;
458 }
459 #endif
460
461 /**
462 * gst_adapter_map:
463 * @adapter: a #GstAdapter
464 * @size: (in): the number of bytes to map/peek
465 *
466 * Gets the first @size bytes stored in the @adapter. The returned pointer is
467 * valid until the next function is called on the adapter.
468 *
469 * Note that setting the returned pointer as the data of a #GstBuffer is
470 * incorrect for general-purpose plugins. The reason is that if a downstream
471 * element stores the buffer so that it has access to it outside of the bounds
472 * of its chain function, the buffer will have an invalid data pointer after
473 * your element flushes the bytes. In that case you should use
474 * gst_adapter_take(), which returns a freshly-allocated buffer that you can set
475 * as #GstBuffer memory or the potentially more performant
476 * gst_adapter_take_buffer().
477 *
478 * Returns %NULL if @size bytes are not available.
479 *
480 * Returns: (transfer none) (array length=size) (element-type guint8) (nullable):
481 * a pointer to the first @size bytes of data, or %NULL
482 */
483 gconstpointer
484 gst_adapter_map (GstAdapter * adapter, gsize size)
485 {
486 GstBuffer *cur;
487 gsize skip, csize;
488 gsize toreuse, tocopy;
489 guint8 *data;
490
491 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
492 g_return_val_if_fail (size > 0, NULL);
493
494 if (adapter->info.memory)
495 gst_adapter_unmap (adapter);
496
497 /* we don't have enough data, return NULL. This is unlikely
498 * as one usually does an _available() first instead of peeking a
499 * random size. */
500 if (G_UNLIKELY (size > adapter->size))
501 return NULL;
502
503 /* we have enough assembled data, return it */
504 if (adapter->assembled_len >= size)
505 return adapter->assembled_data;
506
507 #if 0
508 do {
509 #endif
510 cur = gst_vec_deque_peek_head (adapter->bufqueue);
511 skip = adapter->skip;
512
513 csize = gst_buffer_get_size (cur);
514 if (csize >= size + skip) {
515 if (!gst_buffer_map (cur, &adapter->info, GST_MAP_READ))
516 return FALSE;
517
518 return (guint8 *) adapter->info.data + skip;
519 }
520 /* We may be able to efficiently merge buffers in our pool to
521 * gather a big enough chunk to return it from the head buffer directly */
522 #if 0
523 } while (gst_adapter_try_to_merge_up (adapter, size));
524 #endif
525
526 /* see how much data we can reuse from the assembled memory and how much
527 * we need to copy */
528 toreuse = adapter->assembled_len;
529 tocopy = size - toreuse;
530
531 /* Gonna need to copy stuff out */
532 if (G_UNLIKELY (adapter->assembled_size < size)) {
533 adapter->assembled_size = (size / DEFAULT_SIZE + 1) * DEFAULT_SIZE;
534 GST_DEBUG_OBJECT (adapter, "resizing internal buffer to %" G_GSIZE_FORMAT,
535 adapter->assembled_size);
536 if (toreuse == 0) {
537 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "alloc new buffer");
538 /* no g_realloc to avoid a memcpy that is not desired here since we are
539 * not going to reuse any data here */
540 g_free (adapter->assembled_data);
541 adapter->assembled_data = g_malloc (adapter->assembled_size);
542 } else {
543 /* we are going to reuse all data, realloc then */
544 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "reusing %" G_GSIZE_FORMAT " bytes",
545 toreuse);
546 adapter->assembled_data =
547 g_realloc (adapter->assembled_data, adapter->assembled_size);
548 }
549 }
550 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy remaining %" G_GSIZE_FORMAT
551 " bytes from adapter", tocopy);
552 data = adapter->assembled_data;
553 copy_into_unchecked (adapter, data + toreuse, skip + toreuse, tocopy);
554 adapter->assembled_len = size;
555
556 return adapter->assembled_data;
557 }
558
559 /**
560 * gst_adapter_unmap:
561 * @adapter: a #GstAdapter
562 *
563 * Releases the memory obtained with the last gst_adapter_map().
564 */
565 void
566 gst_adapter_unmap (GstAdapter * adapter)
567 {
568 g_return_if_fail (GST_IS_ADAPTER (adapter));
569
570 if (adapter->info.memory) {
571 GstBuffer *cur = gst_vec_deque_peek_head (adapter->bufqueue);
572 GST_LOG_OBJECT (adapter, "unmap memory buffer %p", cur);
573 gst_buffer_unmap (cur, &adapter->info);
574 adapter->info.memory = NULL;
575 }
576 }
577
578 /**
579 * gst_adapter_copy: (skip)
580 * @adapter: a #GstAdapter
581 * @dest: (out caller-allocates) (array length=size) (element-type guint8):
582 * the memory to copy into
583 * @offset: the bytes offset in the adapter to start from
584 * @size: (in): the number of bytes to copy
585 *
586 * Copies @size bytes of data starting at @offset out of the buffers
587 * contained in #GstAdapter into an array @dest provided by the caller.
588 *
589 * The array @dest should be large enough to contain @size bytes.
590 * The user should check that the adapter has (@offset + @size) bytes
591 * available before calling this function.
592 */
593 void
594 gst_adapter_copy (GstAdapter * adapter, gpointer dest, gsize offset, gsize size)
595 {
596 g_return_if_fail (GST_IS_ADAPTER (adapter));
597 g_return_if_fail (size > 0);
598 g_return_if_fail (offset + size <= adapter->size);
599
600 copy_into_unchecked (adapter, dest, offset + adapter->skip, size);
601 }
602
603 /**
604 * gst_adapter_copy_bytes: (rename-to gst_adapter_copy)
605 * @adapter: a #GstAdapter
606 * @offset: the bytes offset in the adapter to start from
607 * @size: the number of bytes to copy
608 *
609 * Similar to gst_adapter_copy, but more suitable for language bindings. @size
610 * bytes of data starting at @offset will be copied out of the buffers contained
611 * in @adapter and into a new #GBytes structure which is returned. Depending on
612 * the value of the @size argument an empty #GBytes structure may be returned.
613 *
614 * Returns: (transfer full): A new #GBytes structure containing the copied data.
615 *
616 * Since: 1.4
617 */
618 GBytes *
619 gst_adapter_copy_bytes (GstAdapter * adapter, gsize offset, gsize size)
620 {
621 gpointer data;
622 data = g_malloc (size);
623 gst_adapter_copy (adapter, data, offset, size);
624 return g_bytes_new_take (data, size);
625 }
626
627 /*Flushes the first @flush bytes in the @adapter*/
628 static void
629 gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush)
630 {
631 GstBuffer *cur;
632 gsize size;
633
634 GST_LOG_OBJECT (adapter, "flushing %" G_GSIZE_FORMAT " bytes", flush);
635
636 if (adapter->info.memory)
637 gst_adapter_unmap (adapter);
638
639 /* clear state */
640 adapter->size -= flush;
641 adapter->assembled_len = 0;
642
643 /* take skip into account */
644 flush += adapter->skip;
645 /* distance is always at least the amount of skipped bytes */
646 adapter->pts_distance -= adapter->skip;
647 adapter->dts_distance -= adapter->skip;
648 adapter->offset_distance -= adapter->skip;
649 adapter->distance_from_discont -= adapter->skip;
650
651 cur = gst_vec_deque_peek_head (adapter->bufqueue);
652 size = gst_buffer_get_size (cur);
653 while (flush >= size) {
654 /* can skip whole buffer */
655 GST_LOG_OBJECT (adapter, "flushing out head buffer");
656 adapter->pts_distance += size;
657 adapter->dts_distance += size;
658 adapter->offset_distance += size;
659 adapter->distance_from_discont += size;
660 flush -= size;
661
662 --adapter->count;
663
664 cur = NULL;
665 gst_buffer_unref (gst_vec_deque_pop_head (adapter->bufqueue));
666
667 if (gst_vec_deque_is_empty (adapter->bufqueue)) {
668 GST_LOG_OBJECT (adapter, "adapter empty now");
669 break;
670 }
671 /* there is a new head buffer, update the timestamps */
672 cur = gst_vec_deque_peek_head (adapter->bufqueue);
673 update_timestamps_and_offset (adapter, cur);
674 size = gst_buffer_get_size (cur);
675 }
676 /* account for the remaining bytes */
677 adapter->skip = flush;
678 adapter->pts_distance += flush;
679 adapter->dts_distance += flush;
680 adapter->offset_distance += flush;
681 adapter->distance_from_discont += flush;
682 /* invalidate scan position */
683 adapter->scan_offset = 0;
684 adapter->scan_entry_idx = G_MAXUINT;
685 }
686
687 /**
688 * gst_adapter_flush:
689 * @adapter: a #GstAdapter
690 * @flush: the number of bytes to flush
691 *
692 * Flushes the first @flush bytes in the @adapter. The caller must ensure that
693 * at least this many bytes are available.
694 *
695 * See also: gst_adapter_map(), gst_adapter_unmap()
696 */
697 void
698 gst_adapter_flush (GstAdapter * adapter, gsize flush)
699 {
700 g_return_if_fail (GST_IS_ADAPTER (adapter));
701 g_return_if_fail (flush <= adapter->size);
702
703 /* flushing out 0 bytes will do nothing */
704 if (G_UNLIKELY (flush == 0))
705 return;
706
707 gst_adapter_flush_unchecked (adapter, flush);
708 }
709
710 /* internal function, nbytes should be flushed if needed after calling this function */
711 static guint8 *
712 gst_adapter_get_internal (GstAdapter * adapter, gsize nbytes)
713 {
714 guint8 *data;
715 gsize toreuse, tocopy;
716
717 /* see how much data we can reuse from the assembled memory and how much
718 * we need to copy */
719 toreuse = MIN (nbytes, adapter->assembled_len);
720 tocopy = nbytes - toreuse;
721
722 /* find memory to return */
723 if (adapter->assembled_size >= nbytes && toreuse > 0) {
724 /* we reuse already allocated memory but only when we're going to reuse
725 * something from it because else we are worse than the malloc and copy
726 * case below */
727 GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes of assembled"
728 " data", toreuse);
729 /* we have enough free space in the assembled array */
730 data = adapter->assembled_data;
731 /* flush after this function should set the assembled_size to 0 */
732 adapter->assembled_data = g_malloc (adapter->assembled_size);
733 } else {
734 GST_LOG_OBJECT (adapter, "allocating %" G_GSIZE_FORMAT " bytes", nbytes);
735 /* not enough bytes in the assembled array, just allocate new space */
736 data = g_malloc (nbytes);
737 /* reuse what we can from the already assembled data */
738 if (toreuse) {
739 GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes", toreuse);
740 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
741 "memcpy %" G_GSIZE_FORMAT " bytes", toreuse);
742 memcpy (data, adapter->assembled_data, toreuse);
743 }
744 }
745 if (tocopy) {
746 /* copy the remaining data */
747 copy_into_unchecked (adapter, toreuse + data, toreuse + adapter->skip,
748 tocopy);
749 }
750 return data;
751 }
752
753 /**
754 * gst_adapter_take:
755 * @adapter: a #GstAdapter
756 * @nbytes: (in): the number of bytes to take
757 *
758 * Returns a freshly allocated buffer containing the first @nbytes bytes of the
759 * @adapter. The returned bytes will be flushed from the adapter.
760 *
761 * Caller owns returned value. g_free after usage.
762 *
763 * Free-function: g_free
764 *
765 * Returns: (transfer full) (array length=nbytes) (element-type guint8) (nullable):
766 * oven-fresh hot data, or %NULL if @nbytes bytes are not available
767 */
768 gpointer
769 gst_adapter_take (GstAdapter * adapter, gsize nbytes)
770 {
771 gpointer data;
772
773 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
774 g_return_val_if_fail (nbytes > 0, NULL);
775
776 /* we don't have enough data, return NULL. This is unlikely
777 * as one usually does an _available() first instead of peeking a
778 * random size. */
779 if (G_UNLIKELY (nbytes > adapter->size))
780 return NULL;
781
782 data = gst_adapter_get_internal (adapter, nbytes);
783
784 gst_adapter_flush_unchecked (adapter, nbytes);
785
786 return data;
787 }
788
789 /**
790 * gst_adapter_get_buffer_fast:
791 * @adapter: a #GstAdapter
792 * @nbytes: the number of bytes to get
793 *
794 * Returns a #GstBuffer containing the first @nbytes of the @adapter, but
795 * does not flush them from the adapter. See gst_adapter_take_buffer_fast()
796 * for details.
797 *
798 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
799 * usage.
800 *
801 * Free-function: gst_buffer_unref
802 *
803 * Returns: (transfer full) (nullable): a #GstBuffer containing the first
804 * @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
805 * gst_buffer_unref() when no longer needed.
806 *
807 * Since: 1.6
808 */
809 GstBuffer *
810 gst_adapter_get_buffer_fast (GstAdapter * adapter, gsize nbytes)
811 {
812 GstBuffer *buffer = NULL;
813 GstBuffer *cur;
814 gsize skip;
815 gsize left = nbytes;
816 guint idx, len;
817
818 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
819 g_return_val_if_fail (nbytes > 0, NULL);
820
821 GST_LOG_OBJECT (adapter, "getting buffer of %" G_GSIZE_FORMAT " bytes",
822 nbytes);
823
824 /* we don't have enough data, return NULL. This is unlikely
825 * as one usually does an _available() first instead of grabbing a
826 * random size. */
827 if (G_UNLIKELY (nbytes > adapter->size))
828 return NULL;
829
830 skip = adapter->skip;
831 cur = gst_vec_deque_peek_head (adapter->bufqueue);
832
833 if (skip == 0 && gst_buffer_get_size (cur) == nbytes) {
834 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
835 " as head buffer", nbytes);
836 buffer = gst_buffer_ref (cur);
837 goto done;
838 }
839
840 len = gst_vec_deque_get_length (adapter->bufqueue);
841
842 for (idx = 0; idx < len && left > 0; idx++) {
843 gsize size, cur_size;
844
845 cur = gst_vec_deque_peek_nth (adapter->bufqueue, idx);
846 cur_size = gst_buffer_get_size (cur);
847 size = MIN (cur_size - skip, left);
848
849 GST_LOG_OBJECT (adapter, "appending %" G_GSIZE_FORMAT " bytes"
850 " via region copy", size);
851 if (buffer)
852 gst_buffer_copy_into (buffer, cur,
853 GST_BUFFER_COPY_MEMORY | GST_BUFFER_COPY_META, skip, size);
854 else
855 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, size);
856 skip = 0;
857 left -= size;
858 }
859
860 done:
861
862 return buffer;
863 }
864
865 /**
866 * gst_adapter_take_buffer_fast:
867 * @adapter: a #GstAdapter
868 * @nbytes: the number of bytes to take
869 *
870 * Returns a #GstBuffer containing the first @nbytes of the @adapter.
871 * The returned bytes will be flushed from the adapter. This function
872 * is potentially more performant than gst_adapter_take_buffer() since
873 * it can reuse the memory in pushed buffers by subbuffering or
874 * merging. Unlike gst_adapter_take_buffer(), the returned buffer may
875 * be composed of multiple non-contiguous #GstMemory objects, no
876 * copies are made.
877 *
878 * Note that no assumptions should be made as to whether certain buffer
879 * flags such as the DISCONT flag are set on the returned buffer, or not.
880 * The caller needs to explicitly set or unset flags that should be set or
881 * unset.
882 *
883 * This will also copy over all GstMeta of the input buffers except
884 * for meta with the %GST_META_FLAG_POOLED flag or with the "memory" tag.
885 *
886 * This function can return buffer up to the return value of
887 * gst_adapter_available() without making copies if possible.
888 *
889 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
890 * usage.
891 *
892 * Free-function: gst_buffer_unref
893 *
894 * Returns: (transfer full) (nullable): a #GstBuffer containing the first
895 * @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
896 * gst_buffer_unref() when no longer needed.
897 *
898 * Since: 1.2
899 */
900 GstBuffer *
901 gst_adapter_take_buffer_fast (GstAdapter * adapter, gsize nbytes)
902 {
903 GstBuffer *buffer;
904
905 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
906 g_return_val_if_fail (nbytes > 0, NULL);
907
908 buffer = gst_adapter_get_buffer_fast (adapter, nbytes);
909 if (buffer)
910 gst_adapter_flush_unchecked (adapter, nbytes);
911
912 return buffer;
913 }
914
915 static gboolean
916 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
917 {
918 GstBuffer *outbuf = user_data;
919 const GstMetaInfo *info = (*meta)->info;
920 gboolean do_copy = FALSE;
921
922 if (gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)
923 || gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory_reference)) {
924 /* never call the transform_meta with memory specific metadata */
925 GST_DEBUG ("not copying memory specific metadata %s",
926 g_type_name (info->api));
927 do_copy = FALSE;
928 } else {
929 do_copy = TRUE;
930 GST_DEBUG ("copying metadata %s", g_type_name (info->api));
931 }
932
933 if (do_copy && info->transform_func) {
934 GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
935 GST_DEBUG ("copy metadata %s", g_type_name (info->api));
936 /* simply copy then */
937 info->transform_func (outbuf, *meta, inbuf,
938 _gst_meta_transform_copy, &copy_data);
939 }
940 return TRUE;
941 }
942
943 /**
944 * gst_adapter_get_buffer:
945 * @adapter: a #GstAdapter
946 * @nbytes: the number of bytes to get
947 *
948 * Returns a #GstBuffer containing the first @nbytes of the @adapter, but
949 * does not flush them from the adapter. See gst_adapter_take_buffer()
950 * for details.
951 *
952 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
953 * usage.
954 *
955 * Free-function: gst_buffer_unref
956 *
957 * Returns: (transfer full) (nullable): a #GstBuffer containing the first
958 * @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
959 * gst_buffer_unref() when no longer needed.
960 *
961 * Since: 1.6
962 */
963 GstBuffer *
964 gst_adapter_get_buffer (GstAdapter * adapter, gsize nbytes)
965 {
966 GstBuffer *buffer;
967 GstBuffer *cur;
968 gsize hsize, skip;
969 guint8 *data;
970
971 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
972 g_return_val_if_fail (nbytes > 0, NULL);
973
974 GST_LOG_OBJECT (adapter, "getting buffer of %" G_GSIZE_FORMAT " bytes",
975 nbytes);
976
977 /* we don't have enough data, return NULL. This is unlikely
978 * as one usually does an _available() first instead of grabbing a
979 * random size. */
980 if (G_UNLIKELY (nbytes > adapter->size))
981 return NULL;
982
983 cur = gst_vec_deque_peek_head (adapter->bufqueue);
984 skip = adapter->skip;
985 hsize = gst_buffer_get_size (cur);
986
987 /* our head buffer has enough data left, return it */
988 if (skip == 0 && hsize == nbytes) {
989 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
990 " as head buffer", nbytes);
991 buffer = gst_buffer_ref (cur);
992 goto done;
993 } else if (hsize >= nbytes + skip) {
994 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
995 " via region copy", nbytes);
996 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
997 goto done;
998 }
999 #if 0
1000 if (gst_adapter_try_to_merge_up (adapter, nbytes)) {
1001 /* Merged something, let's try again for sub-buffering */
1002 cur = adapter->buflist->data;
1003 skip = adapter->skip;
1004 if (gst_buffer_get_size (cur) >= nbytes + skip) {
1005 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
1006 " via sub-buffer", nbytes);
1007 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
1008 goto done;
1009 }
1010 }
1011 #endif
1012
1013 data = gst_adapter_get_internal (adapter, nbytes);
1014
1015 buffer = gst_buffer_new_wrapped (data, nbytes);
1016
1017 {
1018 guint idx, len;
1019 GstBuffer *cur;
1020 gsize read_offset = 0;
1021
1022 idx = 0;
1023 len = gst_vec_deque_get_length (adapter->bufqueue);
1024
1025 while (idx < len && read_offset < nbytes + adapter->skip) {
1026 cur = gst_vec_deque_peek_nth (adapter->bufqueue, idx);
1027
1028 gst_buffer_foreach_meta (cur, foreach_metadata, buffer);
1029 read_offset += gst_buffer_get_size (cur);
1030
1031 idx++;
1032 }
1033 }
1034
1035 done:
1036
1037 return buffer;
1038 }
1039
1040 /**
1041 * gst_adapter_take_buffer:
1042 * @adapter: a #GstAdapter
1043 * @nbytes: the number of bytes to take
1044 *
1045 * Returns a #GstBuffer containing the first @nbytes bytes of the
1046 * @adapter. The returned bytes will be flushed from the adapter.
1047 * This function is potentially more performant than
1048 * gst_adapter_take() since it can reuse the memory in pushed buffers
1049 * by subbuffering or merging. This function will always return a
1050 * buffer with a single memory region.
1051 *
1052 * Note that no assumptions should be made as to whether certain buffer
1053 * flags such as the DISCONT flag are set on the returned buffer, or not.
1054 * The caller needs to explicitly set or unset flags that should be set or
1055 * unset.
1056 *
1057 * Likewise, no assumptions should be made about timestamps and offset of the
1058 * returned buffer. The caller should use gst_adapter_prev_pts(),
1059 * gst_adapter_prev_dts(), and gst_adapter_prev_offset() to obtain the relevant
1060 * information.
1061 *
1062 * The returned buffer might not be writable, use gst_buffer_make_writable()
1063 * if you need to change e.g. flags or timestamps.
1064 *
1065 * Since 1.6 this will also copy over all GstMeta of the input buffers except
1066 * for meta with the %GST_META_FLAG_POOLED flag or with the "memory" tag.
1067 *
1068 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
1069 * usage.
1070 *
1071 * Free-function: gst_buffer_unref
1072 *
1073 * Returns: (transfer full) (nullable): a #GstBuffer containing the first
1074 * @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
1075 * gst_buffer_unref() when no longer needed.
1076 */
1077 GstBuffer *
1078 gst_adapter_take_buffer (GstAdapter * adapter, gsize nbytes)
1079 {
1080 GstBuffer *buffer;
1081
1082 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1083 g_return_val_if_fail (nbytes > 0, NULL);
1084
1085 buffer = gst_adapter_get_buffer (adapter, nbytes);
1086 if (buffer)
1087 gst_adapter_flush_unchecked (adapter, nbytes);
1088
1089 return buffer;
1090 }
1091
1092 /**
1093 * gst_adapter_take_list:
1094 * @adapter: a #GstAdapter
1095 * @nbytes: the number of bytes to take
1096 *
1097 * Returns a #GList of buffers containing the first @nbytes bytes of the
1098 * @adapter. The returned bytes will be flushed from the adapter.
1099 * When the caller can deal with individual buffers, this function is more
1100 * performant because no memory should be copied.
1101 *
1102 * Caller owns returned list and contained buffers. gst_buffer_unref() each
1103 * buffer in the list before freeing the list after usage.
1104 *
1105 * Returns: (element-type Gst.Buffer) (transfer full) (nullable): a #GList of
1106 * buffers containing the first @nbytes of the adapter, or %NULL if @nbytes
1107 * bytes are not available
1108 */
1109 GList *
1110 gst_adapter_take_list (GstAdapter * adapter, gsize nbytes)
1111 {
1112 GQueue queue = G_QUEUE_INIT;
1113 GstBuffer *cur;
1114 gsize hsize, skip, cur_size;
1115
1116 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1117 g_return_val_if_fail (nbytes <= adapter->size, NULL);
1118
1119 GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes);
1120
1121 while (nbytes > 0) {
1122 cur = gst_vec_deque_peek_head (adapter->bufqueue);
1123 skip = adapter->skip;
1124 cur_size = gst_buffer_get_size (cur);
1125 hsize = MIN (nbytes, cur_size - skip);
1126
1127 cur = gst_adapter_take_buffer (adapter, hsize);
1128
1129 g_queue_push_tail (&queue, cur);
1130
1131 nbytes -= hsize;
1132 }
1133 return queue.head;
1134 }
1135
1136 /**
1137 * gst_adapter_get_list:
1138 * @adapter: a #GstAdapter
1139 * @nbytes: the number of bytes to get
1140 *
1141 * Returns a #GList of buffers containing the first @nbytes bytes of the
1142 * @adapter, but does not flush them from the adapter. See
1143 * gst_adapter_take_list() for details.
1144 *
1145 * Caller owns returned list and contained buffers. gst_buffer_unref() each
1146 * buffer in the list before freeing the list after usage.
1147 *
1148 * Returns: (element-type Gst.Buffer) (transfer full) (nullable): a #GList of
1149 * buffers containing the first @nbytes of the adapter, or %NULL if @nbytes
1150 * bytes are not available
1151 *
1152 * Since: 1.6
1153 */
1154 GList *
1155 gst_adapter_get_list (GstAdapter * adapter, gsize nbytes)
1156 {
1157 GQueue queue = G_QUEUE_INIT;
1158 GstBuffer *cur, *buffer;
1159 gsize hsize, skip, cur_size;
1160 guint idx;
1161
1162 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1163 g_return_val_if_fail (nbytes <= adapter->size, NULL);
1164
1165 GST_LOG_OBJECT (adapter, "getting %" G_GSIZE_FORMAT " bytes", nbytes);
1166
1167 idx = 0;
1168 skip = adapter->skip;
1169
1170 while (nbytes > 0) {
1171 cur = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
1172 cur_size = gst_buffer_get_size (cur);
1173 hsize = MIN (nbytes, cur_size - skip);
1174
1175 if (skip == 0 && cur_size == hsize) {
1176 GST_LOG_OBJECT (adapter,
1177 "inserting a buffer of %" G_GSIZE_FORMAT " bytes", hsize);
1178 buffer = gst_buffer_ref (cur);
1179 } else {
1180 GST_LOG_OBJECT (adapter, "inserting a buffer of %" G_GSIZE_FORMAT " bytes"
1181 " via region copy", hsize);
1182 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, hsize);
1183 }
1184
1185 g_queue_push_tail (&queue, buffer);
1186
1187 nbytes -= hsize;
1188 skip = 0;
1189 }
1190
1191 return queue.head;
1192 }
1193
1194 /**
1195 * gst_adapter_take_buffer_list:
1196 * @adapter: a #GstAdapter
1197 * @nbytes: the number of bytes to take
1198 *
1199 * Returns a #GstBufferList of buffers containing the first @nbytes bytes of
1200 * the @adapter. The returned bytes will be flushed from the adapter.
1201 * When the caller can deal with individual buffers, this function is more
1202 * performant because no memory should be copied.
1203 *
1204 * Caller owns the returned list. Call gst_buffer_list_unref() to free
1205 * the list after usage.
1206 *
1207 * Returns: (transfer full) (nullable): a #GstBufferList of buffers containing
1208 * the first @nbytes of the adapter, or %NULL if @nbytes bytes are not
1209 * available
1210 *
1211 * Since: 1.6
1212 */
1213 GstBufferList *
1214 gst_adapter_take_buffer_list (GstAdapter * adapter, gsize nbytes)
1215 {
1216 GstBufferList *buffer_list;
1217 GstBuffer *cur;
1218 gsize hsize, skip, cur_size;
1219 guint n_bufs;
1220
1221 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1222
1223 if (nbytes > adapter->size)
1224 return NULL;
1225
1226 GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes);
1227
1228 /* try to create buffer list with sufficient size, so no resize is done later */
1229 if (adapter->count < 64)
1230 n_bufs = adapter->count;
1231 else
1232 n_bufs = (adapter->count * nbytes * 1.2 / adapter->size) + 1;
1233
1234 buffer_list = gst_buffer_list_new_sized (n_bufs);
1235
1236 while (nbytes > 0) {
1237 cur = gst_vec_deque_peek_head (adapter->bufqueue);
1238 skip = adapter->skip;
1239 cur_size = gst_buffer_get_size (cur);
1240 hsize = MIN (nbytes, cur_size - skip);
1241
1242 gst_buffer_list_add (buffer_list, gst_adapter_take_buffer (adapter, hsize));
1243 nbytes -= hsize;
1244 }
1245 return buffer_list;
1246 }
1247
1248 /**
1249 * gst_adapter_get_buffer_list:
1250 * @adapter: a #GstAdapter
1251 * @nbytes: the number of bytes to get
1252 *
1253 * Returns a #GstBufferList of buffers containing the first @nbytes bytes of
1254 * the @adapter but does not flush them from the adapter. See
1255 * gst_adapter_take_buffer_list() for details.
1256 *
1257 * Caller owns the returned list. Call gst_buffer_list_unref() to free
1258 * the list after usage.
1259 *
1260 * Returns: (transfer full) (nullable): a #GstBufferList of buffers containing
1261 * the first @nbytes of the adapter, or %NULL if @nbytes bytes are not
1262 * available
1263 *
1264 * Since: 1.6
1265 */
1266 GstBufferList *
1267 gst_adapter_get_buffer_list (GstAdapter * adapter, gsize nbytes)
1268 {
1269 GstBufferList *buffer_list;
1270 GstBuffer *cur, *buffer;
1271 gsize hsize, skip, cur_size;
1272 guint n_bufs;
1273 guint idx;
1274
1275 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1276
1277 if (nbytes > adapter->size)
1278 return NULL;
1279
1280 GST_LOG_OBJECT (adapter, "getting %" G_GSIZE_FORMAT " bytes", nbytes);
1281
1282 /* try to create buffer list with sufficient size, so no resize is done later */
1283 if (adapter->count < 64)
1284 n_bufs = adapter->count;
1285 else
1286 n_bufs = (adapter->count * nbytes * 1.2 / adapter->size) + 1;
1287
1288 buffer_list = gst_buffer_list_new_sized (n_bufs);
1289
1290 idx = 0;
1291 skip = adapter->skip;
1292
1293 while (nbytes > 0) {
1294 cur = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
1295 cur_size = gst_buffer_get_size (cur);
1296 hsize = MIN (nbytes, cur_size - skip);
1297
1298 if (skip == 0 && cur_size == hsize) {
1299 GST_LOG_OBJECT (adapter,
1300 "inserting a buffer of %" G_GSIZE_FORMAT " bytes", hsize);
1301 buffer = gst_buffer_ref (cur);
1302 } else {
1303 GST_LOG_OBJECT (adapter, "inserting a buffer of %" G_GSIZE_FORMAT " bytes"
1304 " via region copy", hsize);
1305 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, hsize);
1306 }
1307
1308 gst_buffer_list_add (buffer_list, buffer);
1309
1310 nbytes -= hsize;
1311 skip = 0;
1312 }
1313
1314 return buffer_list;
1315 }
1316
1317 /**
1318 * gst_adapter_available:
1319 * @adapter: a #GstAdapter
1320 *
1321 * Gets the maximum amount of bytes available, that is it returns the maximum
1322 * value that can be supplied to gst_adapter_map() without that function
1323 * returning %NULL.
1324 *
1325 * Calling gst_adapter_map() with the amount of bytes returned by this function
1326 * may require expensive operations (like copying the data into a temporary
1327 * buffer) in some cases.
1328 *
1329 * Returns: number of bytes available in @adapter
1330 */
1331 gsize
1332 gst_adapter_available (GstAdapter * adapter)
1333 {
1334 g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
1335
1336 return adapter->size;
1337 }
1338
1339 /**
1340 * gst_adapter_available_fast:
1341 * @adapter: a #GstAdapter
1342 *
1343 * Gets the maximum number of bytes that can be retrieved in a single map
1344 * operation without merging buffers.
1345 *
1346 * Calling gst_adapter_map() with the amount of bytes returned by this function
1347 * will never require any expensive operations (like copying the data into a
1348 * temporary buffer).
1349 *
1350 * Returns: number of bytes that are available in @adapter without expensive
1351 * operations
1352 */
1353 gsize
1354 gst_adapter_available_fast (GstAdapter * adapter)
1355 {
1356 GstBuffer *cur;
1357 gsize size;
1358 guint idx;
1359
1360 g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
1361
1362 /* no data */
1363 if (adapter->size == 0)
1364 return 0;
1365
1366 /* some stuff we already assembled */
1367 if (adapter->assembled_len)
1368 return adapter->assembled_len;
1369
1370 /* take the first non-zero buffer */
1371 idx = 0;
1372 while (TRUE) {
1373 cur = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
1374 size = gst_buffer_get_size (cur);
1375 if (size != 0)
1376 break;
1377 }
1378
1379 /* we can quickly get the (remaining) data of the first buffer */
1380 return size - adapter->skip;
1381 }
1382
1383 /**
1384 * gst_adapter_distance_from_discont:
1385 * @adapter: a #GstAdapter
1386 *
1387 * Get the distance in bytes since the last buffer with the
1388 * %GST_BUFFER_FLAG_DISCONT flag.
1389 *
1390 * The distance will be reset to 0 for all buffers with
1391 * %GST_BUFFER_FLAG_DISCONT on them, and then calculated for all other
1392 * following buffers based on their size.
1393 *
1394 * Since: 1.10
1395 *
1396 * Returns: The offset. Can be %GST_BUFFER_OFFSET_NONE.
1397 */
1398 guint64
1399 gst_adapter_distance_from_discont (GstAdapter * adapter)
1400 {
1401 return adapter->distance_from_discont;
1402 }
1403
1404 /**
1405 * gst_adapter_offset_at_discont:
1406 * @adapter: a #GstAdapter
1407 *
1408 * Get the offset that was on the last buffer with the GST_BUFFER_FLAG_DISCONT
1409 * flag, or GST_BUFFER_OFFSET_NONE.
1410 *
1411 * Since: 1.10
1412 *
1413 * Returns: The offset at the last discont or GST_BUFFER_OFFSET_NONE.
1414 */
1415 guint64
1416 gst_adapter_offset_at_discont (GstAdapter * adapter)
1417 {
1418 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_BUFFER_OFFSET_NONE);
1419
1420 return adapter->offset_at_discont;
1421 }
1422
1423 /**
1424 * gst_adapter_pts_at_discont:
1425 * @adapter: a #GstAdapter
1426 *
1427 * Get the PTS that was on the last buffer with the GST_BUFFER_FLAG_DISCONT
1428 * flag, or GST_CLOCK_TIME_NONE.
1429 *
1430 * Since: 1.10
1431 *
1432 * Returns: The PTS at the last discont or GST_CLOCK_TIME_NONE.
1433 */
1434 GstClockTime
1435 gst_adapter_pts_at_discont (GstAdapter * adapter)
1436 {
1437 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1438
1439 return adapter->pts_at_discont;
1440 }
1441
1442 /**
1443 * gst_adapter_dts_at_discont:
1444 * @adapter: a #GstAdapter
1445 *
1446 * Get the DTS that was on the last buffer with the GST_BUFFER_FLAG_DISCONT
1447 * flag, or GST_CLOCK_TIME_NONE.
1448 *
1449 * Since: 1.10
1450 *
1451 * Returns: The DTS at the last discont or GST_CLOCK_TIME_NONE.
1452 */
1453 GstClockTime
1454 gst_adapter_dts_at_discont (GstAdapter * adapter)
1455 {
1456 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1457
1458 return adapter->dts_at_discont;
1459 }
1460
1461 /**
1462 * gst_adapter_prev_offset:
1463 * @adapter: a #GstAdapter
1464 * @distance: (out) (allow-none): pointer to a location for distance, or %NULL
1465 *
1466 * Get the offset that was before the current byte in the adapter. When
1467 * @distance is given, the amount of bytes between the offset and the current
1468 * position is returned.
1469 *
1470 * The offset is reset to GST_BUFFER_OFFSET_NONE and the distance is set to 0
1471 * when the adapter is first created or when it is cleared. This also means that
1472 * before the first byte with an offset is added to the adapter, the offset
1473 * and distance returned are GST_BUFFER_OFFSET_NONE and 0 respectively.
1474 *
1475 * Since: 1.10
1476 *
1477 * Returns: The previous seen offset.
1478 */
1479 guint64
1480 gst_adapter_prev_offset (GstAdapter * adapter, guint64 * distance)
1481 {
1482 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_BUFFER_OFFSET_NONE);
1483
1484 if (distance)
1485 *distance = adapter->offset_distance;
1486
1487 return adapter->offset;
1488 }
1489
1490 /**
1491 * gst_adapter_prev_pts:
1492 * @adapter: a #GstAdapter
1493 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1494 *
1495 * Get the pts that was before the current byte in the adapter. When
1496 * @distance is given, the amount of bytes between the pts and the current
1497 * position is returned.
1498 *
1499 * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1500 * the adapter is first created or when it is cleared. This also means that before
1501 * the first byte with a pts is added to the adapter, the pts
1502 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1503 *
1504 * Returns: The previously seen pts.
1505 */
1506 GstClockTime
1507 gst_adapter_prev_pts (GstAdapter * adapter, guint64 * distance)
1508 {
1509 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1510
1511 if (distance)
1512 *distance = adapter->pts_distance;
1513
1514 return adapter->pts;
1515 }
1516
1517 /**
1518 * gst_adapter_prev_dts:
1519 * @adapter: a #GstAdapter
1520 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1521 *
1522 * Get the dts that was before the current byte in the adapter. When
1523 * @distance is given, the amount of bytes between the dts and the current
1524 * position is returned.
1525 *
1526 * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1527 * the adapter is first created or when it is cleared. This also means that before
1528 * the first byte with a dts is added to the adapter, the dts
1529 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1530 *
1531 * Returns: The previously seen dts.
1532 */
1533 GstClockTime
1534 gst_adapter_prev_dts (GstAdapter * adapter, guint64 * distance)
1535 {
1536 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1537
1538 if (distance)
1539 *distance = adapter->dts_distance;
1540
1541 return adapter->dts;
1542 }
1543
1544 /**
1545 * gst_adapter_prev_pts_at_offset:
1546 * @adapter: a #GstAdapter
1547 * @offset: the offset in the adapter at which to get timestamp
1548 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1549 *
1550 * Get the pts that was before the byte at offset @offset in the adapter. When
1551 * @distance is given, the amount of bytes between the pts and the current
1552 * position is returned.
1553 *
1554 * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1555 * the adapter is first created or when it is cleared. This also means that before
1556 * the first byte with a pts is added to the adapter, the pts
1557 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1558 *
1559 * Since: 1.2
1560 * Returns: The previously seen pts at given offset.
1561 */
1562 GstClockTime
1563 gst_adapter_prev_pts_at_offset (GstAdapter * adapter, gsize offset,
1564 guint64 * distance)
1565 {
1566 GstBuffer *cur;
1567 gsize read_offset = 0;
1568 gsize pts_offset = 0;
1569 GstClockTime pts = adapter->pts;
1570 guint idx, len;
1571
1572 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1573
1574 idx = 0;
1575 len = gst_vec_deque_get_length (adapter->bufqueue);
1576
1577 while (idx < len && read_offset < offset + adapter->skip) {
1578 cur = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
1579
1580 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (cur))) {
1581 pts = GST_BUFFER_PTS (cur);
1582 pts_offset = read_offset;
1583 }
1584
1585 read_offset += gst_buffer_get_size (cur);
1586 }
1587
1588 if (distance)
1589 *distance = adapter->pts_distance + offset - pts_offset;
1590
1591 return pts;
1592 }
1593
1594 /**
1595 * gst_adapter_prev_dts_at_offset:
1596 * @adapter: a #GstAdapter
1597 * @offset: the offset in the adapter at which to get timestamp
1598 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1599 *
1600 * Get the dts that was before the byte at offset @offset in the adapter. When
1601 * @distance is given, the amount of bytes between the dts and the current
1602 * position is returned.
1603 *
1604 * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1605 * the adapter is first created or when it is cleared. This also means that before
1606 * the first byte with a dts is added to the adapter, the dts
1607 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1608 *
1609 * Since: 1.2
1610 * Returns: The previously seen dts at given offset.
1611 */
1612 GstClockTime
1613 gst_adapter_prev_dts_at_offset (GstAdapter * adapter, gsize offset,
1614 guint64 * distance)
1615 {
1616 GstBuffer *cur;
1617 gsize read_offset = 0;
1618 gsize dts_offset = 0;
1619 GstClockTime dts = adapter->dts;
1620 guint idx, len;
1621
1622 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1623
1624 idx = 0;
1625 len = gst_vec_deque_get_length (adapter->bufqueue);
1626
1627 while (idx < len && read_offset < offset + adapter->skip) {
1628 cur = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
1629
1630 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cur))) {
1631 dts = GST_BUFFER_DTS (cur);
1632 dts_offset = read_offset;
1633 }
1634
1635 read_offset += gst_buffer_get_size (cur);
1636 }
1637
1638 if (distance)
1639 *distance = adapter->dts_distance + offset - dts_offset;
1640
1641 return dts;
1642 }
1643
1644 /**
1645 * gst_adapter_masked_scan_uint32_peek:
1646 * @adapter: a #GstAdapter
1647 * @mask: mask to apply to data before matching against @pattern
1648 * @pattern: pattern to match (after mask is applied)
1649 * @offset: offset into the adapter data from which to start scanning, returns
1650 * the last scanned position.
1651 * @size: number of bytes to scan from offset
1652 * @value: (out) (allow-none): pointer to uint32 to return matching data
1653 *
1654 * Scan for pattern @pattern with applied mask @mask in the adapter data,
1655 * starting from offset @offset. If a match is found, the value that matched
1656 * is returned through @value, otherwise @value is left untouched.
1657 *
1658 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1659 * of endianness. All four bytes of the pattern must be present in the
1660 * adapter for it to match, even if the first or last bytes are masked out.
1661 *
1662 * It is an error to call this function without making sure that there is
1663 * enough data (offset+size bytes) in the adapter.
1664 *
1665 * Returns: offset of the first match, or -1 if no match was found.
1666 */
1667 gssize
1668 gst_adapter_masked_scan_uint32_peek (GstAdapter * adapter, guint32 mask,
1669 guint32 pattern, gsize offset, gsize size, guint32 * value)
1670 {
1671 gsize skip, bsize, i;
1672 guint32 state;
1673 GstMapInfo info;
1674 guint8 *bdata;
1675 GstBuffer *buf;
1676 guint idx;
1677
1678 g_return_val_if_fail (size > 0, -1);
1679 g_return_val_if_fail (offset + size <= adapter->size, -1);
1680 g_return_val_if_fail (((~mask) & pattern) == 0, -1);
1681
1682 /* we can't find the pattern with less than 4 bytes */
1683 if (G_UNLIKELY (size < 4))
1684 return -1;
1685
1686 skip = offset + adapter->skip;
1687
1688 /* first step, do skipping and position on the first buffer */
1689 /* optimistically assume scanning continues sequentially */
1690 if (adapter->scan_entry_idx != G_MAXUINT && (adapter->scan_offset <= skip)) {
1691 idx = adapter->scan_entry_idx;
1692 skip -= adapter->scan_offset;
1693 } else {
1694 idx = 0;
1695 adapter->scan_offset = 0;
1696 adapter->scan_entry_idx = G_MAXUINT;
1697 }
1698 buf = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
1699 bsize = gst_buffer_get_size (buf);
1700 while (G_UNLIKELY (skip >= bsize)) {
1701 skip -= bsize;
1702 adapter->scan_offset += bsize;
1703 adapter->scan_entry_idx = idx;
1704 buf = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
1705 bsize = gst_buffer_get_size (buf);
1706 }
1707 /* get the data now */
1708 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1709 return -1;
1710
1711 bdata = (guint8 *) info.data + skip;
1712 bsize = info.size - skip;
1713 skip = 0;
1714
1715 /* set the state to something that does not match */
1716 state = ~pattern;
1717
1718 /* now find data */
1719 do {
1720 bsize = MIN (bsize, size);
1721 for (i = 0; i < bsize; i++) {
1722 state = ((state << 8) | bdata[i]);
1723 if (G_UNLIKELY ((state & mask) == pattern)) {
1724 /* we have a match but we need to have skipped at
1725 * least 4 bytes to fill the state. */
1726 if (G_LIKELY (skip + i >= 3)) {
1727 if (G_LIKELY (value))
1728 *value = state;
1729 gst_buffer_unmap (buf, &info);
1730 return offset + skip + i - 3;
1731 }
1732 }
1733 }
1734 size -= bsize;
1735 if (size == 0)
1736 break;
1737
1738 /* nothing found yet, go to next buffer */
1739 skip += bsize;
1740 adapter->scan_offset += info.size;
1741 adapter->scan_entry_idx = idx;
1742 gst_buffer_unmap (buf, &info);
1743 buf = gst_vec_deque_peek_nth (adapter->bufqueue, idx++);
1744
1745 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1746 return -1;
1747
1748 bsize = info.size;
1749 bdata = info.data;
1750 } while (TRUE);
1751
1752 gst_buffer_unmap (buf, &info);
1753
1754 /* nothing found */
1755 return -1;
1756 }
1757
1758 /**
1759 * gst_adapter_masked_scan_uint32:
1760 * @adapter: a #GstAdapter
1761 * @mask: mask to apply to data before matching against @pattern
1762 * @pattern: pattern to match (after mask is applied)
1763 * @offset: offset into the adapter data from which to start scanning, returns
1764 * the last scanned position.
1765 * @size: number of bytes to scan from offset
1766 *
1767 * Scan for pattern @pattern with applied mask @mask in the adapter data,
1768 * starting from offset @offset.
1769 *
1770 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1771 * of endianness. All four bytes of the pattern must be present in the
1772 * adapter for it to match, even if the first or last bytes are masked out.
1773 *
1774 * It is an error to call this function without making sure that there is
1775 * enough data (offset+size bytes) in the adapter.
1776 *
1777 * This function calls gst_adapter_masked_scan_uint32_peek() passing %NULL
1778 * for value.
1779 *
1780 * Returns: offset of the first match, or -1 if no match was found.
1781 *
1782 * Example:
1783 * |[
1784 * // Assume the adapter contains 0x00 0x01 0x02 ... 0xfe 0xff
1785 *
1786 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 0, 256);
1787 * // -> returns 0
1788 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 1, 255);
1789 * // -> returns -1
1790 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x01020304, 1, 255);
1791 * // -> returns 1
1792 * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0001, 0, 256);
1793 * // -> returns -1
1794 * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0203, 0, 256);
1795 * // -> returns 0
1796 * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 256);
1797 * // -> returns 2
1798 * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 4);
1799 * // -> returns -1
1800 * ]|
1801 */
1802 gssize
1803 gst_adapter_masked_scan_uint32 (GstAdapter * adapter, guint32 mask,
1804 guint32 pattern, gsize offset, gsize size)
1805 {
1806 return gst_adapter_masked_scan_uint32_peek (adapter, mask, pattern, offset,
1807 size, NULL);
1808 }