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