]> git.sesse.net Git - vlc/blob - plugins/dvd/dvd_netlist.c
* Added DVD/VCD button and menu for quick DVD device selection to the
[vlc] / plugins / dvd / dvd_netlist.c
1 /*****************************************************************************
2  * dvd_netlist.c: Specific netlist for DVD packets
3  * ---
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.2 2001/03/03 07:07:01 stef Exp $
11  *
12  * Authors: Henri Fallon <henri@videolan.org>
13  *          Stéphane Borel <stef@videolan.org>
14  *
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.
19  * 
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.
24  *
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  *****************************************************************************/
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include "defs.h"
34
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <sys/uio.h>                                         /* struct iovec */
38 #include <unistd.h>
39
40 #include "config.h"
41 #include "common.h"
42 #include "threads.h"                                                /* mutex */
43 #include "mtime.h"
44 #include "intf_msg.h"                                           /* intf_*Msg */
45
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
48 #include "input_ext-dec.h"
49
50 #include "input.h"
51 #include "dvd_netlist.h"
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56
57 /*****************************************************************************
58  * DVDNetlistInit: allocates netlist buffers and init indexes
59  * ---
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.
64  *
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 )
69 {
70     unsigned int        i_loop;
71     dvd_netlist_t *     p_netlist;
72
73     /* First we allocate and initialise our netlist struct */
74     p_netlist = malloc( sizeof(dvd_netlist_t) );
75     if ( p_netlist == NULL )
76     {
77         intf_ErrMsg("Unable to malloc the DVD netlist struct");
78         free( p_netlist );
79         return NULL;
80     }
81     
82     /* Nb of packets read once by input */
83     p_netlist->i_read_once = i_read_once;
84     
85     /* allocate the buffers */ 
86     p_netlist->p_buffers = malloc( i_nb_iovec *i_buffer_size );
87     if ( p_netlist->p_buffers == NULL )
88     {
89         intf_ErrMsg ("Unable to malloc in DVD netlist initialization (1)");
90         free( p_netlist->p_buffers );
91         free( p_netlist );
92         return NULL;
93     }
94     
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 )
98     {
99         intf_ErrMsg ("Unable to malloc in DVD netlist initialization (2)");
100         free( p_netlist->p_buffers );
101         free( p_netlist->p_data );
102         free( p_netlist );
103         return NULL;
104     }
105     
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 )
109     {
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 );
114         free( p_netlist );
115         return NULL;
116     }
117     
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 )
122     {
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 );
128         free( p_netlist );
129         return NULL;
130     }
131     p_netlist->pp_free_pes = 
132                         malloc( i_nb_pes *sizeof(pes_packet_t *) );
133     if ( p_netlist->pp_free_pes == NULL )
134     {
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 );
141         free( p_netlist );
142         return NULL;
143     }
144     
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 )
148     {
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 );
156         free( p_netlist );
157         return NULL;
158     }
159
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 )
163     {
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 );
172         free( p_netlist );
173         return NULL;
174     }
175
176     /* Fill the data FIFO */
177     for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
178     {
179         p_netlist->pp_free_data[i_loop] = 
180             p_netlist->p_data + i_loop;
181     }
182
183     /* Fill the PES FIFO */
184     for ( i_loop = 0; i_loop < i_nb_pes ; i_loop++ )
185     {
186         p_netlist->pp_free_pes[i_loop] = 
187             p_netlist->p_pes + i_loop;
188     }
189    
190     /* Deal with the iovec */
191     for ( i_loop = 0; i_loop < i_nb_iovec; i_loop++ )
192     {
193         p_netlist->p_free_iovec[i_loop].iov_base = 
194             p_netlist->p_buffers + i_loop * i_buffer_size;
195    
196         p_netlist->p_free_iovec[i_loop].iov_len = i_buffer_size;
197     }
198
199     /* initialize reference counters */
200     memset( p_netlist->pi_refcount, 0, i_nb_iovec *sizeof(int) );
201    
202     /* vlc_mutex_init */
203     vlc_mutex_init (&p_netlist->lock);
204     
205     /* initialize indexes */
206     p_netlist->i_iovec_start = 0;
207     p_netlist->i_iovec_end = i_nb_iovec - 1;
208
209     p_netlist->i_data_start = 0;
210     p_netlist->i_data_end = i_nb_data - 1;
211
212     p_netlist->i_pes_start = 0;
213     p_netlist->i_pes_end = i_nb_pes - 1;
214
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;
221
222     return p_netlist; /* Everything went all right */
223 }
224
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 )
233 {
234     dvd_netlist_t *     p_netlist;
235
236     /* cast */
237     p_netlist = (dvd_netlist_t *)p_method_data;
238     
239     /* check */
240     if( (
241      (p_netlist->i_iovec_end - p_netlist->i_iovec_start)
242         & p_netlist->i_nb_iovec ) < p_netlist->i_read_once )
243     {
244         intf_ErrMsg("Empty iovec FIFO. Unable to allocate memory");
245         return (NULL);
246     }
247
248     /* readv only takes contiguous buffers 
249      * so, as a solution, we chose to have a FIFO a bit longer
250      * than i_nb_data, and copy the begining of the FIFO to its end
251      * if the readv needs to go after the end */
252     if( p_netlist->i_nb_iovec - p_netlist->i_iovec_start + 1 <
253                                                     p_netlist->i_read_once )
254     {
255         memcpy( &p_netlist->p_free_iovec[p_netlist->i_nb_iovec + 1], 
256                 p_netlist->p_free_iovec, 
257                 (p_netlist->i_read_once -
258                     (p_netlist->i_nb_iovec + 1 - p_netlist->i_iovec_start))
259                     * sizeof(struct iovec)
260               );
261
262     }
263
264     return p_netlist->p_free_iovec + p_netlist->i_iovec_start;
265
266 }
267
268 /*****************************************************************************
269  * DVDMviovec: move the iovec pointer by one after a readv() operation and
270  * gives a data_packet corresponding to iovec in p_data
271  *****************************************************************************/
272 void DVDMviovec( void * p_method_data, int i_nb_iovec,
273                  struct data_packet_s ** pp_data )
274 {
275     dvd_netlist_t *     p_netlist;
276     unsigned int        i_loop = 0;
277
278     /* cast */
279     p_netlist = (dvd_netlist_t *)p_method_data;
280     
281     /* lock */
282     vlc_mutex_lock( &p_netlist->lock );
283
284     /* Fills a table of pointers to packets associated with the io_vec's */
285     while( i_loop < i_nb_iovec )
286     {
287         pp_data[i_loop] = p_netlist->pp_free_data[p_netlist->i_data_start];
288         
289         pp_data[i_loop]->p_buffer =
290                     p_netlist->p_free_iovec[p_netlist->i_iovec_start].iov_base;
291         
292         pp_data[i_loop]->pi_refcount = p_netlist->pi_refcount +
293                                        p_netlist->i_iovec_start;
294         p_netlist->i_iovec_start ++;
295         p_netlist->i_iovec_start &= p_netlist->i_nb_iovec;
296
297         p_netlist->i_data_start ++;
298         p_netlist->i_data_start &= p_netlist->i_nb_data;
299
300         i_loop ++;
301     }
302
303     /* unlock */
304     vlc_mutex_unlock( &p_netlist->lock );
305     
306 }
307
308 /*****************************************************************************
309  * DVDNewPtr: returns a free data_packet_t
310  * Gives a pointer ; its fields need to be initialized
311  *****************************************************************************/
312 struct data_packet_s * DVDNewPtr( void * p_method_data )
313 {    
314     dvd_netlist_t *         p_netlist; 
315     struct data_packet_s *  p_return;
316     
317     /* cast */
318     p_netlist = (dvd_netlist_t *)p_method_data; 
319
320 #ifdef DEBUG
321     if( i_buffer_size > p_netlist->i_buffer_size )
322     {
323         /* This should not happen */
324         intf_ErrMsg( "Netlist packet too small !" );
325         return NULL;
326     }
327 #endif
328
329     /* lock */
330     vlc_mutex_lock ( &p_netlist->lock );
331         
332     /* check */
333     if ( p_netlist->i_data_start == p_netlist->i_data_end )
334     {
335         intf_ErrMsg("Empty Data FIFO in netlist. Unable to allocate memory");
336         return ( NULL );
337     }
338     
339     p_return = (p_netlist->pp_free_data[p_netlist->i_data_start]);
340
341     p_netlist->i_data_start++;
342     p_netlist->i_data_start &= p_netlist->i_nb_data;
343
344     /* unlock */
345     vlc_mutex_unlock (&p_netlist->lock);
346
347     return ( p_return );
348 }
349
350 /*****************************************************************************
351  * DVDNewPacket: returns a free data_packet_t, and takes a corresponding
352  * storage iovec
353  *****************************************************************************/
354 struct data_packet_s * DVDNewPacket( void * p_method_data,
355                                      size_t i_buffer_size )
356 {
357     dvd_netlist_t *         p_netlist;
358     struct data_packet_s *  p_packet;
359
360     /* cast */
361     p_netlist = (dvd_netlist_t *)p_method_data;
362     
363     /* lock */
364     vlc_mutex_lock( &p_netlist->lock );
365
366      /* check */
367     if ( p_netlist->i_iovec_start == p_netlist->i_iovec_end )
368     {
369         intf_ErrMsg("Empty io_vec FIFO in netlist. Unable to allocate memory");
370         return ( NULL );
371     }
372
373     if ( p_netlist->i_data_start == p_netlist->i_data_end )
374     {
375         intf_ErrMsg("Empty Data FIFO in netlist. Unable to allocate memory");
376         return ( NULL );
377     }
378
379
380     /* Gives an io_vec and associated data */
381     p_packet = p_netlist->pp_free_data[p_netlist->i_data_start];
382         
383     p_packet->p_buffer =
384                p_netlist->p_free_iovec[p_netlist->i_iovec_start].iov_base;
385         
386     p_packet->p_payload_start = p_packet->p_buffer;
387         
388     p_packet->p_payload_end =
389                p_packet->p_buffer + i_buffer_size;
390
391     p_packet->pi_refcount = p_netlist->pi_refcount + p_netlist->i_iovec_start;
392
393     p_netlist->i_iovec_start ++;
394     p_netlist->i_iovec_start &= p_netlist->i_nb_iovec;
395
396     p_netlist->i_data_start ++;
397     p_netlist->i_data_start &= p_netlist->i_nb_data;
398
399     /* unlock */
400     vlc_mutex_unlock( &p_netlist->lock );
401
402     return p_packet;
403 }
404
405 /*****************************************************************************
406  * DVDNewPES: returns a free pes_packet_t
407  *****************************************************************************/
408 struct pes_packet_s * DVDNewPES( void * p_method_data )
409 {
410     dvd_netlist_t *     p_netlist;
411     pes_packet_t *      p_return;
412     
413     /* cast */ 
414     p_netlist = (dvd_netlist_t *)p_method_data;
415     
416     /* lock */
417     vlc_mutex_lock ( &p_netlist->lock );
418     
419     /* check */
420     if ( p_netlist->i_pes_start == p_netlist->i_pes_end )
421     {
422         intf_ErrMsg("Empty PES FIFO in netlist - Unable to allocate memory");
423         return ( NULL );
424     }
425
426     /* allocate */
427     p_return = p_netlist->pp_free_pes[p_netlist->i_pes_start];
428     p_netlist->i_pes_start++;
429     p_netlist->i_pes_start &= p_netlist->i_nb_pes; 
430    
431     /* unlock */
432     vlc_mutex_unlock (&p_netlist->lock);
433     
434     /* initialize PES */
435     p_return->b_data_alignment = 
436         p_return->b_discontinuity = 
437         p_return->i_pts = p_return->i_dts = 0;
438     p_return->i_pes_size = 0;
439     p_return->p_first = NULL;
440    
441     return ( p_return );
442 }
443
444 /*****************************************************************************
445  * DVDDeletePacket: puts a data_packet_t back into the netlist
446  *****************************************************************************/
447 void DVDDeletePacket( void * p_method_data, data_packet_t * p_data )
448 {
449     dvd_netlist_t * p_netlist;
450     
451     /* cast */
452     p_netlist = (dvd_netlist_t *) p_method_data;
453
454     /* lock */
455     vlc_mutex_lock ( &p_netlist->lock );
456
457
458    /* Delete data_packet */
459     p_netlist->i_data_end ++;
460     p_netlist->i_data_end &= p_netlist->i_nb_data;
461     
462     p_netlist->pp_free_data[p_netlist->i_data_end] = p_data;
463
464     /* Update reference counter */
465     (*p_data->pi_refcount)--;
466
467     if( (*p_data->pi_refcount) == 0 )
468     {
469
470         p_netlist->i_iovec_end++;
471         p_netlist->i_iovec_end &= p_netlist->i_nb_iovec;
472         p_netlist->p_free_iovec[p_netlist->i_iovec_end].iov_base =
473                                                             p_data->p_buffer;
474     }
475
476     /* re initialize for next time */
477     p_data->p_next = NULL;
478     p_data->b_discard_payload = 0;
479  
480     /* unlock */
481     vlc_mutex_unlock (&p_netlist->lock);
482 }
483
484 /*****************************************************************************
485  * DVDDeletePES: puts a pes_packet_t back into the netlist
486  *****************************************************************************/
487 void DVDDeletePES( void * p_method_data, pes_packet_t * p_pes )
488 {
489     dvd_netlist_t *     p_netlist; 
490     data_packet_t *     p_current_packet;
491     data_packet_t *     p_next_packet;
492     
493     /* cast */
494     p_netlist = (dvd_netlist_t *)p_method_data;
495
496     /* lock */
497     vlc_mutex_lock ( &p_netlist->lock );
498
499     /* delete free  p_pes->p_first, p_next ... */
500     p_current_packet = p_pes->p_first;
501     while ( p_current_packet != NULL )
502     {
503         /* copy of NetListDeletePacket, duplicate code avoid many locks */
504
505         p_netlist->i_data_end ++;
506         p_netlist->i_data_end &= p_netlist->i_nb_data;
507
508         /* re initialize*/
509         p_current_packet->p_payload_start = p_current_packet->p_buffer;
510         
511         p_netlist->pp_free_data[p_netlist->i_data_end] = p_current_packet;
512
513         /* Update reference counter */
514         (*p_current_packet->pi_refcount)--;
515
516         if( (*p_current_packet->pi_refcount) == 0 )
517         {
518             p_netlist->i_iovec_end++;
519             p_netlist->i_iovec_end &= p_netlist->i_nb_iovec;
520             p_netlist->p_free_iovec[p_netlist->i_iovec_end].iov_base =
521                     p_current_packet->p_buffer;
522         }
523     
524         p_next_packet = p_current_packet->p_next;
525         p_current_packet->p_next = NULL;
526         p_current_packet = p_next_packet;
527     }
528  
529     /* delete our current PES packet */
530     p_netlist->i_pes_end ++;
531     p_netlist->i_pes_end &= p_netlist->i_nb_pes;
532     p_netlist->pp_free_pes[p_netlist->i_pes_end] = p_pes;
533     
534     /* unlock */
535     vlc_mutex_unlock (&p_netlist->lock);
536
537 }
538
539 /*****************************************************************************
540  * DVDNetlistEnd: frees all allocated structures
541  *****************************************************************************/
542 void DVDNetlistEnd( dvd_netlist_t * p_netlist )
543 {
544     /* destroy the mutex lock */
545     vlc_mutex_destroy( &p_netlist->lock );
546     
547     /* free the FIFO, the buffer, and the netlist structure */
548     free( p_netlist->pp_free_data );
549     free( p_netlist->pp_free_pes );
550     free( p_netlist->pi_refcount );
551     free( p_netlist->p_pes );
552     free( p_netlist->p_data );
553     free( p_netlist->p_buffers );
554
555     /* free the netlist */
556     free( p_netlist );
557 }