]> git.sesse.net Git - vlc/blob - src/input/input_programs.c
. destroy fifo mutex and cond variables _after_ they are used for
[vlc] / src / input / input_programs.c
1 /*****************************************************************************
2  * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: input_programs.c,v 1.24 2001/01/12 14:49:55 sam Exp $
6  *
7  * Authors:
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <stdlib.h>
30
31 #include "config.h"
32 #include "common.h"
33 #include "threads.h"
34 #include "mtime.h"
35 #include "debug.h"
36
37 #include "intf_msg.h"
38
39 #include "stream_control.h"
40 #include "input_ext-intf.h"
41 #include "input_ext-dec.h"
42 #include "input.h"
43
44 #include "main.h"                                     /* --noaudio --novideo */
45
46 /*
47  * NOTICE : all of these functions expect you to have taken the lock on
48  * p_input->stream.lock
49  */
50
51 /*****************************************************************************
52  * input_InitStream: init the stream descriptor of the given input
53  *****************************************************************************/
54 void input_InitStream( input_thread_t * p_input, size_t i_data_len )
55 {
56     p_input->stream.i_stream_id = 0;
57     p_input->stream.pp_es = NULL;
58     p_input->stream.pp_selected_es = NULL;
59     p_input->stream.pp_programs = NULL;
60
61     if( i_data_len )
62     {
63         if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
64         {
65             intf_ErrMsg( "Unable to allocate memory in input_InitStream");
66             /* FIXME : find a way to tell if failed */
67             return;
68         }
69         memset( p_input->stream.p_demux_data, 0, i_data_len );
70     }
71 }
72
73 /*****************************************************************************
74  * input_EndStream: free all stream descriptors
75  *****************************************************************************/
76 void input_EndStream( input_thread_t * p_input )
77 {
78     int i;
79
80     /* Free all programs and associated ES, and associated decoders. */
81     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
82     {
83         /* Don't put i instead of 0 !! */
84         input_DelProgram( p_input, p_input->stream.pp_programs[0] );
85     }
86
87     /* Free standalone ES */
88     for( i = 0; i < p_input->stream.i_es_number; i++ )
89     {
90         /* Don't put i instead of 0 !! */
91         input_DelES( p_input, p_input->stream.pp_es[0] );
92     }
93 }
94
95 /*****************************************************************************
96  * input_FindProgram: returns a pointer to a program described by its ID
97  *****************************************************************************/
98 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
99 {
100     int     i;
101
102     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
103     {
104         if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
105         {
106             return p_input->stream.pp_programs[i];
107         }
108     }
109
110     return( NULL );
111 }
112
113 /*****************************************************************************
114  * input_AddProgram: add and init a program descriptor
115  *****************************************************************************
116  * This program descriptor will be referenced in the given stream descriptor
117  *****************************************************************************/
118 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
119                                       u16 i_pgrm_id, size_t i_data_len )
120 {
121     /* Where to add the pgrm */
122     int i_pgrm_index = p_input->stream.i_pgrm_number;
123
124     intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
125
126     /* Add an entry to the list of program associated with the stream */
127     p_input->stream.i_pgrm_number++;
128     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
129                                            p_input->stream.i_pgrm_number
130                                             * sizeof(pgrm_descriptor_t *) );
131     if( p_input->stream.pp_programs == NULL )
132     {
133         intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
134         return( NULL );
135     }
136     
137     /* Allocate the structure to store this description */
138     p_input->stream.pp_programs[i_pgrm_index] =
139                                         malloc( sizeof(pgrm_descriptor_t) );
140     if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
141     {
142         intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
143         return( NULL );
144     }
145     
146     /* Init this entry */
147     p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
148     p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
149     p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
150
151     p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
152     p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
153
154     p_input->stream.pp_programs[i_pgrm_index]->delta_cr = 0;
155     p_input->stream.pp_programs[i_pgrm_index]->delta_absolute = 0;
156     p_input->stream.pp_programs[i_pgrm_index]->last_cr = 0;
157     p_input->stream.pp_programs[i_pgrm_index]->c_average_count = 0;
158     p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
159                                                 = SYNCHRO_NOT_STARTED;
160     p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
161
162     p_input->stream.pp_programs[i_pgrm_index]->p_vout
163                                             = p_input->p_default_vout;
164     p_input->stream.pp_programs[i_pgrm_index]->p_aout
165                                             = p_input->p_default_aout;
166
167     if( i_data_len )
168     {
169         p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
170             malloc( i_data_len );
171         if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
172         {
173             intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
174             return( NULL );
175         }
176         memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
177                 i_data_len );
178     }
179
180     return p_input->stream.pp_programs[i_pgrm_index];
181 }
182
183 /*****************************************************************************
184  * input_DelProgram: destroy a program descriptor
185  *****************************************************************************
186  * All ES descriptions referenced in the descriptor will be deleted.
187  *****************************************************************************/
188 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
189 {
190     int i_index, i_pgrm_index;
191
192     ASSERT( p_pgrm );
193
194     intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
195
196     /* Free the structures that describe the es that belongs to that program */
197     for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
198     {
199         /* Don't put i_index instead of 0 !! */
200         input_DelES( p_input, p_pgrm->pp_es[0] );
201     }
202
203     /* Free the demux data */
204     if( p_pgrm->p_demux_data != NULL )
205     {
206         free( p_pgrm->p_demux_data );
207     }
208
209     /* Find the program in the programs table */
210     for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
211          i_pgrm_index++ )
212     {
213         if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
214             break;
215     }
216
217     /* Remove this program from the stream's list of programs */
218     p_input->stream.i_pgrm_number--;
219
220     p_input->stream.pp_programs[i_pgrm_index] =
221         p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
222     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
223                                            p_input->stream.i_pgrm_number
224                                             * sizeof(pgrm_descriptor_t *) );
225
226     if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
227     {
228         intf_ErrMsg( "input error: unable to realloc program list"
229                      " in input_DelProgram" );
230     }
231
232     /* Free the description of this program */
233     free( p_pgrm );
234 }
235
236 /*****************************************************************************
237  * input_FindES: returns a pointer to an ES described by its ID
238  *****************************************************************************/
239 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
240 {
241     int     i;
242
243     for( i = 0; i < p_input->stream.i_es_number; i++ )
244     {
245         if( p_input->stream.pp_es[i]->i_id == i_es_id )
246         {
247             return p_input->stream.pp_es[i];
248         }
249     }
250
251     return( NULL );
252 }
253
254 /*****************************************************************************
255  * input_AddES:
256  *****************************************************************************
257  * Reserve a slot in the table of ES descriptors for the ES and add it to the
258  * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
259  * alone (PSI ?)
260  *****************************************************************************/
261 es_descriptor_t * input_AddES( input_thread_t * p_input,
262                                pgrm_descriptor_t * p_pgrm, u16 i_es_id,
263                                size_t i_data_len )
264 {
265     es_descriptor_t * p_es;
266
267     intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
268
269     p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
270     if( p_es == NULL )
271     {
272         intf_ErrMsg( "Unable to allocate memory in input_AddES" );
273         return( NULL);
274     }
275     p_input->stream.i_es_number++;
276     p_input->stream.pp_es = realloc( p_input->stream.pp_es,
277                                      p_input->stream.i_es_number
278                                       * sizeof(es_descriptor_t *) );
279     if( p_input->stream.pp_es == NULL )
280     {
281         intf_ErrMsg( "Unable to realloc memory in input_AddES" );
282         return( NULL );
283     }
284     p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
285     p_es->i_id = i_es_id;
286
287     /* Init its values */
288     p_es->b_discontinuity = 0;
289     p_es->p_pes = NULL;
290     p_es->p_decoder_fifo = NULL;
291
292     if( i_data_len )
293     {
294         p_es->p_demux_data = malloc( i_data_len );
295         if( p_es->p_demux_data == NULL )
296         {
297             intf_ErrMsg( "Unable to allocate memory in input_AddES" );
298             return( NULL );
299         }
300         memset( p_es->p_demux_data, 0, i_data_len );
301     }
302     else
303     {
304         p_es->p_demux_data = NULL;
305     }
306
307     /* Add this ES to the program definition if one is given */
308     if( p_pgrm )
309     {
310         p_pgrm->i_es_number++;
311         p_pgrm->pp_es = realloc( p_pgrm->pp_es,
312                                  p_pgrm->i_es_number
313                                   * sizeof(es_descriptor_t *) );
314         if( p_pgrm->pp_es == NULL )
315         {
316             intf_ErrMsg( "Unable to realloc memory in input_AddES" );
317             return( NULL );
318         }
319         p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
320         p_es->p_pgrm = p_pgrm;
321     }
322     else
323     {
324         p_es->p_pgrm = NULL;
325     }
326
327     return p_es;
328 }
329
330 /*****************************************************************************
331  * input_DelES:
332  *****************************************************************************/
333 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
334 {
335     int                     i_index, i_es_index;
336     pgrm_descriptor_t *     p_pgrm;
337
338     ASSERT( p_es );
339     p_pgrm = p_es->p_pgrm;
340
341     /* Kill associated decoder, if any. */
342     if( p_es->p_decoder_fifo != NULL )
343     {
344         input_EndDecoder( p_input, p_es );
345
346         /* Destroy the lock and cond */
347         vlc_cond_destroy( &p_es->p_decoder_fifo->data_wait );
348         vlc_mutex_destroy( &p_es->p_decoder_fifo->data_lock );
349     
350         free( p_es->p_decoder_fifo );
351     }
352
353     /* Remove this ES from the description of the program if it is associated to
354      * one */
355     if( p_pgrm )
356     {
357         for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
358         {
359             if( p_pgrm->pp_es[i_index] == p_es )
360             {
361                 p_pgrm->i_es_number--;
362                 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
363                 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
364                                          p_pgrm->i_es_number
365                                           * sizeof(es_descriptor_t *));
366                 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
367                 {
368                     intf_ErrMsg( "Unable to realloc memory in input_DelES" );
369                 }
370                 break;
371             }
372         }
373     }
374
375     /* Free the demux data */
376     if( p_es->p_demux_data != NULL )
377     {
378         free( p_es->p_demux_data );
379     }
380
381     /* Find the ES in the ES table */
382     for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
383          i_es_index++ )
384     {
385         if( p_input->stream.pp_es[i_es_index] == p_es )
386             break;
387     }
388
389     /* Free the ES */
390     free( p_es );
391     p_input->stream.i_es_number--;
392     p_input->stream.pp_es[i_es_index] =
393                     p_input->stream.pp_es[p_input->stream.i_es_number];
394     p_input->stream.pp_es = realloc( p_input->stream.pp_es,
395                                      p_input->stream.i_es_number
396                                       * sizeof(es_descriptor_t *));
397     if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
398     {
399         intf_ErrMsg( "Unable to realloc memory in input_DelES" );
400     }
401     
402 }
403
404 #ifdef STATS
405 /*****************************************************************************
406  * input_DumpStream: dumps the contents of a stream descriptor
407  *****************************************************************************/
408 void input_DumpStream( input_thread_t * p_input )
409 {
410     int i, j;
411 #define S   p_input->stream
412     intf_Msg( "input info: Dumping stream ID 0x%x", S.i_stream_id );
413     if( S.b_seekable )
414         intf_Msg( "input info: seekable stream, position: %d/%d",
415                   S.i_tell, S.i_size );
416     else
417         intf_Msg( "input info: %s", S.b_pace_control ? "pace controlled" :
418                   "pace un-controlled" );
419 #undef S
420     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
421     {
422 #define P   p_input->stream.pp_programs[i]
423         intf_Msg( "input info: Dumping program 0x%x, version %d (%s)",
424                   P->i_number, P->i_version,
425                   P->b_is_ok ? "complete" : "partial" );
426         if( P->i_synchro_state == SYNCHRO_OK )
427             intf_Msg( "input info: synchro absolute delta : %lld (jitter : %lld)",
428                       P->delta_absolute, P->delta_cr );
429 #undef P
430         for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
431         {
432 #define ES  p_input->stream.pp_programs[i]->pp_es[j]
433             intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s",
434                       ES->i_id, ES->i_stream_id, ES->i_type,
435                       ES->p_decoder_fifo != NULL ? "selected" : "not selected");
436 #undef ES
437         }
438     }
439 }
440 #endif
441
442 /*****************************************************************************
443  * InitDecConfig: initializes a decoder_config_t
444  *****************************************************************************/
445 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
446                           decoder_config_t * p_config )
447 {
448     p_config->i_id = p_es->i_id;
449     p_config->i_type = p_es->i_type;
450     p_config->p_stream_ctrl =
451         &p_input->stream.control;
452
453     /* Decoder FIFO */
454     if( (p_config->p_decoder_fifo =
455             (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
456     {
457         intf_ErrMsg( "Out of memory" );
458         return( -1 );
459     }
460
461     vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
462     vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
463     p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
464     p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
465     p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
466     p_config->p_decoder_fifo->pf_delete_pes =
467         p_input->p_plugin->pf_delete_pes;
468     p_es->p_decoder_fifo = p_config->p_decoder_fifo;
469
470     p_config->pf_init_bit_stream = InitBitstream;
471
472     return( 0 );
473 }
474
475 /*****************************************************************************
476  * GetVdecConfig: returns a valid vdec_config_t
477  *****************************************************************************/
478 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
479                                       es_descriptor_t * p_es )
480 {
481     vdec_config_t *     p_config;
482
483     p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
484     if( p_config == NULL )
485     {
486         intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
487         return( NULL );
488     }
489     p_config->p_vout = p_input->p_default_vout;
490     if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
491     {
492         free( p_config );
493         return( NULL );
494     }
495
496     return( p_config );
497 }
498
499 /*****************************************************************************
500  * GetAdecConfig: returns a valid adec_config_t
501  *****************************************************************************/
502 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
503                                       es_descriptor_t * p_es )
504 {
505     adec_config_t *     p_config;
506
507     p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
508     if( p_config == NULL )
509     {
510         intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
511         return( NULL );
512     }
513     p_config->p_aout = p_input->p_default_aout;
514     if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
515     {
516         free( p_config );
517         return( NULL );
518     }
519
520     return( p_config );
521 }
522
523 /*****************************************************************************
524  * input_SelectES: selects an ES and spawns the associated decoder
525  *****************************************************************************/
526 /* FIXME */
527 vlc_thread_t adec_CreateThread( void * );
528 vlc_thread_t ac3dec_CreateThread( void * );
529 vlc_thread_t vpar_CreateThread( void * );
530 vlc_thread_t spudec_CreateThread( void * );
531
532 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
533 {
534     /* FIXME ! */
535     decoder_capabilities_t  decoder;
536
537 #ifdef DEBUG_INPUT
538     intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
539 #endif
540
541     if( p_es->p_decoder_fifo != NULL )
542     {
543         intf_ErrMsg( "ES %d is already selected", p_es->i_id );
544         return( -1 );
545     }
546
547     switch( p_es->i_type )
548     {
549     case MPEG1_AUDIO_ES:
550     case MPEG2_AUDIO_ES:
551         if( p_main->b_audio )
552         {
553             decoder.pf_create_thread = adec_CreateThread;
554             p_es->thread_id = input_RunDecoder( &decoder,
555                                     (void *)GetAdecConfig( p_input, p_es ) );
556         }
557         break;
558
559     case MPEG1_VIDEO_ES:
560     case MPEG2_VIDEO_ES:
561         if( p_main->b_video )
562         {
563             decoder.pf_create_thread = vpar_CreateThread;
564             p_es->thread_id = input_RunDecoder( &decoder,
565                                     (void *)GetVdecConfig( p_input, p_es ) );
566         }
567         break;
568
569     case AC3_AUDIO_ES:
570         if( p_main->b_audio )
571         {
572             decoder.pf_create_thread = ac3dec_CreateThread;
573             p_es->thread_id = input_RunDecoder( &decoder,
574                                     (void *)GetAdecConfig( p_input, p_es ) );
575         }
576         break;
577
578     case DVD_SPU_ES:
579         if( p_main->b_video )
580         {
581             decoder.pf_create_thread = spudec_CreateThread;
582             p_es->thread_id = input_RunDecoder( &decoder,
583                                     (void *)GetVdecConfig( p_input, p_es ) );
584         }
585         break;
586
587     default:
588         intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
589         return( -1 );
590         break;
591     }
592
593     if( p_es->p_decoder_fifo != NULL )
594     {
595         p_input->stream.i_selected_es_number++;
596         p_input->stream.pp_selected_es = realloc(
597                                            p_input->stream.pp_selected_es,
598                                            p_input->stream.i_selected_es_number
599                                             * sizeof(es_descriptor_t *) );
600         if( p_input->stream.pp_selected_es == NULL )
601         {
602             intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
603             return(-1);
604         }
605         p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
606                 = p_es;
607     }
608     return( 0 );
609 }