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