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.41 2001/03/15 01:42:20 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 *****************************************************************************/
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;
378 p_es->p_demux_data = malloc( i_data_len );
379 if( p_es->p_demux_data == NULL )
381 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
384 memset( p_es->p_demux_data, 0, i_data_len );
388 p_es->p_demux_data = NULL;
391 /* Add this ES to the program definition if one is given */
394 p_pgrm->i_es_number++;
395 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
397 * sizeof(es_descriptor_t *) );
398 if( p_pgrm->pp_es == NULL )
400 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
403 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
404 p_es->p_pgrm = p_pgrm;
414 /*****************************************************************************
416 *****************************************************************************/
417 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
419 int i_index, i_es_index;
420 pgrm_descriptor_t * p_pgrm;
423 p_pgrm = p_es->p_pgrm;
425 /* Kill associated decoder, if any. */
426 if( p_es->p_decoder_fifo != NULL )
428 input_EndDecoder( p_input, p_es );
431 /* Remove this ES from the description of the program if it is associated to
435 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
437 if( p_pgrm->pp_es[i_index] == p_es )
439 p_pgrm->i_es_number--;
440 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
441 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
443 * sizeof(es_descriptor_t *));
444 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
446 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
453 /* Free the demux data */
454 if( p_es->p_demux_data != NULL )
456 free( p_es->p_demux_data );
459 /* Find the ES in the ES table */
460 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
463 if( p_input->stream.pp_es[i_es_index] == p_es )
469 p_input->stream.i_es_number--;
470 p_input->stream.pp_es[i_es_index] =
471 p_input->stream.pp_es[p_input->stream.i_es_number];
472 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
473 p_input->stream.i_es_number
474 * sizeof(es_descriptor_t *));
475 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
477 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
482 /*****************************************************************************
483 * InitDecConfig: initializes a decoder_config_t
484 *****************************************************************************/
485 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
486 decoder_config_t * p_config )
488 p_config->i_id = p_es->i_id;
489 p_config->i_type = p_es->i_type;
490 p_config->p_stream_ctrl =
491 &p_input->stream.control;
494 if( (p_config->p_decoder_fifo =
495 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
497 intf_ErrMsg( "Out of memory" );
501 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
502 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
503 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
504 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
505 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
506 p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
507 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
509 p_config->pf_init_bit_stream = InitBitstream;
511 p_input->stream.i_selected_es_number++;
513 p_input->stream.pp_selected_es = realloc(
514 p_input->stream.pp_selected_es,
515 p_input->stream.i_selected_es_number
516 * sizeof(es_descriptor_t *) );
517 if( p_input->stream.pp_selected_es == NULL )
519 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
522 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
528 /*****************************************************************************
529 * GetVdecConfig: returns a valid vdec_config_t
530 *****************************************************************************/
531 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
532 es_descriptor_t * p_es )
534 vdec_config_t * p_config;
536 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
537 if( p_config == NULL )
539 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
542 p_config->p_vout = p_input->p_default_vout;
543 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
552 /*****************************************************************************
553 * GetAdecConfig: returns a valid adec_config_t
554 *****************************************************************************/
555 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
556 es_descriptor_t * p_es )
558 adec_config_t * p_config;
560 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
561 if( p_config == NULL )
563 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
566 p_config->p_aout = p_input->p_default_aout;
567 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
576 /*****************************************************************************
577 * input_SelectES: selects an ES and spawns the associated decoder
578 *****************************************************************************
579 * Remember we are still supposed to have stream_lock when entering this
581 *****************************************************************************/
583 vlc_thread_t adec_CreateThread( void * );
584 vlc_thread_t ac3dec_CreateThread( void * );
585 vlc_thread_t vpar_CreateThread( void * );
586 vlc_thread_t spudec_CreateThread( void * );
588 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
591 decoder_capabilities_t decoder;
595 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
598 if( p_es->p_decoder_fifo != NULL )
600 intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
604 switch( p_es->i_type )
608 if( p_main->b_audio )
610 decoder.pf_create_thread = adec_CreateThread;
611 p_config = (void *)GetAdecConfig( p_input, p_es );
613 /* Release the lock, not to block the input thread during
614 * the creation of the thread. */
615 vlc_mutex_unlock( &p_input->stream.stream_lock );
616 p_es->thread_id = input_RunDecoder( &decoder, p_config );
617 vlc_mutex_lock( &p_input->stream.stream_lock );
623 if( p_main->b_video )
625 decoder.pf_create_thread = vpar_CreateThread;
626 p_config = (void *)GetVdecConfig( p_input, p_es );
628 /* Release the lock, not to block the input thread during
629 * the creation of the thread. */
630 vlc_mutex_unlock( &p_input->stream.stream_lock );
631 p_es->thread_id = input_RunDecoder( &decoder, p_config );
632 vlc_mutex_lock( &p_input->stream.stream_lock );
637 if( p_main->b_audio )
639 decoder.pf_create_thread = ac3dec_CreateThread;
640 p_config = (void *)GetAdecConfig( p_input, p_es );
642 /* Release the lock, not to block the input thread during
643 * the creation of the thread. */
644 vlc_mutex_unlock( &p_input->stream.stream_lock );
645 p_es->thread_id = input_RunDecoder( &decoder, p_config );
646 vlc_mutex_lock( &p_input->stream.stream_lock );
651 if( p_main->b_video )
653 decoder.pf_create_thread = spudec_CreateThread;
654 p_config = (void *)GetVdecConfig( p_input, p_es );
656 /* Release the lock, not to block the input thread during
657 * the creation of the thread. */
658 vlc_mutex_unlock( &p_input->stream.stream_lock );
659 p_es->thread_id = input_RunDecoder( &decoder, p_config );
660 vlc_mutex_lock( &p_input->stream.stream_lock );
665 intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
670 if( p_es->thread_id == 0 )
678 /*****************************************************************************
679 * input_UnselectES: removes an ES from the list of selected ES
680 *****************************************************************************/
681 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
687 intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
690 if( p_es->p_decoder_fifo == NULL )
692 intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
696 /* Release lock, not to block the input thread. */
697 vlc_mutex_unlock( &p_input->stream.stream_lock );
698 input_EndDecoder( p_input, p_es );
699 vlc_mutex_lock( &p_input->stream.stream_lock );
701 if( p_es->p_decoder_fifo == NULL )
703 p_input->stream.i_selected_es_number--;
705 while( ( i_index < p_input->stream.i_selected_es_number ) &&
706 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
711 p_input->stream.pp_selected_es[i_index] =
712 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
714 p_input->stream.pp_selected_es = realloc(
715 p_input->stream.pp_selected_es,
716 p_input->stream.i_selected_es_number
717 * sizeof(es_descriptor_t *) );
718 if( p_input->stream.pp_selected_es == NULL )
720 intf_ErrMsg( "Unable to realloc memory in input_UnSelectES" );