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

block.c

00001 /*****************************************************************************
00002  * block.c: Data blocks management functions
00003  *****************************************************************************
00004  * Copyright (C) 2003-2004 the VideoLAN team
00005  * $Id: block.c 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 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>
00028 
00029 #include <vlc/vlc.h>
00030 #include "vlc_block.h"
00031 
00032 /*****************************************************************************
00033  * Block functions.
00034  *****************************************************************************/
00035 /* private */
00036 struct block_sys_t
00037 {
00038     uint8_t     *p_allocated_buffer;
00039     int         i_allocated_buffer;
00040 };
00041 
00042 #define BLOCK_PADDING_SIZE 32
00043 static void BlockRelease( block_t * );
00044 
00045 block_t *__block_New( vlc_object_t *p_obj, int i_size )
00046 {
00047     /* We do only one malloc
00048      * TODO bench if doing 2 malloc but keeping a pool of buffer is better
00049      * 16 -> align on 16
00050      * 2 * BLOCK_PADDING_SIZE -> pre + post padding
00051      */
00052     block_sys_t *p_sys;
00053     const int i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + 16;
00054     block_t *p_block =
00055         malloc( sizeof( block_t ) + sizeof( block_sys_t ) + i_alloc );
00056 
00057     if( p_block == NULL ) return NULL;
00058 
00059     /* Fill opaque data */
00060     p_sys = (block_sys_t*)( (uint8_t*)p_block + sizeof( block_t ) );
00061     p_sys->i_allocated_buffer = i_alloc;
00062     p_sys->p_allocated_buffer = (uint8_t*)p_block + sizeof( block_t ) +
00063         sizeof( block_sys_t );
00064 
00065     /* Fill all fields */
00066     p_block->p_next         = NULL;
00067     p_block->p_prev         = NULL;
00068     p_block->i_flags        = 0;
00069     p_block->i_pts          = 0;
00070     p_block->i_dts          = 0;
00071     p_block->i_length       = 0;
00072     p_block->i_rate         = 0;
00073     p_block->i_buffer       = i_size;
00074     p_block->p_buffer       =
00075         &p_sys->p_allocated_buffer[BLOCK_PADDING_SIZE +
00076             16 - ((uintptr_t)p_sys->p_allocated_buffer % 16 )];
00077     p_block->pf_release     = BlockRelease;
00078 
00079     /* Is ok, as no comunication between p_vlc */
00080     p_block->p_manager      = VLC_OBJECT( p_obj->p_vlc );
00081     p_block->p_sys          = p_sys;
00082 
00083     return p_block;
00084 }
00085 
00086 block_t *block_Realloc( block_t *p_block, int i_prebody, int i_body )
00087 {
00088     int i_buffer_size;
00089 
00090     if( p_block->pf_release != BlockRelease )
00091     {
00092         /* Special case when pf_release if overloaded
00093          * TODO if used one day, them implement it in a smarter way */
00094         block_t *p_dup = block_Duplicate( p_block );
00095         block_Release( p_block );
00096 
00097         p_block = p_dup;
00098     }
00099 
00100     i_buffer_size = i_prebody + i_body;
00101 
00102     if( i_body < 0 || i_buffer_size <= 0 ) return NULL;
00103 
00104     if( p_block->p_buffer - i_prebody > p_block->p_sys->p_allocated_buffer &&
00105         p_block->p_buffer - i_prebody < p_block->p_sys->p_allocated_buffer +
00106         p_block->p_sys->i_allocated_buffer )
00107     {
00108         p_block->p_buffer -= i_prebody;
00109         p_block->i_buffer += i_prebody;
00110         i_prebody = 0;
00111     }
00112     if( p_block->p_buffer + i_body < p_block->p_sys->p_allocated_buffer +
00113         p_block->p_sys->i_allocated_buffer )
00114     {
00115         p_block->i_buffer = i_buffer_size;
00116         i_body = 0;
00117     }
00118 
00119     if( i_body > 0 || i_prebody > 0 )
00120     {
00121         block_t *p_rea = block_New( p_block->p_manager, i_buffer_size );
00122 
00123         p_rea->i_dts     = p_block->i_dts;
00124         p_rea->i_pts     = p_block->i_pts;
00125         p_rea->i_flags   = p_block->i_flags;
00126         p_rea->i_length  = p_block->i_length;
00127         p_rea->i_rate    = p_block->i_rate;
00128         p_rea->i_samples = p_block->i_samples;
00129 
00130         memcpy( p_rea->p_buffer + i_prebody, p_block->p_buffer,
00131                 __MIN( p_block->i_buffer, p_rea->i_buffer - i_prebody ) );
00132 
00133         block_Release( p_block );
00134 
00135         return p_rea;
00136     }
00137 
00138     return p_block;
00139 }
00140 
00141 static void BlockRelease( block_t *p_block )
00142 {
00143     free( p_block );
00144 }
00145 
00146 
00147 /*****************************************************************************
00148  * block_fifo_t management
00149  *****************************************************************************/
00150 block_fifo_t *__block_FifoNew( vlc_object_t *p_obj )
00151 {
00152     block_fifo_t *p_fifo;
00153 
00154     p_fifo = malloc( sizeof( vlc_object_t ) );
00155     vlc_mutex_init( p_obj, &p_fifo->lock );
00156     vlc_cond_init( p_obj, &p_fifo->wait );
00157     p_fifo->i_depth = p_fifo->i_size = 0;
00158     p_fifo->p_first = NULL;
00159     p_fifo->pp_last = &p_fifo->p_first;
00160 
00161     return p_fifo;
00162 }
00163 
00164 void block_FifoRelease( block_fifo_t *p_fifo )
00165 {
00166     block_FifoEmpty( p_fifo );
00167     vlc_cond_destroy( &p_fifo->wait );
00168     vlc_mutex_destroy( &p_fifo->lock );
00169     free( p_fifo );
00170 }
00171 
00172 void block_FifoEmpty( block_fifo_t *p_fifo )
00173 {
00174     block_t *b;
00175 
00176     vlc_mutex_lock( &p_fifo->lock );
00177     for( b = p_fifo->p_first; b != NULL; )
00178     {
00179         block_t *p_next;
00180 
00181         p_next = b->p_next;
00182         block_Release( b );
00183         b = p_next;
00184     }
00185 
00186     p_fifo->i_depth = p_fifo->i_size = 0;
00187     p_fifo->p_first = NULL;
00188     p_fifo->pp_last = &p_fifo->p_first;
00189     vlc_mutex_unlock( &p_fifo->lock );
00190 }
00191 
00192 int block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
00193 {
00194     int i_size = 0;
00195     vlc_mutex_lock( &p_fifo->lock );
00196 
00197     do
00198     {
00199         i_size += p_block->i_buffer;
00200 
00201         *p_fifo->pp_last = p_block;
00202         p_fifo->pp_last = &p_block->p_next;
00203         p_fifo->i_depth++;
00204         p_fifo->i_size += p_block->i_buffer;
00205 
00206         p_block = p_block->p_next;
00207 
00208     } while( p_block );
00209 
00210     /* warn there is data in this fifo */
00211     vlc_cond_signal( &p_fifo->wait );
00212     vlc_mutex_unlock( &p_fifo->lock );
00213 
00214     return i_size;
00215 }
00216 
00217 block_t *block_FifoGet( block_fifo_t *p_fifo )
00218 {
00219     block_t *b;
00220 
00221     vlc_mutex_lock( &p_fifo->lock );
00222 
00223     /* We do a while here because there is a race condition in the
00224      * win32 implementation of vlc_cond_wait() (We can't be sure the fifo
00225      * hasn't been emptied again since we were signaled). */
00226     while( p_fifo->p_first == NULL )
00227     {
00228         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
00229     }
00230 
00231     b = p_fifo->p_first;
00232 
00233     p_fifo->p_first = b->p_next;
00234     p_fifo->i_depth--;
00235     p_fifo->i_size -= b->i_buffer;
00236 
00237     if( p_fifo->p_first == NULL )
00238     {
00239         p_fifo->pp_last = &p_fifo->p_first;
00240     }
00241 
00242     vlc_mutex_unlock( &p_fifo->lock );
00243 
00244     b->p_next = NULL;
00245     return b;
00246 }
00247 
00248 block_t *block_FifoShow( block_fifo_t *p_fifo )
00249 {
00250     block_t *b;
00251 
00252     vlc_mutex_lock( &p_fifo->lock );
00253 
00254     if( p_fifo->p_first == NULL )
00255     {
00256         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
00257     }
00258 
00259     b = p_fifo->p_first;
00260 
00261     vlc_mutex_unlock( &p_fifo->lock );
00262 
00263     return( b );
00264 
00265 }
00266 

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