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 $
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"
44 #include "main.h" /* --noaudio --novideo */
47 * NOTICE : all of these functions expect you to have taken the lock on
48 * p_input->stream.lock
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 )
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;
63 if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
65 intf_ErrMsg( "Unable to allocate memory in input_InitStream");
66 /* FIXME : find a way to tell if failed */
69 memset( p_input->stream.p_demux_data, 0, i_data_len );
73 /*****************************************************************************
74 * input_EndStream: free all stream descriptors
75 *****************************************************************************/
76 void input_EndStream( input_thread_t * p_input )
78 /* Free all programs and associated ES, and associated decoders. */
79 while( p_input->stream.i_pgrm_number )
81 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
84 /* Free standalone ES */
85 while( p_input->stream.i_es_number )
87 input_DelES( p_input, p_input->stream.pp_es[0] );
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 )
98 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
100 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
102 return p_input->stream.pp_programs[i];
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 )
117 /* Where to add the pgrm */
118 int i_pgrm_index = p_input->stream.i_pgrm_number;
120 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
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 )
129 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
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 )
138 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
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;
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;
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
157 p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
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;
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 )
170 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
173 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
177 return p_input->stream.pp_programs[i_pgrm_index];
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 )
191 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
193 /* Free the structures that describe the es that belongs to that program */
194 while( p_pgrm->i_es_number )
196 input_DelES( p_input, p_pgrm->pp_es[0] );
199 /* Free the demux data */
200 if( p_pgrm->p_demux_data != NULL )
202 free( p_pgrm->p_demux_data );
205 /* Find the program in the programs table */
206 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
209 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
213 /* Remove this program from the stream's list of programs */
214 p_input->stream.i_pgrm_number--;
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 *) );
222 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
224 intf_ErrMsg( "input error: unable to realloc program list"
225 " in input_DelProgram" );
228 /* Free the description of this program */
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 )
239 for( i = 0; i < p_input->stream.i_es_number; i++ )
241 if( p_input->stream.pp_es[i]->i_id == i_es_id )
243 return p_input->stream.pp_es[i];
250 /*****************************************************************************
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
256 *****************************************************************************/
257 es_descriptor_t * input_AddES( input_thread_t * p_input,
258 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
261 es_descriptor_t * p_es;
263 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
265 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
268 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
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 )
277 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
280 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
282 /* Init its values */
283 p_es->i_id = i_es_id;
284 p_es->b_discontinuity = 0;
286 p_es->p_decoder_fifo = NULL;
291 p_es->p_demux_data = malloc( i_data_len );
292 if( p_es->p_demux_data == NULL )
294 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
297 memset( p_es->p_demux_data, 0, i_data_len );
301 p_es->p_demux_data = NULL;
304 /* Add this ES to the program definition if one is given */
307 p_pgrm->i_es_number++;
308 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
310 * sizeof(es_descriptor_t *) );
311 if( p_pgrm->pp_es == NULL )
313 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
316 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
317 p_es->p_pgrm = p_pgrm;
327 /*****************************************************************************
329 *****************************************************************************/
330 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
332 int i_index, i_es_index;
333 pgrm_descriptor_t * p_pgrm;
336 p_pgrm = p_es->p_pgrm;
338 /* Kill associated decoder, if any. */
339 if( p_es->p_decoder_fifo != NULL )
341 input_EndDecoder( p_input, p_es );
344 /* Remove this ES from the description of the program if it is associated to
348 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
350 if( p_pgrm->pp_es[i_index] == p_es )
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,
356 * sizeof(es_descriptor_t *));
357 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
359 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
366 /* Free the demux data */
367 if( p_es->p_demux_data != NULL )
369 free( p_es->p_demux_data );
372 /* Find the ES in the ES table */
373 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
376 if( p_input->stream.pp_es[i_es_index] == 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 )
390 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
396 /*****************************************************************************
397 * input_DumpStream: dumps the contents of a stream descriptor
398 *****************************************************************************/
399 void input_DumpStream( input_thread_t * p_input )
402 #define S p_input->stream
403 intf_Msg( "input info: Dumping stream ID 0x%x", S.i_stream_id );
405 intf_Msg( "input info: seekable stream, position: %d/%d",
406 S.i_tell, S.i_size );
408 intf_Msg( "input info: %s", S.b_pace_control ? "pace controlled" :
409 "pace un-controlled" );
411 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
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" );
418 for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
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");
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 )
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;
442 if( (p_config->p_decoder_fifo =
443 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
445 intf_ErrMsg( "Out of memory" );
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;
458 p_config->pf_init_bit_stream = InitBitstream;
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 )
469 vdec_config_t * p_config;
471 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
472 if( p_config == NULL )
474 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
477 p_config->p_vout = p_input->p_default_vout;
478 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
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 )
493 adec_config_t * p_config;
495 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
496 if( p_config == NULL )
498 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
501 p_config->p_aout = p_input->p_default_aout;
502 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
511 /*****************************************************************************
512 * input_SelectES: selects an ES and spawns the associated decoder
513 *****************************************************************************/
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 * );
520 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
523 decoder_capabilities_t decoder;
526 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
529 if( p_es->p_decoder_fifo != NULL )
531 intf_ErrMsg( "ES %d is already selected", p_es->i_id );
535 switch( p_es->i_type )
539 if( p_main->b_audio )
541 decoder.pf_create_thread = adec_CreateThread;
542 p_es->thread_id = input_RunDecoder( &decoder,
543 (void *)GetAdecConfig( p_input, p_es ) );
549 if( p_main->b_video )
551 decoder.pf_create_thread = vpar_CreateThread;
552 p_es->thread_id = input_RunDecoder( &decoder,
553 (void *)GetVdecConfig( p_input, p_es ) );
558 if( p_main->b_audio )
560 decoder.pf_create_thread = ac3dec_CreateThread;
561 p_es->thread_id = input_RunDecoder( &decoder,
562 (void *)GetAdecConfig( p_input, p_es ) );
567 if( p_main->b_video )
569 decoder.pf_create_thread = spudec_CreateThread;
570 p_es->thread_id = input_RunDecoder( &decoder,
571 (void *)GetVdecConfig( p_input, p_es ) );
576 intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
581 if( p_es->thread_id == 0 )
586 if( p_es->p_decoder_fifo != NULL )
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 )
595 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
598 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]