1 /*****************************************************************************
2 * spudec.c : SPU decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: spudec.c,v 1.17 2003/03/30 18:14:37 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-eutopiabold36.rle"
56 add_category_hint( N_("subtitles"), NULL, VLC_TRUE );
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, VLC_TRUE );
62 add_file( "spudec-font", "share/" DEFAULT_FONT, NULL,
63 FONT_TEXT, FONT_LONGTEXT, VLC_TRUE );
65 set_description( _("subtitles decoder") );
66 set_capability( "decoder", 50 );
67 set_callbacks( OpenDecoder, NULL );
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
75 *****************************************************************************/
76 static int OpenDecoder( vlc_object_t *p_this )
78 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
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') )
87 p_fifo->pf_run = RunDecoder;
92 /*****************************************************************************
93 * RunDecoder: this function is called just after the thread is created
94 *****************************************************************************/
95 static int RunDecoder( decoder_fifo_t * p_fifo )
97 spudec_thread_t * p_spudec;
98 subtitler_font_t * p_font = NULL;
100 vout_thread_t * p_vout_backup = NULL;
102 /* Allocate the memory needed to store the thread's structure */
103 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
105 if ( p_spudec == NULL )
107 msg_Err( p_fifo, "out of memory" );
108 DecoderError( p_fifo );
113 * Initialize the thread properties
115 p_spudec->p_vout = NULL;
116 p_spudec->p_fifo = p_fifo;
119 * Initialize thread and free configuration
121 p_spudec->p_fifo->b_error = InitThread( p_spudec );
124 * Main loop - it is not executed if an error occured during
127 if( p_fifo->i_fourcc == VLC_FOURCC('s','u','b','t') )
129 /* Here we are dealing with text subtitles */
131 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
132 if ( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
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);
139 #elif defined(WIN32) && !defined(UNDER_CE)
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);
148 if( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
150 msg_Err( p_fifo, "no default font selected" );
151 p_spudec->p_fifo->b_error = VLC_TRUE;
155 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
157 /* Find/Wait for a video output */
158 p_spudec->p_vout = FindVout( p_spudec );
160 if( p_spudec->p_vout )
162 if( p_spudec->p_vout != p_vout_backup )
164 /* The vout has changed, we need to reload the fonts */
165 p_vout_backup = p_spudec->p_vout;
167 p_font = E_(subtitler_LoadFont)( p_spudec->p_vout,
171 msg_Err( p_fifo, "unable to load font: %s", psz_font );
172 p_spudec->p_fifo->b_error = VLC_TRUE;
174 vlc_object_release( p_spudec->p_vout );
179 E_(ParseText)( p_spudec, p_font );
181 vlc_object_release( p_spudec->p_vout );
185 if( psz_font ) free( psz_font );
189 /* Find/Wait for a video output */
190 p_spudec->p_vout = FindVout( p_spudec );
192 if( p_spudec->p_vout )
194 E_(subtitler_UnloadFont)( p_spudec->p_vout, p_font );
196 vlc_object_release( p_spudec->p_vout );
202 /* Here we are dealing with sub-pictures subtitles*/
204 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
206 if( E_(SyncPacket)( p_spudec ) )
211 /* Find/Wait for a video output */
212 p_spudec->p_vout = FindVout( p_spudec );
213 if( p_spudec->p_vout )
215 E_(ParsePacket)( p_spudec );
216 vlc_object_release( p_spudec->p_vout );
224 if( p_spudec->p_fifo->b_error )
226 DecoderError( p_spudec->p_fifo );
229 EndThread( p_spudec );
234 EndThread( p_spudec );
238 /* following functions are local */
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 )
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,
256 /* Check for a video output */
257 p_spudec->p_vout = FindVout( p_spudec );
259 if( !p_spudec->p_vout )
264 /* It was just a check */
265 vlc_object_release( p_spudec->p_vout );
266 p_spudec->p_vout = NULL;
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 )
276 vout_thread_t *p_vout = NULL;
278 /* Find an available video output */
281 if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
286 p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
294 msleep( VOUT_OUTMEM_SLEEP );
301 /*****************************************************************************
302 * EndThread: thread destruction
303 *****************************************************************************
304 * This function is called when the thread ends after a sucessful
306 *****************************************************************************/
307 static void EndThread( spudec_thread_t *p_spudec )
309 if( p_spudec->p_vout != NULL
310 && p_spudec->p_vout->p_subpicture != NULL )
312 subpicture_t * p_subpic;
315 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
317 p_subpic = &p_spudec->p_vout->p_subpicture[i_subpic];
319 if( p_subpic != NULL &&
320 ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
321 || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
323 vout_DestroySubPicture( p_spudec->p_vout, p_subpic );
328 CloseBitstream( &p_spudec->bit_stream );