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.64 2003/10/08 21:01:07 gbazin 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() */
31 #include <vlc/decoder.h>
33 #include "input_ext-intf.h"
34 #include "input_ext-plugins.h"
36 static decoder_t * CreateDecoder( input_thread_t *, es_descriptor_t * );
37 static int DecoderThread( decoder_t * );
38 static void DeleteDecoder( decoder_t * );
40 /*****************************************************************************
41 * input_RunDecoder: spawns a new decoder thread
42 *****************************************************************************/
43 decoder_fifo_t * input_RunDecoder( input_thread_t * p_input,
44 es_descriptor_t * p_es )
50 /* Create the decoder configuration structure */
51 p_dec = CreateDecoder( p_input, p_es );
54 msg_Err( p_input, "could not create decoder" );
58 p_dec->p_module = NULL;
60 /* If we are in sout mode, search for packetizer module */
61 var_Get( p_input, "sout", &val );
62 if( !p_es->b_force_decoder && val.psz_string && *val.psz_string )
64 free( val.psz_string );
65 val.b_bool = VLC_TRUE;
67 if( p_es->i_cat == AUDIO_ES )
69 var_Get( p_input, "sout-audio", &val );
71 else if( p_es->i_cat == VIDEO_ES )
73 var_Get( p_input, "sout-video", &val );
79 module_Need( p_dec, "packetizer", "$packetizer" );
84 /* default Get a suitable decoder module */
85 p_dec->p_module = module_Need( p_dec, "decoder", "$codec" );
87 if( val.psz_string ) free( val.psz_string );
90 if( p_dec->p_module == NULL )
92 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
93 "VLC probably does not support this sound or video format.",
94 (char*)&p_dec->p_fifo->i_fourcc );
96 DeleteDecoder( p_dec );
97 vlc_object_destroy( p_dec );
101 if ( p_es->i_cat == AUDIO_ES )
103 i_priority = VLC_THREAD_PRIORITY_AUDIO;
107 i_priority = VLC_THREAD_PRIORITY_VIDEO;
110 /* Spawn the decoder thread */
111 if( vlc_thread_create( p_dec, "decoder", DecoderThread,
112 i_priority, VLC_FALSE ) )
114 msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
115 p_dec->p_module->psz_object_name );
116 module_Unneed( p_dec, p_dec->p_module );
117 DeleteDecoder( p_dec );
118 vlc_object_destroy( p_dec );
122 p_input->stream.b_changed = 1;
124 return p_dec->p_fifo;
127 /*****************************************************************************
128 * input_EndDecoder: kills a decoder thread and waits until it's finished
129 *****************************************************************************/
130 void input_EndDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
133 decoder_t *p_dec = p_es->p_decoder_fifo->p_dec;
135 p_es->p_decoder_fifo->b_die = 1;
137 /* Make sure the thread leaves the NextDataPacket() function by
138 * sending it a few null packets. */
139 for( i_dummy = 0; i_dummy < PADDING_PACKET_NUMBER; i_dummy++ )
141 input_NullPacket( p_input, p_es );
144 if( p_es->p_pes != NULL )
146 input_DecodePES( p_es->p_decoder_fifo, p_es->p_pes );
149 /* Waiting for the thread to exit */
150 /* I thought that unlocking was better since thread join can be long
151 * but it actually creates late pictures and freezes --stef */
152 /* vlc_mutex_unlock( &p_input->stream.stream_lock ); */
153 vlc_thread_join( p_dec );
154 /* vlc_mutex_lock( &p_input->stream.stream_lock ); */
157 module_Unneed( p_dec, p_dec->p_module );
159 /* Delete decoder configuration */
160 DeleteDecoder( p_dec );
162 /* Delete the decoder */
163 vlc_object_destroy( p_dec );
165 /* Tell the input there is no more decoder */
166 p_es->p_decoder_fifo = NULL;
168 p_input->stream.b_changed = 1;
171 /*****************************************************************************
173 *****************************************************************************
174 * Put a PES in the decoder's fifo.
175 *****************************************************************************/
176 void input_DecodePES( decoder_fifo_t * p_decoder_fifo, pes_packet_t * p_pes )
178 vlc_mutex_lock( &p_decoder_fifo->data_lock );
180 p_pes->p_next = NULL;
181 *p_decoder_fifo->pp_last = p_pes;
182 p_decoder_fifo->pp_last = &p_pes->p_next;
183 p_decoder_fifo->i_depth++;
185 /* Warn the decoder that it's got work to do. */
186 vlc_cond_signal( &p_decoder_fifo->data_wait );
187 vlc_mutex_unlock( &p_decoder_fifo->data_lock );
190 /*****************************************************************************
191 * Create a NULL packet for padding in case of a data loss
192 *****************************************************************************/
193 void input_NullPacket( input_thread_t * p_input,
194 es_descriptor_t * p_es )
196 data_packet_t * p_pad_data;
197 pes_packet_t * p_pes;
199 if( (p_pad_data = input_NewPacketForce( p_input->p_method_data,
200 PADDING_PACKET_SIZE)) == NULL )
202 msg_Err( p_input, "no new packet" );
203 p_input->b_error = 1;
207 memset( p_pad_data->p_payload_start, 0, PADDING_PACKET_SIZE );
208 p_pad_data->b_discard_payload = 1;
213 p_pes->b_discontinuity = 1;
214 p_pes->p_last->p_next = p_pad_data;
215 p_pes->p_last = p_pad_data;
220 if( (p_pes = input_NewPES( p_input->p_method_data )) == NULL )
222 msg_Err( p_input, "no PES packet" );
223 p_input->b_error = 1;
227 p_pes->i_rate = p_input->stream.control.i_rate;
228 p_pes->p_first = p_pes->p_last = p_pad_data;
229 p_pes->i_nb_data = 1;
230 p_pes->b_discontinuity = 1;
231 input_DecodePES( p_es->p_decoder_fifo, p_pes );
235 /*****************************************************************************
236 * input_EscapeDiscontinuity: send a NULL packet to the decoders
237 *****************************************************************************/
238 void input_EscapeDiscontinuity( input_thread_t * p_input )
240 unsigned int i_es, i;
242 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
244 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
246 if( p_es->p_decoder_fifo != NULL )
248 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
250 input_NullPacket( p_input, p_es );
256 /*****************************************************************************
257 * input_EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
258 *****************************************************************************/
259 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
261 unsigned int i_es, i;
263 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
265 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
267 if( p_es->p_decoder_fifo != NULL && p_es->i_cat == AUDIO_ES )
269 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
271 input_NullPacket( p_input, p_es );
277 /*****************************************************************************
278 * CreateDecoderFifo: create a decoder_fifo_t
279 *****************************************************************************/
280 static decoder_t * CreateDecoder( input_thread_t * p_input,
281 es_descriptor_t * p_es )
285 p_dec = vlc_object_create( p_input, VLC_OBJECT_DECODER );
288 msg_Err( p_input, "out of memory" );
293 p_dec->pf_decode = 0;
297 /* Select a new ES */
298 INSERT_ELEM( p_input->stream.pp_selected_es,
299 p_input->stream.i_selected_es_number,
300 p_input->stream.i_selected_es_number,
303 /* Allocate the memory needed to store the decoder's fifo */
304 //p_dec->p_fifo = (decoder_fifo_t *)malloc(sizeof(decoder_fifo_t));
305 p_dec->p_fifo = vlc_object_create( p_input, VLC_OBJECT_DECODER_FIFO );
306 if( p_dec->p_fifo == NULL )
308 msg_Err( p_input, "out of memory" );
312 /* Initialize the decoder fifo */
313 //memset( p_dec->p_fifo, 0, sizeof(decoder_fifo_t) );
314 p_dec->p_module = NULL;
316 /* Initialize the p_fifo structure */
317 vlc_mutex_init( p_input, &p_dec->p_fifo->data_lock );
318 vlc_cond_init( p_input, &p_dec->p_fifo->data_wait );
319 p_es->p_decoder_fifo = p_dec->p_fifo;
321 p_dec->p_fifo->i_id = p_es->i_id;
322 p_dec->p_fifo->i_fourcc = p_es->i_fourcc;
323 p_dec->p_fifo->p_demux_data = p_es->p_demux_data;
324 p_dec->p_fifo->p_waveformatex = p_es->p_waveformatex;
325 p_dec->p_fifo->p_bitmapinfoheader = p_es->p_bitmapinfoheader;
326 p_dec->p_fifo->p_stream_ctrl = &p_input->stream.control;
327 p_dec->p_fifo->p_sout = p_input->stream.p_sout;
329 p_dec->p_fifo->p_first = NULL;
330 p_dec->p_fifo->pp_last = &p_dec->p_fifo->p_first;
331 p_dec->p_fifo->i_depth = 0;
332 p_dec->p_fifo->b_die = p_dec->p_fifo->b_error = 0;
333 p_dec->p_fifo->p_packets_mgt = p_input->p_method_data;
335 p_dec->p_fifo->p_dec = p_dec;
337 vlc_object_attach( p_dec->p_fifo, p_input );
338 vlc_object_attach( p_dec, p_input );
343 /*****************************************************************************
344 * DecoderThread: the decoding main loop
345 *****************************************************************************/
346 static int DecoderThread( decoder_t * p_dec )
349 data_packet_t *p_data;
352 /* Temporary wrapper to keep old decoder api functional */
355 p_dec->pf_run( p_dec->p_fifo );
360 /* Initialize the decoder */
361 p_dec->p_fifo->b_error = p_dec->pf_init( p_dec );
363 /* The decoder's main loop */
364 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
368 input_ExtractPES( p_dec->p_fifo, &p_pes );
371 p_dec->p_fifo->b_error = 1;
375 for( i_size = 0, p_data = p_pes->p_first;
376 p_data != NULL; p_data = p_data->p_next )
378 i_size += p_data->p_payload_end - p_data->p_payload_start;
380 p_block = block_New( p_dec, i_size );
381 for( i_size = 0, p_data = p_pes->p_first;
382 p_data != NULL; p_data = p_data->p_next )
384 if( p_data->p_payload_end == p_data->p_payload_start )
386 memcpy( p_block->p_buffer + i_size, p_data->p_payload_start,
387 p_data->p_payload_end - p_data->p_payload_start );
388 i_size += p_data->p_payload_end - p_data->p_payload_start;
391 p_block->i_pts = p_pes->i_pts;
392 p_block->i_dts = p_pes->i_dts;
393 p_block->b_discontinuity = p_pes->b_discontinuity;
394 p_dec->p_fifo->b_error = p_dec->pf_decode( p_dec, p_block );
396 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
399 /* If b_error is set, the decoder thread enters the error loop */
400 if( p_dec->p_fifo->b_error )
402 /* Wait until a `die' order is sent */
403 while( !p_dec->p_fifo->b_die )
405 /* Trash all received PES packets */
406 input_ExtractPES( p_dec->p_fifo, NULL );
410 /* End of the decoder */
411 p_dec->pf_end( p_dec );
416 /*****************************************************************************
417 * DeleteDecoderFifo: destroy a decoder_fifo_t
418 *****************************************************************************/
419 static void DeleteDecoder( decoder_t * p_dec )
421 vlc_object_detach( p_dec );
422 vlc_object_detach( p_dec->p_fifo );
425 "killing decoder for 0x%x, fourcc `%4.4s', %d PES in FIFO",
426 p_dec->p_fifo->i_id, (char*)&p_dec->p_fifo->i_fourcc,
427 p_dec->p_fifo->i_depth );
429 /* Free all packets still in the decoder fifo. */
430 input_DeletePES( p_dec->p_fifo->p_packets_mgt,
431 p_dec->p_fifo->p_first );
433 /* Destroy the lock and cond */
434 vlc_cond_destroy( &p_dec->p_fifo->data_wait );
435 vlc_mutex_destroy( &p_dec->p_fifo->data_lock );
438 vlc_object_destroy( p_dec->p_fifo );
439 //free( p_dec->p_fifo );