]> git.sesse.net Git - vlc/blob - modules/codec/spudec/spudec.c
599606e46806f56564c40fa0354ea8b4150eba8f
[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.12 2003/01/20 02:45:25 titer 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
44 /*****************************************************************************
45  * Module descriptor.
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.")
51
52 #define DEFAULT_FONT "font-eutopiabold36.rle"
53
54 vlc_module_begin();
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 );
59 #else
60     add_file( "spudec-font", "./share/" DEFAULT_FONT, NULL,
61               FONT_TEXT, FONT_LONGTEXT );
62 #endif
63     set_description( _("subtitles decoder module") );
64     set_capability( "decoder", 50 );
65     set_callbacks( OpenDecoder, NULL );
66 vlc_module_end();
67
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 
72  * to chose.
73  *****************************************************************************/
74 static int OpenDecoder( vlc_object_t *p_this )
75 {
76     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
77
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') )
81     {
82         return VLC_EGENERIC;
83     }
84
85     p_fifo->pf_run = RunDecoder;
86
87     return VLC_SUCCESS;
88 }
89
90 /*****************************************************************************
91  * RunDecoder: this function is called just after the thread is created
92  *****************************************************************************/
93 static int RunDecoder( decoder_fifo_t * p_fifo )
94 {
95     spudec_thread_t *     p_spudec;
96     subtitler_font_t *    p_font;
97     char *                psz_font;
98
99     /* Allocate the memory needed to store the thread's structure */
100     p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
101
102     if ( p_spudec == NULL )
103     {
104         msg_Err( p_fifo, "out of memory" );
105         DecoderError( p_fifo );
106         return( -1 );
107     }
108     
109     /*
110      * Initialize the thread properties
111      */
112     p_spudec->p_vout = NULL;
113     p_spudec->p_fifo = p_fifo;
114
115     /*
116      * Initialize thread and free configuration
117      */
118     p_spudec->p_fifo->b_error = InitThread( p_spudec );
119
120     /*
121      * Main loop - it is not executed if an error occured during
122      * initialization
123      */
124     if( p_fifo->i_fourcc == VLC_FOURCC('s','u','b','t') )
125     {
126         /* Here we are dealing with text subtitles */
127
128 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
129         if ( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
130         {
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);
135         }
136 #else
137         if( (psz_font = config_GetPsz( p_fifo, "spudec-font" )) == NULL )
138         {
139             msg_Err( p_fifo, "no default font selected" );
140             p_font = NULL;
141             p_spudec->p_fifo->b_error;
142         }
143         else
144 #endif
145         {
146             p_font = E_(subtitler_LoadFont)( p_spudec->p_vout, psz_font );
147             if ( p_font == NULL )
148             {
149                 msg_Err( p_fifo, "unable to load font: %s", psz_font );
150                 p_spudec->p_fifo->b_error;
151             }
152         }
153         if( psz_font ) free( psz_font );
154
155         while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
156         {
157             E_(ParseText)( p_spudec, p_font );
158         }
159
160         if( p_font ) E_(subtitler_UnloadFont)( p_spudec->p_vout, p_font );
161
162     }
163     else
164     {
165         /* Here we are dealing with sub-pictures subtitles*/
166
167         while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
168         {
169             if( E_(SyncPacket)( p_spudec ) )
170             {
171                 continue;
172             }
173
174             E_(ParsePacket)( p_spudec );
175         }
176     }
177
178     /*
179      * Error loop
180      */
181     if( p_spudec->p_fifo->b_error )
182     {
183         DecoderError( p_spudec->p_fifo );
184
185         /* End of thread */
186         EndThread( p_spudec );
187         return -1;
188     }
189
190     /* End of thread */
191     EndThread( p_spudec );
192     return 0;
193 }
194
195 /* following functions are local */
196
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 )
205 {
206     /* Find an available video output */
207     do
208     {
209         if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
210         {
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,
214                            NULL, NULL );
215             return -1;
216         }
217
218         p_spudec->p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
219                                                               FIND_ANYWHERE );
220
221         if( p_spudec->p_vout )
222         {
223             break;
224         }
225
226         msleep( VOUT_OUTMEM_SLEEP );
227     }
228     while( 1 );
229
230     return InitBitstream( &p_spudec->bit_stream, p_spudec->p_fifo,
231                           NULL, NULL );
232 }
233
234 /*****************************************************************************
235  * EndThread: thread destruction
236  *****************************************************************************
237  * This function is called when the thread ends after a sucessful
238  * initialization.
239  *****************************************************************************/
240 static void EndThread( spudec_thread_t *p_spudec )
241 {
242     if( p_spudec->p_vout != NULL 
243          && p_spudec->p_vout->p_subpicture != NULL )
244     {
245         subpicture_t *  p_subpic;
246         int             i_subpic;
247     
248         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
249         {
250             p_subpic = &p_spudec->p_vout->p_subpicture[i_subpic];
251
252             if( p_subpic != NULL &&
253               ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
254              || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
255             {
256                 vout_DestroySubPicture( p_spudec->p_vout, p_subpic );
257             }
258         }
259
260         vlc_object_release( p_spudec->p_vout );
261     }
262     
263     CloseBitstream( &p_spudec->bit_stream );
264     free( p_spudec );
265 }