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