vlc_block.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * vlc_block.h: Data blocks management functions
00003  *****************************************************************************
00004  * Copyright (C) 2003 VLC authors and VideoLAN
00005  * $Id: 2f2a90b3f9ef19d57d82bba41c78ebcc0fa1d85c $
00006  *
00007  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
00008  *
00009  * This program is free software; you can redistribute it and/or modify it
00010  * under the terms of the GNU Lesser General Public License as published by
00011  * the Free Software Foundation; either version 2.1 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017  * GNU Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with this program; if not, write to the Free Software Foundation,
00021  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00022  *****************************************************************************/
00023 
00024 #ifndef VLC_BLOCK_H
00025 #define VLC_BLOCK_H 1
00026 
00027 /**
00028  * \file
00029  * This file implements functions and structures to handle blocks of data in vlc
00030  *
00031  */
00032 
00033 #include <sys/types.h>  /* for ssize_t */
00034 
00035 /****************************************************************************
00036  * block:
00037  ****************************************************************************
00038  * - block_sys_t is opaque and thus block_t->p_sys is PRIVATE
00039  * - i_flags may not always be set (ie could be 0, even for a key frame
00040  *      it depends where you receive the buffer (before/after a packetizer
00041  *      and the demux/packetizer implementations.
00042  * - i_dts/i_pts could be VLC_TS_INVALID, it means no pts/dts
00043  * - i_length: length in microseond of the packet, can be null except in the
00044  *      sout where it is mandatory.
00045  *
00046  * - i_buffer number of valid data pointed by p_buffer
00047  *      you can freely decrease it but never increase it yourself
00048  *      (use block_Realloc)
00049  * - p_buffer: pointer over datas. You should never overwrite it, you can
00050  *   only incremment it to skip datas, in others cases use block_Realloc
00051  *   (don't duplicate yourself in a bigger buffer, block_Realloc is
00052  *   optimised for preheader/postdatas increase)
00053  ****************************************************************************/
00054 typedef struct block_sys_t block_sys_t;
00055 
00056 /** The content doesn't follow the last block, or is probably broken */
00057 #define BLOCK_FLAG_DISCONTINUITY 0x0001
00058 /** Intra frame */
00059 #define BLOCK_FLAG_TYPE_I        0x0002
00060 /** Inter frame with backward reference only */
00061 #define BLOCK_FLAG_TYPE_P        0x0004
00062 /** Inter frame with backward and forward reference */
00063 #define BLOCK_FLAG_TYPE_B        0x0008
00064 /** For inter frame when you don't know the real type */
00065 #define BLOCK_FLAG_TYPE_PB       0x0010
00066 /** Warn that this block is a header one */
00067 #define BLOCK_FLAG_HEADER        0x0020
00068 /** This is the last block of the frame */
00069 #define BLOCK_FLAG_END_OF_FRAME  0x0040
00070 /** This is not a key frame for bitrate shaping */
00071 #define BLOCK_FLAG_NO_KEYFRAME   0x0080
00072 /** This block contains the last part of a sequence  */
00073 #define BLOCK_FLAG_END_OF_SEQUENCE 0x0100
00074 /** This block contains a clock reference */
00075 #define BLOCK_FLAG_CLOCK         0x0200
00076 /** This block is scrambled */
00077 #define BLOCK_FLAG_SCRAMBLED     0x0400
00078 /** This block has to be decoded but not be displayed */
00079 #define BLOCK_FLAG_PREROLL       0x0800
00080 /** This block is corrupted and/or there is data loss  */
00081 #define BLOCK_FLAG_CORRUPTED     0x1000
00082 /** This block contains an interlaced picture with top field first */
00083 #define BLOCK_FLAG_TOP_FIELD_FIRST 0x2000
00084 /** This block contains an interlaced picture with bottom field first */
00085 #define BLOCK_FLAG_BOTTOM_FIELD_FIRST 0x4000
00086 
00087 /** This block contains an interlaced picture */
00088 #define BLOCK_FLAG_INTERLACED_MASK \
00089     (BLOCK_FLAG_TOP_FIELD_FIRST|BLOCK_FLAG_BOTTOM_FIELD_FIRST)
00090 
00091 #define BLOCK_FLAG_TYPE_MASK \
00092     (BLOCK_FLAG_TYPE_I|BLOCK_FLAG_TYPE_P|BLOCK_FLAG_TYPE_B|BLOCK_FLAG_TYPE_PB)
00093 
00094 /* These are for input core private usage only */
00095 #define BLOCK_FLAG_CORE_PRIVATE_MASK  0x00ff0000
00096 #define BLOCK_FLAG_CORE_PRIVATE_SHIFT 16
00097 
00098 /* These are for module private usage only */
00099 #define BLOCK_FLAG_PRIVATE_MASK  0xff000000
00100 #define BLOCK_FLAG_PRIVATE_SHIFT 24
00101 
00102 typedef void (*block_free_t) (block_t *);
00103 
00104 struct block_t
00105 {
00106     block_t    *p_next;
00107 
00108     uint8_t    *p_buffer; /**< Payload start */
00109     size_t      i_buffer; /**< Payload length */
00110     uint8_t    *p_start; /**< Buffer start */
00111     size_t      i_size; /**< Buffer total size */
00112 
00113     uint32_t    i_flags;
00114     unsigned    i_nb_samples; /* Used for audio */
00115 
00116     mtime_t     i_pts;
00117     mtime_t     i_dts;
00118     mtime_t     i_length;
00119 
00120     /* Rudimentary support for overloading block (de)allocation. */
00121     block_free_t pf_release;
00122 };
00123 
00124 /****************************************************************************
00125  * Blocks functions:
00126  ****************************************************************************
00127  * - block_Alloc : create a new block with the requested size ( >= 0 ), return
00128  *      NULL for failure.
00129  * - block_Release : release a block allocated with block_Alloc.
00130  * - block_Realloc : realloc a block,
00131  *      i_pre: how many bytes to insert before body if > 0, else how many
00132  *      bytes of body to skip (the latter can be done without using
00133  *      block_Realloc i_buffer -= -i_pre, p_buffer += -i_pre as i_pre < 0)
00134  *      i_body (>= 0): the final size of the body (decreasing it can directly
00135  *      be done with i_buffer = i_body).
00136  *      with preheader and or body (increase
00137  *      and decrease are supported). Use it as it is optimised.
00138  * - block_Duplicate : create a copy of a block.
00139  ****************************************************************************/
00140 VLC_API void block_Init( block_t *, void *, size_t );
00141 VLC_API block_t * block_Alloc( size_t ) VLC_USED;
00142 VLC_API block_t * block_Realloc( block_t *, ssize_t i_pre, size_t i_body ) VLC_USED;
00143 
00144 #define block_New( dummy, size ) block_Alloc(size)
00145 
00146 VLC_USED
00147 static inline block_t *block_Duplicate( block_t *p_block )
00148 {
00149     block_t *p_dup = block_Alloc( p_block->i_buffer );
00150     if( p_dup == NULL )
00151         return NULL;
00152 
00153     p_dup->i_flags   = p_block->i_flags;
00154     p_dup->i_nb_samples = p_block->i_nb_samples;
00155     p_dup->i_dts     = p_block->i_dts;
00156     p_dup->i_pts     = p_block->i_pts;
00157     p_dup->i_length  = p_block->i_length;
00158     memcpy( p_dup->p_buffer, p_block->p_buffer, p_block->i_buffer );
00159 
00160     return p_dup;
00161 }
00162 
00163 static inline void block_Release( block_t *p_block )
00164 {
00165     p_block->pf_release( p_block );
00166 }
00167 
00168 VLC_API block_t * block_heap_Alloc(void *, size_t) VLC_USED;
00169 VLC_API block_t * block_mmap_Alloc(void *addr, size_t length) VLC_USED;
00170 VLC_API block_t * block_File(int fd) VLC_USED;
00171 
00172 static inline void block_Cleanup (void *block)
00173 {
00174     block_Release ((block_t *)block);
00175 }
00176 #define block_cleanup_push( block ) vlc_cleanup_push (block_Cleanup, block)
00177 
00178 /****************************************************************************
00179  * Chains of blocks functions helper
00180  ****************************************************************************
00181  * - block_ChainAppend : append a block to the last block of a chain. Try to
00182  *      avoid using with a lot of data as it's really slow, prefer
00183  *      block_ChainLastAppend, p_block can be NULL
00184  * - block_ChainLastAppend : use a pointer over a pointer to the next blocks,
00185  *      and update it.
00186  * - block_ChainRelease : release a chain of block
00187  * - block_ChainExtract : extract data from a chain, return real bytes counts
00188  * - block_ChainGather : gather a chain, free it and return one block.
00189  ****************************************************************************/
00190 static inline void block_ChainAppend( block_t **pp_list, block_t *p_block )
00191 {
00192     if( *pp_list == NULL )
00193     {
00194         *pp_list = p_block;
00195     }
00196     else
00197     {
00198         block_t *p = *pp_list;
00199 
00200         while( p->p_next ) p = p->p_next;
00201         p->p_next = p_block;
00202     }
00203 }
00204 
00205 static inline void block_ChainLastAppend( block_t ***ppp_last, block_t *p_block )
00206 {
00207     block_t *p_last = p_block;
00208 
00209     **ppp_last = p_block;
00210 
00211     while( p_last->p_next ) p_last = p_last->p_next;
00212     *ppp_last = &p_last->p_next;
00213 }
00214 
00215 static inline void block_ChainRelease( block_t *p_block )
00216 {
00217     while( p_block )
00218     {
00219         block_t *p_next = p_block->p_next;
00220         block_Release( p_block );
00221         p_block = p_next;
00222     }
00223 }
00224 
00225 static size_t block_ChainExtract( block_t *p_list, void *p_data, size_t i_max )
00226 {
00227     size_t  i_total = 0;
00228     uint8_t *p = (uint8_t*)p_data;
00229 
00230     while( p_list && i_max )
00231     {
00232         size_t i_copy = __MIN( i_max, p_list->i_buffer );
00233         memcpy( p, p_list->p_buffer, i_copy );
00234         i_max   -= i_copy;
00235         i_total += i_copy;
00236         p       += i_copy;
00237 
00238         p_list = p_list->p_next;
00239     }
00240     return i_total;
00241 }
00242 
00243 static inline void block_ChainProperties( block_t *p_list, int *pi_count, size_t *pi_size, mtime_t *pi_length )
00244 {
00245     size_t i_size = 0;
00246     mtime_t i_length = 0;
00247     int i_count = 0;
00248 
00249     while( p_list )
00250     {
00251         i_size += p_list->i_buffer;
00252         i_length += p_list->i_length;
00253         i_count++;
00254 
00255         p_list = p_list->p_next;
00256     }
00257 
00258     if( pi_size )
00259         *pi_size = i_size;
00260     if( pi_length )
00261         *pi_length = i_length;
00262     if( pi_count )
00263         *pi_count = i_count;
00264 }
00265 
00266 static inline block_t *block_ChainGather( block_t *p_list )
00267 {
00268     size_t  i_total = 0;
00269     mtime_t i_length = 0;
00270     block_t *g;
00271 
00272     if( p_list->p_next == NULL )
00273         return p_list;  /* Already gathered */
00274 
00275     block_ChainProperties( p_list, NULL, &i_total, &i_length );
00276 
00277     g = block_Alloc( i_total );
00278     block_ChainExtract( p_list, g->p_buffer, g->i_buffer );
00279 
00280     g->i_flags = p_list->i_flags;
00281     g->i_pts   = p_list->i_pts;
00282     g->i_dts   = p_list->i_dts;
00283     g->i_length = i_length;
00284 
00285     /* free p_list */
00286     block_ChainRelease( p_list );
00287     return g;
00288 }
00289 
00290 /****************************************************************************
00291  * Fifos of blocks.
00292  ****************************************************************************
00293  * - block_FifoNew : create and init a new fifo
00294  * - block_FifoRelease : destroy a fifo and free all blocks in it.
00295  * - block_FifoPace : wait for a fifo to drain to a specified number of packets or total data size
00296  * - block_FifoEmpty : free all blocks in a fifo
00297  * - block_FifoPut : put a block
00298  * - block_FifoGet : get a packet from the fifo (and wait if it is empty)
00299  * - block_FifoShow : show the first packet of the fifo (and wait if
00300  *      needed), be carefull, you can use it ONLY if you are sure to be the
00301  *      only one getting data from the fifo.
00302  * - block_FifoCount : how many packets are waiting in the fifo
00303  * - block_FifoWake : wake ups a thread with block_FifoGet() = NULL
00304  *   (this is used to wakeup a thread when there is no data to queue)
00305  *
00306  * block_FifoGet and block_FifoShow are cancellation points.
00307  ****************************************************************************/
00308 
00309 VLC_API block_fifo_t * block_FifoNew( void ) VLC_USED;
00310 VLC_API void block_FifoRelease( block_fifo_t * );
00311 VLC_API void block_FifoPace( block_fifo_t *fifo, size_t max_depth, size_t max_size );
00312 VLC_API void block_FifoEmpty( block_fifo_t * );
00313 VLC_API size_t block_FifoPut( block_fifo_t *, block_t * );
00314 VLC_API void block_FifoWake( block_fifo_t * );
00315 VLC_API block_t * block_FifoGet( block_fifo_t * ) VLC_USED;
00316 VLC_API block_t * block_FifoShow( block_fifo_t * );
00317 size_t block_FifoSize( const block_fifo_t *p_fifo ) VLC_USED;
00318 VLC_API size_t block_FifoCount( const block_fifo_t *p_fifo ) VLC_USED;
00319 
00320 #endif /* VLC_BLOCK_H */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines