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.35 2001/02/22 16:17:12 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] );
92 /*****************************************************************************
93 * input_FindProgram: returns a pointer to a program described by its ID
94 *****************************************************************************/
95 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
99 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
101 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
103 return p_input->stream.pp_programs[i];
110 /*****************************************************************************
111 * input_AddProgram: add and init a program descriptor
112 *****************************************************************************
113 * This program descriptor will be referenced in the given stream descriptor
114 *****************************************************************************/
115 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
116 u16 i_pgrm_id, size_t i_data_len )
118 /* Where to add the pgrm */
119 int i_pgrm_index = p_input->stream.i_pgrm_number;
121 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
123 /* Add an entry to the list of program associated with the stream */
124 p_input->stream.i_pgrm_number++;
125 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
126 p_input->stream.i_pgrm_number
127 * sizeof(pgrm_descriptor_t *) );
128 if( p_input->stream.pp_programs == NULL )
130 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
134 /* Allocate the structure to store this description */
135 p_input->stream.pp_programs[i_pgrm_index] =
136 malloc( sizeof(pgrm_descriptor_t) );
137 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
139 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
143 /* Init this entry */
144 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
145 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
146 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
148 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
149 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
151 input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
153 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
156 p_input->stream.pp_programs[i_pgrm_index]->p_vout
157 = p_input->p_default_vout;
158 p_input->stream.pp_programs[i_pgrm_index]->p_aout
159 = p_input->p_default_aout;
163 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
164 malloc( i_data_len );
165 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
167 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
170 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
174 return p_input->stream.pp_programs[i_pgrm_index];
177 /*****************************************************************************
178 * input_DelProgram: destroy a program descriptor
179 *****************************************************************************
180 * All ES descriptions referenced in the descriptor will be deleted.
181 *****************************************************************************/
182 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
188 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
190 /* Free the structures that describe the es that belongs to that program */
191 while( p_pgrm->i_es_number )
193 input_DelES( p_input, p_pgrm->pp_es[0] );
196 /* Free the demux data */
197 if( p_pgrm->p_demux_data != NULL )
199 free( p_pgrm->p_demux_data );
202 /* Find the program in the programs table */
203 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
206 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
210 /* Remove this program from the stream's list of programs */
211 p_input->stream.i_pgrm_number--;
213 p_input->stream.pp_programs[i_pgrm_index] =
214 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
215 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
216 p_input->stream.i_pgrm_number
217 * sizeof(pgrm_descriptor_t *) );
219 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
221 intf_ErrMsg( "input error: unable to realloc program list"
222 " in input_DelProgram" );
225 /* Free the description of this program */
229 /*****************************************************************************
230 * input_AddArea: add and init an area descriptor
231 *****************************************************************************
232 * This area descriptor will be referenced in the given stream descriptor
233 *****************************************************************************/
234 input_area_t * input_AddArea( input_thread_t * p_input )
236 /* Where to add the pgrm */
237 int i_area_index = p_input->stream.i_area_nb;
239 intf_DbgMsg("Adding description for area %d", i_area_index );
241 /* Add an entry to the list of program associated with the stream */
242 p_input->stream.i_area_nb++;
243 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
244 p_input->stream.i_area_nb
245 * sizeof(input_area_t *) );
246 if( p_input->stream.pp_areas == NULL )
248 intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
252 /* Allocate the structure to store this description */
253 p_input->stream.pp_areas[i_area_index] =
254 malloc( sizeof(input_area_t) );
255 if( p_input->stream.pp_areas[i_area_index] == NULL )
257 intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
261 /* Init this entry */
262 p_input->stream.pp_areas[i_area_index]->i_id = 0;
263 p_input->stream.pp_areas[i_area_index]->i_start = 0;
264 p_input->stream.pp_areas[i_area_index]->i_size = 0;
265 p_input->stream.pp_areas[i_area_index]->i_tell = 0;
266 p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
267 p_input->stream.pp_areas[i_area_index]->i_part_nb = 0;
268 p_input->stream.pp_areas[i_area_index]->i_part= 0;
270 return p_input->stream.pp_areas[i_area_index];
273 /*****************************************************************************
274 * input_DelArea: destroy a area descriptor
275 *****************************************************************************
276 * All ES descriptions referenced in the descriptor will be deleted.
277 *****************************************************************************/
278 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
284 intf_DbgMsg("Deleting description for area %d", p_area->i_nb );
286 /* Find the area in the areas table */
287 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
290 if( p_input->stream.pp_areas[i_area_index] == p_area )
294 /* Remove this area from the stream's list of areas */
295 p_input->stream.i_area_nb--;
297 p_input->stream.pp_areas[i_area_index] =
298 p_input->stream.pp_areas[p_input->stream.i_area_nb];
299 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
300 p_input->stream.i_area_nb
301 * sizeof(input_area_t *) );
303 if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
305 intf_ErrMsg( "input error: unable to realloc area list"
306 " in input_DelArea" );
309 /* Free the description of this area */
314 /*****************************************************************************
315 * input_FindES: returns a pointer to an ES described by its ID
316 *****************************************************************************/
317 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
321 for( i = 0; i < p_input->stream.i_es_number; i++ )
323 if( p_input->stream.pp_es[i]->i_id == i_es_id )
325 return p_input->stream.pp_es[i];
332 /*****************************************************************************
334 *****************************************************************************
335 * Reserve a slot in the table of ES descriptors for the ES and add it to the
336 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
338 *****************************************************************************/
339 es_descriptor_t * input_AddES( input_thread_t * p_input,
340 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
343 es_descriptor_t * p_es;
345 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
347 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
350 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
353 p_input->stream.i_es_number++;
354 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
355 p_input->stream.i_es_number
356 * sizeof(es_descriptor_t *) );
357 if( p_input->stream.pp_es == NULL )
359 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
362 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
364 /* Init its values */
365 p_es->i_id = i_es_id;
367 p_es->p_decoder_fifo = NULL;
372 p_es->p_demux_data = malloc( i_data_len );
373 if( p_es->p_demux_data == NULL )
375 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
378 memset( p_es->p_demux_data, 0, i_data_len );
382 p_es->p_demux_data = NULL;
385 /* Add this ES to the program definition if one is given */
388 p_pgrm->i_es_number++;
389 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
391 * sizeof(es_descriptor_t *) );
392 if( p_pgrm->pp_es == NULL )
394 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
397 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
398 p_es->p_pgrm = p_pgrm;
408 /*****************************************************************************
410 *****************************************************************************/
411 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
413 int i_index, i_es_index;
414 pgrm_descriptor_t * p_pgrm;
417 p_pgrm = p_es->p_pgrm;
419 /* Kill associated decoder, if any. */
420 if( p_es->p_decoder_fifo != NULL )
422 input_EndDecoder( p_input, p_es );
425 /* Remove this ES from the description of the program if it is associated to
429 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
431 if( p_pgrm->pp_es[i_index] == p_es )
433 p_pgrm->i_es_number--;
434 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
435 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
437 * sizeof(es_descriptor_t *));
438 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
440 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
447 /* Free the demux data */
448 if( p_es->p_demux_data != NULL )
450 free( p_es->p_demux_data );
453 /* Find the ES in the ES table */
454 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
457 if( p_input->stream.pp_es[i_es_index] == p_es )
463 p_input->stream.i_es_number--;
464 p_input->stream.pp_es[i_es_index] =
465 p_input->stream.pp_es[p_input->stream.i_es_number];
466 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
467 p_input->stream.i_es_number
468 * sizeof(es_descriptor_t *));
469 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
471 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
476 /*****************************************************************************
477 * input_DumpStream: dumps the contents of a stream descriptor
478 *****************************************************************************/
479 void input_DumpStream( input_thread_t * p_input )
482 #define S p_input->stream
483 intf_Msg( "input info: Dumping stream ID 0x%x", S.i_stream_id );
485 intf_Msg( "input info: seekable stream, position: %lld/%lld",
486 S.p_selected_area->i_tell, S.p_selected_area->i_size );
488 intf_Msg( "input info: %s", S.b_pace_control ? "pace controlled" :
489 "pace un-controlled" );
491 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
493 #define P p_input->stream.pp_programs[i]
494 intf_Msg( "input info: Dumping program 0x%x, version %d (%s)",
495 P->i_number, P->i_version,
496 P->b_is_ok ? "complete" : "partial" );
498 for( j = 0; j < p_input->stream.pp_programs[i]->i_es_number; j++ )
500 #define ES p_input->stream.pp_programs[i]->pp_es[j]
501 intf_Msg( "input info: ES 0x%x, stream 0x%x, type 0x%x, %s",
502 ES->i_id, ES->i_stream_id, ES->i_type,
503 ES->p_decoder_fifo != NULL ? "selected" : "not selected");
509 /*****************************************************************************
510 * InitDecConfig: initializes a decoder_config_t
511 *****************************************************************************/
512 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
513 decoder_config_t * p_config )
515 p_config->i_id = p_es->i_id;
516 p_config->i_type = p_es->i_type;
517 p_config->p_stream_ctrl =
518 &p_input->stream.control;
521 if( (p_config->p_decoder_fifo =
522 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
524 intf_ErrMsg( "Out of memory" );
528 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
529 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
530 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
531 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
532 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
533 p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
534 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
536 p_config->pf_init_bit_stream = InitBitstream;
541 /*****************************************************************************
542 * GetVdecConfig: returns a valid vdec_config_t
543 *****************************************************************************/
544 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
545 es_descriptor_t * p_es )
547 vdec_config_t * p_config;
549 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
550 if( p_config == NULL )
552 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
555 p_config->p_vout = p_input->p_default_vout;
556 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
565 /*****************************************************************************
566 * GetAdecConfig: returns a valid adec_config_t
567 *****************************************************************************/
568 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
569 es_descriptor_t * p_es )
571 adec_config_t * p_config;
573 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
574 if( p_config == NULL )
576 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
579 p_config->p_aout = p_input->p_default_aout;
580 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
589 /*****************************************************************************
590 * input_SelectES: selects an ES and spawns the associated decoder
591 *****************************************************************************/
593 vlc_thread_t adec_CreateThread( void * );
594 vlc_thread_t ac3dec_CreateThread( void * );
595 vlc_thread_t vpar_CreateThread( void * );
596 vlc_thread_t spudec_CreateThread( void * );
598 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
601 decoder_capabilities_t decoder;
604 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
607 if( p_es->p_decoder_fifo != NULL )
609 intf_ErrMsg( "ES %d is already selected", p_es->i_id );
613 switch( p_es->i_type )
617 if( p_main->b_audio )
619 decoder.pf_create_thread = adec_CreateThread;
620 p_es->thread_id = input_RunDecoder( &decoder,
621 (void *)GetAdecConfig( p_input, p_es ) );
627 if( p_main->b_video )
629 decoder.pf_create_thread = vpar_CreateThread;
630 p_es->thread_id = input_RunDecoder( &decoder,
631 (void *)GetVdecConfig( p_input, p_es ) );
636 if( p_main->b_audio )
638 decoder.pf_create_thread = ac3dec_CreateThread;
639 p_es->thread_id = input_RunDecoder( &decoder,
640 (void *)GetAdecConfig( p_input, p_es ) );
645 if( p_main->b_video )
647 decoder.pf_create_thread = spudec_CreateThread;
648 p_es->thread_id = input_RunDecoder( &decoder,
649 (void *)GetVdecConfig( p_input, p_es ) );
654 intf_ErrMsg( "Unknown stream type %d", p_es->i_type );
659 if( p_es->thread_id == 0 )
664 if( p_es->p_decoder_fifo != NULL )
666 p_input->stream.i_selected_es_number++;
667 p_input->stream.pp_selected_es = realloc(
668 p_input->stream.pp_selected_es,
669 p_input->stream.i_selected_es_number
670 * sizeof(es_descriptor_t *) );
671 if( p_input->stream.pp_selected_es == NULL )
673 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
676 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
682 /*****************************************************************************
683 * input_UnSelectES: removes an ES from the list of selected ES
684 *****************************************************************************/
685 int input_UnSelectES( input_thread_t * p_input, es_descriptor_t * p_es )
691 intf_DbgMsg( "UnSelecting ES 0x%x", p_es->i_id );
694 if( p_es->p_decoder_fifo == NULL )
696 intf_ErrMsg( "ES %d is not selected", p_es->i_id );
700 input_EndDecoder( p_input, p_es );
702 if( p_es->p_decoder_fifo == NULL )
704 p_input->stream.i_selected_es_number--;
706 while( ( i_index < p_input->stream.i_selected_es_number ) &&
707 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
712 p_input->stream.pp_selected_es[i_index] =
713 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
715 p_input->stream.pp_selected_es = realloc(
716 p_input->stream.pp_selected_es,
717 p_input->stream.i_selected_es_number
718 * sizeof(es_descriptor_t *) );
719 if( p_input->stream.pp_selected_es == NULL )
721 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );