]> git.sesse.net Git - vlc/blob - modules/codec/subsdec.c
string review
[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.16 2004/01/25 18:20:12 bigben 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 <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include <vlc/decoder.h>
31
32 #include <osd.h>
33
34 #if defined(HAVE_ICONV)
35 #include <iconv.h>
36 #endif
37
38 #include "charset.h"
39
40 /*****************************************************************************
41  * decoder_sys_t : decoder descriptor
42  *****************************************************************************/
43 struct decoder_sys_t
44 {
45     int                 i_align;          /* Subtitles alignment on the vout */
46
47 #if defined(HAVE_ICONV)
48     iconv_t             iconv_handle;            /* handle to iconv instance */
49 #endif
50 };
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int  OpenDecoder   ( vlc_object_t * );
56 static void CloseDecoder  ( vlc_object_t * );
57
58 static void DecodeBlock   ( decoder_t *, block_t ** );
59
60 static void ParseText     ( decoder_t *, block_t *, vout_thread_t * );
61
62 #define DEFAULT_NAME "System Default"
63
64 /*****************************************************************************
65  * Module descriptor.
66  *****************************************************************************/
67 #if defined(HAVE_ICONV)
68 static char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
69     "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
70     "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
71     "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
72     "ISO-8859-6", "CP1256", "MacArabic", "",
73     "ISO-8859-7", "CP1253", "MacGreek", "",
74     "ISO-8859-8", "CP1255", "MacHebrew", "",
75     "ISO-8859-9", "CP1254", "MacTurkish", "",
76     "ISO-8859-13", "CP1257", "",
77     "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
78     "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
79     "ISO-2022-KR", "EUC-KR", "",
80     "MacThai", "KOI8-T", "",
81     "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
82     "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
83     "Macintosh", "",
84     "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
85     "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
86     "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
87     "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
88     "HPROMAN8", "NEXTSTEP" };
89 #endif
90
91 static int  pi_justification[] = { 0, 1, 2 };
92 static char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
93
94 #define ENCODING_TEXT N_("Subtitles text encoding")
95 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
96 #define ALIGN_TEXT N_("Subtitles justification")
97 #define ALIGN_LONGTEXT N_("Set the justification of substitles")
98
99 vlc_module_begin();
100     set_description( _("text subtitles decoder") );
101     set_capability( "decoder", 50 );
102     set_callbacks( OpenDecoder, CloseDecoder );
103
104     add_category_hint( N_("Subtitles"), NULL, VLC_FALSE );
105     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
106                  VLC_TRUE );
107         change_integer_list( pi_justification, ppsz_justification_text, 0 );
108 #if defined(HAVE_ICONV)
109     add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
110                 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
111         change_string_list( ppsz_encodings, 0, 0 );
112 #endif
113 vlc_module_end();
114
115 /*****************************************************************************
116  * OpenDecoder: probe the decoder and return score
117  *****************************************************************************
118  * Tries to launch a decoder and return score so that the interface is able
119  * to chose.
120  *****************************************************************************/
121 static int OpenDecoder( vlc_object_t *p_this )
122 {
123     decoder_t *p_dec = (decoder_t*)p_this;
124     decoder_sys_t *p_sys;
125     vlc_value_t val;
126
127     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') && 
128         p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
129     {
130         return VLC_EGENERIC;
131     }
132
133     p_dec->pf_decode_sub = DecodeBlock;
134
135     /* Allocate the memory needed to store the decoder's structure */
136     if( ( p_dec->p_sys = p_sys =
137           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
138     {
139         msg_Err( p_dec, "out of memory" );
140         return VLC_EGENERIC;
141     }
142
143     var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
144     var_Get( p_dec, "subsdec-align", &val );
145     p_sys->i_align = val.i_int;
146
147 #if defined(HAVE_ICONV)
148     var_Create( p_dec, "subsdec-encoding",
149                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
150     var_Get( p_dec, "subsdec-encoding", &val );
151     if( !strcmp( val.psz_string, DEFAULT_NAME ) )
152     {
153         char *psz_charset =(char*)malloc( 100 );
154         vlc_current_charset( &psz_charset );
155         p_sys->iconv_handle = iconv_open( "UTF-8", psz_charset );
156         msg_Dbg( p_dec, "using character encoding: %s", psz_charset );
157         free( psz_charset );
158     }
159     else if( val.psz_string )
160     {
161         msg_Dbg( p_dec, "using character encoding: %s", val.psz_string );
162         p_sys->iconv_handle = iconv_open( "UTF-8", val.psz_string );
163     }
164
165     if( p_sys->iconv_handle == (iconv_t)-1 )
166     {
167         msg_Warn( p_dec, "unable to do requested conversion" );
168     }
169
170     if( val.psz_string ) free( val.psz_string );
171 #else
172
173     msg_Dbg( p_dec, "no iconv support available" );
174 #endif
175
176 #if 0
177     if( p_demux_data )
178         msg_Dbg( p_dec, p_demux_data->psz_header );
179 #endif
180
181     return VLC_SUCCESS;
182 }
183
184 /****************************************************************************
185  * DecodeBlock: the whole thing
186  ****************************************************************************
187  * This function must be fed with complete subtitles units.
188  ****************************************************************************/
189 static void DecodeBlock( decoder_t *p_dec, block_t **pp_block )
190 {
191     vout_thread_t *p_vout;
192
193     if( !pp_block || *pp_block == NULL )
194     {
195         return;
196     }
197
198     /* Here we are dealing with text subtitles */
199     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
200     if( p_vout )
201     {
202         ParseText( p_dec, *pp_block, p_vout );
203         vlc_object_release( p_vout );
204     }
205     else
206     {
207         msg_Warn( p_dec, "couldn't find a video output, trashing subtitle" );
208     }
209
210     block_Release( *pp_block );
211     *pp_block = NULL;
212 }
213
214 /*****************************************************************************
215  * CloseDecoder: clean up the decoder
216  *****************************************************************************/
217 static void CloseDecoder( vlc_object_t *p_this )
218 {
219     decoder_t *p_dec = (decoder_t *)p_this;
220     decoder_sys_t *p_sys = p_dec->p_sys;
221     vout_thread_t *p_vout;
222
223     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
224     if( p_vout != NULL && p_vout->p_subpicture != NULL )
225     {
226         subpicture_t *p_subpic;
227         int          i_subpic;
228
229         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
230         {
231             p_subpic = &p_vout->p_subpicture[i_subpic];
232
233             if( p_subpic != NULL &&
234               ( p_subpic->i_status == RESERVED_SUBPICTURE
235                 || p_subpic->i_status == READY_SUBPICTURE ) )
236             {
237                 vout_DestroySubPicture( p_vout, p_subpic );
238             }
239         }
240     }
241     if( p_vout ) vlc_object_release( p_vout );
242
243 #if defined(HAVE_ICONV)
244     if( p_sys->iconv_handle != (iconv_t)-1 )
245     {
246         iconv_close( p_sys->iconv_handle );
247     }
248 #endif
249
250     free( p_sys );
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         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, "Failed to convert subtitle encoding, dropping subtitle.\nTry setting a different character-encoding for the subtitle." );
303             free( psz_subtitle );
304             return;
305         }
306         else
307         {
308             free( psz_subtitle );
309             psz_subtitle = psz_new_subtitle;
310         }
311     }
312 #endif
313
314     if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
315     {
316         /* Decode SSA strings */
317         /* We expect: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
318         char *psz_new_subtitle;
319         char *psz_buffer_sub;
320         int         i_comma;
321         int         i_text;
322
323         psz_buffer_sub = psz_subtitle;
324         for( ;; )
325         {
326             i_comma = 0;
327             while( i_comma < 8 &&
328                 *psz_buffer_sub != '\0' )
329             {
330                 if( *psz_buffer_sub == ',' )
331                 {
332                     i_comma++;
333                 }
334                 psz_buffer_sub++;
335             }
336             psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
337             i_text = 0;
338             while( psz_buffer_sub[0] != '\0' )
339             {
340                 if( psz_buffer_sub[0] == '\\' && ( psz_buffer_sub[1] =='n' || psz_buffer_sub[1] =='N' ) )
341                 {
342                     psz_new_subtitle[i_text] = '\n';
343                     i_text++;
344                     psz_buffer_sub += 2;
345                 }
346                 else if( psz_buffer_sub[0] == '{' && psz_buffer_sub[1] == '\\' )
347                 {
348                     /* SSA control code */
349                     while( psz_buffer_sub[0] != '\0' && psz_buffer_sub[0] != '}' )
350                     {
351                         psz_buffer_sub++;
352                     }
353                     psz_buffer_sub++;
354                 }
355                 else
356                 {
357                     psz_new_subtitle[i_text] = psz_buffer_sub[0];
358                     i_text++;
359                     psz_buffer_sub++;
360                 }
361             }
362             psz_new_subtitle[i_text] = '\0';
363             free( psz_subtitle );
364             psz_subtitle = psz_new_subtitle;
365             break;
366         }
367     }
368
369     vout_ShowTextAbsolute( p_vout, psz_subtitle, NULL, 
370                            OSD_ALIGN_BOTTOM | p_sys->i_align,
371                            i_align_h, i_align_v, 
372                            p_block->i_pts, p_block->i_dts );
373
374     free( psz_subtitle );
375 }