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.29 2001/02/08 04:43:28 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"
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 int 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");
68 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 )
79 /* Free all programs and associated ES, and associated decoders. */
80 while( p_input->stream.i_pgrm_number )
82 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
85 /* Free standalone ES */
86 while( p_input->stream.i_es_number )
88 input_DelES( p_input, p_input->stream.pp_es[0] );
92 /*****************************************************************************
93 * input_FindProgram: returns a pointer to a program described by its ID
94 *****************************************************************************/
95 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
99 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
101 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
103 return p_input->stream.pp_programs[i];
110 /*****************************************************************************
111 * input_AddProgram: add and init a program descriptor
112 *****************************************************************************
113 * This program descriptor will be referenced in the given stream descriptor
114 *****************************************************************************/
115 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
116 u16 i_pgrm_id, size_t i_data_len )
118 /* Where to add the pgrm */
119 int i_pgrm_index = p_input->stream.i_pgrm_number;
121 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
123 /* Add an entry to the list of program associated with the stream */
124 p_input->stream.i_pgrm_number++;
125 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
126 p_input->stream.i_pgrm_number
127 * sizeof(pgrm_descriptor_t *) );
128 if( p_input->stream.pp_programs == NULL )
130 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
134 /* Allocate the structure to store this description */
135 p_input->stream.pp_programs[i_pgrm_index] =
136 malloc( sizeof(pgrm_descriptor_t) );
137 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
139 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
143 /* Init this entry */
144 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
145 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
146 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
148 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
149 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
151 input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
153 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
155 p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
157 p_input->stream.pp_programs[i_pgrm_index]->p_vout
158 = p_input->p_default_vout;
159 p_input->stream.pp_programs[i_pgrm_index]->p_aout
160 = p_input->p_default_aout;
164 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
165 malloc( i_data_len );
166 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
168 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
171 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
175 return p_input->stream.pp_programs[i_pgrm_index];
178 /*****************************************************************************
179 * input_DelProgram: destroy a program descriptor
180 *****************************************************************************
181 * All ES descriptions referenced in the descriptor will be deleted.
182 *****************************************************************************/
183 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
189 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
191 /* Free the structures that describe the es that belongs to that program */
192 while( p_pgrm->i_es_number )
194 input_DelES( p_input, p_pgrm->pp_es[0] );
197 /* Free the demux data */
198 if( p_pgrm->p_demux_data != NULL )
200 free( p_pgrm->p_demux_data );
203 /* Find the program in the programs table */
204 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
207 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
211 /* Remove this program from the stream's list of programs */
212 p_input->stream.i_pgrm_number--;
214 p_input->stream.pp_programs[i_pgrm_index] =
215 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
216 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
217 p_input->stream.i_pgrm_number
218 * sizeof(pgrm_descriptor_t *) );
220 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
222 intf_ErrMsg( "input error: unable to realloc program list"
223 " in input_DelProgram" );
226 /* Free the description of this program */
230 /*****************************************************************************
231 * input_FindES: returns a pointer to an ES described by its ID
232 *****************************************************************************/
233 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
237 for( i = 0; i < p_input->stream.i_es_number; i++ )
239 if( p_input->stream.pp_es[i]->i_id == i_es_id )
241 return p_input->stream.pp_es[i];
248 /*****************************************************************************
250 *****************************************************************************
251 * Reserve a slot in the table of ES descriptors for the ES and add it to the
252 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
254 *****************************************************************************/
255 es_descriptor_t * input_AddES( input_thread_t * p_input,
256 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
259 es_descriptor_t * p_es;
261 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
263 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
266 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
269 p_input->stream.i_es_number++;
270 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
271 p_input->stream.i_es_number
272 * sizeof(es_descriptor_t *) );
273 if( p_input->stream.pp_es == NULL )
275 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
278 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
280 /* Init its values */
281 p_es->i_id = i_es_id;
282 p_es->b_discontinuity = 0;
284 p_es->p_decoder_fifo = NULL;
289 p_es->p_demux_data = malloc( i_data_len );
290 if( p_es->p_demux_data == NULL )
292 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
295 memset( p_es->p_demux_data, 0, i_data_len );
299 p_es->p_demux_data = NULL;
302 /* Add this ES to the program definition if one is given */
305 p_pgrm->i_es_number++;
306 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
308 * sizeof(es_descriptor_t *) );
309 if( p_pgrm->pp_es == NULL )
311 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
314 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
315 p_es->p_pgrm = p_pgrm;
325 /*****************************************************************************
327 *****************************************************************************/
328 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
330 int i_index, i_es_index;
331 pgrm_descriptor_t * p_pgrm;
334 p_pgrm = p_es->p_pgrm;
336 /* Kill associated decoder, if any. */
337 if( p_es->p_decoder_fifo != NULL )
339 input_EndDecoder( p_input, p_es );
342 /* Remove this ES from the description of the program if it is associated to
346 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
348 if( p_pgrm->pp_es[i_index] == p_es )
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,
354 * sizeof(es_descriptor_t *));
355 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
357 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
364 /* Free the demux data */
365 if( p_es->p_demux_data != NULL )
367 free( p_es->p_demux_data );
370 /* Find the ES in the ES table */
371 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
374 if( p_input->stream.pp_es[i_es_index] == 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 )
388 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
394 /*****************************************************************************
395 * input_DumpStream: dumps the contents of a stream descriptor
396 *****************************************************************************/
397 void input_DumpStream( input_thread_t * p_input )
400 #define S p_input->stream
401 intf_Msg( "input info: Dumping stream ID 0x%x", S.i_stream_id );
403 intf_Msg( "input info: seekable stream, position: %d/%d",
404 S.i_tell, S.i_size );
406 intf_Msg( "input info: %s", S.b_pace_control ? "pace controlled" :
407 "pace un-controlled" );
409 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
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" );
416 for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
418 #define ES p_input->stream.pp_programs[i]->pp_es[j]
419 intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s",
420 ES->i_id, ES->i_stream_id, ES->i_type,
421 ES->p_decoder_fifo != NULL ? "selected" : "not selected");
428 /*****************************************************************************
429 * InitDecConfig: initializes a decoder_config_t
430 *****************************************************************************/
431 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
432 decoder_config_t * p_config )
434 p_config->i_id = p_es->i_id;
435 p_config->i_type = p_es->i_type;
436 p_config->p_stream_ctrl =
437 &p_input->stream.control;
440 if( (p_config->p_decoder_fifo =
441 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
443 intf_ErrMsg( "Out of memory" );
447 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
448 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
449 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
450 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
451 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
452 p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
453 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
455 p_config->pf_init_bit_stream = InitBitstream;
460 /*****************************************************************************
461 * GetVdecConfig: returns a valid vdec_config_t
462 *****************************************************************************/
463 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
464 es_descriptor_t * p_es )
466 vdec_config_t * p_config;
468 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
469 if( p_config == NULL )
471 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
474 p_config->p_vout = p_input->p_default_vout;
475 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
484 /*****************************************************************************
485 * GetAdecConfig: returns a valid adec_config_t
486 *****************************************************************************/
487 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
488 es_descriptor_t * p_es )
490 adec_config_t * p_config;
492 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
493 if( p_config == NULL )
495 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
498 p_config->p_aout = p_input->p_default_aout;
499 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
508 /*****************************************************************************
509 * input_SelectES: selects an ES and spawns the associated decoder
510 *****************************************************************************/
512 vlc_thread_t adec_CreateThread( void * );
513 vlc_thread_t ac3dec_CreateThread( void * );
514 vlc_thread_t vpar_CreateThread( void * );
515 vlc_thread_t spudec_CreateThread( void * );
517 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
520 decoder_capabilities_t decoder;
523 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
526 if( p_es->p_decoder_fifo != NULL )
528 intf_ErrMsg( "ES %d is already selected", p_es->i_id );
532 switch( p_es->i_type )
536 if( p_main->b_audio )
538 decoder.pf_create_thread = adec_CreateThread;
539 p_es->thread_id = input_RunDecoder( &decoder,
540 (void *)GetAdecConfig( p_input, p_es ) );
546 if( p_main->b_video )
548 decoder.pf_create_thread = vpar_CreateThread;
549 p_es->thread_id = input_RunDecoder( &decoder,
550 (void *)GetVdecConfig( p_input, p_es ) );
555 if( p_main->b_audio )
557 decoder.pf_create_thread = ac3dec_CreateThread;
558 p_es->thread_id = input_RunDecoder( &decoder,
559 (void *)GetAdecConfig( p_input, p_es ) );
564 if( p_main->b_video )
566 decoder.pf_create_thread = spudec_CreateThread;
567 p_es->thread_id = input_RunDecoder( &decoder,
568 (void *)GetVdecConfig( p_input, p_es ) );
573 intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
578 if( p_es->thread_id == 0 )
583 if( p_es->p_decoder_fifo != NULL )
585 p_input->stream.i_selected_es_number++;
586 p_input->stream.pp_selected_es = realloc(
587 p_input->stream.pp_selected_es,
588 p_input->stream.i_selected_es_number
589 * sizeof(es_descriptor_t *) );
590 if( p_input->stream.pp_selected_es == NULL )
592 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
595 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]