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