1 /*****************************************************************************
2 * input_ctrl.c: Decoder control
3 * Controls the extraction and the decoding of the programs elements carried
5 *****************************************************************************
6 * Copyright (C) 1999, 2000 VideoLAN
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <sys/types.h> /* on BSD, uio.h needs types.h */
30 #include <sys/uio.h> /* "input.h" */
32 #include <netinet/in.h> /* ntohs */
42 #include "input_netlist.h"
43 #include "decoder_fifo.h"
45 #include "audio_output.h" /* aout_thread_t */
47 #include "audio_decoder.h" /* adec_thread_t */
49 #include "ac3_decoder.h" /* ac3dec_t (for ac3_decoder_thread.h) */
50 #include "ac3_decoder_thread.h" /* ac3dec_thread_t */
52 #include "video.h" /* picture_t (for video_output.h) */
53 #include "video_output.h" /* vout_thread_t */
55 #include "vdec_idct.h" /* dctelem_t (for video_parser.h) */
56 #include "vdec_motion.h" /* f_motion_t (for video_parser.h) */
57 #include "vpar_blocks.h" /* macroblock_t (for video_parser.h) */
58 #include "vpar_headers.h" /* sequence_t (for video_parser.h) */
59 #include "vpar_synchro.h" /* video_synchro_t (for video_parser.h) */
60 #include "video_parser.h" /* vpar_thread_t */
62 #include "spu_decoder.h" /* spudec_thread_t */
64 /*****************************************************************************
65 * input_AddPgrmElem: Start the extraction and the decoding of a program element
66 *****************************************************************************
67 * Add the element given by its PID in the list of PID to extract and spawn
68 * the decoding thread.
69 * This function only modifies the table of selected es, but must NOT modify
70 * the table of ES itself.
71 *****************************************************************************/
72 int input_AddPgrmElem( input_thread_t *p_input, int i_current_id )
74 int i_es_loop, i_selected_es_loop;
76 /* Since this function is intended to be called by interface, lock the
77 * elementary stream structure. */
78 vlc_mutex_lock( &p_input->es_lock );
80 /* Find out which PID we need. */
81 for( i_es_loop = 0; i_es_loop < INPUT_MAX_ES; i_es_loop++ )
83 if( p_input->p_es[i_es_loop].i_id == i_current_id )
85 if( p_input->p_es[i_es_loop].p_dec != NULL )
87 /* We already have a decoder for that PID. */
88 vlc_mutex_unlock( &p_input->es_lock );
89 intf_ErrMsg("input error: PID %d already selected\n",
94 intf_DbgMsg("Requesting selection of PID %d\n",
97 /* Find a free spot in pp_selected_es. */
98 for( i_selected_es_loop = 0; p_input->pp_selected_es[i_selected_es_loop] != NULL
99 && i_selected_es_loop < INPUT_MAX_SELECTED_ES; i_selected_es_loop++ );
101 if( i_selected_es_loop == INPUT_MAX_SELECTED_ES )
104 vlc_mutex_unlock( &p_input->es_lock );
105 intf_ErrMsg("input error: MAX_SELECTED_ES reached: try increasing it in config.h\n");
109 /* Don't decode PSI streams ! */
110 if( p_input->p_es[i_es_loop].b_psi )
112 intf_ErrMsg("input_error: trying to decode PID %d which is the one of a PSI\n", i_current_id);
113 vlc_mutex_unlock( &p_input->es_lock );
118 /* Spawn the decoder. */
119 switch( p_input->p_es[i_es_loop].i_type )
122 /* Spawn ac3 thread */
123 if ( ((ac3dec_thread_t *)(p_input->p_es[i_es_loop].p_dec) =
124 ac3dec_CreateThread(p_input)) == NULL )
126 intf_ErrMsg( "Could not start ac3 decoder\n" );
127 vlc_mutex_unlock( &p_input->es_lock );
133 /* Spawn spu thread */
134 if ( ((spudec_thread_t *)(p_input->p_es[i_es_loop].p_dec) =
135 spudec_CreateThread(p_input)) == NULL )
137 intf_ErrMsg( "Could not start spu decoder\n" );
138 vlc_mutex_unlock( &p_input->es_lock );
145 /* Spawn audio thread. */
146 if( ((adec_thread_t*)(p_input->p_es[i_es_loop].p_dec) =
147 adec_CreateThread( p_input )) == NULL )
149 intf_ErrMsg("Could not start audio decoder\n");
150 vlc_mutex_unlock( &p_input->es_lock );
157 /* Spawn video thread. */
159 if( ((vdec_thread_t*)(p_input->p_es[i_es_loop].p_dec) =
160 vdec_CreateThread( p_input )) == NULL )
162 if( ((vpar_thread_t*)(p_input->p_es[i_es_loop].p_dec) =
163 vpar_CreateThread( p_input )) == NULL )
167 intf_ErrMsg("Could not start video decoder\n");
169 intf_ErrMsg("Could not start video parser\n");
171 vlc_mutex_unlock( &p_input->es_lock );
177 /* That should never happen. */
178 intf_DbgMsg("input error: unknown stream type (0x%.2x)\n",
179 p_input->p_es[i_es_loop].i_type);
180 vlc_mutex_unlock( &p_input->es_lock );
185 /* Initialise the demux */
186 p_input->p_es[i_es_loop].p_pes_packet = NULL;
187 p_input->p_es[i_es_loop].i_continuity_counter = 0xff;
188 p_input->p_es[i_es_loop].b_random = 0;
190 /* Mark stream to be demultiplexed. */
191 intf_DbgMsg("Stream %d added in %d\n", i_current_id, i_selected_es_loop);
192 p_input->pp_selected_es[i_selected_es_loop] = &p_input->p_es[i_es_loop];
193 vlc_mutex_unlock( &p_input->es_lock );
199 /* We haven't found this PID in the current stream. */
200 vlc_mutex_unlock( &p_input->es_lock );
201 intf_ErrMsg("input error: can't find PID %d\n", i_current_id);
205 /*****************************************************************************
206 * input_DelPgrmElem: Stop the decoding of a program element
207 *****************************************************************************
208 * Stop the extraction of the element given by its PID and kill the associated
210 * This function only modifies the table of selected es, but must NOT modify
211 * the table of ES itself.
212 *****************************************************************************/
213 int input_DelPgrmElem( input_thread_t *p_input, int i_current_id )
215 int i_selected_es_loop, i_last_selected;
217 /* Since this function is intended to be called by interface, lock the
219 vlc_mutex_lock( &p_input->es_lock );
221 /* Find out which PID we need. */
222 for( i_selected_es_loop = 0; i_selected_es_loop < INPUT_MAX_SELECTED_ES;
223 i_selected_es_loop++ )
225 if( p_input->pp_selected_es[i_selected_es_loop] )
227 if( p_input->pp_selected_es[i_selected_es_loop]->i_id == i_current_id )
229 if( !(p_input->pp_selected_es[i_selected_es_loop]->p_dec) )
231 /* We don't have a decoder for that PID. */
232 vlc_mutex_unlock( &p_input->es_lock );
233 intf_ErrMsg("input error: PID %d already deselected\n",
238 intf_DbgMsg("input debug: requesting termination of PID %d\n",
241 /* Cancel the decoder. */
242 switch( p_input->pp_selected_es[i_selected_es_loop]->i_type )
245 ac3dec_DestroyThread( (ac3dec_thread_t *)(p_input->pp_selected_es[i_selected_es_loop]->p_dec) );
249 spudec_DestroyThread( (spudec_thread_t *)(p_input->pp_selected_es[i_selected_es_loop]->p_dec) );
254 adec_DestroyThread( (adec_thread_t*)(p_input->pp_selected_es[i_selected_es_loop]->p_dec) );
260 vdec_DestroyThread( (vdec_thread_t*)(p_input->pp_selected_es[i_selected_es_loop]->p_dec) /*, NULL */ );
262 vpar_DestroyThread( (vpar_thread_t*)(p_input->pp_selected_es[i_selected_es_loop]->p_dec) /*, NULL */ );
268 p_input->pp_selected_es[i_selected_es_loop]->p_dec = NULL;
270 /* Find last selected stream. */
271 for( i_last_selected = i_selected_es_loop;
272 p_input->pp_selected_es[i_last_selected]
273 && i_last_selected < INPUT_MAX_SELECTED_ES;
276 /* Exchange streams. */
277 p_input->pp_selected_es[i_selected_es_loop] =
278 p_input->pp_selected_es[i_last_selected];
279 p_input->pp_selected_es[i_last_selected] = NULL;
281 vlc_mutex_unlock( &p_input->es_lock );
287 /* We haven't found this PID in the current stream. */
288 vlc_mutex_unlock( &p_input->es_lock );
289 intf_ErrMsg("input error: can't find PID %d\n", i_current_id);
295 /*****************************************************************************
296 * input_IsElemRecv: Test if an element given by its PID is currently received
297 *****************************************************************************
298 * Cannot return the position of the es in the pp_selected_es, for it can
299 * change once we have released the lock
300 *****************************************************************************/
301 boolean_t input_IsElemRecv( input_thread_t *p_input, int i_id )
303 boolean_t b_is_recv = 0;
306 /* Since this function is intended to be called by interface, lock the
308 vlc_mutex_lock( &p_input->es_lock );
311 while( i_index < INPUT_MAX_SELECTED_ES && !p_input->pp_selected_es[i_index] )
313 if( p_input->pp_selected_es[i_index]->i_id == i_id )
320 /* Unlock the structure */
321 vlc_mutex_unlock( &p_input->es_lock );