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