VLC 4.0.0-dev
Loading...
Searching...
No Matches
vlc_frame.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_frame.h: frame management functions
3 *****************************************************************************
4 * Copyright (C) 2003 VLC authors and VideoLAN
5 *
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
22
23#ifndef VLC_FRAME_H
24#define VLC_FRAME_H 1
25
26#include <vlc_tick.h>
27
28struct vlc_ancillary;
29typedef uint32_t vlc_ancillary_id;
31/**
32 * \defgroup frame Frames
33 * \ingroup input
34 *
35 * Frames of binary data.
36 *
37 * @ref vlc_frame_t is a generic structure to represent a binary blob within VLC.
38 * The primary goal of the structure is to avoid memory copying as data is
39 * passed around. It is notably used between the \ref demux, the packetizer
40 * (if present) and the \ref decoder, and for audio, between the \ref decoder,
41 * the audio filters, and the \ref audio_output.
42 *
43 * @{
44 * \file
45 * Frames definition and functions
46 */
47
48#include <sys/types.h> /* for ssize_t */
49
50/****************************************************************************
51 * frame:
52 ****************************************************************************
53 * - i_flags may not always be set (ie could be 0, even for a key frame
54 * it depends where you receive the buffer (before/after a packetizer
55 * and the demux/packetizer implementations.
56 * - i_dts/i_pts could be VLC_TICK_INVALID, it means no pts/dts
57 * - i_length: length in microsecond of the packet, can be null except in the
58 * sout where it is mandatory.
59 *
60 * - i_buffer number of valid data pointed by p_buffer
61 * you can freely decrease it but never increase it yourself
62 * (use vlc_frame_Realloc)
63 * - p_buffer: pointer over data. You should never overwrite it, you can
64 * only increment it to skip data, in others cases use vlc_frame_Realloc
65 * (don't duplicate yourself in a bigger buffer, vlc_frame_Realloc is
66 * optimised for preheader/postdata increase)
67 ****************************************************************************/
68
69typedef struct vlc_frame_t vlc_frame_t;
71/** The content doesn't follow the last frame, possible some frames in between
72 * have been lost */
73#define VLC_FRAME_FLAG_DISCONTINUITY 0x0001
74/** Intra frame */
75#define VLC_FRAME_FLAG_TYPE_I 0x0002
76/** Inter frame with backward reference only */
77#define VLC_FRAME_FLAG_TYPE_P 0x0004
78/** Inter frame with backward and forward reference */
79#define VLC_FRAME_FLAG_TYPE_B 0x0008
80/** For inter frame when you don't know the real type */
81#define VLC_FRAME_FLAG_TYPE_PB 0x0010
82/** Warn that this frame is a header one */
83#define VLC_FRAME_FLAG_HEADER 0x0020
84/** This frame contains the last part of a sequence */
85#define VLC_FRAME_FLAG_END_OF_SEQUENCE 0x0040
86/** This frame is scrambled */
87#define VLC_FRAME_FLAG_SCRAMBLED 0x0100
88/** This frame has to be decoded but not be displayed */
89#define VLC_FRAME_FLAG_PREROLL 0x0200
90/** This frame is corrupted and/or there is data loss */
91#define VLC_FRAME_FLAG_CORRUPTED 0x0400
92/** This frame is last of its access unit */
93#define VLC_FRAME_FLAG_AU_END 0x0800
94/** This frame contains an interlaced picture with top field stored first */
95#define VLC_FRAME_FLAG_TOP_FIELD_FIRST 0x1000
96/** This frame contains an interlaced picture with bottom field stored first */
97#define VLC_FRAME_FLAG_BOTTOM_FIELD_FIRST 0x2000
98/** This frame contains a single field from interlaced picture. */
99#define VLC_FRAME_FLAG_SINGLE_FIELD 0x4000
101/** This frame contains an interlaced picture */
102#define VLC_FRAME_FLAG_INTERLACED_MASK \
103 (VLC_FRAME_FLAG_TOP_FIELD_FIRST|VLC_FRAME_FLAG_BOTTOM_FIELD_FIRST|VLC_FRAME_FLAG_SINGLE_FIELD)
104
105#define VLC_FRAME_FLAG_TYPE_MASK \
106 (VLC_FRAME_FLAG_TYPE_I|VLC_FRAME_FLAG_TYPE_P|VLC_FRAME_FLAG_TYPE_B|VLC_FRAME_FLAG_TYPE_PB)
107
108/* These are for input core private usage only */
109#define VLC_FRAME_FLAG_CORE_PRIVATE_MASK 0x00ff0000
110#define VLC_FRAME_FLAG_CORE_PRIVATE_SHIFT 16
112/* These are for module private usage only */
113#define VLC_FRAME_FLAG_PRIVATE_MASK 0xff000000
114#define VLC_FRAME_FLAG_PRIVATE_SHIFT 24
118 void (*free)(vlc_frame_t *);
120
121struct vlc_frame_t
125 uint8_t *p_buffer; /**< Payload start */
126 size_t i_buffer; /**< Payload length */
127 uint8_t *p_start; /**< Buffer start */
128 size_t i_size; /**< Buffer total size */
130 uint32_t i_flags;
131 unsigned i_nb_samples; /* Used for audio */
137 /** Private ancillary struct. Don't use it directly, but use it via
138 * vlc_frame_AttachAncillary() and vlc_frame_GetAncillary(). */
141 const struct vlc_frame_callbacks *cbs;
143
144/**
145 * Initializes a custom frame.
146 *
147 * This function initialize a frame of timed data allocated by custom means.
148 * This allows passing data without copying even if the data has been allocated
149 * with unusual means or outside of LibVLC.
150 *
151 * Normally, frames are allocated and initialized by vlc_frame_Alloc() instead.
152 *
153 * @param frame allocated frame structure to initialize
154 * @param cbs structure of custom callbacks to handle the frame [IN]
155 * @param base start address of the frame data
156 * @param length byte length of the frame data
157 *
158 * @return @c frame (this function cannot fail)
159 */
161 const struct vlc_frame_callbacks *cbs,
162 void *base, size_t length);
163
164/**
165 * Creates a custom frame.
166 *
167 * This function initialize a frame of timed data allocated by custom means.
168 * This allows passing data without copying even if the data has been allocated
169 * with unusual means or outside of LibVLC.
170 *
171 * Normally, frames are allocated and initialized by vlc_frame_Alloc() instead.
172 *
173 * @param cbs structure of custom callbacks to handle the frame [IN]
174 * @param base start address of the frame data
175 * @param length byte length of the frame data
176 *
177 * @return the created frame, or NULL on memory error.
178 */
180 void *base, size_t length);
181
182/**
183 * Allocates a frame.
184 *
185 * Creates a new frame with the requested size.
186 * The frame must be released with vlc_frame_Release().
187 *
188 * @param size size in bytes (possibly zero)
189 * @return the created frame, or NULL on memory error.
190 */
192
193VLC_API vlc_frame_t *vlc_frame_TryRealloc(vlc_frame_t *, ssize_t pre, size_t body) VLC_USED;
194
195/**
196 * Reallocates a frame.
197 *
198 * This function expands, shrinks or moves a data frame.
199 * In many cases, this function can return without any memory allocation by
200 * reusing spare buffer space. Otherwise, a new frame is created and data is
201 * copied.
202 *
203 * @param frame the frame to realloc, which will be freed after the call to
204 * this function
205 * @param pre count of bytes to prepend if positive,
206 * count of leading bytes to discard if negative
207 * @param body new bytes size of the frame
208 *
209 * @return the reallocated frame on success, NULL on error.
210 *
211 * @note Skipping leading bytes can be achieved directly by subtracting from
212 * vlc_frame_t.i_buffer and adding vlc_frame_t.p_buffer.
213 * @note Discard trailing bytes can be achieved directly by subtracting from
214 * vlc_frame_t.i_buffer.
215 * @note On error, the frame is discarded.
216 * To avoid that, use vlc_frame_TryRealloc() instead.
217 */
219vlc_frame_Realloc(vlc_frame_t *frame, ssize_t pre, size_t body) VLC_USED;
220
221/**
222 * Releases a frame.
223 *
224 * This function works for any @ref vlc_frame_t frame, regardless of the way it was
225 * allocated.
226 *
227 * @note
228 * If the frame is in a chain, this function does <b>not</b> release any
229 * subsequent frame in the chain. Use vlc_frame_ChainRelease() for that purpose.
230 *
231 * @param frame frame to release (cannot be NULL)
232 */
234
235/**
236 * Attach an ancillary to the frame
237 *
238 * @warning the ancillary will be released only if the frame is allocated from
239 * a vlc_frame Alloc function (vlc_frame_Alloc(), vlc_frame_mmap_Alloc()...).
240 *
241 * @note Several ancillaries can be attached to a frame, but if two ancillaries
242 * are identified by the same ID, only the last one take precedence.
243 *
244 * @param frame the frame to attach an ancillary
245 * @param ancillary ancillary that will be held by the frame, can't be NULL
246 * @return VLC_SUCCESS in case of success, VLC_ENOMEM in case of alloc error
247 */
248VLC_API int
249vlc_frame_AttachAncillary(vlc_frame_t *frame, struct vlc_ancillary *ancillary);
250
251/**
252 * Return the ancillary identified by an ID
253 *
254 * @param frame the frame to read the ancillary from
255 * @param id id of ancillary to request
256 * @return the ancillary or NULL if the ancillary for that particular id is
257 * not present
258 */
259VLC_API struct vlc_ancillary *
261
262/**
263 * Copy frame properties from src to dst
264 *
265 * Copy i_flags, i_nb_samples, i_dts, i_pts, and i_length.
266 *
267 * @note if src has an ancillary, the ancillary will be copied and refcounted
268 * to dst.
269 *
270 * @param dst the frame to copy properties into
271 * @param src the frame to copy properties from
272 */
275/**
276 * Duplicates a frame.
277 *
278 * Creates a writeable duplicate of a frame.
279 *
280 * @return the duplicate on success, NULL on error.
281 */
283static inline vlc_frame_t *vlc_frame_Duplicate( const vlc_frame_t *frame )
285 vlc_frame_t *p_dup = vlc_frame_Alloc( frame->i_buffer );
286 if( p_dup == NULL )
287 return NULL;
288
289 vlc_frame_CopyProperties( p_dup, frame );
290 memcpy( p_dup->p_buffer, frame->p_buffer, frame->i_buffer );
291
292 return p_dup;
293}
294
295/**
296 * Wraps heap in a frame.
297 *
298 * Creates a @ref vlc_frame_t out of an existing heap allocation.
299 * This is provided by LibVLC so that manually heap-allocated frames can safely
300 * be deallocated even after the origin plugin has been unloaded from memory.
301 *
302 * When vlc_frame_Release() is called, VLC will free() the specified pointer.
303 *
304 * @param addr base address of the heap allocation (will be free()'d)
305 * @param length bytes length of the heap allocation
306 * @return NULL in case of error (ptr free()'d in that case), or a valid
307 * vlc_frame_t pointer.
308 */
310
311/**
312 * Wraps a memory mapping in a frame
313 *
314 * Creates a @ref vlc_frame_t from a virtual address memory mapping (mmap).
315 * This is provided by LibVLC so that mmap frames can safely be deallocated
316 * even after the allocating plugin has been unloaded from memory.
317 *
318 * @param addr base address of the mapping (as returned by mmap)
319 * @param length length (bytes) of the mapping (as passed to mmap)
320 * @return NULL if addr is MAP_FAILED, or an error occurred (in the later
321 * case, munmap(addr, length) is invoked before returning).
322 */
324
325/**
326 * Wraps a System V memory segment in a frame
327 *
328 * Creates a @ref vlc_frame_t from a System V shared memory segment (shmget()).
329 * This is provided by LibVLC so that segments can safely be deallocated
330 * even after the allocating plugin has been unloaded from memory.
331 *
332 * @param addr base address of the segment (as returned by shmat())
333 * @param length length (bytes) of the segment (as passed to shmget())
334 * @return NULL if an error occurred (in that case, shmdt(addr) is invoked
335 * before returning NULL).
336 */
337VLC_API vlc_frame_t * vlc_frame_shm_Alloc(void *addr, size_t length) VLC_USED VLC_MALLOC;
338
339/**
340 * Maps a file handle in memory.
341 *
342 * Loads a file into a frame of memory through a file descriptor.
343 * If possible a private file mapping is created. Otherwise, the file is read
344 * normally. This function is a cancellation point.
345 *
346 * @note On 32-bits platforms,
347 * this function will not work for very large files,
348 * due to memory space constraints.
349 *
350 * @param fd file descriptor to load from
351 * @param write If true, request a read/write private mapping.
352 * If false, request a read-only potentially shared mapping.
353 *
354 * @return a new frame with the file content at p_buffer, and file length at
355 * i_buffer (release it with vlc_frame_Release()), or NULL upon error (see errno).
356 */
358
359/**
360 * Maps a file in memory.
361 *
362 * Loads a file into a frame of memory from a path to the file.
363 * See also vlc_frame_File().
364 *
365 * @param path the file path to load the memory block from
366 * @param write If true, request a read/write private mapping.
367 * If false, request a read-only potentially shared mapping.
368 */
370vlc_frame_FilePath(const char *path, bool write)
372
373static inline void vlc_frame_Cleanup (void *frame)
376}
377#define vlc_frame_cleanup_push( frame ) vlc_cleanup_push (vlc_frame_Cleanup, frame)
379/**
380 * \defgroup vlc_frame_chain Frame chain
381 * @{
382 */
383
384/**
385 * Appends a @ref vlc_frame_t to the chain
386 *
387 * The given frame is appended to the last frame of the given chain.
388 *
389 * @attention
390 * Using this function on long chains or repeatedly calling it
391 * to append a lot of frames can be slow, as it has to iterate the
392 * whole chain to append the frame.
393 * In these cases @ref vlc_frame_ChainLastAppend should be used.
394 *
395 * @param pp_list Pointer to the vlc_frame_t chain
396 * @param frame The vlc_frame_t to append (can be NULL)
397 *
398 * @see vlc_frame_ChainLastAppend()
399 *
400 * Example:
401 * @code{.c}
402 * vlc_frame_t *p_chain = NULL;
403 *
404 * vlc_frame_ChainAppend(&p_chain, p_frame);
405 * @endcode
406 */
407static inline void vlc_frame_ChainAppend( vlc_frame_t **pp_list, vlc_frame_t *frame )
409 if( *pp_list == NULL )
410 {
411 *pp_list = frame;
412 }
413 else
414 {
415 vlc_frame_t *p = *pp_list;
416
417 while( p->p_next ) p = p->p_next;
418 p->p_next = frame;
419 }
420}
421
422/**
423 * Appends a @ref vlc_frame_t to the last frame pointer and update it
424 *
425 * Uses a pointer over a pointer to p_next of the last frame of the frame chain
426 * to append a frame at the end of the chain and updates the pointer to the new
427 * last frame's @c p_next. If the appended frame is itself a chain, it is iterated
428 * till the end to correctly update @c ppp_last.
429 *
430 * @param[in,out] ppp_last Pointer to pointer to the end of the chain
431 * (The vlc_frame_t::p_next of the last vlc_frame_t in the chain)
432 * @param frame The vlc_frame_t to append
433 *
434 * Example:
435 * @code{.c}
436 * vlc_frame_t *p_frame = NULL;
437 * vlc_frame_t **pp_frame_last = &p_frame;
438 *
439 * vlc_frame_ChainLastAppend(&pp_frame_last, p_other_frame);
440 * @endcode
441 */
442static inline void vlc_frame_ChainLastAppend( vlc_frame_t ***ppp_last, vlc_frame_t *frame )
444 vlc_frame_t *p_last = frame;
445
446 **ppp_last = frame;
447
448 while( p_last->p_next ) p_last = p_last->p_next;
449 *ppp_last = &p_last->p_next;
450}
451
452/**
453 * Releases a chain of blocks
454 *
455 * The frame pointed to by frame and all following frames in the
456 * chain are released.
457 *
458 * @param frame Pointer to first vlc_frame_t of the chain to release
459 *
460 * @see vlc_frame_Release()
461 */
462static inline void vlc_frame_ChainRelease( vlc_frame_t *frame )
464 while( frame )
465 {
466 vlc_frame_t *p_next = frame->p_next;
467 vlc_frame_Release( frame );
468 frame = p_next;
469 }
470}
471
472/**
473 * Extracts data from a chain of frames
474 *
475 * Copies the specified amount of data from the chain into the given buffer.
476 * If the data in the chain is less than the maximum amount given, the remainder
477 * of the buffer is not modified.
478 *
479 * @param p_list Pointer to the first vlc_frame_t of the chain to copy from
480 * @param[out] p_data Destination buffer to copy the data to
481 * @param i_max Number of bytes to copy
482 * @return Number of bytes actually copied
483 *
484 * @see vlc_frame_ChainGather()
485 */
486static size_t vlc_frame_ChainExtract( vlc_frame_t *p_list, void *p_data, size_t i_max )
488 size_t i_total = 0;
489 uint8_t *p = (uint8_t*)p_data;
490
491 while( p_list && i_max )
492 {
493 size_t i_copy = __MIN( i_max, p_list->i_buffer );
494 memcpy( p, p_list->p_buffer, i_copy );
495 i_max -= i_copy;
496 i_total += i_copy;
497 p += i_copy;
498
499 p_list = p_list->p_next;
500 }
501 return i_total;
502}
503
504/**
505 * Retrieves chain properties
506 *
507 * Can be used to retrieve count of frames, number of bytes and the duration
508 * of the chain.
509 *
510 * @param p_list Pointer to the first vlc_frame_t of the chain
511 * @param[out] pi_count Pointer to count of frames in the chain (may be NULL)
512 * @param[out] pi_size Pointer to number of bytes in the chain (may be NULL)
513 * @param[out] pi_length Pointer to length (duration) of the chain (may be NULL)
514 */
515static inline void vlc_frame_ChainProperties( const vlc_frame_t *p_list, int *pi_count, size_t *pi_size, vlc_tick_t *pi_length )
517 size_t i_size = 0;
518 vlc_tick_t i_length = 0;
519 int i_count = 0;
520
521 while( p_list )
522 {
523 i_size += p_list->i_buffer;
524 i_length += p_list->i_length;
525 i_count++;
526
527 p_list = p_list->p_next;
528 }
529
530 if( pi_size )
531 *pi_size = i_size;
532 if( pi_length )
533 *pi_length = i_length;
534 if( pi_count )
535 *pi_count = i_count;
536}
537
538/**
539 * Gathers a chain into a single vlc_frame_t
540 *
541 * All frames in the chain are gathered into a single vlc_frame_t and the
542 * original chain is released.
543 *
544 * @param p_list Pointer to the first vlc_frame_t of the chain to gather
545 * @return Returns a pointer to a new vlc_frame_t or NULL if the frame can not
546 * be allocated, in which case the original chain is not released.
547 * If the chain pointed to by p_list is already gathered, a pointer
548 * to it is returned and no new frame will be allocated.
549 *
550 * @see vlc_frame_ChainExtract()
551 */
552static inline vlc_frame_t *vlc_frame_ChainGather( vlc_frame_t *p_list )
554 size_t i_total = 0;
555 vlc_tick_t i_length = 0;
556 vlc_frame_t *g;
557
558 if( p_list->p_next == NULL )
559 return p_list; /* Already gathered */
560
561 vlc_frame_ChainProperties( p_list, NULL, &i_total, &i_length );
562
563 g = vlc_frame_Alloc( i_total );
564 if( !g )
565 return NULL;
566 vlc_frame_ChainExtract( p_list, g->p_buffer, g->i_buffer );
567
568 g->i_flags = p_list->i_flags;
569 g->i_pts = p_list->i_pts;
570 g->i_dts = p_list->i_dts;
571 g->i_length = i_length;
572
573 /* free p_list */
574 vlc_frame_ChainRelease( p_list );
575 return g;
576}
577
578/**
579 * @}
580 * \defgroup block_fifo Block FIFO
581 * Thread-safe block queue functions
582 * @{
583 */
584
585#include <vlc_queue.h>
586
587/**
588 * Creates a thread-safe FIFO queue of blocks.
589 *
590 * See also vlc_fifo_Put() and vlc_fifo_Get().
591 * The created queue must be deleted with vlc_fifo_Delete().
592 *
593 * @return the FIFO or NULL on memory error
594 */
596
597/**
598 * Delete a FIFO created by vlc_fifo_New().
599 *
600 * @note Any queued blocks are also deleted.
601 * @warning No other threads may be using the FIFO when this function is
602 * called. Otherwise, undefined behaviour will occur.
603 */
605
606/**
607 * Dequeue the first block from the FIFO. If necessary, wait until there is
608 * one block in the queue. This function is (always) cancellation point.
609 *
610 * @return a valid block
611 */
613
614/**
615 * Peeks the first block in the FIFO.
616 *
617 * @warning This function leaves the block in the FIFO.
618 * You need to protect against concurrent threads who could dequeue the block.
619 * Preferably, there should be only one thread reading from the FIFO.
620 *
621 * @warning This function is undefined if the FIFO is empty.
622 *
623 * @return a valid block.
624 */
626
627static inline vlc_queue_t *vlc_fifo_queue(const vlc_fifo_t *fifo)
629 return (vlc_queue_t *)fifo;
630}
631
632/**
633 * Locks a block FIFO.
634 *
635 * No more than one thread can lock the FIFO at any given
636 * time, and no other thread can modify the FIFO while it is locked.
637 * vlc_fifo_Unlock() releases the lock.
638 *
639 * @note If the FIFO is already locked by another thread, this function waits.
640 * This function is not a cancellation point.
641 *
642 * @warning Recursively locking a single FIFO is undefined. Locking more than
643 * one FIFO at a time may lead to lock inversion; mind the locking order.
644 */
645static inline void vlc_fifo_Lock(vlc_fifo_t *fifo)
648}
649
650/**
651 * Unlocks a block FIFO.
652 *
653 * The calling thread must have locked the FIFO previously with
654 * vlc_fifo_Lock(). Otherwise, the behaviour is undefined.
655 *
656 * @note This function is not a cancellation point.
657 */
658static inline void vlc_fifo_Unlock(vlc_fifo_t *fifo)
661}
662
663/**
664 * Wakes up one thread waiting on the FIFO, if any.
665 *
666 * @note This function is not a cancellation point.
667 *
668 * @warning For race-free operations, the FIFO should be locked by the calling
669 * thread. The function can be called on a unlocked FIFO however.
670 */
671static inline void vlc_fifo_Signal(vlc_fifo_t *fifo)
674}
675
676/**
677 * Waits on the FIFO.
678 *
679 * Atomically unlocks the FIFO and waits until one thread signals the FIFO,
680 * then locks the FIFO again. A signal can be sent by queueing a block to the
681 * previously empty FIFO or by calling vlc_fifo_Signal() directly.
682 * This function may also return spuriously at any moment.
683 *
684 * @note This function is a cancellation point. In case of cancellation, the
685 * the FIFO will be locked before cancellation cleanup handlers are processed.
686 */
687static inline void vlc_fifo_Wait(vlc_fifo_t *fifo)
690}
691
692static inline void vlc_fifo_WaitCond(vlc_fifo_t *fifo, vlc_cond_t *condvar)
694 vlc_queue_t *q = vlc_fifo_queue(fifo);
695
696 vlc_cond_wait(condvar, &q->lock);
697}
698
699/**
700 * Queues a linked-list of blocks into a locked FIFO.
701 *
702 * @param fifo a fifo object locked with ::vlc_fifo_Lock()
703 * @param block the head of the list of blocks
704 * (if NULL, this function has no effects)
705 *
706 * @note This function is not a cancellation point.
707 *
708 * @warning The FIFO must be locked by the calling thread using
709 * vlc_fifo_Lock(). Otherwise behaviour is undefined.
710 */
712
713/**
714 * Dequeues the first block from a locked FIFO, if any.
715 *
716 * @note This function is not a cancellation point.
717 *
718 * @warning The FIFO must be locked by the calling thread using
719 * vlc_fifo_Lock(). Otherwise behaviour is undefined.
720 *
721 * @return the first block in the FIFO or NULL if the FIFO is empty
722 */
724
725/**
726 * Dequeues the all blocks from a locked FIFO.
727 *
728 * This is equivalent to calling vlc_fifo_DequeueUnlocked() repeatedly until
729 * the FIFO is emptied, but this function is much faster.
730 *
731 * @note This function is not a cancellation point.
732 *
733 * @warning The FIFO must be locked by the calling thread using
734 * vlc_fifo_Lock(). Otherwise behaviour is undefined.
735 *
736 * @return a linked-list of all blocks in the FIFO (possibly NULL)
737 */
739
740/**
741 * Counts blocks in a FIFO.
742 *
743 * Checks how many blocks are queued in a locked FIFO.
744 *
745 * @note This function is not cancellation point.
746 *
747 * @warning The FIFO must be locked by the calling thread using
748 * vlc_fifo_Lock(). Otherwise behaviour is undefined.
749 *
750 * @return the number of blocks in the FIFO (zero if it is empty)
751 */
753
754/**
755 * Counts bytes in a FIFO.
756 *
757 * Checks how many bytes are queued in a locked FIFO.
758 *
759 * @note This function is not cancellation point.
760 *
761 * @warning The FIFO must be locked by the calling thread using
762 * vlc_fifo_Lock(). Otherwise behaviour is undefined.
763 *
764 * @return the total number of bytes
765 *
766 * @note Zero bytes does not necessarily mean that the FIFO is empty since
767 * a block could contain zero bytes. Use vlc_fifo_GetCount() to determine if
768 * a FIFO is empty.
769 */
771
772/**
773 * Checks whether the vlc_fifo_t object is being locked.
774 *
775 * This function checks if the calling thread holds a given vlc_fifo_t
776 * object. It has no side effects and is essentially intended for run-time
777 * debugging.
778 *
779 * @note This function is the vlc_fifo_t equivalent of vlc_mutex_held.
780 *
781 * @note To assert that the calling thread holds a lock, the helper macro
782 * vlc_fifo_Assert() should be used instead of this function.
783 *
784 * @retval false the fifo is not locked by the calling thread
785 * @retval true the fifo is locked by the calling thread
786 */
787VLC_API bool vlc_fifo_Held(const vlc_fifo_t *fifo) VLC_USED;
788
789/**
790 * Asserts that a vlc_fifo_t is locked by the calling thread.
791 */
792#define vlc_fifo_Assert(fifo) assert(vlc_fifo_Held(fifo))
794VLC_USED static inline bool vlc_fifo_IsEmpty(const vlc_fifo_t *fifo)
796 return vlc_queue_IsEmpty(vlc_fifo_queue(fifo));
797}
798
799static inline void vlc_fifo_Cleanup(void *fifo)
802}
803#define vlc_fifo_CleanupPush(fifo) vlc_cleanup_push(vlc_fifo_Cleanup, fifo)
805/**
806 * Clears all blocks in a FIFO.
807 */
808static inline void vlc_fifo_Empty(vlc_fifo_t *fifo)
810 vlc_frame_t *block;
811
812 vlc_fifo_Lock(fifo);
813 block = vlc_fifo_DequeueAllUnlocked(fifo);
814 vlc_fifo_Unlock(fifo);
816}
817
818/**
819 * Immediately queue one block at the end of a FIFO.
820 *
821 * @param fifo queue
822 * @param block head of a block list to queue (may be NULL)
823 */
824static inline void vlc_fifo_Put(vlc_fifo_t *fifo, vlc_frame_t *block)
826 vlc_fifo_Lock(fifo);
827 vlc_fifo_QueueUnlocked(fifo, block);
828 vlc_fifo_Unlock(fifo);
829}
830
831/* FIXME: not (really) thread-safe */
833static inline size_t vlc_fifo_Size (vlc_fifo_t *fifo)
835 size_t size;
836
837 vlc_fifo_Lock(fifo);
838 size = vlc_fifo_GetBytes(fifo);
839 vlc_fifo_Unlock(fifo);
840 return size;
841}
842
843/* FIXME: not (really) thread-safe */
845static inline size_t vlc_fifo_Count (vlc_fifo_t *fifo)
847 size_t depth;
848
849 vlc_fifo_Lock(fifo);
850 depth = vlc_fifo_GetCount(fifo);
851 vlc_fifo_Unlock(fifo);
852 return depth;
853}
854
855/** @} */
856
857/** @} */
858
859#endif /* VLC_FRAME_H */
#define VLC_USED
Definition fourcc_gen.c:32
#define VLC_API
Definition fourcc_gen.c:31
#define p(t)
uint32_t vlc_ancillary_id
ID of an ancillary.
Definition vlc_ancillary.h:68
static void vlc_fifo_Empty(vlc_fifo_t *fifo)
Clears all blocks in a FIFO.
Definition vlc_frame.h:809
vlc_fifo_t * vlc_fifo_New(void)
Creates a thread-safe FIFO queue of blocks.
Definition fifo.c:95
static size_t vlc_fifo_Size(vlc_fifo_t *fifo)
Definition vlc_frame.h:834
static void vlc_fifo_Lock(vlc_fifo_t *fifo)
Locks a block FIFO.
Definition vlc_frame.h:646
static void vlc_fifo_WaitCond(vlc_fifo_t *fifo, vlc_cond_t *condvar)
Definition vlc_frame.h:693
size_t vlc_fifo_GetBytes(const vlc_fifo_t *)
Counts bytes in a FIFO.
Definition fifo.c:58
vlc_frame_t * vlc_fifo_Show(vlc_fifo_t *)
Peeks the first block in the FIFO.
Definition fifo.c:133
static void vlc_fifo_Wait(vlc_fifo_t *fifo)
Waits on the FIFO.
Definition vlc_frame.h:688
static void vlc_fifo_Unlock(vlc_fifo_t *fifo)
Unlocks a block FIFO.
Definition vlc_frame.h:659
bool vlc_fifo_Held(const vlc_fifo_t *fifo)
Checks whether the vlc_fifo_t object is being locked.
Definition fifo.c:47
vlc_frame_t * vlc_fifo_DequeueUnlocked(vlc_fifo_t *)
Dequeues the first block from a locked FIFO, if any.
Definition fifo.c:74
static vlc_queue_t * vlc_fifo_queue(const vlc_fifo_t *fifo)
Definition vlc_frame.h:628
void vlc_fifo_QueueUnlocked(vlc_fifo_t *fifo, vlc_frame_t *block)
Queues a linked-list of blocks into a locked FIFO.
Definition fifo.c:64
static void vlc_fifo_Put(vlc_fifo_t *fifo, vlc_frame_t *block)
Immediately queue one block at the end of a FIFO.
Definition vlc_frame.h:825
size_t vlc_fifo_GetCount(const vlc_fifo_t *)
Counts blocks in a FIFO.
Definition fifo.c:52
static void vlc_fifo_Signal(vlc_fifo_t *fifo)
Wakes up one thread waiting on the FIFO, if any.
Definition vlc_frame.h:672
vlc_frame_t * vlc_fifo_Get(vlc_fifo_t *)
Dequeue the first block from the FIFO.
Definition fifo.c:114
static size_t vlc_fifo_Count(vlc_fifo_t *fifo)
Definition vlc_frame.h:846
void vlc_fifo_Delete(vlc_fifo_t *)
Delete a FIFO created by vlc_fifo_New().
Definition fifo.c:108
vlc_frame_t * vlc_fifo_DequeueAllUnlocked(vlc_fifo_t *)
Dequeues the all blocks from a locked FIFO.
Definition fifo.c:88
static bool vlc_fifo_IsEmpty(const vlc_fifo_t *fifo)
Definition vlc_frame.h:795
static void vlc_fifo_Cleanup(void *fifo)
Definition vlc_frame.h:800
#define VLC_MALLOC
Definition vlc_common.h:157
#define VLC_DEPRECATED
Definition vlc_common.h:158
void vlc_cond_wait(vlc_cond_t *cond, vlc_mutex_t *mutex)
Waits on a condition variable.
Definition threads.c:280
void vlc_frame_Release(vlc_frame_t *frame)
Releases a frame.
Definition frame.c:154
vlc_frame_t * vlc_frame_Alloc(size_t size)
Allocates a frame.
Definition frame.c:115
struct vlc_ancillary * vlc_frame_GetAncillary(vlc_frame_t *frame, vlc_ancillary_id id)
Return the ancillary identified by an ID.
Definition frame.c:565
static vlc_frame_t * vlc_frame_Duplicate(const vlc_frame_t *frame)
Duplicates a frame.
Definition vlc_frame.h:284
vlc_frame_t * vlc_frame_FilePath(const char *path, bool write)
Maps a file in memory.
Definition frame.c:545
vlc_frame_t * vlc_frame_shm_Alloc(void *addr, size_t length)
Wraps a System V memory segment in a frame.
Definition frame.c:382
static void vlc_frame_Cleanup(void *frame)
Definition vlc_frame.h:374
vlc_frame_t * vlc_frame_New(const struct vlc_frame_callbacks *cbs, void *base, size_t length)
Creates a custom frame.
Definition frame.c:78
vlc_frame_t * vlc_frame_Init(vlc_frame_t *frame, const struct vlc_frame_callbacks *cbs, void *base, size_t length)
Initializes a custom frame.
void vlc_frame_CopyProperties(vlc_frame_t *dst, const vlc_frame_t *src)
Copy frame properties from src to dst.
int vlc_frame_AttachAncillary(vlc_frame_t *frame, struct vlc_ancillary *ancillary)
Attach an ancillary to the frame.
Definition frame.c:559
vlc_frame_t * vlc_frame_Realloc(vlc_frame_t *frame, ssize_t pre, size_t body)
Reallocates a frame.
Definition frame.c:256
vlc_frame_t * vlc_frame_heap_Alloc(void *addr, size_t length)
Wraps heap in a frame.
Definition frame.c:275
vlc_frame_t * vlc_frame_mmap_Alloc(void *addr, size_t length)
Wraps a memory mapping in a frame.
Definition frame.c:316
vlc_frame_t * vlc_frame_File(int fd, bool write)
Maps a file handle in memory.
Definition frame.c:398
vlc_frame_t * vlc_frame_TryRealloc(vlc_frame_t *, ssize_t pre, size_t body)
Definition frame.c:181
static void vlc_queue_Lock(vlc_queue_t *q)
Locks a queue.
Definition vlc_queue.h:88
static void vlc_queue_Wait(vlc_queue_t *q)
Waits for a queue entry.
Definition vlc_queue.h:122
static void vlc_queue_Signal(vlc_queue_t *q)
Wakes one thread waiting for a queue entry up.
Definition vlc_queue.h:108
static void vlc_queue_Unlock(vlc_queue_t *q)
Unlocks a queue.
Definition vlc_queue.h:100
static bool vlc_queue_IsEmpty(const vlc_queue_t *q)
Checks if a queue is empty (without locking).
Definition vlc_queue.h:179
static void vlc_frame_ChainLastAppend(vlc_frame_t ***ppp_last, vlc_frame_t *frame)
Appends a vlc_frame_t to the last frame pointer and update it.
Definition vlc_frame.h:443
static void vlc_frame_ChainProperties(const vlc_frame_t *p_list, int *pi_count, size_t *pi_size, vlc_tick_t *pi_length)
Retrieves chain properties.
Definition vlc_frame.h:516
static void vlc_frame_ChainAppend(vlc_frame_t **pp_list, vlc_frame_t *frame)
Appends a vlc_frame_t to the chain.
Definition vlc_frame.h:408
static void vlc_frame_ChainRelease(vlc_frame_t *frame)
Releases a chain of blocks.
Definition vlc_frame.h:463
static size_t vlc_frame_ChainExtract(vlc_frame_t *p_list, void *p_data, size_t i_max)
Extracts data from a chain of frames.
Definition vlc_frame.h:487
static vlc_frame_t * vlc_frame_ChainGather(vlc_frame_t *p_list)
Gathers a chain into a single vlc_frame_t.
Definition vlc_frame.h:553
Definition ancillary.c:31
Condition variable.
Definition vlc_threads.h:270
Internal state for block queues.
Definition fifo.c:39
Definition vlc_frame.h:118
void(* free)(vlc_frame_t *)
Definition vlc_frame.h:119
Definition vlc_frame.h:123
uint8_t * p_start
Buffer start.
Definition vlc_frame.h:128
vlc_tick_t i_dts
Definition vlc_frame.h:135
vlc_frame_t * p_next
Definition vlc_frame.h:124
vlc_tick_t i_length
Definition vlc_frame.h:136
uint8_t * p_buffer
Payload start.
Definition vlc_frame.h:126
struct vlc_ancillary ** priv_ancillaries
Private ancillary struct.
Definition vlc_frame.h:140
vlc_tick_t i_pts
Definition vlc_frame.h:134
unsigned i_nb_samples
Definition vlc_frame.h:132
uint32_t i_flags
Definition vlc_frame.h:131
size_t i_size
Buffer total size.
Definition vlc_frame.h:129
const struct vlc_frame_callbacks * cbs
Definition vlc_frame.h:142
size_t i_buffer
Payload length.
Definition vlc_frame.h:127
Thread-safe queue (a.k.a.
Definition vlc_queue.h:46
vlc_mutex_t lock
Definition vlc_queue.h:50
This file is a collection of common definitions and types.
uint32_t vlc_ancillary_id
Definition vlc_frame.h:30
int64_t vlc_tick_t
High precision date or time interval.
Definition vlc_tick.h:48