1 /*****************************************************************************
2 * cvd.c : CVD Subtitle decoder thread
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: cvd.c,v 1.9 2004/01/11 01:54:20 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>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int DecoderOpen ( vlc_object_t * );
43 static int PacketizerOpen( vlc_object_t * );
46 set_description( _("CVD 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 N_(DEBUG_LONGTEXT), VLC_TRUE );
54 add_string( "sub-aspect-ratio", "", NULL,
55 SUB_ASPECT_RATIO_TEXT, SUB_ASPECT_RATIO_LONGTEXT,
59 set_description( _("Chaoji VCD subtitle packetizer") );
60 set_capability( "packetizer", 50 );
61 set_callbacks( PacketizerOpen, VCDSubClose );
64 /*****************************************************************************
66 *****************************************************************************/
68 static block_t *Reassemble( decoder_t *, block_t ** );
69 static void Decode ( decoder_t *, block_t ** );
70 static block_t *Packetize( decoder_t *, block_t ** );
73 /*****************************************************************************
75 *****************************************************************************
76 * Tries to launch a decoder and return score so that the interface is able
78 *****************************************************************************/
80 DecoderOpen( vlc_object_t *p_this )
82 decoder_t *p_dec = (decoder_t*)p_this;
85 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'c','v','d',' ' ) )
91 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
93 p_sys->i_debug = config_GetInt( p_this, MODULE_STRING "-debug" );
94 p_sys->b_packetizer = VLC_FALSE;
97 p_sys->subtitle_data = NULL;
99 VCDSubInitSubtitleBlock( p_sys );
101 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'c','v','d',' ' ) );
104 p_dec->pf_decode_sub = Decode;
105 p_dec->pf_packetize = Packetize;
107 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_EXT) , "");
112 /*****************************************************************************
114 *****************************************************************************
115 * Tries to launch a decoder and return score so that the interface is able
117 *****************************************************************************/
118 static int PacketizerOpen( vlc_object_t *p_this )
120 decoder_t *p_dec = (decoder_t*)p_this;
122 if( DecoderOpen( p_this ) )
126 p_dec->p_sys->b_packetizer = VLC_TRUE;
131 /*****************************************************************************
133 *****************************************************************************/
135 Decode ( decoder_t *p_dec, block_t **pp_block )
137 decoder_sys_t *p_sys = p_dec->p_sys;
138 block_t *p_spu = Reassemble( p_dec, pp_block );
140 dbg_print( (DECODE_DBG_CALL) , "");
144 p_sys->i_spu = block_ChainExtract( p_spu, p_sys->buffer, 65536 );
145 p_sys->i_pts = p_spu->i_pts;
146 block_ChainRelease( p_spu );
148 if( ( p_sys->p_vout = VCDSubFindVout( p_dec ) ) )
150 /* Parse and decode */
151 E_(ParsePacket)( p_dec );
153 vlc_object_release( p_sys->p_vout );
156 VCDSubInitSubtitleBlock ( p_sys );
161 /*****************************************************************************
163 *****************************************************************************/
165 Packetize( decoder_t *p_dec, block_t **pp_block )
167 decoder_sys_t *p_sys = p_dec->p_sys;
168 block_t *p_spu = Reassemble( p_dec, pp_block );
172 p_spu->i_dts = p_spu->i_pts;
175 VCDSubInitSubtitleBlock( p_sys );
177 return block_ChainGather( p_spu );
182 /* following functions are local */
184 #define SPU_HEADER_LEN 1
186 /*****************************************************************************
189 Data for single screen subtitle may come in several non-contiguous
190 packets of a stream. This routine is called when the next packet in
191 the stream comes in. The job of this routine is to parse the header,
192 if this is the beginning, and combine the packets into one complete
195 If everything is complete, we will return a block. Otherwise return
198 *****************************************************************************/
200 Reassemble( decoder_t *p_dec, block_t **pp_block )
202 decoder_sys_t *p_sys = p_dec->p_sys;
206 if( pp_block == NULL || *pp_block == NULL )
213 if( p_block->i_buffer < SPU_HEADER_LEN )
215 msg_Dbg( p_dec, "invalid packet header (size %d < %d)" ,
216 p_block->i_buffer, SPU_HEADER_LEN );
217 block_Release( p_block );
221 p_buffer = p_block->p_buffer;
223 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_PACKET),
224 "header: 0x%02x 0x%02x 0x%02x 0x%02x, 0x%02x, 0x%02x, size: %i",
225 p_buffer[1], p_buffer[2], p_buffer[3], p_buffer[4],
226 p_buffer[5], p_buffer[6],
229 if( config_GetInt( p_dec, "spu-channel" ) != p_buffer[0] )
232 /* From the scant data on the format, there is only only way known
233 to detect the first packet in a subtitle. The first packet
234 seems to have a valid PTS while later packets for the same
237 if ( p_sys->state == SUBTITLE_BLOCK_EMPTY && p_block->i_pts == 0 ) {
239 "first packet expected but no PTS present -- skipped\n");
243 if ( p_sys->subtitle_data_pos == 0 ) {
244 /* First packet in the subtitle block */
245 E_(ParseHeader)( p_dec, p_buffer, p_block );
246 VCDSubInitSubtitleData(p_sys);
249 /* FIXME - remove append_data and use chainappend */
250 VCDSubAppendData( p_dec, p_buffer + SPU_HEADER_LEN,
251 p_block->i_buffer - SPU_HEADER_LEN );
253 block_ChainAppend( &p_sys->p_block, p_block );
255 p_sys->i_spu += p_block->i_buffer - SPU_HEADER_LEN;
257 if ( p_sys->subtitle_data_pos == p_sys->i_spu_size ) {
258 E_(ParseMetaInfo)( p_dec );
259 return p_sys->p_block;
261 /* Not last block in subtitle, so wait for another. */
262 p_sys->state = SUBTITLE_BLOCK_PARTIAL;