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.11 2000/12/21 13:54:15 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"
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.i_pgrm_number = 0;
59 p_input->stream.pp_programs = NULL;
63 p_input->stream.p_demux_data = malloc( i_data_len );
64 memset( p_input->stream.p_demux_data, 0, i_data_len );
68 /*****************************************************************************
69 * input_AddProgram: add and init a program descriptor
70 *****************************************************************************
71 * This program descriptor will be referenced in the given stream descriptor
72 *****************************************************************************/
73 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
74 u16 i_pgrm_id, size_t i_data_len )
76 /* Where to add the pgrm */
77 int i_pgrm_index = p_input->stream.i_pgrm_number;
79 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
81 /* Add an entry to the list of program associated with the stream */
82 p_input->stream.i_pgrm_number++;
83 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
84 p_input->stream.i_pgrm_number
85 * sizeof(pgrm_descriptor_t *) );
87 /* Allocate the structure to store this description */
88 p_input->stream.pp_programs[i_pgrm_index] =
89 malloc( sizeof(pgrm_descriptor_t) );
92 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
93 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
94 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
96 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
97 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
99 p_input->stream.pp_programs[i_pgrm_index]->delta_cr = 0;
100 p_input->stream.pp_programs[i_pgrm_index]->delta_absolute = 0;
101 p_input->stream.pp_programs[i_pgrm_index]->last_cr = 0;
102 p_input->stream.pp_programs[i_pgrm_index]->c_average_count = 0;
103 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
104 = SYNCHRO_NOT_STARTED;
105 p_input->stream.pp_programs[i_pgrm_index]->b_discontinuity = 0;
107 p_input->stream.pp_programs[i_pgrm_index]->p_vout
108 = p_input->p_default_vout;
109 p_input->stream.pp_programs[i_pgrm_index]->p_aout
110 = p_input->p_default_aout;
114 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
115 malloc( i_data_len );
116 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
120 return p_input->stream.pp_programs[i_pgrm_index];
123 /*****************************************************************************
124 * input_DelProgram: destroy a program descriptor
125 *****************************************************************************
126 * All ES descriptions referenced in the descriptor will be deleted.
127 *****************************************************************************/
128 void input_DelProgram( input_thread_t * p_input, u16 i_pgrm_id )
130 int i_index, i_pgrm_index = -1;
131 pgrm_descriptor_t * p_pgrm = NULL;
133 intf_DbgMsg("Deleting description for pgrm %d", i_pgrm_id);
135 /* Find where this program is described */
136 for( i_index = 0; i_index < p_input->stream.i_pgrm_number; i_index++ )
138 if( p_input->stream.pp_programs[i_index]->i_number == i_pgrm_id )
140 i_pgrm_index = i_index;
141 p_pgrm = p_input->stream.pp_programs[ i_pgrm_index ];
146 /* Make sure that the pgrm exists */
147 ASSERT(i_pgrm_index >= 0);
150 /* Free the structures that describe the es that belongs to that program */
151 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
153 input_DelES( p_input, p_pgrm->pp_es[i_index]->i_id );
156 /* Free the table of es descriptors */
157 free( p_pgrm->pp_es );
159 /* Free the demux data */
160 if( p_pgrm->p_demux_data != NULL )
162 free( p_pgrm->p_demux_data );
165 /* Free the description of this stream */
168 /* Remove this program from the stream's list of programs */
169 p_input->stream.i_pgrm_number--;
170 p_input->stream.pp_programs[i_pgrm_index] =
171 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
172 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
173 p_input->stream.i_pgrm_number
174 * sizeof(pgrm_descriptor_t *) );
177 /*****************************************************************************
179 *****************************************************************************
180 * Reserve a slot in the table of ES descriptors for the ES and add it to the
181 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
183 *****************************************************************************/
184 es_descriptor_t * input_AddES( input_thread_t * p_input,
185 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
188 es_descriptor_t * p_es;
190 intf_DbgMsg("Adding description for ES %d", i_es_id);
192 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
193 p_input->i_es_number++;
194 p_input->pp_es = realloc( p_input->pp_es, p_input->i_es_number
195 * sizeof(es_descriptor_t *) );
196 p_input->pp_es[p_input->i_es_number - 1] = p_es;
197 p_es->i_id = i_es_id;
199 /* Init its values */
200 p_es->b_discontinuity = 0;
202 p_es->p_decoder_fifo = NULL;
206 p_es->p_demux_data = malloc( i_data_len );
207 memset( p_es->p_demux_data, 0, i_data_len );
210 /* Add this ES to the program definition if one is given */
213 p_pgrm->i_es_number++;
214 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
216 * sizeof(es_descriptor_t *) );
217 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
218 p_es->p_pgrm = p_pgrm;
228 /*****************************************************************************
230 *****************************************************************************/
231 void input_DelES( input_thread_t * p_input, u16 i_id )
234 pgrm_descriptor_t * p_pgrm = NULL;
235 es_descriptor_t * p_es = NULL;
237 /* Look for the description of the ES */
238 for( i_es = 0; i_es < p_input->i_es_number; i_es++ )
240 if( p_input->pp_es[i_es]->i_id == i_id )
242 p_es = p_input->pp_es[i_es];
243 p_pgrm = p_input->pp_es[i_es]->p_pgrm;
250 /* Remove this ES from the description of the program if it is associated to
254 for( i_index = 0; ; i_index++ )
256 if( p_pgrm->pp_es[i_index]->i_id == i_id )
258 p_pgrm->i_es_number--;
259 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
260 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
262 * sizeof(es_descriptor_t *));
268 /* Free the demux data */
269 if( p_es->p_demux_data != NULL )
271 free( p_es->p_demux_data );
276 p_input->i_es_number--;
277 p_input->pp_es[i_es] = p_input->pp_es[p_input->i_es_number];
278 p_input->pp_es = realloc( p_input->pp_es, p_input->i_es_number
279 * sizeof(es_descriptor_t *));
283 /*****************************************************************************
284 * input_DumpStream: dumps the contents of a stream descriptor
285 *****************************************************************************/
286 void input_DumpStream( input_thread_t * p_input )
289 #define S p_input->stream
290 intf_Msg( "input info: Dumping stream ID 0x%x\n", S.i_stream_id );
292 intf_Msg( "input info: seekable stream, position: %d/%d\n",
293 S.i_tell, S.i_size );
295 intf_Msg( "input info: %s\n", S.b_pace_control ? "pace controlled" :
296 "pace un-controlled" );
298 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
300 #define P p_input->stream.pp_programs[i]
301 intf_Msg( "input info: Dumping program 0x%x, version %d (%s)\n",
302 P->i_number, P->i_version,
303 P->b_is_ok ? "complete" : "partial" );
304 if( P->i_synchro_state == SYNCHRO_OK )
305 intf_Msg( "input info: synchro absolute delta : %lld (jitter : %lld)\n",
306 P->delta_absolute, P->delta_cr );
308 for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
310 #define ES p_input->stream.pp_programs[i]->pp_es[j]
311 intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s\n",
312 ES->i_id, ES->i_stream_id, ES->i_type,
313 ES->p_decoder_fifo != NULL ? "selected" : "not selected");
320 /*****************************************************************************
321 * InitDecConfig: initializes a decoder_config_t
322 *****************************************************************************/
323 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
324 decoder_config_t * p_config )
326 p_config->i_stream_id = p_es->i_stream_id;
327 p_config->i_type = p_es->i_type;
328 p_config->p_stream_ctrl =
329 &p_input->stream.control;
332 if( (p_config->p_decoder_fifo =
333 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
335 intf_ErrMsg( "Out of memory" );
339 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
340 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
341 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
342 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
343 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
344 p_config->p_decoder_fifo->pf_delete_pes =
345 p_input->p_plugin->pf_delete_pes;
346 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
348 p_config->pf_init_bit_stream = InitBitstream;
353 /*****************************************************************************
354 * GetVdecConfig: returns a valid vdec_config_t
355 *****************************************************************************/
356 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
357 es_descriptor_t * p_es )
359 vdec_config_t * p_config;
361 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
362 p_config->p_vout = p_input->p_default_vout;
363 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
372 /*****************************************************************************
373 * GetAdecConfig: returns a valid adec_config_t
374 *****************************************************************************/
375 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
376 es_descriptor_t * p_es )
378 adec_config_t * p_config;
380 p_config = (adec_config_t *)malloc( sizeof(adec_config_t) );
381 p_config->p_aout = p_input->p_default_aout;
382 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
391 /*****************************************************************************
392 * input_SelectES: selects an ES and spawns the associated decoder
393 *****************************************************************************/
394 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
399 intf_DbgMsg( "Selecting ES %d", p_es->i_id );
402 if( p_es->p_decoder_fifo != NULL )
404 intf_ErrMsg( "ES %d is already selected", p_es->i_id );
408 switch( p_es->i_type )
412 if( p_main->b_audio )
414 p_es->thread_id = adec_CreateThread( GetAdecConfig( p_input,
421 if( p_main->b_video )
423 p_es->thread_id = vpar_CreateThread( GetVdecConfig( p_input,
429 if( p_main->b_audio )
431 p_es->thread_id = ac3dec_CreateThread( GetAdecConfig( p_input,
437 if( p_main->b_video )
439 p_es->thread_id = spudec_CreateThread( GetVdecConfig( p_input,
445 intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
450 if( p_es->p_decoder_fifo != NULL )
452 p_input->i_selected_es_number++;
453 p_input->pp_selected_es = realloc( p_input->pp_selected_es,
454 p_input->i_selected_es_number
455 * sizeof(es_descriptor_t *) );
456 p_input->pp_selected_es[p_input->i_selected_es_number - 1] = p_es;