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 $
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 /*****************************************************************************
26 *****************************************************************************/
30 #include <string.h> /* memcpy(), memset() */
31 #include <sys/types.h>
33 #include <sys/uio.h> /* struct iovec */
39 #include "threads.h" /* mutex */
41 #include "intf_msg.h" /* intf_*Msg */
43 #include "stream_control.h"
44 #include "input_ext-intf.h"
45 #include "input_ext-dec.h"
48 #include "input_netlist.h"
53 void *iov_base; /* Pointer to data. */
54 size_t iov_len; /* Length of data. */
57 /*****************************************************************************
59 *****************************************************************************/
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 )
68 netlist_t * p_netlist;
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 )
74 intf_ErrMsg("Unable to malloc the netlist struct");
78 p_netlist = (netlist_t *) p_input->p_method_data;
80 p_netlist->i_read_once = i_read_once;
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
88 for( i_loop=1; i_loop < i_nb_data; i_loop*=2 )
90 intf_DbgMsg( "Netlist : Required %i byte, got %u",i_nb_data,i_loop );
93 /* Same thing for i_nb_pes */
94 for( i_loop=1; i_loop < i_nb_data; i_loop*=2 )
96 intf_DbgMsg( "Netlist : Required %i byte, got %u",i_nb_data,i_loop );
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 )
104 intf_ErrMsg ("Unable to malloc in netlist initialization (1)");
109 (data_packet_t *) malloc(sizeof(data_packet_t)*(i_nb_data));
110 if ( p_netlist->p_data == NULL )
112 intf_ErrMsg ("Unable to malloc in netlist initialization (2)");
117 (pes_packet_t *) malloc(sizeof(pes_packet_t)*(i_nb_pes));
118 if ( p_netlist->p_pes == NULL )
120 intf_ErrMsg ("Unable to malloc in netlist initialization (3)");
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 )
129 intf_ErrMsg ("Unable to malloc in netlist initialization (4)");
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 )
135 intf_ErrMsg ("Unable to malloc in netlist initialization (5)");
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 )
142 intf_ErrMsg ("Unable to malloc in netlist initialization (6)");
145 /* Fill the data FIFO */
146 for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
148 p_netlist->pp_free_data[i_loop] =
149 p_netlist->p_data + i_loop;
151 p_netlist->pp_free_data[i_loop]->p_buffer =
152 p_netlist->p_buffers + i_loop * i_buffer_size;
154 p_netlist->pp_free_data[i_loop]->p_payload_start =
155 p_netlist->pp_free_data[i_loop]->p_buffer;
157 p_netlist->pp_free_data[i_loop]->p_payload_end =
158 p_netlist->pp_free_data[i_loop]->p_buffer + i_buffer_size;
160 /* Fill the PES FIFO */
161 for ( i_loop = 0; i_loop < i_nb_pes ; i_loop++ )
163 p_netlist->pp_free_pes[i_loop] =
164 p_netlist->p_pes + i_loop;
167 /* Deal with the iovec */
168 for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
170 p_netlist->p_free_iovec[i_loop].iov_base =
171 p_netlist->p_buffers + i_loop * i_buffer_size;
173 p_netlist->p_free_iovec[i_loop].iov_len = i_buffer_size;
177 vlc_mutex_init (&p_netlist->lock);
179 /* initialize indexes */
180 p_netlist->i_data_start = 0;
181 p_netlist->i_data_end = i_nb_data - 1;
183 p_netlist->i_pes_start = 0;
184 p_netlist->i_pes_end = i_nb_pes - 1;
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;
190 return (0); /* Everything went all right */
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 )
202 netlist_t * p_netlist;
205 p_netlist = ( netlist_t * ) p_method_data;
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 )
212 intf_ErrMsg("Empty iovec FIFO. Unable to allocate memory");
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 )
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)
232 return &p_netlist->p_free_iovec[p_netlist->i_data_start];
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] )
242 netlist_t * p_netlist;
243 unsigned int i_loop = 0;
244 unsigned int i_current;
247 p_netlist = (netlist_t *) p_method_data;
250 vlc_mutex_lock ( &p_netlist->lock );
252 i_current = p_netlist->i_data_start;
255 /* Fills a table of pointers to packets associated with the io_vec's */
256 while (i_loop < i_nb_iovec )
258 if( i_current >= p_netlist->i_nb_data )
259 i_current-=p_netlist->i_nb_data;
261 pp_packets[i_loop] = p_netlist->pp_free_data[i_current];
262 pp_packets[i_loop]->b_discard_payload = 0;
268 p_netlist->i_data_start += i_nb_iovec;
269 p_netlist->i_data_start &= ( p_netlist->i_nb_data - 1 );
272 vlc_mutex_unlock (&p_netlist->lock);
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 )
282 netlist_t * p_netlist;
283 struct data_packet_s * p_return;
286 p_netlist = ( netlist_t * ) p_method_data;
289 if( i_buffer_size > p_netlist->i_buffer_size )
291 /* This should not happen */
292 intf_ErrMsg( "Netlist packet too small !" );
298 vlc_mutex_lock ( &p_netlist->lock );
301 if ( p_netlist->i_data_start == p_netlist->i_data_end )
303 intf_ErrMsg("Empty Data FIFO in netlist. Unable to allocate memory");
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 );
312 vlc_mutex_unlock (&p_netlist->lock);
315 /* initialize data */
316 p_return->p_next = NULL;
317 p_return->b_discard_payload = 0;
319 p_return->p_payload_start = p_return->p_buffer;
320 p_return->p_payload_end = p_return->p_payload_start + i_buffer_size;
325 /*****************************************************************************
326 * input_NetlistNewPES: returns a free pes_packet_t
327 *****************************************************************************/
328 struct pes_packet_s * input_NetlistNewPES( void * p_method_data )
330 netlist_t * p_netlist;
331 pes_packet_t * p_return;
334 p_netlist = (netlist_t *) p_method_data;
337 vlc_mutex_lock ( &p_netlist->lock );
340 if ( p_netlist->i_pes_start == p_netlist->i_pes_end )
342 intf_ErrMsg("Empty PES FIFO in netlist - Unable to allocate memory");
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 );
352 vlc_mutex_unlock (&p_netlist->lock);
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;
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 )
369 netlist_t * p_netlist;
372 p_netlist = (netlist_t *) p_method_data;
375 vlc_mutex_lock ( &p_netlist->lock );
378 /* Delete data_packet */
379 p_netlist->i_data_end ++;
380 p_netlist->i_data_end &= ( p_netlist->i_nb_data - 1 );
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;
385 /* re initialize for next time */
386 p_data->p_payload_start = p_data->p_buffer;
387 p_data->p_next = NULL;
390 vlc_mutex_unlock (&p_netlist->lock);
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 )
398 netlist_t * p_netlist;
399 data_packet_t * p_current_packet,* p_next_packet;
402 p_netlist = (netlist_t *)p_method_data;
405 vlc_mutex_lock ( &p_netlist->lock );
407 /* delete free p_pes->p_first, p_next ... */
408 p_current_packet = p_pes->p_first;
409 while ( p_current_packet != NULL )
411 /* copy of NetListDeletePacket, duplicate code avoid many locks */
413 p_netlist->i_data_end ++;
414 p_netlist->i_data_end &= ( p_netlist->i_nb_data - 1 );
417 p_current_packet->p_payload_start = p_current_packet->p_buffer;
419 p_netlist->pp_free_data[p_netlist->i_data_end] = p_current_packet;
421 p_netlist->p_free_iovec[p_netlist->i_data_end].iov_base
422 = p_current_packet->p_buffer;
424 p_next_packet = p_current_packet->p_next;
425 p_current_packet->p_next = NULL;
426 p_current_packet = p_next_packet;
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;
435 vlc_mutex_unlock (&p_netlist->lock);
439 /*****************************************************************************
440 * input_NetlistEnd: frees all allocated structures
441 *****************************************************************************/
442 void input_NetlistEnd( input_thread_t * p_input)
444 netlist_t * p_netlist;
447 p_netlist = ( netlist_t * ) p_input->p_method_data;
449 /* destroy the mutex lock */
450 vlc_mutex_destroy (&p_netlist->lock);
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);
459 /* free the netlist */