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 *****************************************************************************/
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;
68 static int CheckSync( const uint8_t *p_peek );
70 #define DTS_PACKET_SIZE 16384
71 #define DTS_PROBE_SIZE (DTS_PACKET_SIZE * 4)
72 #define DTS_MAX_HEADER_SIZE 11
74 /*****************************************************************************
75 * Open: initializes ES structures
76 *****************************************************************************/
77 static int Open( vlc_object_t * p_this )
79 demux_t *p_demux = (demux_t*)p_this;
81 const uint8_t *p_peek;
84 /* Check if we are dealing with a WAV file */
85 if( stream_Peek( p_demux->s, &p_peek, 20 ) == 20 &&
86 !memcmp( p_peek, "RIFF", 4 ) && !memcmp( &p_peek[8], "WAVE", 4 ) )
90 /* Find the wave format header */
92 while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
94 i_size = GetDWLE( p_peek + i_peek - 4 );
95 if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
98 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
102 /* Sanity check the wave format header */
103 i_size = GetDWLE( p_peek + i_peek - 4 );
104 if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
105 i_peek += i_size + 8;
106 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
108 if( GetWLE( p_peek + i_peek - i_size - 8 /* wFormatTag */ ) !=
109 1 /* WAVE_FORMAT_PCM */ )
111 if( GetWLE( p_peek + i_peek - i_size - 6 /* nChannels */ ) != 2 )
113 if( GetDWLE( p_peek + i_peek - i_size - 4 /* nSamplesPerSec */ ) !=
117 /* Skip the wave header */
118 while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
120 i_size = GetDWLE( p_peek + i_peek - 4 );
121 if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
122 i_peek += i_size + 8;
124 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
128 /* Some DTS wav files don't begin with a sync code so we do a more
129 * extensive search */
130 i_size = stream_Peek( p_demux->s, &p_peek, DTS_PROBE_SIZE );
131 i_size -= DTS_MAX_HEADER_SIZE;
133 while( i_peek < i_size )
135 if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
136 /* The data is stored in 16 bits words */
143 /* Have a peep at the show. */
144 CHECK_PEEK( p_peek, i_peek + DTS_MAX_HEADER_SIZE * 2 );
146 if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
148 if( !p_demux->b_force )
152 msg_Err( p_demux, "this doesn't look like a DTS audio stream, "
153 "continuing anyway" );
156 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
158 INIT_APACKETIZER( p_sys->p_packetizer, 'd','t','s',' ' );
159 LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "DTS" );
161 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
166 /*****************************************************************************
167 * Close: frees unused data
168 *****************************************************************************/
169 static void Close( vlc_object_t *p_this )
171 demux_t *p_demux = (demux_t*)p_this;
172 demux_sys_t *p_sys = p_demux->p_sys;
174 DESTROY_PACKETIZER( p_sys->p_packetizer );
179 /*****************************************************************************
180 * Demux: reads and demuxes data packets
181 *****************************************************************************
182 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
183 *****************************************************************************/
184 static int Demux( demux_t *p_demux )
186 demux_sys_t *p_sys = p_demux->p_sys;
187 block_t *p_block_in, *p_block_out;
189 if( !( p_block_in = stream_Block( p_demux->s, DTS_PACKET_SIZE ) ) )
195 p_block_in->i_pts = p_block_in->i_dts = 1;
197 p_block_in->i_pts = p_block_in->i_dts = 0;
199 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
200 p_sys->p_packetizer, &p_block_in )) )
202 p_sys->b_start = false;
206 block_t *p_next = p_block_out->p_next;
208 /* We assume a constant bitrate */
209 if( p_block_out->i_length )
212 p_block_out->i_buffer * INT64_C(1000000) / p_block_out->i_length;
216 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
218 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
220 p_block_out = p_next;
227 /*****************************************************************************
229 *****************************************************************************/
230 static int Control( demux_t *p_demux, int i_query, va_list args )
232 demux_sys_t *p_sys = p_demux->p_sys;
233 if( i_query == DEMUX_SET_TIME )
236 return demux_vaControlHelper( p_demux->s,
238 8*p_sys->i_mux_rate, 1, i_query, args );
241 /*****************************************************************************
242 * CheckSync: Check if buffer starts with a DTS sync code
243 *****************************************************************************/
244 static int CheckSync( const uint8_t *p_peek )
246 /* 14 bits, little endian version of the bitstream */
247 if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
248 p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
249 (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
253 /* 14 bits, big endian version of the bitstream */
254 else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
255 p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
256 p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
260 /* 16 bits, big endian version of the bitstream */
261 else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
262 p_peek[2] == 0x80 && p_peek[3] == 0x01 )
266 /* 16 bits, little endian version of the bitstream */
267 else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
268 p_peek[2] == 0x01 && p_peek[3] == 0x80 )