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.44 2002/08/12 09:34:15 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 *****************************************************************************/
28 #include <string.h> /* memcpy(), memset() */
29 #include <sys/types.h> /* off_t */
33 #include "stream_control.h"
34 #include "input_ext-dec.h"
35 #include "input_ext-intf.h"
36 #include "input_ext-plugins.h"
38 static decoder_fifo_t * CreateDecoderFifo( input_thread_t *,
40 static void DeleteDecoderFifo( decoder_fifo_t * );
42 /*****************************************************************************
43 * input_RunDecoder: spawns a new decoder thread
44 *****************************************************************************/
45 decoder_fifo_t * input_RunDecoder( input_thread_t * p_input,
46 es_descriptor_t * p_es )
48 decoder_fifo_t *p_fifo;
50 /* Create the decoder configuration structure */
51 p_fifo = CreateDecoderFifo( p_input, p_es );
55 msg_Err( p_input, "could not create decoder fifo" );
59 /* Get a suitable module */
60 p_fifo->p_module = module_Need( p_fifo, "decoder", "$codec" );
61 if( p_fifo->p_module == NULL )
63 msg_Err( p_fifo, "no suitable decoder module for fourcc `%4.4s'",
64 (char*)&p_fifo->i_fourcc );
65 DeleteDecoderFifo( p_fifo );
66 vlc_object_destroy( p_fifo );
70 /* Spawn the decoder thread */
71 if( vlc_thread_create( p_fifo, "decoder", p_fifo->pf_run, 0 ) )
73 msg_Err( p_fifo, "cannot spawn decoder thread \"%s\"",
74 p_fifo->p_module->psz_object_name );
75 module_Unneed( p_fifo, p_fifo->p_module );
79 p_input->stream.b_changed = 1;
85 /*****************************************************************************
86 * input_EndDecoder: kills a decoder thread and waits until it's finished
87 *****************************************************************************/
88 void input_EndDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
92 p_es->p_decoder_fifo->b_die = 1;
94 /* Make sure the thread leaves the NextDataPacket() function by
95 * sending it a few null packets. */
96 for( i_dummy = 0; i_dummy < PADDING_PACKET_NUMBER; i_dummy++ )
98 input_NullPacket( p_input, p_es );
101 if( p_es->p_pes != NULL )
103 input_DecodePES( p_es->p_decoder_fifo, p_es->p_pes );
106 /* Waiting for the thread to exit */
107 /* I thought that unlocking was better since thread join can be long
108 * but it actually creates late pictures and freezes --stef */
109 /* vlc_mutex_unlock( &p_input->stream.stream_lock ); */
110 vlc_thread_join( p_es->p_decoder_fifo );
111 /* vlc_mutex_lock( &p_input->stream.stream_lock ); */
113 /* Delete decoder configuration */
114 DeleteDecoderFifo( p_es->p_decoder_fifo );
117 module_Unneed( p_es->p_decoder_fifo, p_es->p_decoder_fifo->p_module );
119 /* Delete the fifo */
120 vlc_object_destroy( p_es->p_decoder_fifo );
122 /* Tell the input there is no more decoder */
123 p_es->p_decoder_fifo = NULL;
125 p_input->stream.b_changed = 1;
128 /*****************************************************************************
130 *****************************************************************************
131 * Put a PES in the decoder's fifo.
132 *****************************************************************************/
133 void input_DecodePES( decoder_fifo_t * p_decoder_fifo, pes_packet_t * p_pes )
135 vlc_mutex_lock( &p_decoder_fifo->data_lock );
137 p_pes->p_next = NULL;
138 *p_decoder_fifo->pp_last = p_pes;
139 p_decoder_fifo->pp_last = &p_pes->p_next;
140 p_decoder_fifo->i_depth++;
142 /* Warn the decoder that it's got work to do. */
143 vlc_cond_signal( &p_decoder_fifo->data_wait );
144 vlc_mutex_unlock( &p_decoder_fifo->data_lock );
147 /*****************************************************************************
148 * input_EscapeDiscontinuity: send a NULL packet to the decoders
149 *****************************************************************************/
150 void input_EscapeDiscontinuity( input_thread_t * p_input )
154 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
156 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
158 if( p_es->p_decoder_fifo != NULL )
160 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
162 input_NullPacket( p_input, p_es );
168 /*****************************************************************************
169 * input_EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
170 *****************************************************************************/
171 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
175 for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
177 es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
179 if( p_es->p_decoder_fifo != NULL && p_es->i_cat == AUDIO_ES )
181 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
183 input_NullPacket( p_input, p_es );
189 /*****************************************************************************
190 * CreateDecoderFifo: create a decoder_fifo_t
191 *****************************************************************************/
192 static decoder_fifo_t * CreateDecoderFifo( input_thread_t * p_input,
193 es_descriptor_t * p_es )
195 decoder_fifo_t * p_fifo;
198 p_fifo = vlc_object_create( p_input, VLC_OBJECT_DECODER );
201 msg_Err( p_input, "out of memory" );
205 /* Select a new ES */
206 p_input->stream.i_selected_es_number++;
207 p_input->stream.pp_selected_es = realloc(
208 p_input->stream.pp_selected_es,
209 p_input->stream.i_selected_es_number
210 * sizeof(es_descriptor_t *) );
211 if( p_input->stream.pp_selected_es == NULL )
213 msg_Err( p_input, "out of memory" );
214 vlc_object_destroy( p_fifo );
218 p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
221 /* Initialize the p_fifo structure */
222 vlc_mutex_init( p_input, &p_fifo->data_lock );
223 vlc_cond_init( p_input, &p_fifo->data_wait );
224 p_es->p_decoder_fifo = p_fifo;
226 p_fifo->i_id = p_es->i_id;
227 p_fifo->i_fourcc = p_es->i_fourcc;
228 p_fifo->p_demux_data = p_es->p_demux_data;
230 p_fifo->p_stream_ctrl = &p_input->stream.control;
232 p_fifo->p_first = NULL;
233 p_fifo->pp_last = &p_fifo->p_first;
235 p_fifo->b_die = p_fifo->b_error = 0;
236 p_fifo->p_packets_mgt = p_input->p_method_data;
238 vlc_object_attach( p_fifo, p_input );
243 /*****************************************************************************
244 * DeleteDecoderFifo: destroy a decoder_fifo_t
245 *****************************************************************************/
246 static void DeleteDecoderFifo( decoder_fifo_t * p_fifo )
248 vlc_object_detach( p_fifo );
250 msg_Dbg( p_fifo, "killing decoder for 0x%x, fourcc `%4.4s', %d PES in FIFO",
251 p_fifo->i_id, (char*)&p_fifo->i_fourcc, p_fifo->i_depth );
253 /* Free all packets still in the decoder fifo. */
254 input_DeletePES( p_fifo->p_packets_mgt,
257 /* Destroy the lock and cond */
258 vlc_cond_destroy( &p_fifo->data_wait );
259 vlc_mutex_destroy( &p_fifo->data_lock );