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.63 2001/10/03 15:10:55 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"
44 #include "input_ext-plugins.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_stream_id = 0;
61 /* initialized to 0 since we don't give the signal to the interface
62 * before the end of input initialization */
63 p_input->stream.b_changed = 0;
64 p_input->stream.pp_es = NULL;
65 p_input->stream.pp_selected_es = NULL;
66 p_input->stream.p_removed_es = NULL;
67 p_input->stream.p_newly_selected_es = NULL;
68 p_input->stream.pp_programs = NULL;
72 if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
74 intf_ErrMsg( "Unable to allocate memory in input_InitStream");
77 memset( p_input->stream.p_demux_data, 0, i_data_len );
81 p_input->stream.p_demux_data = NULL;
87 /*****************************************************************************
88 * input_EndStream: free all stream descriptors
89 *****************************************************************************/
90 void input_EndStream( input_thread_t * p_input )
92 /* Free all programs and associated ES, and associated decoders. */
93 while( p_input->stream.i_pgrm_number )
95 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
98 /* Free standalone ES */
99 while( p_input->stream.i_es_number )
101 input_DelES( p_input, p_input->stream.pp_es[0] );
105 while( p_input->stream.i_area_nb )
107 input_DelArea( p_input, p_input->stream.pp_areas[0] );
110 /* Free selected ES */
111 if( p_input->stream.pp_selected_es != NULL )
113 free( p_input->stream.pp_selected_es );
116 if( p_input->stream.p_demux_data != NULL )
118 free( p_input->stream.p_demux_data );
122 /*****************************************************************************
123 * input_FindProgram: returns a pointer to a program described by its ID
124 *****************************************************************************/
125 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
129 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
131 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
133 return p_input->stream.pp_programs[i];
140 /*****************************************************************************
141 * input_AddProgram: add and init a program descriptor
142 *****************************************************************************
143 * This program descriptor will be referenced in the given stream descriptor
144 *****************************************************************************/
145 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
146 u16 i_pgrm_id, size_t i_data_len )
148 /* Where to add the pgrm */
149 int i_pgrm_index = p_input->stream.i_pgrm_number;
151 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
153 /* Add an entry to the list of program associated with the stream */
154 p_input->stream.i_pgrm_number++;
155 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
156 p_input->stream.i_pgrm_number
157 * sizeof(pgrm_descriptor_t *) );
158 if( p_input->stream.pp_programs == NULL )
160 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
164 /* Allocate the structure to store this description */
165 p_input->stream.pp_programs[i_pgrm_index] =
166 malloc( sizeof(pgrm_descriptor_t) );
167 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
169 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
173 /* Init this entry */
174 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
175 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
176 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
178 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
179 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
181 input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
183 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
188 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
189 malloc( i_data_len );
190 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
192 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
195 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
200 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data = NULL;
203 return p_input->stream.pp_programs[i_pgrm_index];
206 /*****************************************************************************
207 * input_DelProgram: destroy a program descriptor
208 *****************************************************************************
209 * All ES descriptions referenced in the descriptor will be deleted.
210 *****************************************************************************/
211 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
217 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
219 /* Free the structures that describe the es that belongs to that program */
220 while( p_pgrm->i_es_number )
222 input_DelES( p_input, p_pgrm->pp_es[0] );
225 /* Free the demux data */
226 if( p_pgrm->p_demux_data != NULL )
228 free( p_pgrm->p_demux_data );
231 /* Find the program in the programs table */
232 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
235 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
239 /* Remove this program from the stream's list of programs */
240 p_input->stream.i_pgrm_number--;
242 p_input->stream.pp_programs[i_pgrm_index] =
243 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
244 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
245 p_input->stream.i_pgrm_number
246 * sizeof(pgrm_descriptor_t *) );
248 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
250 intf_ErrMsg( "input error: unable to realloc program list"
251 " in input_DelProgram" );
254 /* Free the description of this program */
258 /*****************************************************************************
259 * input_AddArea: add and init an area descriptor
260 *****************************************************************************
261 * This area descriptor will be referenced in the given stream descriptor
262 *****************************************************************************/
263 input_area_t * input_AddArea( input_thread_t * p_input )
265 /* Where to add the pgrm */
266 int i_area_index = p_input->stream.i_area_nb;
268 intf_DbgMsg("Adding description for area %d", i_area_index );
270 /* Add an entry to the list of program associated with the stream */
271 p_input->stream.i_area_nb++;
272 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
273 p_input->stream.i_area_nb
274 * sizeof(input_area_t *) );
275 if( p_input->stream.pp_areas == NULL )
277 intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
281 /* Allocate the structure to store this description */
282 p_input->stream.pp_areas[i_area_index] =
283 malloc( sizeof(input_area_t) );
284 if( p_input->stream.pp_areas[i_area_index] == NULL )
286 intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
290 /* Init this entry */
291 p_input->stream.pp_areas[i_area_index]->i_id = 0;
292 p_input->stream.pp_areas[i_area_index]->i_start = 0;
293 p_input->stream.pp_areas[i_area_index]->i_size = 0;
294 p_input->stream.pp_areas[i_area_index]->i_tell = 0;
295 p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
296 p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
297 p_input->stream.pp_areas[i_area_index]->i_part= 0;
298 p_input->stream.pp_areas[i_area_index]->i_angle_nb = 1;
299 p_input->stream.pp_areas[i_area_index]->i_angle = 0;
301 return p_input->stream.pp_areas[i_area_index];
304 /*****************************************************************************
305 * input_DelArea: destroy a area descriptor
306 *****************************************************************************
307 * All ES descriptions referenced in the descriptor will be deleted.
308 *****************************************************************************/
309 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
315 intf_DbgMsg("Deleting description for area %d", p_area->i_id );
317 /* Find the area in the areas table */
318 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
321 if( p_input->stream.pp_areas[i_area_index] == p_area )
325 /* Remove this area from the stream's list of areas */
326 p_input->stream.i_area_nb--;
328 p_input->stream.pp_areas[i_area_index] =
329 p_input->stream.pp_areas[p_input->stream.i_area_nb];
330 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
331 p_input->stream.i_area_nb
332 * sizeof(input_area_t *) );
334 if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
336 intf_ErrMsg( "input error: unable to realloc area list"
337 " in input_DelArea" );
340 /* Free the description of this area */
345 /*****************************************************************************
346 * input_FindES: returns a pointer to an ES described by its ID
347 *****************************************************************************/
348 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
352 for( i = 0; i < p_input->stream.i_es_number; i++ )
354 if( p_input->stream.pp_es[i]->i_id == i_es_id )
356 return p_input->stream.pp_es[i];
363 /*****************************************************************************
365 *****************************************************************************
366 * Reserve a slot in the table of ES descriptors for the ES and add it to the
367 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
369 *****************************************************************************/
370 es_descriptor_t * input_AddES( input_thread_t * p_input,
371 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
374 es_descriptor_t * p_es;
376 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
378 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
381 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
384 p_input->stream.i_es_number++;
385 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
386 p_input->stream.i_es_number
387 * sizeof(es_descriptor_t *) );
388 if( p_input->stream.pp_es == NULL )
390 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
393 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
395 /* Init its values */
396 p_es->i_id = i_es_id;
397 p_es->psz_desc[0] = '\0';
399 p_es->p_decoder_fifo = NULL;
401 p_es->i_cat = UNKNOWN_ES;
405 p_es->p_demux_data = malloc( i_data_len );
406 if( p_es->p_demux_data == NULL )
408 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
411 memset( p_es->p_demux_data, 0, i_data_len );
415 p_es->p_demux_data = NULL;
418 /* Add this ES to the program definition if one is given */
421 p_pgrm->i_es_number++;
422 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
424 * sizeof(es_descriptor_t *) );
425 if( p_pgrm->pp_es == NULL )
427 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
430 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
431 p_es->p_pgrm = p_pgrm;
441 /*****************************************************************************
443 *****************************************************************************/
444 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
446 int i_index, i_es_index;
447 pgrm_descriptor_t * p_pgrm;
450 p_pgrm = p_es->p_pgrm;
452 /* Kill associated decoder, if any. */
453 if( p_es->p_decoder_fifo != NULL )
455 input_EndDecoder( p_input, p_es );
458 /* Remove this ES from the description of the program if it is associated to
462 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
464 if( p_pgrm->pp_es[i_index] == p_es )
466 p_pgrm->i_es_number--;
467 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
468 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
470 * sizeof(es_descriptor_t *));
471 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
473 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
480 /* Free the demux data */
481 if( p_es->p_demux_data != NULL )
483 free( p_es->p_demux_data );
486 /* Find the ES in the ES table */
487 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
490 if( p_input->stream.pp_es[i_es_index] == p_es )
496 p_input->stream.i_es_number--;
497 p_input->stream.pp_es[i_es_index] =
498 p_input->stream.pp_es[p_input->stream.i_es_number];
499 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
500 p_input->stream.i_es_number
501 * sizeof(es_descriptor_t *));
502 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
504 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
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 = p_input->pf_init_bit_stream;
538 p_input->stream.i_selected_es_number++;
540 p_input->stream.pp_selected_es = realloc(
541 p_input->stream.pp_selected_es,
542 p_input->stream.i_selected_es_number
543 * sizeof(es_descriptor_t *) );
544 if( p_input->stream.pp_selected_es == NULL )
546 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
549 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
555 /*****************************************************************************
556 * GetVdecConfig: returns a valid vdec_config_t
557 *****************************************************************************/
558 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
559 es_descriptor_t * p_es )
561 vdec_config_t * p_config;
563 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
564 if( p_config == NULL )
566 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
569 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
578 /*****************************************************************************
579 * GetAdecConfig: returns a valid adec_config_t
580 *****************************************************************************/
581 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
582 es_descriptor_t * p_es )
584 adec_config_t * p_config;
586 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
587 if( p_config == NULL )
589 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
593 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
602 /*****************************************************************************
603 * input_SelectES: selects an ES and spawns the associated decoder
604 *****************************************************************************
605 * Remember we are still supposed to have stream_lock when entering this
607 *****************************************************************************/
609 vlc_thread_t adec_CreateThread( void * );
610 vlc_thread_t ac3dec_CreateThread( void * );
611 vlc_thread_t ac3spdif_CreateThread( void * );
612 vlc_thread_t spdif_CreateThread( void * );
613 vlc_thread_t vpar_CreateThread( void * );
614 vlc_thread_t spudec_CreateThread( void * );
615 vlc_thread_t lpcmdec_CreateThread( void * );
617 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
620 decoder_capabilities_t decoder;
625 intf_ErrMsg( "Nothing to do in input_SelectES" );
630 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
633 if( p_es->p_decoder_fifo != NULL )
635 intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
639 switch( p_es->i_type )
643 if( p_main->b_audio )
645 decoder.pf_create_thread = adec_CreateThread;
646 p_config = (void *)GetAdecConfig( p_input, p_es );
649 /* Release the lock, not to block the input thread during
650 * the creation of the thread. */
651 vlc_mutex_unlock( &p_input->stream.stream_lock );
652 p_es->thread_id = input_RunDecoder( &decoder, p_config );
653 vlc_mutex_lock( &p_input->stream.stream_lock );
659 if( p_main->b_video )
661 decoder.pf_create_thread = vpar_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 if( p_main->b_audio )
675 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) )
677 decoder.pf_create_thread = spdif_CreateThread;
681 decoder.pf_create_thread = ac3dec_CreateThread;
684 p_config = (void *)GetAdecConfig( p_input, p_es );
687 /* Release the lock, not to block the input thread during
688 * the creation of the thread. */
689 vlc_mutex_unlock( &p_input->stream.stream_lock );
690 p_es->thread_id = input_RunDecoder( &decoder, p_config );
691 vlc_mutex_lock( &p_input->stream.stream_lock );
695 if( p_main->b_audio )
697 decoder.pf_create_thread = lpcmdec_CreateThread;
698 p_config = (void *)GetAdecConfig( p_input, p_es );
701 /* Release the lock, not to block the input thread during
702 * the creation of the thread. */
703 vlc_mutex_unlock( &p_input->stream.stream_lock );
704 p_es->thread_id = input_RunDecoder( &decoder, p_config );
705 vlc_mutex_lock( &p_input->stream.stream_lock );
709 if( p_main->b_video )
711 decoder.pf_create_thread = spudec_CreateThread;
712 p_config = (void *)GetVdecConfig( p_input, p_es );
714 /* Release the lock, not to block the input thread during
715 * the creation of the thread. */
716 vlc_mutex_unlock( &p_input->stream.stream_lock );
717 p_es->thread_id = input_RunDecoder( &decoder, p_config );
718 vlc_mutex_lock( &p_input->stream.stream_lock );
723 intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
728 if( p_es->thread_id == 0 )
736 /*****************************************************************************
737 * input_UnselectES: removes an ES from the list of selected ES
738 *****************************************************************************/
739 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
746 intf_ErrMsg( "Nothing to do in input_UnselectES" );
751 intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
754 if( p_es->p_decoder_fifo == NULL )
756 intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
760 input_EndDecoder( p_input, p_es );
762 if( ( p_es->p_decoder_fifo == NULL ) &&
763 ( p_input->stream.i_selected_es_number > 0 ) )
765 p_input->stream.i_selected_es_number--;
767 while( ( i_index < p_input->stream.i_selected_es_number ) &&
768 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
773 p_input->stream.pp_selected_es[i_index] =
774 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
776 p_input->stream.pp_selected_es = realloc(
777 p_input->stream.pp_selected_es,
778 p_input->stream.i_selected_es_number
779 * sizeof(es_descriptor_t *) );
781 if( p_input->stream.pp_selected_es == NULL )
784 intf_DbgMsg( "No more selected ES in input_UnselectES" );