]> git.sesse.net Git - vlc/blob - src/input/input_dec.c
8b601dc1cb4405ea698e120b4d4435aa7fda4a72
[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.40 2002/07/23 00:39:17 sam 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 <vlc/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_fifo_t * CreateDecoderFifo( input_thread_t *,
39                                            es_descriptor_t * );
40 static void             DeleteDecoderFifo( decoder_fifo_t * );
41
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 )
47 {
48     char * psz_plugin = config_GetPsz( p_input, "codec" );
49
50     /* Get a suitable module */
51     p_es->p_module = module_Need( p_input, MODULE_CAPABILITY_DECODER,
52                                   psz_plugin, (void *)&p_es->i_fourcc );
53     if( psz_plugin ) free( psz_plugin );
54     if( p_es->p_module == NULL )
55     {
56         msg_Err( p_input, "no suitable decoder module for fourcc `%4.4s'",
57                           (char*)&p_es->i_fourcc );
58         return NULL;
59     }
60
61     /* Create the decoder configuration structure */
62     p_es->p_decoder_fifo = CreateDecoderFifo( p_input, p_es );
63
64     if( p_es->p_decoder_fifo == NULL )
65     {
66         msg_Err( p_input, "could not create decoder fifo" );
67         module_Unneed( p_es->p_module );
68         return NULL;
69     }
70
71     /* Spawn the decoder thread */
72     if ( vlc_thread_create( p_es->p_decoder_fifo, "decoder",
73                  p_es->p_module->p_functions->dec.functions.dec.pf_run, 0 ) )
74     {
75         msg_Err( p_input, "cannot spawn decoder thread \"%s\"",
76                            p_es->p_module->psz_object_name );
77         DeleteDecoderFifo( p_es->p_decoder_fifo );
78         module_Unneed( p_es->p_module );
79         return NULL;
80     }
81
82     p_input->stream.b_changed = 1;
83
84     return p_es->p_decoder_fifo;
85 }
86
87
88 /*****************************************************************************
89  * input_EndDecoder: kills a decoder thread and waits until it's finished
90  *****************************************************************************/
91 void input_EndDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
92 {
93     int i_dummy;
94
95     p_es->p_decoder_fifo->b_die = 1;
96
97     /* Make sure the thread leaves the NextDataPacket() function by
98      * sending it a few null packets. */
99     for( i_dummy = 0; i_dummy < PADDING_PACKET_NUMBER; i_dummy++ )
100     {
101         input_NullPacket( p_input, p_es );
102     }
103
104     if( p_es->p_pes != NULL )
105     {
106         input_DecodePES( p_es->p_decoder_fifo, p_es->p_pes );
107     }
108
109     /* Waiting for the thread to exit */
110     /* I thought that unlocking was better since thread join can be long
111      * but it actually creates late pictures and freezes --stef */
112 //    vlc_mutex_unlock( &p_input->stream.stream_lock );
113     vlc_thread_join( p_es->p_decoder_fifo );
114 //    vlc_mutex_lock( &p_input->stream.stream_lock );
115
116     /* Delete decoder configuration */
117     DeleteDecoderFifo( p_es->p_decoder_fifo );
118
119     /* Unneed module */
120     module_Unneed( p_es->p_module );
121
122     /* Tell the input there is no more decoder */
123     p_es->p_decoder_fifo = NULL;
124
125     p_input->stream.b_changed = 1;
126 }
127
128 /*****************************************************************************
129  * input_DecodePES
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 )
134 {
135     vlc_mutex_lock( &p_decoder_fifo->data_lock );
136
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++;
141
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 );
145 }
146
147 /*****************************************************************************
148  * input_EscapeDiscontinuity: send a NULL packet to the decoders
149  *****************************************************************************/
150 void input_EscapeDiscontinuity( input_thread_t * p_input )
151 {
152     int     i_es, i;
153
154     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
155     {
156         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
157
158         if( p_es->p_decoder_fifo != NULL )
159         {
160             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
161             {
162                 input_NullPacket( p_input, p_es );
163             }
164         }
165     }
166 }
167
168 /*****************************************************************************
169  * input_EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
170  *****************************************************************************/
171 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
172 {
173     int     i_es, i;
174
175     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
176     {
177         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
178
179         if( p_es->p_decoder_fifo != NULL && p_es->i_cat == AUDIO_ES )
180         {
181             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
182             {
183                 input_NullPacket( p_input, p_es );
184             }
185         }
186     }
187 }
188
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 )
194 {
195     decoder_fifo_t * p_fifo;
196
197     /* Decoder FIFO */
198     p_fifo = vlc_object_create( p_input, VLC_OBJECT_DECODER );
199     if( p_fifo == NULL )
200     {
201         msg_Err( p_input, "out of memory" );
202         return NULL;
203     }
204
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 )
212     {
213         msg_Err( p_input, "out of memory" );
214         vlc_object_destroy( p_fifo );
215         return NULL;
216     }
217
218     p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
219             = p_es;
220
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;
225
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;
229     
230     p_fifo->p_stream_ctrl = &p_input->stream.control;
231
232     p_fifo->p_first = NULL;
233     p_fifo->pp_last = &p_fifo->p_first;
234     p_fifo->i_depth = 0;
235     p_fifo->b_die = p_fifo->b_error = 0;
236     p_fifo->p_packets_mgt = p_input->p_method_data;
237
238     vlc_object_attach( p_fifo, p_input );
239
240     return p_fifo;
241 }
242
243 /*****************************************************************************
244  * DeleteDecoderFifo: destroy a decoder_fifo_t
245  *****************************************************************************/
246 static void DeleteDecoderFifo( decoder_fifo_t * p_fifo )
247 {
248     vlc_object_detach_all( p_fifo );
249
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 );
252
253     /* Free all packets still in the decoder fifo. */
254     input_DeletePES( p_fifo->p_packets_mgt,
255                      p_fifo->p_first );
256
257     /* Destroy the lock and cond */
258     vlc_cond_destroy( &p_fifo->data_wait );
259     vlc_mutex_destroy( &p_fifo->data_lock );
260
261     vlc_object_destroy( p_fifo );
262 }
263