1 /*****************************************************************************
2 * ogt.c : Overlay Graphics Text (SVCD subtitles) decoder thread
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: ogt.c,v 1.4 2003/12/26 02:47:59 rocky Exp $
7 * Authors: Rocky Bernstein
9 * Julio Sanchez Fernandez (http://subhandler.sourceforge.net)
10 * Samuel Hocevar <sam@zoy.org>
11 * Laurent Aimar <fenrir@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
33 #include <vlc/decoder.h>
37 #define DEBUG_LONGTEXT N_( \
38 "This integer when viewed in binary is a debugging mask\n" \
44 /*****************************************************************************
46 *****************************************************************************/
47 static int DecoderOpen ( vlc_object_t * );
48 static int PacketizerOpen( vlc_object_t * );
50 static void Close ( vlc_object_t * );
53 set_description( _("Philips OGT (SVCD subtitle) decoder") );
54 set_capability( "decoder", 50 );
55 set_callbacks( DecoderOpen, Close );
57 add_integer ( MODULE_STRING "-debug", 0, NULL,
58 N_("set debug mask for additional debugging."),
59 N_(DEBUG_LONGTEXT), VLC_TRUE );
62 set_description( _("Philips OGT (SVCD subtitle) packetizer") );
63 set_capability( "packetizer", 50 );
64 set_callbacks( PacketizerOpen, Close );
67 /*****************************************************************************
69 *****************************************************************************/
70 static void InitSubtitleBlock( decoder_sys_t * p_sys );
71 static vout_thread_t *FindVout( decoder_t *);
73 static block_t *Reassemble( decoder_t *, block_t ** );
75 static void Decode ( decoder_t *, block_t ** );
76 static block_t *Packetize( decoder_t *, block_t ** );
79 /*****************************************************************************
81 *****************************************************************************
82 * Tries to launch a decoder and return score so that the interface is able
84 *****************************************************************************/
86 DecoderOpen( vlc_object_t *p_this )
88 decoder_t *p_dec = (decoder_t*)p_this;
91 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'o','g','t',' ' ) )
97 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
99 p_sys->i_debug = config_GetInt( p_this, MODULE_STRING "-debug" );
100 p_sys->b_packetizer = VLC_FALSE;
101 p_sys->p_vout = NULL;
103 p_sys->subtitle_data = NULL;
105 InitSubtitleBlock( p_sys );
107 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'o','g','t',' ' ) );
110 p_dec->pf_decode_sub = Decode;
111 p_dec->pf_packetize = Packetize;
113 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_EXT) , "");
118 /*****************************************************************************
120 *****************************************************************************
121 * Tries to launch a decoder and return score so that the interface is able
123 *****************************************************************************/
124 static int PacketizerOpen( vlc_object_t *p_this )
126 decoder_t *p_dec = (decoder_t*)p_this;
128 if( DecoderOpen( p_this ) )
132 p_dec->p_sys->b_packetizer = VLC_TRUE;
137 /*****************************************************************************
139 *****************************************************************************/
140 static void Close( vlc_object_t *p_this )
142 decoder_t *p_dec = (decoder_t*)p_this;
143 decoder_sys_t *p_sys = p_dec->p_sys;
145 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_EXT) , "");
147 if( !p_sys->b_packetizer )
149 /* FIXME check if it's ok to not lock vout */
150 if( p_sys->p_vout != NULL && p_sys->p_vout->p_subpicture != NULL )
152 subpicture_t * p_subpic;
155 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
157 p_subpic = &p_sys->p_vout->p_subpicture[i_subpic];
159 if( p_subpic != NULL &&
160 ( ( p_subpic->i_status == RESERVED_SUBPICTURE ) ||
161 ( p_subpic->i_status == READY_SUBPICTURE ) ) )
163 vout_DestroySubPicture( p_sys->p_vout, p_subpic );
171 block_ChainRelease( p_sys->p_block );
177 /*****************************************************************************
179 *****************************************************************************/
181 Decode ( decoder_t *p_dec, block_t **pp_block )
183 decoder_sys_t *p_sys = p_dec->p_sys;
184 block_t *p_spu = Reassemble( p_dec, pp_block );
186 dbg_print( (DECODE_DBG_CALL) , "");
190 p_sys->i_spu = block_ChainExtract( p_spu, p_sys->buffer, 65536 );
191 p_sys->i_pts = p_spu->i_pts;
192 block_ChainRelease( p_spu );
194 if( ( p_sys->p_vout = FindVout( p_dec ) ) )
196 /* Parse and decode */
197 E_(ParsePacket)( p_dec );
199 vlc_object_release( p_sys->p_vout );
202 InitSubtitleBlock ( p_sys );
207 /*****************************************************************************
209 *****************************************************************************/
211 Packetize( decoder_t *p_dec, block_t **pp_block )
213 decoder_sys_t *p_sys = p_dec->p_sys;
214 block_t *p_spu = Reassemble( p_dec, pp_block );
218 p_spu->i_dts = p_spu->i_pts;
221 InitSubtitleBlock( p_sys );
223 return block_ChainGather( p_spu );
228 /* following functions are local */
231 InitSubtitleData(decoder_sys_t *p_sys)
233 if ( p_sys->subtitle_data ) {
234 if ( p_sys->subtitle_data_size < p_sys->i_spu_size ) {
235 p_sys->subtitle_data = realloc(p_sys->subtitle_data,
237 p_sys->subtitle_data_size = p_sys->i_spu_size;
240 p_sys->subtitle_data = malloc(p_sys->i_spu_size);
241 p_sys->subtitle_data_size = p_sys->i_spu_size;
242 /* FIXME: wrong place to get p_sys */
245 p_sys->subtitle_data_pos = 0;
249 AppendData ( decoder_t *p_dec, uint8_t *buffer, uint32_t buf_len )
251 decoder_sys_t *p_sys = p_dec->p_sys;
252 int chunk_length = buf_len;
254 if ( chunk_length > p_sys->i_spu_size - p_sys->subtitle_data_pos ) {
255 msg_Warn( p_dec, "too much data (%d) expecting at most %u",
256 chunk_length, p_sys->i_spu_size - p_sys->subtitle_data_pos );
258 chunk_length = p_sys->i_spu_size - p_sys->subtitle_data_pos;
261 if ( chunk_length > 0 ) {
262 memcpy(p_sys->subtitle_data + p_sys->subtitle_data_pos,
263 buffer, chunk_length);
264 p_sys->subtitle_data_pos += chunk_length;
265 dbg_print(DECODE_DBG_PACKET, "%d bytes appended, pointer now %d",
266 chunk_length, p_sys->subtitle_data_pos);
270 /*****************************************************************************
273 Initialize so the next packet will start off a new one.
275 *****************************************************************************/
277 InitSubtitleBlock( decoder_sys_t * p_sys )
279 p_sys->i_spu_size = 0;
280 p_sys->state = SUBTITLE_BLOCK_EMPTY;
282 p_sys->p_block = NULL;
283 p_sys->subtitle_data_pos = 0;
287 #define SPU_HEADER_LEN 5
289 /*****************************************************************************
292 The data for single screen subtitle may come in one of many
293 non-contiguous packets of a stream. This routine is called when the
294 next packet in the stream comes in. The job of this routine is to
295 parse the header, if this is the beginning, and combine the packets
296 into one complete subtitle unit.
298 If everything is complete, we will return a block. Otherwise return
302 The format of the beginning of the subtitle packet that is used here.
305 -------------------------------------------
306 byte subtitle channel (0..7) in bits 0-3
307 byte subtitle packet number of this subtitle image 0-N,
308 if the subtitle packet is complete, the top bit of the byte is 1.
309 uint16 subtitle image number
311 *****************************************************************************/
313 Reassemble( decoder_t *p_dec, block_t **pp_block )
315 decoder_sys_t *p_sys = p_dec->p_sys;
318 uint16_t i_expected_image;
319 uint8_t i_packet, i_expected_packet;
321 if( pp_block == NULL || *pp_block == NULL )
328 if( p_block->i_buffer < SPU_HEADER_LEN )
330 msg_Dbg( p_dec, "invalid packet header (size %d < %d)" ,
331 p_block->i_buffer, SPU_HEADER_LEN );
332 block_Release( p_block );
336 p_buffer = p_block->p_buffer;
338 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_PACKET),
339 "header: 0x%02x 0x%02x 0x%02x 0x%02x, size: %i",
340 p_buffer[1], p_buffer[2], p_buffer[3], p_buffer[4],
343 if( config_GetInt( p_dec, "spu-channel" ) != p_buffer[1] )
346 if ( p_sys->state == SUBTITLE_BLOCK_EMPTY ) {
347 i_expected_image = p_sys->i_image+1;
348 i_expected_packet = 0;
350 i_expected_image = p_sys->i_image;
351 i_expected_packet = p_sys->i_packet+1;
356 if ( *p_buffer & 0x80 ) {
357 p_sys->state = SUBTITLE_BLOCK_COMPLETE;
358 i_packet = ( *p_buffer++ & 0x7F );
360 p_sys->state = SUBTITLE_BLOCK_PARTIAL;
361 i_packet = *p_buffer++;
364 p_sys->i_image = GETINT16(p_buffer);
366 if ( p_sys->i_image != i_expected_image ) {
367 msg_Warn( p_dec, "expecting subtitle image %u but found %u",
368 i_expected_image, p_sys->i_image );
371 if ( i_packet != i_expected_packet ) {
372 msg_Warn( p_dec, "expecting subtitle image packet %u but found %u",
373 i_expected_packet, i_packet);
376 p_sys->i_packet = i_packet;
378 if ( p_sys->i_packet == 0 ) {
379 /* First packet in the subtitle block */
380 E_(ParseHeader)( p_dec, p_buffer, p_block );
381 InitSubtitleData(p_sys);
384 /* FIXME - remove append_data and use chainappend */
385 AppendData( p_dec, p_buffer, p_block->i_buffer - 5 );
387 block_ChainAppend( &p_sys->p_block, p_block );
389 p_sys->i_spu += p_block->i_buffer - SPU_HEADER_LEN;
391 if (p_sys->state == SUBTITLE_BLOCK_COMPLETE)
393 if( p_sys->i_spu != p_sys->i_spu_size )
395 msg_Warn( p_dec, "SPU packets size=%d should be %d",
396 p_sys->i_spu, p_sys->i_spu_size );
399 dbg_print( (DECODE_DBG_PACKET),
400 "subtitle packet complete, size=%d", p_sys->i_spu );
402 return p_sys->p_block;
407 /*****************************************************************************
408 * FindVout: Find a vout or wait for one to be created.
409 *****************************************************************************/
410 static vout_thread_t *FindVout( decoder_t *p_dec )
412 vout_thread_t *p_vout = NULL;
414 /* Find an available video output */
417 if( p_dec->b_die || p_dec->b_error )
422 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
428 msleep( VOUT_OUTMEM_SLEEP );