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 $
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;
103 /*****************************************************************************
104 * RunDecoder: this function is called just after the thread is created
105 *****************************************************************************/
106 static int RunDecoder( decoder_fifo_t * p_fifo )
108 subsdec_thread_t * p_subsdec;
110 /* Allocate the memory needed to store the thread's structure */
111 p_subsdec = (subsdec_thread_t *)malloc( sizeof(subsdec_thread_t) );
113 if ( p_subsdec == NULL )
115 msg_Err( p_fifo, "out of memory" );
116 DecoderError( p_fifo );
121 * Initialize the thread properties
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;
130 * Initialize thread and free configuration
132 p_subsdec->p_fifo->b_error = InitThread( p_subsdec );
135 * Main loop - it is not executed if an error occured during
138 if( p_fifo->i_fourcc == VLC_FOURCC('s','u','b','t') )
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 )
146 msg_Warn( p_subsdec->p_fifo, "Unable to do requested conversion" );
149 while( (!p_subsdec->p_fifo->b_die) && (!p_subsdec->p_fifo->b_error) )
151 /* Find/Wait for a video output */
152 p_subsdec->p_vout = FindVout( p_subsdec );
154 if( p_subsdec->p_vout )
156 E_(ParseText)( p_subsdec );
157 vlc_object_release( p_subsdec->p_vout );
165 if( p_subsdec->p_fifo->b_error )
167 DecoderError( p_subsdec->p_fifo );
170 EndThread( p_subsdec );
175 EndThread( p_subsdec );
179 /* following functions are local */
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 )
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,
197 /* Check for a video output */
198 p_subsdec->p_vout = FindVout( p_subsdec );
200 if( !p_subsdec->p_vout )
205 /* It was just a check */
206 vlc_object_release( p_subsdec->p_vout );
207 p_subsdec->p_vout = NULL;
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 )
217 vout_thread_t *p_vout = NULL;
219 /* Find an available video output */
222 if( p_subsdec->p_fifo->b_die || p_subsdec->p_fifo->b_error )
227 p_vout = vlc_object_find( p_subsdec->p_fifo, VLC_OBJECT_VOUT,
235 msleep( VOUT_OUTMEM_SLEEP );
242 /*****************************************************************************
243 * EndThread: thread destruction
244 *****************************************************************************
245 * This function is called when the thread ends after a sucessful
247 *****************************************************************************/
248 static void EndThread( subsdec_thread_t *p_subsdec )
250 if( p_subsdec->p_vout != NULL
251 && p_subsdec->p_vout->p_subpicture != NULL )
253 subpicture_t * p_subpic;
256 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
258 p_subpic = &p_subsdec->p_vout->p_subpicture[i_subpic];
260 if( p_subpic != NULL &&
261 ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
262 || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
264 vout_DestroySubPicture( p_subsdec->p_vout, p_subpic );
268 #if defined(HAVE_ICONV)
269 if( p_subsdec->iconv_handle != (iconv_t)-1 )
271 iconv_close( p_subsdec->iconv_handle );
274 CloseBitstream( &p_subsdec->bit_stream );
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 )
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;
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" );
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)) )
303 /* Dump the packet */
304 NextDataPacket( p_subsdec->p_fifo, &p_subsdec->bit_stream );
305 msg_Warn( p_subsdec->p_fifo, "invalid subtitle" );
308 psz_subtitle = p_subsdec->bit_stream.p_data->p_payload_start;
310 if( psz_subtitle[0] != '\0' )
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;
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';
327 msg_Warn( p_subsdec->p_fifo, "Something fishy happened during conversion" );
331 msg_Dbg( p_subsdec->p_fifo, "reencoded \"%s\" into \"%s\"", psz_subtitle, psz_new_subtitle );
332 psz_subtitle = psz_new_subtitle;
335 vout_ShowTextAbsolute( p_subsdec->p_vout, psz_subtitle, NULL,
336 OSD_ALIGN_BOTTOM|OSD_ALIGN_LEFT, 20, 20,
338 #if defined(HAVE_ICONV)
339 free( psz_new_subtitle );
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 );