subsdec.c 12.8 KB
Newer Older
gbazin's avatar
   
gbazin committed
1
2
3
4
/*****************************************************************************
 * subsdec.c : text subtitles decoder
 *****************************************************************************
 * Copyright (C) 2000-2001 VideoLAN
Laurent Aimar's avatar
Laurent Aimar committed
5
 * $Id: subsdec.c,v 1.12 2003/11/23 03:55:01 fenrir Exp $
gbazin's avatar
   
gbazin committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
 *          Samuel Hocevar <sam@zoy.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/vout.h>
#include <vlc/decoder.h>
31

gbazin's avatar
   
gbazin committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <osd.h>

#if defined(HAVE_ICONV)
#include <iconv.h>
#endif

#include "charset.h"

/*****************************************************************************
 * decoder_sys_t : decoder descriptor
 *****************************************************************************/
struct decoder_sys_t
{
    int                 i_align;          /* Subtitles alignment on the vout */

#if defined(HAVE_ICONV)
    iconv_t             iconv_handle;            /* handle to iconv instance */
#endif
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  OpenDecoder   ( vlc_object_t * );
gbazin's avatar
   
gbazin committed
56
static void CloseDecoder  ( vlc_object_t * );
gbazin's avatar
   
gbazin committed
57

gbazin's avatar
   
gbazin committed
58
static void DecodeBlock   ( decoder_t *, block_t ** );
gbazin's avatar
   
gbazin committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

static void ParseText     ( decoder_t *, block_t *, vout_thread_t * );

#define DEFAULT_NAME "System Default"

/*****************************************************************************
 * Module descriptor.
 *****************************************************************************/
#if defined(HAVE_ICONV)
static char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
    "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
    "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
    "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
    "ISO-8859-6", "CP1256", "MacArabic", "",
    "ISO-8859-7", "CP1253", "MacGreek", "",
    "ISO-8859-8", "CP1255", "MacHebrew", "",
    "ISO-8859-9", "CP1254", "MacTurkish", "",
    "ISO-8859-13", "CP1257", "",
    "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
    "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
    "ISO-2022-KR", "EUC-KR", "",
    "MacThai", "KOI8-T", "",
    "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
    "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
    "Macintosh", "",
    "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
    "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
    "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
    "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
88
    "HPROMAN8", "NEXTSTEP" };
gbazin's avatar
   
gbazin committed
89
90
#endif

91
92
93
static int  pi_justification[] = { 0, 1, 2 };
static char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};

gbazin's avatar
   
gbazin committed
94
95
96
#define ENCODING_TEXT N_("Subtitles text encoding")
#define ENCODING_LONGTEXT N_("Change the encoding used in text subtitles")
#define ALIGN_TEXT N_("Subtitles justification")
97
#define ALIGN_LONGTEXT N_("Change the justification of substitles")
gbazin's avatar
   
gbazin committed
98
99
100
101

vlc_module_begin();
    set_description( _("text subtitles decoder") );
    set_capability( "decoder", 50 );
gbazin's avatar
   
gbazin committed
102
    set_callbacks( OpenDecoder, CloseDecoder );
gbazin's avatar
   
gbazin committed
103
104

    add_category_hint( N_("Subtitles"), NULL, VLC_FALSE );
105
106
107
    add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
                 VLC_TRUE );
        change_integer_list( pi_justification, ppsz_justification_text, 0 );
gbazin's avatar
   
gbazin committed
108
#if defined(HAVE_ICONV)
109
    add_string( "subsdec-encoding", "UTF-8", NULL,
110
111
                ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
        change_string_list( ppsz_encodings, 0, 0 );
gbazin's avatar
   
gbazin committed
112
113
114
115
116
117
118
119
120
121
122
123
#endif
vlc_module_end();

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************
 * Tries to launch a decoder and return score so that the interface is able
 * to chose.
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;
gbazin's avatar
   
gbazin committed
124
125
    decoder_sys_t *p_sys;
    vlc_value_t val;
gbazin's avatar
   
gbazin committed
126

gbazin's avatar
   
gbazin committed
127
128
    if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') && 
        p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
gbazin's avatar
   
gbazin committed
129
130
131
132
    {
        return VLC_EGENERIC;
    }

gbazin's avatar
   
gbazin committed
133
    p_dec->pf_decode_sub = DecodeBlock;
gbazin's avatar
   
gbazin committed
134
135

    /* Allocate the memory needed to store the decoder's structure */
gbazin's avatar
   
gbazin committed
136
    if( ( p_dec->p_sys = p_sys =
gbazin's avatar
   
gbazin committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
    {
        msg_Err( p_dec, "out of memory" );
        return VLC_EGENERIC;
    }

    var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_dec, "subsdec-align", &val );
    p_sys->i_align = val.i_int;

#if defined(HAVE_ICONV)
    var_Create( p_dec, "subsdec-encoding",
                VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_dec, "subsdec-encoding", &val );
    if( !strcmp( val.psz_string, DEFAULT_NAME ) )
    {
        char *psz_charset =(char*)malloc( 100 );
        vlc_current_charset( &psz_charset );
        p_sys->iconv_handle = iconv_open( "UTF-8", psz_charset );
156
        msg_Dbg( p_dec, "Using character encoding: %s", psz_charset );
gbazin's avatar
   
gbazin committed
157
158
        free( psz_charset );
    }
159
    else if( val.psz_string )
gbazin's avatar
   
gbazin committed
160
    {
161
        msg_Dbg( p_dec, "Using character encoding: %s", val.psz_string );
gbazin's avatar
   
gbazin committed
162
163
164
165
166
167
168
169
170
        p_sys->iconv_handle = iconv_open( "UTF-8", val.psz_string );
    }

    if( p_sys->iconv_handle == (iconv_t)-1 )
    {
        msg_Warn( p_dec, "Unable to do requested conversion" );
    }

    if( val.psz_string ) free( val.psz_string );
hartman's avatar
hartman committed
171
#else
gbazin's avatar
   
gbazin committed
172

hartman's avatar
hartman committed
173
    msg_Dbg( p_dec, "No iconv support available" );
gbazin's avatar
   
gbazin committed
174
175
#endif

176
#if 0
177
178
179
180
    if( p_demux_data )
        msg_Dbg( p_dec, p_demux_data->psz_header );
#endif

gbazin's avatar
   
gbazin committed
181
182
183
184
    return VLC_SUCCESS;
}

/****************************************************************************
gbazin's avatar
   
gbazin committed
185
 * DecodeBlock: the whole thing
gbazin's avatar
   
gbazin committed
186
187
188
 ****************************************************************************
 * This function must be fed with complete subtitles units.
 ****************************************************************************/
gbazin's avatar
   
gbazin committed
189
static void DecodeBlock( decoder_t *p_dec, block_t **pp_block )
gbazin's avatar
   
gbazin committed
190
191
192
{
    vout_thread_t *p_vout;

193
194
195
196
197
    if( !pp_block || *pp_block == NULL )
    {
        return;
    }

gbazin's avatar
   
gbazin committed
198
199
200
201
202
    /* Here we are dealing with text subtitles */
    p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
    if( !p_vout )
    {
        msg_Warn( p_dec, "couldn't find a video output, trashing subtitle" );
203
        return;
gbazin's avatar
   
gbazin committed
204
205
    }

gbazin's avatar
   
gbazin committed
206
    ParseText( p_dec, *pp_block, p_vout );
gbazin's avatar
   
gbazin committed
207
    vlc_object_release( p_vout );
208
209
210

    block_Release( *pp_block );
    *pp_block = NULL;
gbazin's avatar
   
gbazin committed
211
212
213
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
214
 * CloseDecoder: clean up the decoder
gbazin's avatar
   
gbazin committed
215
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
216
static void CloseDecoder( vlc_object_t *p_this )
gbazin's avatar
   
gbazin committed
217
{
gbazin's avatar
   
gbazin committed
218
    decoder_t *p_dec = (decoder_t *)p_this;
gbazin's avatar
   
gbazin committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
    decoder_sys_t *p_sys = p_dec->p_sys;
    vout_thread_t *p_vout;

    p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
    if( p_vout != NULL && p_vout->p_subpicture != NULL )
    {
        subpicture_t *p_subpic;
        int          i_subpic;

        for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
        {
            p_subpic = &p_vout->p_subpicture[i_subpic];

            if( p_subpic != NULL &&
              ( p_subpic->i_status == RESERVED_SUBPICTURE
                || p_subpic->i_status == READY_SUBPICTURE ) )
            {
                vout_DestroySubPicture( p_vout, p_subpic );
            }
        }
    }
    if( p_vout ) vlc_object_release( p_vout );

#if defined(HAVE_ICONV)
    if( p_sys->iconv_handle != (iconv_t)-1 )
    {
        iconv_close( p_sys->iconv_handle );
    }
#endif

    free( p_sys );
}

/*****************************************************************************
 * ParseText: parse an text subtitle packet and send it to the video output
 *****************************************************************************/
static void ParseText( decoder_t *p_dec, block_t *p_block,
                       vout_thread_t *p_vout )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    char *psz_subtitle;
260
    int i_align_h, i_align_v;
gbazin's avatar
   
gbazin committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

    /* We cannot display a subpicture with no date */
    if( p_block->i_pts == 0 )
    {
        msg_Warn( p_dec, "subtitle without a date" );
        return;
    }

    /* Check validity of packet data */
    if( p_block->i_buffer <= 1 ||  p_block->p_buffer[0] == '\0' )
    {
        msg_Warn( p_dec, "empty subtitle" );
        return;
    }

    /* Should be resiliant against bad subtitles */
    psz_subtitle = strndup( p_block->p_buffer, p_block->i_buffer );
278
279
280
    
    i_align_h = p_sys->i_align ? 20 : 0;
    i_align_v = 10;
gbazin's avatar
   
gbazin committed
281
282
283
284
285

#if defined(HAVE_ICONV)
    if( p_sys->iconv_handle != (iconv_t)-1 )
    {
        char *psz_new_subtitle;
286
        char *psz_convert_buffer_out;
287
        char *psz_convert_buffer_in;
gbazin's avatar
   
gbazin committed
288
289
290
291
292
293
294
295
296
297
298
299
300
        size_t ret, inbytes_left, outbytes_left;

        psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) );
        psz_convert_buffer_out = psz_new_subtitle;
        psz_convert_buffer_in = psz_subtitle;
        inbytes_left = strlen( psz_subtitle );
        outbytes_left = 6 * inbytes_left;
        ret = iconv( p_sys->iconv_handle, &psz_convert_buffer_in,
                     &inbytes_left, &psz_convert_buffer_out, &outbytes_left );
        *psz_convert_buffer_out = '\0';

        if( inbytes_left )
        {
sigmunau's avatar
sigmunau committed
301
302
303
            msg_Warn( p_dec, "Failed to convert subtitle encoding, dropping subtitle" );
            free( psz_subtitle );
            return;
gbazin's avatar
   
gbazin committed
304
305
306
307
308
309
310
311
312
        }
        else
        {
            free( psz_subtitle );
            psz_subtitle = psz_new_subtitle;
        }
    }
#endif

Laurent Aimar's avatar
Laurent Aimar committed
313
    if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    {
        /* Decode SSA strings */
        /* We expect: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
        char *psz_new_subtitle;
        char *psz_buffer_sub;
        int         i_comma;
        int         i_text;

        psz_buffer_sub = psz_subtitle;
        for( ;; )
        {
            i_comma = 0;
            while( i_comma < 8 &&
                *psz_buffer_sub != '\0' )
            {
                if( *psz_buffer_sub == ',' )
                {
                    i_comma++;
                }
                psz_buffer_sub++;
            }
            psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
            i_text = 0;
            while( psz_buffer_sub[0] != '\0' )
            {
                if( psz_buffer_sub[0] == '\\' && ( psz_buffer_sub[1] =='n' || psz_buffer_sub[1] =='N' ) )
                {
                    psz_new_subtitle[i_text] = '\n';
                    i_text++;
                    psz_buffer_sub += 2;
                }
                else if( psz_buffer_sub[0] == '{' && psz_buffer_sub[1] == '\\' )
                {
                    /* SSA control code */
                    while( psz_buffer_sub[0] != '\0' && psz_buffer_sub[0] != '}' )
                    {
                        psz_buffer_sub++;
                    }
                    psz_buffer_sub++;
                }
                else
                {
                    psz_new_subtitle[i_text] = psz_buffer_sub[0];
                    i_text++;
                    psz_buffer_sub++;
                }
            }
            psz_new_subtitle[i_text] = '\0';
            free( psz_subtitle );
            psz_subtitle = psz_new_subtitle;
            break;
        }
    }

gbazin's avatar
   
gbazin committed
368
369
    vout_ShowTextAbsolute( p_vout, psz_subtitle, NULL, 
                           OSD_ALIGN_BOTTOM | p_sys->i_align,
370
                           i_align_h, i_align_v, 
gbazin's avatar
   
gbazin committed
371
372
373
374
                           p_block->i_pts, p_block->i_dts );

    free( psz_subtitle );
}