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