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 $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* memcpy(), memset() */
32 #include <vlc/decoder.h>
36 /*****************************************************************************
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 * );
45 /*****************************************************************************
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.")
53 #define DEFAULT_FONT "font-eutopiabold21.rle"
55 #define ENCODING_TEXT N_("subtitle text encoding")
56 #define ENCODING_LONGTEXT N_("change the encoding used in text subtitles")
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 );
65 add_file( "spudec-font", "share/" DEFAULT_FONT, NULL,
66 FONT_TEXT, FONT_LONGTEXT, VLC_TRUE );
68 #if defined(HAVE_ICONV)
69 add_string( "spudec-encoding", "ISO-8859-1", NULL, ENCODING_TEXT,
70 ENCODING_LONGTEXT, VLC_FALSE );
72 set_description( _("subtitles decoder") );
73 set_capability( "decoder", 50 );
74 set_callbacks( OpenDecoder, NULL );
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
82 *****************************************************************************/
83 static int OpenDecoder( vlc_object_t *p_this )
85 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
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') )
94 p_fifo->pf_run = RunDecoder;
99 /*****************************************************************************
100 * RunDecoder: this function is called just after the thread is created
101 *****************************************************************************/
102 static int RunDecoder( decoder_fifo_t * p_fifo )
104 spudec_thread_t * p_spudec;
105 subtitler_font_t * p_font = NULL;
107 vout_thread_t * p_vout_backup = NULL;
109 /* Allocate the memory needed to store the thread's structure */
110 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
112 if ( p_spudec == NULL )
114 msg_Err( p_fifo, "out of memory" );
115 DecoderError( p_fifo );
120 * Initialize the thread properties
122 p_spudec->p_vout = NULL;
123 p_spudec->p_fifo = p_fifo;
124 p_spudec->iconv_handle = (iconv_t)-1;
127 * Initialize thread and free configuration
129 p_spudec->p_fifo->b_error = InitThread( p_spudec );
132 * Main loop - it is not executed if an error occured during
135 if( p_fifo->i_fourcc == VLC_FOURCC('s','u','b','t') )
137 /* Here we are dealing with text subtitles */
139 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
140 if ( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
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);
147 #elif defined(WIN32) && !defined(UNDER_CE)
148 if ( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
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);
156 if( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
158 msg_Err( p_fifo, "no default font selected" );
159 p_spudec->p_fifo->b_error = VLC_TRUE;
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 )
167 msg_Warn( p_spudec->p_fifo, "Unable to do requested conversion" );
170 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
172 /* Find/Wait for a video output */
173 p_spudec->p_vout = FindVout( p_spudec );
175 if( p_spudec->p_vout )
177 if( p_spudec->p_vout != p_vout_backup )
179 /* The vout has changed, we need to reload the fonts */
180 p_vout_backup = p_spudec->p_vout;
182 p_font = E_(subtitler_LoadFont)( p_spudec->p_vout,
186 msg_Err( p_fifo, "unable to load font: %s", psz_font );
187 p_spudec->p_fifo->b_error = VLC_TRUE;
189 vlc_object_release( p_spudec->p_vout );
194 E_(ParseText)( p_spudec, p_font );
196 vlc_object_release( p_spudec->p_vout );
200 if( psz_font ) free( psz_font );
204 /* Find/Wait for a video output */
205 p_spudec->p_vout = FindVout( p_spudec );
207 if( p_spudec->p_vout )
209 E_(subtitler_UnloadFont)( p_spudec->p_vout, p_font );
211 vlc_object_release( p_spudec->p_vout );
217 /* Here we are dealing with sub-pictures subtitles*/
219 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
221 if( E_(SyncPacket)( p_spudec ) )
226 /* Find/Wait for a video output */
227 p_spudec->p_vout = FindVout( p_spudec );
228 if( p_spudec->p_vout )
230 E_(ParsePacket)( p_spudec );
231 vlc_object_release( p_spudec->p_vout );
239 if( p_spudec->p_fifo->b_error )
241 DecoderError( p_spudec->p_fifo );
244 EndThread( p_spudec );
249 EndThread( p_spudec );
253 /* following functions are local */
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 )
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,
271 /* Check for a video output */
272 p_spudec->p_vout = FindVout( p_spudec );
274 if( !p_spudec->p_vout )
279 /* It was just a check */
280 vlc_object_release( p_spudec->p_vout );
281 p_spudec->p_vout = NULL;
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 )
291 vout_thread_t *p_vout = NULL;
293 /* Find an available video output */
296 if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
301 p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
309 msleep( VOUT_OUTMEM_SLEEP );
316 /*****************************************************************************
317 * EndThread: thread destruction
318 *****************************************************************************
319 * This function is called when the thread ends after a sucessful
321 *****************************************************************************/
322 static void EndThread( spudec_thread_t *p_spudec )
324 if( p_spudec->p_vout != NULL
325 && p_spudec->p_vout->p_subpicture != NULL )
327 subpicture_t * p_subpic;
330 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
332 p_subpic = &p_spudec->p_vout->p_subpicture[i_subpic];
334 if( p_subpic != NULL &&
335 ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
336 || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
338 vout_DestroySubPicture( p_spudec->p_vout, p_subpic );
342 #if defined(HAVE_ICONV)
343 if( p_spudec->iconv_handle != (iconv_t)-1 )
345 iconv_close( p_spudec->iconv_handle );
348 CloseBitstream( &p_spudec->bit_stream );