]> git.sesse.net Git - vlc/blob - src/input/input_dec.c
f46911849eca81ca9fa010704571529b354d67b0
[vlc] / src / input / input_dec.c
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.25 2002/02/05 21:46:41 gbazin Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>                                    /* memcpy(), memset() */
29 #include <sys/types.h>                                              /* off_t */
30
31 #include <videolan/vlc.h>
32
33 #include "stream_control.h"
34 #include "input_ext-dec.h"
35 #include "input_ext-intf.h"
36 #include "input_ext-plugins.h"
37
38 static decoder_config_t * CreateDecoderConfig( input_thread_t * p_input,
39                                                es_descriptor_t * p_es );
40 static void DeleteDecoderConfig( decoder_config_t * p_config );
41
42 /*****************************************************************************
43  * input_RunDecoder: spawns a new decoder thread
44  *****************************************************************************/
45 vlc_thread_t input_RunDecoder( input_thread_t * p_input,
46                                es_descriptor_t * p_es )
47 {
48     probedata_t probedata;
49     vlc_thread_t thread_id;
50     char * psz_plugin = NULL;
51
52     /* Get a suitable module */
53     probedata.i_type = p_es->i_type;
54
55     if( p_es->i_type == MPEG1_AUDIO_ES || p_es->i_type == MPEG2_AUDIO_ES )
56     {
57         psz_plugin = main_GetPszVariable( ADEC_MPEG_VAR, NULL );
58     }
59     if( p_es->i_type == AC3_AUDIO_ES )
60     {
61         psz_plugin = main_GetPszVariable( ADEC_AC3_VAR, NULL );
62     }
63
64     p_es->p_module = module_Need( MODULE_CAPABILITY_DECODER, psz_plugin, &probedata );
65     if( p_es->p_module == NULL )
66     {
67         intf_ErrMsg( "input error: no suitable decoder module for type 0x%x",
68                       p_es->i_type );
69         return( 0 );
70     }
71
72     /* Create the decoder configuration structure */
73     p_es->p_config = CreateDecoderConfig( p_input, p_es );
74
75     if( p_es->p_config == NULL )
76     {
77         intf_ErrMsg( "input error: could not create decoder config" );
78         module_Unneed( p_es->p_module );
79         return( 0 );
80     }
81
82     /* Spawn the decoder thread */
83     if ( vlc_thread_create( &thread_id, "decoder",
84          (vlc_thread_func_t)p_es->p_module->
85              p_functions->dec.functions.dec.pf_run,
86          (void *)p_es->p_config) ) 
87     {
88         intf_ErrMsg( "input error: can't spawn decoder thread \"%s\"",
89                      p_es->p_module->psz_name );
90         free( p_es->p_config );
91         module_Unneed( p_es->p_module );
92         return( 0 );
93     }
94
95     intf_DbgMsg( "input debug: decoder \"%s\"thread created", 
96                  p_es->p_module->psz_name );
97     
98     return thread_id;
99 }
100
101
102 /*****************************************************************************
103  * input_EndDecoder: kills a decoder thread and waits until it's finished
104  *****************************************************************************/
105 void input_EndDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
106 {
107     int i_dummy;
108
109     p_es->p_decoder_fifo->b_die = 1;
110
111     /* Make sure the thread leaves the NextDataPacket() function by
112      * sending it a few null packets. */
113     for( i_dummy = 0; i_dummy < PADDING_PACKET_NUMBER; i_dummy++ )
114     {
115         input_NullPacket( p_input, p_es );
116     }
117
118     if( p_es->p_pes != NULL )
119     {
120         input_DecodePES( p_es->p_decoder_fifo, p_es->p_pes );
121     }
122
123     /* Waiting for the thread to exit */
124     /* I thought that unlocking was better since thread join can be long
125      * but it actually creates late pictures and freezes --stef */
126 //    vlc_mutex_unlock( &p_input->stream.stream_lock );
127     vlc_thread_join( p_es->thread_id );
128 //    vlc_mutex_lock( &p_input->stream.stream_lock );
129
130     /* Delete decoder configuration */
131     DeleteDecoderConfig( p_es->p_config );
132
133     /* Unneed module */
134     module_Unneed( p_es->p_module );
135
136     /* Tell the input there is no more decoder */
137     p_es->p_decoder_fifo = NULL;
138 }
139
140 /*****************************************************************************
141  * input_DecodePES
142  *****************************************************************************
143  * Put a PES in the decoder's fifo.
144  *****************************************************************************/
145 void input_DecodePES( decoder_fifo_t * p_decoder_fifo, pes_packet_t * p_pes )
146 {
147     vlc_mutex_lock( &p_decoder_fifo->data_lock );
148
149     p_pes->p_next = NULL;
150     *p_decoder_fifo->pp_last = p_pes;
151     p_decoder_fifo->pp_last = &p_pes->p_next;
152     p_decoder_fifo->i_depth++;
153
154     /* Warn the decoder that it's got work to do. */
155     vlc_cond_signal( &p_decoder_fifo->data_wait );
156     vlc_mutex_unlock( &p_decoder_fifo->data_lock );
157 }
158
159 /*****************************************************************************
160  * input_EscapeDiscontinuity: send a NULL packet to the decoders
161  *****************************************************************************/
162 void input_EscapeDiscontinuity( input_thread_t * p_input,
163                                 pgrm_descriptor_t * p_pgrm )
164 {
165     int     i_es, i;
166
167     for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
168     {
169         es_descriptor_t * p_es = p_pgrm->pp_es[i_es];
170
171         if( p_es->p_decoder_fifo != NULL )
172         {
173             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
174             {
175                 input_NullPacket( p_input, p_es );
176             }
177         }
178     }
179 }
180
181 /*****************************************************************************
182  * input_EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
183  *****************************************************************************/
184 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
185 {
186     int     i_pgrm, i_es, i;
187
188     for( i_pgrm = 0; i_pgrm < p_input->stream.i_pgrm_number; i_pgrm++ )
189     {
190         pgrm_descriptor_t * p_pgrm = p_input->stream.pp_programs[i_pgrm];
191
192         for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
193         {
194             es_descriptor_t * p_es = p_pgrm->pp_es[i_es];
195
196             if( p_es->p_decoder_fifo != NULL && p_es->b_audio )
197             {
198                 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
199                 {
200                     input_NullPacket( p_input, p_es );
201                 }
202             }
203         }
204     }
205 }
206
207 /*****************************************************************************
208  * CreateDecoderConfig: create a decoder_config_t
209  *****************************************************************************/
210 static decoder_config_t * CreateDecoderConfig( input_thread_t * p_input,
211                                                es_descriptor_t * p_es )
212 {
213     decoder_config_t * p_config;
214
215     p_config = (decoder_config_t *)malloc( sizeof(decoder_config_t) );
216     if( p_config == NULL )
217     {
218         intf_ErrMsg( "Unable to allocate memory in CreateDecoderConfig" );
219         return NULL;
220     }
221
222     /* Decoder FIFO */
223     if( (p_config->p_decoder_fifo =
224             (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) )) == NULL )
225     {
226         intf_ErrMsg( "Out of memory" );
227         free( p_config );
228         return NULL;
229     }
230
231     /* Select a new ES */
232     p_input->stream.i_selected_es_number++;
233     p_input->stream.pp_selected_es = realloc(
234                                        p_input->stream.pp_selected_es,
235                                        p_input->stream.i_selected_es_number
236                                         * sizeof(es_descriptor_t *) );
237     if( p_input->stream.pp_selected_es == NULL )
238     {
239         intf_ErrMsg( "Unable to realloc memory" );
240         free( p_config->p_decoder_fifo );
241         free( p_config );
242         return NULL;
243     }
244     p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
245             = p_es;
246
247     /* Initialize the p_config structure */
248     vlc_mutex_init(&p_config->p_decoder_fifo->data_lock);
249     vlc_cond_init(&p_config->p_decoder_fifo->data_wait);
250     p_es->p_decoder_fifo = p_config->p_decoder_fifo;
251
252     p_config->i_id = p_es->i_id;
253     p_config->i_type = p_es->i_type;
254     p_config->p_stream_ctrl = &p_input->stream.control;
255
256     p_config->p_decoder_fifo->p_first = NULL;
257     p_config->p_decoder_fifo->pp_last = &p_config->p_decoder_fifo->p_first;
258     p_config->p_decoder_fifo->i_depth = 0;
259     p_config->p_decoder_fifo->b_die = p_config->p_decoder_fifo->b_error = 0;
260     p_config->p_decoder_fifo->p_packets_mgt = p_input->p_method_data;
261     p_config->p_decoder_fifo->pf_delete_pes = p_input->pf_delete_pes;
262
263     return p_config;
264 }
265
266 /*****************************************************************************
267  * DeleteDecoderConfig: create a decoder_config_t
268  *****************************************************************************/
269 static void DeleteDecoderConfig( decoder_config_t * p_config )
270 {
271     intf_StatMsg( "input stats: killing decoder for 0x%x, type 0x%x, %d PES in FIFO",
272                   p_config->i_id, p_config->i_type,
273                   p_config->p_decoder_fifo->i_depth );
274     /* Free all packets still in the decoder fifo. */
275     p_config->p_decoder_fifo->pf_delete_pes(
276                         p_config->p_decoder_fifo->p_packets_mgt,
277                         p_config->p_decoder_fifo->p_first );
278
279     /* Destroy the lock and cond */
280     vlc_cond_destroy( &p_config->p_decoder_fifo->data_wait );
281     vlc_mutex_destroy( &p_config->p_decoder_fifo->data_lock );
282
283     free( p_config->p_decoder_fifo );
284
285     free( p_config );
286 }
287