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.22 2001/01/08 18:16:33 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 /* Don't put i instead of 0 !! */
92 input_DelES( p_input, p_input->stream.pp_es[0] );
96 /*****************************************************************************
97 * input_FindProgram: returns a pointer to a program described by its ID
98 *****************************************************************************/
99 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
103 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
105 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
107 return p_input->stream.pp_programs[i];
114 /*****************************************************************************
115 * input_AddProgram: add and init a program descriptor
116 *****************************************************************************
117 * This program descriptor will be referenced in the given stream descriptor
118 *****************************************************************************/
119 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
120 u16 i_pgrm_id, size_t i_data_len )
122 /* Where to add the pgrm */
123 int i_pgrm_index = p_input->stream.i_pgrm_number;
125 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
127 /* Add an entry to the list of program associated with the stream */
128 p_input->stream.i_pgrm_number++;
129 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
130 p_input->stream.i_pgrm_number
131 * sizeof(pgrm_descriptor_t *) );
132 if( p_input->stream.pp_programs == NULL )
134 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
138 /* Allocate the structure to store this description */
139 p_input->stream.pp_programs[i_pgrm_index] =
140 malloc( sizeof(pgrm_descriptor_t) );
141 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
143 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
147 /* Init this entry */
148 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
149 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
150 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
152 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
153 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
155 p_input->stream.pp_programs[i_pgrm_index]->delta_cr = 0;
156 p_input->stream.pp_programs[i_pgrm_index]->delta_absolute = 0;
157 p_input->stream.pp_programs[i_pgrm_index]->last_cr = 0;
158 p_input->stream.pp_programs[i_pgrm_index]->c_average_count = 0;
159 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
160 = SYNCHRO_NOT_STARTED;
161 p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
163 p_input->stream.pp_programs[i_pgrm_index]->p_vout
164 = p_input->p_default_vout;
165 p_input->stream.pp_programs[i_pgrm_index]->p_aout
166 = p_input->p_default_aout;
170 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
171 malloc( i_data_len );
172 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
174 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
177 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
181 return p_input->stream.pp_programs[i_pgrm_index];
184 /*****************************************************************************
185 * input_DelProgram: destroy a program descriptor
186 *****************************************************************************
187 * All ES descriptions referenced in the descriptor will be deleted.
188 *****************************************************************************/
189 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
191 int i_index, i_pgrm_index;
195 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
197 /* Free the structures that describe the es that belongs to that program */
198 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
200 /* Don't put i_index instead of 0 !! */
201 input_DelES( p_input, p_pgrm->pp_es[0] );
204 /* Free the demux data */
205 if( p_pgrm->p_demux_data != NULL )
207 free( p_pgrm->p_demux_data );
210 /* Find the program in the programs table */
211 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
214 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
218 /* Remove this program from the stream's list of programs */
219 p_input->stream.i_pgrm_number--;
221 p_input->stream.pp_programs[i_pgrm_index] =
222 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
223 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
224 p_input->stream.i_pgrm_number
225 * sizeof(pgrm_descriptor_t *) );
227 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
229 intf_ErrMsg( "input error: unable to realloc program list"
230 " in input_DelProgram" );
233 /* Free the description of this program */
237 /*****************************************************************************
238 * input_FindES: returns a pointer to an ES described by its ID
239 *****************************************************************************/
240 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
244 for( i = 0; i < p_input->stream.i_es_number; i++ )
246 if( p_input->stream.pp_es[i]->i_id == i_es_id )
248 return p_input->stream.pp_es[i];
255 /*****************************************************************************
257 *****************************************************************************
258 * Reserve a slot in the table of ES descriptors for the ES and add it to the
259 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
261 *****************************************************************************/
262 es_descriptor_t * input_AddES( input_thread_t * p_input,
263 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
266 es_descriptor_t * p_es;
268 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
270 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
273 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
276 p_input->stream.i_es_number++;
277 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
278 p_input->stream.i_es_number
279 * sizeof(es_descriptor_t *) );
280 if( p_input->stream.pp_es == NULL )
282 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
285 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
286 p_es->i_id = i_es_id;
288 /* Init its values */
289 p_es->b_discontinuity = 0;
291 p_es->p_decoder_fifo = NULL;
295 p_es->p_demux_data = malloc( i_data_len );
296 if( p_es->p_demux_data == NULL )
298 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
301 memset( p_es->p_demux_data, 0, i_data_len );
305 p_es->p_demux_data = NULL;
308 /* Add this ES to the program definition if one is given */
311 p_pgrm->i_es_number++;
312 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
314 * sizeof(es_descriptor_t *) );
315 if( p_pgrm->pp_es == NULL )
317 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
320 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
321 p_es->p_pgrm = p_pgrm;
331 /*****************************************************************************
333 *****************************************************************************/
334 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
336 int i_index, i_es_index;
337 pgrm_descriptor_t * p_pgrm;
340 p_pgrm = p_es->p_pgrm;
342 /* Kill associated decoder, if any. */
343 if( p_es->p_decoder_fifo != NULL )
345 input_EndDecoder( p_es->p_decoder_fifo, p_es->thread_id );
346 free( p_es->p_decoder_fifo );
349 /* Remove this ES from the description of the program if it is associated to
353 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
355 if( p_pgrm->pp_es[i_index] == p_es )
357 p_pgrm->i_es_number--;
358 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
359 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
361 * sizeof(es_descriptor_t *));
362 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
364 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
371 /* Free the demux data */
372 if( p_es->p_demux_data != NULL )
374 free( p_es->p_demux_data );
377 /* Find the ES in the ES table */
378 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
381 if( p_input->stream.pp_es[i_es_index] == p_es )
387 p_input->stream.i_es_number--;
388 p_input->stream.pp_es[i_es_index] =
389 p_input->stream.pp_es[p_input->stream.i_es_number];
390 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
391 p_input->stream.i_es_number
392 * sizeof(es_descriptor_t *));
393 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
395 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
401 /*****************************************************************************
402 * input_DumpStream: dumps the contents of a stream descriptor
403 *****************************************************************************/
404 void input_DumpStream( input_thread_t * p_input )
407 #define S p_input->stream
408 intf_Msg( "input info: Dumping stream ID 0x%x", S.i_stream_id );
410 intf_Msg( "input info: seekable stream, position: %d/%d",
411 S.i_tell, S.i_size );
413 intf_Msg( "input info: %s", S.b_pace_control ? "pace controlled" :
414 "pace un-controlled" );
416 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
418 #define P p_input->stream.pp_programs[i]
419 intf_Msg( "input info: Dumping program 0x%x, version %d (%s)",
420 P->i_number, P->i_version,
421 P->b_is_ok ? "complete" : "partial" );
422 if( P->i_synchro_state == SYNCHRO_OK )
423 intf_Msg( "input info: synchro absolute delta : %lld (jitter : %lld)",
424 P->delta_absolute, P->delta_cr );
426 for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
428 #define ES p_input->stream.pp_programs[i]->pp_es[j]
429 intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s",
430 ES->i_id, ES->i_stream_id, ES->i_type,
431 ES->p_decoder_fifo != NULL ? "selected" : "not selected");
438 /*****************************************************************************
439 * InitDecConfig: initializes a decoder_config_t
440 *****************************************************************************/
441 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
442 decoder_config_t * p_config )
444 p_config->i_id = p_es->i_id;
445 p_config->i_type = p_es->i_type;
446 p_config->p_stream_ctrl =
447 &p_input->stream.control;
450 if( (p_config->p_decoder_fifo =
451 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
453 intf_ErrMsg( "Out of memory" );
457 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
458 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
459 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
460 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
461 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
462 p_config->p_decoder_fifo->pf_delete_pes =
463 p_input->p_plugin->pf_delete_pes;
464 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
466 p_config->pf_init_bit_stream = InitBitstream;
471 /*****************************************************************************
472 * GetVdecConfig: returns a valid vdec_config_t
473 *****************************************************************************/
474 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
475 es_descriptor_t * p_es )
477 vdec_config_t * p_config;
479 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
480 if( p_config == 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 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
504 if( p_config == NULL )
506 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
509 p_config->p_aout = p_input->p_default_aout;
510 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
519 /*****************************************************************************
520 * input_SelectES: selects an ES and spawns the associated decoder
521 *****************************************************************************/
523 vlc_thread_t adec_CreateThread( void * );
524 vlc_thread_t ac3dec_CreateThread( void * );
525 vlc_thread_t vpar_CreateThread( void * );
526 vlc_thread_t spudec_CreateThread( void * );
528 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
531 decoder_capabilities_t decoder;
534 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
537 if( p_es->p_decoder_fifo != NULL )
539 intf_ErrMsg( "ES %d is already selected", p_es->i_id );
543 switch( p_es->i_type )
547 if( p_main->b_audio )
549 decoder.pf_create_thread = adec_CreateThread;
550 p_es->thread_id = input_RunDecoder( &decoder,
551 (void *)GetAdecConfig( p_input, p_es ) );
557 if( p_main->b_video )
559 decoder.pf_create_thread = vpar_CreateThread;
560 p_es->thread_id = input_RunDecoder( &decoder,
561 (void *)GetVdecConfig( p_input, p_es ) );
566 if( p_main->b_audio )
568 decoder.pf_create_thread = ac3dec_CreateThread;
569 p_es->thread_id = input_RunDecoder( &decoder,
570 (void *)GetAdecConfig( p_input, p_es ) );
575 if( p_main->b_video )
577 decoder.pf_create_thread = spudec_CreateThread;
578 p_es->thread_id = input_RunDecoder( &decoder,
579 (void *)GetVdecConfig( p_input, p_es ) );
584 intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
589 if( p_es->p_decoder_fifo != NULL )
591 p_input->stream.i_selected_es_number++;
592 p_input->stream.pp_selected_es = realloc(
593 p_input->stream.pp_selected_es,
594 p_input->stream.i_selected_es_number
595 * sizeof(es_descriptor_t *) );
596 if( p_input->stream.pp_selected_es == NULL )
598 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
601 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]