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