1 /*****************************************************************************
2 * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
3 * FIXME : check the return value of realloc() and malloc() !
4 *****************************************************************************
5 * Copyright (C) 1999, 2000 VideoLAN
6 * $Id: input_programs.c,v 1.18 2000/12/28 18:00:38 massiot Exp $
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
40 #include "stream_control.h"
41 #include "input_ext-intf.h"
42 #include "input_ext-dec.h"
44 #include "input_dec.h"
46 #include "main.h" /* --noaudio --novideo */
49 * NOTICE : all of these functions expect you to have taken the lock on
50 * p_input->stream.lock
53 /*****************************************************************************
54 * input_InitStream: init the stream descriptor of the given input
55 *****************************************************************************/
56 void input_InitStream( input_thread_t * p_input, size_t i_data_len )
58 p_input->stream.i_stream_id = 0;
59 p_input->stream.pp_es = NULL;
60 p_input->stream.pp_selected_es = NULL;
61 p_input->stream.pp_programs = NULL;
65 p_input->stream.p_demux_data = malloc( i_data_len );
66 memset( p_input->stream.p_demux_data, 0, i_data_len );
70 /*****************************************************************************
71 * input_EndStream: free all stream descriptors
72 *****************************************************************************/
73 void input_EndStream( input_thread_t * p_input )
77 /* Free all programs and associated ES, and associated decoders. */
78 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
80 /* Don't put i instead of 0 !! */
81 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
83 free( p_input->stream.pp_programs );
85 /* Free standalone ES */
86 for( i = 0; i < p_input->stream.i_es_number; i++ )
88 input_DelES( p_input, p_input->stream.pp_es[0] );
90 free( p_input->stream.pp_es );
91 free( p_input->stream.pp_selected_es );
94 /*****************************************************************************
95 * input_FindProgram: returns a pointer to a program described by its ID
96 *****************************************************************************/
97 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
101 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
103 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
105 return p_input->stream.pp_programs[i];
112 /*****************************************************************************
113 * input_AddProgram: add and init a program descriptor
114 *****************************************************************************
115 * This program descriptor will be referenced in the given stream descriptor
116 *****************************************************************************/
117 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
118 u16 i_pgrm_id, size_t i_data_len )
120 /* Where to add the pgrm */
121 int i_pgrm_index = p_input->stream.i_pgrm_number;
123 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
125 /* Add an entry to the list of program associated with the stream */
126 p_input->stream.i_pgrm_number++;
127 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
128 p_input->stream.i_pgrm_number
129 * sizeof(pgrm_descriptor_t *) );
131 /* Allocate the structure to store this description */
132 p_input->stream.pp_programs[i_pgrm_index] =
133 malloc( sizeof(pgrm_descriptor_t) );
135 /* Init this entry */
136 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
137 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
138 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
140 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
141 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
143 p_input->stream.pp_programs[i_pgrm_index]->delta_cr = 0;
144 p_input->stream.pp_programs[i_pgrm_index]->delta_absolute = 0;
145 p_input->stream.pp_programs[i_pgrm_index]->last_cr = 0;
146 p_input->stream.pp_programs[i_pgrm_index]->c_average_count = 0;
147 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
148 = SYNCHRO_NOT_STARTED;
149 p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
151 p_input->stream.pp_programs[i_pgrm_index]->p_vout
152 = p_input->p_default_vout;
153 p_input->stream.pp_programs[i_pgrm_index]->p_aout
154 = p_input->p_default_aout;
158 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
159 malloc( i_data_len );
160 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
164 return p_input->stream.pp_programs[i_pgrm_index];
167 /*****************************************************************************
168 * input_DelProgram: destroy a program descriptor
169 *****************************************************************************
170 * All ES descriptions referenced in the descriptor will be deleted.
171 *****************************************************************************/
172 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
174 int i_index, i_pgrm_index;
178 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
180 /* Free the structures that describe the es that belongs to that program */
181 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
183 input_DelES( p_input, p_pgrm->pp_es[i_index] );
186 /* Free the table of es descriptors */
187 free( p_pgrm->pp_es );
189 /* Free the demux data */
190 if( p_pgrm->p_demux_data != NULL )
192 free( p_pgrm->p_demux_data );
195 /* Find the program in the programs table */
196 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
199 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
203 /* Remove this program from the stream's list of programs */
204 p_input->stream.i_pgrm_number--;
205 p_input->stream.pp_programs[i_pgrm_index] =
206 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
207 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
208 p_input->stream.i_pgrm_number
209 * sizeof(pgrm_descriptor_t *) );
211 /* Free the description of this program */
215 /*****************************************************************************
216 * input_FindES: returns a pointer to an ES described by its ID
217 *****************************************************************************/
218 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
222 for( i = 0; i < p_input->stream.i_es_number; i++ )
224 if( p_input->stream.pp_es[i]->i_id == i_es_id )
226 return p_input->stream.pp_es[i];
233 /*****************************************************************************
235 *****************************************************************************
236 * Reserve a slot in the table of ES descriptors for the ES and add it to the
237 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
239 *****************************************************************************/
240 es_descriptor_t * input_AddES( input_thread_t * p_input,
241 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
244 es_descriptor_t * p_es;
246 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
248 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
249 p_input->stream.i_es_number++;
250 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
251 p_input->stream.i_es_number
252 * sizeof(es_descriptor_t *) );
253 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
254 p_es->i_id = i_es_id;
256 /* Init its values */
257 p_es->b_discontinuity = 0;
259 p_es->p_decoder_fifo = NULL;
263 p_es->p_demux_data = malloc( i_data_len );
264 memset( p_es->p_demux_data, 0, i_data_len );
268 p_es->p_demux_data = NULL;
271 /* Add this ES to the program definition if one is given */
274 p_pgrm->i_es_number++;
275 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
277 * sizeof(es_descriptor_t *) );
278 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
279 p_es->p_pgrm = p_pgrm;
289 /*****************************************************************************
291 *****************************************************************************/
292 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
294 int i_index, i_es_index;
295 pgrm_descriptor_t * p_pgrm;
298 p_pgrm = p_es->p_pgrm;
300 /* Kill associated decoder, if any. */
301 if( p_es->p_decoder_fifo != NULL )
303 input_EndDecoder( p_es->p_decoder_fifo, p_es->thread_id );
304 free( p_es->p_decoder_fifo );
307 /* Remove this ES from the description of the program if it is associated to
311 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
313 if( p_pgrm->pp_es[i_index] == p_es )
315 p_pgrm->i_es_number--;
316 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
317 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
319 * sizeof(es_descriptor_t *));
325 /* Free the demux data */
326 if( p_es->p_demux_data != NULL )
328 free( p_es->p_demux_data );
331 /* Find the ES in the ES table */
332 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
335 if( p_input->stream.pp_es[i_es_index] == p_es )
341 p_input->stream.i_es_number--;
342 p_input->stream.pp_es[i_es_index] =
343 p_input->stream.pp_es[p_input->stream.i_es_number];
344 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
345 p_input->stream.i_es_number
346 * sizeof(es_descriptor_t *));
350 /*****************************************************************************
351 * input_DumpStream: dumps the contents of a stream descriptor
352 *****************************************************************************/
353 void input_DumpStream( input_thread_t * p_input )
356 #define S p_input->stream
357 intf_Msg( "input info: Dumping stream ID 0x%x", S.i_stream_id );
359 intf_Msg( "input info: seekable stream, position: %d/%d",
360 S.i_tell, S.i_size );
362 intf_Msg( "input info: %s", S.b_pace_control ? "pace controlled" :
363 "pace un-controlled" );
365 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
367 #define P p_input->stream.pp_programs[i]
368 intf_Msg( "input info: Dumping program 0x%x, version %d (%s)",
369 P->i_number, P->i_version,
370 P->b_is_ok ? "complete" : "partial" );
371 if( P->i_synchro_state == SYNCHRO_OK )
372 intf_Msg( "input info: synchro absolute delta : %lld (jitter : %lld)",
373 P->delta_absolute, P->delta_cr );
375 for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
377 #define ES p_input->stream.pp_programs[i]->pp_es[j]
378 intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s",
379 ES->i_id, ES->i_stream_id, ES->i_type,
380 ES->p_decoder_fifo != NULL ? "selected" : "not selected");
387 /*****************************************************************************
388 * InitDecConfig: initializes a decoder_config_t
389 *****************************************************************************/
390 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
391 decoder_config_t * p_config )
393 p_config->i_id = p_es->i_id;
394 p_config->i_type = p_es->i_type;
395 p_config->p_stream_ctrl =
396 &p_input->stream.control;
399 if( (p_config->p_decoder_fifo =
400 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
402 intf_ErrMsg( "Out of memory" );
406 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
407 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
408 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
409 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
410 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
411 p_config->p_decoder_fifo->pf_delete_pes =
412 p_input->p_plugin->pf_delete_pes;
413 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
415 p_config->pf_init_bit_stream = InitBitstream;
420 /*****************************************************************************
421 * GetVdecConfig: returns a valid vdec_config_t
422 *****************************************************************************/
423 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
424 es_descriptor_t * p_es )
426 vdec_config_t * p_config;
428 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
429 p_config->p_vout = p_input->p_default_vout;
430 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
439 /*****************************************************************************
440 * GetAdecConfig: returns a valid adec_config_t
441 *****************************************************************************/
442 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
443 es_descriptor_t * p_es )
445 adec_config_t * p_config;
447 p_config = (adec_config_t *)malloc( sizeof(adec_config_t) );
448 p_config->p_aout = p_input->p_default_aout;
449 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
458 /*****************************************************************************
459 * input_SelectES: selects an ES and spawns the associated decoder
460 *****************************************************************************/
462 vlc_thread_t adec_CreateThread( void * );
463 vlc_thread_t ac3dec_CreateThread( void * );
464 vlc_thread_t vpar_CreateThread( void * );
465 vlc_thread_t spudec_CreateThread( void * );
467 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
470 decoder_capabilities_t decoder;
473 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
476 if( p_es->p_decoder_fifo != NULL )
478 intf_ErrMsg( "ES %d is already selected", p_es->i_id );
482 switch( p_es->i_type )
486 if( p_main->b_audio )
488 decoder.pf_create_thread = adec_CreateThread;
489 p_es->thread_id = input_RunDecoder( &decoder,
490 (void *)GetAdecConfig( p_input, p_es ) );
496 if( p_main->b_video )
498 decoder.pf_create_thread = vpar_CreateThread;
499 p_es->thread_id = input_RunDecoder( &decoder,
500 (void *)GetVdecConfig( p_input, p_es ) );
505 if( p_main->b_audio )
507 decoder.pf_create_thread = ac3dec_CreateThread;
508 p_es->thread_id = input_RunDecoder( &decoder,
509 (void *)GetAdecConfig( p_input, p_es ) );
514 if( p_main->b_video )
516 decoder.pf_create_thread = spudec_CreateThread;
517 p_es->thread_id = input_RunDecoder( &decoder,
518 (void *)GetVdecConfig( p_input, p_es ) );
523 intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
528 if( p_es->p_decoder_fifo != NULL )
530 p_input->stream.i_selected_es_number++;
531 p_input->stream.pp_selected_es = realloc(
532 p_input->stream.pp_selected_es,
533 p_input->stream.i_selected_es_number
534 * sizeof(es_descriptor_t *) );
535 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]