1 /*****************************************************************************
2 * dts.c : raw DTS stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_codec.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
43 set_category( CAT_INPUT );
44 set_subcategory( SUBCAT_INPUT_DEMUX );
45 set_description( N_("Raw DTS demuxer") );
46 set_capability( "demux", 155 );
47 set_callbacks( Open, Close );
48 add_shortcut( "dts" );
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Demux ( demux_t * );
55 static int Control( demux_t *, int, va_list );
63 decoder_t *p_packetizer;
66 mtime_t i_time_offset;
71 static int CheckSync( const uint8_t *p_peek );
73 #define DTS_PACKET_SIZE 16384
74 #define DTS_PROBE_SIZE (DTS_PACKET_SIZE * 4)
75 #define DTS_MAX_HEADER_SIZE 11
77 /*****************************************************************************
78 * Open: initializes ES structures
79 *****************************************************************************/
80 static int Open( vlc_object_t * p_this )
82 demux_t *p_demux = (demux_t*)p_this;
84 const uint8_t *p_peek;
87 /* Check if we are dealing with a WAV file */
88 if( stream_Peek( p_demux->s, &p_peek, 20 ) == 20 &&
89 !memcmp( p_peek, "RIFF", 4 ) && !memcmp( &p_peek[8], "WAVE", 4 ) )
91 /* Find the wave format header */
93 while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
95 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
96 if( i_len > DTS_PROBE_SIZE || i_peek + i_len > DTS_PROBE_SIZE )
100 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
104 /* Sanity check the wave format header */
105 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
106 if( i_len > DTS_PROBE_SIZE )
110 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
112 if( GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ ) !=
113 1 /* WAVE_FORMAT_PCM */ )
115 if( GetWLE( p_peek + i_peek - i_len - 6 /* nChannels */ ) != 2 )
117 if( GetDWLE( p_peek + i_peek - i_len - 4 /* nSamplesPerSec */ ) !=
121 /* Skip the wave header */
122 while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
124 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
125 if( i_len > DTS_PROBE_SIZE || i_peek + i_len > DTS_PROBE_SIZE )
129 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
133 /* Some DTS wav files don't begin with a sync code so we do a more
134 * extensive search */
135 int i_size = stream_Peek( p_demux->s, &p_peek, DTS_PROBE_SIZE );
136 i_size -= DTS_MAX_HEADER_SIZE;
138 while( i_peek < i_size )
140 if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
141 /* The data is stored in 16 bits words */
148 /* Have a peep at the show. */
149 CHECK_PEEK( p_peek, i_peek + DTS_MAX_HEADER_SIZE * 2 );
151 if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
153 if( !p_demux->b_force )
157 msg_Err( p_demux, "this doesn't look like a DTS audio stream, "
158 "continuing anyway" );
161 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
162 p_sys->i_mux_rate = 0;
164 p_sys->i_time_offset = 0;
166 INIT_APACKETIZER( p_sys->p_packetizer, 'd','t','s',' ' );
167 LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "DTS" );
169 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
174 /*****************************************************************************
175 * Close: frees unused data
176 *****************************************************************************/
177 static void Close( vlc_object_t *p_this )
179 demux_t *p_demux = (demux_t*)p_this;
180 demux_sys_t *p_sys = p_demux->p_sys;
182 DESTROY_PACKETIZER( p_sys->p_packetizer );
187 /*****************************************************************************
188 * Demux: reads and demuxes data packets
189 *****************************************************************************
190 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
191 *****************************************************************************/
192 static int Demux( demux_t *p_demux )
194 demux_sys_t *p_sys = p_demux->p_sys;
195 block_t *p_block_in, *p_block_out;
197 if( !( p_block_in = stream_Block( p_demux->s, DTS_PACKET_SIZE ) ) )
203 p_block_in->i_pts = p_block_in->i_dts = 1;
205 p_block_in->i_pts = p_block_in->i_dts = 0;
207 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
208 p_sys->p_packetizer, &p_block_in )) )
210 p_sys->b_start = false;
214 block_t *p_next = p_block_out->p_next;
216 /* We assume a constant bitrate */
217 if( p_block_out->i_length )
220 p_block_out->i_buffer * INT64_C(1000000) / p_block_out->i_length;
223 /* Correct timestamp */
224 p_block_out->i_pts += p_sys->i_time_offset;
225 p_block_out->i_dts += p_sys->i_time_offset;
228 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
230 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
232 p_block_out = p_next;
239 /*****************************************************************************
241 *****************************************************************************/
242 static int Control( demux_t *p_demux, int i_query, va_list args )
244 demux_sys_t *p_sys = p_demux->p_sys;
251 case DEMUX_HAS_UNSUPPORTED_META:
252 pb_bool = (bool*)va_arg( args, bool* );
257 pi64 = (int64_t*)va_arg( args, int64_t * );
258 *pi64 = p_sys->i_pts + p_sys->i_time_offset;
261 case DEMUX_SET_TIME: /* TODO implement a high precicsion seek */
263 i_ret = demux_vaControlHelper( p_demux->s,
265 8*p_sys->i_mux_rate, 1, i_query, args );
266 if( !i_ret && p_sys->i_mux_rate > 0 &&
267 ( i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME ) )
270 const int64_t i_time = INT64_C(1000000) * stream_Tell(p_demux->s) /
273 /* Fix time_offset */
275 p_sys->i_time_offset = i_time - p_sys->i_pts;
281 /*****************************************************************************
282 * CheckSync: Check if buffer starts with a DTS sync code
283 *****************************************************************************/
284 static int CheckSync( const uint8_t *p_peek )
286 /* 14 bits, little endian version of the bitstream */
287 if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
288 p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
289 (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
293 /* 14 bits, big endian version of the bitstream */
294 else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
295 p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
296 p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
300 /* 16 bits, big endian version of the bitstream */
301 else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
302 p_peek[2] == 0x80 && p_peek[3] == 0x01 )
306 /* 16 bits, little endian version of the bitstream */
307 else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
308 p_peek[2] == 0x01 && p_peek[3] == 0x80 )