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.49 2001/04/13 05:36:12 stef 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 )
58 p_input->stream.i_method = INPUT_METHOD_NONE;
59 p_input->stream.i_stream_id = 0;
60 p_input->stream.b_changed = 1;
61 p_input->stream.pp_es = NULL;
62 p_input->stream.pp_selected_es = NULL;
63 p_input->stream.pp_programs = NULL;
67 if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
69 intf_ErrMsg( "Unable to allocate memory in input_InitStream");
72 memset( p_input->stream.p_demux_data, 0, i_data_len );
78 /*****************************************************************************
79 * input_EndStream: free all stream descriptors
80 *****************************************************************************/
81 void input_EndStream( input_thread_t * p_input )
83 /* Free all programs and associated ES, and associated decoders. */
84 while( p_input->stream.i_pgrm_number )
86 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
89 /* Free standalone ES */
90 while( p_input->stream.i_es_number )
92 input_DelES( p_input, p_input->stream.pp_es[0] );
96 while( p_input->stream.i_area_nb )
98 input_DelArea( p_input, p_input->stream.pp_areas[0] );
101 if( p_input->stream.p_demux_data != NULL )
103 free( p_input->stream.p_demux_data );
107 /*****************************************************************************
108 * input_FindProgram: returns a pointer to a program described by its ID
109 *****************************************************************************/
110 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
114 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
116 if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
118 return p_input->stream.pp_programs[i];
125 /*****************************************************************************
126 * input_AddProgram: add and init a program descriptor
127 *****************************************************************************
128 * This program descriptor will be referenced in the given stream descriptor
129 *****************************************************************************/
130 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
131 u16 i_pgrm_id, size_t i_data_len )
133 /* Where to add the pgrm */
134 int i_pgrm_index = p_input->stream.i_pgrm_number;
136 intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
138 /* Add an entry to the list of program associated with the stream */
139 p_input->stream.i_pgrm_number++;
140 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
141 p_input->stream.i_pgrm_number
142 * sizeof(pgrm_descriptor_t *) );
143 if( p_input->stream.pp_programs == NULL )
145 intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
149 /* Allocate the structure to store this description */
150 p_input->stream.pp_programs[i_pgrm_index] =
151 malloc( sizeof(pgrm_descriptor_t) );
152 if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
154 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
158 /* Init this entry */
159 p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
160 p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
161 p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
163 p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
164 p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
166 input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
168 p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
171 p_input->stream.pp_programs[i_pgrm_index]->p_vout
172 = p_input->p_default_vout;
173 p_input->stream.pp_programs[i_pgrm_index]->p_aout
174 = p_input->p_default_aout;
178 p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
179 malloc( i_data_len );
180 if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
182 intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
185 memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
189 return p_input->stream.pp_programs[i_pgrm_index];
192 /*****************************************************************************
193 * input_DelProgram: destroy a program descriptor
194 *****************************************************************************
195 * All ES descriptions referenced in the descriptor will be deleted.
196 *****************************************************************************/
197 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
203 intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
205 /* Free the structures that describe the es that belongs to that program */
206 while( p_pgrm->i_es_number )
208 input_DelES( p_input, p_pgrm->pp_es[0] );
211 /* Free the demux data */
212 if( p_pgrm->p_demux_data != NULL )
214 free( p_pgrm->p_demux_data );
217 /* Find the program in the programs table */
218 for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
221 if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
225 /* Remove this program from the stream's list of programs */
226 p_input->stream.i_pgrm_number--;
228 p_input->stream.pp_programs[i_pgrm_index] =
229 p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
230 p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
231 p_input->stream.i_pgrm_number
232 * sizeof(pgrm_descriptor_t *) );
234 if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
236 intf_ErrMsg( "input error: unable to realloc program list"
237 " in input_DelProgram" );
240 /* Free the description of this program */
244 /*****************************************************************************
245 * input_AddArea: add and init an area descriptor
246 *****************************************************************************
247 * This area descriptor will be referenced in the given stream descriptor
248 *****************************************************************************/
249 input_area_t * input_AddArea( input_thread_t * p_input )
251 /* Where to add the pgrm */
252 int i_area_index = p_input->stream.i_area_nb;
254 intf_DbgMsg("Adding description for area %d", i_area_index );
256 /* Add an entry to the list of program associated with the stream */
257 p_input->stream.i_area_nb++;
258 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
259 p_input->stream.i_area_nb
260 * sizeof(input_area_t *) );
261 if( p_input->stream.pp_areas == NULL )
263 intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
267 /* Allocate the structure to store this description */
268 p_input->stream.pp_areas[i_area_index] =
269 malloc( sizeof(input_area_t) );
270 if( p_input->stream.pp_areas[i_area_index] == NULL )
272 intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
276 /* Init this entry */
277 p_input->stream.pp_areas[i_area_index]->i_id = 0;
278 p_input->stream.pp_areas[i_area_index]->i_start = 0;
279 p_input->stream.pp_areas[i_area_index]->i_size = 0;
280 p_input->stream.pp_areas[i_area_index]->i_tell = 0;
281 p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
282 p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
283 p_input->stream.pp_areas[i_area_index]->i_part= 0;
285 return p_input->stream.pp_areas[i_area_index];
288 /*****************************************************************************
289 * input_DelArea: destroy a area descriptor
290 *****************************************************************************
291 * All ES descriptions referenced in the descriptor will be deleted.
292 *****************************************************************************/
293 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
299 intf_DbgMsg("Deleting description for area %d", p_area->i_id );
301 /* Find the area in the areas table */
302 for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
305 if( p_input->stream.pp_areas[i_area_index] == p_area )
309 /* Remove this area from the stream's list of areas */
310 p_input->stream.i_area_nb--;
312 p_input->stream.pp_areas[i_area_index] =
313 p_input->stream.pp_areas[p_input->stream.i_area_nb];
314 p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
315 p_input->stream.i_area_nb
316 * sizeof(input_area_t *) );
318 if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
320 intf_ErrMsg( "input error: unable to realloc area list"
321 " in input_DelArea" );
324 /* Free the description of this area */
329 /*****************************************************************************
330 * input_FindES: returns a pointer to an ES described by its ID
331 *****************************************************************************/
332 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
336 for( i = 0; i < p_input->stream.i_es_number; i++ )
338 if( p_input->stream.pp_es[i]->i_id == i_es_id )
340 return p_input->stream.pp_es[i];
347 /*****************************************************************************
349 *****************************************************************************
350 * Reserve a slot in the table of ES descriptors for the ES and add it to the
351 * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
353 *****************************************************************************/
354 es_descriptor_t * input_AddES( input_thread_t * p_input,
355 pgrm_descriptor_t * p_pgrm, u16 i_es_id,
358 es_descriptor_t * p_es;
360 intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
362 p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
365 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
368 p_input->stream.i_es_number++;
369 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
370 p_input->stream.i_es_number
371 * sizeof(es_descriptor_t *) );
372 if( p_input->stream.pp_es == NULL )
374 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
377 p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
379 /* Init its values */
380 p_es->i_id = i_es_id;
381 p_es->psz_desc[0] = '\0';
383 p_es->p_decoder_fifo = NULL;
385 p_es->i_cat = UNKNOWN_ES;
389 p_es->p_demux_data = malloc( i_data_len );
390 if( p_es->p_demux_data == NULL )
392 intf_ErrMsg( "Unable to allocate memory in input_AddES" );
395 memset( p_es->p_demux_data, 0, i_data_len );
399 p_es->p_demux_data = NULL;
402 /* Add this ES to the program definition if one is given */
405 p_pgrm->i_es_number++;
406 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
408 * sizeof(es_descriptor_t *) );
409 if( p_pgrm->pp_es == NULL )
411 intf_ErrMsg( "Unable to realloc memory in input_AddES" );
414 p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
415 p_es->p_pgrm = p_pgrm;
425 /*****************************************************************************
427 *****************************************************************************/
428 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
430 int i_index, i_es_index;
431 pgrm_descriptor_t * p_pgrm;
434 p_pgrm = p_es->p_pgrm;
436 /* Kill associated decoder, if any. */
437 if( p_es->p_decoder_fifo != NULL )
439 input_EndDecoder( p_input, p_es );
442 /* Remove this ES from the description of the program if it is associated to
446 for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
448 if( p_pgrm->pp_es[i_index] == p_es )
450 p_pgrm->i_es_number--;
451 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
452 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
454 * sizeof(es_descriptor_t *));
455 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
457 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
464 /* Free the demux data */
465 if( p_es->p_demux_data != NULL )
467 free( p_es->p_demux_data );
470 /* Find the ES in the ES table */
471 for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
474 if( p_input->stream.pp_es[i_es_index] == p_es )
480 p_input->stream.i_es_number--;
481 p_input->stream.pp_es[i_es_index] =
482 p_input->stream.pp_es[p_input->stream.i_es_number];
483 p_input->stream.pp_es = realloc( p_input->stream.pp_es,
484 p_input->stream.i_es_number
485 * sizeof(es_descriptor_t *));
486 if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
488 intf_ErrMsg( "Unable to realloc memory in input_DelES" );
493 /*****************************************************************************
494 * InitDecConfig: initializes a decoder_config_t
495 *****************************************************************************/
496 static int InitDecConfig( input_thread_t * p_input, es_descriptor_t * p_es,
497 decoder_config_t * p_config )
499 p_config->i_id = p_es->i_id;
500 p_config->i_type = p_es->i_type;
501 p_config->p_stream_ctrl =
502 &p_input->stream.control;
505 if( (p_config->p_decoder_fifo =
506 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
508 intf_ErrMsg( "Out of memory" );
512 vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
513 vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
514 p_config->p_decoder_fifo->i_start = p_config->p_decoder_fifo->i_end = 0;
515 p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
516 p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
517 p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
518 p_es->p_decoder_fifo = p_config->p_decoder_fifo;
520 p_config->pf_init_bit_stream = InitBitstream;
522 p_input->stream.i_selected_es_number++;
524 p_input->stream.pp_selected_es = realloc(
525 p_input->stream.pp_selected_es,
526 p_input->stream.i_selected_es_number
527 * sizeof(es_descriptor_t *) );
528 if( p_input->stream.pp_selected_es == NULL )
530 intf_ErrMsg( "Unable to realloc memory in input_SelectES" );
533 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
539 /*****************************************************************************
540 * GetVdecConfig: returns a valid vdec_config_t
541 *****************************************************************************/
542 static vdec_config_t * GetVdecConfig( input_thread_t * p_input,
543 es_descriptor_t * p_es )
545 vdec_config_t * p_config;
547 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
548 if( p_config == NULL )
550 intf_ErrMsg( "Unable to allocate memory in GetVdecConfig" );
553 p_config->p_vout = p_input->p_default_vout;
554 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
563 /*****************************************************************************
564 * GetAdecConfig: returns a valid adec_config_t
565 *****************************************************************************/
566 static adec_config_t * GetAdecConfig( input_thread_t * p_input,
567 es_descriptor_t * p_es )
569 adec_config_t * p_config;
571 p_config = (adec_config_t *)malloc( sizeof(adec_config_t));
572 if( p_config == NULL )
574 intf_ErrMsg( "Unable to allocate memory in GetAdecConfig" );
577 p_config->p_aout = p_input->p_default_aout;
578 if( InitDecConfig( p_input, p_es, &p_config->decoder_config ) == -1 )
587 /*****************************************************************************
588 * input_SelectES: selects an ES and spawns the associated decoder
589 *****************************************************************************
590 * Remember we are still supposed to have stream_lock when entering this
592 *****************************************************************************/
594 vlc_thread_t adec_CreateThread( void * );
595 vlc_thread_t ac3dec_CreateThread( void * );
596 vlc_thread_t vpar_CreateThread( void * );
597 vlc_thread_t spudec_CreateThread( void * );
599 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
602 decoder_capabilities_t decoder;
607 intf_ErrMsg( "Nothing to do in input_SelectES" );
612 intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
615 if( p_es->p_decoder_fifo != NULL )
617 intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
621 switch( p_es->i_type )
625 if( p_main->b_audio )
627 decoder.pf_create_thread = adec_CreateThread;
628 p_config = (void *)GetAdecConfig( p_input, p_es );
630 /* Release the lock, not to block the input thread during
631 * the creation of the thread. */
632 vlc_mutex_unlock( &p_input->stream.stream_lock );
633 p_es->thread_id = input_RunDecoder( &decoder, p_config );
634 vlc_mutex_lock( &p_input->stream.stream_lock );
640 if( p_main->b_video )
642 decoder.pf_create_thread = vpar_CreateThread;
643 p_config = (void *)GetVdecConfig( p_input, p_es );
645 /* Release the lock, not to block the input thread during
646 * the creation of the thread. */
647 vlc_mutex_unlock( &p_input->stream.stream_lock );
648 p_es->thread_id = input_RunDecoder( &decoder, p_config );
649 vlc_mutex_lock( &p_input->stream.stream_lock );
654 if( p_main->b_audio )
656 decoder.pf_create_thread = ac3dec_CreateThread;
657 p_config = (void *)GetAdecConfig( p_input, p_es );
659 /* Release the lock, not to block the input thread during
660 * the creation of the thread. */
661 vlc_mutex_unlock( &p_input->stream.stream_lock );
662 p_es->thread_id = input_RunDecoder( &decoder, p_config );
663 vlc_mutex_lock( &p_input->stream.stream_lock );
668 if( p_main->b_video )
670 decoder.pf_create_thread = spudec_CreateThread;
671 p_config = (void *)GetVdecConfig( 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 intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
687 if( p_es->thread_id == 0 )
695 /*****************************************************************************
696 * input_UnselectES: removes an ES from the list of selected ES
697 *****************************************************************************/
698 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
705 intf_ErrMsg( "Nothing to do in input_UnselectES" );
710 intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
713 if( p_es->p_decoder_fifo == NULL )
715 intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
719 input_EndDecoder( p_input, p_es );
721 if( ( p_es->p_decoder_fifo == NULL ) &&
722 ( p_input->stream.i_selected_es_number > 0 ) )
724 p_input->stream.i_selected_es_number--;
726 while( ( i_index < p_input->stream.i_selected_es_number ) &&
727 ( p_input->stream.pp_selected_es[i_index] != p_es ) )
732 p_input->stream.pp_selected_es[i_index] =
733 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
735 p_input->stream.pp_selected_es = realloc(
736 p_input->stream.pp_selected_es,
737 p_input->stream.i_selected_es_number
738 * sizeof(es_descriptor_t *) );
740 if( p_input->stream.pp_selected_es == NULL )
743 intf_DbgMsg( "No more selected ES in input_UnselectES" );