1 /*****************************************************************************
2 * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: input_programs.c,v 1.42 2001/04/01 07:31:38 stef 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 *****************************************************************************/
39 #include "stream_control.h"
40 #include "input_ext-intf.h"
41 #include "input_ext-dec.h"
44 #include "main.h" /* --noaudio --novideo */
47 * NOTICE : all of these functions expect you to have taken the lock on
48 * p_input->stream.lock
51 /*****************************************************************************
52 * input_InitStream: init the stream descriptor of the given input
53 *****************************************************************************/
54 int input_InitStream( input_thread_t * p_input, size_t i_data_len )
56 p_input->stream.i_stream_id = 0;
57 p_input->stream.pp_es = NULL;
58 p_input->stream.pp_selected_es = NULL;
59 p_input->stream.pp_programs = NULL;
63 if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
65 intf_ErrMsg( "Unable to allocate memory in input_InitStream");
68 memset( p_input->stream.p_demux_data, 0, i_data_len );
74 /*****************************************************************************
75 * input_EndStream: free all stream descriptors
76 *****************************************************************************/
77 void input_EndStream( input_thread_t * p_input )
79 /* Free all programs and associated ES, and associated decoders. */
80 while( p_input->stream.i_pgrm_number )
82 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
85 /* Free standalone ES */
86 while( p_input->stream.i_es_number )
88 input_DelES( p_input, p_input->stream.pp_es[0] );
91 if( p_input->stream.p_demux_data != NULL )
93 free( p_input->stream.p_demux_data );
97 /*****************************************************************************
98 * input_FindProgram: returns a pointer to a program described by its ID
99 *****************************************************************************/
100 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
104 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
106 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
108 return p_input->stream.pp_programs[i];
115 /*****************************************************************************
116 * input_AddProgram: add and init a program descriptor
117 *****************************************************************************
118 * This program descriptor will be referenced in the given stream descriptor
119 *****************************************************************************/
120 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
121 u16 i_pgrm_id, size_t i_data_len )
123 /* Where to add the pgrm */
124 int i_pgrm_index = p_input->stream.i_pgrm_number;
126 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
128 /* Add an entry to the list of program associated with the stream */
129 p_input->stream.i_pgrm_number++;
130 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
131 p_input->stream.i_pgrm_number
132 * sizeof(pgrm_descriptor_t *) );
133 if( p_input->stream.pp_programs == NULL )
135 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
139 /* Allocate the structure to store this description */
140 p_input->stream.pp_programs[i_pgrm_index] =
141 malloc( sizeof(pgrm_descriptor_t) );
142 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
144 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
148 /* Init this entry */
149 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
150 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
151 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
153 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
154 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
156 input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
158 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
161 p_input->stream.pp_programs[i_pgrm_index]->p_vout
162 = p_input->p_default_vout;
163 p_input->stream.pp_programs[i_pgrm_index]->p_aout
164 = p_input->p_default_aout;
168 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
169 malloc( i_data_len );
170 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
172 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
175 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
179 return p_input->stream.pp_programs[i_pgrm_index];
182 /*****************************************************************************
183 * input_DelProgram: destroy a program descriptor
184 *****************************************************************************
185 * All ES descriptions referenced in the descriptor will be deleted.
186 *****************************************************************************/
187 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
193 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
195 /* Free the structures that describe the es that belongs to that program */
196 while( p_pgrm->i_es_number )
198 input_DelES( p_input, p_pgrm->pp_es[0] );
201 /* Free the demux data */
202 if( p_pgrm->p_demux_data != NULL )
204 free( p_pgrm->p_demux_data );
207 /* Find the program in the programs table */
208 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
211 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
215 /* Remove this program from the stream's list of programs */
216 p_input->stream.i_pgrm_number--;
218 p_input->stream.pp_programs[i_pgrm_index] =
219 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
220 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
221 p_input->stream.i_pgrm_number
222 * sizeof(pgrm_descriptor_t *) );
224 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
226 intf_ErrMsg( "input error: unable to realloc program list"
227 " in input_DelProgram" );
230 /* Free the description of this program */
234 /*****************************************************************************
235 * input_AddArea: add and init an area descriptor
236 *****************************************************************************
237 * This area descriptor will be referenced in the given stream descriptor
238 *****************************************************************************/
239 input_area_t * input_AddArea( input_thread_t * p_input )
241 /* Where to add the pgrm */
242 int i_area_index = p_input->stream.i_area_nb;
244 intf_DbgMsg("Adding description for area %d", i_area_index );
246 /* Add an entry to the list of program associated with the stream */
247 p_input->stream.i_area_nb++;
248 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
249 p_input->stream.i_area_nb
250 * sizeof(input_area_t *) );
251 if( p_input->stream.pp_areas == NULL )
253 intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
257 /* Allocate the structure to store this description */
258 p_input->stream.pp_areas[i_area_index] =
259 malloc( sizeof(input_area_t) );
260 if( p_input->stream.pp_areas[i_area_index] == NULL )
262 intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
266 /* Init this entry */
267 p_input->stream.pp_areas[i_area_index]->i_id = 0;
268 p_input->stream.pp_areas[i_area_index]->i_start = 0;
269 p_input->stream.pp_areas[i_area_index]->i_size = 0;
270 p_input->stream.pp_areas[i_area_index]->i_tell = 0;
271 p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
272 p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
273 p_input->stream.pp_areas[i_area_index]->i_part= 0;
275 return p_input->stream.pp_areas[i_area_index];
278 /*****************************************************************************
279 * input_DelArea: destroy a area descriptor
280 *****************************************************************************
281 * All ES descriptions referenced in the descriptor will be deleted.
282 *****************************************************************************/
283 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
289 intf_DbgMsg("Deleting description for area %d", p_area->i_id );
291 /* Find the area in the areas table */
292 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
295 if( p_input->stream.pp_areas[i_area_index] == p_area )
299 /* Remove this area from the stream's list of areas */
300 p_input->stream.i_area_nb--;
302 p_input->stream.pp_areas[i_area_index] =
303 p_input->stream.pp_areas[p_input->stream.i_area_nb];
304 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
305 p_input->stream.i_area_nb
306 * sizeof(input_area_t *) );
308 if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
310 intf_ErrMsg( "input error: unable to realloc area list"
311 " in input_DelArea" );
314 /* Free the description of this area */
319 /*****************************************************************************
320 * input_FindES: returns a pointer to an ES described by its ID
321 *****************************************************************************/
322 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
326 for( i = 0; i < p_input->stream.i_es_number; i++ )
328 if( p_input->stream.pp_es[i]->i_id == i_es_id )
330 return p_input->stream.pp_es[i];
337 /*****************************************************************************
339 *****************************************************************************
340 * Reserve a slot in the table of ES descriptors for the ES and add it to the
341 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
343 *****************************************************************************/
344 es_descriptor_t * input_AddES( input_thread_t * p_input,
345 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
348 es_descriptor_t * p_es;
350 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
352 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
355 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
358 p_input->stream.i_es_number++;
359 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
360 p_input->stream.i_es_number
361 * sizeof(es_descriptor_t *) );
362 if( p_input->stream.pp_es == NULL )
364 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
367 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
369 /* Init its values */
370 p_es->i_id = i_es_id;
371 p_es->psz_desc[0] = '\0';
373 p_es->p_decoder_fifo = NULL;
379 p_es->p_demux_data = malloc( i_data_len );
380 if( p_es->p_demux_data == NULL )
382 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
385 memset( p_es->p_demux_data, 0, i_data_len );
389 p_es->p_demux_data = NULL;
392 /* Add this ES to the program definition if one is given */
395 p_pgrm->i_es_number++;
396 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
398 * sizeof(es_descriptor_t *) );
399 if( p_pgrm->pp_es == NULL )
401 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
404 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
405 p_es->p_pgrm = p_pgrm;
415 /*****************************************************************************
417 *****************************************************************************/
418 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
420 int i_index, i_es_index;
421 pgrm_descriptor_t * p_pgrm;
424 p_pgrm = p_es->p_pgrm;
426 /* Kill associated decoder, if any. */
427 if( p_es->p_decoder_fifo != NULL )
429 input_EndDecoder( p_input, p_es );
432 /* Remove this ES from the description of the program if it is associated to
436 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
438 if( p_pgrm->pp_es[i_index] == p_es )
440 p_pgrm->i_es_number--;
441 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
442 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
444 * sizeof(es_descriptor_t *));
445 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
447 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
454 /* Free the demux data */
455 if( p_es->p_demux_data != NULL )
457 free( p_es->p_demux_data );
460 /* Find the ES in the ES table */
461 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
464 if( p_input->stream.pp_es[i_es_index] == p_es )
470 p_input->stream.i_es_number--;
471 p_input->stream.pp_es[i_es_index] =
472 p_input->stream.pp_es[p_input->stream.i_es_number];
473 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
474 p_input->stream.i_es_number
475 * sizeof(es_descriptor_t *));
476 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
478 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
483 /*****************************************************************************
484 * InitDecConfig: initializes a decoder_config_t
485 *****************************************************************************/
486 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
487 decoder_config_t * p_config )
489 p_config->i_id = p_es->i_id;
490 p_config->i_type = p_es->i_type;
491 p_config->p_stream_ctrl =
492 &p_input->stream.control;
495 if( (p_config->p_decoder_fifo =
496 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
498 intf_ErrMsg( "Out of memory" );
502 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
503 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
504 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
505 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
506 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
507 p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
508 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
510 p_config->pf_init_bit_stream = InitBitstream;
512 p_input->stream.i_selected_es_number++;
514 p_input->stream.pp_selected_es = realloc(
515 p_input->stream.pp_selected_es,
516 p_input->stream.i_selected_es_number
517 * sizeof(es_descriptor_t *) );
518 if( p_input->stream.pp_selected_es == NULL )
520 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
523 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
529 /*****************************************************************************
530 * GetVdecConfig: returns a valid vdec_config_t
531 *****************************************************************************/
532 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
533 es_descriptor_t * p_es )
535 vdec_config_t * p_config;
537 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
538 if( p_config == NULL )
540 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
543 p_config->p_vout = p_input->p_default_vout;
544 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
553 /*****************************************************************************
554 * GetAdecConfig: returns a valid adec_config_t
555 *****************************************************************************/
556 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
557 es_descriptor_t * p_es )
559 adec_config_t * p_config;
561 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
562 if( p_config == NULL )
564 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
567 p_config->p_aout = p_input->p_default_aout;
568 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
577 /*****************************************************************************
578 * input_SelectES: selects an ES and spawns the associated decoder
579 *****************************************************************************
580 * Remember we are still supposed to have stream_lock when entering this
582 *****************************************************************************/
584 vlc_thread_t adec_CreateThread( void * );
585 vlc_thread_t ac3dec_CreateThread( void * );
586 vlc_thread_t vpar_CreateThread( void * );
587 vlc_thread_t spudec_CreateThread( void * );
589 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
592 decoder_capabilities_t decoder;
597 intf_ErrMsg( "Nothing to do in input_SelectES" );
602 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
605 if( p_es->p_decoder_fifo != NULL )
607 intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
611 switch( p_es->i_type )
615 if( p_main->b_audio )
617 decoder.pf_create_thread = adec_CreateThread;
618 p_config = (void *)GetAdecConfig( p_input, p_es );
620 /* Release the lock, not to block the input thread during
621 * the creation of the thread. */
622 vlc_mutex_unlock( &p_input->stream.stream_lock );
623 p_es->thread_id = input_RunDecoder( &decoder, p_config );
624 vlc_mutex_lock( &p_input->stream.stream_lock );
630 if( p_main->b_video )
632 decoder.pf_create_thread = vpar_CreateThread;
633 p_config = (void *)GetVdecConfig( p_input, p_es );
635 /* Release the lock, not to block the input thread during
636 * the creation of the thread. */
637 vlc_mutex_unlock( &p_input->stream.stream_lock );
638 p_es->thread_id = input_RunDecoder( &decoder, p_config );
639 vlc_mutex_lock( &p_input->stream.stream_lock );
644 if( p_main->b_audio )
646 decoder.pf_create_thread = ac3dec_CreateThread;
647 p_config = (void *)GetAdecConfig( p_input, p_es );
649 /* Release the lock, not to block the input thread during
650 * the creation of the thread. */
651 vlc_mutex_unlock( &p_input->stream.stream_lock );
652 p_es->thread_id = input_RunDecoder( &decoder, p_config );
653 vlc_mutex_lock( &p_input->stream.stream_lock );
658 if( p_main->b_video )
660 decoder.pf_create_thread = spudec_CreateThread;
661 p_config = (void *)GetVdecConfig( p_input, p_es );
663 /* Release the lock, not to block the input thread during
664 * the creation of the thread. */
665 vlc_mutex_unlock( &p_input->stream.stream_lock );
666 p_es->thread_id = input_RunDecoder( &decoder, p_config );
667 vlc_mutex_lock( &p_input->stream.stream_lock );
672 intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
677 if( p_es->thread_id == 0 )
685 /*****************************************************************************
686 * input_UnselectES: removes an ES from the list of selected ES
687 *****************************************************************************/
688 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
695 intf_ErrMsg( "Nothing to do in input_UnselectES" );
700 intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
703 if( p_es->p_decoder_fifo == NULL )
705 intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
709 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 p_input->stream.pp_selected_es = realloc(
726 p_input->stream.pp_selected_es,
727 p_input->stream.i_selected_es_number
728 * sizeof(es_descriptor_t *) );
730 if( p_input->stream.pp_selected_es == NULL )
732 intf_ErrMsg( "No more selected ES in input_UnselectES" );
733 vlc_mutex_unlock( &p_input->stream.stream_lock );