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