]> git.sesse.net Git - vlc/blob - modules/demux/dts.c
Try to close ticket '#1775' (Qt intf startup size is too big)
[vlc] / modules / demux / dts.c
1 /*****************************************************************************
2  * dts.c : raw DTS stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_codec.h>
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open  ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
41
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();
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int Demux  ( demux_t * );
55 static int Control( demux_t *, int, va_list );
56
57 struct demux_sys_t
58 {
59     bool  b_start;
60     es_out_id_t *p_es;
61
62     /* Packetizer */
63     decoder_t *p_packetizer;
64
65     mtime_t i_pts;
66     mtime_t i_time_offset;
67
68     int i_mux_rate;
69 };
70
71 static int CheckSync( const uint8_t *p_peek );
72
73 #define DTS_PACKET_SIZE 16384
74 #define DTS_PROBE_SIZE (DTS_PACKET_SIZE * 4)
75 #define DTS_MAX_HEADER_SIZE 11
76
77 /*****************************************************************************
78  * Open: initializes ES structures
79  *****************************************************************************/
80 static int Open( vlc_object_t * p_this )
81 {
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;
86
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 ) )
90     {
91         /* Find the wave format header */
92         i_peek = 12 + 8;
93         while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
94         {
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;
98
99             i_peek += i_len + 8;
100             if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
101                 return VLC_EGENERIC;
102         }
103
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;
108
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;
120
121         /* Skip the wave header */
122         while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
123         {
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;
127
128             i_peek += i_len + 8;
129             if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
130                 return VLC_EGENERIC;
131         }
132
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;
137
138         while( i_peek < i_size )
139         {
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;
145         }
146     }
147
148     /* Have a peep at the show. */
149     CHECK_PEEK( p_peek, i_peek + DTS_MAX_HEADER_SIZE * 2  );
150
151     if( CheckSync( p_peek + i_peek ) != VLC_SUCCESS )
152     {
153         if( !p_demux->b_force )
154             return VLC_EGENERIC;
155
156         /* User forced */
157         msg_Err( p_demux, "this doesn't look like a DTS audio stream, "
158                  "continuing anyway" );
159     }
160
161     DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
162     p_sys->i_mux_rate = 0;
163     p_sys->i_pts = 0;
164     p_sys->i_time_offset = 0;
165  
166     INIT_APACKETIZER( p_sys->p_packetizer, 'd','t','s',' ' );
167     LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "DTS" );
168
169     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_in );
170
171     return VLC_SUCCESS;
172 }
173
174 /*****************************************************************************
175  * Close: frees unused data
176  *****************************************************************************/
177 static void Close( vlc_object_t *p_this )
178 {
179     demux_t     *p_demux = (demux_t*)p_this;
180     demux_sys_t *p_sys = p_demux->p_sys;
181
182     DESTROY_PACKETIZER( p_sys->p_packetizer );
183
184     free( p_sys );
185 }
186
187 /*****************************************************************************
188  * Demux: reads and demuxes data packets
189  *****************************************************************************
190  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
191  *****************************************************************************/
192 static int Demux( demux_t *p_demux )
193 {
194     demux_sys_t *p_sys = p_demux->p_sys;
195     block_t *p_block_in, *p_block_out;
196
197     if( !( p_block_in = stream_Block( p_demux->s, DTS_PACKET_SIZE ) ) )
198     {
199         return 0;
200     }
201
202     if( p_sys->b_start )
203         p_block_in->i_pts = p_block_in->i_dts = 1;
204     else
205         p_block_in->i_pts = p_block_in->i_dts = 0;
206
207     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
208                 p_sys->p_packetizer, &p_block_in )) )
209     {
210         p_sys->b_start = false;
211
212         while( p_block_out )
213         {
214             block_t *p_next = p_block_out->p_next;
215
216             /* We assume a constant bitrate */
217             if( p_block_out->i_length )
218             {
219                 p_sys->i_mux_rate =
220                     p_block_out->i_buffer * INT64_C(1000000) / p_block_out->i_length;
221             }
222
223             /* Correct timestamp */
224             p_block_out->i_pts += p_sys->i_time_offset;
225             p_block_out->i_dts += p_sys->i_time_offset;
226
227             /* set PCR */
228             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
229
230             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
231
232             p_block_out = p_next;
233         }
234     }
235
236     return 1;
237 }
238
239 /*****************************************************************************
240  * Control:
241  *****************************************************************************/
242 static int Control( demux_t *p_demux, int i_query, va_list args )
243 {
244     demux_sys_t *p_sys  = p_demux->p_sys;
245     bool *pb_bool;
246     int64_t *pi64;
247     int i_ret;
248
249     switch( i_query )
250     {
251     case DEMUX_HAS_UNSUPPORTED_META:
252         pb_bool = (bool*)va_arg( args, bool* );
253         *pb_bool = true;
254         return VLC_SUCCESS;
255
256     case DEMUX_GET_TIME:
257         pi64 = (int64_t*)va_arg( args, int64_t * );
258         *pi64 = p_sys->i_pts + p_sys->i_time_offset;
259         return VLC_SUCCESS;
260
261     case DEMUX_SET_TIME: /* TODO implement a high precicsion seek */
262     default:
263         i_ret = demux_vaControlHelper( p_demux->s,
264                                        0, -1,
265                                        8*p_sys->i_mux_rate, 1, i_query, args );
266         if( !i_ret && p_sys->i_mux_rate > 0 &&
267             ( i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME ) )
268         {
269
270             const int64_t i_time = INT64_C(1000000) * stream_Tell(p_demux->s) /
271                                         p_sys->i_mux_rate;
272
273             /* Fix time_offset */
274             if( i_time >= 0 )
275                 p_sys->i_time_offset = i_time - p_sys->i_pts;
276         }
277         return i_ret;
278     }
279 }
280
281 /*****************************************************************************
282  * CheckSync: Check if buffer starts with a DTS sync code
283  *****************************************************************************/
284 static int CheckSync( const uint8_t *p_peek )
285 {
286     /* 14 bits, little endian version of the bitstream */
287     if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
288         p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
289         (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
290     {
291         return VLC_SUCCESS;
292     }
293     /* 14 bits, big endian version of the bitstream */
294     else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
295              p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
296              p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
297     {
298         return VLC_SUCCESS;
299     }
300     /* 16 bits, big endian version of the bitstream */
301     else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
302              p_peek[2] == 0x80 && p_peek[3] == 0x01 )
303     {
304         return VLC_SUCCESS;
305     }
306     /* 16 bits, little endian version of the bitstream */
307     else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
308              p_peek[2] == 0x01 && p_peek[3] == 0x80 )
309     {
310         return VLC_SUCCESS;
311     }
312
313     return VLC_EGENERIC;
314 }
315