vlc_block.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * vlc_block.h: Data blocks management functions
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: 4b629fc87d7d41912b6c6c06f02708bd245c70e9 $
00006  *
00007  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 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 /****************************************************************************
00034  * block:
00035  ****************************************************************************
00036  * - block_sys_t is opaque and thus block_t->p_sys is PRIVATE
00037  * - i_flags may not always be set (ie could be 0, even for a key frame
00038  *      it depends where you receive the buffer (before/after a packetizer
00039  *      and the demux/packetizer implementations.
00040  * - i_dts/i_pts could be VLC_TS_INVALID, it means no pts/dts
00041  * - i_length: length in microseond of the packet, can be null except in the
00042  *      sout where it is mandatory.
00043  * - i_rate 0 or a valid input rate, look at vlc_input.h
00044  *
00045  * - i_buffer number of valid data pointed by p_buffer
00046  *      you can freely decrease it but never increase it yourself
00047  *      (use block_Realloc)
00048  * - p_buffer: pointer over datas. You should never overwrite it, you can
00049  *   only incremment it to skip datas, in others cases use block_Realloc
00050  *   (don't duplicate yourself in a bigger buffer, block_Realloc is
00051  *   optimised for prehader/postdatas increase)
00052  ****************************************************************************/
00053 typedef struct block_sys_t block_sys_t;
00054 
00055 /** The content doesn't follow the last block, or is probably broken */
00056 #define BLOCK_FLAG_DISCONTINUITY 0x0001
00057 /** Intra frame */
00058 #define BLOCK_FLAG_TYPE_I        0x0002
00059 /** Inter frame with backward reference only */
00060 #define BLOCK_FLAG_TYPE_P        0x0004
00061 /** Inter frame with backward and forward reference */
00062 #define BLOCK_FLAG_TYPE_B        0x0008
00063 /** For inter frame when you don't know the real type */
00064 #define BLOCK_FLAG_TYPE_PB       0x0010
00065 /** Warn that this block is a header one */
00066 #define BLOCK_FLAG_HEADER        0x0020
00067 /** This is the last block of the frame */
00068 #define BLOCK_FLAG_END_OF_FRAME  0x0040
00069 /** This is not a key frame for bitrate shaping */
00070 #define BLOCK_FLAG_NO_KEYFRAME   0x0080
00071 /** This block contains the last part of a sequence  */
00072 #define BLOCK_FLAG_END_OF_SEQUENCE 0x0100
00073 /** This block contains a clock reference */
00074 #define BLOCK_FLAG_CLOCK         0x0200
00075 /** This block is scrambled */
00076 #define BLOCK_FLAG_SCRAMBLED     0x0400
00077 /** This block has to be decoded but not be displayed */
00078 #define BLOCK_FLAG_PREROLL       0x0800
00079 /** This block is corrupted and/or there is data loss  */
00080 #define BLOCK_FLAG_CORRUPTED     0x1000
00081 /** This block contains an interlaced picture with top field first */
00082 #define BLOCK_FLAG_TOP_FIELD_FIRST 0x2000
00083 /** This block contains an interlaced picture with bottom field first */
00084 #define BLOCK_FLAG_BOTTOM_FIELD_FIRST 0x4000
00085 
00086 /** This block contains an interlaced picture */
00087 #define BLOCK_FLAG_INTERLACED_MASK \
00088     (BLOCK_FLAG_TOP_FIELD_FIRST|BLOCK_FLAG_BOTTOM_FIELD_FIRST)
00089 
00090 #define BLOCK_FLAG_TYPE_MASK \
00091     (BLOCK_FLAG_TYPE_I|BLOCK_FLAG_TYPE_P|BLOCK_FLAG_TYPE_B|BLOCK_FLAG_TYPE_PB)
00092 
00093 /* These are for input core private usage only */
00094 #define BLOCK_FLAG_CORE_PRIVATE_MASK  0x00ff0000
00095 #define BLOCK_FLAG_CORE_PRIVATE_SHIFT 16
00096 
00097 /* These are for module private usage only */
00098 #define BLOCK_FLAG_PRIVATE_MASK  0xff000000
00099 #define BLOCK_FLAG_PRIVATE_SHIFT 24
00100 
00101 typedef void (*block_free_t) (block_t *);
00102 
00103 struct block_t
00104 {
00105     block_t     *p_next;
00106 
00107     uint32_t    i_flags;
00108 
00109     mtime_t     i_pts;
00110     mtime_t     i_dts;
00111     mtime_t     i_length;
00112 
00113     unsigned    i_nb_samples; /* Used for audio */
00114     int         i_rate;
00115 
00116     size_t      i_buffer;
00117     uint8_t     *p_buffer;
00118 
00119     /* Rudimentary support for overloading block (de)allocation. */
00120     block_free_t pf_release;
00121 };
00122 
00123 /****************************************************************************
00124  * Blocks functions:
00125  ****************************************************************************
00126  * - block_Alloc : create a new block with the requested size ( >= 0 ), return
00127  *      NULL for failure.
00128  * - block_Release : release a block allocated with block_Alloc.
00129  * - block_Realloc : realloc a block,
00130  *      i_pre: how many bytes to insert before body if > 0, else how many
00131  *      bytes of body to skip (the latter can be done without using
00132  *      block_Realloc i_buffer -= -i_pre, p_buffer += -i_pre as i_pre < 0)
00133  *      i_body (>= 0): the final size of the body (decreasing it can directly
00134  *      be done with i_buffer = i_body).
00135  *      with preheader and or body (increase
00136  *      and decrease are supported). Use it as it is optimised.
00137  * - block_Duplicate : create a copy of a block.
00138  ****************************************************************************/
00139 VLC_EXPORT( void,      block_Init,    ( block_t *, void *, size_t ) );
00140 VLC_EXPORT( block_t *, block_Alloc,   ( size_t ) LIBVLC_USED );
00141 VLC_EXPORT( block_t *, block_Realloc, ( block_t *, ssize_t i_pre, size_t i_body ) LIBVLC_USED );
00142 
00143 #define block_New( dummy, size ) block_Alloc(size)
00144 
00145 LIBVLC_USED
00146 static inline block_t *block_Duplicate( block_t *p_block )
00147 {
00148     block_t *p_dup = block_Alloc( p_block->i_buffer );
00149     if( p_dup == NULL )
00150         return NULL;
00151 
00152     p_dup->i_dts     = p_block->i_dts;
00153     p_dup->i_pts     = p_block->i_pts;
00154     p_dup->i_flags   = p_block->i_flags;
00155     p_dup->i_length  = p_block->i_length;
00156     p_dup->i_rate    = p_block->i_rate;
00157     p_dup->i_nb_samples = p_block->i_nb_samples;
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_EXPORT( block_t *, block_heap_Alloc, (void *, void *, size_t) LIBVLC_USED );
00169 VLC_EXPORT( block_t *, block_mmap_Alloc, (void *addr, size_t length) LIBVLC_USED );
00170 VLC_EXPORT( block_t *, block_File, (int fd) LIBVLC_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
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_FifoEmpty : free all blocks in a fifo
00296  * - block_FifoPut : put a block
00297  * - block_FifoGet : get a packet from the fifo (and wait if it is empty)
00298  * - block_FifoShow : show the first packet of the fifo (and wait if
00299  *      needed), be carefull, you can use it ONLY if you are sure to be the
00300  *      only one getting data from the fifo.
00301  * - block_FifoCount : how many packets are waiting in the fifo
00302  * - block_FifoWake : wake ups a thread with block_FifoGet() = NULL
00303  *   (this is used to wakeup a thread when there is no data to queue)
00304  *
00305  * block_FifoGet and block_FifoShow are cancellation points.
00306  ****************************************************************************/
00307 
00308 VLC_EXPORT( block_fifo_t *, block_FifoNew,      ( void ) LIBVLC_USED );
00309 VLC_EXPORT( void,           block_FifoRelease,  ( block_fifo_t * ) );
00310 /* TODO: do we need to export this? */
00311 void block_FifoPace (block_fifo_t *fifo, size_t max_depth, size_t max_size);
00312 VLC_EXPORT( void,           block_FifoEmpty,    ( block_fifo_t * ) );
00313 VLC_EXPORT( size_t,         block_FifoPut,      ( block_fifo_t *, block_t * ) );
00314 VLC_EXPORT( void,           block_FifoWake,     ( block_fifo_t * ) );
00315 VLC_EXPORT( block_t *,      block_FifoGet,      ( block_fifo_t * ) LIBVLC_USED );
00316 VLC_EXPORT( block_t *,      block_FifoShow,     ( block_fifo_t * ) );
00317 size_t block_FifoSize( const block_fifo_t *p_fifo ) LIBVLC_USED;
00318 VLC_EXPORT( size_t,         block_FifoCount,    ( const block_fifo_t *p_fifo ) LIBVLC_USED );
00319 
00320 #endif /* VLC_BLOCK_H */

Generated on Sun Nov 22 08:05:12 2009 for VLC by  doxygen 1.5.6