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_demux.h>
33 #include <vlc_codec.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
42 set_category( CAT_INPUT );
43 set_subcategory( SUBCAT_INPUT_DEMUX );
44 set_description( _("Raw DTS demuxer") );
45 set_capability( "demux2", 155 );
46 set_callbacks( Open, Close );
47 add_shortcut( "dts" );
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Demux ( demux_t * );
54 static int Control( demux_t *, int, va_list );
62 decoder_t *p_packetizer;
67 static int CheckSync( const uint8_t *p_peek );
69 #define DTS_PACKET_SIZE 16384
70 #define DTS_PROBE_SIZE (DTS_PACKET_SIZE * 4)
71 #define DTS_MAX_HEADER_SIZE 11
73 /*****************************************************************************
74 * Open: initializes ES structures
75 *****************************************************************************/
76 static int Open( vlc_object_t * p_this )
78 demux_t *p_demux = (demux_t*)p_this;
83 /* Check if we are dealing with a WAV file */
84 if( stream_Peek( p_demux->s, &p_peek, 20 ) == 20 &&
85 !memcmp( p_peek, "RIFF", 4 ) && !memcmp( &p_peek[8], "WAVE", 4 ) )
89 /* Find the wave format header */
91 while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
93 i_size = GetDWLE( p_peek + i_peek - 4 );
94 if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
97 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
101 /* Sanity check the wave format header */
102 i_size = GetDWLE( p_peek + i_peek - 4 );
103 if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
104 i_peek += i_size + 8;
105 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
107 if( GetWLE( p_peek + i_peek - i_size - 8 /* wFormatTag */ ) !=
108 1 /* WAVE_FORMAT_PCM */ )
110 if( GetWLE( p_peek + i_peek - i_size - 6 /* nChannels */ ) != 2 )
112 if( GetDWLE( p_peek + i_peek - i_size - 4 /* nSamplesPerSec */ ) !=
116 /* Skip the wave header */
117 while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
119 i_size = GetDWLE( p_peek + i_peek - 4 );
120 if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
121 i_peek += i_size + 8;
123 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
127 /* Some DTS wav files don't begin with a sync code so we do a more
128 * extensive search */
129 i_size = stream_Peek( p_demux->s, &p_peek, DTS_PROBE_SIZE );
130 i_size -= DTS_MAX_HEADER_SIZE;
132 while( i_peek < i_size )
134 if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
135 /* The data is stored in 16 bits words */
142 /* Have a peep at the show. */
143 CHECK_PEEK( p_peek, i_peek + DTS_MAX_HEADER_SIZE * 2 );
145 if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
147 if( !p_demux->b_force )
151 msg_Err( p_demux, "this doesn't look like a DTS audio stream, "
152 "continuing anyway" );
155 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
157 INIT_APACKETIZER( p_sys->p_packetizer, 'd','t','s',' ' );
158 LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "DTS" );
160 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
165 /*****************************************************************************
166 * Close: frees unused data
167 *****************************************************************************/
168 static void Close( vlc_object_t *p_this )
170 demux_t *p_demux = (demux_t*)p_this;
171 demux_sys_t *p_sys = p_demux->p_sys;
173 DESTROY_PACKETIZER( p_sys->p_packetizer );
178 /*****************************************************************************
179 * Demux: reads and demuxes data packets
180 *****************************************************************************
181 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
182 *****************************************************************************/
183 static int Demux( demux_t *p_demux )
185 demux_sys_t *p_sys = p_demux->p_sys;
186 block_t *p_block_in, *p_block_out;
188 if( !( p_block_in = stream_Block( p_demux->s, DTS_PACKET_SIZE ) ) )
194 p_block_in->i_pts = p_block_in->i_dts = 1;
196 p_block_in->i_pts = p_block_in->i_dts = 0;
198 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
199 p_sys->p_packetizer, &p_block_in )) )
201 p_sys->b_start = VLC_FALSE;
205 block_t *p_next = p_block_out->p_next;
207 /* We assume a constant bitrate */
208 if( p_block_out->i_length )
211 p_block_out->i_buffer * I64C(1000000) / p_block_out->i_length;
215 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
217 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
219 p_block_out = p_next;
226 /*****************************************************************************
228 *****************************************************************************/
229 static int Control( demux_t *p_demux, int i_query, va_list args )
231 demux_sys_t *p_sys = p_demux->p_sys;
232 if( i_query == DEMUX_SET_TIME )
235 return demux2_vaControlHelper( p_demux->s,
237 8*p_sys->i_mux_rate, 1, i_query, args );
240 /*****************************************************************************
241 * CheckSync: Check if buffer starts with a DTS sync code
242 *****************************************************************************/
243 static int CheckSync( const uint8_t *p_peek )
245 /* 14 bits, little endian version of the bitstream */
246 if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
247 p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
248 (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
252 /* 14 bits, big endian version of the bitstream */
253 else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
254 p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
255 p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
259 /* 16 bits, big endian version of the bitstream */
260 else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
261 p_peek[2] == 0x80 && p_peek[3] == 0x01 )
265 /* 16 bits, little endian version of the bitstream */
266 else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
267 p_peek[2] == 0x01 && p_peek[3] == 0x80 )