]> git.sesse.net Git - vlc/blob - modules/codec/subsdec/subsdec.c
* subsdec now uses the new options code.
[vlc] / modules / codec / subsdec / subsdec.c
1 /*****************************************************************************
2  * subsdec.c : SPU decoder thread
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: subsdec.c,v 1.4 2003/07/25 01:11:32 hartman Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>                                    /* memcpy(), memset() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33 #include <vlc/decoder.h>
34 #include <osd.h>
35
36 #include "subsdec.h"
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  OpenDecoder   ( vlc_object_t * );
42 static int  RunDecoder    ( decoder_fifo_t * );
43 static int  InitThread    ( subsdec_thread_t * );
44 static void EndThread     ( subsdec_thread_t * );
45 static vout_thread_t *FindVout( subsdec_thread_t * );
46
47 /*****************************************************************************
48  * Module descriptor.
49  *****************************************************************************/
50 static char *ppsz_encodings[] = { "ASCII", "ISO-8859-1", "ISO-8859-2", "ISO-8859-3",
51     "ISO-8859-4", "ISO-8859-5", "ISO-8859-6", "ISO-8859-7", "ISO-8859-8", 
52     "ISO-8859-9", "ISO-8859-10", "ISO-8859-13", "ISO-8859-14", "ISO-8859-15",
53     "ISO-8859-16", "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "ISO-2022-CN",
54     "ISO-2022-CN-EXT", "ISO-2022-KR",
55     "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133",
56     "CP1250", "CP1251", "CP1252", "CP1253", "CP1254", "CP1255", "CP1256", "CP1257", "CP1258",
57     "MacRoman", "MacCentralEurope", "MacIceland", "MacCroatian", "MacRomania",
58     "MacCyrillic", "MacUkraine", "MacGreek", "MacTurkish", "MacHebrew", "MacArabic",
59     "MacThai", "Macintosh",
60     "UTF-7", "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
61     "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE",
62     "KOI8-R", "KOI8-U", "KOI8-RU", "KOI8-T",
63     "EUC-JP", "EUC-CN", "EUC-KR", "EUC-TW",
64     "SHIFT_JIS", "HZ", "GBK", "GB18030", "BIG5", "BIG5-HKSCS", "JOHAB", "ARMSCII-8",
65     "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
66     "HPROMAN8", "NEXTSTEP", NULL };
67
68 #define ENCODING_TEXT N_("subtitle text encoding")
69 #define ENCODING_LONGTEXT N_("change the encoding used in text subtitles")
70
71 vlc_module_begin();
72     set_description( _("file subtitles decoder") );
73     set_capability( "decoder", 50 );
74     set_callbacks( OpenDecoder, NULL );
75     add_category_hint( N_("Subtitles"), NULL, VLC_FALSE );
76
77 #if defined(HAVE_ICONV)
78     add_string_from_list( "subsdec-encoding", "ISO-8859-1", ppsz_encodings, NULL,
79                           ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
80 #endif
81 vlc_module_end();
82
83 /*****************************************************************************
84  * OpenDecoder: probe the decoder and return score
85  *****************************************************************************
86  * Tries to launch a decoder and return score so that the interface is able
87  * to chose.
88  *****************************************************************************/
89 static int OpenDecoder( vlc_object_t *p_this )
90 {
91     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
92
93     if( p_fifo->i_fourcc != VLC_FOURCC('s','u','b','t') )
94     {
95         return VLC_EGENERIC;
96     }
97
98     p_fifo->pf_run = RunDecoder;
99
100 #if defined(HAVE_ICONV)
101     var_Create( p_this, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
102 #endif
103     return VLC_SUCCESS;
104 }
105
106 /*****************************************************************************
107  * RunDecoder: this function is called just after the thread is created
108  *****************************************************************************/
109 static int RunDecoder( decoder_fifo_t * p_fifo )
110 {
111     subsdec_thread_t *    p_subsdec;
112     vlc_value_t           val;
113
114     /* Allocate the memory needed to store the thread's structure */
115     p_subsdec = (subsdec_thread_t *)malloc( sizeof(subsdec_thread_t) );
116
117     if ( p_subsdec == NULL )
118     {
119         msg_Err( p_fifo, "out of memory" );
120         DecoderError( p_fifo );
121         return( -1 );
122     }
123
124     /*
125      * Initialize the thread properties
126      */
127     p_subsdec->p_vout = NULL;
128     p_subsdec->p_fifo = p_fifo;
129 #if defined(HAVE_ICONV)
130     p_subsdec->iconv_handle = (iconv_t)-1;
131 #endif
132
133     /*
134      * Initialize thread and free configuration
135      */
136     p_subsdec->p_fifo->b_error = InitThread( p_subsdec );
137
138     /*
139      * Main loop - it is not executed if an error occured during
140      * initialization
141      */
142     if( p_fifo->i_fourcc == VLC_FOURCC('s','u','b','t') )
143     {
144         /* Here we are dealing with text subtitles */
145 #if defined(HAVE_ICONV)
146         var_Get( p_subsdec->p_fifo, "subsdec-encoding", &val );
147         p_subsdec->iconv_handle = iconv_open( "UTF-8", val.psz_string);
148         if( p_subsdec->iconv_handle == (iconv_t)-1 )
149         {
150             msg_Warn( p_subsdec->p_fifo, "Unable to do requested conversion" );
151         }
152         free( val.psz_string);
153 #endif
154         while( (!p_subsdec->p_fifo->b_die) && (!p_subsdec->p_fifo->b_error) )
155         {
156             /* Find/Wait for a video output */
157             p_subsdec->p_vout = FindVout( p_subsdec );
158
159             if( p_subsdec->p_vout )
160             {
161                 E_(ParseText)( p_subsdec );
162                 vlc_object_release( p_subsdec->p_vout );
163             }
164         }
165     }
166
167     /*
168      * Error loop
169      */
170     if( p_subsdec->p_fifo->b_error )
171     {
172         DecoderError( p_subsdec->p_fifo );
173
174         /* End of thread */
175         EndThread( p_subsdec );
176         return -1;
177     }
178
179     /* End of thread */
180     EndThread( p_subsdec );
181     return 0;
182 }
183
184 /* following functions are local */
185
186 /*****************************************************************************
187  * InitThread: initialize spu decoder thread
188  *****************************************************************************
189  * This function is called from RunThread and performs the second step of the
190  * initialization. It returns 0 on success. Note that the thread's flag are not
191  * modified inside this function.
192  *****************************************************************************/
193 static int InitThread( subsdec_thread_t *p_subsdec )
194 {
195     int i_ret;
196
197     /* Call InitBitstream anyway so p_subsdec->bit_stream is in a known
198      * state before calling CloseBitstream */
199     i_ret = InitBitstream( &p_subsdec->bit_stream, p_subsdec->p_fifo,
200                            NULL, NULL );
201
202     /* Check for a video output */
203     p_subsdec->p_vout = FindVout( p_subsdec );
204
205     if( !p_subsdec->p_vout )
206     {
207         return -1;
208     }
209
210     /* It was just a check */
211     vlc_object_release( p_subsdec->p_vout );
212     p_subsdec->p_vout = NULL;
213
214     return i_ret;
215 }
216
217 /*****************************************************************************
218  * FindVout: Find a vout or wait for one to be created.
219  *****************************************************************************/
220 static vout_thread_t *FindVout( subsdec_thread_t *p_subsdec )
221 {
222     vout_thread_t *p_vout = NULL;
223
224     /* Find an available video output */
225     do
226     {
227         if( p_subsdec->p_fifo->b_die || p_subsdec->p_fifo->b_error )
228         {
229             break;
230         }
231
232         p_vout = vlc_object_find( p_subsdec->p_fifo, VLC_OBJECT_VOUT,
233                                   FIND_ANYWHERE );
234
235         if( p_vout )
236         {
237             break;
238         }
239
240         msleep( VOUT_OUTMEM_SLEEP );
241     }
242     while( 1 );
243
244     return p_vout;
245 }
246
247 /*****************************************************************************
248  * EndThread: thread destruction
249  *****************************************************************************
250  * This function is called when the thread ends after a sucessful
251  * initialization.
252  *****************************************************************************/
253 static void EndThread( subsdec_thread_t *p_subsdec )
254 {
255     if( p_subsdec->p_vout != NULL
256          && p_subsdec->p_vout->p_subpicture != NULL )
257     {
258         subpicture_t *  p_subpic;
259         int             i_subpic;
260
261         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
262         {
263             p_subpic = &p_subsdec->p_vout->p_subpicture[i_subpic];
264
265             if( p_subpic != NULL &&
266               ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
267              || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
268             {
269                 vout_DestroySubPicture( p_subsdec->p_vout, p_subpic );
270             }
271         }
272     }
273 #if defined(HAVE_ICONV)
274     if( p_subsdec->iconv_handle != (iconv_t)-1 )
275     {
276         iconv_close( p_subsdec->iconv_handle );
277     }
278 #endif
279     CloseBitstream( &p_subsdec->bit_stream );
280     free( p_subsdec );
281 }
282
283 /*****************************************************************************
284  * ParseText: parse an text subtitle packet and send it to the video output
285  *****************************************************************************/
286 void E_(ParseText)( subsdec_thread_t *p_subsdec )
287 {
288     char         * psz_subtitle;
289     mtime_t        i_pts, i_dts;
290     /* We cannot display a subpicture with no date */
291     i_pts = p_subsdec->bit_stream.p_pes->i_pts;
292     i_dts = p_subsdec->bit_stream.p_pes->i_dts;
293     if( i_pts == 0 )
294     {
295         /* Dump the packet */
296         NextDataPacket( p_subsdec->p_fifo, &p_subsdec->bit_stream );
297         msg_Warn( p_subsdec->p_fifo, "subtitle without a date" );
298         return;
299     }
300
301     /* Check validity of packet data */
302     if( (p_subsdec->bit_stream.p_data->p_payload_end
303           - p_subsdec->bit_stream.p_data->p_payload_start) <= 0
304         || (strlen(p_subsdec->bit_stream.p_data->p_payload_start)
305             > (size_t)(p_subsdec->bit_stream.p_data->p_payload_end
306                         - p_subsdec->bit_stream.p_data->p_payload_start)) )
307     {
308         /* Dump the packet */
309         NextDataPacket( p_subsdec->p_fifo, &p_subsdec->bit_stream );
310         msg_Warn( p_subsdec->p_fifo, "invalid subtitle" );
311         return;
312     }
313     psz_subtitle = p_subsdec->bit_stream.p_data->p_payload_start;
314
315     if( psz_subtitle[0] != '\0' )
316     {
317 #if defined(HAVE_ICONV)
318         char *psz_new_subtitle, *psz_convert_buffer_out, *psz_convert_buffer_in;
319         size_t ret, inbytes_left, outbytes_left;
320
321         psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) * sizeof(char) );
322         psz_convert_buffer_out = psz_new_subtitle;
323         psz_convert_buffer_in = psz_subtitle;
324         inbytes_left = strlen( psz_subtitle );
325         outbytes_left = 6 * inbytes_left;
326         ret = iconv( p_subsdec->iconv_handle, &psz_convert_buffer_in,
327                      &inbytes_left, &psz_convert_buffer_out, &outbytes_left );
328         *psz_convert_buffer_out = '\0';
329
330         if( inbytes_left )
331         {
332             msg_Warn( p_subsdec->p_fifo, "Something fishy happened during conversion" );
333         }
334         else
335         {
336             msg_Dbg( p_subsdec->p_fifo, "reencoded \"%s\" into \"%s\"", psz_subtitle, psz_new_subtitle );
337             psz_subtitle = psz_new_subtitle;
338         }
339 #endif
340         vout_ShowTextAbsolute( p_subsdec->p_vout, psz_subtitle, NULL, 
341                                OSD_ALIGN_BOTTOM|OSD_ALIGN_LEFT, 20, 20, 
342                                i_pts, i_dts );
343 #if defined(HAVE_ICONV)
344         free( psz_new_subtitle );
345 #endif
346     }
347
348     /* Prepare for next time. No need to check that
349      * p_subsdec->bit_stream->p_data is valid since we check later on
350      * for b_die and b_error */
351     NextDataPacket( p_subsdec->p_fifo, &p_subsdec->bit_stream );
352 }