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 *****************************************************************************/
28 #include <vlc_demux.h>
29 #include <vlc_codec.h>
31 /*****************************************************************************
33 *****************************************************************************/
34 static int Open ( vlc_object_t * );
35 static void Close ( vlc_object_t * );
38 set_category( CAT_INPUT );
39 set_subcategory( SUBCAT_INPUT_DEMUX );
40 set_description( _("Raw DTS demuxer") );
41 set_capability( "demux2", 155 );
42 set_callbacks( Open, Close );
43 add_shortcut( "dts" );
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Demux ( demux_t * );
50 static int Control( demux_t *, int, va_list );
58 decoder_t *p_packetizer;
63 static int CheckSync( const uint8_t *p_peek );
65 #define DTS_PACKET_SIZE 16384
66 #define DTS_PROBE_SIZE (DTS_PACKET_SIZE * 4)
67 #define DTS_MAX_HEADER_SIZE 11
69 /*****************************************************************************
70 * Open: initializes ES structures
71 *****************************************************************************/
72 static int Open( vlc_object_t * p_this )
74 demux_t *p_demux = (demux_t*)p_this;
79 /* Check if we are dealing with a WAV file */
80 if( stream_Peek( p_demux->s, &p_peek, 20 ) == 20 &&
81 !memcmp( p_peek, "RIFF", 4 ) && !memcmp( &p_peek[8], "WAVE", 4 ) )
85 /* Find the wave format header */
87 while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
89 i_size = GetDWLE( p_peek + i_peek - 4 );
90 if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
93 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
97 /* Sanity check the wave format header */
98 i_size = GetDWLE( p_peek + i_peek - 4 );
99 if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
100 i_peek += i_size + 8;
101 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
103 if( GetWLE( p_peek + i_peek - i_size - 8 /* wFormatTag */ ) !=
104 1 /* WAVE_FORMAT_PCM */ )
106 if( GetWLE( p_peek + i_peek - i_size - 6 /* nChannels */ ) != 2 )
108 if( GetDWLE( p_peek + i_peek - i_size - 4 /* nSamplesPerSec */ ) !=
112 /* Skip the wave header */
113 while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
115 i_size = GetDWLE( p_peek + i_peek - 4 );
116 if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
117 i_peek += i_size + 8;
119 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
123 /* Some DTS wav files don't begin with a sync code so we do a more
124 * extensive search */
125 i_size = stream_Peek( p_demux->s, &p_peek, DTS_PROBE_SIZE );
126 i_size -= DTS_MAX_HEADER_SIZE;
128 while( i_peek < i_size )
130 if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
131 /* The data is stored in 16 bits words */
138 /* Have a peep at the show. */
139 CHECK_PEEK( p_peek, i_peek + DTS_MAX_HEADER_SIZE * 2 );
141 if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
143 if( strncmp( p_demux->psz_demux, "dts", 3 ) )
148 msg_Err( p_demux, "this doesn't look like a DTS audio stream, "
149 "continuing anyway" );
152 STANDARD_DEMUX_INIT; p_sys = p_demux->p_sys;
154 INIT_APACKETIZER( p_sys->p_packetizer, 'd','t','s',' ' );
155 LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "DTS" );
157 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
162 /*****************************************************************************
163 * Close: frees unused data
164 *****************************************************************************/
165 static void Close( vlc_object_t *p_this )
167 demux_t *p_demux = (demux_t*)p_this;
168 demux_sys_t *p_sys = p_demux->p_sys;
170 DESTROY_PACKETIZER( p_sys->p_packetizer );
175 /*****************************************************************************
176 * Demux: reads and demuxes data packets
177 *****************************************************************************
178 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
179 *****************************************************************************/
180 static int Demux( demux_t *p_demux )
182 demux_sys_t *p_sys = p_demux->p_sys;
183 block_t *p_block_in, *p_block_out;
185 if( !( p_block_in = stream_Block( p_demux->s, DTS_PACKET_SIZE ) ) )
191 p_block_in->i_pts = p_block_in->i_dts = 1;
193 p_block_in->i_pts = p_block_in->i_dts = 0;
195 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
196 p_sys->p_packetizer, &p_block_in )) )
198 p_sys->b_start = VLC_FALSE;
202 block_t *p_next = p_block_out->p_next;
204 /* We assume a constant bitrate */
205 if( p_block_out->i_length )
208 p_block_out->i_buffer * I64C(1000000) / p_block_out->i_length;
212 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
214 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
216 p_block_out = p_next;
223 /*****************************************************************************
225 *****************************************************************************/
226 static int Control( demux_t *p_demux, int i_query, va_list args )
228 demux_sys_t *p_sys = p_demux->p_sys;
229 if( i_query == DEMUX_SET_TIME )
232 return demux2_vaControlHelper( p_demux->s,
234 8*p_sys->i_mux_rate, 1, i_query, args );
237 /*****************************************************************************
238 * CheckSync: Check if buffer starts with a DTS sync code
239 *****************************************************************************/
240 static int CheckSync( const uint8_t *p_peek )
242 /* 14 bits, little endian version of the bitstream */
243 if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
244 p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
245 (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
249 /* 14 bits, big endian version of the bitstream */
250 else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
251 p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
252 p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
256 /* 16 bits, big endian version of the bitstream */
257 else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
258 p_peek[2] == 0x80 && p_peek[3] == 0x01 )
262 /* 16 bits, little endian version of the bitstream */
263 else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
264 p_peek[2] == 0x01 && p_peek[3] == 0x80 )