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