Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

vlc_block.h

00001 /*****************************************************************************
00002  * vlc_block.h: Data blocks management functions
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: vlc_block.h 13022 2005-10-29 18:11:46Z md $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 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 General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 #ifndef _VLC_BLOCK_H
00025 #define _VLC_BLOCK_H 1
00026 
00027 /****************************************************************************
00028  * block:
00029  ****************************************************************************
00030  * - block_sys_t is opaque and thus block_t->p_sys is PRIVATE
00031  * - i_flags may not always be set (ie could be 0, even for a key frame
00032  *      it depends where you receive the buffer (before/after a packetizer
00033  *      and the demux/packetizer implementations.
00034  * - i_dts/i_pts could be 0, it means no pts
00035  * - i_length: length in microseond of the packet, can be null except in the
00036  *      sout where it is mandatory.
00037  * - i_rate 0 or a valid input rate, look at vlc_input.h
00038  *
00039  * - i_buffer number of valid data pointed by p_buffer
00040  *      you can freely decrease it but never increase it yourself
00041  *      (use block_Realloc)
00042  * - p_buffer: pointer over datas. You should never overwrite it, you can
00043  *   only incremment it to skip datas, in others cases use block_Realloc
00044  *   (don't duplicate yourself in a bigger buffer, block_Realloc is
00045  *   optimised for prehader/postdatas increase)
00046  ****************************************************************************/
00047 typedef struct block_sys_t block_sys_t;
00048 
00050 #define BLOCK_FLAG_DISCONTINUITY 0x0001
00051 
00052 #define BLOCK_FLAG_TYPE_I        0x0002
00053 
00054 #define BLOCK_FLAG_TYPE_P        0x0004
00055 
00056 #define BLOCK_FLAG_TYPE_B        0x0008
00057 
00058 #define BLOCK_FLAG_TYPE_PB       0x0010
00059 
00060 #define BLOCK_FLAG_HEADER        0x0020
00061 
00062 #define BLOCK_FLAG_END_OF_FRAME  0x0040
00063 
00064 #define BLOCK_FLAG_NO_KEYFRAME   0x0080
00065 
00066 #define BLOCK_FLAG_CLOCK         0x0200
00067 
00068 #define BLOCK_FLAG_SCRAMBLED     0x0400
00069 
00070 #define BLOCK_FLAG_PREROLL       0x0800
00071 
00072 #define BLOCK_FLAG_CORRUPTED     0x1000
00073 
00074 #define BLOCK_FLAG_PRIVATE_MASK  0xffff0000
00075 #define BLOCK_FLAG_PRIVATE_SHIFT 16
00076 
00077 struct block_t
00078 {
00079     block_t     *p_next;
00080     block_t     *p_prev;
00081 
00082     uint32_t    i_flags;
00083 
00084     mtime_t     i_pts;
00085     mtime_t     i_dts;
00086     mtime_t     i_length;
00087 
00088     int         i_samples; /* Used for audio */
00089     int         i_rate;
00090 
00091     int         i_buffer;
00092     uint8_t     *p_buffer;
00093 
00094     /* This way the block_Release can be overloaded
00095      * Don't mess with it now, if you need it the ask on ML
00096      */
00097     void        (*pf_release)   ( block_t * );
00098 
00099     /* It's an object that should be valid as long as the block_t is valid */
00100     /* It should become a true block manager to reduce malloc/free */
00101     vlc_object_t    *p_manager;
00102 
00103     /* Following fields are private, user should never touch it */
00104     /* XXX never touch that OK !!! the first that access that will
00105      * have cvs account removed ;) XXX */
00106     block_sys_t *p_sys;
00107 };
00108 
00109 /****************************************************************************
00110  * Blocks functions:
00111  ****************************************************************************
00112  * - block_New : create a new block with the requested size ( >= 0 ), return
00113  *      NULL for failure.
00114  * - block_Release : release a block allocated with block_New.
00115  * - block_Realloc : realloc a block,
00116  *      i_pre: how many bytes to insert before body if > 0, else how many
00117  *      bytes of body to skip (the latter can be done without using
00118  *      block_Realloc i_buffer -= -i_pre, p_buffer += -i_pre as i_pre < 0)
00119  *      i_body (>= 0): the final size of the body (decreasing it can directly
00120  *      be done with i_buffer = i_body).
00121  *      with preheader and or body (increase
00122  *      and decrease are supported). Use it as it is optimised.
00123  * - block_Duplicate : create a copy of a block.
00124  ****************************************************************************/
00125 #define block_New( a, b ) __block_New( VLC_OBJECT(a), b )
00126 VLC_EXPORT( block_t *,  __block_New,        ( vlc_object_t *, int ) );
00127 VLC_EXPORT( block_t *, block_Realloc,       ( block_t *, int i_pre, int i_body ) );
00128 
00129 static inline block_t *block_Duplicate( block_t *p_block )
00130 {
00131     block_t *p_dup = block_New( p_block->p_manager, p_block->i_buffer );
00132 
00133     p_dup->i_dts     = p_block->i_dts;
00134     p_dup->i_pts     = p_block->i_pts;
00135     p_dup->i_flags   = p_block->i_flags;
00136     p_dup->i_length  = p_block->i_length;
00137     p_dup->i_rate    = p_block->i_rate;
00138     p_dup->i_samples = p_block->i_samples;
00139 
00140     if( p_dup && p_block->i_buffer > 0 )
00141         memcpy( p_dup->p_buffer, p_block->p_buffer, p_block->i_buffer );
00142 
00143     return p_dup;
00144 }
00145 static inline void block_Release( block_t *p_block )
00146 {
00147     p_block->pf_release( p_block );
00148 }
00149 
00150 /****************************************************************************
00151  * Chains of blocks functions helper
00152  ****************************************************************************
00153  * - block_ChainAppend : append a block the the last block of a chain. Try to
00154  *      avoid using with a lot of data as it's really slow, prefer
00155  *      block_ChainLastAppend
00156  * - block_ChainLastAppend : use a pointer over a pointer to the next blocks,
00157  *      and update it.
00158  * - block_ChainRelease : release a chain of block
00159  * - block_ChainExtract : extract data from a chain, return real bytes counts
00160  * - block_ChainGather : gather a chain, free it and return a block.
00161  ****************************************************************************/
00162 static inline void block_ChainAppend( block_t **pp_list, block_t *p_block )
00163 {
00164     if( *pp_list == NULL )
00165     {
00166         *pp_list = p_block;
00167     }
00168     else
00169     {
00170         block_t *p = *pp_list;
00171 
00172         while( p->p_next ) p = p->p_next;
00173         p->p_next = p_block;
00174     }
00175 }
00176 
00177 static inline void block_ChainLastAppend( block_t ***ppp_last, block_t *p_block  )
00178 {
00179     block_t *p_last = p_block;
00180 
00181     **ppp_last = p_block;
00182 
00183     while( p_last->p_next ) p_last = p_last->p_next;
00184     *ppp_last = &p_last->p_next;
00185 }
00186 
00187 static inline void block_ChainRelease( block_t *p_block )
00188 {
00189     while( p_block )
00190     {
00191         block_t *p_next = p_block->p_next;
00192         block_Release( p_block );
00193         p_block = p_next;
00194     }
00195 }
00196 static int block_ChainExtract( block_t *p_list, void *p_data, int i_max )
00197 {
00198     block_t *b;
00199     int     i_total = 0;
00200     uint8_t *p = (uint8_t*)p_data;
00201 
00202     for( b = p_list; b != NULL; b = b->p_next )
00203     {
00204         int i_copy = __MIN( i_max, b->i_buffer );
00205         if( i_copy > 0 )
00206         {
00207             memcpy( p, b->p_buffer, i_copy );
00208             i_max   -= i_copy;
00209             i_total += i_copy;
00210             p       += i_copy;
00211 
00212             if( i_max == 0 )
00213                 return i_total;
00214         }
00215     }
00216     return i_total;
00217 }
00218 
00219 static inline block_t *block_ChainGather( block_t *p_list )
00220 {
00221     int     i_total = 0;
00222     mtime_t i_length = 0;
00223     block_t *b, *g;
00224 
00225     if( p_list->p_next == NULL )
00226         return p_list;  /* Already gathered */
00227 
00228     for( b = p_list; b != NULL; b = b->p_next )
00229     {
00230         i_total += b->i_buffer;
00231         i_length += b->i_length;
00232     }
00233 
00234     g = block_New( p_list->p_manager, i_total );
00235     block_ChainExtract( p_list, g->p_buffer, g->i_buffer );
00236 
00237     g->i_flags = p_list->i_flags;
00238     g->i_pts   = p_list->i_pts;
00239     g->i_dts   = p_list->i_dts;
00240     g->i_length = i_length;
00241 
00242     /* free p_list */
00243     block_ChainRelease( p_list );
00244     return g;
00245 }
00246 
00247 
00248 /****************************************************************************
00249  * Fifos of blocks.
00250  ****************************************************************************
00251  * Avoid touching block_fifo_t unless you really know what you are doing.
00252  * ( Some race conditions has to be correctly handled, like in win32 ;)
00253  * - block_FifoNew : create and init a new fifo
00254  * - block_FifoRelease : destroy a fifo and free all blocks in it.
00255  * - block_FifoEmpty : free all blocks in a fifo
00256  * - block_FifoPut : put a block
00257  * - block_FifoGet : get a packet from the fifo (and wait if it is empty)
00258  * - block_FifoShow : show the first packet of the fifo (and wait if
00259  *      needed), becarefull, you can use it ONLY if you are sure to be the
00260  *      only one getting data from the fifo.
00261  ****************************************************************************/
00262 struct block_fifo_t
00263 {
00264     vlc_mutex_t         lock;                         /* fifo data lock */
00265     vlc_cond_t          wait;         /* fifo data conditional variable */
00266 
00267     int                 i_depth;
00268     block_t             *p_first;
00269     block_t             **pp_last;
00270     int                 i_size;
00271 };
00272 
00273 
00274 #define block_FifoNew( a ) __block_FifoNew( VLC_OBJECT(a) )
00275 VLC_EXPORT( block_fifo_t *, __block_FifoNew,    ( vlc_object_t * ) );
00276 VLC_EXPORT( void,           block_FifoRelease,  ( block_fifo_t * ) );
00277 VLC_EXPORT( void,           block_FifoEmpty,    ( block_fifo_t * ) );
00278 VLC_EXPORT( int,            block_FifoPut,      ( block_fifo_t *, block_t * ) );
00279 VLC_EXPORT( block_t *,      block_FifoGet,      ( block_fifo_t * ) );
00280 VLC_EXPORT( block_t *,      block_FifoShow,     ( block_fifo_t * ) );
00281 
00282 #endif /* VLC_BLOCK_H */

Generated on Tue Dec 20 10:14:19 2005 for vlc-0.8.4a by  doxygen 1.4.2