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