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