1 /*****************************************************************************
2 * input_dec.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: input_dec.c,v 1.56 2003/01/22 10:44:50 fenrir 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 *****************************************************************************/
28 #include <string.h> /* memcpy(), memset() */
32 #include "stream_control.h"
33 #include "input_ext-dec.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-plugins.h"
37 static decoder_fifo_t * CreateDecoderFifo( input_thread_t *,
39 static void DeleteDecoderFifo( decoder_fifo_t * );
41 /*****************************************************************************
42 * input_RunDecoder: spawns a new decoder thread
43 *****************************************************************************/
44 decoder_fifo_t * input_RunDecoder( input_thread_t * p_input,
45 es_descriptor_t * p_es )
48 decoder_fifo_t *p_fifo;
51 /* Create the decoder configuration structure */
52 p_fifo = CreateDecoderFifo( p_input, p_es );
56 msg_Err( p_input, "could not create decoder fifo" );
60 p_fifo->p_module = NULL;
61 /* If we are in sout mode, search first for packetizer module then
62 * codec to do transcoding */
63 psz_sout = config_GetPsz( p_input, "sout" );
64 if( psz_sout != NULL && *psz_sout != 0 )
66 vlc_bool_t b_sout = VLC_TRUE;
68 if( p_es->i_cat == AUDIO_ES )
70 b_sout = config_GetInt( p_input, "sout-audio" );
72 else if( p_es->i_cat == VIDEO_ES )
74 b_sout = config_GetInt( p_input, "sout-video" );
79 vlc_bool_t b_reencode = VLC_FALSE;
81 if( p_es->i_cat == AUDIO_ES )
83 char *psz_sout_acodec = config_GetPsz( p_input, "sout-acodec" );
84 if( psz_sout_acodec != NULL && *psz_sout_acodec != '\0' )
86 msg_Dbg( p_input, "audio reencoding requested -> unsupported" );
87 b_reencode = VLC_TRUE;
90 else if( p_es->i_cat == VIDEO_ES )
92 char *psz_sout_vcodec = config_GetPsz( p_input, "sout-vcodec" );
93 if( psz_sout_vcodec != NULL && *psz_sout_vcodec != '\0' )
95 msg_Dbg( p_input, "video reencoding requested" );
96 /* force encoder video output */
97 config_PutPsz( p_input, "vout", "encoder" );
98 b_reencode = VLC_TRUE;
104 /* we don't want to reencode so search for a packetizer */
106 module_Need( p_fifo, "packetizer", "$packetizer" );
110 /* get a suitable decoder module to do reencoding*/
111 p_fifo->p_module = module_Need( p_fifo, "decoder", "$codec" );
117 /* default Get a suitable decoder module */
118 p_fifo->p_module = module_Need( p_fifo, "decoder", "$codec" );
121 if( p_fifo->p_module == NULL )
123 msg_Err( p_fifo, "no suitable decoder module for fourcc `%4.4s'",
124 (char*)&p_fifo->i_fourcc );
125 DeleteDecoderFifo( p_fifo );
126 vlc_object_destroy( p_fifo );
130 if ( p_es->i_cat == AUDIO_ES )
132 i_priority = VLC_THREAD_PRIORITY_AUDIO;
136 i_priority = VLC_THREAD_PRIORITY_VIDEO;
139 /* Spawn the decoder thread */
140 if( vlc_thread_create( p_fifo, "decoder", p_fifo->pf_run,
141 i_priority, VLC_FALSE ) )
143 msg_Err( p_fifo, "cannot spawn decoder thread \"%s\"",
144 p_fifo->p_module->psz_object_name );
145 module_Unneed( p_fifo, p_fifo->p_module );
149 p_input->stream.b_changed = 1;
155 /*****************************************************************************
156 * input_EndDecoder: kills a decoder thread and waits until it's finished
157 *****************************************************************************/
158 void input_EndDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
162 p_es->p_decoder_fifo->b_die = 1;
164 /* Make sure the thread leaves the NextDataPacket() function by
165 * sending it a few null packets. */
166 for( i_dummy = 0; i_dummy < PADDING_PACKET_NUMBER; i_dummy++ )
168 input_NullPacket( p_input, p_es );
171 if( p_es->p_pes != NULL )
173 input_DecodePES( p_es->p_decoder_fifo, p_es->p_pes );
176 /* Waiting for the thread to exit */
177 /* I thought that unlocking was better since thread join can be long
178 * but it actually creates late pictures and freezes --stef */
179 /* vlc_mutex_unlock( &p_input->stream.stream_lock ); */
180 vlc_thread_join( p_es->p_decoder_fifo );
181 /* vlc_mutex_lock( &p_input->stream.stream_lock ); */
183 /* Delete decoder configuration */
184 DeleteDecoderFifo( p_es->p_decoder_fifo );
187 module_Unneed( p_es->p_decoder_fifo, p_es->p_decoder_fifo->p_module );
189 /* Delete the fifo */
190 vlc_object_destroy( p_es->p_decoder_fifo );
192 /* Tell the input there is no more decoder */
193 p_es->p_decoder_fifo = NULL;
195 p_input->stream.b_changed = 1;
198 /*****************************************************************************
200 *****************************************************************************
201 * Put a PES in the decoder's fifo.
202 *****************************************************************************/
203 void input_DecodePES( decoder_fifo_t * p_decoder_fifo, pes_packet_t * p_pes )
205 vlc_mutex_lock( &p_decoder_fifo->data_lock );
207 p_pes->p_next = NULL;
208 *p_decoder_fifo->pp_last = p_pes;
209 p_decoder_fifo->pp_last = &p_pes->p_next;
210 p_decoder_fifo->i_depth++;
212 /* Warn the decoder that it's got work to do. */
213 vlc_cond_signal( &p_decoder_fifo->data_wait );
214 vlc_mutex_unlock( &p_decoder_fifo->data_lock );
217 /****************************************************************************
219 *****************************************************************************
220 * Extract a PES from the fifo. If pp_pes is NULL then the PES is just
221 * deleted, otherwise *pp_pes will point to this PES.
222 ****************************************************************************/
223 void input_ExtractPES( decoder_fifo_t *p_fifo, pes_packet_t **pp_pes )
227 vlc_mutex_lock( &p_fifo->data_lock );
229 if( p_fifo->p_first == NULL )
233 vlc_mutex_unlock( &p_fifo->data_lock );
234 if( pp_pes ) *pp_pes = NULL;
238 /* Signal the input thread we're waiting. This is only
239 * needed in case of slave clock (ES plug-in) but it won't
241 vlc_cond_signal( &p_fifo->data_wait );
243 /* Wait for the input to tell us when we received a packet. */
244 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
247 p_pes = p_fifo->p_first;
248 p_fifo->p_first = p_pes->p_next;
249 p_pes->p_next = NULL;
252 if( !p_fifo->p_first )
254 /* No PES in the FIFO. p_last is no longer valid. */
255 p_fifo->pp_last = &p_fifo->p_first;
258 vlc_mutex_unlock( &p_fifo->data_lock );
263 input_DeletePES( p_fifo->p_packets_mgt, p_pes );
266 /****************************************************************************
268 *****************************************************************************
269 * Empties the PES fifo of the decoder.
270 ****************************************************************************/
271 void input_FlushPESFifo( decoder_fifo_t *p_fifo )
273 pes_packet_t * p_pes;
275 vlc_mutex_lock( &p_fifo->data_lock );
276 while( p_fifo->p_first )
278 p_pes = p_fifo->p_first;
279 p_fifo->p_first = p_fifo->p_first->p_next;
280 input_DeletePES( p_fifo->p_packets_mgt, p_pes );
282 /* No PES in the FIFO. p_last is no longer valid. */
283 p_fifo->pp_last = &p_fifo->p_first;
284 vlc_mutex_unlock( &p_fifo->data_lock );
287 /*****************************************************************************
288 * input_EscapeDiscontinuity: send a NULL packet to the decoders
289 *****************************************************************************/
290 void input_EscapeDiscontinuity( input_thread_t * p_input )
292 unsigned int i_es, i;
294 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
296 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
298 if( p_es->p_decoder_fifo != NULL )
300 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
302 input_NullPacket( p_input, p_es );
308 /*****************************************************************************
309 * input_EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
310 *****************************************************************************/
311 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
313 unsigned int i_es, i;
315 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
317 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
319 if( p_es->p_decoder_fifo != NULL && p_es->i_cat == AUDIO_ES )
321 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
323 input_NullPacket( p_input, p_es );
329 /*****************************************************************************
330 * CreateDecoderFifo: create a decoder_fifo_t
331 *****************************************************************************/
332 static decoder_fifo_t * CreateDecoderFifo( input_thread_t * p_input,
333 es_descriptor_t * p_es )
335 decoder_fifo_t * p_fifo;
338 p_fifo = vlc_object_create( p_input, VLC_OBJECT_DECODER );
341 msg_Err( p_input, "out of memory" );
345 /* Select a new ES */
346 INSERT_ELEM( p_input->stream.pp_selected_es,
347 p_input->stream.i_selected_es_number,
348 p_input->stream.i_selected_es_number,
351 /* Initialize the p_fifo structure */
352 vlc_mutex_init( p_input, &p_fifo->data_lock );
353 vlc_cond_init( p_input, &p_fifo->data_wait );
354 p_es->p_decoder_fifo = p_fifo;
356 p_fifo->i_id = p_es->i_id;
357 p_fifo->i_fourcc = p_es->i_fourcc;
358 p_fifo->p_demux_data = p_es->p_demux_data;
359 p_fifo->p_waveformatex = p_es->p_waveformatex;
360 p_fifo->p_bitmapinfoheader = p_es->p_bitmapinfoheader;
361 p_fifo->p_stream_ctrl = &p_input->stream.control;
362 p_fifo->p_sout = p_input->stream.p_sout;
364 p_fifo->p_first = NULL;
365 p_fifo->pp_last = &p_fifo->p_first;
367 p_fifo->b_die = p_fifo->b_error = 0;
368 p_fifo->p_packets_mgt = p_input->p_method_data;
370 vlc_object_attach( p_fifo, p_input );
375 /*****************************************************************************
376 * DeleteDecoderFifo: destroy a decoder_fifo_t
377 *****************************************************************************/
378 static void DeleteDecoderFifo( decoder_fifo_t * p_fifo )
380 vlc_object_detach( p_fifo );
382 msg_Dbg( p_fifo, "killing decoder for 0x%x, fourcc `%4.4s', %d PES in FIFO",
383 p_fifo->i_id, (char*)&p_fifo->i_fourcc, p_fifo->i_depth );
385 /* Free all packets still in the decoder fifo. */
386 input_DeletePES( p_fifo->p_packets_mgt,
389 /* Destroy the lock and cond */
390 vlc_cond_destroy( &p_fifo->data_wait );
391 vlc_mutex_destroy( &p_fifo->data_lock );