* modules/codec/subsdec/subsdec.c: reordered the list of encodings. Should
[vlc.git] / modules / codec / subsdec / subsdec.c
1 /*****************************************************************************
2  * subsdec.c : SPU decoder thread
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: subsdec.c,v 1.7 2003/08/23 19:20:29 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 #include "subsdec.h"
37 #include "charset.h"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  OpenDecoder   ( vlc_object_t * );
43 static int  RunDecoder    ( decoder_fifo_t * );
44 static int  InitThread    ( subsdec_thread_t * );
45 static void EndThread     ( subsdec_thread_t * );
46 static vout_thread_t *FindVout( subsdec_thread_t * );
47
48 /*****************************************************************************
49  * Module descriptor.
50  *****************************************************************************/
51 static char *ppsz_encodings[] = { N_("System Default"), "ASCII", "UTF-8", "",
52     "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
53     "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
54     "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
55     "ISO-8859-6", "CP1256", "MacArabic", "",
56     "ISO-8859-7", "CP1253", "MacGreek", "",
57     "ISO-8859-8", "CP1255", "MacHebrew", "",
58     "ISO-8859-9", "CP1254", "MacTurkish", "",
59     "ISO-8859-13", "CP1257", "",
60     "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
61     "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
62     "ISO-2022-KR", "EUC-KR", "",
63     "MacThai", "KOI8-T", "",
64     "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
65     "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
66     "Macintosh", "",
67     "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
68     "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
69     "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
70     "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
71     "HPROMAN8", "NEXTSTEP", NULL };
72
73 #define ENCODING_TEXT N_("Subtitles text encoding")
74 #define ENCODING_LONGTEXT N_("Change the encoding used in text subtitles")
75 #define ALIGN_TEXT N_("Subtitles justification")
76 #define ALIGN_LONGTEXT N_("Change the justification of substitles (0=center, 1=left, 2=right)")
77
78 vlc_module_begin();
79     set_description( _("file subtitles decoder") );
80     set_capability( "decoder", 50 );
81     set_callbacks( OpenDecoder, NULL );
82     add_category_hint( N_("Subtitles"), NULL, VLC_FALSE );
83
84     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT, VLC_TRUE );
85 #if defined(HAVE_ICONV)
86     add_string_from_list( "subsdec-encoding", N_("System Default"), ppsz_encodings, NULL, ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
87 #endif
88 vlc_module_end();
89
90 /*****************************************************************************
91  * OpenDecoder: probe the decoder and return score
92  *****************************************************************************
93  * Tries to launch a decoder and return score so that the interface is able
94  * to chose.
95  *****************************************************************************/
96 static int OpenDecoder( vlc_object_t *p_this )
97 {
98     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
99
100     if( p_fifo->i_fourcc != VLC_FOURCC('s','u','b','t') )
101     {
102         return VLC_EGENERIC;
103     }
104
105     p_fifo->pf_run = RunDecoder;
106
107     var_Create( p_this, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
108 #if defined(HAVE_ICONV)
109     var_Create( p_this, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
110 #endif
111     return VLC_SUCCESS;
112 }
113
114 /*****************************************************************************
115  * RunDecoder: this function is called just after the thread is created
116  *****************************************************************************/
117 static int RunDecoder( decoder_fifo_t * p_fifo )
118 {
119     subsdec_thread_t *    p_subsdec;
120     vlc_value_t           val;
121
122     /* Allocate the memory needed to store the thread's structure */
123     p_subsdec = (subsdec_thread_t *)malloc( sizeof(subsdec_thread_t) );
124
125     if ( p_subsdec == NULL )
126     {
127         msg_Err( p_fifo, "out of memory" );
128         DecoderError( p_fifo );
129         return( -1 );
130     }
131
132     /*
133      * Initialize the thread properties
134      */
135     p_subsdec->p_vout = NULL;
136     p_subsdec->p_fifo = p_fifo;
137 #if defined(HAVE_ICONV)
138     p_subsdec->iconv_handle = (iconv_t)-1;
139 #endif
140     var_Get( p_subsdec->p_fifo, "subsdec-align", &val );
141     p_subsdec->i_align = val.i_int;
142
143     /*
144      * Initialize thread and free configuration
145      */
146     p_subsdec->p_fifo->b_error = InitThread( p_subsdec );
147
148     /*
149      * Main loop - it is not executed if an error occured during
150      * initialization
151      */
152     if( p_fifo->i_fourcc == VLC_FOURCC('s','u','b','t') )
153     {
154         /* Here we are dealing with text subtitles */
155 #if defined(HAVE_ICONV)
156         var_Get( p_subsdec->p_fifo, "subsdec-encoding", &val );
157         if( strcmp( val.psz_string, N_("System Default") ) == 0 )
158         {
159             char *psz_charset =(char*)malloc( 100 );
160             vlc_current_charset(&psz_charset);
161             p_subsdec->iconv_handle = iconv_open( "UTF-8", psz_charset );
162         }
163         else
164         {
165             p_subsdec->iconv_handle = iconv_open( "UTF-8", val.psz_string );
166         }
167         if( p_subsdec->iconv_handle == (iconv_t)-1 )
168         {
169             msg_Warn( p_subsdec->p_fifo, "Unable to do requested conversion" );
170         }
171         free( val.psz_string);
172 #endif
173         while( (!p_subsdec->p_fifo->b_die) && (!p_subsdec->p_fifo->b_error) )
174         {
175             /* Find/Wait for a video output */
176             p_subsdec->p_vout = FindVout( p_subsdec );
177
178             if( p_subsdec->p_vout )
179             {
180                 E_(ParseText)( p_subsdec );
181                 vlc_object_release( p_subsdec->p_vout );
182             }
183         }
184     }
185
186     /*
187      * Error loop
188      */
189     if( p_subsdec->p_fifo->b_error )
190     {
191         DecoderError( p_subsdec->p_fifo );
192
193         /* End of thread */
194         EndThread( p_subsdec );
195         return -1;
196     }
197
198     /* End of thread */
199     EndThread( p_subsdec );
200     return 0;
201 }
202
203 /* following functions are local */
204
205 /*****************************************************************************
206  * InitThread: initialize spu decoder thread
207  *****************************************************************************
208  * This function is called from RunThread and performs the second step of the
209  * initialization. It returns 0 on success. Note that the thread's flag are not
210  * modified inside this function.
211  *****************************************************************************/
212 static int InitThread( subsdec_thread_t *p_subsdec )
213 {
214     int i_ret;
215
216     /* Call InitBitstream anyway so p_subsdec->bit_stream is in a known
217      * state before calling CloseBitstream */
218     i_ret = InitBitstream( &p_subsdec->bit_stream, p_subsdec->p_fifo,
219                            NULL, NULL );
220
221     /* Check for a video output */
222     p_subsdec->p_vout = FindVout( p_subsdec );
223
224     if( !p_subsdec->p_vout )
225     {
226         return -1;
227     }
228
229     /* It was just a check */
230     vlc_object_release( p_subsdec->p_vout );
231     p_subsdec->p_vout = NULL;
232
233     return i_ret;
234 }
235
236 /*****************************************************************************
237  * FindVout: Find a vout or wait for one to be created.
238  *****************************************************************************/
239 static vout_thread_t *FindVout( subsdec_thread_t *p_subsdec )
240 {
241     vout_thread_t *p_vout = NULL;
242
243     /* Find an available video output */
244     do
245     {
246         if( p_subsdec->p_fifo->b_die || p_subsdec->p_fifo->b_error )
247         {
248             break;
249         }
250
251         p_vout = vlc_object_find( p_subsdec->p_fifo, VLC_OBJECT_VOUT,
252                                   FIND_ANYWHERE );
253
254         if( p_vout )
255         {
256             break;
257         }
258
259         msleep( VOUT_OUTMEM_SLEEP );
260     }
261     while( 1 );
262
263     return p_vout;
264 }
265
266 /*****************************************************************************
267  * EndThread: thread destruction
268  *****************************************************************************
269  * This function is called when the thread ends after a sucessful
270  * initialization.
271  *****************************************************************************/
272 static void EndThread( subsdec_thread_t *p_subsdec )
273 {
274     if( p_subsdec->p_vout != NULL
275          && p_subsdec->p_vout->p_subpicture != NULL )
276     {
277         subpicture_t *  p_subpic;
278         int             i_subpic;
279
280         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
281         {
282             p_subpic = &p_subsdec->p_vout->p_subpicture[i_subpic];
283
284             if( p_subpic != NULL &&
285               ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
286              || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
287             {
288                 vout_DestroySubPicture( p_subsdec->p_vout, p_subpic );
289             }
290         }
291     }
292 #if defined(HAVE_ICONV)
293     if( p_subsdec->iconv_handle != (iconv_t)-1 )
294     {
295         iconv_close( p_subsdec->iconv_handle );
296     }
297 #endif
298     CloseBitstream( &p_subsdec->bit_stream );
299     free( p_subsdec );
300 }
301
302 /*****************************************************************************
303  * ParseText: parse an text subtitle packet and send it to the video output
304  *****************************************************************************/
305 void E_(ParseText)( subsdec_thread_t *p_subsdec )
306 {
307     char         * psz_subtitle;
308     mtime_t        i_pts, i_dts;
309     /* We cannot display a subpicture with no date */
310     i_pts = p_subsdec->bit_stream.p_pes->i_pts;
311     i_dts = p_subsdec->bit_stream.p_pes->i_dts;
312     if( i_pts == 0 )
313     {
314         /* Dump the packet */
315         NextDataPacket( p_subsdec->p_fifo, &p_subsdec->bit_stream );
316         msg_Warn( p_subsdec->p_fifo, "subtitle without a date" );
317         return;
318     }
319
320     /* Check validity of packet data */
321     if( (p_subsdec->bit_stream.p_data->p_payload_end
322           - p_subsdec->bit_stream.p_data->p_payload_start) <= 0
323         || (strlen(p_subsdec->bit_stream.p_data->p_payload_start)
324             > (size_t)(p_subsdec->bit_stream.p_data->p_payload_end
325                         - p_subsdec->bit_stream.p_data->p_payload_start)) )
326     {
327         /* Dump the packet */
328         NextDataPacket( p_subsdec->p_fifo, &p_subsdec->bit_stream );
329         msg_Warn( p_subsdec->p_fifo, "invalid subtitle" );
330         return;
331     }
332     psz_subtitle = p_subsdec->bit_stream.p_data->p_payload_start;
333
334     if( psz_subtitle[0] != '\0' )
335     {
336 #if defined(HAVE_ICONV)
337         char *psz_new_subtitle, *psz_convert_buffer_out, *psz_convert_buffer_in;
338         size_t ret, inbytes_left, outbytes_left;
339
340         psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) * sizeof(char) );
341         psz_convert_buffer_out = psz_new_subtitle;
342         psz_convert_buffer_in = psz_subtitle;
343         inbytes_left = strlen( psz_subtitle );
344         outbytes_left = 6 * inbytes_left;
345         ret = iconv( p_subsdec->iconv_handle, &psz_convert_buffer_in,
346                      &inbytes_left, &psz_convert_buffer_out, &outbytes_left );
347         *psz_convert_buffer_out = '\0';
348
349         if( inbytes_left )
350         {
351             msg_Warn( p_subsdec->p_fifo, "Something fishy happened during conversion" );
352         }
353         else
354         {
355             msg_Dbg( p_subsdec->p_fifo, "reencoded \"%s\" into \"%s\"", psz_subtitle, psz_new_subtitle );
356             psz_subtitle = psz_new_subtitle;
357         }
358 #endif
359         vout_ShowTextAbsolute( p_subsdec->p_vout, psz_subtitle, NULL, 
360                                OSD_ALIGN_BOTTOM | p_subsdec->i_align,
361                                p_subsdec->i_align ? 20 : 0, 10, 
362                                i_pts, i_dts );
363 #if defined(HAVE_ICONV)
364         free( psz_new_subtitle );
365 #endif
366     }
367
368     /* Prepare for next time. No need to check that
369      * p_subsdec->bit_stream->p_data is valid since we check later on
370      * for b_die and b_error */
371     NextDataPacket( p_subsdec->p_fifo, &p_subsdec->bit_stream );
372 }