1 /*****************************************************************************
2 * subsdec.c : SPU decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: subsdec.c,v 1.4 2003/07/25 01:11:32 hartman Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
29 #include <string.h> /* memcpy(), memset() */
33 #include <vlc/decoder.h>
38 /*****************************************************************************
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 * );
47 /*****************************************************************************
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 };
68 #define ENCODING_TEXT N_("subtitle text encoding")
69 #define ENCODING_LONGTEXT N_("change the encoding used in text subtitles")
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 );
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 );
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
88 *****************************************************************************/
89 static int OpenDecoder( vlc_object_t *p_this )
91 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
93 if( p_fifo->i_fourcc != VLC_FOURCC('s','u','b','t') )
98 p_fifo->pf_run = RunDecoder;
100 #if defined(HAVE_ICONV)
101 var_Create( p_this, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
106 /*****************************************************************************
107 * RunDecoder: this function is called just after the thread is created
108 *****************************************************************************/
109 static int RunDecoder( decoder_fifo_t * p_fifo )
111 subsdec_thread_t * p_subsdec;
114 /* Allocate the memory needed to store the thread's structure */
115 p_subsdec = (subsdec_thread_t *)malloc( sizeof(subsdec_thread_t) );
117 if ( p_subsdec == NULL )
119 msg_Err( p_fifo, "out of memory" );
120 DecoderError( p_fifo );
125 * Initialize the thread properties
127 p_subsdec->p_vout = NULL;
128 p_subsdec->p_fifo = p_fifo;
129 #if defined(HAVE_ICONV)
130 p_subsdec->iconv_handle = (iconv_t)-1;
134 * Initialize thread and free configuration
136 p_subsdec->p_fifo->b_error = InitThread( p_subsdec );
139 * Main loop - it is not executed if an error occured during
142 if( p_fifo->i_fourcc == VLC_FOURCC('s','u','b','t') )
144 /* Here we are dealing with text subtitles */
145 #if defined(HAVE_ICONV)
146 var_Get( p_subsdec->p_fifo, "subsdec-encoding", &val );
147 p_subsdec->iconv_handle = iconv_open( "UTF-8", val.psz_string);
148 if( p_subsdec->iconv_handle == (iconv_t)-1 )
150 msg_Warn( p_subsdec->p_fifo, "Unable to do requested conversion" );
152 free( val.psz_string);
154 while( (!p_subsdec->p_fifo->b_die) && (!p_subsdec->p_fifo->b_error) )
156 /* Find/Wait for a video output */
157 p_subsdec->p_vout = FindVout( p_subsdec );
159 if( p_subsdec->p_vout )
161 E_(ParseText)( p_subsdec );
162 vlc_object_release( p_subsdec->p_vout );
170 if( p_subsdec->p_fifo->b_error )
172 DecoderError( p_subsdec->p_fifo );
175 EndThread( p_subsdec );
180 EndThread( p_subsdec );
184 /* following functions are local */
186 /*****************************************************************************
187 * InitThread: initialize spu decoder thread
188 *****************************************************************************
189 * This function is called from RunThread and performs the second step of the
190 * initialization. It returns 0 on success. Note that the thread's flag are not
191 * modified inside this function.
192 *****************************************************************************/
193 static int InitThread( subsdec_thread_t *p_subsdec )
197 /* Call InitBitstream anyway so p_subsdec->bit_stream is in a known
198 * state before calling CloseBitstream */
199 i_ret = InitBitstream( &p_subsdec->bit_stream, p_subsdec->p_fifo,
202 /* Check for a video output */
203 p_subsdec->p_vout = FindVout( p_subsdec );
205 if( !p_subsdec->p_vout )
210 /* It was just a check */
211 vlc_object_release( p_subsdec->p_vout );
212 p_subsdec->p_vout = NULL;
217 /*****************************************************************************
218 * FindVout: Find a vout or wait for one to be created.
219 *****************************************************************************/
220 static vout_thread_t *FindVout( subsdec_thread_t *p_subsdec )
222 vout_thread_t *p_vout = NULL;
224 /* Find an available video output */
227 if( p_subsdec->p_fifo->b_die || p_subsdec->p_fifo->b_error )
232 p_vout = vlc_object_find( p_subsdec->p_fifo, VLC_OBJECT_VOUT,
240 msleep( VOUT_OUTMEM_SLEEP );
247 /*****************************************************************************
248 * EndThread: thread destruction
249 *****************************************************************************
250 * This function is called when the thread ends after a sucessful
252 *****************************************************************************/
253 static void EndThread( subsdec_thread_t *p_subsdec )
255 if( p_subsdec->p_vout != NULL
256 && p_subsdec->p_vout->p_subpicture != NULL )
258 subpicture_t * p_subpic;
261 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
263 p_subpic = &p_subsdec->p_vout->p_subpicture[i_subpic];
265 if( p_subpic != NULL &&
266 ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
267 || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
269 vout_DestroySubPicture( p_subsdec->p_vout, p_subpic );
273 #if defined(HAVE_ICONV)
274 if( p_subsdec->iconv_handle != (iconv_t)-1 )
276 iconv_close( p_subsdec->iconv_handle );
279 CloseBitstream( &p_subsdec->bit_stream );
283 /*****************************************************************************
284 * ParseText: parse an text subtitle packet and send it to the video output
285 *****************************************************************************/
286 void E_(ParseText)( subsdec_thread_t *p_subsdec )
289 mtime_t i_pts, i_dts;
290 /* We cannot display a subpicture with no date */
291 i_pts = p_subsdec->bit_stream.p_pes->i_pts;
292 i_dts = p_subsdec->bit_stream.p_pes->i_dts;
295 /* Dump the packet */
296 NextDataPacket( p_subsdec->p_fifo, &p_subsdec->bit_stream );
297 msg_Warn( p_subsdec->p_fifo, "subtitle without a date" );
301 /* Check validity of packet data */
302 if( (p_subsdec->bit_stream.p_data->p_payload_end
303 - p_subsdec->bit_stream.p_data->p_payload_start) <= 0
304 || (strlen(p_subsdec->bit_stream.p_data->p_payload_start)
305 > (size_t)(p_subsdec->bit_stream.p_data->p_payload_end
306 - p_subsdec->bit_stream.p_data->p_payload_start)) )
308 /* Dump the packet */
309 NextDataPacket( p_subsdec->p_fifo, &p_subsdec->bit_stream );
310 msg_Warn( p_subsdec->p_fifo, "invalid subtitle" );
313 psz_subtitle = p_subsdec->bit_stream.p_data->p_payload_start;
315 if( psz_subtitle[0] != '\0' )
317 #if defined(HAVE_ICONV)
318 char *psz_new_subtitle, *psz_convert_buffer_out, *psz_convert_buffer_in;
319 size_t ret, inbytes_left, outbytes_left;
321 psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) * sizeof(char) );
322 psz_convert_buffer_out = psz_new_subtitle;
323 psz_convert_buffer_in = psz_subtitle;
324 inbytes_left = strlen( psz_subtitle );
325 outbytes_left = 6 * inbytes_left;
326 ret = iconv( p_subsdec->iconv_handle, &psz_convert_buffer_in,
327 &inbytes_left, &psz_convert_buffer_out, &outbytes_left );
328 *psz_convert_buffer_out = '\0';
332 msg_Warn( p_subsdec->p_fifo, "Something fishy happened during conversion" );
336 msg_Dbg( p_subsdec->p_fifo, "reencoded \"%s\" into \"%s\"", psz_subtitle, psz_new_subtitle );
337 psz_subtitle = psz_new_subtitle;
340 vout_ShowTextAbsolute( p_subsdec->p_vout, psz_subtitle, NULL,
341 OSD_ALIGN_BOTTOM|OSD_ALIGN_LEFT, 20, 20,
343 #if defined(HAVE_ICONV)
344 free( psz_new_subtitle );
348 /* Prepare for next time. No need to check that
349 * p_subsdec->bit_stream->p_data is valid since we check later on
350 * for b_die and b_error */
351 NextDataPacket( p_subsdec->p_fifo, &p_subsdec->bit_stream );