1 /*****************************************************************************
2 * dts.c : raw DTS stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: dts.c,v 1.5 2004/02/05 22:56:11 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/input.h>
29 #include <vlc_codec.h>
31 #define DTS_PACKET_SIZE 16384
32 #define DTS_MAX_HEADER_SIZE 11
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close ( vlc_object_t * );
39 static int Demux ( input_thread_t * );
41 static int Control( input_thread_t *, int, va_list );
49 decoder_t *p_packetizer;
54 /*****************************************************************************
56 *****************************************************************************/
58 set_description( _("Raw DTS demuxer") );
59 set_capability( "demux", 155 );
60 set_callbacks( Open, Close );
61 add_shortcut( "dts" );
64 /*****************************************************************************
65 * CheckSync: Check if buffer starts with a DTS sync code
66 *****************************************************************************/
67 int CheckSync( uint8_t *p_peek )
69 /* 14 bits, little endian version of the bitstream */
70 if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
71 p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
72 (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
76 /* 14 bits, big endian version of the bitstream */
77 else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
78 p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
79 p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
83 /* 16 bits, big endian version of the bitstream */
84 else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
85 p_peek[2] == 0x80 && p_peek[3] == 0x01 )
89 /* 16 bits, little endian version of the bitstream */
90 else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
91 p_peek[2] == 0x01 && p_peek[3] == 0x80 )
99 /*****************************************************************************
100 * Open: initializes ES structures
101 *****************************************************************************/
102 static int Open( vlc_object_t * p_this )
104 input_thread_t *p_input = (input_thread_t *)p_this;
109 p_input->pf_demux = Demux;
110 p_input->pf_demux_control = Control;
111 p_input->pf_rewind = NULL;
113 /* Check if we are dealing with a WAV file */
114 if( input_Peek( p_input, &p_peek, 12 ) == 12 &&
115 !strncmp( p_peek, "RIFF", 4 ) && !strncmp( &p_peek[8], "WAVE", 4 ) )
117 /* Skip the wave header */
119 while( input_Peek( p_input, &p_peek, i_peek ) == i_peek &&
120 strncmp( p_peek + i_peek - 8, "data", 4 ) )
122 i_peek += GetDWLE( p_peek + i_peek - 4 ) + 8;
125 /* TODO: should check wave format and sample_rate */
127 /* Some DTS wav files don't begin with a sync code so we do a more
128 * extensive search */
129 if( input_Peek( p_input, &p_peek, i_peek + DTS_PACKET_SIZE ) ==
130 i_peek + DTS_PACKET_SIZE )
132 int i_size = i_peek + DTS_PACKET_SIZE - DTS_MAX_HEADER_SIZE;
134 while( i_peek < i_size )
136 if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
137 /* The data is stored in 16 bits words */
145 /* Have a peep at the show. */
146 if( input_Peek( p_input, &p_peek, i_peek + DTS_MAX_HEADER_SIZE ) <
147 i_peek + DTS_MAX_HEADER_SIZE )
149 /* Stream too short */
150 msg_Err( p_input, "cannot peek()" );
154 if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
156 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "dts", 3 ) )
159 msg_Err( p_input, "this doesn't look like a DTS audio stream, "
160 "continuing anyway" );
168 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
169 p_sys->b_start = VLC_TRUE;
170 p_sys->i_mux_rate = 0;
173 * Load the DTS packetizer
175 p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_DECODER );
176 p_sys->p_packetizer->pf_decode_audio = 0;
177 p_sys->p_packetizer->pf_decode_video = 0;
178 p_sys->p_packetizer->pf_decode_sub = 0;
179 p_sys->p_packetizer->pf_packetize = 0;
181 /* Initialization of decoder structure */
182 es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
183 VLC_FOURCC( 'd', 't', 's', ' ' ) );
185 p_sys->p_packetizer->p_module =
186 module_Need( p_sys->p_packetizer, "packetizer", NULL );
187 if( !p_sys->p_packetizer->p_module )
189 msg_Err( p_input, "cannot find DTS packetizer" );
193 /* Create one program */
194 vlc_mutex_lock( &p_input->stream.stream_lock );
195 if( input_InitStream( p_input, 0 ) == -1 )
197 vlc_mutex_unlock( &p_input->stream.stream_lock );
198 msg_Err( p_input, "cannot init stream" );
201 p_input->stream.i_mux_rate = 0;
202 vlc_mutex_unlock( &p_input->stream.stream_lock );
205 es_out_Add( p_input->p_es_out, &p_sys->p_packetizer->fmt_in );
210 /*****************************************************************************
211 * Close: frees unused data
212 *****************************************************************************/
213 static void Close( vlc_object_t * p_this )
215 input_thread_t *p_input = (input_thread_t*)p_this;
216 demux_sys_t *p_sys = p_input->p_demux_data;
219 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
221 /* Delete the decoder */
222 vlc_object_destroy( p_sys->p_packetizer );
227 /*****************************************************************************
228 * Demux: reads and demuxes data packets
229 *****************************************************************************
230 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
231 *****************************************************************************/
232 static int Demux( input_thread_t * p_input )
234 demux_sys_t *p_sys = p_input->p_demux_data;
235 block_t *p_block_in, *p_block_out;
237 if( !( p_block_in = stream_Block( p_input->s, DTS_PACKET_SIZE ) ) )
244 p_block_in->i_pts = p_block_in->i_dts = 1;
245 p_sys->b_start = VLC_FALSE;
249 p_block_in->i_pts = p_block_in->i_dts = 0;
252 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
253 p_sys->p_packetizer, &p_block_in )) )
257 block_t *p_next = p_block_out->p_next;
259 /* We assume a constant bitrate */
260 if( p_block_out->i_length )
262 p_block_out->i_buffer * I64C(1000000) / p_block_out->i_length;
263 p_input->stream.i_mux_rate = p_sys->i_mux_rate / 50;
265 input_ClockManageRef( p_input,
266 p_input->stream.p_selected_program,
267 p_block_out->i_pts * 9 / 100 );
269 p_block_in->b_discontinuity = 0;
270 p_block_out->i_dts = p_block_out->i_pts =
271 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
272 p_block_out->i_pts * 9 / 100 );
274 es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
276 p_block_out = p_next;
283 /*****************************************************************************
285 *****************************************************************************/
286 static int Control( input_thread_t *p_input, int i_query, va_list args )
288 demux_sys_t *p_sys = (demux_sys_t *)p_input->p_demux_data;
294 pi64 = (int64_t*)va_arg( args, int64_t * );
295 if( p_sys->i_mux_rate > 0 )
297 *pi64 = I64C(1000000) * stream_Tell( p_input->s ) /
304 case DEMUX_GET_LENGTH:
305 pi64 = (int64_t*)va_arg( args, int64_t * );
306 if( p_sys->i_mux_rate > 0 )
308 *pi64 = I64C(1000000) * stream_Size( p_input->s ) /
316 return demux_vaControlDefault( p_input, i_query, args );