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