1 /*****************************************************************************
2 * dvd_netlist.c: Specific netlist for DVD packets
4 * The original is in src/input.
5 * There is only one major change from input_netlist.c : data is now a
6 * pointer to an offset in iovec ; and iovec has a reference counter. It
7 * will only be given back to netlist when refcount is zero.
8 *****************************************************************************
9 * Copyright (C) 1998, 1999, 2000, 2001 VideoLAN
10 * $Id: dvd_netlist.c,v 1.4 2001/04/01 07:31:38 stef Exp $
12 * Authors: Henri Fallon <henri@videolan.org>
13 * Stéphane Borel <stef@videolan.org>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
28 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
36 #include <sys/types.h>
37 #include <sys/uio.h> /* struct iovec */
42 #include "threads.h" /* mutex */
44 #include "intf_msg.h" /* intf_*Msg */
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
48 #include "input_ext-dec.h"
51 #include "dvd_netlist.h"
53 /*****************************************************************************
55 *****************************************************************************/
57 /*****************************************************************************
58 * DVDNetlistInit: allocates netlist buffers and init indexes
60 * Changes from input_NetList: we have to give the length of the buffer which
61 * is different from i_nb_data now, since we may have several data pointers
62 * in one iovec. Thus we can only delete an iovec when its refcount is 0.
63 * We only received a buffer with a GetIovec whereas NewPacket gives a pointer.
65 * Warning: i_nb_iovec, i_nb_data, i_nb_pes have to be 2^x
66 *****************************************************************************/
67 dvd_netlist_t * DVDNetlistInit( int i_nb_iovec, int i_nb_data, int i_nb_pes,
68 size_t i_buffer_size, int i_read_once )
71 dvd_netlist_t * p_netlist;
73 /* First we allocate and initialise our netlist struct */
74 p_netlist = malloc( sizeof(dvd_netlist_t) );
75 if ( p_netlist == NULL )
77 intf_ErrMsg("Unable to malloc the DVD netlist struct");
82 /* Nb of packets read once by input */
83 p_netlist->i_read_once = i_read_once;
85 /* allocate the buffers */
86 p_netlist->p_buffers = malloc( i_nb_iovec *i_buffer_size );
87 if ( p_netlist->p_buffers == NULL )
89 intf_ErrMsg ("Unable to malloc in DVD netlist initialization (1)");
90 free( p_netlist->p_buffers );
95 /* table of pointers to data packets */
96 p_netlist->p_data = malloc( i_nb_data *sizeof(data_packet_t) );
97 if ( p_netlist->p_data == NULL )
99 intf_ErrMsg ("Unable to malloc in DVD netlist initialization (2)");
100 free( p_netlist->p_buffers );
101 free( p_netlist->p_data );
106 /* table of pointer to PES packets */
107 p_netlist->p_pes = malloc( i_nb_pes *sizeof(pes_packet_t) );
108 if ( p_netlist->p_pes == NULL )
110 intf_ErrMsg ("Unable to malloc in DVD netlist initialization (3)");
111 free( p_netlist->p_buffers );
112 free( p_netlist->p_data );
113 free( p_netlist->p_pes );
118 /* allocate the FIFOs : tables of free pointers */
119 p_netlist->pp_free_data =
120 malloc( i_nb_data *sizeof(data_packet_t *) );
121 if ( p_netlist->pp_free_data == NULL )
123 intf_ErrMsg ("Unable to malloc in DVD netlist initialization (4)");
124 free( p_netlist->p_buffers );
125 free( p_netlist->p_data );
126 free( p_netlist->p_pes );
127 free( p_netlist->pp_free_data );
131 p_netlist->pp_free_pes =
132 malloc( i_nb_pes *sizeof(pes_packet_t *) );
133 if ( p_netlist->pp_free_pes == NULL )
135 intf_ErrMsg ("Unable to malloc in DVD netlist initialization (5)");
136 free( p_netlist->p_buffers );
137 free( p_netlist->p_data );
138 free( p_netlist->p_pes );
139 free( p_netlist->pp_free_data );
140 free( p_netlist->pp_free_pes );
145 p_netlist->p_free_iovec =
146 malloc( (i_nb_iovec + p_netlist->i_read_once) * sizeof(struct iovec) );
147 if ( p_netlist->p_free_iovec == NULL )
149 intf_ErrMsg ("Unable to malloc in DVD netlist initialization (6)");
150 free( p_netlist->p_buffers );
151 free( p_netlist->p_data );
152 free( p_netlist->p_pes );
153 free( p_netlist->pp_free_data );
154 free( p_netlist->pp_free_pes );
155 free( p_netlist->p_free_iovec );
160 /* table for reference counter of iovecs */
161 p_netlist->pi_refcount = malloc( i_nb_iovec *sizeof(int) );
162 if ( p_netlist->pi_refcount == NULL )
164 intf_ErrMsg ("Unable to malloc in DVD netlist initialization (7)");
165 free( p_netlist->p_buffers );
166 free( p_netlist->p_data );
167 free( p_netlist->p_pes );
168 free( p_netlist->pp_free_data );
169 free( p_netlist->pp_free_pes );
170 free( p_netlist->p_free_iovec );
171 free( p_netlist->pi_refcount );
176 /* Fill the data FIFO */
177 for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
179 p_netlist->pp_free_data[i_loop] =
180 p_netlist->p_data + i_loop;
183 /* Fill the PES FIFO */
184 for ( i_loop = 0; i_loop < i_nb_pes ; i_loop++ )
186 p_netlist->pp_free_pes[i_loop] =
187 p_netlist->p_pes + i_loop;
190 /* Deal with the iovec */
191 for ( i_loop = 0; i_loop < i_nb_iovec; i_loop++ )
193 p_netlist->p_free_iovec[i_loop].iov_base =
194 p_netlist->p_buffers + i_loop * i_buffer_size;
196 p_netlist->p_free_iovec[i_loop].iov_len = i_buffer_size;
199 /* initialize reference counters */
200 memset( p_netlist->pi_refcount, 0, i_nb_iovec *sizeof(int) );
203 vlc_mutex_init (&p_netlist->lock);
205 /* initialize indexes */
206 p_netlist->i_iovec_start = 0;
207 p_netlist->i_iovec_end = i_nb_iovec - 1;
209 p_netlist->i_data_start = 0;
210 p_netlist->i_data_end = i_nb_data - 1;
212 p_netlist->i_pes_start = 0;
213 p_netlist->i_pes_end = i_nb_pes - 1;
215 /* we give (nb - 1) to use & instead of %
216 * if you really need nb you have to add 1 */
217 p_netlist->i_nb_iovec = i_nb_iovec - 1;
218 p_netlist->i_nb_data = i_nb_data - 1;
219 p_netlist->i_nb_pes = i_nb_pes - 1;
220 p_netlist->i_buffer_size = i_buffer_size;
222 return p_netlist; /* Everything went all right */
225 /*****************************************************************************
226 * DVDGetiovec: returns an iovec pointer for a readv() operation
227 *****************************************************************************
228 * We return an iovec vector, so that readv can read many packets at a time,
229 * and we set pp_data to direct to the fifo pointer, which will allow us
230 * to get the corresponding data_packet.
231 *****************************************************************************/
232 struct iovec * DVDGetiovec( void * p_method_data )
234 dvd_netlist_t * p_netlist;
237 p_netlist = (dvd_netlist_t *)p_method_data;
241 (p_netlist->i_iovec_end - p_netlist->i_iovec_start)
242 & p_netlist->i_nb_iovec ) < p_netlist->i_read_once )
244 intf_ErrMsg("Empty iovec FIFO (%d:%d). Unable to allocate memory",
245 p_netlist->i_iovec_start, p_netlist->i_iovec_end );
250 (p_netlist->i_data_end - p_netlist->i_data_start)
251 & p_netlist->i_nb_data ) < p_netlist->i_read_once )
253 intf_ErrMsg("Empty data FIFO (%d:%d). Unable to allocate memory",
254 p_netlist->i_data_start, p_netlist->i_data_end );
257 /* readv only takes contiguous buffers
258 * so, as a solution, we chose to have a FIFO a bit longer
259 * than i_nb_data, and copy the begining of the FIFO to its end
260 * if the readv needs to go after the end */
261 if( p_netlist->i_nb_iovec - p_netlist->i_iovec_start + 1 <
262 p_netlist->i_read_once )
264 memcpy( &p_netlist->p_free_iovec[p_netlist->i_nb_iovec + 1],
265 p_netlist->p_free_iovec,
266 (p_netlist->i_read_once -
267 (p_netlist->i_nb_iovec + 1 - p_netlist->i_iovec_start))
268 * sizeof(struct iovec)
273 return p_netlist->p_free_iovec + p_netlist->i_iovec_start;
277 /*****************************************************************************
278 * DVDMviovec: move the iovec pointer by one after a readv() operation and
279 * gives a data_packet corresponding to iovec in p_data
280 *****************************************************************************/
281 void DVDMviovec( void * p_method_data, int i_nb_iovec,
282 struct data_packet_s ** pp_data )
284 dvd_netlist_t * p_netlist;
285 unsigned int i_loop = 0;
288 p_netlist = (dvd_netlist_t *)p_method_data;
291 vlc_mutex_lock( &p_netlist->lock );
293 /* Fills a table of pointers to packets associated with the io_vec's */
294 while( i_loop < i_nb_iovec )
296 pp_data[i_loop] = p_netlist->pp_free_data[p_netlist->i_data_start];
298 pp_data[i_loop]->p_buffer =
299 p_netlist->p_free_iovec[p_netlist->i_iovec_start].iov_base;
301 pp_data[i_loop]->pi_refcount = p_netlist->pi_refcount +
302 p_netlist->i_iovec_start;
304 p_netlist->i_iovec_start ++;
305 p_netlist->i_iovec_start &= p_netlist->i_nb_iovec;
307 p_netlist->i_data_start ++;
308 p_netlist->i_data_start &= p_netlist->i_nb_data;
314 vlc_mutex_unlock( &p_netlist->lock );
318 /*****************************************************************************
319 * DVDNewPtr: returns a free data_packet_t
320 * Gives a pointer ; its fields need to be initialized
321 *****************************************************************************/
322 struct data_packet_s * DVDNewPtr( void * p_method_data )
324 dvd_netlist_t * p_netlist;
325 struct data_packet_s * p_return;
328 p_netlist = (dvd_netlist_t *)p_method_data;
331 vlc_mutex_lock ( &p_netlist->lock );
334 if ( p_netlist->i_data_start == p_netlist->i_data_end )
336 intf_ErrMsg("Empty Data FIFO in netlist. Unable to allocate memory");
340 p_return = (p_netlist->pp_free_data[p_netlist->i_data_start]);
342 p_netlist->i_data_start++;
343 p_netlist->i_data_start &= p_netlist->i_nb_data;
346 vlc_mutex_unlock (&p_netlist->lock);
351 /*****************************************************************************
352 * DVDNewPacket: returns a free data_packet_t, and takes a corresponding
354 *****************************************************************************/
355 struct data_packet_s * DVDNewPacket( void * p_method_data,
356 size_t i_buffer_size )
358 dvd_netlist_t * p_netlist;
359 struct data_packet_s * p_packet;
360 //intf_ErrMsg( "netlist: New packet" );
362 p_netlist = (dvd_netlist_t *)p_method_data;
365 vlc_mutex_lock( &p_netlist->lock );
368 if ( p_netlist->i_iovec_start == p_netlist->i_iovec_end )
370 intf_ErrMsg("Empty io_vec FIFO in netlist. Unable to allocate memory");
374 if ( p_netlist->i_data_start == p_netlist->i_data_end )
376 intf_ErrMsg("Empty Data FIFO in netlist. Unable to allocate memory");
381 /* Gives an io_vec and associated data */
382 p_packet = p_netlist->pp_free_data[p_netlist->i_data_start];
385 p_netlist->p_free_iovec[p_netlist->i_iovec_start].iov_base;
387 p_packet->p_payload_start = p_packet->p_buffer;
389 p_packet->p_payload_end =
390 p_packet->p_buffer + i_buffer_size;
392 p_packet->p_next = NULL;
393 p_packet->b_discard_payload = 0;
395 p_packet->pi_refcount = p_netlist->pi_refcount + p_netlist->i_iovec_start;
396 (*p_packet->pi_refcount)++;
398 p_netlist->i_iovec_start ++;
399 p_netlist->i_iovec_start &= p_netlist->i_nb_iovec;
401 p_netlist->i_data_start ++;
402 p_netlist->i_data_start &= p_netlist->i_nb_data;
405 vlc_mutex_unlock( &p_netlist->lock );
410 /*****************************************************************************
411 * DVDNewPES: returns a free pes_packet_t
412 *****************************************************************************/
413 struct pes_packet_s * DVDNewPES( void * p_method_data )
415 dvd_netlist_t * p_netlist;
416 pes_packet_t * p_return;
418 //intf_ErrMsg( "netlist: New pes" );
420 p_netlist = (dvd_netlist_t *)p_method_data;
423 vlc_mutex_lock ( &p_netlist->lock );
426 if ( p_netlist->i_pes_start == p_netlist->i_pes_end )
428 intf_ErrMsg("Empty PES FIFO in netlist - Unable to allocate memory");
433 p_return = p_netlist->pp_free_pes[p_netlist->i_pes_start];
434 p_netlist->i_pes_start++;
435 p_netlist->i_pes_start &= p_netlist->i_nb_pes;
438 vlc_mutex_unlock (&p_netlist->lock);
441 p_return->b_data_alignment = 0;
442 p_return->b_discontinuity = 0;
445 p_return->i_pes_size = 0;
446 p_return->p_first = NULL;
451 /*****************************************************************************
452 * DVDDeletePacket: puts a data_packet_t back into the netlist
453 *****************************************************************************/
454 void DVDDeletePacket( void * p_method_data, data_packet_t * p_data )
456 dvd_netlist_t * p_netlist;
459 p_netlist = (dvd_netlist_t *) p_method_data;
462 vlc_mutex_lock ( &p_netlist->lock );
465 /* Delete data_packet */
466 p_netlist->i_data_end ++;
467 p_netlist->i_data_end &= p_netlist->i_nb_data;
469 p_netlist->pp_free_data[p_netlist->i_data_end] = p_data;
471 p_data->p_next = NULL;
472 p_data->b_discard_payload = 0;
474 /* Update reference counter */
475 (*p_data->pi_refcount)--;
477 if( (*p_data->pi_refcount) == 0 )
480 p_netlist->i_iovec_end++;
481 p_netlist->i_iovec_end &= p_netlist->i_nb_iovec;
482 p_netlist->p_free_iovec[p_netlist->i_iovec_end].iov_base =
487 vlc_mutex_unlock (&p_netlist->lock);
490 /*****************************************************************************
491 * DVDDeletePES: puts a pes_packet_t back into the netlist
492 *****************************************************************************/
493 void DVDDeletePES( void * p_method_data, pes_packet_t * p_pes )
495 dvd_netlist_t * p_netlist;
496 data_packet_t * p_current_packet;
497 data_packet_t * p_next_packet;
500 p_netlist = (dvd_netlist_t *)p_method_data;
503 vlc_mutex_lock ( &p_netlist->lock );
505 /* delete free p_pes->p_first, p_next ... */
506 p_current_packet = p_pes->p_first;
507 while ( p_current_packet != NULL )
509 /* copy of NetListDeletePacket, duplicate code avoid many locks */
511 p_netlist->i_data_end ++;
512 p_netlist->i_data_end &= p_netlist->i_nb_data;
515 p_current_packet->p_payload_start = p_current_packet->p_buffer;
517 p_netlist->pp_free_data[p_netlist->i_data_end] = p_current_packet;
519 /* Update reference counter */
520 (*p_current_packet->pi_refcount)--;
522 if( (*p_current_packet->pi_refcount) <= 0 )
524 p_netlist->i_iovec_end++;
525 p_netlist->i_iovec_end &= p_netlist->i_nb_iovec;
526 p_netlist->p_free_iovec[p_netlist->i_iovec_end].iov_base =
527 p_current_packet->p_buffer;
530 p_next_packet = p_current_packet->p_next;
531 p_current_packet->p_next = NULL;
532 p_current_packet->b_discard_payload = 0;
533 p_current_packet = p_next_packet;
536 /* delete our current PES packet */
537 p_netlist->i_pes_end ++;
538 p_netlist->i_pes_end &= p_netlist->i_nb_pes;
539 p_netlist->pp_free_pes[p_netlist->i_pes_end] = p_pes;
542 vlc_mutex_unlock (&p_netlist->lock);
546 /*****************************************************************************
547 * DVDNetlistEnd: frees all allocated structures
548 *****************************************************************************/
549 void DVDNetlistEnd( dvd_netlist_t * p_netlist )
551 /* destroy the mutex lock */
552 vlc_mutex_destroy( &p_netlist->lock );
554 /* free the FIFO, the buffer, and the netlist structure */
555 free( p_netlist->pp_free_data );
556 free( p_netlist->pp_free_pes );
557 free( p_netlist->pi_refcount );
558 free( p_netlist->p_pes );
559 free( p_netlist->p_data );
560 free( p_netlist->p_buffers );
562 /* free the netlist */