1 /*****************************************************************************
2 * cvd.c : CVD Subtitle decoder thread
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: cvd.c,v 1.7 2004/01/04 04:56:21 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 );
55 set_description( _("Chaoji VCD subtitle packetizer") );
56 set_capability( "packetizer", 50 );
57 set_callbacks( PacketizerOpen, VCDSubClose );
60 /*****************************************************************************
62 *****************************************************************************/
64 static block_t *Reassemble( decoder_t *, block_t ** );
65 static void Decode ( decoder_t *, block_t ** );
66 static block_t *Packetize( decoder_t *, block_t ** );
69 /*****************************************************************************
71 *****************************************************************************
72 * Tries to launch a decoder and return score so that the interface is able
74 *****************************************************************************/
76 DecoderOpen( vlc_object_t *p_this )
78 decoder_t *p_dec = (decoder_t*)p_this;
81 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'c','v','d',' ' ) )
87 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
89 p_sys->i_debug = config_GetInt( p_this, MODULE_STRING "-debug" );
90 p_sys->b_packetizer = VLC_FALSE;
93 p_sys->subtitle_data = NULL;
95 VCDSubInitSubtitleBlock( p_sys );
97 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'c','v','d',' ' ) );
100 p_dec->pf_decode_sub = Decode;
101 p_dec->pf_packetize = Packetize;
103 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_EXT) , "");
108 /*****************************************************************************
110 *****************************************************************************
111 * Tries to launch a decoder and return score so that the interface is able
113 *****************************************************************************/
114 static int PacketizerOpen( vlc_object_t *p_this )
116 decoder_t *p_dec = (decoder_t*)p_this;
118 if( DecoderOpen( p_this ) )
122 p_dec->p_sys->b_packetizer = VLC_TRUE;
127 /*****************************************************************************
129 *****************************************************************************/
131 Decode ( decoder_t *p_dec, block_t **pp_block )
133 decoder_sys_t *p_sys = p_dec->p_sys;
134 block_t *p_spu = Reassemble( p_dec, pp_block );
136 dbg_print( (DECODE_DBG_CALL) , "");
140 p_sys->i_spu = block_ChainExtract( p_spu, p_sys->buffer, 65536 );
141 p_sys->i_pts = p_spu->i_pts;
142 block_ChainRelease( p_spu );
144 if( ( p_sys->p_vout = VCDSubFindVout( p_dec ) ) )
146 /* Parse and decode */
147 E_(ParsePacket)( p_dec );
149 vlc_object_release( p_sys->p_vout );
152 VCDSubInitSubtitleBlock ( p_sys );
157 /*****************************************************************************
159 *****************************************************************************/
161 Packetize( decoder_t *p_dec, block_t **pp_block )
163 decoder_sys_t *p_sys = p_dec->p_sys;
164 block_t *p_spu = Reassemble( p_dec, pp_block );
168 p_spu->i_dts = p_spu->i_pts;
171 VCDSubInitSubtitleBlock( p_sys );
173 return block_ChainGather( p_spu );
178 /* following functions are local */
180 #define SPU_HEADER_LEN 1
182 /*****************************************************************************
185 The data for single screen subtitle may come in one of many
186 non-contiguous packets of a stream. This routine is called when the
187 next packet in the stream comes in. The job of this routine is to
188 parse the header, if this is the beginning, and combine the packets
189 into one complete subtitle unit.
191 If everything is complete, we will return a block. Otherwise return
194 *****************************************************************************/
196 Reassemble( decoder_t *p_dec, block_t **pp_block )
198 decoder_sys_t *p_sys = p_dec->p_sys;
202 if( pp_block == NULL || *pp_block == NULL )
209 if( p_block->i_buffer < SPU_HEADER_LEN )
211 msg_Dbg( p_dec, "invalid packet header (size %d < %d)" ,
212 p_block->i_buffer, SPU_HEADER_LEN );
213 block_Release( p_block );
217 p_buffer = p_block->p_buffer;
219 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_PACKET),
220 "header: 0x%02x 0x%02x 0x%02x 0x%02x, 0x%02x, 0x%02x, size: %i",
221 p_buffer[1], p_buffer[2], p_buffer[3], p_buffer[4],
222 p_buffer[5], p_buffer[6],
225 if( config_GetInt( p_dec, "spu-channel" ) != p_buffer[0] )
228 /* There is little data on the format, but it does not seem to have a
229 good way to detect the first packet in the subtitle. It seems,
230 however, that it has a valid pts while later packets for the same
233 if ( p_sys->state == SUBTITLE_BLOCK_EMPTY && p_block->i_pts == 0 ) {
235 "first packet expected but no PTS present -- skipped\n");
239 if ( p_sys->subtitle_data_pos == 0 ) {
240 /* First packet in the subtitle block */
241 E_(ParseHeader)( p_dec, p_buffer, p_block );
242 VCDSubInitSubtitleData(p_sys);
245 /* FIXME - remove append_data and use chainappend */
246 VCDSubAppendData( p_dec, p_buffer + SPU_HEADER_LEN,
247 p_block->i_buffer - SPU_HEADER_LEN );
249 block_ChainAppend( &p_sys->p_block, p_block );
251 p_sys->i_spu += p_block->i_buffer - SPU_HEADER_LEN;
253 if ( p_sys->subtitle_data_pos == p_sys->i_spu_size ) {
254 E_(ParseMetaInfo)( p_dec );
255 return p_sys->p_block;
257 /* Not last block in subtitle, so wait for another. */
258 p_sys->state = SUBTITLE_BLOCK_PARTIAL;