]> git.sesse.net Git - vlc/blob - plugins/dvd/dvd_netlist.c
-Various bug fixes in dvd_netlist. Some bugs seem to remain though.
[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.4 2001/04/01 07:31:38 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 (%d:%d). Unable to allocate memory",
245                     p_netlist->i_iovec_start, p_netlist->i_iovec_end );
246         return (NULL);
247     }
248
249     if( (
250      (p_netlist->i_data_end - p_netlist->i_data_start)
251         & p_netlist->i_nb_data ) < p_netlist->i_read_once )
252     {
253         intf_ErrMsg("Empty data FIFO (%d:%d). Unable to allocate memory", 
254                     p_netlist->i_data_start, p_netlist->i_data_end );
255         return (NULL);
256     }
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 )
263     {
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)
269               );
270
271     }
272
273     return p_netlist->p_free_iovec + p_netlist->i_iovec_start;
274
275 }
276
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 )
283 {
284     dvd_netlist_t *     p_netlist;
285     unsigned int        i_loop = 0;
286
287     /* cast */
288     p_netlist = (dvd_netlist_t *)p_method_data;
289     
290     /* lock */
291     vlc_mutex_lock( &p_netlist->lock );
292
293     /* Fills a table of pointers to packets associated with the io_vec's */
294     while( i_loop < i_nb_iovec )
295     {
296         pp_data[i_loop] = p_netlist->pp_free_data[p_netlist->i_data_start];
297         
298         pp_data[i_loop]->p_buffer =
299                     p_netlist->p_free_iovec[p_netlist->i_iovec_start].iov_base;
300         
301         pp_data[i_loop]->pi_refcount = p_netlist->pi_refcount +
302                                        p_netlist->i_iovec_start;
303
304         p_netlist->i_iovec_start ++;
305         p_netlist->i_iovec_start &= p_netlist->i_nb_iovec;
306
307         p_netlist->i_data_start ++;
308         p_netlist->i_data_start &= p_netlist->i_nb_data;
309
310         i_loop ++;
311     }
312
313     /* unlock */
314     vlc_mutex_unlock( &p_netlist->lock );
315     
316 }
317
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 )
323 {    
324     dvd_netlist_t *         p_netlist; 
325     struct data_packet_s *  p_return;
326     
327     /* cast */
328     p_netlist = (dvd_netlist_t *)p_method_data; 
329
330     /* lock */
331     vlc_mutex_lock ( &p_netlist->lock );
332         
333     /* check */
334     if ( p_netlist->i_data_start == p_netlist->i_data_end )
335     {
336         intf_ErrMsg("Empty Data FIFO in netlist. Unable to allocate memory");
337         return ( NULL );
338     }
339     
340     p_return = (p_netlist->pp_free_data[p_netlist->i_data_start]);
341
342     p_netlist->i_data_start++;
343     p_netlist->i_data_start &= p_netlist->i_nb_data;
344
345     /* unlock */
346     vlc_mutex_unlock (&p_netlist->lock);
347
348     return ( p_return );
349 }
350
351 /*****************************************************************************
352  * DVDNewPacket: returns a free data_packet_t, and takes a corresponding
353  * storage iovec
354  *****************************************************************************/
355 struct data_packet_s * DVDNewPacket( void * p_method_data,
356                                      size_t i_buffer_size )
357 {
358     dvd_netlist_t *         p_netlist;
359     struct data_packet_s *  p_packet;
360 //intf_ErrMsg( "netlist: New packet" );
361     /* cast */
362     p_netlist = (dvd_netlist_t *)p_method_data;
363     
364     /* lock */
365     vlc_mutex_lock( &p_netlist->lock );
366
367      /* check */
368     if ( p_netlist->i_iovec_start == p_netlist->i_iovec_end )
369     {
370         intf_ErrMsg("Empty io_vec FIFO in netlist. Unable to allocate memory");
371         return ( NULL );
372     }
373
374     if ( p_netlist->i_data_start == p_netlist->i_data_end )
375     {
376         intf_ErrMsg("Empty Data FIFO in netlist. Unable to allocate memory");
377         return ( NULL );
378     }
379
380
381     /* Gives an io_vec and associated data */
382     p_packet = p_netlist->pp_free_data[p_netlist->i_data_start];
383         
384     p_packet->p_buffer =
385               p_netlist->p_free_iovec[p_netlist->i_iovec_start].iov_base;
386         
387     p_packet->p_payload_start = p_packet->p_buffer;
388         
389     p_packet->p_payload_end =
390               p_packet->p_buffer + i_buffer_size;
391
392     p_packet->p_next = NULL;
393     p_packet->b_discard_payload = 0;
394
395     p_packet->pi_refcount = p_netlist->pi_refcount + p_netlist->i_iovec_start;
396     (*p_packet->pi_refcount)++;
397
398     p_netlist->i_iovec_start ++;
399     p_netlist->i_iovec_start &= p_netlist->i_nb_iovec;
400
401     p_netlist->i_data_start ++;
402     p_netlist->i_data_start &= p_netlist->i_nb_data;
403
404     /* unlock */
405     vlc_mutex_unlock( &p_netlist->lock );
406
407     return p_packet;
408 }
409
410 /*****************************************************************************
411  * DVDNewPES: returns a free pes_packet_t
412  *****************************************************************************/
413 struct pes_packet_s * DVDNewPES( void * p_method_data )
414 {
415     dvd_netlist_t *     p_netlist;
416     pes_packet_t *      p_return;
417     
418 //intf_ErrMsg( "netlist: New pes" );
419     /* cast */ 
420     p_netlist = (dvd_netlist_t *)p_method_data;
421     
422     /* lock */
423     vlc_mutex_lock ( &p_netlist->lock );
424     
425     /* check */
426     if ( p_netlist->i_pes_start == p_netlist->i_pes_end )
427     {
428         intf_ErrMsg("Empty PES FIFO in netlist - Unable to allocate memory");
429         return ( NULL );
430     }
431
432     /* allocate */
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; 
436    
437     /* unlock */
438     vlc_mutex_unlock (&p_netlist->lock);
439     
440     /* initialize PES */
441     p_return->b_data_alignment = 0;
442     p_return->b_discontinuity = 0; 
443     p_return->i_pts = 0;
444     p_return->i_dts = 0;
445     p_return->i_pes_size = 0;
446     p_return->p_first = NULL;
447
448     return ( p_return );
449 }
450
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 )
455 {
456     dvd_netlist_t * p_netlist;
457     
458     /* cast */
459     p_netlist = (dvd_netlist_t *) p_method_data;
460
461     /* lock */
462     vlc_mutex_lock ( &p_netlist->lock );
463
464
465    /* Delete data_packet */
466     p_netlist->i_data_end ++;
467     p_netlist->i_data_end &= p_netlist->i_nb_data;
468     
469     p_netlist->pp_free_data[p_netlist->i_data_end] = p_data;
470
471     p_data->p_next = NULL;
472     p_data->b_discard_payload = 0;
473
474     /* Update reference counter */
475     (*p_data->pi_refcount)--;
476
477     if( (*p_data->pi_refcount) == 0 )
478     {
479
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 =
483                                                             p_data->p_buffer;
484     }
485  
486     /* unlock */
487     vlc_mutex_unlock (&p_netlist->lock);
488 }
489
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 )
494 {
495     dvd_netlist_t *     p_netlist; 
496     data_packet_t *     p_current_packet;
497     data_packet_t *     p_next_packet;
498     
499     /* cast */
500     p_netlist = (dvd_netlist_t *)p_method_data;
501
502     /* lock */
503     vlc_mutex_lock ( &p_netlist->lock );
504
505     /* delete free  p_pes->p_first, p_next ... */
506     p_current_packet = p_pes->p_first;
507     while ( p_current_packet != NULL )
508     {
509         /* copy of NetListDeletePacket, duplicate code avoid many locks */
510
511         p_netlist->i_data_end ++;
512         p_netlist->i_data_end &= p_netlist->i_nb_data;
513
514         /* re initialize */
515         p_current_packet->p_payload_start = p_current_packet->p_buffer;
516         
517         p_netlist->pp_free_data[p_netlist->i_data_end] = p_current_packet;
518
519         /* Update reference counter */
520         (*p_current_packet->pi_refcount)--;
521
522         if( (*p_current_packet->pi_refcount) <= 0 )
523         {
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;
528         }
529     
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;
534     }
535  
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;
540     
541     /* unlock */
542     vlc_mutex_unlock (&p_netlist->lock);
543
544 }
545
546 /*****************************************************************************
547  * DVDNetlistEnd: frees all allocated structures
548  *****************************************************************************/
549 void DVDNetlistEnd( dvd_netlist_t * p_netlist )
550 {
551     /* destroy the mutex lock */
552     vlc_mutex_destroy( &p_netlist->lock );
553     
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 );
561
562     /* free the netlist */
563     free( p_netlist );
564 }