Added support for DTS to es demuxer.
[vlc/davidf-public.git] / modules / demux / dts.c
blobdc48f719a2b8ec7a766a9d372d91d8a67e98e1f0
1 /*****************************************************************************
2 * dts.c : raw DTS stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_codec.h>
36 /*****************************************************************************
37 * Module descriptor
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
42 vlc_module_begin();
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" );
49 vlc_module_end();
51 /*****************************************************************************
52 * Local prototypes
53 *****************************************************************************/
54 static int Demux ( demux_t * );
55 static int Control( demux_t *, int, va_list );
57 struct demux_sys_t
59 bool b_start;
60 es_out_id_t *p_es;
62 /* Packetizer */
63 decoder_t *p_packetizer;
65 mtime_t i_pts;
66 mtime_t i_time_offset;
68 int i_mux_rate;
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;
83 demux_sys_t *p_sys;
84 const uint8_t *p_peek;
85 int i_peek = 0;
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 */
92 i_peek = 12 + 8;
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 )
97 return VLC_EGENERIC;
99 i_peek += i_len + 8;
100 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
101 return VLC_EGENERIC;
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 )
107 return VLC_EGENERIC;
109 i_peek += i_len + 8;
110 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
111 return VLC_EGENERIC;
112 if( GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ ) !=
113 1 /* WAVE_FORMAT_PCM */ )
114 return VLC_EGENERIC;
115 if( GetWLE( p_peek + i_peek - i_len - 6 /* nChannels */ ) != 2 )
116 return VLC_EGENERIC;
117 if( GetDWLE( p_peek + i_peek - i_len - 4 /* nSamplesPerSec */ ) !=
118 44100 )
119 return VLC_EGENERIC;
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 )
126 return VLC_EGENERIC;
128 i_peek += i_len + 8;
129 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
130 return VLC_EGENERIC;
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 */
142 i_peek += 2;
143 else
144 break;
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 )
154 return VLC_EGENERIC;
156 /* User forced */
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->b_start = true;
163 p_sys->i_mux_rate = 0;
164 p_sys->i_pts = 0;
165 p_sys->i_time_offset = 0;
167 INIT_APACKETIZER( p_sys->p_packetizer, 'd','t','s',' ' );
168 LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "DTS" );
170 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
172 return VLC_SUCCESS;
175 /*****************************************************************************
176 * Close: frees unused data
177 *****************************************************************************/
178 static void Close( vlc_object_t *p_this )
180 demux_t *p_demux = (demux_t*)p_this;
181 demux_sys_t *p_sys = p_demux->p_sys;
183 DESTROY_PACKETIZER( p_sys->p_packetizer );
185 free( p_sys );
188 /*****************************************************************************
189 * Demux: reads and demuxes data packets
190 *****************************************************************************
191 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
192 *****************************************************************************/
193 static int Demux( demux_t *p_demux )
195 demux_sys_t *p_sys = p_demux->p_sys;
196 block_t *p_block_in, *p_block_out;
198 if( !( p_block_in = stream_Block( p_demux->s, DTS_PACKET_SIZE ) ) )
200 return 0;
203 if( p_sys->b_start )
204 p_block_in->i_pts = p_block_in->i_dts = 1;
205 else
206 p_block_in->i_pts = p_block_in->i_dts = 0;
208 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
209 p_sys->p_packetizer, &p_block_in )) )
211 p_sys->b_start = false;
213 while( p_block_out )
215 block_t *p_next = p_block_out->p_next;
217 /* We assume a constant bitrate */
218 if( p_block_out->i_length )
220 p_sys->i_mux_rate =
221 p_block_out->i_buffer * INT64_C(1000000) / p_block_out->i_length;
224 /* Correct timestamp */
225 p_block_out->i_pts += p_sys->i_time_offset;
226 p_block_out->i_dts += p_sys->i_time_offset;
228 /* set PCR */
229 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
231 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
233 p_block_out = p_next;
237 return 1;
240 /*****************************************************************************
241 * Control:
242 *****************************************************************************/
243 static int Control( demux_t *p_demux, int i_query, va_list args )
245 demux_sys_t *p_sys = p_demux->p_sys;
246 bool *pb_bool;
247 int64_t *pi64;
248 int i_ret;
250 switch( i_query )
252 case DEMUX_HAS_UNSUPPORTED_META:
253 pb_bool = (bool*)va_arg( args, bool* );
254 *pb_bool = true;
255 return VLC_SUCCESS;
257 case DEMUX_GET_TIME:
258 pi64 = (int64_t*)va_arg( args, int64_t * );
259 *pi64 = p_sys->i_pts + p_sys->i_time_offset;
260 return VLC_SUCCESS;
262 case DEMUX_SET_TIME: /* TODO implement a high precicsion seek */
263 default:
264 i_ret = demux_vaControlHelper( p_demux->s,
265 0, -1,
266 8*p_sys->i_mux_rate, 1, i_query, args );
267 if( !i_ret && p_sys->i_mux_rate > 0 &&
268 ( i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME ) )
271 const int64_t i_time = INT64_C(1000000) * stream_Tell(p_demux->s) /
272 p_sys->i_mux_rate;
274 /* Fix time_offset */
275 if( i_time >= 0 )
276 p_sys->i_time_offset = i_time - p_sys->i_pts;
278 return i_ret;
282 /*****************************************************************************
283 * CheckSync: Check if buffer starts with a DTS sync code
284 *****************************************************************************/
285 static int CheckSync( const uint8_t *p_peek )
287 /* 14 bits, little endian version of the bitstream */
288 if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
289 p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
290 (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
292 return VLC_SUCCESS;
294 /* 14 bits, big endian version of the bitstream */
295 else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
296 p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
297 p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
299 return VLC_SUCCESS;
301 /* 16 bits, big endian version of the bitstream */
302 else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
303 p_peek[2] == 0x80 && p_peek[3] == 0x01 )
305 return VLC_SUCCESS;
307 /* 16 bits, little endian version of the bitstream */
308 else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
309 p_peek[2] == 0x01 && p_peek[3] == 0x80 )
311 return VLC_SUCCESS;
314 return VLC_EGENERIC;