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