]> git.sesse.net Git - vlc/blob - src/input/input_programs.c
* Removed all arbitrary limits on the number of elementary streams.
[vlc] / src / input / input_programs.c
1 /*****************************************************************************
2  * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
3  * FIXME : check the return value of realloc() and malloc() !
4  *****************************************************************************
5  * Copyright (C) 1999, 2000 VideoLAN
6  * $Id: input_programs.c,v 1.11 2000/12/21 13:54:15 massiot Exp $
7  *
8  * Authors:
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <stdlib.h>
31
32 #include "config.h"
33 #include "common.h"
34 #include "threads.h"
35 #include "mtime.h"
36 #include "debug.h"
37
38 #include "intf_msg.h"
39
40 #include "stream_control.h"
41 #include "input_ext-intf.h"
42 #include "input_ext-dec.h"
43 #include "input.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.i_pgrm_number = 0;
59     p_input->stream.pp_programs = NULL;
60
61     if( i_data_len )
62     {
63         p_input->stream.p_demux_data = malloc( i_data_len );
64         memset( p_input->stream.p_demux_data, 0, i_data_len );
65     }
66 }
67
68 /*****************************************************************************
69  * input_AddProgram: add and init a program descriptor
70  *****************************************************************************
71  * This program descriptor will be referenced in the given stream descriptor
72  *****************************************************************************/
73 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
74                                       u16 i_pgrm_id, size_t i_data_len )
75 {
76     /* Where to add the pgrm */
77     int i_pgrm_index = p_input->stream.i_pgrm_number;
78
79     intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
80
81     /* Add an entry to the list of program associated with the stream */
82     p_input->stream.i_pgrm_number++;
83     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
84                                            p_input->stream.i_pgrm_number
85                                             * sizeof(pgrm_descriptor_t *) );
86
87     /* Allocate the structure to store this description */
88     p_input->stream.pp_programs[i_pgrm_index] =
89                                         malloc( sizeof(pgrm_descriptor_t) );
90
91     /* Init this entry */
92     p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
93     p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
94     p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
95
96     p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
97     p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
98
99     p_input->stream.pp_programs[i_pgrm_index]->delta_cr = 0;
100     p_input->stream.pp_programs[i_pgrm_index]->delta_absolute = 0;
101     p_input->stream.pp_programs[i_pgrm_index]->last_cr = 0;
102     p_input->stream.pp_programs[i_pgrm_index]->c_average_count = 0;
103     p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
104                                                 = SYNCHRO_NOT_STARTED;
105     p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
106
107     p_input->stream.pp_programs[i_pgrm_index]->p_vout
108                                             = p_input->p_default_vout;
109     p_input->stream.pp_programs[i_pgrm_index]->p_aout
110                                             = p_input->p_default_aout;
111
112     if( i_data_len )
113     {
114         p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
115             malloc( i_data_len );
116         memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
117                 i_data_len );
118     }
119
120     return p_input->stream.pp_programs[i_pgrm_index];
121 }
122
123 /*****************************************************************************
124  * input_DelProgram: destroy a program descriptor
125  *****************************************************************************
126  * All ES descriptions referenced in the descriptor will be deleted.
127  *****************************************************************************/
128 void input_DelProgram( input_thread_t * p_input, u16 i_pgrm_id )
129 {
130     int i_index, i_pgrm_index = -1;
131     pgrm_descriptor_t * p_pgrm = NULL;
132
133     intf_DbgMsg("Deleting description for pgrm %d", i_pgrm_id);
134
135     /* Find where this program is described */
136     for( i_index = 0; i_index < p_input->stream.i_pgrm_number; i_index++ )
137     {
138         if( p_input->stream.pp_programs[i_index]->i_number == i_pgrm_id )
139         {
140             i_pgrm_index = i_index;
141             p_pgrm = p_input->stream.pp_programs[ i_pgrm_index ];
142             break;
143         }
144     }
145
146     /* Make sure that the pgrm exists */
147     ASSERT(i_pgrm_index >= 0);
148     ASSERT(p_pgrm);
149
150     /* Free the structures that describe the es that belongs to that program */
151     for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
152     {
153         input_DelES( p_input, p_pgrm->pp_es[i_index]->i_id );
154     }
155
156     /* Free the table of es descriptors */
157     free( p_pgrm->pp_es );
158
159     /* Free the demux data */
160     if( p_pgrm->p_demux_data != NULL )
161     {
162         free( p_pgrm->p_demux_data );
163     }
164
165     /* Free the description of this stream */
166     free( p_pgrm );
167
168     /* Remove this program from the stream's list of programs */
169     p_input->stream.i_pgrm_number--;
170     p_input->stream.pp_programs[i_pgrm_index] =
171         p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
172     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
173                                            p_input->stream.i_pgrm_number
174                                             * sizeof(pgrm_descriptor_t *) );
175 }
176
177 /*****************************************************************************
178  * input_AddES:
179  *****************************************************************************
180  * Reserve a slot in the table of ES descriptors for the ES and add it to the
181  * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
182  * alone (PSI ?)
183  *****************************************************************************/
184 es_descriptor_t * input_AddES( input_thread_t * p_input,
185                                pgrm_descriptor_t * p_pgrm, u16 i_es_id,
186                                size_t i_data_len )
187 {
188     es_descriptor_t * p_es;
189
190     intf_DbgMsg("Adding description for ES %d", i_es_id);
191
192     p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
193     p_input->i_es_number++;
194     p_input->pp_es = realloc( p_input->pp_es, p_input->i_es_number
195                                                * sizeof(es_descriptor_t *) );
196     p_input->pp_es[p_input->i_es_number - 1] = p_es;
197     p_es->i_id = i_es_id;
198
199     /* Init its values */
200     p_es->b_discontinuity = 0;
201     p_es->p_pes = NULL;
202     p_es->p_decoder_fifo = NULL;
203
204     if( i_data_len )
205     {
206         p_es->p_demux_data = malloc( i_data_len );
207         memset( p_es->p_demux_data, 0, i_data_len );
208     }
209
210     /* Add this ES to the program definition if one is given */
211     if( p_pgrm )
212     {
213         p_pgrm->i_es_number++;
214         p_pgrm->pp_es = realloc( p_pgrm->pp_es,
215                                  p_pgrm->i_es_number
216                                   * sizeof(es_descriptor_t *) );
217         p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
218         p_es->p_pgrm = p_pgrm;
219     }
220     else
221     {
222         p_es->p_pgrm = NULL;
223     }
224
225     return p_es;
226 }
227
228 /*****************************************************************************
229  * input_DelES:
230  *****************************************************************************/
231 void input_DelES( input_thread_t * p_input, u16 i_id )
232 {
233     int                     i_index, i_es;
234     pgrm_descriptor_t *     p_pgrm = NULL;
235     es_descriptor_t *       p_es = NULL;
236
237     /* Look for the description of the ES */
238     for( i_es = 0; i_es < p_input->i_es_number; i_es++ )
239     {
240         if( p_input->pp_es[i_es]->i_id == i_id )
241         {
242             p_es = p_input->pp_es[i_es];
243             p_pgrm = p_input->pp_es[i_es]->p_pgrm;
244             break;
245         }
246     }
247
248     ASSERT( p_es );
249
250     /* Remove this ES from the description of the program if it is associated to
251      * one */
252     if( p_pgrm )
253     {
254         for( i_index = 0; ; i_index++ )
255         {
256             if( p_pgrm->pp_es[i_index]->i_id == i_id )
257             {
258                 p_pgrm->i_es_number--;
259                 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
260                 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
261                                          p_pgrm->i_es_number
262                                           * sizeof(es_descriptor_t *));
263                 break;
264             }
265         }
266     }
267
268     /* Free the demux data */
269     if( p_es->p_demux_data != NULL )
270     {
271         free( p_es->p_demux_data );
272     }
273
274     /* Free the ES */
275     free( p_es );
276     p_input->i_es_number--;
277     p_input->pp_es[i_es] = p_input->pp_es[p_input->i_es_number];
278     p_input->pp_es = realloc( p_input->pp_es, p_input->i_es_number
279                                                * sizeof(es_descriptor_t *));
280 }
281
282 #ifdef STATS
283 /*****************************************************************************
284  * input_DumpStream: dumps the contents of a stream descriptor
285  *****************************************************************************/
286 void input_DumpStream( input_thread_t * p_input )
287 {
288     int i, j;
289 #define S   p_input->stream
290     intf_Msg( "input info: Dumping stream ID 0x%x\n", S.i_stream_id );
291     if( S.b_seekable )
292         intf_Msg( "input info: seekable stream, position: %d/%d\n",
293                   S.i_tell, S.i_size );
294     else
295         intf_Msg( "input info: %s\n", S.b_pace_control ? "pace controlled" :
296                   "pace un-controlled" );
297 #undef S
298     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
299     {
300 #define P   p_input->stream.pp_programs[i]
301         intf_Msg( "input info: Dumping program 0x%x, version %d (%s)\n",
302                   P->i_number, P->i_version,
303                   P->b_is_ok ? "complete" : "partial" );
304         if( P->i_synchro_state == SYNCHRO_OK )
305             intf_Msg( "input info: synchro absolute delta : %lld (jitter : %lld)\n",
306                       P->delta_absolute, P->delta_cr );
307 #undef P
308         for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
309         {
310 #define ES  p_input->stream.pp_programs[i]->pp_es[j]
311             intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s\n",
312                       ES->i_id, ES->i_stream_id, ES->i_type,
313                       ES->p_decoder_fifo != NULL ? "selected" : "not selected");
314 #undef ES
315         }
316     }
317 }
318 #endif
319
320 /*****************************************************************************
321  * InitDecConfig: initializes a decoder_config_t
322  *****************************************************************************/
323 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
324                           decoder_config_t * p_config )
325 {
326     p_config->i_stream_id = p_es->i_stream_id;
327     p_config->i_type = p_es->i_type;
328     p_config->p_stream_ctrl =
329         &p_input->stream.control;
330
331     /* Decoder FIFO */
332     if( (p_config->p_decoder_fifo =
333             (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
334     {
335         intf_ErrMsg( "Out of memory" );
336         return( -1 );
337     }
338
339     vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
340     vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
341     p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
342     p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
343     p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
344     p_config->p_decoder_fifo->pf_delete_pes =
345         p_input->p_plugin->pf_delete_pes;
346     p_es->p_decoder_fifo = p_config->p_decoder_fifo;
347
348     p_config->pf_init_bit_stream = InitBitstream;
349
350     return( 0 );
351 }
352
353 /*****************************************************************************
354  * GetVdecConfig: returns a valid vdec_config_t
355  *****************************************************************************/
356 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
357                                       es_descriptor_t * p_es )
358 {
359     vdec_config_t *     p_config;
360
361     p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
362     p_config->p_vout = p_input->p_default_vout;
363     if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
364     {
365         free( p_config );
366         return NULL;
367     }
368
369     return( p_config );
370 }
371
372 /*****************************************************************************
373  * GetAdecConfig: returns a valid adec_config_t
374  *****************************************************************************/
375 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
376                                       es_descriptor_t * p_es )
377 {
378     adec_config_t *     p_config;
379
380     p_config = (adec_config_t *)malloc( sizeof(adec_config_t) );
381     p_config->p_aout = p_input->p_default_aout;
382     if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
383     {
384         free( p_config );
385         return NULL;
386     }
387
388     return( p_config );
389 }
390
391 /*****************************************************************************
392  * input_SelectES: selects an ES and spawns the associated decoder
393  *****************************************************************************/
394 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
395 {
396     int                 i;
397
398 #ifdef DEBUG_INPUT
399     intf_DbgMsg( "Selecting ES %d", p_es->i_id );
400 #endif
401
402     if( p_es->p_decoder_fifo != NULL )
403     {
404         intf_ErrMsg( "ES %d is already selected", p_es->i_id );
405         return( -1 );
406     }
407
408     switch( p_es->i_type )
409     {
410     case MPEG1_AUDIO_ES:
411     case MPEG2_AUDIO_ES:
412         if( p_main->b_audio )
413         {
414             p_es->thread_id = adec_CreateThread( GetAdecConfig( p_input,
415                                                                 p_es ) );
416         }
417         break;
418
419     case MPEG1_VIDEO_ES:
420     case MPEG2_VIDEO_ES:
421         if( p_main->b_video )
422         {
423             p_es->thread_id = vpar_CreateThread( GetVdecConfig( p_input,
424                                                                 p_es ) );
425         }
426         break;
427
428     case AC3_AUDIO_ES:
429         if( p_main->b_audio )
430         {
431             p_es->thread_id = ac3dec_CreateThread( GetAdecConfig( p_input,
432                                                                   p_es ) );
433         }
434         break;
435
436     case DVD_SPU_ES:
437         if( p_main->b_video )
438         {
439             p_es->thread_id = spudec_CreateThread( GetVdecConfig( p_input,
440                                                                   p_es ) );
441         }
442         break;
443
444     default:
445         intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
446         return( -1 );
447         break;
448     }
449
450     if( p_es->p_decoder_fifo != NULL )
451     {
452         p_input->i_selected_es_number++;
453         p_input->pp_selected_es = realloc( p_input->pp_selected_es,
454                                            p_input->i_selected_es_number
455                                             * sizeof(es_descriptor_t *) );
456         p_input->pp_selected_es[p_input->i_selected_es_number - 1] = p_es;
457     }
458     return( 0 );
459 }