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