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
->b_start
= true;
163 p_sys
->i_mux_rate
= 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
);
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
);
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
) ) )
204 p_block_in
->i_pts
= p_block_in
->i_dts
= 1;
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;
215 block_t
*p_next
= p_block_out
->p_next
;
217 /* We assume a constant bitrate */
218 if( p_block_out
->i_length
)
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
;
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
;
240 /*****************************************************************************
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
;
252 case DEMUX_HAS_UNSUPPORTED_META
:
253 pb_bool
= (bool*)va_arg( args
, bool* );
258 pi64
= (int64_t*)va_arg( args
, int64_t * );
259 *pi64
= p_sys
->i_pts
+ p_sys
->i_time_offset
;
262 case DEMUX_SET_TIME
: /* TODO implement a high precicsion seek */
264 i_ret
= demux_vaControlHelper( p_demux
->s
,
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
) /
274 /* Fix time_offset */
276 p_sys
->i_time_offset
= i_time
- p_sys
->i_pts
;
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 )
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)
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 )
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 )