]> git.sesse.net Git - vlc/blob - src/input/input_dec.c
321b5dbc0d14122c0abd112e2bcb539b5b6d7ee9
[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.49 2002/10/29 13:22:48 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     decoder_fifo_t *p_fifo;
49     int i_priority;
50
51     /* Create the decoder configuration structure */
52     p_fifo = CreateDecoderFifo( p_input, p_es );
53
54     if( p_fifo == NULL )
55     {
56         msg_Err( p_input, "could not create decoder fifo" );
57         return NULL;
58     }
59
60     /* Get a suitable module */
61     p_fifo->p_module = module_Need( p_fifo, "decoder", "$codec" );
62     if( p_fifo->p_module == NULL )
63     {
64         msg_Err( p_fifo, "no suitable decoder module for fourcc `%4.4s'",
65                        (char*)&p_fifo->i_fourcc );
66         DeleteDecoderFifo( p_fifo );
67         vlc_object_destroy( p_fifo );
68         return NULL;
69     }
70
71     if ( p_es->i_cat == AUDIO_ES )
72     {
73         i_priority = VLC_THREAD_PRIORITY_AUDIO;
74     }
75     else
76     {
77         i_priority = VLC_THREAD_PRIORITY_VIDEO;
78     }
79
80     /* Spawn the decoder thread */
81     if( vlc_thread_create( p_fifo, "decoder", p_fifo->pf_run,
82                            i_priority, VLC_FALSE ) )
83     {
84         msg_Err( p_fifo, "cannot spawn decoder thread \"%s\"",
85                          p_fifo->p_module->psz_object_name );
86         module_Unneed( p_fifo, p_fifo->p_module );
87         return NULL;
88     }
89
90     p_input->stream.b_changed = 1;
91
92     return p_fifo;
93 }
94
95
96 /*****************************************************************************
97  * input_EndDecoder: kills a decoder thread and waits until it's finished
98  *****************************************************************************/
99 void input_EndDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
100 {
101     int i_dummy;
102
103     p_es->p_decoder_fifo->b_die = 1;
104
105     /* Make sure the thread leaves the NextDataPacket() function by
106      * sending it a few null packets. */
107     for( i_dummy = 0; i_dummy < PADDING_PACKET_NUMBER; i_dummy++ )
108     {
109         input_NullPacket( p_input, p_es );
110     }
111
112     if( p_es->p_pes != NULL )
113     {
114         input_DecodePES( p_es->p_decoder_fifo, p_es->p_pes );
115     }
116
117     /* Waiting for the thread to exit */
118     /* I thought that unlocking was better since thread join can be long
119      * but it actually creates late pictures and freezes --stef */
120     /* vlc_mutex_unlock( &p_input->stream.stream_lock ); */
121     vlc_thread_join( p_es->p_decoder_fifo );
122     /* vlc_mutex_lock( &p_input->stream.stream_lock ); */
123
124     /* Delete decoder configuration */
125     DeleteDecoderFifo( p_es->p_decoder_fifo );
126
127     /* Unneed module */
128     module_Unneed( p_es->p_decoder_fifo, p_es->p_decoder_fifo->p_module );
129
130     /* Delete the fifo */
131     vlc_object_destroy( p_es->p_decoder_fifo );
132
133     /* Tell the input there is no more decoder */
134     p_es->p_decoder_fifo = NULL;
135
136     p_input->stream.b_changed = 1;
137 }
138
139 /*****************************************************************************
140  * input_DecodePES
141  *****************************************************************************
142  * Put a PES in the decoder's fifo.
143  *****************************************************************************/
144 void input_DecodePES( decoder_fifo_t * p_decoder_fifo, pes_packet_t * p_pes )
145 {
146     vlc_mutex_lock( &p_decoder_fifo->data_lock );
147
148     p_pes->p_next = NULL;
149     *p_decoder_fifo->pp_last = p_pes;
150     p_decoder_fifo->pp_last = &p_pes->p_next;
151     p_decoder_fifo->i_depth++;
152
153     /* Warn the decoder that it's got work to do. */
154     vlc_cond_signal( &p_decoder_fifo->data_wait );
155     vlc_mutex_unlock( &p_decoder_fifo->data_lock );
156 }
157
158 /****************************************************************************
159  * input_ExtractPES
160  *****************************************************************************
161  * Extract a PES from the fifo. If pp_pes is NULL then the PES is just
162  * deleted, otherwise *pp_pes will point to this PES.
163  ****************************************************************************/
164 void input_ExtractPES( decoder_fifo_t *p_fifo, pes_packet_t **pp_pes )
165 {
166     pes_packet_t *p_pes;
167
168     vlc_mutex_lock( &p_fifo->data_lock );
169
170     if( p_fifo->p_first == NULL )
171     {
172         if( p_fifo->b_die )
173         {
174             vlc_mutex_unlock( &p_fifo->data_lock );
175             if( pp_pes ) *pp_pes = NULL;
176             return;
177         }
178
179         /* Signal the input thread we're waiting. This is only
180          * needed in case of slave clock (ES plug-in) but it won't
181          * harm. */
182         vlc_cond_signal( &p_fifo->data_wait );
183
184         /* Wait for the input to tell us when we received a packet. */
185         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
186     }
187
188     p_pes = p_fifo->p_first;
189     p_fifo->p_first = p_pes->p_next;
190     p_pes->p_next = NULL;
191     p_fifo->i_depth--;
192
193     if( !p_fifo->p_first )
194     {
195         /* No PES in the FIFO. p_last is no longer valid. */
196         p_fifo->pp_last = &p_fifo->p_first;
197     }
198
199     vlc_mutex_unlock( &p_fifo->data_lock );
200
201     if( pp_pes )
202         *pp_pes = p_pes;
203     else
204         input_DeletePES( p_fifo->p_packets_mgt, p_pes );
205 }
206
207 /****************************************************************************
208  * input_FlushPESFifo
209  *****************************************************************************
210  * Empties the PES fifo of the decoder.
211  ****************************************************************************/
212 void input_FlushPESFifo( decoder_fifo_t *p_fifo )
213 {
214     pes_packet_t * p_pes;
215
216     vlc_mutex_lock( &p_fifo->data_lock );
217     while( p_fifo->p_first )
218     {
219         p_pes = p_fifo->p_first;
220         p_fifo->p_first = p_fifo->p_first->p_next;
221         input_DeletePES( p_fifo->p_packets_mgt, p_pes );
222     }
223     /* No PES in the FIFO. p_last is no longer valid. */
224     p_fifo->pp_last = &p_fifo->p_first;
225     vlc_mutex_unlock( &p_fifo->data_lock );
226 }
227
228 /*****************************************************************************
229  * input_EscapeDiscontinuity: send a NULL packet to the decoders
230  *****************************************************************************/
231 void input_EscapeDiscontinuity( input_thread_t * p_input )
232 {
233     int     i_es, i;
234
235     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
236     {
237         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
238
239         if( p_es->p_decoder_fifo != NULL )
240         {
241             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
242             {
243                 input_NullPacket( p_input, p_es );
244             }
245         }
246     }
247 }
248
249 /*****************************************************************************
250  * input_EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
251  *****************************************************************************/
252 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
253 {
254     int     i_es, i;
255
256     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
257     {
258         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
259
260         if( p_es->p_decoder_fifo != NULL && p_es->i_cat == AUDIO_ES )
261         {
262             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
263             {
264                 input_NullPacket( p_input, p_es );
265             }
266         }
267     }
268 }
269
270 /*****************************************************************************
271  * CreateDecoderFifo: create a decoder_fifo_t
272  *****************************************************************************/
273 static decoder_fifo_t * CreateDecoderFifo( input_thread_t * p_input,
274                                            es_descriptor_t * p_es )
275 {
276     decoder_fifo_t * p_fifo;
277
278     /* Decoder FIFO */
279     p_fifo = vlc_object_create( p_input, VLC_OBJECT_DECODER );
280     if( p_fifo == NULL )
281     {
282         msg_Err( p_input, "out of memory" );
283         return NULL;
284     }
285
286     /* Select a new ES */
287     INSERT_ELEM( p_input->stream.pp_selected_es,
288                  p_input->stream.i_selected_es_number,
289                  p_input->stream.i_selected_es_number,
290                  p_es );
291
292     /* Initialize the p_fifo structure */
293     vlc_mutex_init( p_input, &p_fifo->data_lock );
294     vlc_cond_init( p_input, &p_fifo->data_wait );
295     p_es->p_decoder_fifo = p_fifo;
296
297     p_fifo->i_id = p_es->i_id;
298     p_fifo->i_fourcc = p_es->i_fourcc;
299     p_fifo->p_demux_data = p_es->p_demux_data;
300     
301     p_fifo->p_stream_ctrl = &p_input->stream.control;
302     p_fifo->p_sout = p_input->stream.p_sout;
303
304     p_fifo->p_first = NULL;
305     p_fifo->pp_last = &p_fifo->p_first;
306     p_fifo->i_depth = 0;
307     p_fifo->b_die = p_fifo->b_error = 0;
308     p_fifo->p_packets_mgt = p_input->p_method_data;
309
310     vlc_object_attach( p_fifo, p_input );
311
312     return p_fifo;
313 }
314
315 /*****************************************************************************
316  * DeleteDecoderFifo: destroy a decoder_fifo_t
317  *****************************************************************************/
318 static void DeleteDecoderFifo( decoder_fifo_t * p_fifo )
319 {
320     vlc_object_detach( p_fifo );
321
322     msg_Dbg( p_fifo, "killing decoder for 0x%x, fourcc `%4.4s', %d PES in FIFO",
323                      p_fifo->i_id, (char*)&p_fifo->i_fourcc, p_fifo->i_depth );
324
325     /* Free all packets still in the decoder fifo. */
326     input_DeletePES( p_fifo->p_packets_mgt,
327                      p_fifo->p_first );
328
329     /* Destroy the lock and cond */
330     vlc_cond_destroy( &p_fifo->data_wait );
331     vlc_mutex_destroy( &p_fifo->data_lock );
332 }
333