]> git.sesse.net Git - vlc/blob - src/input/input_dec.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[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.41 2002/07/31 20:56:52 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;
49     decoder_fifo_t *p_fifo;
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     psz_plugin = config_GetPsz( p_fifo, "codec" );
62     p_fifo->p_module = module_Need( p_fifo, "decoder", psz_plugin );
63     if( psz_plugin ) free( psz_plugin );
64     if( p_fifo->p_module == NULL )
65     {
66         msg_Err( p_fifo, "no suitable decoder module for fourcc `%4.4s'",
67                        (char*)&p_fifo->i_fourcc );
68         DeleteDecoderFifo( p_fifo );
69         vlc_object_destroy( p_fifo );
70         return NULL;
71     }
72
73     /* Spawn the decoder thread */
74     if( vlc_thread_create( p_fifo, "decoder", p_fifo->pf_run, 0 ) )
75     {
76         msg_Err( p_fifo, "cannot spawn decoder thread \"%s\"",
77                          p_fifo->p_module->psz_object_name );
78         module_Unneed( p_fifo, p_fifo->p_module );
79         return NULL;
80     }
81
82     p_input->stream.b_changed = 1;
83
84     return p_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_decoder_fifo, p_es->p_decoder_fifo->p_module );
121
122     /* Delete the fifo */
123     vlc_object_destroy( p_es->p_decoder_fifo );
124
125     /* Tell the input there is no more decoder */
126     p_es->p_decoder_fifo = NULL;
127
128     p_input->stream.b_changed = 1;
129 }
130
131 /*****************************************************************************
132  * input_DecodePES
133  *****************************************************************************
134  * Put a PES in the decoder's fifo.
135  *****************************************************************************/
136 void input_DecodePES( decoder_fifo_t * p_decoder_fifo, pes_packet_t * p_pes )
137 {
138     vlc_mutex_lock( &p_decoder_fifo->data_lock );
139
140     p_pes->p_next = NULL;
141     *p_decoder_fifo->pp_last = p_pes;
142     p_decoder_fifo->pp_last = &p_pes->p_next;
143     p_decoder_fifo->i_depth++;
144
145     /* Warn the decoder that it's got work to do. */
146     vlc_cond_signal( &p_decoder_fifo->data_wait );
147     vlc_mutex_unlock( &p_decoder_fifo->data_lock );
148 }
149
150 /*****************************************************************************
151  * input_EscapeDiscontinuity: send a NULL packet to the decoders
152  *****************************************************************************/
153 void input_EscapeDiscontinuity( input_thread_t * p_input )
154 {
155     int     i_es, i;
156
157     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
158     {
159         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
160
161         if( p_es->p_decoder_fifo != NULL )
162         {
163             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
164             {
165                 input_NullPacket( p_input, p_es );
166             }
167         }
168     }
169 }
170
171 /*****************************************************************************
172  * input_EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
173  *****************************************************************************/
174 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
175 {
176     int     i_es, i;
177
178     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
179     {
180         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
181
182         if( p_es->p_decoder_fifo != NULL && p_es->i_cat == AUDIO_ES )
183         {
184             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
185             {
186                 input_NullPacket( p_input, p_es );
187             }
188         }
189     }
190 }
191
192 /*****************************************************************************
193  * CreateDecoderFifo: create a decoder_fifo_t
194  *****************************************************************************/
195 static decoder_fifo_t * CreateDecoderFifo( input_thread_t * p_input,
196                                            es_descriptor_t * p_es )
197 {
198     decoder_fifo_t * p_fifo;
199
200     /* Decoder FIFO */
201     p_fifo = vlc_object_create( p_input, VLC_OBJECT_DECODER );
202     if( p_fifo == NULL )
203     {
204         msg_Err( p_input, "out of memory" );
205         return NULL;
206     }
207
208     /* Select a new ES */
209     p_input->stream.i_selected_es_number++;
210     p_input->stream.pp_selected_es = realloc(
211                                           p_input->stream.pp_selected_es,
212                                           p_input->stream.i_selected_es_number
213                                            * sizeof(es_descriptor_t *) );
214     if( p_input->stream.pp_selected_es == NULL )
215     {
216         msg_Err( p_input, "out of memory" );
217         vlc_object_destroy( p_fifo );
218         return NULL;
219     }
220
221     p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number - 1]
222             = p_es;
223
224     /* Initialize the p_fifo structure */
225     vlc_mutex_init( p_input, &p_fifo->data_lock );
226     vlc_cond_init( p_input, &p_fifo->data_wait );
227     p_es->p_decoder_fifo = p_fifo;
228
229     p_fifo->i_id = p_es->i_id;
230     p_fifo->i_fourcc = p_es->i_fourcc;
231     p_fifo->p_demux_data = p_es->p_demux_data;
232     
233     p_fifo->p_stream_ctrl = &p_input->stream.control;
234
235     p_fifo->p_first = NULL;
236     p_fifo->pp_last = &p_fifo->p_first;
237     p_fifo->i_depth = 0;
238     p_fifo->b_die = p_fifo->b_error = 0;
239     p_fifo->p_packets_mgt = p_input->p_method_data;
240
241     vlc_object_attach( p_fifo, p_input );
242
243     return p_fifo;
244 }
245
246 /*****************************************************************************
247  * DeleteDecoderFifo: destroy a decoder_fifo_t
248  *****************************************************************************/
249 static void DeleteDecoderFifo( decoder_fifo_t * p_fifo )
250 {
251     vlc_object_detach_all( p_fifo );
252
253     msg_Dbg( p_fifo, "killing decoder for 0x%x, fourcc `%4.4s', %d PES in FIFO",
254                      p_fifo->i_id, (char*)&p_fifo->i_fourcc, p_fifo->i_depth );
255
256     /* Free all packets still in the decoder fifo. */
257     input_DeletePES( p_fifo->p_packets_mgt,
258                      p_fifo->p_first );
259
260     /* Destroy the lock and cond */
261     vlc_cond_destroy( &p_fifo->data_wait );
262     vlc_mutex_destroy( &p_fifo->data_lock );
263 }
264