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