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