]> git.sesse.net Git - vlc/blob - modules/codec/subsdec.c
* removed an undef HAVE_ICONV from this file, and fixed a cast warning.
[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.3 2003/10/11 14:08:58 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 #if defined(HAVE_ICONV)
37 #include <iconv.h>
38 #endif
39
40 #include "charset.h"
41
42 /*****************************************************************************
43  * decoder_sys_t : decoder descriptor
44  *****************************************************************************/
45 struct decoder_sys_t
46 {
47     int                 i_align;          /* Subtitles alignment on the vout */
48
49 #if defined(HAVE_ICONV)
50     iconv_t             iconv_handle;            /* handle to iconv instance */
51 #endif
52 };
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static int  OpenDecoder   ( vlc_object_t * );
58
59 static int  InitDecoder   ( decoder_t * );
60 static int  RunDecoder    ( decoder_t *, block_t * );
61 static int  EndDecoder    ( decoder_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", NULL };
92 #endif
93
94 #define ENCODING_TEXT N_("Subtitles text encoding")
95 #define ENCODING_LONGTEXT N_("Change the encoding used in text subtitles")
96 #define ALIGN_TEXT N_("Subtitles justification")
97 #define ALIGN_LONGTEXT N_("Change the justification of substitles (0=center, 1=left, 2=right)")
98
99 vlc_module_begin();
100     set_description( _("text subtitles decoder") );
101     set_capability( "decoder", 50 );
102     set_callbacks( OpenDecoder, NULL );
103
104     add_category_hint( N_("Subtitles"), NULL, VLC_FALSE );
105     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT, VLC_TRUE );
106 #if defined(HAVE_ICONV)
107     add_string_from_list( "subsdec-encoding", DEFAULT_NAME, ppsz_encodings, NULL, ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
108 #endif
109 vlc_module_end();
110
111 /*****************************************************************************
112  * OpenDecoder: probe the decoder and return score
113  *****************************************************************************
114  * Tries to launch a decoder and return score so that the interface is able
115  * to chose.
116  *****************************************************************************/
117 static int OpenDecoder( vlc_object_t *p_this )
118 {
119     decoder_t *p_dec = (decoder_t*)p_this;
120
121     if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('s','u','b','t') )
122     {
123         return VLC_EGENERIC;
124     }
125
126     p_dec->pf_init = InitDecoder;
127     p_dec->pf_decode = RunDecoder;
128     p_dec->pf_end = EndDecoder;
129
130     /* Allocate the memory needed to store the decoder's structure */
131     if( ( p_dec->p_sys =
132           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
133     {
134         msg_Err( p_dec, "out of memory" );
135         return VLC_EGENERIC;
136     }
137
138     return VLC_SUCCESS;
139 }
140
141 /*****************************************************************************
142  * InitDecoder: Initalize the decoder
143  *****************************************************************************/
144 static int InitDecoder( decoder_t *p_dec )
145 {
146     decoder_sys_t *p_sys = p_dec->p_sys;
147     vlc_value_t val;
148
149     var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
150     var_Get( p_dec, "subsdec-align", &val );
151     p_sys->i_align = val.i_int;
152
153 #if defined(HAVE_ICONV)
154     var_Create( p_dec, "subsdec-encoding",
155                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
156     var_Get( p_dec, "subsdec-encoding", &val );
157     if( !strcmp( val.psz_string, DEFAULT_NAME ) )
158     {
159         char *psz_charset =(char*)malloc( 100 );
160         vlc_current_charset( &psz_charset );
161         p_sys->iconv_handle = iconv_open( "UTF-8", psz_charset );
162         free( psz_charset );
163     }
164     else
165     {
166         p_sys->iconv_handle = iconv_open( "UTF-8", val.psz_string );
167     }
168
169     if( p_sys->iconv_handle == (iconv_t)-1 )
170     {
171         msg_Warn( p_dec, "Unable to do requested conversion" );
172     }
173
174     if( val.psz_string ) free( val.psz_string );
175 #else
176     msg_Dbg( p_dec, "No iconv support available" );
177 #endif
178
179     return VLC_SUCCESS;
180 }
181
182 /****************************************************************************
183  * RunDecoder: the whole thing
184  ****************************************************************************
185  * This function must be fed with complete subtitles units.
186  ****************************************************************************/
187 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
188 {
189     vout_thread_t *p_vout;
190
191     /* Here we are dealing with text subtitles */
192     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
193     if( !p_vout )
194     {
195         msg_Warn( p_dec, "couldn't find a video output, trashing subtitle" );
196         return VLC_SUCCESS;
197     }
198
199     ParseText( p_dec, p_block, p_vout );
200     vlc_object_release( p_vout );
201
202     return VLC_SUCCESS;
203 }
204
205 /*****************************************************************************
206  * EndDecoder: clean up the decoder
207  *****************************************************************************/
208 static int EndDecoder( decoder_t *p_dec )
209 {
210     decoder_sys_t *p_sys = p_dec->p_sys;
211     vout_thread_t *p_vout;
212
213     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
214     if( p_vout != NULL && p_vout->p_subpicture != NULL )
215     {
216         subpicture_t *p_subpic;
217         int          i_subpic;
218
219         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
220         {
221             p_subpic = &p_vout->p_subpicture[i_subpic];
222
223             if( p_subpic != NULL &&
224               ( p_subpic->i_status == RESERVED_SUBPICTURE
225                 || p_subpic->i_status == READY_SUBPICTURE ) )
226             {
227                 vout_DestroySubPicture( p_vout, p_subpic );
228             }
229         }
230     }
231     if( p_vout ) vlc_object_release( p_vout );
232
233 #if defined(HAVE_ICONV)
234     if( p_sys->iconv_handle != (iconv_t)-1 )
235     {
236         iconv_close( p_sys->iconv_handle );
237     }
238 #endif
239
240     free( p_sys );
241
242     return VLC_SUCCESS;
243 }
244
245 /*****************************************************************************
246  * ParseText: parse an text subtitle packet and send it to the video output
247  *****************************************************************************/
248 static void ParseText( decoder_t *p_dec, block_t *p_block,
249                        vout_thread_t *p_vout )
250 {
251     decoder_sys_t *p_sys = p_dec->p_sys;
252     char *psz_subtitle;
253
254     /* We cannot display a subpicture with no date */
255     if( p_block->i_pts == 0 )
256     {
257         msg_Warn( p_dec, "subtitle without a date" );
258         return;
259     }
260
261     /* Check validity of packet data */
262     if( p_block->i_buffer <= 1 ||  p_block->p_buffer[0] == '\0' )
263     {
264         msg_Warn( p_dec, "empty subtitle" );
265         return;
266     }
267
268     /* Should be resiliant against bad subtitles */
269     psz_subtitle = strndup( p_block->p_buffer, p_block->i_buffer );
270
271 #if defined(HAVE_ICONV)
272     if( p_sys->iconv_handle != (iconv_t)-1 )
273     {
274         char *psz_new_subtitle;
275         char *psz_convert_buffer_out;
276         const char *psz_convert_buffer_in;
277         size_t ret, inbytes_left, outbytes_left;
278
279         psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) );
280         psz_convert_buffer_out = psz_new_subtitle;
281         psz_convert_buffer_in = psz_subtitle;
282         inbytes_left = strlen( psz_subtitle );
283         outbytes_left = 6 * inbytes_left;
284         ret = iconv( p_sys->iconv_handle, &psz_convert_buffer_in,
285                      &inbytes_left, &psz_convert_buffer_out, &outbytes_left );
286         *psz_convert_buffer_out = '\0';
287
288         if( inbytes_left )
289         {
290             msg_Warn( p_dec, "Something fishy happened during conversion" );
291         }
292         else
293         {
294             free( psz_subtitle );
295             psz_subtitle = psz_new_subtitle;
296         }
297     }
298 #endif
299
300     vout_ShowTextAbsolute( p_vout, psz_subtitle, NULL, 
301                            OSD_ALIGN_BOTTOM | p_sys->i_align,
302                            p_sys->i_align ? 20 : 0, 10, 
303                            p_block->i_pts, p_block->i_dts );
304
305     free( psz_subtitle );
306 }