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.87 2002/05/16 13:20:22 gbazin 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"
39 * NOTICE : all of these functions expect you to have taken the lock on
40 * p_input->stream.lock
43 /*****************************************************************************
44 * input_InitStream: init the stream descriptor of the given input
45 *****************************************************************************/
46 int input_InitStream( input_thread_t * p_input, size_t i_data_len )
49 p_input->stream.i_stream_id = 0;
51 /* initialized to 0 since we don't give the signal to the interface
52 * before the end of input initialization */
53 p_input->stream.b_changed = 0;
54 p_input->stream.pp_es = NULL;
55 p_input->stream.pp_selected_es = NULL;
56 p_input->stream.p_removed_es = NULL;
57 p_input->stream.p_newly_selected_es = NULL;
58 p_input->stream.pp_programs = NULL;
59 p_input->stream.p_selected_program = NULL;
60 p_input->stream.p_new_program = NULL;
64 if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
66 intf_ErrMsg( "Unable to allocate memory in input_InitStream");
69 memset( p_input->stream.p_demux_data, 0, i_data_len );
73 p_input->stream.p_demux_data = NULL;
79 /*****************************************************************************
80 * input_EndStream: free all stream descriptors
81 *****************************************************************************/
82 void input_EndStream( input_thread_t * p_input )
84 /* Free all programs and associated ES, and associated decoders. */
85 while( p_input->stream.i_pgrm_number )
87 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
90 /* Free standalone ES */
91 while( p_input->stream.i_es_number )
93 input_DelES( p_input, p_input->stream.pp_es[0] );
97 while( p_input->stream.i_area_nb )
99 input_DelArea( p_input, p_input->stream.pp_areas[0] );
102 /* Free selected ES */
103 if( p_input->stream.pp_selected_es != NULL )
105 free( p_input->stream.pp_selected_es );
108 if( p_input->stream.p_demux_data != NULL )
110 free( p_input->stream.p_demux_data );
114 /*****************************************************************************
115 * input_FindProgram: returns a pointer to a program described by its ID
116 *****************************************************************************/
117 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
121 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
123 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
125 return p_input->stream.pp_programs[i];
132 /*****************************************************************************
133 * input_AddProgram: add and init a program descriptor
134 *****************************************************************************
135 * This program descriptor will be referenced in the given stream descriptor
136 *****************************************************************************/
137 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
138 u16 i_pgrm_id, size_t i_data_len )
140 /* Where to add the pgrm */
141 int i_pgrm_index = p_input->stream.i_pgrm_number;
143 /* Add an entry to the list of program associated with the stream */
144 p_input->stream.i_pgrm_number++;
145 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
146 p_input->stream.i_pgrm_number
147 * sizeof(pgrm_descriptor_t *) );
148 if( p_input->stream.pp_programs == NULL )
150 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
154 /* Allocate the structure to store this description */
155 p_input->stream.pp_programs[i_pgrm_index] =
156 malloc( sizeof(pgrm_descriptor_t) );
157 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
159 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
163 /* Init this entry */
164 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
165 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
166 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
168 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
169 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
171 input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
173 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
178 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
179 malloc( i_data_len );
180 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
182 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
185 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
190 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data = NULL;
193 return p_input->stream.pp_programs[i_pgrm_index];
196 /*****************************************************************************
197 * input_DelProgram: destroy a program descriptor
198 *****************************************************************************
199 * All ES descriptions referenced in the descriptor will be deleted.
200 *****************************************************************************/
201 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
205 /* Find the program in the programs table */
206 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
209 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
213 /* If the program wasn't found, do nothing */
214 if( i_pgrm_index == p_input->stream.i_pgrm_number )
216 intf_ErrMsg( "input error: program does not belong to this input" );
220 /* Free the structures that describe the es that belongs to that program */
221 while( p_pgrm->i_es_number )
223 input_DelES( p_input, p_pgrm->pp_es[0] );
226 /* Free the demux data */
227 if( p_pgrm->p_demux_data != NULL )
229 free( p_pgrm->p_demux_data );
232 /* Remove this program from the stream's list of programs */
233 p_input->stream.i_pgrm_number--;
235 p_input->stream.pp_programs[i_pgrm_index] =
236 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
237 if( p_input->stream.i_pgrm_number )
239 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
240 p_input->stream.i_pgrm_number
241 * sizeof(pgrm_descriptor_t *) );
242 if( p_input->stream.pp_programs == NULL )
244 intf_ErrMsg( "input error: unable to realloc program list"
245 " in input_DelProgram" );
250 free( p_input->stream.pp_programs );
251 p_input->stream.pp_programs = NULL;
254 /* Free the description of this program */
258 /*****************************************************************************
259 * input_AddArea: add and init an area descriptor
260 *****************************************************************************
261 * This area descriptor will be referenced in the given stream descriptor
262 *****************************************************************************/
263 input_area_t * input_AddArea( input_thread_t * p_input )
265 /* Where to add the pgrm */
266 int i_area_index = p_input->stream.i_area_nb;
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;
297 return p_input->stream.pp_areas[i_area_index];
300 /*****************************************************************************
301 * input_SetProgram: changes the current program
302 *****************************************************************************/
303 int input_SetProgram( input_thread_t * p_input, pgrm_descriptor_t * p_new_prg )
306 int i_required_audio_es;
307 int i_required_spu_es;
311 if ( p_input->stream.p_selected_program )
313 for ( i_es_index = 1 ; /* 0 should be the PMT */
314 i_es_index < p_input->stream.p_selected_program->
318 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
319 if ( p_es->p_decoder_fifo ) /* if the ES was selected */
321 input_UnselectES( p_input , p_es );
326 /* Get the number of the required audio stream */
327 if( p_main->b_audio )
329 /* Default is the first one */
330 i_required_audio_es = config_GetIntVariable( "audio-channel" );
331 if( i_required_audio_es < 0 )
333 i_required_audio_es = 1;
338 i_required_audio_es = 0;
341 /* Same thing for subtitles */
342 if( p_main->b_video )
344 /* for spu, default is none */
345 i_required_spu_es = config_GetIntVariable( "spu-channel" );
346 if( i_required_spu_es < 0 )
348 i_required_spu_es = 0;
353 i_required_spu_es = 0;
356 for (i_es_index = 0 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
358 switch( p_new_prg->pp_es[i_es_index]->i_cat )
361 intf_WarnMsg( 4, "Selecting ES %x",
362 p_new_prg->pp_es[i_es_index]->i_id );
363 input_SelectES( p_input, p_new_prg->pp_es[i_es_index] );
367 if( i_audio_es <= i_required_audio_es )
369 intf_WarnMsg( 4, "Selecting ES %x",
370 p_new_prg->pp_es[i_es_index]->i_id );
371 input_SelectES( p_input, p_new_prg->pp_es[i_es_index]);
374 /* Not sure this one is fully specification-compliant */
377 if( i_spu_es <= i_required_spu_es )
379 intf_WarnMsg( 4, "Selecting ES %x",
380 p_new_prg->pp_es[i_es_index]->i_id );
381 input_SelectES( p_input, p_new_prg->pp_es[i_es_index] );
385 intf_WarnMsg( 2, "ES %x has unknown type",
386 p_new_prg->pp_es[i_es_index]->i_id );
393 p_input->stream.p_selected_program = p_new_prg;
399 /*****************************************************************************
400 * input_DelArea: destroy a area descriptor
401 *****************************************************************************
402 * All ES descriptions referenced in the descriptor will be deleted.
403 *****************************************************************************/
404 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
408 /* Find the area in the areas table */
409 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
412 if( p_input->stream.pp_areas[i_area_index] == p_area )
416 /* If the area wasn't found, do nothing */
417 if( i_area_index == p_input->stream.i_area_nb )
419 intf_ErrMsg( "input error: area does not belong to this input" );
423 /* Remove this area from the stream's list of areas */
424 p_input->stream.i_area_nb--;
426 p_input->stream.pp_areas[i_area_index] =
427 p_input->stream.pp_areas[p_input->stream.i_area_nb];
428 if( p_input->stream.i_area_nb )
430 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
431 p_input->stream.i_area_nb
432 * sizeof(input_area_t *) );
434 if( p_input->stream.pp_areas == NULL )
436 intf_ErrMsg( "input error: unable to realloc area list"
437 " in input_DelArea" );
442 free( p_input->stream.pp_areas );
443 p_input->stream.pp_areas = NULL;
446 /* Free the description of this area */
451 /*****************************************************************************
452 * input_FindES: returns a pointer to an ES described by its ID
453 *****************************************************************************/
454 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
458 for( i = 0; i < p_input->stream.i_es_number; i++ )
460 if( p_input->stream.pp_es[i]->i_id == i_es_id )
462 return p_input->stream.pp_es[i];
469 /*****************************************************************************
471 *****************************************************************************
472 * Reserve a slot in the table of ES descriptors for the ES and add it to the
473 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
475 *****************************************************************************/
476 es_descriptor_t * input_AddES( input_thread_t * p_input,
477 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
480 es_descriptor_t * p_es;
482 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
485 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
488 p_input->stream.i_es_number++;
489 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
490 p_input->stream.i_es_number
491 * sizeof(es_descriptor_t *) );
492 if( p_input->stream.pp_es == NULL )
494 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
498 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
500 /* Init its values */
501 p_es->i_id = i_es_id;
502 p_es->psz_desc[0] = '\0';
504 p_es->p_decoder_fifo = NULL;
506 p_es->i_cat = UNKNOWN_ES;
507 p_es->i_demux_fd = 0;
511 p_es->p_demux_data = malloc( i_data_len );
512 if( p_es->p_demux_data == NULL )
514 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
517 memset( p_es->p_demux_data, 0, i_data_len );
521 p_es->p_demux_data = NULL;
524 /* Add this ES to the program definition if one is given */
527 p_pgrm->i_es_number++;
528 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
530 * sizeof(es_descriptor_t *) );
531 if( p_pgrm->pp_es == NULL )
533 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
537 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
538 p_es->p_pgrm = p_pgrm;
548 /*****************************************************************************
550 *****************************************************************************/
551 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
553 int i_index, i_es_index;
554 pgrm_descriptor_t * p_pgrm;
556 /* Find the ES in the ES table */
557 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
560 if( p_input->stream.pp_es[i_es_index] == p_es )
564 /* If the ES wasn't found, do nothing */
565 if( i_es_index == p_input->stream.i_es_number )
567 intf_ErrMsg( "input error: ES does not belong to this input" );
571 p_pgrm = p_es->p_pgrm;
573 /* Kill associated decoder, if any. */
574 if( p_es->p_decoder_fifo != NULL )
576 input_EndDecoder( p_input, p_es );
579 /* Remove this ES from the description of the program if it is associated to
583 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
585 if( p_pgrm->pp_es[i_index] == p_es )
587 p_pgrm->i_es_number--;
588 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
589 if( p_pgrm->i_es_number )
591 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
593 * sizeof(es_descriptor_t *));
594 if( p_pgrm->pp_es == NULL )
596 intf_ErrMsg( "Unable to realloc memory in "
602 free( p_pgrm->pp_es );
603 p_pgrm->pp_es = NULL;
610 /* Free the demux data */
611 if( p_es->p_demux_data != NULL )
613 free( p_es->p_demux_data );
616 /* Remove this ES from the stream's list of ES */
617 p_input->stream.i_es_number--;
618 p_input->stream.pp_es[i_es_index] =
619 p_input->stream.pp_es[p_input->stream.i_es_number];
620 if( p_input->stream.i_es_number )
622 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
623 p_input->stream.i_es_number
624 * sizeof(es_descriptor_t *));
625 if( p_input->stream.pp_es == NULL )
627 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
632 free( p_input->stream.pp_es );
633 p_input->stream.pp_es = NULL;
640 /*****************************************************************************
641 * input_SelectES: selects an ES and spawns the associated decoder
642 *****************************************************************************
643 * Remember we are still supposed to have stream_lock when entering this
645 *****************************************************************************/
646 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
650 intf_ErrMsg( "input error: nothing to do in input_SelectES" );
654 intf_WarnMsg( 4, "input: selecting ES 0x%x", p_es->i_id );
656 if( p_es->p_decoder_fifo != NULL )
658 intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
664 switch( p_es->i_type )
670 if( p_main->b_audio )
672 /* Release the lock, not to block the input thread during
673 * the creation of the thread. */
674 vlc_mutex_unlock( &p_input->stream.stream_lock );
675 p_es->thread_id = input_RunDecoder( p_input, p_es );
676 vlc_mutex_lock( &p_input->stream.stream_lock );
683 case MSMPEG4v1_VIDEO_ES:
684 case MSMPEG4v2_VIDEO_ES:
685 case MSMPEG4v3_VIDEO_ES:
687 if( p_main->b_video )
689 /* Release the lock, not to block the input thread during
690 * the creation of the thread. */
691 vlc_mutex_unlock( &p_input->stream.stream_lock );
692 p_es->thread_id = input_RunDecoder( p_input, p_es );
693 vlc_mutex_lock( &p_input->stream.stream_lock );
698 intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
703 if( p_es->thread_id == 0 )
711 /*****************************************************************************
712 * input_UnselectES: removes an ES from the list of selected ES
713 *****************************************************************************/
714 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
721 intf_ErrMsg( "Nothing to do in input_UnselectES" );
725 intf_WarnMsg( 4, "input: unselecting ES 0x%x", p_es->i_id );
727 if( p_es->p_decoder_fifo == NULL )
729 intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
733 input_EndDecoder( p_input, p_es );
736 if( ( p_es->p_decoder_fifo == NULL ) &&
737 ( p_input->stream.i_selected_es_number > 0 ) )
739 p_input->stream.i_selected_es_number--;
741 while( ( i_index < p_input->stream.i_selected_es_number ) &&
742 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
747 p_input->stream.pp_selected_es[i_index] =
748 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
750 if( p_input->stream.i_selected_es_number )
752 p_input->stream.pp_selected_es = realloc(
753 p_input->stream.pp_selected_es,
754 p_input->stream.i_selected_es_number
755 * sizeof(es_descriptor_t *) );
756 if( p_input->stream.pp_selected_es == NULL )
758 intf_ErrMsg( "Unable to realloc memory in input_UnselectES" );
764 free( p_input->stream.pp_selected_es );
765 p_input->stream.pp_selected_es = NULL;
766 intf_WarnMsg( 4, "input: no more selected ES in input_UnselectES" ); return( 1 );