]> git.sesse.net Git - vlc/blob - modules/codec/subsdec.c
Please test if this has any regressions
[vlc] / modules / codec / subsdec.c
1 /*****************************************************************************
2  * subsdec.c : text subtitles decoder
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: subsdec.c,v 1.4 2003/11/05 00:17:50 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 #include <codecs.h>
36
37 #if defined(HAVE_ICONV)
38 #include <iconv.h>
39 #endif
40
41 #include "charset.h"
42
43 /*****************************************************************************
44  * decoder_sys_t : decoder descriptor
45  *****************************************************************************/
46 struct decoder_sys_t
47 {
48     int                 i_align;          /* Subtitles alignment on the vout */
49
50 #if defined(HAVE_ICONV)
51     iconv_t             iconv_handle;            /* handle to iconv instance */
52 #endif
53 };
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int  OpenDecoder   ( vlc_object_t * );
59
60 static int  InitDecoder   ( decoder_t * );
61 static int  RunDecoder    ( decoder_t *, block_t * );
62 static int  EndDecoder    ( decoder_t * );
63
64 static void ParseText     ( decoder_t *, block_t *, vout_thread_t * );
65
66 #define DEFAULT_NAME "System Default"
67
68 /*****************************************************************************
69  * Module descriptor.
70  *****************************************************************************/
71 #if defined(HAVE_ICONV)
72 static char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
73     "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
74     "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
75     "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
76     "ISO-8859-6", "CP1256", "MacArabic", "",
77     "ISO-8859-7", "CP1253", "MacGreek", "",
78     "ISO-8859-8", "CP1255", "MacHebrew", "",
79     "ISO-8859-9", "CP1254", "MacTurkish", "",
80     "ISO-8859-13", "CP1257", "",
81     "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
82     "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
83     "ISO-2022-KR", "EUC-KR", "",
84     "MacThai", "KOI8-T", "",
85     "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
86     "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
87     "Macintosh", "",
88     "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
89     "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
90     "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
91     "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
92     "HPROMAN8", "NEXTSTEP", NULL };
93 #endif
94
95 #define ENCODING_TEXT N_("Subtitles text encoding")
96 #define ENCODING_LONGTEXT N_("Change the encoding used in text subtitles")
97 #define ALIGN_TEXT N_("Subtitles justification")
98 #define ALIGN_LONGTEXT N_("Change the justification of substitles (0=center, 1=left, 2=right)")
99
100 vlc_module_begin();
101     set_description( _("text subtitles decoder") );
102     set_capability( "decoder", 50 );
103     set_callbacks( OpenDecoder, NULL );
104
105     add_category_hint( N_("Subtitles"), NULL, VLC_FALSE );
106     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT, VLC_TRUE );
107 #if defined(HAVE_ICONV)
108     add_string_from_list( "subsdec-encoding", DEFAULT_NAME, ppsz_encodings, NULL, ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
109 #endif
110 vlc_module_end();
111
112 /*****************************************************************************
113  * OpenDecoder: probe the decoder and return score
114  *****************************************************************************
115  * Tries to launch a decoder and return score so that the interface is able
116  * to chose.
117  *****************************************************************************/
118 static int OpenDecoder( vlc_object_t *p_this )
119 {
120     decoder_t *p_dec = (decoder_t*)p_this;
121
122     if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('s','u','b','t') && 
123         p_dec->p_fifo->i_fourcc != VLC_FOURCC('s','s','a',' ') )
124     {
125         return VLC_EGENERIC;
126     }
127
128     p_dec->pf_init = InitDecoder;
129     p_dec->pf_decode = RunDecoder;
130     p_dec->pf_end = EndDecoder;
131
132     /* Allocate the memory needed to store the decoder's structure */
133     if( ( p_dec->p_sys =
134           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
135     {
136         msg_Err( p_dec, "out of memory" );
137         return VLC_EGENERIC;
138     }
139
140     return VLC_SUCCESS;
141 }
142
143 /*****************************************************************************
144  * InitDecoder: Initalize the decoder
145  *****************************************************************************/
146 static int InitDecoder( decoder_t *p_dec )
147 {
148     decoder_sys_t *p_sys = p_dec->p_sys;
149     subtitle_data_t *p_demux_data = (subtitle_data_t *)p_dec->p_fifo->p_demux_data;
150     vlc_value_t val;
151
152     var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
153     var_Get( p_dec, "subsdec-align", &val );
154     p_sys->i_align = val.i_int;
155
156 #if defined(HAVE_ICONV)
157     var_Create( p_dec, "subsdec-encoding",
158                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
159     var_Get( p_dec, "subsdec-encoding", &val );
160     if( !strcmp( val.psz_string, DEFAULT_NAME ) )
161     {
162         char *psz_charset =(char*)malloc( 100 );
163         vlc_current_charset( &psz_charset );
164         p_sys->iconv_handle = iconv_open( "UTF-8", psz_charset );
165         free( psz_charset );
166     }
167     else
168     {
169         p_sys->iconv_handle = iconv_open( "UTF-8", val.psz_string );
170     }
171
172     if( p_sys->iconv_handle == (iconv_t)-1 )
173     {
174         msg_Warn( p_dec, "Unable to do requested conversion" );
175     }
176
177     if( val.psz_string ) free( val.psz_string );
178 #else
179     msg_Dbg( p_dec, "No iconv support available" );
180 #endif
181
182 #if 1
183     if( p_demux_data )
184         msg_Dbg( p_dec, p_demux_data->psz_header );
185 #endif
186
187     return VLC_SUCCESS;
188 }
189
190 /****************************************************************************
191  * RunDecoder: the whole thing
192  ****************************************************************************
193  * This function must be fed with complete subtitles units.
194  ****************************************************************************/
195 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
196 {
197     vout_thread_t *p_vout;
198
199     /* Here we are dealing with text subtitles */
200     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
201     if( !p_vout )
202     {
203         msg_Warn( p_dec, "couldn't find a video output, trashing subtitle" );
204         return VLC_SUCCESS;
205     }
206
207     ParseText( p_dec, p_block, p_vout );
208     vlc_object_release( p_vout );
209
210     return VLC_SUCCESS;
211 }
212
213 /*****************************************************************************
214  * EndDecoder: clean up the decoder
215  *****************************************************************************/
216 static int EndDecoder( decoder_t *p_dec )
217 {
218     decoder_sys_t *p_sys = p_dec->p_sys;
219     vout_thread_t *p_vout;
220
221     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
222     if( p_vout != NULL && p_vout->p_subpicture != NULL )
223     {
224         subpicture_t *p_subpic;
225         int          i_subpic;
226
227         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
228         {
229             p_subpic = &p_vout->p_subpicture[i_subpic];
230
231             if( p_subpic != NULL &&
232               ( p_subpic->i_status == RESERVED_SUBPICTURE
233                 || p_subpic->i_status == READY_SUBPICTURE ) )
234             {
235                 vout_DestroySubPicture( p_vout, p_subpic );
236             }
237         }
238     }
239     if( p_vout ) vlc_object_release( p_vout );
240
241 #if defined(HAVE_ICONV)
242     if( p_sys->iconv_handle != (iconv_t)-1 )
243     {
244         iconv_close( p_sys->iconv_handle );
245     }
246 #endif
247
248     free( p_sys );
249
250     return VLC_SUCCESS;
251 }
252
253 /*****************************************************************************
254  * ParseText: parse an text subtitle packet and send it to the video output
255  *****************************************************************************/
256 static void ParseText( decoder_t *p_dec, block_t *p_block,
257                        vout_thread_t *p_vout )
258 {
259     decoder_sys_t *p_sys = p_dec->p_sys;
260     char *psz_subtitle;
261     int i_align_h, i_align_v;
262
263     /* We cannot display a subpicture with no date */
264     if( p_block->i_pts == 0 )
265     {
266         msg_Warn( p_dec, "subtitle without a date" );
267         return;
268     }
269
270     /* Check validity of packet data */
271     if( p_block->i_buffer <= 1 ||  p_block->p_buffer[0] == '\0' )
272     {
273         msg_Warn( p_dec, "empty subtitle" );
274         return;
275     }
276
277     /* Should be resiliant against bad subtitles */
278     psz_subtitle = strndup( p_block->p_buffer, p_block->i_buffer );
279     
280     i_align_h = p_sys->i_align ? 20 : 0;
281     i_align_v = 10;
282
283 #if defined(HAVE_ICONV)
284     if( p_sys->iconv_handle != (iconv_t)-1 )
285     {
286         char *psz_new_subtitle;
287         char *psz_convert_buffer_out;
288         const char *psz_convert_buffer_in;
289         size_t ret, inbytes_left, outbytes_left;
290
291         psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) );
292         psz_convert_buffer_out = psz_new_subtitle;
293         psz_convert_buffer_in = psz_subtitle;
294         inbytes_left = strlen( psz_subtitle );
295         outbytes_left = 6 * inbytes_left;
296         ret = iconv( p_sys->iconv_handle, &psz_convert_buffer_in,
297                      &inbytes_left, &psz_convert_buffer_out, &outbytes_left );
298         *psz_convert_buffer_out = '\0';
299
300         if( inbytes_left )
301         {
302             msg_Warn( p_dec, "Something fishy happened during conversion" );
303         }
304         else
305         {
306             free( psz_subtitle );
307             psz_subtitle = psz_new_subtitle;
308         }
309     }
310 #endif
311
312     if( p_dec->p_fifo->i_fourcc == VLC_FOURCC('s','s','a',' ') )
313     {
314         /* Decode SSA strings */
315         /* We expect: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
316         char *psz_new_subtitle;
317         char *psz_buffer_sub;
318         int         i_comma;
319         int         i_text;
320
321         psz_buffer_sub = psz_subtitle;
322         for( ;; )
323         {
324             i_comma = 0;
325             while( i_comma < 8 &&
326                 *psz_buffer_sub != '\0' )
327             {
328                 if( *psz_buffer_sub == ',' )
329                 {
330                     i_comma++;
331                 }
332                 psz_buffer_sub++;
333             }
334             psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
335             i_text = 0;
336             while( psz_buffer_sub[0] != '\0' )
337             {
338                 if( psz_buffer_sub[0] == '\\' && ( psz_buffer_sub[1] =='n' || psz_buffer_sub[1] =='N' ) )
339                 {
340                     psz_new_subtitle[i_text] = '\n';
341                     i_text++;
342                     psz_buffer_sub += 2;
343                 }
344                 else if( psz_buffer_sub[0] == '{' && psz_buffer_sub[1] == '\\' )
345                 {
346                     /* SSA control code */
347                     while( psz_buffer_sub[0] != '\0' && psz_buffer_sub[0] != '}' )
348                     {
349                         psz_buffer_sub++;
350                     }
351                     psz_buffer_sub++;
352                 }
353                 else
354                 {
355                     psz_new_subtitle[i_text] = psz_buffer_sub[0];
356                     i_text++;
357                     psz_buffer_sub++;
358                 }
359             }
360             psz_new_subtitle[i_text] = '\0';
361             free( psz_subtitle );
362             psz_subtitle = psz_new_subtitle;
363             break;
364         }
365     }
366
367     vout_ShowTextAbsolute( p_vout, psz_subtitle, NULL, 
368                            OSD_ALIGN_BOTTOM | p_sys->i_align,
369                            i_align_h, i_align_v, 
370                            p_block->i_pts, p_block->i_dts );
371
372     free( psz_subtitle );
373 }