* removed useless includes in intf_gnome.c
[vlc.git] / src / input / input_netlist.c
blob884377f2c041f9d48ab72f410dc41d03529e0e4b
1 /*****************************************************************************
2 * input_netlist.c: netlist management
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
5 * $Id: input_netlist.c,v 1.31 2001/02/14 15:58:29 henri Exp $
7 * Authors: Henri Fallon <henri@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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #include "defs.h"
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/uio.h> /* struct iovec */
32 #include <unistd.h>
34 #include "config.h"
35 #include "common.h"
36 #include "threads.h" /* mutex */
37 #include "mtime.h"
38 #include "intf_msg.h" /* intf_*Msg */
40 #include "stream_control.h"
41 #include "input_ext-intf.h"
42 #include "input_ext-dec.h"
44 #include "input.h"
45 #include "input_netlist.h"
47 /*****************************************************************************
48 * Local prototypes
49 *****************************************************************************/
51 /*****************************************************************************
52 * input_NetlistInit: allocates netlist buffers and init indexes
53 *****************************************************************************/
54 int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
55 size_t i_buffer_size, int i_read_once )
57 unsigned int i_loop;
58 netlist_t * p_netlist;
60 /* First we allocate and initialise our netlist struct */
61 p_input->p_method_data = malloc(sizeof(netlist_t));
62 if ( p_input->p_method_data == NULL )
64 intf_ErrMsg("Unable to malloc the netlist struct");
65 return (-1);
68 p_netlist = (netlist_t *) p_input->p_method_data;
70 p_netlist->i_read_once = i_read_once;
72 /* allocate the buffers */
73 p_netlist->p_buffers =
74 (byte_t *) malloc(i_buffer_size* i_nb_data );
75 if ( p_netlist->p_buffers == NULL )
77 intf_ErrMsg ("Unable to malloc in netlist initialization (1)");
78 return (-1);
81 p_netlist->p_data =
82 (data_packet_t *) malloc(sizeof(data_packet_t)*(i_nb_data));
83 if ( p_netlist->p_data == NULL )
85 intf_ErrMsg ("Unable to malloc in netlist initialization (2)");
86 return (-1);
89 p_netlist->p_pes =
90 (pes_packet_t *) malloc(sizeof(pes_packet_t)*(i_nb_pes));
91 if ( p_netlist->p_pes == NULL )
93 intf_ErrMsg ("Unable to malloc in netlist initialization (3)");
94 return (-1);
97 /* allocate the FIFOs */
98 p_netlist->pp_free_data =
99 (data_packet_t **) malloc (i_nb_data * sizeof(data_packet_t *) );
100 if ( p_netlist->pp_free_data == NULL )
102 intf_ErrMsg ("Unable to malloc in netlist initialization (4)");
104 p_netlist->pp_free_pes =
105 (pes_packet_t **) malloc (i_nb_pes * sizeof(pes_packet_t *) );
106 if ( p_netlist->pp_free_pes == NULL )
108 intf_ErrMsg ("Unable to malloc in netlist initialization (5)");
111 p_netlist->p_free_iovec = ( struct iovec * )
112 malloc( (i_nb_data + p_netlist->i_read_once) * sizeof(struct iovec) );
113 if ( p_netlist->p_free_iovec == NULL )
115 intf_ErrMsg ("Unable to malloc in netlist initialization (6)");
118 /* Fill the data FIFO */
119 for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
121 p_netlist->pp_free_data[i_loop] =
122 p_netlist->p_data + i_loop;
124 p_netlist->pp_free_data[i_loop]->p_buffer =
125 p_netlist->p_buffers + i_loop * i_buffer_size;
127 p_netlist->pp_free_data[i_loop]->p_payload_start =
128 p_netlist->pp_free_data[i_loop]->p_buffer;
130 p_netlist->pp_free_data[i_loop]->p_payload_end =
131 p_netlist->pp_free_data[i_loop]->p_buffer + i_buffer_size;
133 /* Fill the PES FIFO */
134 for ( i_loop = 0; i_loop < i_nb_pes ; i_loop++ )
136 p_netlist->pp_free_pes[i_loop] =
137 p_netlist->p_pes + i_loop;
140 /* Deal with the iovec */
141 for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
143 p_netlist->p_free_iovec[i_loop].iov_base =
144 p_netlist->p_buffers + i_loop * i_buffer_size;
146 p_netlist->p_free_iovec[i_loop].iov_len = i_buffer_size;
149 /* vlc_mutex_init */
150 vlc_mutex_init (&p_netlist->lock);
152 /* initialize indexes */
153 p_netlist->i_data_start = 0;
154 p_netlist->i_data_end = i_nb_data - 1;
156 p_netlist->i_pes_start = 0;
157 p_netlist->i_pes_end = i_nb_pes - 1;
159 p_netlist->i_nb_data = i_nb_data;
160 p_netlist->i_nb_pes = i_nb_pes;
161 p_netlist->i_buffer_size = i_buffer_size;
163 return (0); /* Everything went all right */
166 /*****************************************************************************
167 * input_NetlistGetiovec: returns an iovec pointer for a readv() operation
168 *****************************************************************************
169 * We return an iovec vector, so that readv can read many packets at a time,
170 * and we set pp_data to direct to the fifo pointer, which will allow us
171 * to get the corresponding data_packet.
172 *****************************************************************************/
173 struct iovec * input_NetlistGetiovec( void * p_method_data )
175 netlist_t * p_netlist;
177 /* cast */
178 p_netlist = ( netlist_t * ) p_method_data;
180 /* check */
181 if(
182 (p_netlist->i_data_end - p_netlist->i_data_start + p_netlist->i_nb_data)
183 %p_netlist->i_nb_data < p_netlist->i_read_once )
185 intf_ErrMsg("Empty iovec FIFO. Unable to allocate memory");
186 return (NULL);
189 /* readv only takes contiguous buffers
190 * so, as a solution, we chose to have a FIFO a bit longer
191 * than i_nb_data, and copy the begining of the FIFO to its end
192 * if the readv needs to go after the end */
193 if( p_netlist->i_nb_data - p_netlist->i_data_start <
194 p_netlist->i_read_once )
196 memcpy( &p_netlist->p_free_iovec[p_netlist->i_nb_data],
197 p_netlist->p_free_iovec,
198 (p_netlist->i_read_once-
199 (p_netlist->i_nb_data-p_netlist->i_data_start))
200 * sizeof(struct iovec)
205 return &p_netlist->p_free_iovec[p_netlist->i_data_start];
209 /*****************************************************************************
210 * input_NetlistMviovec: move the iovec pointer after a readv() operation
211 *****************************************************************************/
212 void input_NetlistMviovec( void * p_method_data, size_t i_nb_iovec,
213 struct data_packet_s * pp_packets[INPUT_READ_ONCE] )
215 netlist_t * p_netlist;
216 unsigned int i_loop = 0;
217 unsigned int i_current;
219 /* cast */
220 p_netlist = (netlist_t *) p_method_data;
222 /* lock */
223 vlc_mutex_lock ( &p_netlist->lock );
225 i_current = p_netlist->i_data_start;
228 /* Fills a table of pointers to packets associated with the io_vec's */
229 while (i_loop < i_nb_iovec )
231 if( i_current >= p_netlist->i_nb_data )
232 i_current-=p_netlist->i_nb_data;
234 pp_packets[i_loop] = p_netlist->pp_free_data[i_current];
236 i_loop ++;
237 i_current ++;
240 p_netlist->i_data_start += i_nb_iovec;
241 p_netlist->i_data_start %= p_netlist->i_nb_data;
243 /* unlock */
244 vlc_mutex_unlock (&p_netlist->lock);
248 /*****************************************************************************
249 * input_NetlistNewPacket: returns a free data_packet_t
250 *****************************************************************************/
251 struct data_packet_s * input_NetlistNewPacket( void * p_method_data,
252 size_t i_buffer_size )
254 netlist_t * p_netlist;
255 struct data_packet_s * p_return;
257 /* cast */
258 p_netlist = ( netlist_t * ) p_method_data;
260 #ifdef DEBUG
261 if( i_buffer_size > p_netlist->i_buffer_size )
263 /* This should not happen */
264 intf_ErrMsg( "Netlist packet too small !" );
265 return NULL;
267 #endif
269 /* lock */
270 vlc_mutex_lock ( &p_netlist->lock );
272 /* check */
273 if ( p_netlist->i_data_start == p_netlist->i_data_end )
275 intf_ErrMsg("Empty Data FIFO in netlist. Unable to allocate memory");
276 return ( NULL );
279 p_return = (p_netlist->pp_free_data[p_netlist->i_data_start]);
280 p_netlist->i_data_start++;
281 p_netlist->i_data_start %= p_netlist->i_nb_data;
283 /* unlock */
284 vlc_mutex_unlock (&p_netlist->lock);
287 /* initialize data */
288 p_return->p_next = NULL;
289 p_return->b_discard_payload = 0;
291 p_return->p_payload_start = p_return->p_buffer;
292 p_return->p_payload_end = p_return->p_payload_start + i_buffer_size;
294 return ( p_return );
297 /*****************************************************************************
298 * input_NetlistNewPES: returns a free pes_packet_t
299 *****************************************************************************/
300 struct pes_packet_s * input_NetlistNewPES( void * p_method_data )
302 netlist_t * p_netlist;
303 pes_packet_t * p_return;
305 /* cast */
306 p_netlist = (netlist_t *) p_method_data;
308 /* lock */
309 vlc_mutex_lock ( &p_netlist->lock );
311 /* check */
312 if ( p_netlist->i_pes_start == p_netlist->i_pes_end )
314 intf_ErrMsg("Empty PES FIFO in netlist - Unable to allocate memory");
315 return ( NULL );
318 /* allocate */
319 p_return = p_netlist->pp_free_pes[p_netlist->i_pes_start];
320 p_netlist->i_pes_start++;
321 p_netlist->i_pes_start %= p_netlist->i_nb_pes;
323 /* unlock */
324 vlc_mutex_unlock (&p_netlist->lock);
326 /* initialize PES */
327 p_return->b_data_alignment =
328 p_return->b_discontinuity =
329 p_return->i_pts = p_return->i_dts = 0;
330 p_return->i_pes_size = 0;
331 p_return->p_first = NULL;
333 return ( p_return );
336 /*****************************************************************************
337 * input_NetlistDeletePacket: puts a data_packet_t back into the netlist
338 *****************************************************************************/
339 void input_NetlistDeletePacket( void * p_method_data, data_packet_t * p_data )
341 netlist_t * p_netlist;
343 /* cast */
344 p_netlist = (netlist_t *) p_method_data;
346 /* lock */
347 vlc_mutex_lock ( &p_netlist->lock );
350 /* Delete data_packet */
351 p_netlist->i_data_end ++;
352 p_netlist->i_data_end %= p_netlist->i_nb_data;
354 p_netlist->pp_free_data[p_netlist->i_data_end] = p_data;
355 p_netlist->p_free_iovec[p_netlist->i_data_end].iov_base = p_data->p_buffer;
357 /* re initialize for next time */
358 p_data->p_payload_start = p_data->p_buffer;
359 p_data->p_next = NULL;
361 /* unlock */
362 vlc_mutex_unlock (&p_netlist->lock);
365 /*****************************************************************************
366 * input_NetlistDeletePES: puts a pes_packet_t back into the netlist
367 *****************************************************************************/
368 void input_NetlistDeletePES( void * p_method_data, pes_packet_t * p_pes )
370 netlist_t * p_netlist;
371 data_packet_t * p_current_packet,* p_next_packet;
373 /* cast */
374 p_netlist = (netlist_t *)p_method_data;
376 /* lock */
377 vlc_mutex_lock ( &p_netlist->lock );
379 /* delete free p_pes->p_first, p_next ... */
380 p_current_packet = p_pes->p_first;
381 while ( p_current_packet != NULL )
383 /* copy of NetListDeletePacket, duplicate code avoid many locks */
385 p_netlist->i_data_end ++;
386 p_netlist->i_data_end %= p_netlist->i_nb_data;
388 /* re initialize*/
389 p_current_packet->p_payload_start = p_current_packet->p_buffer;
391 p_netlist->pp_free_data[p_netlist->i_data_end] = p_current_packet;
393 p_netlist->p_free_iovec[p_netlist->i_data_end].iov_base
394 = p_current_packet->p_buffer;
396 p_next_packet = p_current_packet->p_next;
397 p_current_packet->p_next = NULL;
398 p_current_packet = p_next_packet;
401 /* delete our current PES packet */
402 p_netlist->i_pes_end ++;
403 p_netlist->i_pes_end %= p_netlist->i_nb_pes;
404 p_netlist->pp_free_pes[p_netlist->i_pes_end] = p_pes;
406 /* unlock */
407 vlc_mutex_unlock (&p_netlist->lock);
411 /*****************************************************************************
412 * input_NetlistEnd: frees all allocated structures
413 *****************************************************************************/
414 void input_NetlistEnd( input_thread_t * p_input)
416 netlist_t * p_netlist;
418 /* cast */
419 p_netlist = ( netlist_t * ) p_input->p_method_data;
421 /* destroy the mutex lock */
422 vlc_mutex_destroy (&p_netlist->lock);
424 /* free the FIFO, the buffer, and the netlist structure */
425 free (p_netlist->pp_free_data);
426 free (p_netlist->pp_free_pes);
427 free (p_netlist->p_pes);
428 free (p_netlist->p_data);
429 free (p_netlist->p_buffers);
431 /* free the netlist */
432 free (p_netlist);