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 $
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 );
57 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
58 add_file( "spudec-font", NULL, NULL,
59 FONT_TEXT, FONT_LONGTEXT );
61 add_file( "spudec-font", "./share/" DEFAULT_FONT, NULL,
62 FONT_TEXT, FONT_LONGTEXT );
64 set_description( _("subtitles decoder module") );
65 set_capability( "decoder", 50 );
66 set_callbacks( OpenDecoder, NULL );
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
74 *****************************************************************************/
75 static int OpenDecoder( vlc_object_t *p_this )
77 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
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') )
86 p_fifo->pf_run = RunDecoder;
91 /*****************************************************************************
92 * RunDecoder: this function is called just after the thread is created
93 *****************************************************************************/
94 static int RunDecoder( decoder_fifo_t * p_fifo )
96 spudec_thread_t * p_spudec;
97 subtitler_font_t * p_font = NULL;
99 vout_thread_t * p_vout_backup = NULL;
101 /* Allocate the memory needed to store the thread's structure */
102 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
104 if ( p_spudec == NULL )
106 msg_Err( p_fifo, "out of memory" );
107 DecoderError( p_fifo );
112 * Initialize the thread properties
114 p_spudec->p_vout = NULL;
115 p_spudec->p_fifo = p_fifo;
118 * Initialize thread and free configuration
120 p_spudec->p_fifo->b_error = InitThread( p_spudec );
123 * Main loop - it is not executed if an error occured during
126 if( p_fifo->i_fourcc == VLC_FOURCC('s','u','b','t') )
128 /* Here we are dealing with text subtitles */
130 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
131 if ( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
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);
139 if( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
141 msg_Err( p_fifo, "no default font selected" );
142 p_spudec->p_fifo->b_error = VLC_TRUE;
146 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
148 /* Find/Wait for a video output */
149 p_spudec->p_vout = FindVout( p_spudec );
151 if( p_spudec->p_vout )
153 if( p_spudec->p_vout != p_vout_backup )
155 /* The vout has changed, we need to reload the fonts */
156 p_vout_backup = p_spudec->p_vout;
158 p_font = E_(subtitler_LoadFont)( p_spudec->p_vout,
162 msg_Err( p_fifo, "unable to load font: %s", psz_font );
163 p_spudec->p_fifo->b_error = VLC_TRUE;
165 vlc_object_release( p_spudec->p_vout );
170 E_(ParseText)( p_spudec, p_font );
172 vlc_object_release( p_spudec->p_vout );
176 if( psz_font ) free( psz_font );
180 /* Find/Wait for a video output */
181 p_spudec->p_vout = FindVout( p_spudec );
183 if( p_spudec->p_vout )
185 E_(subtitler_UnloadFont)( p_spudec->p_vout, p_font );
187 vlc_object_release( p_spudec->p_vout );
193 /* Here we are dealing with sub-pictures subtitles*/
195 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
197 if( E_(SyncPacket)( p_spudec ) )
202 /* Find/Wait for a video output */
203 p_spudec->p_vout = FindVout( p_spudec );
204 if( p_spudec->p_vout )
206 E_(ParsePacket)( p_spudec );
207 vlc_object_release( p_spudec->p_vout );
215 if( p_spudec->p_fifo->b_error )
217 DecoderError( p_spudec->p_fifo );
220 EndThread( p_spudec );
225 EndThread( p_spudec );
229 /* following functions are local */
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 )
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,
247 /* Check for a video output */
248 p_spudec->p_vout = FindVout( p_spudec );
250 if( !p_spudec->p_vout )
255 /* It was just a check */
256 vlc_object_release( p_spudec->p_vout );
257 p_spudec->p_vout = NULL;
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 )
267 vout_thread_t *p_vout = NULL;
269 /* Find an available video output */
272 if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
277 p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
285 msleep( VOUT_OUTMEM_SLEEP );
292 /*****************************************************************************
293 * EndThread: thread destruction
294 *****************************************************************************
295 * This function is called when the thread ends after a sucessful
297 *****************************************************************************/
298 static void EndThread( spudec_thread_t *p_spudec )
300 if( p_spudec->p_vout != NULL
301 && p_spudec->p_vout->p_subpicture != NULL )
303 subpicture_t * p_subpic;
306 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
308 p_subpic = &p_spudec->p_vout->p_subpicture[i_subpic];
310 if( p_subpic != NULL &&
311 ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
312 || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
314 vout_DestroySubPicture( p_spudec->p_vout, p_subpic );
319 CloseBitstream( &p_spudec->bit_stream );