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