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.54 2001/05/01 04:18:18 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 *****************************************************************************/
30 #include <string.h> /* memcpy(), memset() */
31 #include <sys/types.h> /* off_t */
41 #include "stream_control.h"
42 #include "input_ext-intf.h"
43 #include "input_ext-dec.h"
46 #include "main.h" /* --noaudio --novideo */
49 * NOTICE : all of these functions expect you to have taken the lock on
50 * p_input->stream.lock
53 /*****************************************************************************
54 * input_InitStream: init the stream descriptor of the given input
55 *****************************************************************************/
56 int input_InitStream( input_thread_t * p_input, size_t i_data_len )
59 p_input->stream.i_method = INPUT_METHOD_NONE;
60 p_input->stream.i_stream_id = 0;
62 /* initialized to 0 since we don't give the signal to the interface
63 * before the end of input initialization */
64 p_input->stream.b_changed = 0;
65 p_input->stream.pp_es = NULL;
66 p_input->stream.pp_selected_es = NULL;
67 p_input->stream.pp_programs = NULL;
71 if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
73 intf_ErrMsg( "Unable to allocate memory in input_InitStream");
76 memset( p_input->stream.p_demux_data, 0, i_data_len );
82 /*****************************************************************************
83 * input_EndStream: free all stream descriptors
84 *****************************************************************************/
85 void input_EndStream( input_thread_t * p_input )
87 /* Free all programs and associated ES, and associated decoders. */
88 while( p_input->stream.i_pgrm_number )
90 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
93 /* Free standalone ES */
94 while( p_input->stream.i_es_number )
96 input_DelES( p_input, p_input->stream.pp_es[0] );
100 while( p_input->stream.i_area_nb )
102 input_DelArea( p_input, p_input->stream.pp_areas[0] );
105 if( p_input->stream.p_demux_data != NULL )
107 free( p_input->stream.p_demux_data );
111 /*****************************************************************************
112 * input_FindProgram: returns a pointer to a program described by its ID
113 *****************************************************************************/
114 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
118 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
120 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
122 return p_input->stream.pp_programs[i];
129 /*****************************************************************************
130 * input_AddProgram: add and init a program descriptor
131 *****************************************************************************
132 * This program descriptor will be referenced in the given stream descriptor
133 *****************************************************************************/
134 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
135 u16 i_pgrm_id, size_t i_data_len )
137 /* Where to add the pgrm */
138 int i_pgrm_index = p_input->stream.i_pgrm_number;
140 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
142 /* Add an entry to the list of program associated with the stream */
143 p_input->stream.i_pgrm_number++;
144 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
145 p_input->stream.i_pgrm_number
146 * sizeof(pgrm_descriptor_t *) );
147 if( p_input->stream.pp_programs == NULL )
149 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
153 /* Allocate the structure to store this description */
154 p_input->stream.pp_programs[i_pgrm_index] =
155 malloc( sizeof(pgrm_descriptor_t) );
156 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
158 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
162 /* Init this entry */
163 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
164 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
165 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
167 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
168 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
170 input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
172 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
175 p_input->stream.pp_programs[i_pgrm_index]->p_vout
176 = p_input->p_default_vout;
180 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
181 malloc( i_data_len );
182 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
184 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
187 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
191 return p_input->stream.pp_programs[i_pgrm_index];
194 /*****************************************************************************
195 * input_DelProgram: destroy a program descriptor
196 *****************************************************************************
197 * All ES descriptions referenced in the descriptor will be deleted.
198 *****************************************************************************/
199 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
205 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
207 /* Free the structures that describe the es that belongs to that program */
208 while( p_pgrm->i_es_number )
210 input_DelES( p_input, p_pgrm->pp_es[0] );
213 /* Free the demux data */
214 if( p_pgrm->p_demux_data != NULL )
216 free( p_pgrm->p_demux_data );
219 /* Find the program in the programs table */
220 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
223 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
227 /* Remove this program from the stream's list of programs */
228 p_input->stream.i_pgrm_number--;
230 p_input->stream.pp_programs[i_pgrm_index] =
231 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
232 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
233 p_input->stream.i_pgrm_number
234 * sizeof(pgrm_descriptor_t *) );
236 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
238 intf_ErrMsg( "input error: unable to realloc program list"
239 " in input_DelProgram" );
242 /* Free the description of this program */
246 /*****************************************************************************
247 * input_AddArea: add and init an area descriptor
248 *****************************************************************************
249 * This area descriptor will be referenced in the given stream descriptor
250 *****************************************************************************/
251 input_area_t * input_AddArea( input_thread_t * p_input )
253 /* Where to add the pgrm */
254 int i_area_index = p_input->stream.i_area_nb;
256 intf_DbgMsg("Adding description for area %d", i_area_index );
258 /* Add an entry to the list of program associated with the stream */
259 p_input->stream.i_area_nb++;
260 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
261 p_input->stream.i_area_nb
262 * sizeof(input_area_t *) );
263 if( p_input->stream.pp_areas == NULL )
265 intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
269 /* Allocate the structure to store this description */
270 p_input->stream.pp_areas[i_area_index] =
271 malloc( sizeof(input_area_t) );
272 if( p_input->stream.pp_areas[i_area_index] == NULL )
274 intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
278 /* Init this entry */
279 p_input->stream.pp_areas[i_area_index]->i_id = 0;
280 p_input->stream.pp_areas[i_area_index]->i_start = 0;
281 p_input->stream.pp_areas[i_area_index]->i_size = 0;
282 p_input->stream.pp_areas[i_area_index]->i_tell = 0;
283 p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
284 p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
285 p_input->stream.pp_areas[i_area_index]->i_part= 0;
286 p_input->stream.pp_areas[i_area_index]->i_angle_nb = 1;
287 p_input->stream.pp_areas[i_area_index]->i_angle = 0;
289 return p_input->stream.pp_areas[i_area_index];
292 /*****************************************************************************
293 * input_DelArea: destroy a area descriptor
294 *****************************************************************************
295 * All ES descriptions referenced in the descriptor will be deleted.
296 *****************************************************************************/
297 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
303 intf_DbgMsg("Deleting description for area %d", p_area->i_id );
305 /* Find the area in the areas table */
306 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
309 if( p_input->stream.pp_areas[i_area_index] == p_area )
313 /* Remove this area from the stream's list of areas */
314 p_input->stream.i_area_nb--;
316 p_input->stream.pp_areas[i_area_index] =
317 p_input->stream.pp_areas[p_input->stream.i_area_nb];
318 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
319 p_input->stream.i_area_nb
320 * sizeof(input_area_t *) );
322 if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
324 intf_ErrMsg( "input error: unable to realloc area list"
325 " in input_DelArea" );
328 /* Free the description of this area */
333 /*****************************************************************************
334 * input_FindES: returns a pointer to an ES described by its ID
335 *****************************************************************************/
336 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
340 for( i = 0; i < p_input->stream.i_es_number; i++ )
342 if( p_input->stream.pp_es[i]->i_id == i_es_id )
344 return p_input->stream.pp_es[i];
351 /*****************************************************************************
353 *****************************************************************************
354 * Reserve a slot in the table of ES descriptors for the ES and add it to the
355 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
357 *****************************************************************************/
358 es_descriptor_t * input_AddES( input_thread_t * p_input,
359 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
362 es_descriptor_t * p_es;
364 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
366 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
369 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
372 p_input->stream.i_es_number++;
373 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
374 p_input->stream.i_es_number
375 * sizeof(es_descriptor_t *) );
376 if( p_input->stream.pp_es == NULL )
378 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
381 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
383 /* Init its values */
384 p_es->i_id = i_es_id;
385 p_es->psz_desc[0] = '\0';
387 p_es->p_decoder_fifo = NULL;
389 p_es->i_cat = UNKNOWN_ES;
393 p_es->p_demux_data = malloc( i_data_len );
394 if( p_es->p_demux_data == NULL )
396 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
399 memset( p_es->p_demux_data, 0, i_data_len );
403 p_es->p_demux_data = NULL;
406 /* Add this ES to the program definition if one is given */
409 p_pgrm->i_es_number++;
410 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
412 * sizeof(es_descriptor_t *) );
413 if( p_pgrm->pp_es == NULL )
415 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
418 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
419 p_es->p_pgrm = p_pgrm;
429 /*****************************************************************************
431 *****************************************************************************/
432 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
434 int i_index, i_es_index;
435 pgrm_descriptor_t * p_pgrm;
438 p_pgrm = p_es->p_pgrm;
440 /* Kill associated decoder, if any. */
441 if( p_es->p_decoder_fifo != NULL )
443 input_EndDecoder( p_input, p_es );
446 /* Remove this ES from the description of the program if it is associated to
450 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
452 if( p_pgrm->pp_es[i_index] == p_es )
454 p_pgrm->i_es_number--;
455 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
456 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
458 * sizeof(es_descriptor_t *));
459 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
461 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
468 /* Free the demux data */
469 if( p_es->p_demux_data != NULL )
471 free( p_es->p_demux_data );
474 /* Find the ES in the ES table */
475 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
478 if( p_input->stream.pp_es[i_es_index] == p_es )
484 p_input->stream.i_es_number--;
485 p_input->stream.pp_es[i_es_index] =
486 p_input->stream.pp_es[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.i_es_number && p_input->stream.pp_es == NULL )
492 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
497 /*****************************************************************************
498 * InitDecConfig: initializes a decoder_config_t
499 *****************************************************************************/
500 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
501 decoder_config_t * p_config )
503 p_config->i_id = p_es->i_id;
504 p_config->i_type = p_es->i_type;
505 p_config->p_stream_ctrl =
506 &p_input->stream.control;
509 if( (p_config->p_decoder_fifo =
510 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
512 intf_ErrMsg( "Out of memory" );
516 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
517 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
518 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
519 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
520 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
521 p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
522 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
524 p_config->pf_init_bit_stream = InitBitstream;
526 p_input->stream.i_selected_es_number++;
528 p_input->stream.pp_selected_es = realloc(
529 p_input->stream.pp_selected_es,
530 p_input->stream.i_selected_es_number
531 * sizeof(es_descriptor_t *) );
532 if( p_input->stream.pp_selected_es == NULL )
534 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
537 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
543 /*****************************************************************************
544 * GetVdecConfig: returns a valid vdec_config_t
545 *****************************************************************************/
546 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
547 es_descriptor_t * p_es )
549 vdec_config_t * p_config;
551 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
552 if( p_config == NULL )
554 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
557 p_config->p_vout = p_input->p_default_vout;
558 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
567 /*****************************************************************************
568 * GetAdecConfig: returns a valid adec_config_t
569 *****************************************************************************/
570 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
571 es_descriptor_t * p_es )
573 adec_config_t * p_config;
575 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
576 if( p_config == NULL )
578 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
582 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
591 /*****************************************************************************
592 * input_SelectES: selects an ES and spawns the associated decoder
593 *****************************************************************************
594 * Remember we are still supposed to have stream_lock when entering this
596 *****************************************************************************/
598 vlc_thread_t adec_CreateThread( void * );
599 vlc_thread_t ac3dec_CreateThread( void * );
600 vlc_thread_t ac3spdif_CreateThread( void * );
601 vlc_thread_t spdif_CreateThread( void * );
602 vlc_thread_t vpar_CreateThread( void * );
603 vlc_thread_t spudec_CreateThread( void * );
605 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
608 decoder_capabilities_t decoder;
613 intf_ErrMsg( "Nothing to do in input_SelectES" );
618 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
621 if( p_es->p_decoder_fifo != NULL )
623 intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
627 switch( p_es->i_type )
631 if( p_main->b_audio )
633 decoder.pf_create_thread = adec_CreateThread;
634 p_config = (void *)GetAdecConfig( p_input, p_es );
636 /* Release the lock, not to block the input thread during
637 * the creation of the thread. */
638 vlc_mutex_unlock( &p_input->stream.stream_lock );
639 p_es->thread_id = input_RunDecoder( &decoder, p_config );
640 vlc_mutex_lock( &p_input->stream.stream_lock );
646 if( p_main->b_video )
648 decoder.pf_create_thread = vpar_CreateThread;
649 p_config = (void *)GetVdecConfig( p_input, p_es );
651 /* Release the lock, not to block the input thread during
652 * the creation of the thread. */
653 vlc_mutex_unlock( &p_input->stream.stream_lock );
654 p_es->thread_id = input_RunDecoder( &decoder, p_config );
655 vlc_mutex_lock( &p_input->stream.stream_lock );
660 if( p_main->b_audio )
662 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) )
664 decoder.pf_create_thread = spdif_CreateThread;
668 decoder.pf_create_thread = ac3dec_CreateThread;
671 p_config = (void *)GetAdecConfig( p_input, p_es );
673 /* Release the lock, not to block the input thread during
674 * the creation of the thread. */
675 vlc_mutex_unlock( &p_input->stream.stream_lock );
676 p_es->thread_id = input_RunDecoder( &decoder, p_config );
677 vlc_mutex_lock( &p_input->stream.stream_lock );
682 if( p_main->b_audio )
684 if( p_main->b_spdif )
686 decoder.pf_create_thread = spdif_CreateThread;
690 intf_ErrMsg( "input error: LPCM audio not handled yet" );
694 p_config = (void *)GetAdecConfig( p_input, p_es );
696 /* Release the lock, not to block the input thread during
697 * the creation of the thread. */
698 vlc_mutex_unlock( &p_input->stream.stream_lock );
699 p_es->thread_id = input_RunDecoder( &decoder, p_config );
700 vlc_mutex_lock( &p_input->stream.stream_lock );
705 if( p_main->b_video )
707 decoder.pf_create_thread = spudec_CreateThread;
708 p_config = (void *)GetVdecConfig( p_input, p_es );
710 /* Release the lock, not to block the input thread during
711 * the creation of the thread. */
712 vlc_mutex_unlock( &p_input->stream.stream_lock );
713 p_es->thread_id = input_RunDecoder( &decoder, p_config );
714 vlc_mutex_lock( &p_input->stream.stream_lock );
719 intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
724 if( p_es->thread_id == 0 )
732 /*****************************************************************************
733 * input_UnselectES: removes an ES from the list of selected ES
734 *****************************************************************************/
735 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
742 intf_ErrMsg( "Nothing to do in input_UnselectES" );
747 intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
750 if( p_es->p_decoder_fifo == NULL )
752 intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
756 input_EndDecoder( p_input, p_es );
758 if( ( p_es->p_decoder_fifo == NULL ) &&
759 ( p_input->stream.i_selected_es_number > 0 ) )
761 p_input->stream.i_selected_es_number--;
763 while( ( i_index < p_input->stream.i_selected_es_number ) &&
764 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
769 p_input->stream.pp_selected_es[i_index] =
770 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
772 p_input->stream.pp_selected_es = realloc(
773 p_input->stream.pp_selected_es,
774 p_input->stream.i_selected_es_number
775 * sizeof(es_descriptor_t *) );
777 if( p_input->stream.pp_selected_es == NULL )
780 intf_DbgMsg( "No more selected ES in input_UnselectES" );