]> git.sesse.net Git - vlc/blob - modules/demux/dts.c
* modules/demux/dts.c: improved detection of wav dts files.
[vlc] / modules / demux / dts.c
1 /*****************************************************************************
2  * dts.c : raw DTS stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: dts.c,v 1.9 2004/02/24 19:23:11 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  *
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.
18  *
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29 #include <vlc_codec.h>
30
31 #define DTS_PACKET_SIZE 16384
32 #define DTS_PROBE_SIZE (DTS_PACKET_SIZE * 4)
33 #define DTS_MAX_HEADER_SIZE 11
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Open  ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
40 static int  Demux ( input_thread_t * );
41
42 static int Control( input_thread_t *, int, va_list );
43
44 struct demux_sys_t
45 {
46     vlc_bool_t  b_start;
47     es_out_id_t *p_es;
48
49     /* Packetizer */
50     decoder_t *p_packetizer;
51
52     int i_mux_rate;
53 };
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 vlc_module_begin();
59     set_description( _("Raw DTS demuxer") );
60     set_capability( "demux", 155 );
61     set_callbacks( Open, Close );
62     add_shortcut( "dts" );
63 vlc_module_end();
64
65 /*****************************************************************************
66  * CheckSync: Check if buffer starts with a DTS sync code
67  *****************************************************************************/
68 static int CheckSync( uint8_t *p_peek )
69 {
70     /* 14 bits, little endian version of the bitstream */
71     if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
72         p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
73         (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
74     {
75         return VLC_SUCCESS;
76     }
77     /* 14 bits, big endian version of the bitstream */
78     else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
79              p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
80              p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
81     {
82         return VLC_SUCCESS;
83     }
84     /* 16 bits, big endian version of the bitstream */
85     else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
86              p_peek[2] == 0x80 && p_peek[3] == 0x01 )
87     {
88         return VLC_SUCCESS;
89     }
90     /* 16 bits, little endian version of the bitstream */
91     else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
92              p_peek[2] == 0x01 && p_peek[3] == 0x80 )
93     {
94         return VLC_SUCCESS;
95     }
96
97     return VLC_EGENERIC;
98 }
99
100 /*****************************************************************************
101  * Open: initializes ES structures
102  *****************************************************************************/
103 static int Open( vlc_object_t * p_this )
104 {
105     input_thread_t *p_input = (input_thread_t *)p_this;
106     demux_sys_t    *p_sys;
107     byte_t *       p_peek;
108     int            i_peek = 0;
109
110     p_input->pf_demux = Demux;
111     p_input->pf_demux_control = Control;
112     p_input->pf_rewind = NULL;
113
114     /* Check if we are dealing with a WAV file */
115     if( input_Peek( p_input, &p_peek, 20 ) == 20 &&
116         !strncmp( p_peek, "RIFF", 4 ) && !strncmp( &p_peek[8], "WAVE", 4 ) )
117     {
118         int i_size;
119
120         /* Find the wave format header */
121         i_peek = 20;
122         while( strncmp( p_peek + i_peek - 8, "fmt ", 4 ) )
123         {
124             i_size = GetDWLE( p_peek + i_peek - 4 );
125             if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
126             i_peek += i_size + 8;
127
128             if( input_Peek( p_input, &p_peek, i_peek ) != i_peek )
129                 return VLC_EGENERIC;
130         }
131
132         /* Sanity check the wave format header */
133         i_size = GetDWLE( p_peek + i_peek - 4 );
134         if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
135         i_peek += i_size + 8;
136         if( input_Peek( p_input, &p_peek, i_peek ) != i_peek )
137             return VLC_EGENERIC;
138         if( GetWLE( p_peek + i_peek - i_size - 8 /* wFormatTag */ ) !=
139             1 /* WAVE_FORMAT_PCM */ )
140             return VLC_EGENERIC;
141         if( GetWLE( p_peek + i_peek - i_size - 6 /* nChannels */ ) != 2 )
142             return VLC_EGENERIC;
143         if( GetDWLE( p_peek + i_peek - i_size - 4 /* nSamplesPerSec */ ) !=
144             44100 )
145             return VLC_EGENERIC;
146
147         /* Skip the wave header */
148         while( strncmp( p_peek + i_peek - 8, "data", 4 ) )
149         {
150             i_size = GetDWLE( p_peek + i_peek - 4 );
151             if( i_size + i_peek > DTS_PROBE_SIZE ) return VLC_EGENERIC;
152             i_peek += i_size + 8;
153
154             if( input_Peek( p_input, &p_peek, i_peek ) != i_peek )
155                 return VLC_EGENERIC;
156         }
157
158         /* Some DTS wav files don't begin with a sync code so we do a more
159          * extensive search */
160         i_size = input_Peek( p_input, &p_peek, DTS_PROBE_SIZE );
161         i_size -= DTS_MAX_HEADER_SIZE;
162
163         while( i_peek < i_size )
164         {
165             if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
166                 /* The data is stored in 16 bits words */
167                 i_peek += 2;
168             else
169                 break;
170         }
171     }
172
173     /* Have a peep at the show. */
174     if( input_Peek( p_input, &p_peek, i_peek + DTS_MAX_HEADER_SIZE * 2 ) <
175         i_peek + DTS_MAX_HEADER_SIZE * 2 )
176     {
177         /* Stream too short */
178         msg_Warn( p_input, "cannot peek()" );
179         return VLC_EGENERIC;
180     }
181
182     if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
183     {
184         if( p_input->psz_demux && !strncmp( p_input->psz_demux, "dts", 3 ) )
185         {
186             /* User forced */
187             msg_Err( p_input, "this doesn't look like a DTS audio stream, "
188                      "continuing anyway" );
189         }
190         else
191         {
192             return VLC_EGENERIC;
193         }
194     }
195
196     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
197     p_sys->b_start = VLC_TRUE;
198     p_sys->i_mux_rate = 0;
199
200     /*
201      * Load the DTS packetizer
202      */
203     p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_DECODER );
204     p_sys->p_packetizer->pf_decode_audio = 0;
205     p_sys->p_packetizer->pf_decode_video = 0;
206     p_sys->p_packetizer->pf_decode_sub = 0;
207     p_sys->p_packetizer->pf_packetize = 0;
208
209     /* Initialization of decoder structure */
210     es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
211                     VLC_FOURCC( 'd', 't', 's', ' ' ) );
212
213     p_sys->p_packetizer->p_module =
214         module_Need( p_sys->p_packetizer, "packetizer", NULL );
215     if( !p_sys->p_packetizer->p_module )
216     {
217         msg_Err( p_input, "cannot find DTS packetizer" );
218         return VLC_EGENERIC;
219     }
220
221     /* Create one program */
222     vlc_mutex_lock( &p_input->stream.stream_lock );
223     if( input_InitStream( p_input, 0 ) == -1 )
224     {
225         vlc_mutex_unlock( &p_input->stream.stream_lock );
226         msg_Err( p_input, "cannot init stream" );
227         return VLC_EGENERIC;
228     }
229     p_input->stream.i_mux_rate = 0;
230     vlc_mutex_unlock( &p_input->stream.stream_lock );
231
232     p_sys->p_es =
233         es_out_Add( p_input->p_es_out, &p_sys->p_packetizer->fmt_in );
234
235     return VLC_SUCCESS;
236 }
237
238 /*****************************************************************************
239  * Close: frees unused data
240  *****************************************************************************/
241 static void Close( vlc_object_t * p_this )
242 {
243     input_thread_t *p_input = (input_thread_t*)p_this;
244     demux_sys_t    *p_sys = p_input->p_demux_data;
245
246     /* Unneed module */
247     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
248
249     /* Delete the decoder */
250     vlc_object_destroy( p_sys->p_packetizer );
251
252     free( p_sys );
253 }
254
255 /*****************************************************************************
256  * Demux: reads and demuxes data packets
257  *****************************************************************************
258  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
259  *****************************************************************************/
260 static int Demux( input_thread_t * p_input )
261 {
262     demux_sys_t  *p_sys = p_input->p_demux_data;
263     block_t *p_block_in, *p_block_out;
264
265     if( !( p_block_in = stream_Block( p_input->s, DTS_PACKET_SIZE ) ) )
266     {
267         return 0;
268     }
269
270     if( p_sys->b_start )
271         p_block_in->i_pts = p_block_in->i_dts = 1;
272     else
273         p_block_in->i_pts = p_block_in->i_dts = 0;
274
275     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
276                 p_sys->p_packetizer, &p_block_in )) )
277     {
278         p_sys->b_start = VLC_FALSE;
279
280         while( p_block_out )
281         {
282             block_t *p_next = p_block_out->p_next;
283
284             /* We assume a constant bitrate */
285             if( p_block_out->i_length )
286             p_sys->i_mux_rate =
287                 p_block_out->i_buffer * I64C(1000000) / p_block_out->i_length;
288             p_input->stream.i_mux_rate = p_sys->i_mux_rate / 50;
289
290             input_ClockManageRef( p_input,
291                                   p_input->stream.p_selected_program,
292                                   p_block_out->i_pts * 9 / 100 );
293
294             p_block_in->b_discontinuity = 0;
295             p_block_out->i_dts = p_block_out->i_pts =
296                 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
297                                   p_block_out->i_pts * 9 / 100 );
298
299             es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
300
301             p_block_out = p_next;
302         }
303     }
304
305     return 1;
306 }
307
308 /*****************************************************************************
309  * Control:
310  *****************************************************************************/
311 static int Control( input_thread_t *p_input, int i_query, va_list args )
312 {
313     demux_sys_t *p_sys  = (demux_sys_t *)p_input->p_demux_data;
314     int64_t *pi64;
315
316     switch( i_query )
317     {
318         case DEMUX_GET_TIME:
319             pi64 = (int64_t*)va_arg( args, int64_t * );
320             if( p_sys->i_mux_rate > 0 )
321             {
322                 *pi64 = I64C(1000000) * stream_Tell( p_input->s ) /
323                         p_sys->i_mux_rate;
324                 return VLC_SUCCESS;
325             }
326             *pi64 = 0;
327             return VLC_EGENERIC;
328
329         case DEMUX_GET_LENGTH:
330             pi64 = (int64_t*)va_arg( args, int64_t * );
331             if( p_sys->i_mux_rate > 0 )
332             {
333                 *pi64 = I64C(1000000) * stream_Size( p_input->s ) /
334                         p_sys->i_mux_rate;
335                 return VLC_SUCCESS;
336             }
337             *pi64 = 0;
338             return VLC_EGENERIC;
339
340         default:
341             return demux_vaControlDefault( p_input, i_query, args );
342     }
343 }