1 /*****************************************************************************
2 * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: input_programs.c,v 1.70 2001/12/30 07:09:56 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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 *****************************************************************************/
28 #include <string.h> /* memcpy(), memset() */
29 #include <sys/types.h> /* off_t */
31 #include <videolan/vlc.h>
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
36 #include "input_ext-plugins.h"
41 * NOTICE : all of these functions expect you to have taken the lock on
42 * p_input->stream.lock
45 /*****************************************************************************
46 * input_InitStream: init the stream descriptor of the given input
47 *****************************************************************************/
48 int input_InitStream( input_thread_t * p_input, size_t i_data_len )
51 p_input->stream.i_stream_id = 0;
53 /* initialized to 0 since we don't give the signal to the interface
54 * before the end of input initialization */
55 p_input->stream.b_changed = 0;
56 p_input->stream.pp_es = NULL;
57 p_input->stream.pp_selected_es = NULL;
58 p_input->stream.p_removed_es = NULL;
59 p_input->stream.p_newly_selected_es = NULL;
60 p_input->stream.pp_programs = NULL;
61 p_input->stream.p_selected_program = NULL;
62 p_input->stream.p_new_program = NULL;
66 if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
68 intf_ErrMsg( "Unable to allocate memory in input_InitStream");
71 memset( p_input->stream.p_demux_data, 0, i_data_len );
75 p_input->stream.p_demux_data = NULL;
81 /*****************************************************************************
82 * input_EndStream: free all stream descriptors
83 *****************************************************************************/
84 void input_EndStream( input_thread_t * p_input )
86 /* Free all programs and associated ES, and associated decoders. */
87 while( p_input->stream.i_pgrm_number )
89 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
92 /* Free standalone ES */
93 while( p_input->stream.i_es_number )
95 input_DelES( p_input, p_input->stream.pp_es[0] );
99 while( p_input->stream.i_area_nb )
101 input_DelArea( p_input, p_input->stream.pp_areas[0] );
104 /* Free selected ES */
105 if( p_input->stream.pp_selected_es != NULL )
107 free( p_input->stream.pp_selected_es );
110 if( p_input->stream.p_demux_data != NULL )
112 free( p_input->stream.p_demux_data );
116 /*****************************************************************************
117 * input_FindProgram: returns a pointer to a program described by its ID
118 *****************************************************************************/
119 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
123 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
125 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
127 return p_input->stream.pp_programs[i];
134 /*****************************************************************************
135 * input_AddProgram: add and init a program descriptor
136 *****************************************************************************
137 * This program descriptor will be referenced in the given stream descriptor
138 *****************************************************************************/
139 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
140 u16 i_pgrm_id, size_t i_data_len )
142 /* Where to add the pgrm */
143 int i_pgrm_index = p_input->stream.i_pgrm_number;
145 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
147 /* Add an entry to the list of program associated with the stream */
148 p_input->stream.i_pgrm_number++;
149 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
150 p_input->stream.i_pgrm_number
151 * sizeof(pgrm_descriptor_t *) );
152 if( p_input->stream.pp_programs == NULL )
154 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
158 /* Allocate the structure to store this description */
159 p_input->stream.pp_programs[i_pgrm_index] =
160 malloc( sizeof(pgrm_descriptor_t) );
161 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
163 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
167 /* Init this entry */
168 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
169 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
170 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
172 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
173 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
175 input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
177 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
182 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
183 malloc( i_data_len );
184 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
186 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
189 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
194 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data = NULL;
197 return p_input->stream.pp_programs[i_pgrm_index];
200 /*****************************************************************************
201 * input_DelProgram: destroy a program descriptor
202 *****************************************************************************
203 * All ES descriptions referenced in the descriptor will be deleted.
204 *****************************************************************************/
205 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
211 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
213 /* Free the structures that describe the es that belongs to that program */
214 while( p_pgrm->i_es_number )
216 input_DelES( p_input, p_pgrm->pp_es[0] );
219 /* Free the demux data */
220 if( p_pgrm->p_demux_data != NULL )
222 free( p_pgrm->p_demux_data );
225 /* Find the program in the programs table */
226 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
229 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
233 /* Remove this program from the stream's list of programs */
234 p_input->stream.i_pgrm_number--;
236 p_input->stream.pp_programs[i_pgrm_index] =
237 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
238 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
239 p_input->stream.i_pgrm_number
240 * sizeof(pgrm_descriptor_t *) );
242 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
244 intf_ErrMsg( "input error: unable to realloc program list"
245 " in input_DelProgram" );
248 /* Free the description of this program */
252 /*****************************************************************************
253 * input_AddArea: add and init an area descriptor
254 *****************************************************************************
255 * This area descriptor will be referenced in the given stream descriptor
256 *****************************************************************************/
257 input_area_t * input_AddArea( input_thread_t * p_input )
259 /* Where to add the pgrm */
260 int i_area_index = p_input->stream.i_area_nb;
262 intf_DbgMsg("Adding description for area %d", i_area_index );
264 /* Add an entry to the list of program associated with the stream */
265 p_input->stream.i_area_nb++;
266 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
267 p_input->stream.i_area_nb
268 * sizeof(input_area_t *) );
269 if( p_input->stream.pp_areas == NULL )
271 intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
275 /* Allocate the structure to store this description */
276 p_input->stream.pp_areas[i_area_index] =
277 malloc( sizeof(input_area_t) );
278 if( p_input->stream.pp_areas[i_area_index] == NULL )
280 intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
284 /* Init this entry */
285 p_input->stream.pp_areas[i_area_index]->i_id = 0;
286 p_input->stream.pp_areas[i_area_index]->i_start = 0;
287 p_input->stream.pp_areas[i_area_index]->i_size = 0;
288 p_input->stream.pp_areas[i_area_index]->i_tell = 0;
289 p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
290 p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
291 p_input->stream.pp_areas[i_area_index]->i_part= 0;
292 p_input->stream.pp_areas[i_area_index]->i_angle_nb = 1;
293 p_input->stream.pp_areas[i_area_index]->i_angle = 0;
295 return p_input->stream.pp_areas[i_area_index];
298 /*****************************************************************************
299 * input_SetProgram: changes the current program
300 *****************************************************************************/
301 int input_SetProgram( input_thread_t * p_input, pgrm_descriptor_t * p_new_prg )
304 #define old_prg p_input->stream.p_selected_program
305 for ( i_es_index = 0 ; i_es_index < old_prg->i_es_number ; i_es_index ++ )
307 input_UnselectES( p_input , old_prg->pp_es[i_es_index] );
310 for (i_es_index = 0 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
312 input_SelectES( p_input , p_new_prg->pp_es[i_es_index] );
315 p_input->stream.p_selected_program = p_new_prg;
321 /*****************************************************************************
322 * input_DelArea: destroy a area descriptor
323 *****************************************************************************
324 * All ES descriptions referenced in the descriptor will be deleted.
325 *****************************************************************************/
326 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
332 intf_DbgMsg("Deleting description for area %d", p_area->i_id );
334 /* Find the area in the areas table */
335 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
338 if( p_input->stream.pp_areas[i_area_index] == p_area )
342 /* Remove this area from the stream's list of areas */
343 p_input->stream.i_area_nb--;
345 p_input->stream.pp_areas[i_area_index] =
346 p_input->stream.pp_areas[p_input->stream.i_area_nb];
347 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
348 p_input->stream.i_area_nb
349 * sizeof(input_area_t *) );
351 if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
353 intf_ErrMsg( "input error: unable to realloc area list"
354 " in input_DelArea" );
357 /* Free the description of this area */
362 /*****************************************************************************
363 * input_FindES: returns a pointer to an ES described by its ID
364 *****************************************************************************/
365 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
369 for( i = 0; i < p_input->stream.i_es_number; i++ )
371 if( p_input->stream.pp_es[i]->i_id == i_es_id )
373 return p_input->stream.pp_es[i];
380 /*****************************************************************************
382 *****************************************************************************
383 * Reserve a slot in the table of ES descriptors for the ES and add it to the
384 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
386 *****************************************************************************/
387 es_descriptor_t * input_AddES( input_thread_t * p_input,
388 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
391 es_descriptor_t * p_es;
393 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
395 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
398 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
401 p_input->stream.i_es_number++;
402 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
403 p_input->stream.i_es_number
404 * sizeof(es_descriptor_t *) );
405 if( p_input->stream.pp_es == NULL )
407 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
410 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
412 /* Init its values */
413 p_es->i_id = i_es_id;
414 p_es->psz_desc[0] = '\0';
416 p_es->p_decoder_fifo = NULL;
418 p_es->i_cat = UNKNOWN_ES;
422 p_es->p_demux_data = malloc( i_data_len );
423 if( p_es->p_demux_data == NULL )
425 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
428 memset( p_es->p_demux_data, 0, i_data_len );
432 p_es->p_demux_data = NULL;
435 /* Add this ES to the program definition if one is given */
438 p_pgrm->i_es_number++;
439 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
441 * sizeof(es_descriptor_t *) );
442 if( p_pgrm->pp_es == NULL )
444 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
447 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
448 p_es->p_pgrm = p_pgrm;
458 /*****************************************************************************
460 *****************************************************************************/
461 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
463 int i_index, i_es_index;
464 pgrm_descriptor_t * p_pgrm;
467 p_pgrm = p_es->p_pgrm;
469 /* Kill associated decoder, if any. */
470 if( p_es->p_decoder_fifo != NULL )
472 input_EndDecoder( p_input, p_es );
475 /* Remove this ES from the description of the program if it is associated to
479 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
481 if( p_pgrm->pp_es[i_index] == p_es )
483 p_pgrm->i_es_number--;
484 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
485 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
487 * sizeof(es_descriptor_t *));
488 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
490 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
497 /* Free the demux data */
498 if( p_es->p_demux_data != NULL )
500 free( p_es->p_demux_data );
503 /* Find the ES in the ES table */
504 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
507 if( p_input->stream.pp_es[i_es_index] == p_es )
513 p_input->stream.i_es_number--;
514 p_input->stream.pp_es[i_es_index] =
515 p_input->stream.pp_es[p_input->stream.i_es_number];
516 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
517 p_input->stream.i_es_number
518 * sizeof(es_descriptor_t *));
519 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
521 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
526 /*****************************************************************************
527 * input_SelectES: selects an ES and spawns the associated decoder
528 *****************************************************************************
529 * Remember we are still supposed to have stream_lock when entering this
531 *****************************************************************************/
532 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
536 intf_ErrMsg( "Nothing to do in input_SelectES" );
541 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
544 if( p_es->p_decoder_fifo != NULL )
546 intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
550 switch( p_es->i_type )
556 if( p_main->b_audio )
558 /* This kludge should be removed */
559 p_main->b_ac3 = ( p_es->i_type == AC3_AUDIO_ES );
561 /* Release the lock, not to block the input thread during
562 * the creation of the thread. */
563 vlc_mutex_unlock( &p_input->stream.stream_lock );
564 p_es->thread_id = input_RunDecoder( p_input, p_es );
565 vlc_mutex_lock( &p_input->stream.stream_lock );
572 if( p_main->b_video )
574 /* Release the lock, not to block the input thread during
575 * the creation of the thread. */
576 vlc_mutex_unlock( &p_input->stream.stream_lock );
577 p_es->thread_id = input_RunDecoder( p_input, p_es );
578 vlc_mutex_lock( &p_input->stream.stream_lock );
583 intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
588 if( p_es->thread_id == 0 )
596 /*****************************************************************************
597 * input_UnselectES: removes an ES from the list of selected ES
598 *****************************************************************************/
599 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
606 intf_ErrMsg( "Nothing to do in input_UnselectES" );
611 intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
614 if( p_es->p_decoder_fifo == NULL )
616 intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
620 input_EndDecoder( p_input, p_es );
622 if( ( p_es->p_decoder_fifo == NULL ) &&
623 ( p_input->stream.i_selected_es_number > 0 ) )
625 p_input->stream.i_selected_es_number--;
627 while( ( i_index < p_input->stream.i_selected_es_number ) &&
628 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
633 p_input->stream.pp_selected_es[i_index] =
634 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
636 p_input->stream.pp_selected_es = realloc(
637 p_input->stream.pp_selected_es,
638 p_input->stream.i_selected_es_number
639 * sizeof(es_descriptor_t *) );
641 if( p_input->stream.pp_selected_es == NULL )
644 intf_DbgMsg( "No more selected ES in input_UnselectES" );