5bcdda3495c50525519cd425cf1a233c84d22263
[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 <vlc/vlc.h>
28 #include "vlc_block.h"
29
30 /*****************************************************************************
31  * Block functions.
32  *****************************************************************************/
33 /* private */
34 struct block_sys_t
35 {
36     uint8_t     *p_allocated_buffer;
37     size_t      i_allocated_buffer;
38 };
39
40 #define BLOCK_PADDING_SIZE 32
41 static void BlockRelease( block_t * );
42
43 block_t *__block_New( vlc_object_t *p_obj, size_t i_size )
44 {
45     /* We do only one malloc
46      * TODO bench if doing 2 malloc but keeping a pool of buffer is better
47      * 16 -> align on 16
48      * 2 * BLOCK_PADDING_SIZE -> pre + post padding
49      */
50     block_sys_t *p_sys;
51     const size_t i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + 16;
52     block_t *p_block =
53         malloc( sizeof( block_t ) + sizeof( block_sys_t ) + i_alloc );
54
55     if( p_block == NULL )
56         return NULL;
57
58     /* Fill opaque data */
59     p_sys = (block_sys_t*)( (uint8_t*)p_block + sizeof( block_t ) );
60     p_sys->i_allocated_buffer = i_alloc;
61     p_sys->p_allocated_buffer = (uint8_t*)p_block + sizeof( block_t ) +
62         sizeof( block_sys_t );
63
64     /* Fill all fields */
65     p_block->p_next         = NULL;
66     p_block->p_prev         = NULL;
67     p_block->i_flags        = 0;
68     p_block->i_pts          = 0;
69     p_block->i_dts          = 0;
70     p_block->i_length       = 0;
71     p_block->i_rate         = 0;
72     p_block->i_buffer       = i_size;
73     p_block->p_buffer       =
74         &p_sys->p_allocated_buffer[BLOCK_PADDING_SIZE +
75             16 - ((uintptr_t)p_sys->p_allocated_buffer % 16 )];
76     p_block->pf_release     = BlockRelease;
77
78     /* Is ok, as no comunication between p_vlc */
79     p_block->p_manager      = VLC_OBJECT( p_obj->p_libvlc );
80     p_block->p_sys          = p_sys;
81
82     return p_block;
83 }
84
85 block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
86 {
87     ssize_t i_buffer_size;
88
89     if( p_block->pf_release != BlockRelease )
90     {
91         /* Special case when pf_release if overloaded
92          * TODO if used one day, them implement it in a smarter way */
93         block_t *p_dup = block_Duplicate( p_block );
94         block_Release( p_block );
95         if( !p_dup )
96             return NULL;
97
98         p_block = p_dup;
99     }
100
101     i_buffer_size = i_prebody + i_body;
102
103     if( i_buffer_size <= 0 )
104     {
105         block_Release( p_block );
106         return NULL;
107     }
108
109     if( p_block->p_buffer - i_prebody > p_block->p_sys->p_allocated_buffer &&
110         p_block->p_buffer - i_prebody < p_block->p_sys->p_allocated_buffer +
111         p_block->p_sys->i_allocated_buffer )
112     {
113         p_block->p_buffer -= i_prebody;
114         p_block->i_buffer += i_prebody;
115         i_prebody = 0;
116     }
117     if( p_block->p_buffer + i_body < p_block->p_sys->p_allocated_buffer +
118         p_block->p_sys->i_allocated_buffer )
119     {
120         p_block->i_buffer = i_buffer_size;
121         i_body = 0;
122     }
123
124     if( i_body > 0 || i_prebody > 0 )
125     {
126         block_t *p_rea = block_New( p_block->p_manager, i_buffer_size );
127
128         if( p_rea )
129         {
130             p_rea->i_dts     = p_block->i_dts;
131             p_rea->i_pts     = p_block->i_pts;
132             p_rea->i_flags   = p_block->i_flags;
133             p_rea->i_length  = p_block->i_length;
134             p_rea->i_rate    = p_block->i_rate;
135             p_rea->i_samples = p_block->i_samples;
136
137             memcpy( p_rea->p_buffer + i_prebody, p_block->p_buffer,
138                     __MIN( p_block->i_buffer, p_rea->i_buffer - i_prebody ) );
139         }
140
141         block_Release( p_block );
142
143         return p_rea;
144     }
145
146     return p_block;
147 }
148
149 static void BlockRelease( block_t *p_block )
150 {
151     free( p_block );
152 }
153
154
155 /*****************************************************************************
156  * block_fifo_t management
157  *****************************************************************************/
158 struct block_fifo_t
159 {
160     vlc_mutex_t         lock;                         /* fifo data lock */
161     vlc_cond_t          wait;         /* fifo data conditional variable */
162
163     block_t             *p_first;
164     block_t             **pp_last;
165     size_t              i_depth;
166     size_t              i_size;
167     vlc_bool_t          b_force_wake;
168 };
169
170 block_fifo_t *__block_FifoNew( vlc_object_t *p_obj )
171 {
172     block_fifo_t *p_fifo;
173
174     p_fifo = malloc( sizeof( block_fifo_t ) );
175     if( !p_fifo ) return NULL;
176     vlc_mutex_init( p_obj, &p_fifo->lock );
177     vlc_cond_init( p_obj, &p_fifo->wait );
178     p_fifo->p_first = NULL;
179     p_fifo->pp_last = &p_fifo->p_first;
180     p_fifo->i_depth = p_fifo->i_size = 0;
181     p_fifo->b_force_wake = VLC_FALSE;
182
183     return p_fifo;
184 }
185
186 void block_FifoRelease( block_fifo_t *p_fifo )
187 {
188     block_FifoEmpty( p_fifo );
189     vlc_cond_destroy( &p_fifo->wait );
190     vlc_mutex_destroy( &p_fifo->lock );
191     free( p_fifo );
192 }
193
194 void block_FifoEmpty( block_fifo_t *p_fifo )
195 {
196     block_t *b;
197
198     vlc_mutex_lock( &p_fifo->lock );
199     for( b = p_fifo->p_first; b != NULL; )
200     {
201         block_t *p_next;
202
203         p_next = b->p_next;
204         block_Release( b );
205         b = p_next;
206     }
207
208     p_fifo->i_depth = p_fifo->i_size = 0;
209     p_fifo->p_first = NULL;
210     p_fifo->pp_last = &p_fifo->p_first;
211     vlc_mutex_unlock( &p_fifo->lock );
212 }
213
214 size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
215 {
216     size_t i_size = 0;
217     vlc_mutex_lock( &p_fifo->lock );
218
219     do
220     {
221         i_size += p_block->i_buffer;
222
223         *p_fifo->pp_last = p_block;
224         p_fifo->pp_last = &p_block->p_next;
225         p_fifo->i_depth++;
226         p_fifo->i_size += p_block->i_buffer;
227
228         p_block = p_block->p_next;
229
230     } while( p_block );
231
232     /* warn there is data in this fifo */
233     vlc_cond_signal( &p_fifo->wait );
234     vlc_mutex_unlock( &p_fifo->lock );
235
236     return i_size;
237 }
238
239 void block_FifoWake( block_fifo_t *p_fifo )
240 {
241     vlc_mutex_lock( &p_fifo->lock );
242     if( p_fifo->p_first == NULL )
243         p_fifo->b_force_wake = VLC_TRUE;
244     vlc_cond_signal( &p_fifo->wait );
245     vlc_mutex_unlock( &p_fifo->lock );
246 }
247
248 block_t *block_FifoGet( block_fifo_t *p_fifo )
249 {
250     block_t *b;
251
252     vlc_mutex_lock( &p_fifo->lock );
253
254     /* Remember vlc_cond_wait() may cause spurious wakeups
255      * (on both Win32 and POSIX) */
256     while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake )
257     {
258         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
259     }
260
261     b = p_fifo->p_first;
262
263     p_fifo->b_force_wake = VLC_FALSE;
264     if( b == NULL )
265     {
266         /* Forced wakeup */
267         vlc_mutex_unlock( &p_fifo->lock );
268         return NULL;
269     }
270
271     p_fifo->p_first = b->p_next;
272     p_fifo->i_depth--;
273     p_fifo->i_size -= b->i_buffer;
274
275     if( p_fifo->p_first == NULL )
276     {
277         p_fifo->pp_last = &p_fifo->p_first;
278     }
279
280     vlc_mutex_unlock( &p_fifo->lock );
281
282     b->p_next = NULL;
283     return b;
284 }
285
286 block_t *block_FifoShow( block_fifo_t *p_fifo )
287 {
288     block_t *b;
289
290     vlc_mutex_lock( &p_fifo->lock );
291
292     if( p_fifo->p_first == NULL )
293     {
294         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
295     }
296
297     b = p_fifo->p_first;
298
299     vlc_mutex_unlock( &p_fifo->lock );
300
301     return( b );
302 }
303
304 size_t block_FifoSize( const block_fifo_t *p_fifo )
305 {
306     return p_fifo->i_size;
307 }
308
309 size_t block_FifoCount( const block_fifo_t *p_fifo )
310 {
311     return p_fifo->i_depth;
312 }