mux: mp4: use samplepriv as fallback location for extra
[vlc.git] / include / vlc_block.h
blobe54cc52e352b044067153ebaab5d170772157bb1
1 /*****************************************************************************
2 * vlc_block.h: Data blocks management functions
3 *****************************************************************************
4 * Copyright (C) 2003 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifndef VLC_BLOCK_H
25 #define VLC_BLOCK_H 1
27 /**
28 * \defgroup block Data blocks
29 * \ingroup input
31 * Blocks of binary data.
33 * @ref block_t is a generic structure to represent a binary blob within VLC.
34 * The primary goal of the structure is to avoid memory copying as data is
35 * passed around. It is notably used between the \ref demux, the packetizer
36 * (if present) and the \ref decoder, and for audio, between the \ref decoder,
37 * the audio filters, and the \ref audio_output.
39 * @{
40 * \file
41 * Data block definition and functions
44 #include <sys/types.h> /* for ssize_t */
46 /****************************************************************************
47 * block:
48 ****************************************************************************
49 * - i_flags may not always be set (ie could be 0, even for a key frame
50 * it depends where you receive the buffer (before/after a packetizer
51 * and the demux/packetizer implementations.
52 * - i_dts/i_pts could be VLC_TICK_INVALID, it means no pts/dts
53 * - i_length: length in microseond of the packet, can be null except in the
54 * sout where it is mandatory.
56 * - i_buffer number of valid data pointed by p_buffer
57 * you can freely decrease it but never increase it yourself
58 * (use block_Realloc)
59 * - p_buffer: pointer over datas. You should never overwrite it, you can
60 * only incremment it to skip datas, in others cases use block_Realloc
61 * (don't duplicate yourself in a bigger buffer, block_Realloc is
62 * optimised for preheader/postdatas increase)
63 ****************************************************************************/
65 /** The content doesn't follow the last block, possible some blocks in between
66 * have been lost */
67 #define BLOCK_FLAG_DISCONTINUITY 0x0001
68 /** Intra frame */
69 #define BLOCK_FLAG_TYPE_I 0x0002
70 /** Inter frame with backward reference only */
71 #define BLOCK_FLAG_TYPE_P 0x0004
72 /** Inter frame with backward and forward reference */
73 #define BLOCK_FLAG_TYPE_B 0x0008
74 /** For inter frame when you don't know the real type */
75 #define BLOCK_FLAG_TYPE_PB 0x0010
76 /** Warn that this block is a header one */
77 #define BLOCK_FLAG_HEADER 0x0020
78 /** This block contains the last part of a sequence */
79 #define BLOCK_FLAG_END_OF_SEQUENCE 0x0040
80 /** This block contains a clock reference */
81 #define BLOCK_FLAG_CLOCK 0x0080
82 /** This block is scrambled */
83 #define BLOCK_FLAG_SCRAMBLED 0x0100
84 /** This block has to be decoded but not be displayed */
85 #define BLOCK_FLAG_PREROLL 0x0200
86 /** This block is corrupted and/or there is data loss */
87 #define BLOCK_FLAG_CORRUPTED 0x0400
88 /** This block contains an interlaced picture with top field stored first */
89 #define BLOCK_FLAG_TOP_FIELD_FIRST 0x0800
90 /** This block contains an interlaced picture with bottom field stored first */
91 #define BLOCK_FLAG_BOTTOM_FIELD_FIRST 0x1000
92 /** This block contains a single field from interlaced picture. */
93 #define BLOCK_FLAG_SINGLE_FIELD 0x2000
95 /** This block contains an interlaced picture */
96 #define BLOCK_FLAG_INTERLACED_MASK \
97 (BLOCK_FLAG_TOP_FIELD_FIRST|BLOCK_FLAG_BOTTOM_FIELD_FIRST|BLOCK_FLAG_SINGLE_FIELD)
99 #define BLOCK_FLAG_TYPE_MASK \
100 (BLOCK_FLAG_TYPE_I|BLOCK_FLAG_TYPE_P|BLOCK_FLAG_TYPE_B|BLOCK_FLAG_TYPE_PB)
102 /* These are for input core private usage only */
103 #define BLOCK_FLAG_CORE_PRIVATE_MASK 0x00ff0000
104 #define BLOCK_FLAG_CORE_PRIVATE_SHIFT 16
106 /* These are for module private usage only */
107 #define BLOCK_FLAG_PRIVATE_MASK 0xff000000
108 #define BLOCK_FLAG_PRIVATE_SHIFT 24
110 struct vlc_block_callbacks
112 void (*free)(block_t *);
115 struct block_t
117 block_t *p_next;
119 uint8_t *p_buffer; /**< Payload start */
120 size_t i_buffer; /**< Payload length */
121 uint8_t *p_start; /**< Buffer start */
122 size_t i_size; /**< Buffer total size */
124 uint32_t i_flags;
125 unsigned i_nb_samples; /* Used for audio */
127 vlc_tick_t i_pts;
128 vlc_tick_t i_dts;
129 vlc_tick_t i_length;
131 const struct vlc_block_callbacks *cbs;
135 * Initializes a custom block.
137 * This function initialize a block of timed data allocated by custom means.
138 * This allows passing data with copying even if the data has been allocated
139 * with unusual means or outside of LibVLC.
141 * Normally, blocks are allocated and initialized by block_Alloc() instead.
143 * @param block allocated block structure to initialize
144 * @param cbs structure of custom callbacks to handle the block [IN]
145 * @param base start address of the block data
146 * @param length byte length of the block data
148 * @return @c block (this function cannot fail)
150 VLC_API block_t *block_Init(block_t *block,
151 const struct vlc_block_callbacks *cbs,
152 void *base, size_t length);
155 * Allocates a block.
157 * Creates a new block with the requested size.
158 * The block must be released with block_Release().
160 * @param size size in bytes (possibly zero)
161 * @return the created block, or NULL on memory error.
163 VLC_API block_t *block_Alloc(size_t size) VLC_USED VLC_MALLOC;
165 VLC_API block_t *block_TryRealloc(block_t *, ssize_t pre, size_t body) VLC_USED;
168 * Reallocates a block.
170 * This function expands, shrinks or moves a data block.
171 * In many cases, this function can return without any memory allocation by
172 * reusing spare buffer space. Otherwise, a new block is created and data is
173 * copied.
175 * @param pre count of bytes to prepend if positive,
176 * count of leading bytes to discard if negative
177 * @param body new bytes size of the block
179 * @return the reallocated block on succes, NULL on error.
181 * @note Skipping leading bytes can be achieved directly by subtracting from
182 * block_t.i_buffer and adding block_t.p_buffer.
183 * @note Discard trailing bytes can be achieved directly by subtracting from
184 * block_t.i_buffer.
185 * @note On error, the block is discarded.
186 * To avoid that, use block_TryRealloc() instead.
188 VLC_API block_t *block_Realloc(block_t *, ssize_t pre, size_t body) VLC_USED;
191 * Releases a block.
193 * This function works for any @ref block_t block, regardless of the way it was
194 * allocated.
196 * @note
197 * If the block is in a chain, this function does <b>not</b> release any
198 * subsequent block in the chain. Use block_ChainRelease() for that purpose.
200 * @param block block to release (cannot be NULL)
202 VLC_API void block_Release(block_t *block);
204 static inline void block_CopyProperties( block_t *dst, block_t *src )
206 dst->i_flags = src->i_flags;
207 dst->i_nb_samples = src->i_nb_samples;
208 dst->i_dts = src->i_dts;
209 dst->i_pts = src->i_pts;
210 dst->i_length = src->i_length;
214 * Duplicates a block.
216 * Creates a writeable duplicate of a block.
218 * @return the duplicate on success, NULL on error.
220 VLC_USED
221 static inline block_t *block_Duplicate( block_t *p_block )
223 block_t *p_dup = block_Alloc( p_block->i_buffer );
224 if( p_dup == NULL )
225 return NULL;
227 block_CopyProperties( p_dup, p_block );
228 memcpy( p_dup->p_buffer, p_block->p_buffer, p_block->i_buffer );
230 return p_dup;
234 * Wraps heap in a block.
236 * Creates a @ref block_t out of an existing heap allocation.
237 * This is provided by LibVLC so that manually heap-allocated blocks can safely
238 * be deallocated even after the origin plugin has been unloaded from memory.
240 * When block_Release() is called, VLC will free() the specified pointer.
242 * @param addr base address of the heap allocation (will be free()'d)
243 * @param length bytes length of the heap allocation
244 * @return NULL in case of error (ptr free()'d in that case), or a valid
245 * block_t pointer.
247 VLC_API block_t *block_heap_Alloc(void *, size_t) VLC_USED VLC_MALLOC;
250 * Wraps a memory mapping in a block
252 * Creates a @ref block_t from a virtual address memory mapping (mmap).
253 * This is provided by LibVLC so that mmap blocks can safely be deallocated
254 * even after the allocating plugin has been unloaded from memory.
256 * @param addr base address of the mapping (as returned by mmap)
257 * @param length length (bytes) of the mapping (as passed to mmap)
258 * @return NULL if addr is MAP_FAILED, or an error occurred (in the later
259 * case, munmap(addr, length) is invoked before returning).
261 VLC_API block_t *block_mmap_Alloc(void *addr, size_t length) VLC_USED VLC_MALLOC;
264 * Wraps a System V memory segment in a block
266 * Creates a @ref block_t from a System V shared memory segment (shmget()).
267 * This is provided by LibVLC so that segments can safely be deallocated
268 * even after the allocating plugin has been unloaded from memory.
270 * @param addr base address of the segment (as returned by shmat())
271 * @param length length (bytes) of the segment (as passed to shmget())
272 * @return NULL if an error occurred (in that case, shmdt(addr) is invoked
273 * before returning NULL).
275 VLC_API block_t * block_shm_Alloc(void *addr, size_t length) VLC_USED VLC_MALLOC;
278 * Maps a file handle in memory.
280 * Loads a file into a block of memory through a file descriptor.
281 * If possible a private file mapping is created. Otherwise, the file is read
282 * normally. This function is a cancellation point.
284 * @note On 32-bits platforms,
285 * this function will not work for very large files,
286 * due to memory space constraints.
288 * @param fd file descriptor to load from
289 * @param write If true, request a read/write private mapping.
290 * If false, request a read-only potentially shared mapping.
292 * @return a new block with the file content at p_buffer, and file length at
293 * i_buffer (release it with block_Release()), or NULL upon error (see errno).
295 VLC_API block_t *block_File(int fd, bool write) VLC_USED VLC_MALLOC;
298 * Maps a file in memory.
300 * Loads a file into a block of memory from a path to the file.
301 * See also block_File().
303 * @param write If true, request a read/write private mapping.
304 * If false, request a read-only potentially shared mapping.
306 VLC_API block_t *block_FilePath(const char *, bool write) VLC_USED VLC_MALLOC;
308 static inline void block_Cleanup (void *block)
310 block_Release ((block_t *)block);
312 #define block_cleanup_push( block ) vlc_cleanup_push (block_Cleanup, block)
315 * \defgroup block_fifo Block chain
316 * @{
319 /****************************************************************************
320 * Chains of blocks functions helper
321 ****************************************************************************
322 * - block_ChainAppend : append a block to the last block of a chain. Try to
323 * avoid using with a lot of data as it's really slow, prefer
324 * block_ChainLastAppend, p_block can be NULL
325 * - block_ChainLastAppend : use a pointer over a pointer to the next blocks,
326 * and update it.
327 * - block_ChainRelease : release a chain of block
328 * - block_ChainExtract : extract data from a chain, return real bytes counts
329 * - block_ChainGather : gather a chain, free it and return one block.
330 ****************************************************************************/
331 static inline void block_ChainAppend( block_t **pp_list, block_t *p_block )
333 if( *pp_list == NULL )
335 *pp_list = p_block;
337 else
339 block_t *p = *pp_list;
341 while( p->p_next ) p = p->p_next;
342 p->p_next = p_block;
346 static inline void block_ChainLastAppend( block_t ***ppp_last, block_t *p_block )
348 block_t *p_last = p_block;
350 **ppp_last = p_block;
352 while( p_last->p_next ) p_last = p_last->p_next;
353 *ppp_last = &p_last->p_next;
356 static inline void block_ChainRelease( block_t *p_block )
358 while( p_block )
360 block_t *p_next = p_block->p_next;
361 block_Release( p_block );
362 p_block = p_next;
366 static size_t block_ChainExtract( block_t *p_list, void *p_data, size_t i_max )
368 size_t i_total = 0;
369 uint8_t *p = (uint8_t*)p_data;
371 while( p_list && i_max )
373 size_t i_copy = __MIN( i_max, p_list->i_buffer );
374 memcpy( p, p_list->p_buffer, i_copy );
375 i_max -= i_copy;
376 i_total += i_copy;
377 p += i_copy;
379 p_list = p_list->p_next;
381 return i_total;
384 static inline void block_ChainProperties( block_t *p_list, int *pi_count, size_t *pi_size, vlc_tick_t *pi_length )
386 size_t i_size = 0;
387 vlc_tick_t i_length = 0;
388 int i_count = 0;
390 while( p_list )
392 i_size += p_list->i_buffer;
393 i_length += p_list->i_length;
394 i_count++;
396 p_list = p_list->p_next;
399 if( pi_size )
400 *pi_size = i_size;
401 if( pi_length )
402 *pi_length = i_length;
403 if( pi_count )
404 *pi_count = i_count;
407 static inline block_t *block_ChainGather( block_t *p_list )
409 size_t i_total = 0;
410 vlc_tick_t i_length = 0;
411 block_t *g;
413 if( p_list->p_next == NULL )
414 return p_list; /* Already gathered */
416 block_ChainProperties( p_list, NULL, &i_total, &i_length );
418 g = block_Alloc( i_total );
419 if( !g )
420 return NULL;
421 block_ChainExtract( p_list, g->p_buffer, g->i_buffer );
423 g->i_flags = p_list->i_flags;
424 g->i_pts = p_list->i_pts;
425 g->i_dts = p_list->i_dts;
426 g->i_length = i_length;
428 /* free p_list */
429 block_ChainRelease( p_list );
430 return g;
434 * @}
435 * \defgroup fifo Block FIFO
436 * Thread-safe block queue functions
437 * @{
441 * Creates a thread-safe FIFO queue of blocks.
443 * See also block_FifoPut() and block_FifoGet().
444 * The created queue must be released with block_FifoRelease().
446 * @return the FIFO or NULL on memory error
448 VLC_API block_fifo_t *block_FifoNew(void) VLC_USED VLC_MALLOC;
451 * Destroys a FIFO created by block_FifoNew().
453 * @note Any queued blocks are also destroyed.
454 * @warning No other threads may be using the FIFO when this function is
455 * called. Otherwise, undefined behaviour will occur.
457 VLC_API void block_FifoRelease(block_fifo_t *);
460 * Clears all blocks in a FIFO.
462 VLC_API void block_FifoEmpty(block_fifo_t *);
465 * Immediately queue one block at the end of a FIFO.
467 * @param fifo queue
468 * @param block head of a block list to queue (may be NULL)
470 VLC_API void block_FifoPut(block_fifo_t *fifo, block_t *block);
473 * Dequeue the first block from the FIFO. If necessary, wait until there is
474 * one block in the queue. This function is (always) cancellation point.
476 * @return a valid block
478 VLC_API block_t *block_FifoGet(block_fifo_t *) VLC_USED;
481 * Peeks the first block in the FIFO.
483 * @warning This function leaves the block in the FIFO.
484 * You need to protect against concurrent threads who could dequeue the block.
485 * Preferably, there should be only one thread reading from the FIFO.
487 * @warning This function is undefined if the FIFO is empty.
489 * @return a valid block.
491 VLC_API block_t *block_FifoShow(block_fifo_t *);
493 size_t block_FifoSize(block_fifo_t *) VLC_USED VLC_DEPRECATED;
494 VLC_API size_t block_FifoCount(block_fifo_t *) VLC_USED VLC_DEPRECATED;
496 typedef struct block_fifo_t vlc_fifo_t;
499 * Locks a block FIFO.
501 * No more than one thread can lock the FIFO at any given
502 * time, and no other thread can modify the FIFO while it is locked.
503 * vlc_fifo_Unlock() releases the lock.
505 * @note If the FIFO is already locked by another thread, this function waits.
506 * This function is not a cancellation point.
508 * @warning Recursively locking a single FIFO is undefined. Locking more than
509 * one FIFO at a time may lead to lock inversion; mind the locking order.
511 VLC_API void vlc_fifo_Lock(vlc_fifo_t *);
514 * Unlocks a block FIFO.
516 * The calling thread must have locked the FIFO previously with
517 * vlc_fifo_Lock(). Otherwise, the behaviour is undefined.
519 * @note This function is not a cancellation point.
521 VLC_API void vlc_fifo_Unlock(vlc_fifo_t *);
524 * Wakes up one thread waiting on the FIFO, if any.
526 * @note This function is not a cancellation point.
528 * @warning For race-free operations, the FIFO should be locked by the calling
529 * thread. The function can be called on a unlocked FIFO however.
531 VLC_API void vlc_fifo_Signal(vlc_fifo_t *);
534 * Waits on the FIFO.
536 * Atomically unlocks the FIFO and waits until one thread signals the FIFO,
537 * then locks the FIFO again. A signal can be sent by queueing a block to the
538 * previously empty FIFO or by calling vlc_fifo_Signal() directly.
539 * This function may also return spuriously at any moment.
541 * @note This function is a cancellation point. In case of cancellation, the
542 * the FIFO will be locked before cancellation cleanup handlers are processed.
544 VLC_API void vlc_fifo_Wait(vlc_fifo_t *);
546 VLC_API void vlc_fifo_WaitCond(vlc_fifo_t *, vlc_cond_t *);
549 * Timed variant of vlc_fifo_WaitCond().
551 * Atomically unlocks the FIFO and waits until one thread signals the FIFO up
552 * to a certain date, then locks the FIFO again. See vlc_fifo_Wait().
554 int vlc_fifo_TimedWaitCond(vlc_fifo_t *, vlc_cond_t *, vlc_tick_t);
557 * Queues a linked-list of blocks into a locked FIFO.
559 * @param block the head of the list of blocks
560 * (if NULL, this function has no effects)
562 * @note This function is not a cancellation point.
564 * @warning The FIFO must be locked by the calling thread using
565 * vlc_fifo_Lock(). Otherwise behaviour is undefined.
567 VLC_API void vlc_fifo_QueueUnlocked(vlc_fifo_t *, block_t *);
570 * Dequeues the first block from a locked FIFO, if any.
572 * @note This function is not a cancellation point.
574 * @warning The FIFO must be locked by the calling thread using
575 * vlc_fifo_Lock(). Otherwise behaviour is undefined.
577 * @return the first block in the FIFO or NULL if the FIFO is empty
579 VLC_API block_t *vlc_fifo_DequeueUnlocked(vlc_fifo_t *) VLC_USED;
582 * Dequeues the all blocks from a locked FIFO.
584 * This is equivalent to calling vlc_fifo_DequeueUnlocked() repeatedly until
585 * the FIFO is emptied, but this function is much faster.
587 * @note This function is not a cancellation point.
589 * @warning The FIFO must be locked by the calling thread using
590 * vlc_fifo_Lock(). Otherwise behaviour is undefined.
592 * @return a linked-list of all blocks in the FIFO (possibly NULL)
594 VLC_API block_t *vlc_fifo_DequeueAllUnlocked(vlc_fifo_t *) VLC_USED;
597 * Counts blocks in a FIFO.
599 * Checks how many blocks are queued in a locked FIFO.
601 * @note This function is not cancellation point.
603 * @warning The FIFO must be locked by the calling thread using
604 * vlc_fifo_Lock(). Otherwise behaviour is undefined.
606 * @return the number of blocks in the FIFO (zero if it is empty)
608 VLC_API size_t vlc_fifo_GetCount(const vlc_fifo_t *) VLC_USED;
611 * Counts bytes in a FIFO.
613 * Checks how many bytes are queued in a locked FIFO.
615 * @note This function is not cancellation point.
617 * @warning The FIFO must be locked by the calling thread using
618 * vlc_fifo_Lock(). Otherwise behaviour is undefined.
620 * @return the total number of bytes
622 * @note Zero bytes does not necessarily mean that the FIFO is empty since
623 * a block could contain zero bytes. Use vlc_fifo_GetCount() to determine if
624 * a FIFO is empty.
626 VLC_API size_t vlc_fifo_GetBytes(const vlc_fifo_t *) VLC_USED;
628 VLC_USED static inline bool vlc_fifo_IsEmpty(const vlc_fifo_t *fifo)
630 return vlc_fifo_GetCount(fifo) == 0;
633 static inline void vlc_fifo_Cleanup(void *fifo)
635 vlc_fifo_Unlock((vlc_fifo_t *)fifo);
637 #define vlc_fifo_CleanupPush(fifo) vlc_cleanup_push(vlc_fifo_Cleanup, fifo)
639 /** @} */
641 /** @} */
643 #endif /* VLC_BLOCK_H */