1 /*****************************************************************************
2 * ogt.c : Overlay Graphics Text (SVCD subtitles) decoder thread
3 *****************************************************************************
4 * Copyright (C) 2003, 2004 VideoLAN
5 * $Id: ogt.c,v 1.11 2004/01/16 13:32:37 rocky Exp $
7 * Author: 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>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int DecoderOpen ( vlc_object_t * );
43 static int PacketizerOpen( vlc_object_t * );
46 set_description( _("Philips OGT (SVCD subtitle) decoder") );
47 set_capability( "decoder", 50 );
48 set_callbacks( DecoderOpen, VCDSubClose );
50 add_integer ( MODULE_STRING "-debug", 0, NULL,
51 N_("set debug mask for additional debugging."),
52 DEBUG_LONGTEXT, VLC_TRUE );
54 add_integer ( MODULE_STRING "-horizontal-correct", 0, NULL,
55 HORIZONTAL_CORRECT, HORIZONTAL_CORRECT_LONGTEXT, VLC_FALSE );
57 add_integer ( MODULE_STRING "-vertical-correct", 0, NULL,
58 VERTICAL_CORRECT, VERTICAL_CORRECT_LONGTEXT, VLC_FALSE );
60 add_string( MODULE_STRING "-aspect-ratio", "", NULL,
61 SUB_ASPECT_RATIO_TEXT, SUB_ASPECT_RATIO_LONGTEXT,
64 add_integer( MODULE_STRING "-duration-scaling", 9, NULL,
65 DURATION_SCALE_TEXT, DURATION_SCALE_LONGTEXT,
69 set_description( _("Philips OGT (SVCD subtitle) packetizer") );
70 set_capability( "packetizer", 50 );
71 set_callbacks( PacketizerOpen, VCDSubClose );
74 /*****************************************************************************
76 *****************************************************************************/
78 static block_t *Reassemble( decoder_t *, block_t ** );
79 static void Decode ( decoder_t *, block_t ** );
80 static block_t *Packetize( decoder_t *, block_t ** );
83 /*****************************************************************************
85 *****************************************************************************
86 * Tries to launch a decoder and return score so that the interface is able
88 *****************************************************************************/
90 DecoderOpen( vlc_object_t *p_this )
92 decoder_t *p_dec = (decoder_t*)p_this;
95 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'o','g','t',' ' ) )
101 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
103 p_sys->i_debug = config_GetInt( p_this, MODULE_STRING "-debug" );
104 p_sys->b_packetizer = VLC_FALSE;
105 p_sys->p_vout = NULL;
107 p_sys->subtitle_data = NULL;
109 VCDSubInitSubtitleBlock( p_sys );
111 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'o','g','t',' ' ) );
114 p_dec->pf_decode_sub = Decode;
115 p_dec->pf_packetize = Packetize;
117 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_EXT) , "");
122 /*****************************************************************************
124 *****************************************************************************
125 * Tries to launch a decoder and return score so that the interface is able
127 *****************************************************************************/
128 static int PacketizerOpen( vlc_object_t *p_this )
130 decoder_t *p_dec = (decoder_t*)p_this;
132 if( DecoderOpen( p_this ) )
136 p_dec->p_sys->b_packetizer = VLC_TRUE;
141 /*****************************************************************************
143 *****************************************************************************/
145 Decode ( decoder_t *p_dec, block_t **pp_block )
147 decoder_sys_t *p_sys = p_dec->p_sys;
148 block_t *p_spu = Reassemble( p_dec, pp_block );
150 dbg_print( (DECODE_DBG_CALL) , "");
154 p_sys->i_spu = block_ChainExtract( p_spu, p_sys->buffer, 65536 );
155 p_sys->i_pts = p_spu->i_pts;
156 block_ChainRelease( p_spu );
158 if( ( p_sys->p_vout = VCDSubFindVout( p_dec ) ) )
160 /* Parse and decode */
161 E_(ParsePacket)( p_dec );
163 vlc_object_release( p_sys->p_vout );
166 VCDSubInitSubtitleBlock ( p_sys );
171 /*****************************************************************************
173 *****************************************************************************/
175 Packetize( decoder_t *p_dec, block_t **pp_block )
177 decoder_sys_t *p_sys = p_dec->p_sys;
178 block_t *p_spu = Reassemble( p_dec, pp_block );
182 p_spu->i_dts = p_spu->i_pts;
185 VCDSubInitSubtitleBlock( p_sys );
187 return block_ChainGather( p_spu );
192 #define SPU_HEADER_LEN 5
194 /*****************************************************************************
197 The data for single screen subtitle may come in one of many
198 non-contiguous packets of a stream. This routine is called when the
199 next packet in the stream comes in. The job of this routine is to
200 parse the header, if this is the beginning, and combine the packets
201 into one complete subtitle unit.
203 If everything is complete, we will return a block. Otherwise return
207 The format of the beginning of the subtitle packet that is used here.
210 -------------------------------------------
211 byte subtitle channel (0..7) in bits 0-3
212 byte subtitle packet number of this subtitle image 0-N,
213 if the subtitle packet is complete, the top bit of the byte is 1.
214 uint16 subtitle image number
216 *****************************************************************************/
218 Reassemble( decoder_t *p_dec, block_t **pp_block )
220 decoder_sys_t *p_sys = p_dec->p_sys;
223 uint16_t i_expected_image;
224 uint8_t i_packet, i_expected_packet;
226 if( pp_block == NULL || *pp_block == NULL )
233 if( p_block->i_buffer < SPU_HEADER_LEN )
235 msg_Dbg( p_dec, "invalid packet header (size %d < %d)" ,
236 p_block->i_buffer, SPU_HEADER_LEN );
237 block_Release( p_block );
241 p_buffer = p_block->p_buffer;
243 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_PACKET),
244 "header: 0x%02x 0x%02x 0x%02x 0x%02x, size: %i",
245 p_buffer[1], p_buffer[2], p_buffer[3], p_buffer[4],
248 if( config_GetInt( p_dec, "spu-channel" ) != p_buffer[1] )
251 if ( p_sys->state == SUBTITLE_BLOCK_EMPTY ) {
252 i_expected_image = p_sys->i_image+1;
253 i_expected_packet = 0;
255 i_expected_image = p_sys->i_image;
256 i_expected_packet = p_sys->i_packet+1;
261 if ( *p_buffer & 0x80 ) {
262 p_sys->state = SUBTITLE_BLOCK_COMPLETE;
263 i_packet = ( *p_buffer++ & 0x7F );
265 p_sys->state = SUBTITLE_BLOCK_PARTIAL;
266 i_packet = *p_buffer++;
269 p_sys->i_image = GETINT16(p_buffer);
271 if ( p_sys->i_image != i_expected_image ) {
272 msg_Warn( p_dec, "expecting subtitle image %u but found %u",
273 i_expected_image, p_sys->i_image );
276 if ( i_packet != i_expected_packet ) {
277 msg_Warn( p_dec, "expecting subtitle image packet %u but found %u",
278 i_expected_packet, i_packet);
281 p_sys->i_packet = i_packet;
283 if ( p_sys->i_packet == 0 ) {
284 /* First packet in the subtitle block */
285 E_(ParseHeader)( p_dec, p_buffer, p_block );
286 VCDSubInitSubtitleData(p_sys);
289 /* FIXME - remove append_data and use chainappend */
290 VCDSubAppendData( p_dec, p_buffer, p_block->i_buffer - SPU_HEADER_LEN );
292 block_ChainAppend( &p_sys->p_block, p_block );
294 p_sys->i_spu += p_block->i_buffer - SPU_HEADER_LEN;
296 if (p_sys->state == SUBTITLE_BLOCK_COMPLETE)
298 if( p_sys->i_spu != p_sys->i_spu_size )
300 msg_Warn( p_dec, "SPU packets size=%d should be %d",
301 p_sys->i_spu, p_sys->i_spu_size );
304 dbg_print( (DECODE_DBG_PACKET),
305 "subtitle packet complete, size=%d", p_sys->i_spu );
307 return p_sys->p_block;
315 * c-file-style: "gnu"
317 * indent-tabs-mode: nil