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.52 2001/04/28 03:36:25 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;
177 p_input->stream.pp_programs[i_pgrm_index]->p_aout
178 = p_input->p_default_aout;
182 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
183 malloc( i_data_len );
184 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
186 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
189 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
193 return p_input->stream.pp_programs[i_pgrm_index];
196 /*****************************************************************************
197 * input_DelProgram: destroy a program descriptor
198 *****************************************************************************
199 * All ES descriptions referenced in the descriptor will be deleted.
200 *****************************************************************************/
201 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
207 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
209 /* Free the structures that describe the es that belongs to that program */
210 while( p_pgrm->i_es_number )
212 input_DelES( p_input, p_pgrm->pp_es[0] );
215 /* Free the demux data */
216 if( p_pgrm->p_demux_data != NULL )
218 free( p_pgrm->p_demux_data );
221 /* Find the program in the programs table */
222 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
225 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
229 /* Remove this program from the stream's list of programs */
230 p_input->stream.i_pgrm_number--;
232 p_input->stream.pp_programs[i_pgrm_index] =
233 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
234 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
235 p_input->stream.i_pgrm_number
236 * sizeof(pgrm_descriptor_t *) );
238 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
240 intf_ErrMsg( "input error: unable to realloc program list"
241 " in input_DelProgram" );
244 /* Free the description of this program */
248 /*****************************************************************************
249 * input_AddArea: add and init an area descriptor
250 *****************************************************************************
251 * This area descriptor will be referenced in the given stream descriptor
252 *****************************************************************************/
253 input_area_t * input_AddArea( input_thread_t * p_input )
255 /* Where to add the pgrm */
256 int i_area_index = p_input->stream.i_area_nb;
258 intf_DbgMsg("Adding description for area %d", i_area_index );
260 /* Add an entry to the list of program associated with the stream */
261 p_input->stream.i_area_nb++;
262 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
263 p_input->stream.i_area_nb
264 * sizeof(input_area_t *) );
265 if( p_input->stream.pp_areas == NULL )
267 intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
271 /* Allocate the structure to store this description */
272 p_input->stream.pp_areas[i_area_index] =
273 malloc( sizeof(input_area_t) );
274 if( p_input->stream.pp_areas[i_area_index] == NULL )
276 intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
280 /* Init this entry */
281 p_input->stream.pp_areas[i_area_index]->i_id = 0;
282 p_input->stream.pp_areas[i_area_index]->i_start = 0;
283 p_input->stream.pp_areas[i_area_index]->i_size = 0;
284 p_input->stream.pp_areas[i_area_index]->i_tell = 0;
285 p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
286 p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
287 p_input->stream.pp_areas[i_area_index]->i_part= 0;
288 p_input->stream.pp_areas[i_area_index]->i_angle_nb = 1;
289 p_input->stream.pp_areas[i_area_index]->i_angle = 0;
291 return p_input->stream.pp_areas[i_area_index];
294 /*****************************************************************************
295 * input_DelArea: destroy a area descriptor
296 *****************************************************************************
297 * All ES descriptions referenced in the descriptor will be deleted.
298 *****************************************************************************/
299 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
305 intf_DbgMsg("Deleting description for area %d", p_area->i_id );
307 /* Find the area in the areas table */
308 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
311 if( p_input->stream.pp_areas[i_area_index] == p_area )
315 /* Remove this area from the stream's list of areas */
316 p_input->stream.i_area_nb--;
318 p_input->stream.pp_areas[i_area_index] =
319 p_input->stream.pp_areas[p_input->stream.i_area_nb];
320 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
321 p_input->stream.i_area_nb
322 * sizeof(input_area_t *) );
324 if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
326 intf_ErrMsg( "input error: unable to realloc area list"
327 " in input_DelArea" );
330 /* Free the description of this area */
335 /*****************************************************************************
336 * input_FindES: returns a pointer to an ES described by its ID
337 *****************************************************************************/
338 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
342 for( i = 0; i < p_input->stream.i_es_number; i++ )
344 if( p_input->stream.pp_es[i]->i_id == i_es_id )
346 return p_input->stream.pp_es[i];
353 /*****************************************************************************
355 *****************************************************************************
356 * Reserve a slot in the table of ES descriptors for the ES and add it to the
357 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
359 *****************************************************************************/
360 es_descriptor_t * input_AddES( input_thread_t * p_input,
361 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
364 es_descriptor_t * p_es;
366 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
368 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
371 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
374 p_input->stream.i_es_number++;
375 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
376 p_input->stream.i_es_number
377 * sizeof(es_descriptor_t *) );
378 if( p_input->stream.pp_es == NULL )
380 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
383 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
385 /* Init its values */
386 p_es->i_id = i_es_id;
387 p_es->psz_desc[0] = '\0';
389 p_es->p_decoder_fifo = NULL;
391 p_es->i_cat = UNKNOWN_ES;
395 p_es->p_demux_data = malloc( i_data_len );
396 if( p_es->p_demux_data == NULL )
398 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
401 memset( p_es->p_demux_data, 0, i_data_len );
405 p_es->p_demux_data = NULL;
408 /* Add this ES to the program definition if one is given */
411 p_pgrm->i_es_number++;
412 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
414 * sizeof(es_descriptor_t *) );
415 if( p_pgrm->pp_es == NULL )
417 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
420 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
421 p_es->p_pgrm = p_pgrm;
431 /*****************************************************************************
433 *****************************************************************************/
434 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
436 int i_index, i_es_index;
437 pgrm_descriptor_t * p_pgrm;
440 p_pgrm = p_es->p_pgrm;
442 /* Kill associated decoder, if any. */
443 if( p_es->p_decoder_fifo != NULL )
445 input_EndDecoder( p_input, p_es );
448 /* Remove this ES from the description of the program if it is associated to
452 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
454 if( p_pgrm->pp_es[i_index] == p_es )
456 p_pgrm->i_es_number--;
457 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
458 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
460 * sizeof(es_descriptor_t *));
461 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
463 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
470 /* Free the demux data */
471 if( p_es->p_demux_data != NULL )
473 free( p_es->p_demux_data );
476 /* Find the ES in the ES table */
477 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
480 if( p_input->stream.pp_es[i_es_index] == p_es )
486 p_input->stream.i_es_number--;
487 p_input->stream.pp_es[i_es_index] =
488 p_input->stream.pp_es[p_input->stream.i_es_number];
489 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
490 p_input->stream.i_es_number
491 * sizeof(es_descriptor_t *));
492 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
494 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
499 /*****************************************************************************
500 * InitDecConfig: initializes a decoder_config_t
501 *****************************************************************************/
502 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
503 decoder_config_t * p_config )
505 p_config->i_id = p_es->i_id;
506 p_config->i_type = p_es->i_type;
507 p_config->p_stream_ctrl =
508 &p_input->stream.control;
511 if( (p_config->p_decoder_fifo =
512 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
514 intf_ErrMsg( "Out of memory" );
518 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
519 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
520 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
521 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
522 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
523 p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
524 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
526 p_config->pf_init_bit_stream = InitBitstream;
528 p_input->stream.i_selected_es_number++;
530 p_input->stream.pp_selected_es = realloc(
531 p_input->stream.pp_selected_es,
532 p_input->stream.i_selected_es_number
533 * sizeof(es_descriptor_t *) );
534 if( p_input->stream.pp_selected_es == NULL )
536 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
539 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
545 /*****************************************************************************
546 * GetVdecConfig: returns a valid vdec_config_t
547 *****************************************************************************/
548 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
549 es_descriptor_t * p_es )
551 vdec_config_t * p_config;
553 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
554 if( p_config == NULL )
556 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
559 p_config->p_vout = p_input->p_default_vout;
560 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
569 /*****************************************************************************
570 * GetAdecConfig: returns a valid adec_config_t
571 *****************************************************************************/
572 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
573 es_descriptor_t * p_es )
575 adec_config_t * p_config;
577 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
578 if( p_config == NULL )
580 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
583 p_config->p_aout = p_input->p_default_aout;
584 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
593 /*****************************************************************************
594 * input_SelectES: selects an ES and spawns the associated decoder
595 *****************************************************************************
596 * Remember we are still supposed to have stream_lock when entering this
598 *****************************************************************************/
600 vlc_thread_t adec_CreateThread( void * );
601 vlc_thread_t ac3dec_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 decoder.pf_create_thread = ac3dec_CreateThread;
663 p_config = (void *)GetAdecConfig( p_input, p_es );
665 /* Release the lock, not to block the input thread during
666 * the creation of the thread. */
667 vlc_mutex_unlock( &p_input->stream.stream_lock );
668 p_es->thread_id = input_RunDecoder( &decoder, p_config );
669 vlc_mutex_lock( &p_input->stream.stream_lock );
674 if( p_main->b_video )
676 decoder.pf_create_thread = spudec_CreateThread;
677 p_config = (void *)GetVdecConfig( p_input, p_es );
679 /* Release the lock, not to block the input thread during
680 * the creation of the thread. */
681 vlc_mutex_unlock( &p_input->stream.stream_lock );
682 p_es->thread_id = input_RunDecoder( &decoder, p_config );
683 vlc_mutex_lock( &p_input->stream.stream_lock );
688 intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
693 if( p_es->thread_id == 0 )
701 /*****************************************************************************
702 * input_UnselectES: removes an ES from the list of selected ES
703 *****************************************************************************/
704 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
711 intf_ErrMsg( "Nothing to do in input_UnselectES" );
716 intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
719 if( p_es->p_decoder_fifo == NULL )
721 intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
725 input_EndDecoder( p_input, p_es );
727 if( ( p_es->p_decoder_fifo == NULL ) &&
728 ( p_input->stream.i_selected_es_number > 0 ) )
730 p_input->stream.i_selected_es_number--;
732 while( ( i_index < p_input->stream.i_selected_es_number ) &&
733 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
738 p_input->stream.pp_selected_es[i_index] =
739 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
741 p_input->stream.pp_selected_es = realloc(
742 p_input->stream.pp_selected_es,
743 p_input->stream.i_selected_es_number
744 * sizeof(es_descriptor_t *) );
746 if( p_input->stream.pp_selected_es == NULL )
749 intf_DbgMsg( "No more selected ES in input_UnselectES" );