1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: block.c,v 1.1 2003/08/23 22:49:50 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@videolan.org>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include "vlc_block.h"
38 uint8_t *p_allocated_buffer
;
39 int i_allocated_buffer
;
41 vlc_bool_t b_modify
; /* has it be put in modify state */
42 int i_duplicated
; /* how many times it has been duplicated (only content) */
46 static void BlockRelease( block_t
*p_block
)
48 vlc_mutex_lock( &p_block
->p_sys
->lock
);
50 p_block
->p_sys
->i_duplicated
--;
51 if( p_block
->p_sys
->i_duplicated
< 0 )
53 vlc_mutex_unlock( &p_block
->p_sys
->lock
);
54 free( p_block
->p_sys
->p_allocated_buffer
);
55 free( p_block
->p_sys
);
61 vlc_mutex_unlock( &p_block
->p_sys
->lock
);
65 static block_t
*__BlockDupContent( block_t
*p_block
)
69 p_dup
= block_New( p_block
->p_manager
, p_block
->i_buffer
);
70 memcpy( p_dup
->p_buffer
, p_block
->p_buffer
, p_block
->i_buffer
);
71 p_dup
->b_frame_display
= p_block
->b_frame_display
;
72 p_dup
->b_frame_start
= p_block
->b_frame_start
;
73 p_dup
->i_pts
= p_block
->i_pts
;
74 p_dup
->i_dts
= p_block
->i_dts
;
79 static block_t
*BlockModify( block_t
*p_block
, vlc_bool_t b_will_modify
)
81 block_t
*p_mod
= p_block
; /* by default */
83 vlc_mutex_lock( &p_block
->p_sys
->lock
);
85 if( p_block
->p_sys
->b_modify
== b_will_modify
)
87 vlc_mutex_unlock( &p_block
->p_sys
->lock
);
91 if( p_block
->p_sys
->i_duplicated
== 0 )
93 p_block
->p_sys
->b_modify
= b_will_modify
;
94 vlc_mutex_unlock( &p_block
->p_sys
->lock
);
98 /* FIXME we could avoid that
99 * we just need to create a new p_sys with new mem FIXME */
100 p_mod
= __BlockDupContent( p_block
);
101 vlc_mutex_unlock( &p_block
->p_sys
->lock
);
103 BlockRelease( p_block
);
107 static block_t
*BlockDuplicate( block_t
*p_block
)
111 vlc_mutex_lock( &p_block
->p_sys
->lock
);
112 if( !p_block
->p_sys
->b_modify
)
114 p_block
->p_sys
->i_duplicated
++;
115 vlc_mutex_unlock( &p_block
->p_sys
->lock
);
116 p_dup
= block_NewEmpty();
117 memcpy( p_dup
, p_block
, sizeof( block_t
) );
118 p_dup
->p_next
= NULL
;
121 p_dup
= __BlockDupContent( p_block
);
122 vlc_mutex_unlock( &p_block
->p_sys
->lock
);
127 static block_t
*BlockRealloc( block_t
*p_block
, int i_prebody
, int i_body
)
130 vlc_mutex_lock( &p_block
->p_sys
->lock
);
132 ( p_block
->p_buffer
- i_prebody
> p_block
->p_sys
->p_allocated_buffer
) )
134 p_block
->p_buffer
-= i_prebody
;
135 p_block
->i_buffer
+= i_prebody
;
139 ( p_block
->p_buffer
+ i_body
< p_block
->p_sys
->p_allocated_buffer
+ p_block
->p_sys
->i_allocated_buffer
) )
141 p_block
->i_buffer
= i_body
;
144 vlc_mutex_unlock( &p_block
->p_sys
->lock
);
148 block_t
*p_rea
= block_New( p_block
->p_manager
, i_prebody
+ i_body
);
150 memcpy( &p_rea
->p_buffer
[i_prebody
], p_block
->p_buffer
, p_block
->i_buffer
);
158 block_t
*p_rea
= BlockModify( p_block
, VLC_TRUE
);
160 i_start
= p_rea
->p_buffer
- p_rea
->p_sys
->p_allocated_buffer
;
162 p_rea
->p_sys
->i_allocated_buffer
+= i_body
- p_rea
->i_buffer
;
163 p_rea
->p_sys
->p_allocated_buffer
= realloc( p_rea
->p_sys
->p_allocated_buffer
, p_rea
->p_sys
->i_allocated_buffer
);
165 p_rea
->p_buffer
= &p_rea
->p_sys
->p_allocated_buffer
[i_start
];
166 p_rea
->i_buffer
= i_body
;
173 /*****************************************************************************
174 * Standard block management
176 *****************************************************************************/
177 /* to be used by other block managment */
178 block_t
*block_NewEmpty( void )
182 p_block
= malloc( sizeof( block_t
) );
183 p_block
->p_next
= NULL
;
184 p_block
->b_frame_display
= VLC_TRUE
;
185 p_block
->b_frame_start
= VLC_FALSE
;
189 p_block
->i_buffer
= 0;
190 p_block
->p_buffer
= NULL
;
192 p_block
->pf_release
= NULL
;
193 p_block
->pf_duplicate
= NULL
;
194 p_block
->pf_modify
= NULL
;
195 p_block
->pf_realloc
= NULL
;
197 p_block
->p_sys
= NULL
;
201 block_t
*__block_New( vlc_object_t
*p_obj
, int i_size
)
207 p_block
= block_NewEmpty();
209 p_block
->i_buffer
= i_size
;
212 p_block
->p_buffer
= malloc( i_size
);
215 p_block
->pf_release
= BlockRelease
;
216 p_block
->pf_duplicate
= BlockDuplicate
;
217 p_block
->pf_modify
= BlockModify
;
218 p_block
->pf_realloc
= BlockRealloc
;
220 /* that should be ok (no comunication between multiple p_vlc) */
221 p_block
->p_manager
= p_obj
->p_vlc
;
223 p_block
->p_sys
= p_sys
= malloc( sizeof( block_sys_t
) );
224 vlc_mutex_init( p_obj
, &p_sys
->lock
);
225 p_sys
->p_allocated_buffer
= p_block
->p_buffer
;
226 p_sys
->i_allocated_buffer
= p_block
->i_buffer
;
227 p_sys
->i_duplicated
= 0;
228 p_sys
->b_modify
= VLC_TRUE
;
233 void block_ChainAppend( block_t
**pp_list
, block_t
*p_block
)
236 if( *pp_list
== NULL
)
242 block_t
*p
= *pp_list
;
252 void block_ChainRelease( block_t
*p_block
)
257 p_next
= p_block
->p_next
;
258 p_block
->pf_release( p_block
);
263 int block_ChainExtract( block_t
*p_list
, void *p_data
, int i_max
)
269 for( b
= p_list
; b
!= NULL
; b
= b
->p_next
)
273 i_copy
= __MIN( i_max
, b
->i_buffer
);
276 memcpy( p
, b
->p_buffer
, i_copy
);
290 block_t
*block_ChainGather( block_t
*p_list
)
295 if( p_list
->p_next
== NULL
)
297 /* only one, so no need */
301 for( b
= p_list
; b
!= NULL
; b
= b
->p_next
)
303 i_total
+= b
->i_buffer
;
306 g
= block_New( p_list
->p_manager
, i_total
);
307 block_ChainExtract( p_list
, g
->p_buffer
, g
->i_buffer
);
309 g
->b_frame_display
= p_list
->b_frame_display
;
310 g
->b_frame_start
= p_list
->b_frame_start
;
311 g
->i_pts
= p_list
->i_pts
;
312 g
->i_dts
= p_list
->i_dts
;
315 block_ChainRelease( p_list
);
320 /*****************************************************************************
321 * block_fifo_t managment
322 *****************************************************************************/
323 block_fifo_t
* __block_FifoNew ( vlc_object_t
*p_obj
)
325 block_fifo_t
*p_fifo
;
327 p_fifo
= malloc( sizeof( vlc_object_t
) );
328 vlc_mutex_init( p_obj
, &p_fifo
->lock
);
329 vlc_cond_init( p_obj
, &p_fifo
->wait
);
331 p_fifo
->p_first
= NULL
;
332 p_fifo
->pp_last
= &p_fifo
->p_first
;
337 void block_FifoRelease( block_fifo_t
*p_fifo
)
339 block_FifoEmpty( p_fifo
);
340 vlc_cond_destroy( &p_fifo
->wait
);
341 vlc_mutex_destroy( &p_fifo
->lock
);
345 void block_FifoEmpty( block_fifo_t
*p_fifo
)
349 vlc_mutex_lock( &p_fifo
->lock
);
350 for( b
= p_fifo
->p_first
; b
!= NULL
; )
360 p_fifo
->p_first
= NULL
;
361 p_fifo
->pp_last
= &p_fifo
->p_first
;
362 vlc_mutex_unlock( &p_fifo
->lock
);
365 int block_FifoPut ( block_fifo_t
*p_fifo
, block_t
*p_block
)
368 vlc_mutex_lock( &p_fifo
->lock
);
372 i_size
+= p_block
->i_buffer
;
374 *p_fifo
->pp_last
= p_block
;
375 p_fifo
->pp_last
= &p_block
->p_next
;
378 p_block
= p_block
->p_next
;
382 /* warm there is data in this fifo */
383 vlc_cond_signal( &p_fifo
->wait
);
384 vlc_mutex_unlock( &p_fifo
->lock
);
389 block_t
* block_FifoGet ( block_fifo_t
*p_fifo
)
393 vlc_mutex_lock( &p_fifo
->lock
);
395 if( p_fifo
->p_first
== NULL
)
397 vlc_cond_wait( &p_fifo
->wait
, &p_fifo
->lock
);
402 p_fifo
->p_first
= b
->p_next
;
405 if( p_fifo
->p_first
== NULL
)
407 p_fifo
->pp_last
= &p_fifo
->p_first
;
410 vlc_mutex_unlock( &p_fifo
->lock
);
416 block_t
* block_FifoShow ( block_fifo_t
*p_fifo
)
420 vlc_mutex_lock( &p_fifo
->lock
);
422 if( p_fifo
->p_first
== NULL
)
424 vlc_cond_wait( &p_fifo
->wait
, &p_fifo
->lock
);
429 vlc_mutex_unlock( &p_fifo
->lock
);
435 block_t
* block_FifoGetFrame( block_fifo_t
*p_fifo
)
442 block_ChainAppend( &b
, block_FifoGet( p_fifo
) );
443 p_next
= block_FifoShow( p_fifo
);
444 if( p_next
== NULL
|| p_next
->b_frame_start
)