]> git.sesse.net Git - vlc/blob - modules/demux/dts.c
* modules/demux/dts.c: support for DTS WAV files.
[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.2 2004/01/21 17:56:05 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  * Open: initializes ES structures
62  *****************************************************************************/
63 static int Open( vlc_object_t * p_this )
64 {
65     input_thread_t *p_input = (input_thread_t *)p_this;
66     demux_sys_t    *p_sys;
67     byte_t *       p_peek;
68     int            i_peek = 0;
69
70     p_input->pf_demux = Demux;
71     p_input->pf_demux_control = demux_vaControlDefault;
72     p_input->pf_rewind = NULL;
73
74     /* Check if we are dealing with a WAV file */
75     if( input_Peek( p_input, &p_peek, 12 ) == 12 &&
76         !strncmp( p_peek, "RIFF", 4 ) && !strncmp( &p_peek[8], "WAVE", 4 ) )
77     {
78         /* Skip the wave header */
79         i_peek = 12 + 8;
80         while( input_Peek( p_input, &p_peek, i_peek ) == i_peek &&
81                strncmp( p_peek + i_peek - 8, "data", 4 ) )
82         {
83             i_peek += GetDWLE( p_peek + i_peek - 4 ) + 8;
84         }
85     }
86
87     /* Have a peep at the show. */
88     if( input_Peek( p_input, &p_peek, i_peek + DTS_MAX_HEADER_SIZE ) <
89         i_peek + DTS_MAX_HEADER_SIZE )
90     {
91         /* Stream too short */
92         msg_Err( p_input, "cannot peek()" );
93         return VLC_EGENERIC;
94     }
95
96     /* 14 bits, little endian version of the bitstream */
97     if( p_peek[i_peek + 0] == 0xff && p_peek[i_peek + 1] == 0x1f &&
98         p_peek[i_peek + 2] == 0x00 && p_peek[i_peek + 3] == 0xe8 &&
99         (p_peek[i_peek + 4] & 0xf0) == 0xf0 && p_peek[i_peek + 5] == 0x07 )
100     {
101     }
102     /* 16 bits, big endian version of the bitstream */
103     else if( p_peek[i_peek + 0] == 0x7f && p_peek[i_peek + 1] == 0xfe &&
104              p_peek[i_peek + 2] == 0x80 && p_peek[i_peek + 3] == 0x01 )
105     {
106     }
107     else
108     {
109         if( p_input->psz_demux && !strncmp( p_input->psz_demux, "dts", 3 ) )
110         {
111             /* User forced */
112             msg_Err( p_input, "this doesn't look like a DTS audio stream, "
113                      "continuing anyway" );
114         }
115         else
116         {
117             return VLC_EGENERIC;
118         }
119     }
120
121     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
122     p_sys->b_start = VLC_TRUE;
123
124     /*
125      * Load the DTS packetizer
126      */
127     p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_DECODER );
128     p_sys->p_packetizer->pf_decode_audio = 0;
129     p_sys->p_packetizer->pf_decode_video = 0;
130     p_sys->p_packetizer->pf_decode_sub = 0;
131     p_sys->p_packetizer->pf_packetize = 0;
132
133     /* Initialization of decoder structure */
134     es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
135                     VLC_FOURCC( 'd', 't', 's', ' ' ) );
136
137     p_sys->p_packetizer->p_module =
138         module_Need( p_sys->p_packetizer, "packetizer", NULL );
139     if( !p_sys->p_packetizer->p_module )
140     {
141         msg_Err( p_input, "cannot find DTS packetizer" );
142         return VLC_EGENERIC;
143     }
144
145     /* Create one program */
146     vlc_mutex_lock( &p_input->stream.stream_lock );
147     if( input_InitStream( p_input, 0 ) == -1 )
148     {
149         vlc_mutex_unlock( &p_input->stream.stream_lock );
150         msg_Err( p_input, "cannot init stream" );
151         return VLC_EGENERIC;
152     }
153     p_input->stream.i_mux_rate = 0;
154     vlc_mutex_unlock( &p_input->stream.stream_lock );
155
156     p_sys->p_es =
157         es_out_Add( p_input->p_es_out, &p_sys->p_packetizer->fmt_in );
158
159     return VLC_SUCCESS;
160 }
161
162 /*****************************************************************************
163  * Close: frees unused data
164  *****************************************************************************/
165 static void Close( vlc_object_t * p_this )
166 {
167     input_thread_t *p_input = (input_thread_t*)p_this;
168     demux_sys_t    *p_sys = p_input->p_demux_data;
169
170     /* Unneed module */
171     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
172
173     /* Delete the decoder */
174     vlc_object_destroy( 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( input_thread_t * p_input )
185 {
186     demux_sys_t  *p_sys = p_input->p_demux_data;
187     block_t *p_block_in, *p_block_out;
188
189     if( !( p_block_in = stream_Block( p_input->s, DTS_PACKET_SIZE ) ) )
190     {
191         return 0;
192     }
193
194     if( p_sys->b_start )
195     {
196         p_block_in->i_pts = p_block_in->i_dts = 1;
197         p_sys->b_start = VLC_FALSE;
198     }
199     else
200     {
201         p_block_in->i_pts = p_block_in->i_dts = 0;
202     }
203
204     while( (p_block_out = p_sys->p_packetizer->pf_packetize(
205                 p_sys->p_packetizer, &p_block_in )) )
206     {
207         while( p_block_out )
208         {
209             block_t *p_next = p_block_out->p_next;
210
211             input_ClockManageRef( p_input,
212                                   p_input->stream.p_selected_program,
213                                   p_block_out->i_pts * 9 / 100 );
214
215             p_block_in->b_discontinuity = 0;
216             p_block_out->i_dts = p_block_out->i_pts =
217                 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
218                                   p_block_out->i_pts * 9 / 100 );
219
220             es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
221
222             p_block_out = p_next;
223         }
224     }
225
226     return 1;
227 }