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