]> git.sesse.net Git - vlc/blob - src/input/input_programs.c
* Added -a, -c and -s options. (-a doesn't work but I will let the people
[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.6 2000/12/20 17:49:40 massiot 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.i_pgrm_number = 0;
58     p_input->stream.pp_programs = NULL;
59
60     if( i_data_len )
61     {
62         p_input->stream.p_demux_data = malloc( i_data_len );
63         memset( p_input->stream.p_demux_data, 0, i_data_len );
64     }
65 }
66
67 /*****************************************************************************
68  * input_AddProgram: add and init a program descriptor
69  *****************************************************************************
70  * This program descriptor will be referenced in the given stream descriptor
71  *****************************************************************************/
72 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
73                                       u16 i_pgrm_id, size_t i_data_len )
74 {
75     /* Where to add the pgrm */
76     int i_pgrm_index = p_input->stream.i_pgrm_number;
77
78     intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
79
80     /* Add an entry to the list of program associated with the stream */
81     p_input->stream.i_pgrm_number++;
82     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
83                                            p_input->stream.i_pgrm_number
84                                             * sizeof(pgrm_descriptor_t *) );
85
86     /* Allocate the structure to store this description */
87     p_input->stream.pp_programs[i_pgrm_index] =
88                                         malloc( sizeof(pgrm_descriptor_t) );
89
90     /* Init this entry */
91     p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
92     p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
93     p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
94
95     p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
96     p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
97
98     p_input->stream.pp_programs[i_pgrm_index]->delta_cr = 0;
99     p_input->stream.pp_programs[i_pgrm_index]->delta_absolute = 0;
100     p_input->stream.pp_programs[i_pgrm_index]->last_cr = 0;
101     p_input->stream.pp_programs[i_pgrm_index]->c_average_count = 0;
102     p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
103                                                 = SYNCHRO_NOT_STARTED;
104     p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
105
106     p_input->stream.pp_programs[i_pgrm_index]->p_vout
107                                             = p_input->p_default_vout;
108     p_input->stream.pp_programs[i_pgrm_index]->p_aout
109                                             = p_input->p_default_aout;
110
111     if( i_data_len )
112     {
113         p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
114             malloc( i_data_len );
115         memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
116                 i_data_len );
117     }
118
119     return p_input->stream.pp_programs[i_pgrm_index];
120 }
121
122 /*****************************************************************************
123  * input_DelProgram: destroy a program descriptor
124  *****************************************************************************
125  * All ES descriptions referenced in the descriptor will be deleted.
126  *****************************************************************************/
127 void input_DelProgram( input_thread_t * p_input, u16 i_pgrm_id )
128 {
129     int i_index, i_pgrm_index = -1;
130     pgrm_descriptor_t * p_pgrm = NULL;
131
132     intf_DbgMsg("Deleting description for pgrm %d", i_pgrm_id);
133
134     /* Find where this program is described */
135     for( i_index = 0; i_index < p_input->stream.i_pgrm_number; i_index++ )
136     {
137         if( p_input->stream.pp_programs[i_index]->i_number == i_pgrm_id )
138         {
139             i_pgrm_index = i_index;
140             p_pgrm = p_input->stream.pp_programs[ i_pgrm_index ];
141             break;
142         }
143     }
144
145     /* Make sure that the pgrm exists */
146     ASSERT(i_pgrm_index >= 0);
147     ASSERT(p_pgrm);
148
149     /* Free the structures that describe the es that belongs to that program */
150     for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
151     {
152         input_DelES( p_input, p_pgrm->pp_es[i_index]->i_id );
153     }
154
155     /* Free the table of es descriptors */
156     free( p_pgrm->pp_es );
157
158     /* Free the demux data */
159     if( p_pgrm->p_demux_data != NULL )
160     {
161         free( p_pgrm->p_demux_data );
162     }
163
164     /* Free the description of this stream */
165     free( p_pgrm );
166
167     /* Remove this program from the stream's list of programs */
168     p_input->stream.i_pgrm_number--;
169     p_input->stream.pp_programs[i_pgrm_index] =
170         p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
171     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
172                                            p_input->stream.i_pgrm_number
173                                             * sizeof(pgrm_descriptor_t *) );
174 }
175
176 /*****************************************************************************
177  * input_AddES:
178  *****************************************************************************
179  * Reserve a slot in the table of ES descriptors for the ES and add it to the
180  * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
181  * alone (PSI ?)
182  *****************************************************************************/
183 es_descriptor_t * input_AddES( input_thread_t * p_input,
184                                pgrm_descriptor_t * p_pgrm, u16 i_es_id,
185                                size_t i_data_len )
186 {
187     int i_index;
188     es_descriptor_t * p_es = NULL;
189
190     intf_DbgMsg("Adding description for ES %d", i_es_id);
191
192     /* Find an empty slot to store the description of that es */
193     for( i_index = 0; i_index < INPUT_MAX_ES &&
194          p_input->p_es[i_index].i_id != EMPTY_ID; i_index++ );
195
196     if( i_index >= INPUT_MAX_ES )
197     {
198         /* No slot is empty */
199         intf_ErrMsg("Stream carries too many ES for our decoder");
200     }
201     else
202     {
203         /* Reserve the slot for that ES */
204         p_es = &p_input->p_es[i_index];
205         p_es->i_id = i_es_id;
206         intf_DbgMsg("Slot %d in p_es table assigned to ES %d",
207                     i_index, i_es_id);
208
209         /* Init its values */
210         p_es->b_discontinuity = 0;
211         p_es->p_pes = NULL;
212         p_es->p_decoder_fifo = NULL;
213
214         if( i_data_len )
215         {
216             p_es->p_demux_data = malloc( i_data_len );
217             memset( p_es->p_demux_data, 0, i_data_len );
218         }
219
220         /* Add this ES to the program definition if one is given */
221         if( p_pgrm )
222         {
223             p_pgrm->i_es_number++;
224             p_pgrm->pp_es = realloc( p_pgrm->pp_es,
225                                      p_pgrm->i_es_number
226                                       * sizeof(es_descriptor_t *) );
227             p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
228             p_es->p_pgrm = p_pgrm;
229         }
230         else
231         {
232             p_es->p_pgrm = NULL;
233         }
234     }
235
236     return p_es;
237 }
238
239 /*****************************************************************************
240  * input_DelES:
241  *****************************************************************************/
242 void input_DelES( input_thread_t * p_input, u16 i_id )
243 {
244     int                     i_index;
245     pgrm_descriptor_t *     p_pgrm = NULL;
246     es_descriptor_t *       p_es = NULL;
247
248     /* Look for the description of the ES */
249     for( i_index = 0; i_index < INPUT_MAX_ES; i_index++ )
250     {
251         if( p_input->p_es[i_index].i_id == i_id )
252         {
253             p_es = &p_input->p_es[i_index];
254             p_pgrm = p_input->p_es[i_index].p_pgrm;
255             break;
256         }
257     }
258
259     ASSERT( p_es );
260
261     /* Remove this ES from the description of the program if it is associated to
262      * one */
263     if( p_pgrm )
264     {
265         for( i_index = 0; ; i_index++ )
266         {
267             if( p_pgrm->pp_es[i_index]->i_id == i_id )
268             {
269                 p_pgrm->i_es_number--;
270                 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
271                 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
272                                          p_pgrm->i_es_number
273                                           * sizeof(es_descriptor_t *));
274                 break;
275             }
276         }
277     }
278
279     /* The table of stream descriptors is static, so don't free memory
280      * but just mark the slot as unused */
281     p_es->i_id = EMPTY_ID;
282
283     /* Free the demux data */
284     if( p_es->p_demux_data != NULL )
285     {
286         free( p_es->p_demux_data );
287     }
288 }
289
290 #ifdef STATS
291 /*****************************************************************************
292  * input_DumpStream: dumps the contents of a stream descriptor
293  *****************************************************************************/
294 void input_DumpStream( input_thread_t * p_input )
295 {
296     int i, j;
297 #define S   p_input->stream
298     intf_Msg( "input info: Dumping stream ID 0x%x\n", S.i_stream_id );
299     if( S.b_seekable )
300         intf_Msg( "input info: seekable stream, position: %d/%d\n",
301                   S.i_tell, S.i_size );
302     else
303         intf_Msg( "input info: %s\n", S.b_pace_control ? "pace controlled" :
304                   "pace un-controlled" );
305 #undef S
306     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
307     {
308 #define P   p_input->stream.pp_programs[i]
309         intf_Msg( "input info: Dumping program 0x%x, version %d (%s)\n",
310                   P->i_number, P->i_version,
311                   P->b_is_ok ? "complete" : "partial" );
312         if( P->i_synchro_state == SYNCHRO_OK )
313             intf_Msg( "input info: synchro absolute delta : %lld (jitter : %lld)\n",
314                       P->delta_absolute, P->delta_cr );
315 #undef P
316         for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
317         {
318 #define ES  p_input->stream.pp_programs[i]->pp_es[j]
319             intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s\n",
320                       ES->i_id, ES->i_stream_id, ES->i_type,
321                       ES->p_decoder_fifo != NULL ? "selected" : "not selected");
322 #undef ES
323         }
324     }
325 }
326 #endif
327
328 /*****************************************************************************
329  * InitDecConfig: initializes a decoder_config_t
330  *****************************************************************************/
331 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
332                           decoder_config_t * p_config )
333 {
334     p_config->i_stream_id = p_es->i_stream_id;
335     p_config->i_type = p_es->i_type;
336     p_config->p_stream_ctrl =
337         &p_input->stream.control;
338
339     /* Decoder FIFO */
340     if( (p_config->p_decoder_fifo =
341             (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
342     {
343         intf_ErrMsg( "Out of memory" );
344         return( -1 );
345     }
346
347     vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
348     vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
349     p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
350     p_config->p_decoder_fifo->b_die = 0;
351     p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
352     p_config->p_decoder_fifo->pf_delete_pes =
353         p_input->p_plugin->pf_delete_pes;
354     p_es->p_decoder_fifo = p_config->p_decoder_fifo;
355
356     p_config->pf_init_bit_stream = InitBitstream;
357
358     return( 0 );
359 }
360
361 /*****************************************************************************
362  * GetVdecConfig: returns a valid vdec_config_t
363  *****************************************************************************/
364 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
365                                       es_descriptor_t * p_es )
366 {
367     vdec_config_t *     p_config;
368
369     p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
370     p_config->p_vout = p_input->p_default_vout;
371     if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
372     {
373         free( p_config );
374         return NULL;
375     }
376
377     return( p_config );
378 }
379
380 /*****************************************************************************
381  * GetAdecConfig: returns a valid adec_config_t
382  *****************************************************************************/
383 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
384                                       es_descriptor_t * p_es )
385 {
386     adec_config_t *     p_config;
387
388     p_config = (adec_config_t *)malloc( sizeof(adec_config_t) );
389     p_config->p_aout = p_input->p_default_aout;
390     if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
391     {
392         free( p_config );
393         return NULL;
394     }
395
396     return( p_config );
397 }
398
399 /*****************************************************************************
400  * input_SelectES: selects an ES and spawns the associated decoder
401  *****************************************************************************/
402 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
403 {
404     int                 i;
405     es_descriptor_t **  p_spot = NULL;
406
407 #ifdef DEBUG_INPUT
408     intf_DbgMsg( "Selecting ES %d", p_es->i_id );
409 #endif
410
411     if( p_es->p_decoder_fifo != NULL )
412     {
413         intf_ErrMsg( "ES %d is already selected", p_es->i_id );
414         return( -1 );
415     }
416
417     /* Find a free spot in pp_selected_es. */
418     for( i = 0; i < INPUT_MAX_SELECTED_ES; i++ )
419     {
420         if( p_input->pp_selected_es[i] == NULL )
421         {
422             p_spot = &p_input->pp_selected_es[i];
423             break;
424         }
425     }
426
427     if( p_spot == NULL )
428     {
429         intf_ErrMsg( "Too many ES selected" );
430         return( -1 );
431     }
432
433     switch( p_es->i_type )
434     {
435     case MPEG1_AUDIO_ES:
436     case MPEG2_AUDIO_ES:
437         if( p_main->b_audio )
438         {
439             p_es->thread_id = adec_CreateThread( GetAdecConfig( p_input,
440                                                                 p_es ) );
441         }
442         break;
443
444     case MPEG1_VIDEO_ES:
445     case MPEG2_VIDEO_ES:
446         if( p_main->b_video )
447         {
448             p_es->thread_id = vpar_CreateThread( GetVdecConfig( p_input,
449                                                                 p_es ) );
450         }
451         break;
452
453     case AC3_AUDIO_ES:
454         if( p_main->b_audio )
455         {
456             p_es->thread_id = ac3dec_CreateThread( GetAdecConfig( p_input,
457                                                                   p_es ) );
458         }
459         break;
460
461     case DVD_SPU_ES:
462         if( p_main->b_video )
463         {
464             p_es->thread_id = spudec_CreateThread( GetVdecConfig( p_input,
465                                                                   p_es ) );
466         }
467         break;
468
469     default:
470         intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
471         return( -1 );
472         break;
473     }
474
475     *p_spot = p_es;
476     return( 0 );
477 }