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.44 2001/04/06 09:15:47 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() */
40 #include "stream_control.h"
41 #include "input_ext-intf.h"
42 #include "input_ext-dec.h"
45 #include "main.h" /* --noaudio --novideo */
48 * NOTICE : all of these functions expect you to have taken the lock on
49 * p_input->stream.lock
52 /*****************************************************************************
53 * input_InitStream: init the stream descriptor of the given input
54 *****************************************************************************/
55 int input_InitStream( input_thread_t * p_input, size_t i_data_len )
57 p_input->stream.i_stream_id = 0;
58 p_input->stream.pp_es = NULL;
59 p_input->stream.pp_selected_es = NULL;
60 p_input->stream.pp_programs = NULL;
64 if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
66 intf_ErrMsg( "Unable to allocate memory in input_InitStream");
69 memset( p_input->stream.p_demux_data, 0, i_data_len );
75 /*****************************************************************************
76 * input_EndStream: free all stream descriptors
77 *****************************************************************************/
78 void input_EndStream( input_thread_t * p_input )
80 /* Free all programs and associated ES, and associated decoders. */
81 while( p_input->stream.i_pgrm_number )
83 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
86 /* Free standalone ES */
87 while( p_input->stream.i_es_number )
89 input_DelES( p_input, p_input->stream.pp_es[0] );
92 if( p_input->stream.p_demux_data != NULL )
94 free( p_input->stream.p_demux_data );
98 /*****************************************************************************
99 * input_FindProgram: returns a pointer to a program described by its ID
100 *****************************************************************************/
101 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
105 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
107 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
109 return p_input->stream.pp_programs[i];
116 /*****************************************************************************
117 * input_AddProgram: add and init a program descriptor
118 *****************************************************************************
119 * This program descriptor will be referenced in the given stream descriptor
120 *****************************************************************************/
121 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
122 u16 i_pgrm_id, size_t i_data_len )
124 /* Where to add the pgrm */
125 int i_pgrm_index = p_input->stream.i_pgrm_number;
127 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
129 /* Add an entry to the list of program associated with the stream */
130 p_input->stream.i_pgrm_number++;
131 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
132 p_input->stream.i_pgrm_number
133 * sizeof(pgrm_descriptor_t *) );
134 if( p_input->stream.pp_programs == NULL )
136 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
140 /* Allocate the structure to store this description */
141 p_input->stream.pp_programs[i_pgrm_index] =
142 malloc( sizeof(pgrm_descriptor_t) );
143 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
145 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
149 /* Init this entry */
150 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
151 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
152 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
154 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
155 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
157 input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
159 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
162 p_input->stream.pp_programs[i_pgrm_index]->p_vout
163 = p_input->p_default_vout;
164 p_input->stream.pp_programs[i_pgrm_index]->p_aout
165 = p_input->p_default_aout;
169 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
170 malloc( i_data_len );
171 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
173 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
176 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
180 return p_input->stream.pp_programs[i_pgrm_index];
183 /*****************************************************************************
184 * input_DelProgram: destroy a program descriptor
185 *****************************************************************************
186 * All ES descriptions referenced in the descriptor will be deleted.
187 *****************************************************************************/
188 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
194 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
196 /* Free the structures that describe the es that belongs to that program */
197 while( p_pgrm->i_es_number )
199 input_DelES( p_input, p_pgrm->pp_es[0] );
202 /* Free the demux data */
203 if( p_pgrm->p_demux_data != NULL )
205 free( p_pgrm->p_demux_data );
208 /* Find the program in the programs table */
209 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
212 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
216 /* Remove this program from the stream's list of programs */
217 p_input->stream.i_pgrm_number--;
219 p_input->stream.pp_programs[i_pgrm_index] =
220 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
221 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
222 p_input->stream.i_pgrm_number
223 * sizeof(pgrm_descriptor_t *) );
225 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
227 intf_ErrMsg( "input error: unable to realloc program list"
228 " in input_DelProgram" );
231 /* Free the description of this program */
235 /*****************************************************************************
236 * input_AddArea: add and init an area descriptor
237 *****************************************************************************
238 * This area descriptor will be referenced in the given stream descriptor
239 *****************************************************************************/
240 input_area_t * input_AddArea( input_thread_t * p_input )
242 /* Where to add the pgrm */
243 int i_area_index = p_input->stream.i_area_nb;
245 intf_DbgMsg("Adding description for area %d", i_area_index );
247 /* Add an entry to the list of program associated with the stream */
248 p_input->stream.i_area_nb++;
249 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
250 p_input->stream.i_area_nb
251 * sizeof(input_area_t *) );
252 if( p_input->stream.pp_areas == NULL )
254 intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
258 /* Allocate the structure to store this description */
259 p_input->stream.pp_areas[i_area_index] =
260 malloc( sizeof(input_area_t) );
261 if( p_input->stream.pp_areas[i_area_index] == NULL )
263 intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
267 /* Init this entry */
268 p_input->stream.pp_areas[i_area_index]->i_id = 0;
269 p_input->stream.pp_areas[i_area_index]->i_start = 0;
270 p_input->stream.pp_areas[i_area_index]->i_size = 0;
271 p_input->stream.pp_areas[i_area_index]->i_tell = 0;
272 p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
273 p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
274 p_input->stream.pp_areas[i_area_index]->i_part= 0;
276 return p_input->stream.pp_areas[i_area_index];
279 /*****************************************************************************
280 * input_DelArea: destroy a area descriptor
281 *****************************************************************************
282 * All ES descriptions referenced in the descriptor will be deleted.
283 *****************************************************************************/
284 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
290 intf_DbgMsg("Deleting description for area %d", p_area->i_id );
292 /* Find the area in the areas table */
293 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
296 if( p_input->stream.pp_areas[i_area_index] == p_area )
300 /* Remove this area from the stream's list of areas */
301 p_input->stream.i_area_nb--;
303 p_input->stream.pp_areas[i_area_index] =
304 p_input->stream.pp_areas[p_input->stream.i_area_nb];
305 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
306 p_input->stream.i_area_nb
307 * sizeof(input_area_t *) );
309 if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
311 intf_ErrMsg( "input error: unable to realloc area list"
312 " in input_DelArea" );
315 /* Free the description of this area */
320 /*****************************************************************************
321 * input_FindES: returns a pointer to an ES described by its ID
322 *****************************************************************************/
323 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
327 for( i = 0; i < p_input->stream.i_es_number; i++ )
329 if( p_input->stream.pp_es[i]->i_id == i_es_id )
331 return p_input->stream.pp_es[i];
338 /*****************************************************************************
340 *****************************************************************************
341 * Reserve a slot in the table of ES descriptors for the ES and add it to the
342 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
344 *****************************************************************************/
345 es_descriptor_t * input_AddES( input_thread_t * p_input,
346 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
349 es_descriptor_t * p_es;
351 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
353 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
356 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
359 p_input->stream.i_es_number++;
360 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
361 p_input->stream.i_es_number
362 * sizeof(es_descriptor_t *) );
363 if( p_input->stream.pp_es == NULL )
365 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
368 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
370 /* Init its values */
371 p_es->i_id = i_es_id;
372 p_es->psz_desc[0] = '\0';
374 p_es->p_decoder_fifo = NULL;
380 p_es->p_demux_data = malloc( i_data_len );
381 if( p_es->p_demux_data == NULL )
383 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
386 memset( p_es->p_demux_data, 0, i_data_len );
390 p_es->p_demux_data = NULL;
393 /* Add this ES to the program definition if one is given */
396 p_pgrm->i_es_number++;
397 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
399 * sizeof(es_descriptor_t *) );
400 if( p_pgrm->pp_es == NULL )
402 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
405 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
406 p_es->p_pgrm = p_pgrm;
416 /*****************************************************************************
418 *****************************************************************************/
419 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
421 int i_index, i_es_index;
422 pgrm_descriptor_t * p_pgrm;
425 p_pgrm = p_es->p_pgrm;
427 /* Kill associated decoder, if any. */
428 if( p_es->p_decoder_fifo != NULL )
430 input_EndDecoder( p_input, p_es );
433 /* Remove this ES from the description of the program if it is associated to
437 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
439 if( p_pgrm->pp_es[i_index] == p_es )
441 p_pgrm->i_es_number--;
442 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
443 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
445 * sizeof(es_descriptor_t *));
446 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
448 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
455 /* Free the demux data */
456 if( p_es->p_demux_data != NULL )
458 free( p_es->p_demux_data );
461 /* Find the ES in the ES table */
462 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
465 if( p_input->stream.pp_es[i_es_index] == p_es )
471 p_input->stream.i_es_number--;
472 p_input->stream.pp_es[i_es_index] =
473 p_input->stream.pp_es[p_input->stream.i_es_number];
474 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
475 p_input->stream.i_es_number
476 * sizeof(es_descriptor_t *));
477 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
479 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
484 /*****************************************************************************
485 * InitDecConfig: initializes a decoder_config_t
486 *****************************************************************************/
487 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
488 decoder_config_t * p_config )
490 p_config->i_id = p_es->i_id;
491 p_config->i_type = p_es->i_type;
492 p_config->p_stream_ctrl =
493 &p_input->stream.control;
496 if( (p_config->p_decoder_fifo =
497 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
499 intf_ErrMsg( "Out of memory" );
503 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
504 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
505 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
506 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
507 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
508 p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
509 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
511 p_config->pf_init_bit_stream = InitBitstream;
513 p_input->stream.i_selected_es_number++;
515 p_input->stream.pp_selected_es = realloc(
516 p_input->stream.pp_selected_es,
517 p_input->stream.i_selected_es_number
518 * sizeof(es_descriptor_t *) );
519 if( p_input->stream.pp_selected_es == NULL )
521 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
524 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
530 /*****************************************************************************
531 * GetVdecConfig: returns a valid vdec_config_t
532 *****************************************************************************/
533 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
534 es_descriptor_t * p_es )
536 vdec_config_t * p_config;
538 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
539 if( p_config == NULL )
541 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
544 p_config->p_vout = p_input->p_default_vout;
545 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
554 /*****************************************************************************
555 * GetAdecConfig: returns a valid adec_config_t
556 *****************************************************************************/
557 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
558 es_descriptor_t * p_es )
560 adec_config_t * p_config;
562 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
563 if( p_config == NULL )
565 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
568 p_config->p_aout = p_input->p_default_aout;
569 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
578 /*****************************************************************************
579 * input_SelectES: selects an ES and spawns the associated decoder
580 *****************************************************************************
581 * Remember we are still supposed to have stream_lock when entering this
583 *****************************************************************************/
585 vlc_thread_t adec_CreateThread( void * );
586 vlc_thread_t ac3dec_CreateThread( void * );
587 vlc_thread_t vpar_CreateThread( void * );
588 vlc_thread_t spudec_CreateThread( void * );
590 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
593 decoder_capabilities_t decoder;
598 intf_ErrMsg( "Nothing to do in input_SelectES" );
603 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
606 if( p_es->p_decoder_fifo != NULL )
608 intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
612 switch( p_es->i_type )
616 if( p_main->b_audio )
618 decoder.pf_create_thread = adec_CreateThread;
619 p_config = (void *)GetAdecConfig( p_input, p_es );
621 /* Release the lock, not to block the input thread during
622 * the creation of the thread. */
623 vlc_mutex_unlock( &p_input->stream.stream_lock );
624 p_es->thread_id = input_RunDecoder( &decoder, p_config );
625 vlc_mutex_lock( &p_input->stream.stream_lock );
631 if( p_main->b_video )
633 decoder.pf_create_thread = vpar_CreateThread;
634 p_config = (void *)GetVdecConfig( 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 );
645 if( p_main->b_audio )
647 decoder.pf_create_thread = ac3dec_CreateThread;
648 p_config = (void *)GetAdecConfig( p_input, p_es );
650 /* Release the lock, not to block the input thread during
651 * the creation of the thread. */
652 vlc_mutex_unlock( &p_input->stream.stream_lock );
653 p_es->thread_id = input_RunDecoder( &decoder, p_config );
654 vlc_mutex_lock( &p_input->stream.stream_lock );
659 if( p_main->b_video )
661 decoder.pf_create_thread = spudec_CreateThread;
662 p_config = (void *)GetVdecConfig( p_input, p_es );
664 /* Release the lock, not to block the input thread during
665 * the creation of the thread. */
666 vlc_mutex_unlock( &p_input->stream.stream_lock );
667 p_es->thread_id = input_RunDecoder( &decoder, p_config );
668 vlc_mutex_lock( &p_input->stream.stream_lock );
673 intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
678 if( p_es->thread_id == 0 )
686 /*****************************************************************************
687 * input_UnselectES: removes an ES from the list of selected ES
688 *****************************************************************************/
689 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
696 intf_ErrMsg( "Nothing to do in input_UnselectES" );
701 intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
704 if( p_es->p_decoder_fifo == NULL )
706 intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
710 input_EndDecoder( p_input, p_es );
712 if( ( p_es->p_decoder_fifo == NULL ) &&
713 ( p_input->stream.i_selected_es_number > 0 ) )
715 p_input->stream.i_selected_es_number--;
717 while( ( i_index < p_input->stream.i_selected_es_number ) &&
718 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
723 p_input->stream.pp_selected_es[i_index] =
724 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
726 p_input->stream.pp_selected_es = realloc(
727 p_input->stream.pp_selected_es,
728 p_input->stream.i_selected_es_number
729 * sizeof(es_descriptor_t *) );
731 if( p_input->stream.pp_selected_es == NULL )
733 intf_ErrMsg( "No more selected ES in input_UnselectES" );