1 /*****************************************************************************
2 * input_netlist.c: netlist management
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
6 * Authors: Henri Fallon <henri@videolan.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
32 #include "threads.h" /* mutex */
34 #include "intf_msg.h" /* intf_*Msg */
36 #include "stream_control.h"
37 #include "input_ext-intf.h"
38 #include "input_ext-dec.h"
40 #include "input_netlist.h"
43 /*****************************************************************************
45 *****************************************************************************/
47 /*****************************************************************************
48 * input_NetlistInit: allocates netlist buffers and init indexes
49 *****************************************************************************/
50 int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
51 size_t i_buffer_size )
54 netlist_t * p_netlist; /* for a cast */
56 /* First we allocate and initialise our netlist struct */
57 p_input->p_method_data = malloc(sizeof(netlist_t));
60 if ( p_input->p_method_data == NULL )
62 intf_ErrMsg("Unable to malloc the netlist struct\n");
66 p_netlist = (netlist_t *) p_input->p_method_data;
68 /* For the time being, I only handle pes and data. iovec is to come soon
70 p_netlist->p_buffers = malloc(i_buffer_size*i_nb_data)
71 if ( p_netlist->p_buffers == NULL )
73 intf_ErrMsg ("Unable to malloc in netlist initialization (1)\n");
78 malloc(i_buffer_size*(i_nb_data + INPUT_READ_ONCE));
79 if ( p_netlist->p_data == NULL )
81 intf_ErrMsg ("Unable to malloc in netlist initialization (2)\n");
85 malloc(i_buffer_size*(i_nb_pes + INPUT_READ_ONCE));
86 if ( p_netlist->p_pes == NULL )
88 intf_ErrMsg ("Unable to malloc in netlist initialization (3)\n");
91 p_netlist->pp_free_data =
92 malloc (i_nb_data * sizeof(data_packet_t *) );
93 if ( p_netlist->pp_free_data == NULL )
95 intf_ErrMsg ("Unable to malloc in netlist initialization (4)\n");
97 p_netlist->pp_free_pes =
98 malloc (i_nb_data * sizeof(pes_packet_t *) );
99 if ( p_netlist->pp_free_pes == NULL )
101 intf_ErrMsg ("Unable to malloc in netlist initialization (5)\n");
105 /* Fill the data FIFO */
106 for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
108 p_netlist->pp_free_data[i_loop] =
109 p_netlist->p_data + i_loop;
111 /* Fill the PES FIFO */
112 for ( i_loop = 0; i_loop < i_nb_pes + INPUT_READ_ONCE; i_loop++ )
114 p_netlist->pp_free_pes[i_loop] =
115 p_netlist->p_pes + i_loop;
118 vlc_mutex_init (&p_netlist->lock);
120 /* initialize indexes */
121 p_netlist->i_data_start = 0;
122 p_netlist->i_data_end = i_nb_data - 1;
124 p_netlist->i_pes_start = 0;
125 p_netlist->i_pes_end = i_nb_pes + INPUT_READ_ONCE - 1;
127 // p_netlist->i_iovec_start = 0;
128 // p_netlist->i_iovec_end = /* ?? */
130 p_netlist->i_nb_data = i_nb_data;
131 p_netlist->i_nb_pes = i_nb_pes;
133 return (0); /* Everything went all right */
136 /*****************************************************************************
137 * input_NetlistGetiovec: returns an iovec pointer for a readv() operation
138 *****************************************************************************/
139 struct iovec * input_NetlistGetiovec( void * p_netlist )
141 /* fonction la plus difficile, terminer par celle-la
142 * je la ferai plus tard :p */
143 return ( NULL ); /* nothing yet */
146 /*****************************************************************************
147 * input_NetlistNewPacket: returns a free data_packet_t
148 *****************************************************************************/
149 struct data_packet_s * input_NetlistNewPacket( void * p_netlist )
151 unsigned int i_return;
152 netlist_t * pt_netlist; /* for a cast */
154 pt_netlist = ( netlist_t * ) p_netlist;
155 /* cast p_netlist -> netlist_t */
158 vlc_mutex_lock ( &pt_netlist->lock );
161 if ( pt_netlist->i_data_start == pt_netlist->i_data_end )
163 intf_ErrMsg("Full Data FIFO in netlist - Unable to allocate memory\n");
167 i_return = (pt_netlist->i_data_start)++;
168 pt_netlist->i_data_start %= pt_netlist->i_nb_data;
171 vlc_mutex_unlock (&pt_netlist->lock);
173 return ( pt_netlist->pp_free_data[i_return] );
176 /*****************************************************************************
177 * input_NetlistNewPES: returns a free pes_packet_t
178 *****************************************************************************/
179 struct pes_packet_s * input_NetlistNewPES( void * p_netlist )
181 unsigned int i_return;
182 netlist_t * pt_netlist; /* for a cast */
184 pt_netlist = (netlist_t *)p_netlist;
187 vlc_mutex_lock ( &pt_netlist->lock );
190 if ( pt_netlist->i_pes_start == pt_netlist->i_pes_end )
192 intf_ErrMsg("Full PES FIFO in netlist - Unable to allocate memory\n");
196 i_return = (pt_netlist->i_pes_start)++;
197 pt_netlist->i_pes_start %= pt_netlist->i_nb_pes;
200 vlc_mutex_unlock (&pt_netlist->lock);
202 return ( pt_netlist->pp_free_pes[i_return] );
205 /*****************************************************************************
206 * input_NetlistDeletePacket: puts a data_packet_t back into the netlist
207 *****************************************************************************/
208 void input_NetlistDeletePacket( void * p_netlist, data_packet_t * p_data )
210 netlist_t * pt_netlist; /* for a cast */
212 pt_netlist = (netlist_t *) p_netlist;
215 vlc_mutex_lock ( &pt_netlist->lock );
217 pt_netlist->i_data_end ++;
218 pt_netlist->i_data_end %= pt_netlist->i_nb_data;
220 pt_netlist->pp_free_data[pt_netlist->i_data_end] = p_data;
223 vlc_mutex_unlock (&pt_netlist->lock);
226 /*****************************************************************************
227 * input_NetlistDeletePES: puts a pes_packet_t back into the netlist
228 *****************************************************************************/
229 void input_NetlistDeletePES( void * p_netlist, pes_packet_t * p_pes )
231 /* idem, plus detruire tous les data_packet_t dans p_pes->p_first,
232 * p_pes->p_first->p_next, etc. */
233 netlist_t * pt_netlist; /* for a cast */
234 data_packet_t * p_current_packet;
236 pt_netlist = (netlist_t *)p_netlist;
239 vlc_mutex_lock ( &pt_netlist->lock );
241 /* free p_pes->p_first, p_next ... */
242 p_current_packet = p_pes->p_first;
243 while ( p_current_packet != NULL )
245 /* copy of NetListDeletePacket
246 * Duplicate code avoid many locks */
247 pt_netlist->i_data_end ++;
248 pt_netlist->i_data_end %= pt_netlist->i_nb_data;
250 pt_netlist->pp_free_data[pt_netlist->i_data_end] = p_current_packet;
252 p_current_packet = p_current_packet->p_next;
255 pt_netlist->i_pes_end ++;
256 pt_netlist->i_pes_end %= pt_netlist->i_nb_pes;
258 pt_netlist->pp_free_pes[pt_netlist->i_pes_end] = p_pes;
261 vlc_mutex_unlock (&pt_netlist->lock);
264 /*****************************************************************************
265 * input_NetlistEnd: frees all allocated structures
266 *****************************************************************************/
267 void input_NetlistEnd( input_thread_t * p_input)
269 netlist_t * p_netlist; /* for a cast */
271 p_netlist = ( netlist_t * ) p_input->p_method_data;
273 /* free the FIFO, the buffer, and the netlist structure */
274 free (p_netlist->pp_free_data);
275 free (p_netlist->pp_free_pes);
276 free (p_netlist->p_pes);
277 free (p_netlist->p_data);