]> git.sesse.net Git - vlc/blob - modules/codec/spudec/spudec.c
* ./src/misc/win32_specific.c: under Win32 we retrieve the executable's
[vlc] / modules / codec / spudec / spudec.c
1 /*****************************************************************************
2  * spudec.c : SPU decoder thread
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: spudec.c,v 1.15 2003/02/17 05:50:31 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
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>                                      /* malloc(), free() */
28 #include <string.h>                                    /* memcpy(), memset() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32 #include <vlc/decoder.h>
33
34 #include "spudec.h"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  OpenDecoder   ( vlc_object_t * );
40 static int  RunDecoder    ( decoder_fifo_t * );
41 static int  InitThread    ( spudec_thread_t * );
42 static void EndThread     ( spudec_thread_t * );
43 static vout_thread_t *FindVout( spudec_thread_t * );
44
45 /*****************************************************************************
46  * Module descriptor.
47  *****************************************************************************/
48 #define FONT_TEXT N_("font used by the text subtitler")
49 #define FONT_LONGTEXT N_(\
50     "When the subtitles are coded in text form then, you can choose " \
51     "which font will be used to display them.")
52
53 #define DEFAULT_FONT "font-eutopiabold36.rle"
54
55 vlc_module_begin();
56     add_category_hint( N_("subtitles"), NULL );
57 #if defined(SYS_DARWIN) || defined(SYS_BEOS) \
58      || (defined(WIN32) && !defined(UNDER_CE))
59     add_file( "spudec-font", NULL, NULL,
60               FONT_TEXT, FONT_LONGTEXT );
61 #else
62     add_file( "spudec-font", "share/" DEFAULT_FONT, NULL,
63               FONT_TEXT, FONT_LONGTEXT );
64 #endif
65     set_description( _("subtitles decoder module") );
66     set_capability( "decoder", 50 );
67     set_callbacks( OpenDecoder, NULL );
68 vlc_module_end();
69
70 /*****************************************************************************
71  * OpenDecoder: probe the decoder and return score
72  *****************************************************************************
73  * Tries to launch a decoder and return score so that the interface is able
74  * to chose.
75  *****************************************************************************/
76 static int OpenDecoder( vlc_object_t *p_this )
77 {
78     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
79
80     if( p_fifo->i_fourcc != VLC_FOURCC('s','p','u',' ')
81          && p_fifo->i_fourcc != VLC_FOURCC('s','p','u','b')
82          && p_fifo->i_fourcc != VLC_FOURCC('s','u','b','t') )
83     {
84         return VLC_EGENERIC;
85     }
86
87     p_fifo->pf_run = RunDecoder;
88
89     return VLC_SUCCESS;
90 }
91
92 /*****************************************************************************
93  * RunDecoder: this function is called just after the thread is created
94  *****************************************************************************/
95 static int RunDecoder( decoder_fifo_t * p_fifo )
96 {
97     spudec_thread_t *     p_spudec;
98     subtitler_font_t *    p_font = NULL;
99     char *                psz_font;
100     vout_thread_t *       p_vout_backup = NULL;
101
102     /* Allocate the memory needed to store the thread's structure */
103     p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
104
105     if ( p_spudec == NULL )
106     {
107         msg_Err( p_fifo, "out of memory" );
108         DecoderError( p_fifo );
109         return( -1 );
110     }
111
112     /*
113      * Initialize the thread properties
114      */
115     p_spudec->p_vout = NULL;
116     p_spudec->p_fifo = p_fifo;
117
118     /*
119      * Initialize thread and free configuration
120      */
121     p_spudec->p_fifo->b_error = InitThread( p_spudec );
122
123     /*
124      * Main loop - it is not executed if an error occured during
125      * initialization
126      */
127     if( p_fifo->i_fourcc == VLC_FOURCC('s','u','b','t') )
128     {
129         /* Here we are dealing with text subtitles */
130
131 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
132         if ( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
133         {
134             char * psz_vlcpath = p_fifo->p_libvlc->psz_vlcpath;
135             psz_font = malloc( strlen(psz_vlcpath) + strlen("/share/")
136                                 + strlen(DEFAULT_FONT) + 1 );
137             sprintf(psz_font, "%s/share/" DEFAULT_FONT, psz_vlcpath);
138         }
139 #elif defined(WIN32) && !defined(UNDER_CE)
140         if ( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
141         {
142             char * psz_vlcpath = p_fifo->p_libvlc->psz_vlcpath;
143             psz_font = malloc( strlen(psz_vlcpath) + strlen("\\share\\")
144                                 + strlen(DEFAULT_FONT) + 1 );
145             sprintf(psz_font, "%s\\share\\" DEFAULT_FONT, psz_vlcpath);
146         }
147 #else
148         if( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
149         {
150             msg_Err( p_fifo, "no default font selected" );
151             p_spudec->p_fifo->b_error = VLC_TRUE;
152         }
153 #endif
154
155         while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
156         {
157             /* Find/Wait for a video output */
158             p_spudec->p_vout = FindVout( p_spudec );
159
160             if( p_spudec->p_vout )
161             {
162                 if( p_spudec->p_vout != p_vout_backup )
163                 {
164                     /* The vout has changed, we need to reload the fonts */
165                     p_vout_backup = p_spudec->p_vout;
166
167                     p_font = E_(subtitler_LoadFont)( p_spudec->p_vout,
168                                                      psz_font );
169                     if( p_font == NULL )
170                     {
171                         msg_Err( p_fifo, "unable to load font: %s", psz_font );
172                         p_spudec->p_fifo->b_error = VLC_TRUE;
173
174                         vlc_object_release( p_spudec->p_vout );
175                         break;
176                     }
177                 }
178
179                 E_(ParseText)( p_spudec, p_font );
180
181                 vlc_object_release( p_spudec->p_vout );
182             }
183         }
184
185         if( psz_font ) free( psz_font );
186
187         if( p_font )
188         {
189             /* Find/Wait for a video output */
190             p_spudec->p_vout = FindVout( p_spudec );
191
192             if( p_spudec->p_vout )
193             {
194                 E_(subtitler_UnloadFont)( p_spudec->p_vout, p_font );
195
196                 vlc_object_release( p_spudec->p_vout );
197             }
198         }
199     }
200     else
201     {
202         /* Here we are dealing with sub-pictures subtitles*/
203
204         while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
205         {
206             if( E_(SyncPacket)( p_spudec ) )
207             {
208                 continue;
209             }
210
211             /* Find/Wait for a video output */
212             p_spudec->p_vout = FindVout( p_spudec );
213             if( p_spudec->p_vout )
214             {
215                 E_(ParsePacket)( p_spudec );
216                 vlc_object_release( p_spudec->p_vout );
217             }
218         }
219     }
220
221     /*
222      * Error loop
223      */
224     if( p_spudec->p_fifo->b_error )
225     {
226         DecoderError( p_spudec->p_fifo );
227
228         /* End of thread */
229         EndThread( p_spudec );
230         return -1;
231     }
232
233     /* End of thread */
234     EndThread( p_spudec );
235     return 0;
236 }
237
238 /* following functions are local */
239
240 /*****************************************************************************
241  * InitThread: initialize spu decoder thread
242  *****************************************************************************
243  * This function is called from RunThread and performs the second step of the
244  * initialization. It returns 0 on success. Note that the thread's flag are not
245  * modified inside this function.
246  *****************************************************************************/
247 static int InitThread( spudec_thread_t *p_spudec )
248 {
249     int i_ret;
250
251     /* Call InitBitstream anyway so p_spudec->bit_stream is in a known
252      * state before calling CloseBitstream */
253     i_ret = InitBitstream( &p_spudec->bit_stream, p_spudec->p_fifo,
254                            NULL, NULL );
255
256     /* Check for a video output */
257     p_spudec->p_vout = FindVout( p_spudec );
258
259     if( !p_spudec->p_vout )
260     {
261         return -1;
262     }
263
264     /* It was just a check */
265     vlc_object_release( p_spudec->p_vout );
266     p_spudec->p_vout = NULL;
267
268     return i_ret;
269 }
270
271 /*****************************************************************************
272  * FindVout: Find a vout or wait for one to be created.
273  *****************************************************************************/
274 static vout_thread_t *FindVout( spudec_thread_t *p_spudec )
275 {
276     vout_thread_t *p_vout = NULL;
277
278     /* Find an available video output */
279     do
280     {
281         if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
282         {
283             break;
284         }
285
286         p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
287                                   FIND_ANYWHERE );
288
289         if( p_vout )
290         {
291             break;
292         }
293
294         msleep( VOUT_OUTMEM_SLEEP );
295     }
296     while( 1 );
297
298     return p_vout;
299 }
300
301 /*****************************************************************************
302  * EndThread: thread destruction
303  *****************************************************************************
304  * This function is called when the thread ends after a sucessful
305  * initialization.
306  *****************************************************************************/
307 static void EndThread( spudec_thread_t *p_spudec )
308 {
309     if( p_spudec->p_vout != NULL
310          && p_spudec->p_vout->p_subpicture != NULL )
311     {
312         subpicture_t *  p_subpic;
313         int             i_subpic;
314
315         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
316         {
317             p_subpic = &p_spudec->p_vout->p_subpicture[i_subpic];
318
319             if( p_subpic != NULL &&
320               ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
321              || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
322             {
323                 vout_DestroySubPicture( p_spudec->p_vout, p_subpic );
324             }
325         }
326     }
327
328     CloseBitstream( &p_spudec->bit_stream );
329     free( p_spudec );
330 }