- Only allow letters, digits and slash in access/demux specification
[vlc.git] / src / misc / block.c
1 /*****************************************************************************
2  * block.c: Data blocks management functions
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include "vlc_block.h"
31
32 /*****************************************************************************
33  * Block functions.
34  *****************************************************************************/
35 /* private */
36 struct block_sys_t
37 {
38     uint8_t     *p_allocated_buffer;
39     int         i_allocated_buffer;
40 };
41
42 #define BLOCK_PADDING_SIZE 32
43 static void BlockRelease( block_t * );
44
45 block_t *__block_New( vlc_object_t *p_obj, int i_size )
46 {
47     /* We do only one malloc
48      * TODO bench if doing 2 malloc but keeping a pool of buffer is better
49      * 16 -> align on 16
50      * 2 * BLOCK_PADDING_SIZE -> pre + post padding
51      */
52     block_sys_t *p_sys;
53     const int i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + 16;
54     block_t *p_block =
55         malloc( sizeof( block_t ) + sizeof( block_sys_t ) + i_alloc );
56
57     if( p_block == NULL )
58         return NULL;
59
60     /* Fill opaque data */
61     p_sys = (block_sys_t*)( (uint8_t*)p_block + sizeof( block_t ) );
62     p_sys->i_allocated_buffer = i_alloc;
63     p_sys->p_allocated_buffer = (uint8_t*)p_block + sizeof( block_t ) +
64         sizeof( block_sys_t );
65
66     /* Fill all fields */
67     p_block->p_next         = NULL;
68     p_block->p_prev         = NULL;
69     p_block->i_flags        = 0;
70     p_block->i_pts          = 0;
71     p_block->i_dts          = 0;
72     p_block->i_length       = 0;
73     p_block->i_rate         = 0;
74     p_block->i_buffer       = i_size;
75     p_block->p_buffer       =
76         &p_sys->p_allocated_buffer[BLOCK_PADDING_SIZE +
77             16 - ((uintptr_t)p_sys->p_allocated_buffer % 16 )];
78     p_block->pf_release     = BlockRelease;
79
80     /* Is ok, as no comunication between p_vlc */
81     p_block->p_manager      = VLC_OBJECT( p_obj->p_libvlc );
82     p_block->p_sys          = p_sys;
83
84     return p_block;
85 }
86
87 block_t *block_Realloc( block_t *p_block, int i_prebody, int i_body )
88 {
89     int i_buffer_size;
90
91     if( p_block->pf_release != BlockRelease )
92     {
93         /* Special case when pf_release if overloaded
94          * TODO if used one day, them implement it in a smarter way */
95         block_t *p_dup = block_Duplicate( p_block );
96         block_Release( p_block );
97         if( !p_dup )
98             return NULL;
99
100         p_block = p_dup;
101     }
102
103     i_buffer_size = i_prebody + i_body;
104
105     if( i_body < 0 || i_buffer_size <= 0 )
106     {
107         block_Release( p_block );
108         return NULL;
109     }
110
111     if( p_block->p_buffer - i_prebody > p_block->p_sys->p_allocated_buffer &&
112         p_block->p_buffer - i_prebody < p_block->p_sys->p_allocated_buffer +
113         p_block->p_sys->i_allocated_buffer )
114     {
115         p_block->p_buffer -= i_prebody;
116         p_block->i_buffer += i_prebody;
117         i_prebody = 0;
118     }
119     if( p_block->p_buffer + i_body < p_block->p_sys->p_allocated_buffer +
120         p_block->p_sys->i_allocated_buffer )
121     {
122         p_block->i_buffer = i_buffer_size;
123         i_body = 0;
124     }
125
126     if( i_body > 0 || i_prebody > 0 )
127     {
128         block_t *p_rea = block_New( p_block->p_manager, i_buffer_size );
129
130         if( p_rea )
131         {
132             p_rea->i_dts     = p_block->i_dts;
133             p_rea->i_pts     = p_block->i_pts;
134             p_rea->i_flags   = p_block->i_flags;
135             p_rea->i_length  = p_block->i_length;
136             p_rea->i_rate    = p_block->i_rate;
137             p_rea->i_samples = p_block->i_samples;
138
139             memcpy( p_rea->p_buffer + i_prebody, p_block->p_buffer,
140                     __MIN( p_block->i_buffer, p_rea->i_buffer - i_prebody ) );
141         }
142
143         block_Release( p_block );
144
145         return p_rea;
146     }
147
148     return p_block;
149 }
150
151 static void BlockRelease( block_t *p_block )
152 {
153     free( p_block );
154 }
155
156
157 /*****************************************************************************
158  * block_fifo_t management
159  *****************************************************************************/
160 block_fifo_t *__block_FifoNew( vlc_object_t *p_obj )
161 {
162     block_fifo_t *p_fifo;
163
164     p_fifo = malloc( sizeof( block_fifo_t ) );
165     vlc_mutex_init( p_obj, &p_fifo->lock );
166     vlc_cond_init( p_obj, &p_fifo->wait );
167     p_fifo->i_depth = p_fifo->i_size = 0;
168     p_fifo->p_first = NULL;
169     p_fifo->pp_last = &p_fifo->p_first;
170
171     return p_fifo;
172 }
173
174 void block_FifoRelease( block_fifo_t *p_fifo )
175 {
176     block_FifoEmpty( p_fifo );
177     vlc_cond_destroy( &p_fifo->wait );
178     vlc_mutex_destroy( &p_fifo->lock );
179     free( p_fifo );
180 }
181
182 void block_FifoEmpty( block_fifo_t *p_fifo )
183 {
184     block_t *b;
185
186     vlc_mutex_lock( &p_fifo->lock );
187     for( b = p_fifo->p_first; b != NULL; )
188     {
189         block_t *p_next;
190
191         p_next = b->p_next;
192         block_Release( b );
193         b = p_next;
194     }
195
196     p_fifo->i_depth = p_fifo->i_size = 0;
197     p_fifo->p_first = NULL;
198     p_fifo->pp_last = &p_fifo->p_first;
199     vlc_mutex_unlock( &p_fifo->lock );
200 }
201
202 int block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
203 {
204     int i_size = 0;
205     vlc_mutex_lock( &p_fifo->lock );
206
207     do
208     {
209         i_size += p_block->i_buffer;
210
211         *p_fifo->pp_last = p_block;
212         p_fifo->pp_last = &p_block->p_next;
213         p_fifo->i_depth++;
214         p_fifo->i_size += p_block->i_buffer;
215
216         p_block = p_block->p_next;
217
218     } while( p_block );
219
220     /* warn there is data in this fifo */
221     vlc_cond_signal( &p_fifo->wait );
222     vlc_mutex_unlock( &p_fifo->lock );
223
224     return i_size;
225 }
226
227 block_t *block_FifoGet( block_fifo_t *p_fifo )
228 {
229     block_t *b;
230
231     vlc_mutex_lock( &p_fifo->lock );
232
233     /* We do a while here because there is a race condition in the
234      * win32 implementation of vlc_cond_wait() (We can't be sure the fifo
235      * hasn't been emptied again since we were signaled). */
236     while( p_fifo->p_first == NULL )
237     {
238         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
239     }
240
241     b = p_fifo->p_first;
242
243     p_fifo->p_first = b->p_next;
244     p_fifo->i_depth--;
245     p_fifo->i_size -= b->i_buffer;
246
247     if( p_fifo->p_first == NULL )
248     {
249         p_fifo->pp_last = &p_fifo->p_first;
250     }
251
252     vlc_mutex_unlock( &p_fifo->lock );
253
254     b->p_next = NULL;
255     return b;
256 }
257
258 block_t *block_FifoShow( block_fifo_t *p_fifo )
259 {
260     block_t *b;
261
262     vlc_mutex_lock( &p_fifo->lock );
263
264     if( p_fifo->p_first == NULL )
265     {
266         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
267     }
268
269     b = p_fifo->p_first;
270
271     vlc_mutex_unlock( &p_fifo->lock );
272
273     return( b );
274
275 }
276