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.39 2001/03/02 15:51:22 massiot 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 = 0;
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;
372 p_es->p_decoder_fifo = NULL;
377 p_es->p_demux_data = malloc( i_data_len );
378 if( p_es->p_demux_data == NULL )
380 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
383 memset( p_es->p_demux_data, 0, i_data_len );
387 p_es->p_demux_data = NULL;
390 /* Add this ES to the program definition if one is given */
393 p_pgrm->i_es_number++;
394 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
396 * sizeof(es_descriptor_t *) );
397 if( p_pgrm->pp_es == NULL )
399 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
402 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
403 p_es->p_pgrm = p_pgrm;
413 /*****************************************************************************
415 *****************************************************************************/
416 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
418 int i_index, i_es_index;
419 pgrm_descriptor_t * p_pgrm;
422 p_pgrm = p_es->p_pgrm;
424 /* Kill associated decoder, if any. */
425 if( p_es->p_decoder_fifo != NULL )
427 input_EndDecoder( p_input, p_es );
430 /* Remove this ES from the description of the program if it is associated to
434 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
436 if( p_pgrm->pp_es[i_index] == p_es )
438 p_pgrm->i_es_number--;
439 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
440 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
442 * sizeof(es_descriptor_t *));
443 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
445 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
452 /* Free the demux data */
453 if( p_es->p_demux_data != NULL )
455 free( p_es->p_demux_data );
458 /* Find the ES in the ES table */
459 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
462 if( p_input->stream.pp_es[i_es_index] == p_es )
468 p_input->stream.i_es_number--;
469 p_input->stream.pp_es[i_es_index] =
470 p_input->stream.pp_es[p_input->stream.i_es_number];
471 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
472 p_input->stream.i_es_number
473 * sizeof(es_descriptor_t *));
474 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
476 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
481 /*****************************************************************************
482 * InitDecConfig: initializes a decoder_config_t
483 *****************************************************************************/
484 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
485 decoder_config_t * p_config )
487 p_config->i_id = p_es->i_id;
488 p_config->i_type = p_es->i_type;
489 p_config->p_stream_ctrl =
490 &p_input->stream.control;
493 if( (p_config->p_decoder_fifo =
494 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
496 intf_ErrMsg( "Out of memory" );
500 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
501 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
502 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
503 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
504 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
505 p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
506 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
508 p_config->pf_init_bit_stream = InitBitstream;
510 p_input->stream.i_selected_es_number++;
511 p_input->stream.pp_selected_es = realloc(
512 p_input->stream.pp_selected_es,
513 p_input->stream.i_selected_es_number
514 * sizeof(es_descriptor_t *) );
515 if( p_input->stream.pp_selected_es == NULL )
517 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
520 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
526 /*****************************************************************************
527 * GetVdecConfig: returns a valid vdec_config_t
528 *****************************************************************************/
529 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
530 es_descriptor_t * p_es )
532 vdec_config_t * p_config;
534 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
535 if( p_config == NULL )
537 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
540 p_config->p_vout = p_input->p_default_vout;
541 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
550 /*****************************************************************************
551 * GetAdecConfig: returns a valid adec_config_t
552 *****************************************************************************/
553 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
554 es_descriptor_t * p_es )
556 adec_config_t * p_config;
558 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
559 if( p_config == NULL )
561 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
564 p_config->p_aout = p_input->p_default_aout;
565 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
574 /*****************************************************************************
575 * input_SelectES: selects an ES and spawns the associated decoder
576 *****************************************************************************
577 * Remember we are still supposed to have stream_lock when entering this
579 *****************************************************************************/
581 vlc_thread_t adec_CreateThread( void * );
582 vlc_thread_t ac3dec_CreateThread( void * );
583 vlc_thread_t vpar_CreateThread( void * );
584 vlc_thread_t spudec_CreateThread( void * );
586 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
589 decoder_capabilities_t decoder;
593 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
596 if( p_es->p_decoder_fifo != NULL )
598 intf_ErrMsg( "ES %d is already selected", p_es->i_id );
602 switch( p_es->i_type )
606 if( p_main->b_audio )
608 decoder.pf_create_thread = adec_CreateThread;
609 p_config = (void *)GetAdecConfig( p_input, p_es );
611 /* Release the lock, not to block the input thread during
612 * the creation of the thread. */
613 vlc_mutex_unlock( &p_input->stream.stream_lock );
614 p_es->thread_id = input_RunDecoder( &decoder, p_config );
615 vlc_mutex_lock( &p_input->stream.stream_lock );
621 if( p_main->b_video )
623 decoder.pf_create_thread = vpar_CreateThread;
624 p_config = (void *)GetVdecConfig( p_input, p_es );
626 /* Release the lock, not to block the input thread during
627 * the creation of the thread. */
628 vlc_mutex_unlock( &p_input->stream.stream_lock );
629 p_es->thread_id = input_RunDecoder( &decoder, p_config );
630 vlc_mutex_lock( &p_input->stream.stream_lock );
635 if( p_main->b_audio )
637 decoder.pf_create_thread = ac3dec_CreateThread;
638 p_config = (void *)GetAdecConfig( p_input, p_es );
640 /* Release the lock, not to block the input thread during
641 * the creation of the thread. */
642 vlc_mutex_unlock( &p_input->stream.stream_lock );
643 p_es->thread_id = input_RunDecoder( &decoder, p_config );
644 vlc_mutex_lock( &p_input->stream.stream_lock );
649 if( p_main->b_video )
651 decoder.pf_create_thread = spudec_CreateThread;
652 p_config = (void *)GetVdecConfig( p_input, p_es );
654 /* Release the lock, not to block the input thread during
655 * the creation of the thread. */
656 vlc_mutex_unlock( &p_input->stream.stream_lock );
657 p_es->thread_id = input_RunDecoder( &decoder, p_config );
658 vlc_mutex_lock( &p_input->stream.stream_lock );
663 intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
668 if( p_es->thread_id == 0 )
676 /*****************************************************************************
677 * input_UnselectES: removes an ES from the list of selected ES
678 *****************************************************************************/
679 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
685 intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
688 if( p_es->p_decoder_fifo == NULL )
690 intf_ErrMsg( "ES %d is not selected", p_es->i_id );
694 /* Release lock, not to block the input thread. */
695 vlc_mutex_unlock( &p_input->stream.stream_lock );
696 input_EndDecoder( p_input, p_es );
697 vlc_mutex_lock( &p_input->stream.stream_lock );
699 if( p_es->p_decoder_fifo == NULL )
701 p_input->stream.i_selected_es_number--;
703 while( ( i_index < p_input->stream.i_selected_es_number ) &&
704 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
709 p_input->stream.pp_selected_es[i_index] =
710 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
712 p_input->stream.pp_selected_es = realloc(
713 p_input->stream.pp_selected_es,
714 p_input->stream.i_selected_es_number
715 * sizeof(es_descriptor_t *) );
716 if( p_input->stream.pp_selected_es == NULL )
718 intf_ErrMsg( "Unable to realloc memory in input_UnSelectES" );