1 /*****************************************************************************
2 * spudec.c : SPU decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: spudec.c,v 1.13 2003/01/28 22:03:21 sam 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 * );
44 /*****************************************************************************
46 *****************************************************************************/
47 #define FONT_TEXT N_("font used by the text subtitler")
48 #define FONT_LONGTEXT N_(\
49 "When the subtitles are coded in text form then, you can choose " \
50 "which font will be used to display them.")
52 #define DEFAULT_FONT "font-eutopiabold36.rle"
55 add_category_hint( N_("subtitles"), NULL );
56 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
57 add_file( "spudec-font", NULL, NULL,
58 FONT_TEXT, FONT_LONGTEXT );
60 add_file( "spudec-font", "./share/" DEFAULT_FONT, NULL,
61 FONT_TEXT, FONT_LONGTEXT );
63 set_description( _("subtitles decoder module") );
64 set_capability( "decoder", 50 );
65 set_callbacks( OpenDecoder, NULL );
68 /*****************************************************************************
69 * OpenDecoder: probe the decoder and return score
70 *****************************************************************************
71 * Tries to launch a decoder and return score so that the interface is able
73 *****************************************************************************/
74 static int OpenDecoder( vlc_object_t *p_this )
76 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
78 if( p_fifo->i_fourcc != VLC_FOURCC('s','p','u',' ')
79 && p_fifo->i_fourcc != VLC_FOURCC('s','p','u','b')
80 && p_fifo->i_fourcc != VLC_FOURCC('s','u','b','t') )
85 p_fifo->pf_run = RunDecoder;
90 /*****************************************************************************
91 * RunDecoder: this function is called just after the thread is created
92 *****************************************************************************/
93 static int RunDecoder( decoder_fifo_t * p_fifo )
95 spudec_thread_t * p_spudec;
96 subtitler_font_t * p_font;
99 /* Allocate the memory needed to store the thread's structure */
100 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
102 if ( p_spudec == NULL )
104 msg_Err( p_fifo, "out of memory" );
105 DecoderError( p_fifo );
110 * Initialize the thread properties
112 p_spudec->p_vout = NULL;
113 p_spudec->p_fifo = p_fifo;
116 * Initialize thread and free configuration
118 p_spudec->p_fifo->b_error = InitThread( p_spudec );
121 * Main loop - it is not executed if an error occured during
124 if( p_fifo->i_fourcc == VLC_FOURCC('s','u','b','t') )
126 /* Here we are dealing with text subtitles */
128 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
129 if ( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
131 char * psz_vlcpath = p_fifo->p_libvlc->psz_vlcpath;
132 psz_font = malloc( strlen(psz_vlcpath) + strlen("/share/")
133 + strlen(DEFAULT_FONT) + 1 );
134 sprintf(psz_font, "%s/share/" DEFAULT_FONT, psz_vlcpath);
137 if( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
139 msg_Err( p_fifo, "no default font selected" );
141 p_spudec->p_fifo->b_error;
146 p_font = E_(subtitler_LoadFont)( p_spudec->p_vout, psz_font );
147 if ( p_font == NULL )
149 msg_Err( p_fifo, "unable to load font: %s", psz_font );
150 p_spudec->p_fifo->b_error;
153 if( psz_font ) free( psz_font );
155 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
157 E_(ParseText)( p_spudec, p_font );
160 if( p_font ) E_(subtitler_UnloadFont)( p_spudec->p_vout, p_font );
165 /* Here we are dealing with sub-pictures subtitles*/
167 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
169 if( E_(SyncPacket)( p_spudec ) )
174 E_(ParsePacket)( p_spudec );
181 if( p_spudec->p_fifo->b_error )
183 DecoderError( p_spudec->p_fifo );
186 EndThread( p_spudec );
191 EndThread( p_spudec );
195 /* following functions are local */
197 /*****************************************************************************
198 * InitThread: initialize spu decoder thread
199 *****************************************************************************
200 * This function is called from RunThread and performs the second step of the
201 * initialization. It returns 0 on success. Note that the thread's flag are not
202 * modified inside this function.
203 *****************************************************************************/
204 static int InitThread( spudec_thread_t *p_spudec )
206 /* Find an available video output */
209 if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
211 /* Call InitBitstream anyway so p_spudec->bit_stream is in a known
212 * state before calling CloseBitstream */
213 InitBitstream( &p_spudec->bit_stream, p_spudec->p_fifo,
218 p_spudec->p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
221 if( p_spudec->p_vout )
226 msleep( VOUT_OUTMEM_SLEEP );
230 return InitBitstream( &p_spudec->bit_stream, p_spudec->p_fifo,
234 /*****************************************************************************
235 * EndThread: thread destruction
236 *****************************************************************************
237 * This function is called when the thread ends after a sucessful
239 *****************************************************************************/
240 static void EndThread( spudec_thread_t *p_spudec )
242 if( p_spudec->p_vout != NULL
243 && p_spudec->p_vout->p_subpicture != NULL )
245 subpicture_t * p_subpic;
248 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
250 p_subpic = &p_spudec->p_vout->p_subpicture[i_subpic];
252 if( p_subpic != NULL &&
253 ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
254 || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
256 vout_DestroySubPicture( p_spudec->p_vout, p_subpic );
260 vlc_object_release( p_spudec->p_vout );
263 CloseBitstream( &p_spudec->bit_stream );