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