1 /*****************************************************************************
2 * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
3 *****************************************************************************
4 * Copyright (C) 1999-2002 VideoLAN
5 * $Id: input_programs.c,v 1.93 2002/07/23 00:39:17 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 */
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 msg_Err( p_input, "out of memory" );
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 msg_Err( p_input, "out of memory" );
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 msg_Err( p_input, "out of memory" );
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 msg_Err( p_input, "out of memory" );
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 msg_Err( p_input, "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 msg_Err( p_input, "cannot realloc memory" );
249 free( p_input->stream.pp_programs );
250 p_input->stream.pp_programs = NULL;
253 /* Free the description of this program */
257 /*****************************************************************************
258 * input_AddArea: add and init an area descriptor
259 *****************************************************************************
260 * This area descriptor will be referenced in the given stream descriptor
261 *****************************************************************************/
262 input_area_t * input_AddArea( input_thread_t * p_input )
264 /* Where to add the pgrm */
265 int i_area_index = p_input->stream.i_area_nb;
267 /* Add an entry to the list of program associated with the stream */
268 p_input->stream.i_area_nb++;
269 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
270 p_input->stream.i_area_nb
271 * sizeof(input_area_t *) );
272 if( p_input->stream.pp_areas == NULL )
274 msg_Err( p_input, "out of memory" );
278 /* Allocate the structure to store this description */
279 p_input->stream.pp_areas[i_area_index] =
280 malloc( sizeof(input_area_t) );
281 if( p_input->stream.pp_areas[i_area_index] == NULL )
283 msg_Err( p_input, "out of memory" );
287 /* Init this entry */
288 p_input->stream.pp_areas[i_area_index]->i_id = 0;
289 p_input->stream.pp_areas[i_area_index]->i_start = 0;
290 p_input->stream.pp_areas[i_area_index]->i_size = 0;
291 p_input->stream.pp_areas[i_area_index]->i_tell = 0;
292 p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
293 p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
294 p_input->stream.pp_areas[i_area_index]->i_part= 0;
296 return p_input->stream.pp_areas[i_area_index];
299 /*****************************************************************************
300 * input_SetProgram: changes the current program
301 *****************************************************************************/
302 int input_SetProgram( input_thread_t * p_input, pgrm_descriptor_t * p_new_prg )
305 int i_required_audio_es;
306 int i_required_spu_es;
310 if ( p_input->stream.p_selected_program )
312 for ( i_es_index = 1 ; /* 0 should be the PMT */
313 i_es_index < p_input->stream.p_selected_program->
317 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
318 if ( p_es->p_decoder_fifo ) /* if the ES was selected */
320 input_UnselectES( p_input , p_es );
325 /* Get the number of the required audio stream */
326 if( config_GetInt( p_input, "audio" ) )
328 /* Default is the first one */
329 i_required_audio_es = config_GetInt( p_input, "audio-channel" );
330 if( i_required_audio_es < 0 )
332 i_required_audio_es = 1;
337 i_required_audio_es = 0;
340 /* Same thing for subtitles */
341 if( config_GetInt( p_input, "video" ) )
343 /* for spu, default is none */
344 i_required_spu_es = config_GetInt( p_input, "spu-channel" );
345 if( i_required_spu_es < 0 )
347 i_required_spu_es = 0;
352 i_required_spu_es = 0;
355 for( i_es_index = 0 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
357 switch( p_new_prg->pp_es[i_es_index]->i_cat )
360 msg_Dbg( p_input, "selecting ES %x",
361 p_new_prg->pp_es[i_es_index]->i_id );
362 input_SelectES( p_input, p_new_prg->pp_es[i_es_index] );
366 if( i_audio_es <= i_required_audio_es )
368 msg_Dbg( p_input, "selecting ES %x",
369 p_new_prg->pp_es[i_es_index]->i_id );
370 input_SelectES( p_input, p_new_prg->pp_es[i_es_index]);
373 /* Not sure this one is fully specification-compliant */
376 if( i_spu_es <= i_required_spu_es )
378 msg_Dbg( p_input, "selecting ES %x",
379 p_new_prg->pp_es[i_es_index]->i_id );
380 input_SelectES( p_input, p_new_prg->pp_es[i_es_index] );
384 msg_Dbg( p_input, "ES %x has unknown type",
385 p_new_prg->pp_es[i_es_index]->i_id );
392 p_input->stream.p_selected_program = p_new_prg;
398 /*****************************************************************************
399 * input_DelArea: destroy a area descriptor
400 *****************************************************************************
401 * All ES descriptions referenced in the descriptor will be deleted.
402 *****************************************************************************/
403 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
407 /* Find the area in the areas table */
408 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
411 if( p_input->stream.pp_areas[i_area_index] == p_area )
415 /* If the area wasn't found, do nothing */
416 if( i_area_index == p_input->stream.i_area_nb )
418 msg_Err( p_input, "area does not belong to this input" );
422 /* Remove this area from the stream's list of areas */
423 p_input->stream.i_area_nb--;
425 p_input->stream.pp_areas[i_area_index] =
426 p_input->stream.pp_areas[p_input->stream.i_area_nb];
427 if( p_input->stream.i_area_nb )
429 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
430 p_input->stream.i_area_nb
431 * sizeof(input_area_t *) );
433 if( p_input->stream.pp_areas == NULL )
435 msg_Err( p_input, "cannot realloc memory" );
440 free( p_input->stream.pp_areas );
441 p_input->stream.pp_areas = NULL;
444 /* Free the description of this area */
449 /*****************************************************************************
450 * input_FindES: returns a pointer to an ES described by its ID
451 *****************************************************************************/
452 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
456 for( i = 0; i < p_input->stream.i_es_number; i++ )
458 if( p_input->stream.pp_es[i]->i_id == i_es_id )
460 return p_input->stream.pp_es[i];
467 /*****************************************************************************
469 *****************************************************************************
470 * Reserve a slot in the table of ES descriptors for the ES and add it to the
471 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
473 *****************************************************************************/
474 es_descriptor_t * input_AddES( input_thread_t * p_input,
475 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
478 es_descriptor_t * p_es;
480 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
483 msg_Err( p_input, "out of memory" );
486 p_input->stream.i_es_number++;
487 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
488 p_input->stream.i_es_number
489 * sizeof(es_descriptor_t *) );
490 if( p_input->stream.pp_es == NULL )
492 msg_Err( p_input, "out of memory" );
496 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
498 /* Init its values */
499 p_es->i_id = i_es_id;
500 p_es->psz_desc[0] = '\0';
502 p_es->p_decoder_fifo = NULL;
503 p_es->i_cat = UNKNOWN_ES;
504 p_es->i_demux_fd = 0;
506 p_es->c_invalid_packets = 0;
510 p_es->p_demux_data = malloc( i_data_len );
511 if( p_es->p_demux_data == NULL )
513 msg_Err( p_input, "out of memory" );
516 memset( p_es->p_demux_data, 0, i_data_len );
520 p_es->p_demux_data = NULL;
523 /* Add this ES to the program definition if one is given */
526 p_pgrm->i_es_number++;
527 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
529 * sizeof(es_descriptor_t *) );
530 if( p_pgrm->pp_es == NULL )
532 msg_Err( p_input, "out of memory" );
536 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
537 p_es->p_pgrm = p_pgrm;
547 /*****************************************************************************
549 *****************************************************************************/
550 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
552 int i_index, i_es_index;
553 pgrm_descriptor_t * p_pgrm;
555 /* Find the ES in the ES table */
556 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
559 if( p_input->stream.pp_es[i_es_index] == p_es )
563 /* If the ES wasn't found, do nothing */
564 if( i_es_index == p_input->stream.i_es_number )
566 msg_Err( p_input, "ES does not belong to this input" );
570 p_pgrm = p_es->p_pgrm;
572 /* Kill associated decoder, if any. */
573 if( p_es->p_decoder_fifo != NULL )
575 input_EndDecoder( p_input, p_es );
578 /* Remove this ES from the description of the program if it is associated to
582 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
584 if( p_pgrm->pp_es[i_index] == p_es )
586 p_pgrm->i_es_number--;
587 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
588 if( p_pgrm->i_es_number )
590 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
592 * sizeof(es_descriptor_t *));
593 if( p_pgrm->pp_es == NULL )
595 msg_Err( p_input, "cannot realloc memory" );
600 free( p_pgrm->pp_es );
601 p_pgrm->pp_es = NULL;
608 /* Free the demux data */
609 if( p_es->p_demux_data != NULL )
611 free( p_es->p_demux_data );
614 /* Find the ES in the ES table */
615 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
618 if( p_input->stream.pp_es[i_es_index] == p_es )
622 /* Remove this ES from the stream's list of ES */
623 p_input->stream.i_es_number--;
624 p_input->stream.pp_es[i_es_index] =
625 p_input->stream.pp_es[p_input->stream.i_es_number];
626 if( p_input->stream.i_es_number )
628 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
629 p_input->stream.i_es_number
630 * sizeof(es_descriptor_t *));
631 if( p_input->stream.pp_es == NULL )
633 msg_Err( p_input, "cannot realloc memory" );
638 free( p_input->stream.pp_es );
639 p_input->stream.pp_es = NULL;
646 /*****************************************************************************
647 * input_SelectES: selects an ES and spawns the associated decoder
648 *****************************************************************************
649 * Remember we are still supposed to have stream_lock when entering this
651 *****************************************************************************/
652 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
654 decoder_fifo_t *p_fifo;
658 msg_Err( p_input, "nothing to do in input_SelectES" );
662 msg_Dbg( p_input, "selecting ES 0x%x", p_es->i_id );
664 if( p_es->p_decoder_fifo != NULL )
666 msg_Err( p_input, "ES 0x%x is already selected", p_es->i_id );
670 /* Release the lock, not to block the input thread during
671 * the creation of the thread. */
672 vlc_mutex_unlock( &p_input->stream.stream_lock );
673 p_fifo = input_RunDecoder( p_input, p_es );
674 vlc_mutex_lock( &p_input->stream.stream_lock );
681 p_es->p_decoder_fifo = p_fifo;
686 /*****************************************************************************
687 * input_UnselectES: removes an ES from the list of selected ES
688 *****************************************************************************/
689 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
696 msg_Err( p_input, "nothing to do in input_UnselectES" );
700 msg_Dbg( p_input, "unselecting ES 0x%x", p_es->i_id );
702 if( p_es->p_decoder_fifo == NULL )
704 msg_Err( p_input, "ES 0x%x is not selected", p_es->i_id );
708 input_EndDecoder( p_input, p_es );
711 if( ( p_es->p_decoder_fifo == NULL ) &&
712 ( p_input->stream.i_selected_es_number > 0 ) )
714 p_input->stream.i_selected_es_number--;
716 while( ( i_index < p_input->stream.i_selected_es_number ) &&
717 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
722 p_input->stream.pp_selected_es[i_index] =
723 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
725 if( p_input->stream.i_selected_es_number )
727 p_input->stream.pp_selected_es = realloc(
728 p_input->stream.pp_selected_es,
729 p_input->stream.i_selected_es_number
730 * sizeof(es_descriptor_t *) );
731 if( p_input->stream.pp_selected_es == NULL )
733 msg_Err( p_input, "cannot realloc memory" );
739 free( p_input->stream.pp_selected_es );
740 p_input->stream.pp_selected_es = NULL;
741 msg_Dbg( p_input, "no more selected ES" );