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.19 2001/01/07 03:56:40 henri 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] );
87 free( p_input->stream.pp_programs );
89 /* Free standalone ES */
90 for( i = 0; i < p_input->stream.i_es_number; i++ )
92 input_DelES( p_input, p_input->stream.pp_es[0] );
94 free( p_input->stream.pp_es );
95 free( p_input->stream.pp_selected_es );
98 /*****************************************************************************
99 * input_FindProgram: returns a pointer to a program described by its ID
100 *****************************************************************************/
101 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
105 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
107 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
109 return p_input->stream.pp_programs[i];
116 /*****************************************************************************
117 * input_AddProgram: add and init a program descriptor
118 *****************************************************************************
119 * This program descriptor will be referenced in the given stream descriptor
120 *****************************************************************************/
121 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
122 u16 i_pgrm_id, size_t i_data_len )
124 /* Where to add the pgrm */
125 int i_pgrm_index = p_input->stream.i_pgrm_number;
127 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
129 /* Add an entry to the list of program associated with the stream */
130 p_input->stream.i_pgrm_number++;
131 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
132 p_input->stream.i_pgrm_number
133 * sizeof(pgrm_descriptor_t *) );
134 if( p_input->stream.pp_programs == NULL )
136 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
140 /* Allocate the structure to store this description */
141 p_input->stream.pp_programs[i_pgrm_index] =
142 malloc( sizeof(pgrm_descriptor_t) );
143 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
145 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
149 /* Init this entry */
150 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
151 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
152 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
154 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
155 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
157 p_input->stream.pp_programs[i_pgrm_index]->delta_cr = 0;
158 p_input->stream.pp_programs[i_pgrm_index]->delta_absolute = 0;
159 p_input->stream.pp_programs[i_pgrm_index]->last_cr = 0;
160 p_input->stream.pp_programs[i_pgrm_index]->c_average_count = 0;
161 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
162 = SYNCHRO_NOT_STARTED;
163 p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
165 p_input->stream.pp_programs[i_pgrm_index]->p_vout
166 = p_input->p_default_vout;
167 p_input->stream.pp_programs[i_pgrm_index]->p_aout
168 = p_input->p_default_aout;
172 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
173 malloc( i_data_len );
174 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
176 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
179 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
183 return p_input->stream.pp_programs[i_pgrm_index];
186 /*****************************************************************************
187 * input_DelProgram: destroy a program descriptor
188 *****************************************************************************
189 * All ES descriptions referenced in the descriptor will be deleted.
190 *****************************************************************************/
191 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
193 int i_index, i_pgrm_index;
197 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
199 /* Free the structures that describe the es that belongs to that program */
200 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
202 input_DelES( p_input, p_pgrm->pp_es[i_index] );
205 /* Free the table of es descriptors */
206 free( p_pgrm->pp_es );
208 /* Free the demux data */
209 if( p_pgrm->p_demux_data != NULL )
211 free( p_pgrm->p_demux_data );
214 /* Find the program in the programs table */
215 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
218 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
222 /* Remove this program from the stream's list of programs */
223 p_input->stream.i_pgrm_number--;
224 p_input->stream.pp_programs[i_pgrm_index] =
225 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
226 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
227 p_input->stream.i_pgrm_number
228 * sizeof(pgrm_descriptor_t *) );
230 if( p_input->stream.pp_programs == NULL)
232 intf_ErrMsg( "Unable to realloc memory in input_DelProgram" );
234 /* Free the description of this program */
238 /*****************************************************************************
239 * input_FindES: returns a pointer to an ES described by its ID
240 *****************************************************************************/
241 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
245 for( i = 0; i < p_input->stream.i_es_number; i++ )
247 if( p_input->stream.pp_es[i]->i_id == i_es_id )
249 return p_input->stream.pp_es[i];
256 /*****************************************************************************
258 *****************************************************************************
259 * Reserve a slot in the table of ES descriptors for the ES and add it to the
260 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
262 *****************************************************************************/
263 es_descriptor_t * input_AddES( input_thread_t * p_input,
264 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
267 es_descriptor_t * p_es;
269 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
271 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
274 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
277 p_input->stream.i_es_number++;
278 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
279 p_input->stream.i_es_number
280 * sizeof(es_descriptor_t *) );
281 if( p_input->stream.pp_es == NULL )
283 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
286 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
287 p_es->i_id = i_es_id;
289 /* Init its values */
290 p_es->b_discontinuity = 0;
292 p_es->p_decoder_fifo = NULL;
296 p_es->p_demux_data = malloc( i_data_len );
297 if( p_es->p_demux_data == NULL )
299 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
302 memset( p_es->p_demux_data, 0, i_data_len );
306 p_es->p_demux_data = NULL;
309 /* Add this ES to the program definition if one is given */
312 p_pgrm->i_es_number++;
313 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
315 * sizeof(es_descriptor_t *) );
316 if( p_pgrm->pp_es == NULL )
318 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
321 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
322 p_es->p_pgrm = p_pgrm;
332 /*****************************************************************************
334 *****************************************************************************/
335 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
337 int i_index, i_es_index;
338 pgrm_descriptor_t * p_pgrm;
341 p_pgrm = p_es->p_pgrm;
343 /* Kill associated decoder, if any. */
344 if( p_es->p_decoder_fifo != NULL )
346 input_EndDecoder( p_es->p_decoder_fifo, p_es->thread_id );
347 free( p_es->p_decoder_fifo );
350 /* Remove this ES from the description of the program if it is associated to
354 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
356 if( p_pgrm->pp_es[i_index] == p_es )
358 p_pgrm->i_es_number--;
359 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
360 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
362 * sizeof(es_descriptor_t *));
363 if( p_pgrm->pp_es == NULL )
365 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
372 /* Free the demux data */
373 if( p_es->p_demux_data != NULL )
375 free( p_es->p_demux_data );
378 /* Find the ES in the ES table */
379 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
382 if( p_input->stream.pp_es[i_es_index] == p_es )
388 p_input->stream.i_es_number--;
389 p_input->stream.pp_es[i_es_index] =
390 p_input->stream.pp_es[p_input->stream.i_es_number];
391 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
392 p_input->stream.i_es_number
393 * sizeof(es_descriptor_t *));
394 if( p_input->stream.pp_es == NULL )
396 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
402 /*****************************************************************************
403 * input_DumpStream: dumps the contents of a stream descriptor
404 *****************************************************************************/
405 void input_DumpStream( input_thread_t * p_input )
408 #define S p_input->stream
409 intf_Msg( "input info: Dumping stream ID 0x%x", S.i_stream_id );
411 intf_Msg( "input info: seekable stream, position: %d/%d",
412 S.i_tell, S.i_size );
414 intf_Msg( "input info: %s", S.b_pace_control ? "pace controlled" :
415 "pace un-controlled" );
417 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
419 #define P p_input->stream.pp_programs[i]
420 intf_Msg( "input info: Dumping program 0x%x, version %d (%s)",
421 P->i_number, P->i_version,
422 P->b_is_ok ? "complete" : "partial" );
423 if( P->i_synchro_state == SYNCHRO_OK )
424 intf_Msg( "input info: synchro absolute delta : %lld (jitter : %lld)",
425 P->delta_absolute, P->delta_cr );
427 for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
429 #define ES p_input->stream.pp_programs[i]->pp_es[j]
430 intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s",
431 ES->i_id, ES->i_stream_id, ES->i_type,
432 ES->p_decoder_fifo != NULL ? "selected" : "not selected");
439 /*****************************************************************************
440 * InitDecConfig: initializes a decoder_config_t
441 *****************************************************************************/
442 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
443 decoder_config_t * p_config )
445 p_config->i_id = p_es->i_id;
446 p_config->i_type = p_es->i_type;
447 p_config->p_stream_ctrl =
448 &p_input->stream.control;
451 if( (p_config->p_decoder_fifo =
452 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
454 intf_ErrMsg( "Out of memory" );
458 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
459 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
460 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
461 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
462 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
463 p_config->p_decoder_fifo->pf_delete_pes =
464 p_input->p_plugin->pf_delete_pes;
465 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
467 p_config->pf_init_bit_stream = InitBitstream;
472 /*****************************************************************************
473 * GetVdecConfig: returns a valid vdec_config_t
474 *****************************************************************************/
475 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
476 es_descriptor_t * p_es )
478 vdec_config_t * p_config;
480 if( (p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) )) == NULL)
482 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
485 p_config->p_vout = p_input->p_default_vout;
486 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
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 )
501 adec_config_t * p_config;
503 if ( (p_config = (adec_config_t *)malloc( sizeof(adec_config_t))) ==NULL )
505 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
508 p_config->p_aout = p_input->p_default_aout;
509 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
518 /*****************************************************************************
519 * input_SelectES: selects an ES and spawns the associated decoder
520 *****************************************************************************/
522 vlc_thread_t adec_CreateThread( void * );
523 vlc_thread_t ac3dec_CreateThread( void * );
524 vlc_thread_t vpar_CreateThread( void * );
525 vlc_thread_t spudec_CreateThread( void * );
527 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
530 decoder_capabilities_t decoder;
533 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
536 if( p_es->p_decoder_fifo != NULL )
538 intf_ErrMsg( "ES %d is already selected", p_es->i_id );
542 switch( p_es->i_type )
546 if( p_main->b_audio )
548 decoder.pf_create_thread = adec_CreateThread;
549 p_es->thread_id = input_RunDecoder( &decoder,
550 (void *)GetAdecConfig( p_input, p_es ) );
556 if( p_main->b_video )
558 decoder.pf_create_thread = vpar_CreateThread;
559 p_es->thread_id = input_RunDecoder( &decoder,
560 (void *)GetVdecConfig( p_input, p_es ) );
565 if( p_main->b_audio )
567 decoder.pf_create_thread = ac3dec_CreateThread;
568 p_es->thread_id = input_RunDecoder( &decoder,
569 (void *)GetAdecConfig( p_input, p_es ) );
574 if( p_main->b_video )
576 decoder.pf_create_thread = spudec_CreateThread;
577 p_es->thread_id = input_RunDecoder( &decoder,
578 (void *)GetVdecConfig( p_input, p_es ) );
583 intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
588 if( p_es->p_decoder_fifo != NULL )
590 p_input->stream.i_selected_es_number++;
591 p_input->stream.pp_selected_es = realloc(
592 p_input->stream.pp_selected_es,
593 p_input->stream.i_selected_es_number
594 * sizeof(es_descriptor_t *) );
595 if( p_input->stream.pp_selected_es == NULL )
597 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
600 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]