1 /*****************************************************************************
2 * cvd.c : CVD Subtitle decoder thread
3 *****************************************************************************
4 * Copyright (C) 2003, 2004 VideoLAN
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 VCDSubOpen ( vlc_object_t * );
43 static int PacketizerOpen( vlc_object_t * );
46 set_description( _("CVD subtitle decoder") );
47 set_capability( "decoder", 50 );
48 set_callbacks( VCDSubOpen, VCDSubClose );
50 add_integer ( MODULE_STRING "-debug", 0, NULL,
51 DEBUG_TEXT, DEBUG_LONGTEXT, VLC_TRUE );
53 add_integer ( MODULE_STRING "-horizontal-correct", 0, NULL,
54 HORIZONTAL_CORRECT, HORIZONTAL_CORRECT_LONGTEXT, VLC_FALSE );
56 add_integer ( MODULE_STRING "-vertical-correct", 0, NULL,
57 VERTICAL_CORRECT, VERTICAL_CORRECT_LONGTEXT, VLC_FALSE );
59 add_string( MODULE_STRING "-aspect-ratio", "", NULL,
60 SUB_ASPECT_RATIO_TEXT, SUB_ASPECT_RATIO_LONGTEXT,
63 add_integer( MODULE_STRING "-duration-scaling", 3, NULL,
64 DURATION_SCALE_TEXT, DURATION_SCALE_LONGTEXT,
68 set_description( _("Chaoji VCD subtitle packetizer") );
69 set_capability( "packetizer", 50 );
70 set_callbacks( PacketizerOpen, VCDSubClose );
73 /*****************************************************************************
75 *****************************************************************************/
77 static block_t *Reassemble( decoder_t *, block_t ** );
78 static void Decode ( decoder_t *, block_t ** );
79 static block_t *Packetize( decoder_t *, block_t ** );
82 /*****************************************************************************
84 *****************************************************************************
85 * Tries to launch a decoder and return score so that the interface is able
87 *****************************************************************************/
89 VCDSubOpen( vlc_object_t *p_this )
91 decoder_t *p_dec = (decoder_t*)p_this;
94 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'c','v','d',' ' ) )
100 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
102 p_sys->i_debug = config_GetInt( p_this, MODULE_STRING "-debug" );
103 p_sys->b_packetizer = VLC_FALSE;
104 p_sys->p_vout = NULL;
106 p_sys->subtitle_data = NULL;
108 VCDSubInitSubtitleBlock( p_sys );
110 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'c','v','d',' ' ) );
113 p_dec->pf_decode_sub = Decode;
114 p_dec->pf_packetize = Packetize;
116 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_EXT) , "");
121 /*****************************************************************************
123 *****************************************************************************
124 * Tries to launch a decoder and return score so that the interface is able
126 *****************************************************************************/
127 static int PacketizerOpen( vlc_object_t *p_this )
129 decoder_t *p_dec = (decoder_t*)p_this;
131 if( VCDSubOpen( p_this ) )
135 p_dec->p_sys->b_packetizer = VLC_TRUE;
140 /*****************************************************************************
142 *****************************************************************************/
144 Decode ( decoder_t *p_dec, block_t **pp_block )
146 decoder_sys_t *p_sys = p_dec->p_sys;
147 block_t *p_spu = Reassemble( p_dec, pp_block );
149 dbg_print( (DECODE_DBG_CALL) , "");
153 p_sys->i_spu = block_ChainExtract( p_spu, p_sys->buffer, 65536 );
154 p_sys->i_pts = p_spu->i_pts;
155 block_ChainRelease( p_spu );
157 if( ( p_sys->p_vout = VCDSubFindVout( p_dec ) ) )
159 /* Parse and decode */
160 E_(ParsePacket)( p_dec );
162 vlc_object_release( p_sys->p_vout );
165 VCDSubInitSubtitleBlock ( p_sys );
170 /*****************************************************************************
172 *****************************************************************************/
174 Packetize( decoder_t *p_dec, block_t **pp_block )
176 decoder_sys_t *p_sys = p_dec->p_sys;
177 block_t *p_spu = Reassemble( p_dec, pp_block );
181 p_spu->i_dts = p_spu->i_pts;
184 VCDSubInitSubtitleBlock( p_sys );
186 return block_ChainGather( p_spu );
191 /* following functions are local */
193 #define SPU_HEADER_LEN 1
195 /*****************************************************************************
198 Data for single screen subtitle may come in several non-contiguous
199 packets of a stream. This routine is called when the next packet in
200 the stream comes in. The job of this routine is to parse the header,
201 if this is the beginning, and combine the packets into one complete
204 If everything is complete, we will return a block. Otherwise return
207 *****************************************************************************/
209 Reassemble( decoder_t *p_dec, block_t **pp_block )
211 decoder_sys_t *p_sys = p_dec->p_sys;
215 if( pp_block == NULL || *pp_block == NULL )
222 if( p_block->i_buffer < SPU_HEADER_LEN )
224 msg_Dbg( p_dec, "invalid packet header (size %d < %d)" ,
225 p_block->i_buffer, SPU_HEADER_LEN );
226 block_Release( p_block );
230 p_buffer = p_block->p_buffer;
232 dbg_print( (DECODE_DBG_CALL|DECODE_DBG_PACKET),
233 "header: 0x%02x 0x%02x 0x%02x 0x%02x, 0x%02x, 0x%02x, size: %i",
234 p_buffer[1], p_buffer[2], p_buffer[3], p_buffer[4],
235 p_buffer[5], p_buffer[6],
239 /* Attach to our input thread and see if subtitle is selected. */
241 vlc_object_t * p_input;
244 p_input = vlc_object_find( p_dec, VLC_OBJECT_INPUT, FIND_PARENT );
246 if( !p_input ) return NULL;
248 if( var_Get( p_input, "spu-channel", &val ) ) return NULL;
250 /* Number could be 0bd, 1bd, 2bd, 3bd for 0..3. If so
253 if ( (val.i_int & 0xff) == 0xbd ) val.i_int >>= 8;
255 if( val.i_int == -1 || val.i_int != p_buffer[0] )
260 /* From the scant data on the format, there is only only way known
261 to detect the first packet in a subtitle. The first packet
262 seems to have a valid PTS while later packets for the same
265 if ( p_sys->state == SUBTITLE_BLOCK_EMPTY && p_block->i_pts == 0 ) {
267 "first packet expected but no PTS present -- skipped\n");
271 if ( p_sys->subtitle_data_pos == 0 ) {
272 /* First packet in the subtitle block */
273 E_(ParseHeader)( p_dec, p_buffer, p_block );
274 VCDSubInitSubtitleData(p_sys);
277 /* FIXME - remove append_data and use chainappend */
278 VCDSubAppendData( p_dec, p_buffer + SPU_HEADER_LEN,
279 p_block->i_buffer - SPU_HEADER_LEN );
281 block_ChainAppend( &p_sys->p_block, p_block );
283 p_sys->i_spu += p_block->i_buffer - SPU_HEADER_LEN;
285 if ( p_sys->subtitle_data_pos == p_sys->i_spu_size ) {
286 E_(ParseMetaInfo)( p_dec );
287 return p_sys->p_block;
289 /* Not last block in subtitle, so wait for another. */
290 p_sys->state = SUBTITLE_BLOCK_PARTIAL;