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.69 2001/12/07 18:33:08 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 *****************************************************************************/
30 #include <string.h> /* memcpy(), memset() */
31 #include <sys/types.h> /* off_t */
39 #include "stream_control.h"
40 #include "input_ext-intf.h"
41 #include "input_ext-dec.h"
42 #include "input_ext-plugins.h"
45 * NOTICE : all of these functions expect you to have taken the lock on
46 * p_input->stream.lock
49 /*****************************************************************************
50 * input_InitStream: init the stream descriptor of the given input
51 *****************************************************************************/
52 int input_InitStream( input_thread_t * p_input, size_t i_data_len )
55 p_input->stream.i_stream_id = 0;
57 /* initialized to 0 since we don't give the signal to the interface
58 * before the end of input initialization */
59 p_input->stream.b_changed = 0;
60 p_input->stream.pp_es = NULL;
61 p_input->stream.pp_selected_es = NULL;
62 p_input->stream.p_removed_es = NULL;
63 p_input->stream.p_newly_selected_es = NULL;
64 p_input->stream.pp_programs = NULL;
65 p_input->stream.p_selected_program = NULL;
66 p_input->stream.p_new_program = NULL;
70 if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
72 intf_ErrMsg( "Unable to allocate memory in input_InitStream");
75 memset( p_input->stream.p_demux_data, 0, i_data_len );
79 p_input->stream.p_demux_data = NULL;
85 /*****************************************************************************
86 * input_EndStream: free all stream descriptors
87 *****************************************************************************/
88 void input_EndStream( input_thread_t * p_input )
90 /* Free all programs and associated ES, and associated decoders. */
91 while( p_input->stream.i_pgrm_number )
93 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
96 /* Free standalone ES */
97 while( p_input->stream.i_es_number )
99 input_DelES( p_input, p_input->stream.pp_es[0] );
103 while( p_input->stream.i_area_nb )
105 input_DelArea( p_input, p_input->stream.pp_areas[0] );
108 /* Free selected ES */
109 if( p_input->stream.pp_selected_es != NULL )
111 free( p_input->stream.pp_selected_es );
114 if( p_input->stream.p_demux_data != NULL )
116 free( p_input->stream.p_demux_data );
120 /*****************************************************************************
121 * input_FindProgram: returns a pointer to a program described by its ID
122 *****************************************************************************/
123 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
127 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
129 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
131 return p_input->stream.pp_programs[i];
138 /*****************************************************************************
139 * input_AddProgram: add and init a program descriptor
140 *****************************************************************************
141 * This program descriptor will be referenced in the given stream descriptor
142 *****************************************************************************/
143 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
144 u16 i_pgrm_id, size_t i_data_len )
146 /* Where to add the pgrm */
147 int i_pgrm_index = p_input->stream.i_pgrm_number;
149 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
151 /* Add an entry to the list of program associated with the stream */
152 p_input->stream.i_pgrm_number++;
153 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
154 p_input->stream.i_pgrm_number
155 * sizeof(pgrm_descriptor_t *) );
156 if( p_input->stream.pp_programs == NULL )
158 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
162 /* Allocate the structure to store this description */
163 p_input->stream.pp_programs[i_pgrm_index] =
164 malloc( sizeof(pgrm_descriptor_t) );
165 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
167 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
171 /* Init this entry */
172 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
173 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
174 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
176 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
177 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
179 input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
181 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
186 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
187 malloc( i_data_len );
188 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
190 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
193 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
198 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data = NULL;
201 return p_input->stream.pp_programs[i_pgrm_index];
204 /*****************************************************************************
205 * input_DelProgram: destroy a program descriptor
206 *****************************************************************************
207 * All ES descriptions referenced in the descriptor will be deleted.
208 *****************************************************************************/
209 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
215 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
217 /* Free the structures that describe the es that belongs to that program */
218 while( p_pgrm->i_es_number )
220 input_DelES( p_input, p_pgrm->pp_es[0] );
223 /* Free the demux data */
224 if( p_pgrm->p_demux_data != NULL )
226 free( p_pgrm->p_demux_data );
229 /* Find the program in the programs table */
230 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
233 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
237 /* Remove this program from the stream's list of programs */
238 p_input->stream.i_pgrm_number--;
240 p_input->stream.pp_programs[i_pgrm_index] =
241 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
242 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
243 p_input->stream.i_pgrm_number
244 * sizeof(pgrm_descriptor_t *) );
246 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
248 intf_ErrMsg( "input error: unable to realloc program list"
249 " in input_DelProgram" );
252 /* Free the description of this program */
256 /*****************************************************************************
257 * input_AddArea: add and init an area descriptor
258 *****************************************************************************
259 * This area descriptor will be referenced in the given stream descriptor
260 *****************************************************************************/
261 input_area_t * input_AddArea( input_thread_t * p_input )
263 /* Where to add the pgrm */
264 int i_area_index = p_input->stream.i_area_nb;
266 intf_DbgMsg("Adding description for area %d", i_area_index );
268 /* Add an entry to the list of program associated with the stream */
269 p_input->stream.i_area_nb++;
270 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
271 p_input->stream.i_area_nb
272 * sizeof(input_area_t *) );
273 if( p_input->stream.pp_areas == NULL )
275 intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
279 /* Allocate the structure to store this description */
280 p_input->stream.pp_areas[i_area_index] =
281 malloc( sizeof(input_area_t) );
282 if( p_input->stream.pp_areas[i_area_index] == NULL )
284 intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
288 /* Init this entry */
289 p_input->stream.pp_areas[i_area_index]->i_id = 0;
290 p_input->stream.pp_areas[i_area_index]->i_start = 0;
291 p_input->stream.pp_areas[i_area_index]->i_size = 0;
292 p_input->stream.pp_areas[i_area_index]->i_tell = 0;
293 p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
294 p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
295 p_input->stream.pp_areas[i_area_index]->i_part= 0;
296 p_input->stream.pp_areas[i_area_index]->i_angle_nb = 1;
297 p_input->stream.pp_areas[i_area_index]->i_angle = 0;
299 return p_input->stream.pp_areas[i_area_index];
302 /*****************************************************************************
303 * input_SetProgram: changes the current program
304 *****************************************************************************/
305 int input_SetProgram( input_thread_t * p_input, pgrm_descriptor_t * p_new_prg )
308 #define old_prg p_input->stream.p_selected_program
309 for ( i_es_index = 0 ; i_es_index < old_prg->i_es_number ; i_es_index ++ )
311 input_UnselectES( p_input , old_prg->pp_es[i_es_index] );
314 for (i_es_index = 0 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
316 input_SelectES( p_input , p_new_prg->pp_es[i_es_index] );
319 p_input->stream.p_selected_program = p_new_prg;
325 /*****************************************************************************
326 * input_DelArea: destroy a area descriptor
327 *****************************************************************************
328 * All ES descriptions referenced in the descriptor will be deleted.
329 *****************************************************************************/
330 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
336 intf_DbgMsg("Deleting description for area %d", p_area->i_id );
338 /* Find the area in the areas table */
339 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
342 if( p_input->stream.pp_areas[i_area_index] == p_area )
346 /* Remove this area from the stream's list of areas */
347 p_input->stream.i_area_nb--;
349 p_input->stream.pp_areas[i_area_index] =
350 p_input->stream.pp_areas[p_input->stream.i_area_nb];
351 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
352 p_input->stream.i_area_nb
353 * sizeof(input_area_t *) );
355 if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
357 intf_ErrMsg( "input error: unable to realloc area list"
358 " in input_DelArea" );
361 /* Free the description of this area */
366 /*****************************************************************************
367 * input_FindES: returns a pointer to an ES described by its ID
368 *****************************************************************************/
369 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
373 for( i = 0; i < p_input->stream.i_es_number; i++ )
375 if( p_input->stream.pp_es[i]->i_id == i_es_id )
377 return p_input->stream.pp_es[i];
384 /*****************************************************************************
386 *****************************************************************************
387 * Reserve a slot in the table of ES descriptors for the ES and add it to the
388 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
390 *****************************************************************************/
391 es_descriptor_t * input_AddES( input_thread_t * p_input,
392 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
395 es_descriptor_t * p_es;
397 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
399 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
402 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
405 p_input->stream.i_es_number++;
406 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
407 p_input->stream.i_es_number
408 * sizeof(es_descriptor_t *) );
409 if( p_input->stream.pp_es == NULL )
411 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
414 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
416 /* Init its values */
417 p_es->i_id = i_es_id;
418 p_es->psz_desc[0] = '\0';
420 p_es->p_decoder_fifo = NULL;
422 p_es->i_cat = UNKNOWN_ES;
426 p_es->p_demux_data = malloc( i_data_len );
427 if( p_es->p_demux_data == NULL )
429 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
432 memset( p_es->p_demux_data, 0, i_data_len );
436 p_es->p_demux_data = NULL;
439 /* Add this ES to the program definition if one is given */
442 p_pgrm->i_es_number++;
443 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
445 * sizeof(es_descriptor_t *) );
446 if( p_pgrm->pp_es == NULL )
448 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
451 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
452 p_es->p_pgrm = p_pgrm;
462 /*****************************************************************************
464 *****************************************************************************/
465 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
467 int i_index, i_es_index;
468 pgrm_descriptor_t * p_pgrm;
471 p_pgrm = p_es->p_pgrm;
473 /* Kill associated decoder, if any. */
474 if( p_es->p_decoder_fifo != NULL )
476 input_EndDecoder( p_input, p_es );
479 /* Remove this ES from the description of the program if it is associated to
483 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
485 if( p_pgrm->pp_es[i_index] == p_es )
487 p_pgrm->i_es_number--;
488 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
489 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
491 * sizeof(es_descriptor_t *));
492 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
494 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
501 /* Free the demux data */
502 if( p_es->p_demux_data != NULL )
504 free( p_es->p_demux_data );
507 /* Find the ES in the ES table */
508 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
511 if( p_input->stream.pp_es[i_es_index] == p_es )
517 p_input->stream.i_es_number--;
518 p_input->stream.pp_es[i_es_index] =
519 p_input->stream.pp_es[p_input->stream.i_es_number];
520 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
521 p_input->stream.i_es_number
522 * sizeof(es_descriptor_t *));
523 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
525 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
530 /*****************************************************************************
531 * input_SelectES: selects an ES and spawns the associated decoder
532 *****************************************************************************
533 * Remember we are still supposed to have stream_lock when entering this
535 *****************************************************************************/
536 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
540 intf_ErrMsg( "Nothing to do in input_SelectES" );
545 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
548 if( p_es->p_decoder_fifo != NULL )
550 intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
554 switch( p_es->i_type )
560 if( p_main->b_audio )
562 /* This kludge should be removed */
563 p_main->b_ac3 = ( p_es->i_type == AC3_AUDIO_ES );
565 /* Release the lock, not to block the input thread during
566 * the creation of the thread. */
567 vlc_mutex_unlock( &p_input->stream.stream_lock );
568 p_es->thread_id = input_RunDecoder( p_input, p_es );
569 vlc_mutex_lock( &p_input->stream.stream_lock );
576 if( p_main->b_video )
578 /* Release the lock, not to block the input thread during
579 * the creation of the thread. */
580 vlc_mutex_unlock( &p_input->stream.stream_lock );
581 p_es->thread_id = input_RunDecoder( p_input, p_es );
582 vlc_mutex_lock( &p_input->stream.stream_lock );
587 intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
592 if( p_es->thread_id == 0 )
600 /*****************************************************************************
601 * input_UnselectES: removes an ES from the list of selected ES
602 *****************************************************************************/
603 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
610 intf_ErrMsg( "Nothing to do in input_UnselectES" );
615 intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
618 if( p_es->p_decoder_fifo == NULL )
620 intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
624 input_EndDecoder( p_input, p_es );
626 if( ( p_es->p_decoder_fifo == NULL ) &&
627 ( p_input->stream.i_selected_es_number > 0 ) )
629 p_input->stream.i_selected_es_number--;
631 while( ( i_index < p_input->stream.i_selected_es_number ) &&
632 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
637 p_input->stream.pp_selected_es[i_index] =
638 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
640 p_input->stream.pp_selected_es = realloc(
641 p_input->stream.pp_selected_es,
642 p_input->stream.i_selected_es_number
643 * sizeof(es_descriptor_t *) );
645 if( p_input->stream.pp_selected_es == NULL )
648 intf_DbgMsg( "No more selected ES in input_UnselectES" );