]> git.sesse.net Git - vlc/blob - src/input/input_netlist.c
* Fixed a few warnings with gcc 3.0.
[vlc] / src / input / input_netlist.c
1 /*****************************************************************************
2  * input_netlist.c: netlist management
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  * $Id: input_netlist.c,v 1.36 2001/05/06 04:32:02 sam Exp $
6  *
7  * Authors: Henri Fallon <henri@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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <stdlib.h>
30 #include <string.h>                                    /* memcpy(), memset() */
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #ifndef WIN32 
35 #   include <sys/uio.h>                                      /* struct iovec */
36 #else
37     struct iovec
38     {
39         void *iov_base; /* Pointer to data.  */
40         size_t iov_len; /* Length of data.  */
41     };
42 #endif
43
44 #include "config.h"
45 #include "common.h"
46 #include "threads.h"                                                /* mutex */
47 #include "mtime.h"
48 #include "intf_msg.h"                                           /* intf_*Msg */
49
50 #include "stream_control.h"
51 #include "input_ext-intf.h"
52 #include "input_ext-dec.h"
53
54 #include "input.h"
55 #include "input_netlist.h"
56
57 /*****************************************************************************
58  * input_NetlistInit: allocates netlist buffers and init indexes
59  *****************************************************************************/
60 int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
61                        size_t i_buffer_size, int i_read_once )
62 {
63     unsigned int i_loop;
64     netlist_t * p_netlist;
65
66     /* First we allocate and initialise our netlist struct */
67     p_input->p_method_data = malloc(sizeof(netlist_t));
68     if ( p_input->p_method_data == NULL )
69     {
70         intf_ErrMsg("Unable to malloc the netlist struct");
71         return (-1);
72     }
73     
74     p_netlist = (netlist_t *) p_input->p_method_data;
75     
76     p_netlist->i_read_once = i_read_once;
77     
78     /* In order to optimize netlist, we are taking i_nb_data a 2^i 
79      * so that modulo is an "&".
80      * This is not changing i_nb data outside this function except in 
81      * the netlist_t struct */
82     /* As i_loop is unsigned int, and i_ns_data int, this shouldn't be a 
83      * problem */
84     for( i_loop=1; i_loop < i_nb_data; i_loop*=2 )
85         ;
86     intf_DbgMsg( "Netlist : Required %i byte, got %u",i_nb_data,i_loop );
87     i_nb_data = i_loop;
88
89     /* Same thing for i_nb_pes */
90     for( i_loop=1; i_loop < i_nb_data; i_loop*=2 )
91         ;
92     intf_DbgMsg( "Netlist : Required %i byte, got %u",i_nb_data,i_loop );
93     i_nb_data = i_loop;
94     
95     /* allocate the buffers */ 
96     p_netlist->p_buffers = 
97         (byte_t *) malloc(i_buffer_size* i_nb_data );
98     if ( p_netlist->p_buffers == NULL )
99     {
100         intf_ErrMsg ("Unable to malloc in netlist initialization (1)");
101         return (-1);
102     }
103     
104     p_netlist->p_data = 
105         (data_packet_t *) malloc(sizeof(data_packet_t)*(i_nb_data));
106     if ( p_netlist->p_data == NULL )
107     {
108         intf_ErrMsg ("Unable to malloc in netlist initialization (2)");
109         return (-1);
110     }
111     
112     p_netlist->p_pes = 
113         (pes_packet_t *) malloc(sizeof(pes_packet_t)*(i_nb_pes));
114     if ( p_netlist->p_pes == NULL )
115     {
116         intf_ErrMsg ("Unable to malloc in netlist initialization (3)");
117         return (-1);
118     }
119     
120     /* allocate the FIFOs */
121     p_netlist->pp_free_data = 
122         (data_packet_t **) malloc (i_nb_data * sizeof(data_packet_t *) );
123     if ( p_netlist->pp_free_data == NULL )
124     {
125         intf_ErrMsg ("Unable to malloc in netlist initialization (4)");
126     }
127     p_netlist->pp_free_pes = 
128         (pes_packet_t **) malloc (i_nb_pes * sizeof(pes_packet_t *) );
129     if ( p_netlist->pp_free_pes == NULL )
130     {
131         intf_ErrMsg ("Unable to malloc in netlist initialization (5)");
132     }
133     
134     p_netlist->p_free_iovec = ( struct iovec * )
135         malloc( (i_nb_data + p_netlist->i_read_once) * sizeof(struct iovec) );
136     if ( p_netlist->p_free_iovec == NULL )
137     {
138         intf_ErrMsg ("Unable to malloc in netlist initialization (6)");
139     }
140     
141     /* Fill the data FIFO */
142     for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
143     {
144         p_netlist->pp_free_data[i_loop] = 
145             p_netlist->p_data + i_loop;
146
147         p_netlist->pp_free_data[i_loop]->p_buffer = 
148             p_netlist->p_buffers + i_loop * i_buffer_size;
149       
150         p_netlist->pp_free_data[i_loop]->p_payload_start = 
151             p_netlist->pp_free_data[i_loop]->p_buffer;
152         
153         p_netlist->pp_free_data[i_loop]->p_payload_end =
154             p_netlist->pp_free_data[i_loop]->p_buffer + i_buffer_size;
155     }
156     /* Fill the PES FIFO */
157     for ( i_loop = 0; i_loop < i_nb_pes ; i_loop++ )
158     {
159         p_netlist->pp_free_pes[i_loop] = 
160             p_netlist->p_pes + i_loop;
161     }
162    
163     /* Deal with the iovec */
164     for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
165     {
166         p_netlist->p_free_iovec[i_loop].iov_base = 
167             p_netlist->p_buffers + i_loop * i_buffer_size;
168    
169         p_netlist->p_free_iovec[i_loop].iov_len = i_buffer_size;
170     }
171    
172     /* vlc_mutex_init */
173     vlc_mutex_init (&p_netlist->lock);
174     
175     /* initialize indexes */
176     p_netlist->i_data_start = 0;
177     p_netlist->i_data_end = i_nb_data - 1;
178
179     p_netlist->i_pes_start = 0;
180     p_netlist->i_pes_end = i_nb_pes - 1;
181
182     p_netlist->i_nb_data = i_nb_data;
183     p_netlist->i_nb_pes = i_nb_pes;
184     p_netlist->i_buffer_size = i_buffer_size;
185
186     return (0); /* Everything went all right */
187 }
188
189 /*****************************************************************************
190  * input_NetlistGetiovec: returns an iovec pointer for a readv() operation
191  *****************************************************************************
192  * We return an iovec vector, so that readv can read many packets at a time,
193  * and we set pp_data to direct to the fifo pointer, which will allow us
194  * to get the corresponding data_packet.
195  *****************************************************************************/
196 struct iovec * input_NetlistGetiovec( void * p_method_data )
197 {
198     netlist_t * p_netlist;
199
200     /* cast */
201     p_netlist = ( netlist_t * ) p_method_data;
202     
203     /* check */
204     if( 
205      ( (p_netlist->i_data_end - p_netlist->i_data_start + p_netlist->i_nb_data)
206      & ( p_netlist->i_nb_data -1 ) ) < p_netlist->i_read_once )
207     {
208         intf_ErrMsg("Empty iovec FIFO. Unable to allocate memory");
209         return (NULL);
210     }
211
212     /* readv only takes contiguous buffers 
213      * so, as a solution, we chose to have a FIFO a bit longer
214      * than i_nb_data, and copy the begining of the FIFO to its end
215      * if the readv needs to go after the end */
216     if( p_netlist->i_nb_data - p_netlist->i_data_start <
217                                                     p_netlist->i_read_once )
218     {
219         memcpy( &p_netlist->p_free_iovec[p_netlist->i_nb_data], 
220                 p_netlist->p_free_iovec, 
221                 (p_netlist->i_read_once-
222                     (p_netlist->i_nb_data-p_netlist->i_data_start))
223                     * sizeof(struct iovec)
224               );
225
226     }
227  
228     return &p_netlist->p_free_iovec[p_netlist->i_data_start];
229
230 }
231
232 /*****************************************************************************
233  * input_NetlistMviovec: move the iovec pointer after a readv() operation
234  *****************************************************************************/
235 void input_NetlistMviovec( void * p_method_data, size_t i_nb_iovec,
236                            struct data_packet_s * pp_packets[INPUT_READ_ONCE] )
237 {
238     netlist_t * p_netlist;
239     unsigned int i_loop = 0;
240     unsigned int i_current;
241
242     /* cast */
243     p_netlist = (netlist_t *) p_method_data;
244     
245     /* lock */
246     vlc_mutex_lock ( &p_netlist->lock );
247
248     i_current = p_netlist->i_data_start;
249
250
251     /* Fills a table of pointers to packets associated with the io_vec's */
252     while (i_loop < i_nb_iovec )
253     {
254         if( i_current >= p_netlist->i_nb_data ) 
255             i_current-=p_netlist->i_nb_data;
256         
257         pp_packets[i_loop] = p_netlist->pp_free_data[i_current];
258         pp_packets[i_loop]->b_discard_payload = 0;
259
260         i_loop ++;
261         i_current ++;
262     }
263
264     p_netlist->i_data_start += i_nb_iovec;
265     p_netlist->i_data_start &= ( p_netlist->i_nb_data - 1 );
266
267     /* unlock */
268     vlc_mutex_unlock (&p_netlist->lock);
269     
270 }
271
272 /*****************************************************************************
273  * input_NetlistNewPacket: returns a free data_packet_t
274  *****************************************************************************/
275 struct data_packet_s * input_NetlistNewPacket( void * p_method_data,
276                                                size_t i_buffer_size )
277 {    
278     netlist_t * p_netlist; 
279     struct data_packet_s * p_return;
280     
281     /* cast */
282     p_netlist = ( netlist_t * ) p_method_data; 
283
284 #ifdef DEBUG
285     if( i_buffer_size > p_netlist->i_buffer_size )
286     {
287         /* This should not happen */
288         intf_ErrMsg( "Netlist packet too small !" );
289         return NULL;
290     }
291 #endif
292
293     /* lock */
294     vlc_mutex_lock ( &p_netlist->lock );
295         
296     /* check */
297     if ( p_netlist->i_data_start == p_netlist->i_data_end )
298     {
299         intf_ErrMsg("Empty Data FIFO in netlist. Unable to allocate memory");
300         return ( NULL );
301     }
302     
303     p_return = p_netlist->pp_free_data[p_netlist->i_data_start];
304     p_netlist->i_data_start++;
305     p_netlist->i_data_start &= ( p_netlist->i_nb_data - 1 );
306
307     /* unlock */
308     vlc_mutex_unlock (&p_netlist->lock);
309
310    
311     /* initialize data */
312     p_return->p_next = NULL;
313     p_return->b_discard_payload = 0;
314     
315     p_return->p_payload_start = p_return->p_buffer;
316     p_return->p_payload_end = p_return->p_payload_start + i_buffer_size;
317     
318     return ( p_return );
319 }
320
321 /*****************************************************************************
322  * input_NetlistNewPES: returns a free pes_packet_t
323  *****************************************************************************/
324 struct pes_packet_s * input_NetlistNewPES( void * p_method_data )
325 {
326     netlist_t * p_netlist;
327     pes_packet_t * p_return;
328     
329     /* cast */ 
330     p_netlist = (netlist_t *) p_method_data;
331     
332     /* lock */
333     vlc_mutex_lock ( &p_netlist->lock );
334     
335     /* check */
336     if ( p_netlist->i_pes_start == p_netlist->i_pes_end )
337     {
338         intf_ErrMsg("Empty PES FIFO in netlist - Unable to allocate memory");
339         return ( NULL );
340     }
341
342     /* allocate */
343     p_return = p_netlist->pp_free_pes[p_netlist->i_pes_start];
344     p_netlist->i_pes_start++;
345     p_netlist->i_pes_start &= ( p_netlist->i_nb_pes - 1 ); 
346    
347     /* unlock */
348     vlc_mutex_unlock (&p_netlist->lock);
349     
350     /* initialize PES */
351     p_return->b_data_alignment = 
352         p_return->b_discontinuity = 
353         p_return->i_pts = p_return->i_dts = 0;
354     p_return->i_pes_size = 0;
355     p_return->p_first = NULL;
356    
357     return ( p_return );
358 }
359
360 /*****************************************************************************
361  * input_NetlistDeletePacket: puts a data_packet_t back into the netlist
362  *****************************************************************************/
363 void input_NetlistDeletePacket( void * p_method_data, data_packet_t * p_data )
364 {
365     netlist_t * p_netlist;
366     
367     /* cast */
368     p_netlist = (netlist_t *) p_method_data;
369
370     /* lock */
371     vlc_mutex_lock ( &p_netlist->lock );
372
373
374    /* Delete data_packet */
375     p_netlist->i_data_end ++;
376     p_netlist->i_data_end &= ( p_netlist->i_nb_data - 1 );
377     
378     p_netlist->pp_free_data[p_netlist->i_data_end] = p_data;
379     p_netlist->p_free_iovec[p_netlist->i_data_end].iov_base = p_data->p_buffer;
380
381     /* re initialize for next time */
382     p_data->p_payload_start = p_data->p_buffer;
383     p_data->p_next = NULL;
384  
385     /* unlock */
386     vlc_mutex_unlock( &p_netlist->lock );
387 }
388
389 /*****************************************************************************
390  * input_NetlistDeletePES: puts a pes_packet_t back into the netlist
391  *****************************************************************************/
392 void input_NetlistDeletePES( void * p_method_data, pes_packet_t * p_pes )
393 {
394     netlist_t * p_netlist; 
395     data_packet_t * p_current_packet,* p_next_packet;
396     
397     /* cast */
398     p_netlist = (netlist_t *)p_method_data;
399
400     /* lock */
401     vlc_mutex_lock ( &p_netlist->lock );
402
403     /* delete free  p_pes->p_first, p_next ... */
404     p_current_packet = p_pes->p_first;
405     while ( p_current_packet != NULL )
406     {
407         /* copy of NetListDeletePacket, duplicate code avoid many locks */
408
409         p_netlist->i_data_end ++;
410         p_netlist->i_data_end &= ( p_netlist->i_nb_data - 1 );
411
412         /* re initialize*/
413         p_current_packet->p_payload_start = p_current_packet->p_buffer;
414         
415         p_netlist->pp_free_data[p_netlist->i_data_end] = p_current_packet;
416
417         p_netlist->p_free_iovec[p_netlist->i_data_end].iov_base 
418             = p_current_packet->p_buffer;
419     
420         p_next_packet = p_current_packet->p_next;
421         p_current_packet->p_next = NULL;
422         p_current_packet = p_next_packet;
423     }
424  
425     /* delete our current PES packet */
426     p_netlist->i_pes_end ++;
427     p_netlist->i_pes_end &= ( p_netlist->i_nb_pes - 1 );
428     p_netlist->pp_free_pes[p_netlist->i_pes_end] = p_pes;
429     
430     /* unlock */
431     vlc_mutex_unlock( &p_netlist->lock );
432
433 }
434
435 /*****************************************************************************
436  * input_NetlistEnd: frees all allocated structures
437  *****************************************************************************/
438 void input_NetlistEnd( input_thread_t * p_input)
439 {
440     netlist_t * p_netlist;
441
442     /* cast */
443     p_netlist = ( netlist_t * ) p_input->p_method_data;
444     
445     /* destroy the mutex lock */
446     vlc_mutex_destroy (&p_netlist->lock);
447     
448     /* free the FIFO, the buffer, and the netlist structure */
449     free( p_netlist->pp_free_data );
450     free( p_netlist->pp_free_pes );
451     free( p_netlist->p_pes );
452     free( p_netlist->p_data );
453     free( p_netlist->p_buffers );
454
455     /* free the netlist */
456     free( p_netlist );
457 }
458