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.21 2001/01/07 16:17:58 sam Exp $
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
39 #include "stream_control.h"
40 #include "input_ext-intf.h"
41 #include "input_ext-dec.h"
43 #include "input_dec.h"
45 #include "main.h" /* --noaudio --novideo */
48 * NOTICE : all of these functions expect you to have taken the lock on
49 * p_input->stream.lock
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 )
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;
64 if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
66 intf_ErrMsg( "Unable to allocate memory in input_InitStream");
67 /* FIXME : find a way to tell if failed */
70 memset( p_input->stream.p_demux_data, 0, i_data_len );
74 /*****************************************************************************
75 * input_EndStream: free all stream descriptors
76 *****************************************************************************/
77 void input_EndStream( input_thread_t * p_input )
81 /* Free all programs and associated ES, and associated decoders. */
82 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
84 /* Don't put i instead of 0 !! */
85 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
88 /* Free standalone ES */
89 for( i = 0; i < p_input->stream.i_es_number; i++ )
91 input_DelES( p_input, p_input->stream.pp_es[0] );
95 /*****************************************************************************
96 * input_FindProgram: returns a pointer to a program described by its ID
97 *****************************************************************************/
98 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
102 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
104 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
106 return p_input->stream.pp_programs[i];
113 /*****************************************************************************
114 * input_AddProgram: add and init a program descriptor
115 *****************************************************************************
116 * This program descriptor will be referenced in the given stream descriptor
117 *****************************************************************************/
118 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
119 u16 i_pgrm_id, size_t i_data_len )
121 /* Where to add the pgrm */
122 int i_pgrm_index = p_input->stream.i_pgrm_number;
124 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
126 /* Add an entry to the list of program associated with the stream */
127 p_input->stream.i_pgrm_number++;
128 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
129 p_input->stream.i_pgrm_number
130 * sizeof(pgrm_descriptor_t *) );
131 if( p_input->stream.pp_programs == NULL )
133 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
137 /* Allocate the structure to store this description */
138 p_input->stream.pp_programs[i_pgrm_index] =
139 malloc( sizeof(pgrm_descriptor_t) );
140 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
142 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
146 /* Init this entry */
147 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
148 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
149 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
151 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
152 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
154 p_input->stream.pp_programs[i_pgrm_index]->delta_cr = 0;
155 p_input->stream.pp_programs[i_pgrm_index]->delta_absolute = 0;
156 p_input->stream.pp_programs[i_pgrm_index]->last_cr = 0;
157 p_input->stream.pp_programs[i_pgrm_index]->c_average_count = 0;
158 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
159 = SYNCHRO_NOT_STARTED;
160 p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
162 p_input->stream.pp_programs[i_pgrm_index]->p_vout
163 = p_input->p_default_vout;
164 p_input->stream.pp_programs[i_pgrm_index]->p_aout
165 = p_input->p_default_aout;
169 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
170 malloc( i_data_len );
171 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
173 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
176 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
180 return p_input->stream.pp_programs[i_pgrm_index];
183 /*****************************************************************************
184 * input_DelProgram: destroy a program descriptor
185 *****************************************************************************
186 * All ES descriptions referenced in the descriptor will be deleted.
187 *****************************************************************************/
188 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
190 int i_index, i_pgrm_index;
194 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
196 /* Free the structures that describe the es that belongs to that program */
197 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
199 input_DelES( p_input, p_pgrm->pp_es[i_index] );
202 /* Free the demux data */
203 if( p_pgrm->p_demux_data != NULL )
205 free( p_pgrm->p_demux_data );
208 /* Find the program in the programs table */
209 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
212 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
216 /* Remove this program from the stream's list of programs */
217 p_input->stream.i_pgrm_number--;
219 p_input->stream.pp_programs[i_pgrm_index] =
220 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
221 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
222 p_input->stream.i_pgrm_number
223 * sizeof(pgrm_descriptor_t *) );
225 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
227 intf_ErrMsg( "input error: unable to realloc program list"
228 " in input_DelProgram" );
231 /* Free the description of this program */
235 /*****************************************************************************
236 * input_FindES: returns a pointer to an ES described by its ID
237 *****************************************************************************/
238 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
242 for( i = 0; i < p_input->stream.i_es_number; i++ )
244 if( p_input->stream.pp_es[i]->i_id == i_es_id )
246 return p_input->stream.pp_es[i];
253 /*****************************************************************************
255 *****************************************************************************
256 * Reserve a slot in the table of ES descriptors for the ES and add it to the
257 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
259 *****************************************************************************/
260 es_descriptor_t * input_AddES( input_thread_t * p_input,
261 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
264 es_descriptor_t * p_es;
266 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
268 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
271 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
274 p_input->stream.i_es_number++;
275 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
276 p_input->stream.i_es_number
277 * sizeof(es_descriptor_t *) );
278 if( p_input->stream.pp_es == NULL )
280 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
283 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
284 p_es->i_id = i_es_id;
286 /* Init its values */
287 p_es->b_discontinuity = 0;
289 p_es->p_decoder_fifo = NULL;
293 p_es->p_demux_data = malloc( i_data_len );
294 if( p_es->p_demux_data == NULL )
296 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
299 memset( p_es->p_demux_data, 0, i_data_len );
303 p_es->p_demux_data = NULL;
306 /* Add this ES to the program definition if one is given */
309 p_pgrm->i_es_number++;
310 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
312 * sizeof(es_descriptor_t *) );
313 if( p_pgrm->pp_es == NULL )
315 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
318 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
319 p_es->p_pgrm = p_pgrm;
329 /*****************************************************************************
331 *****************************************************************************/
332 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
334 int i_index, i_es_index;
335 pgrm_descriptor_t * p_pgrm;
338 p_pgrm = p_es->p_pgrm;
340 /* Kill associated decoder, if any. */
341 if( p_es->p_decoder_fifo != NULL )
343 input_EndDecoder( p_es->p_decoder_fifo, p_es->thread_id );
344 free( p_es->p_decoder_fifo );
347 /* Remove this ES from the description of the program if it is associated to
351 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
353 if( p_pgrm->pp_es[i_index] == p_es )
355 p_pgrm->i_es_number--;
356 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
357 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
359 * sizeof(es_descriptor_t *));
360 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
362 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
369 /* Free the demux data */
370 if( p_es->p_demux_data != NULL )
372 free( p_es->p_demux_data );
375 /* Find the ES in the ES table */
376 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
379 if( p_input->stream.pp_es[i_es_index] == p_es )
385 p_input->stream.i_es_number--;
386 p_input->stream.pp_es[i_es_index] =
387 p_input->stream.pp_es[p_input->stream.i_es_number];
388 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
389 p_input->stream.i_es_number
390 * sizeof(es_descriptor_t *));
391 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
393 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
399 /*****************************************************************************
400 * input_DumpStream: dumps the contents of a stream descriptor
401 *****************************************************************************/
402 void input_DumpStream( input_thread_t * p_input )
405 #define S p_input->stream
406 intf_Msg( "input info: Dumping stream ID 0x%x", S.i_stream_id );
408 intf_Msg( "input info: seekable stream, position: %d/%d",
409 S.i_tell, S.i_size );
411 intf_Msg( "input info: %s", S.b_pace_control ? "pace controlled" :
412 "pace un-controlled" );
414 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
416 #define P p_input->stream.pp_programs[i]
417 intf_Msg( "input info: Dumping program 0x%x, version %d (%s)",
418 P->i_number, P->i_version,
419 P->b_is_ok ? "complete" : "partial" );
420 if( P->i_synchro_state == SYNCHRO_OK )
421 intf_Msg( "input info: synchro absolute delta : %lld (jitter : %lld)",
422 P->delta_absolute, P->delta_cr );
424 for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
426 #define ES p_input->stream.pp_programs[i]->pp_es[j]
427 intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s",
428 ES->i_id, ES->i_stream_id, ES->i_type,
429 ES->p_decoder_fifo != NULL ? "selected" : "not selected");
436 /*****************************************************************************
437 * InitDecConfig: initializes a decoder_config_t
438 *****************************************************************************/
439 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
440 decoder_config_t * p_config )
442 p_config->i_id = p_es->i_id;
443 p_config->i_type = p_es->i_type;
444 p_config->p_stream_ctrl =
445 &p_input->stream.control;
448 if( (p_config->p_decoder_fifo =
449 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
451 intf_ErrMsg( "Out of memory" );
455 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
456 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
457 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
458 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
459 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
460 p_config->p_decoder_fifo->pf_delete_pes =
461 p_input->p_plugin->pf_delete_pes;
462 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
464 p_config->pf_init_bit_stream = InitBitstream;
469 /*****************************************************************************
470 * GetVdecConfig: returns a valid vdec_config_t
471 *****************************************************************************/
472 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
473 es_descriptor_t * p_es )
475 vdec_config_t * p_config;
477 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
478 if( p_config == NULL )
480 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
483 p_config->p_vout = p_input->p_default_vout;
484 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
493 /*****************************************************************************
494 * GetAdecConfig: returns a valid adec_config_t
495 *****************************************************************************/
496 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
497 es_descriptor_t * p_es )
499 adec_config_t * p_config;
501 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
502 if( p_config == NULL )
504 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
507 p_config->p_aout = p_input->p_default_aout;
508 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
517 /*****************************************************************************
518 * input_SelectES: selects an ES and spawns the associated decoder
519 *****************************************************************************/
521 vlc_thread_t adec_CreateThread( void * );
522 vlc_thread_t ac3dec_CreateThread( void * );
523 vlc_thread_t vpar_CreateThread( void * );
524 vlc_thread_t spudec_CreateThread( void * );
526 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
529 decoder_capabilities_t decoder;
532 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
535 if( p_es->p_decoder_fifo != NULL )
537 intf_ErrMsg( "ES %d is already selected", p_es->i_id );
541 switch( p_es->i_type )
545 if( p_main->b_audio )
547 decoder.pf_create_thread = adec_CreateThread;
548 p_es->thread_id = input_RunDecoder( &decoder,
549 (void *)GetAdecConfig( p_input, p_es ) );
555 if( p_main->b_video )
557 decoder.pf_create_thread = vpar_CreateThread;
558 p_es->thread_id = input_RunDecoder( &decoder,
559 (void *)GetVdecConfig( p_input, p_es ) );
564 if( p_main->b_audio )
566 decoder.pf_create_thread = ac3dec_CreateThread;
567 p_es->thread_id = input_RunDecoder( &decoder,
568 (void *)GetAdecConfig( p_input, p_es ) );
573 if( p_main->b_video )
575 decoder.pf_create_thread = spudec_CreateThread;
576 p_es->thread_id = input_RunDecoder( &decoder,
577 (void *)GetVdecConfig( p_input, p_es ) );
582 intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
587 if( p_es->p_decoder_fifo != NULL )
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 )
596 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
599 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]