]> git.sesse.net Git - vlc/blob - modules/mux/wav.c
Don't include config.h from the headers - refs #297.
[vlc] / modules / mux / wav.c
1 /*****************************************************************************
2  * wav.c: wav muxer module for vlc
3  *****************************************************************************
4  * Copyright (C) 2004, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_aout.h>
34 #include <vlc_sout.h>
35 #include <vlc_block.h>
36 #include <vlc_codecs.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open   ( vlc_object_t * );
42 static void Close  ( vlc_object_t * );
43
44 vlc_module_begin();
45     set_description( _("WAV muxer") );
46     set_capability( "sout mux", 5 );
47     set_category( CAT_SOUT );
48     set_subcategory( SUBCAT_SOUT_MUX );
49     set_callbacks( Open, Close );
50     add_shortcut( "wav" );
51 vlc_module_end();
52
53 /*****************************************************************************
54  * Exported prototypes
55  *****************************************************************************/
56 static int Control  ( sout_mux_t *, int, va_list );
57 static int AddStream( sout_mux_t *, sout_input_t * );
58 static int DelStream( sout_mux_t *, sout_input_t * );
59 static int Mux      ( sout_mux_t * );
60
61 #define MAX_CHANNELS 6
62
63 struct sout_mux_sys_t
64 {
65     vlc_bool_t b_used;
66     vlc_bool_t b_header;
67     vlc_bool_t b_ext;
68
69     uint32_t i_data;
70
71     /* Wave header for the output data */
72     uint32_t waveheader[5];
73     WAVEFORMATEXTENSIBLE waveformat;
74     uint32_t waveheader2[2];
75
76     uint32_t i_channel_mask;
77     vlc_bool_t b_chan_reorder;              /* do we need channel reordering */
78     int pi_chan_table[AOUT_CHAN_MAX];
79 };
80
81
82 static const uint32_t pi_channels_src[] =
83     { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
84       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
85       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
86       AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
87 static const uint32_t pi_channels_in[] =
88     { WAVE_SPEAKER_FRONT_LEFT, WAVE_SPEAKER_FRONT_RIGHT,
89       WAVE_SPEAKER_SIDE_LEFT, WAVE_SPEAKER_SIDE_RIGHT,
90       WAVE_SPEAKER_BACK_LEFT, WAVE_SPEAKER_BACK_RIGHT,
91       WAVE_SPEAKER_FRONT_CENTER, WAVE_SPEAKER_LOW_FREQUENCY, 0 };
92 static const uint32_t pi_channels_out[] =
93     { WAVE_SPEAKER_FRONT_LEFT, WAVE_SPEAKER_FRONT_RIGHT,
94       WAVE_SPEAKER_FRONT_CENTER, WAVE_SPEAKER_LOW_FREQUENCY,
95       WAVE_SPEAKER_BACK_LEFT, WAVE_SPEAKER_BACK_RIGHT,
96       WAVE_SPEAKER_SIDE_LEFT, WAVE_SPEAKER_SIDE_RIGHT, 0 };
97
98 /*****************************************************************************
99  * Open:
100  *****************************************************************************/
101 static int Open( vlc_object_t *p_this )
102 {
103     sout_mux_t *p_mux = (sout_mux_t*)p_this;
104     sout_mux_sys_t  *p_sys;
105
106     p_mux->pf_control  = Control;
107     p_mux->pf_addstream = AddStream;
108     p_mux->pf_delstream = DelStream;
109     p_mux->pf_mux       = Mux;
110
111     p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
112     p_sys->b_used   = VLC_FALSE;
113     p_sys->b_header = VLC_TRUE;
114     p_sys->i_data   = 0;
115
116     p_sys->b_chan_reorder = 0;
117
118     return VLC_SUCCESS;
119 }
120
121 /*****************************************************************************
122  * Close:
123  *****************************************************************************/
124 static void Close( vlc_object_t * p_this )
125 {
126     sout_mux_t *p_mux = (sout_mux_t*)p_this;
127     sout_mux_sys_t *p_sys = p_mux->p_sys;
128     free( p_sys );
129 }
130
131 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
132 {
133     vlc_bool_t *pb_bool;
134     char **ppsz;
135
136    switch( i_query )
137    {
138        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
139            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
140            *pb_bool = VLC_FALSE;
141            return VLC_SUCCESS;
142
143        case MUX_GET_ADD_STREAM_WAIT:
144            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
145            *pb_bool = VLC_TRUE;
146            return VLC_SUCCESS;
147
148        case MUX_GET_MIME:
149            ppsz = (char**)va_arg( args, char ** );
150            *ppsz = strdup( "audio/wav" );
151            return VLC_SUCCESS;
152
153         default:
154             return VLC_EGENERIC;
155    }
156 }
157 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
158 {
159     GUID subformat_guid = {0, 0, 0x10,{0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71}};
160     sout_mux_sys_t *p_sys = p_mux->p_sys;
161     WAVEFORMATEX *p_waveformat = &p_sys->waveformat.Format;
162     int i_bytes_per_sample, i_format;
163     vlc_bool_t b_ext;
164
165     if( p_input->p_fmt->i_cat != AUDIO_ES )
166     {
167         msg_Dbg( p_mux, "not an audio stream" );
168         return VLC_EGENERIC;
169     }
170
171     if( p_sys->b_used )
172     {
173         msg_Dbg( p_mux, "can't add more than 1 stream" );
174         return VLC_EGENERIC;
175     }
176
177     msg_Dbg( p_mux, "adding %i input channels, %iHz",
178              p_input->p_fmt->audio.i_channels,
179              p_input->p_fmt->audio.i_rate );
180
181     p_sys->i_channel_mask = 0;
182     if( p_input->p_fmt->audio.i_physical_channels )
183     {
184         unsigned int i;
185  
186         for( i = 0; i < sizeof(pi_channels_in)/sizeof(uint32_t); i++ )
187         {
188             if( p_input->p_fmt->audio.i_physical_channels & pi_channels_src[i])
189                 p_sys->i_channel_mask |= pi_channels_in[i];
190         }
191
192         p_sys->b_chan_reorder =
193             aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
194                                       p_sys->i_channel_mask,
195                                       p_input->p_fmt->audio.i_channels,
196                                       p_sys->pi_chan_table );
197
198         msg_Dbg( p_mux, "channel mask: %x, reordering: %i",
199                  p_sys->i_channel_mask, (int)p_sys->b_chan_reorder );
200     }
201
202     i_format = p_input->p_fmt->i_codec == VLC_FOURCC('f', 'l', '3', '2') ?
203         WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
204     b_ext = p_sys->b_ext = p_input->p_fmt->audio.i_channels > 2;
205
206     /* Build a WAV header for the output data */
207     p_sys->waveheader[0] = VLC_FOURCC('R', 'I', 'F', 'F'); /* MainChunkID */
208     SetDWLE( &p_sys->waveheader[1], 0 ); /* Length */
209     p_sys->waveheader[2] = VLC_FOURCC('W', 'A', 'V', 'E'); /* ChunkTypeID */
210     p_sys->waveheader[3] = VLC_FOURCC('f', 'm', 't', ' '); /* SubChunkID */
211     SetDWLE( &p_sys->waveheader[4], b_ext ? 40 : 16 ); /* SubChunkLength */
212
213     p_sys->waveheader2[0] = VLC_FOURCC('d', 'a', 't', 'a'); /* DataChunkID */
214     SetDWLE( &p_sys->waveheader2[1], 0 ); /* DataLength */
215
216     /* Build a WAVEVFORMAT header for the output data */
217     memset( &p_sys->waveformat, 0, sizeof(WAVEFORMATEXTENSIBLE) );
218     SetWLE( &p_waveformat->wFormatTag,
219             b_ext ? WAVE_FORMAT_EXTENSIBLE : i_format );
220     SetWLE( &p_waveformat->nChannels,
221             p_input->p_fmt->audio.i_channels );
222     SetDWLE( &p_waveformat->nSamplesPerSec, p_input->p_fmt->audio.i_rate );
223     i_bytes_per_sample = p_input->p_fmt->audio.i_channels *
224         p_input->p_fmt->audio.i_bitspersample / 8;
225     SetDWLE( &p_waveformat->nAvgBytesPerSec,
226              i_bytes_per_sample * p_input->p_fmt->audio.i_rate );
227     SetWLE( &p_waveformat->nBlockAlign, i_bytes_per_sample );
228     SetWLE( &p_waveformat->wBitsPerSample,
229             p_input->p_fmt->audio.i_bitspersample );
230     SetWLE( &p_waveformat->cbSize, 22 );
231     SetWLE( &p_sys->waveformat.Samples.wValidBitsPerSample,
232             p_input->p_fmt->audio.i_bitspersample );
233     SetDWLE( &p_sys->waveformat.dwChannelMask,
234              p_sys->i_channel_mask );
235     p_sys->waveformat.SubFormat = subformat_guid;
236     p_sys->waveformat.SubFormat.Data1 = i_format;
237
238
239     p_sys->b_used = VLC_TRUE;
240
241     return VLC_SUCCESS;
242 }
243
244 static block_t *GetHeader( sout_mux_t *p_mux )
245 {
246     sout_mux_sys_t *p_sys = p_mux->p_sys;
247     block_t *p_block =
248         block_New( p_mux, sizeof( WAVEFORMATEXTENSIBLE ) + 7 * 4 );
249
250     SetDWLE( &p_sys->waveheader[1],
251              20 + (p_sys->b_ext ? 40 : 16) + p_sys->i_data ); /* Length */
252     SetDWLE( &p_sys->waveheader2[1], p_sys->i_data ); /* DataLength */
253
254     memcpy( p_block->p_buffer, &p_sys->waveheader, 5 * 4 );
255     memcpy( p_block->p_buffer + 5 * 4, &p_sys->waveformat,
256             sizeof( WAVEFORMATEXTENSIBLE ) );
257     memcpy( p_block->p_buffer + 5 * 4 +
258             (p_sys->b_ext ? sizeof( WAVEFORMATEXTENSIBLE ) : 16),
259             &p_sys->waveheader2, 2 * 4 );
260     if( !p_sys->b_ext ) p_block->i_buffer -= 24;
261     return p_block;
262 }
263
264 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
265 {
266     msg_Dbg( p_mux, "removing input" );
267
268     msg_Dbg( p_mux, "writing header data" );
269     if( !sout_AccessOutSeek( p_mux->p_access, 0 ) )
270     {
271         sout_AccessOutWrite( p_mux->p_access, GetHeader( p_mux ) );
272     }
273
274     return VLC_SUCCESS;
275 }
276
277 static int Mux( sout_mux_t *p_mux )
278 {
279     sout_mux_sys_t *p_sys = p_mux->p_sys;
280     sout_input_t *p_input;
281
282     if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
283
284     if( p_sys->b_header )
285     {
286         msg_Dbg( p_mux, "writing header data" );
287         sout_AccessOutWrite( p_mux->p_access, GetHeader( p_mux ) );
288     }
289     p_sys->b_header = VLC_FALSE;
290
291     p_input = p_mux->pp_inputs[0];
292     while( block_FifoCount( p_input->p_fifo ) > 0 )
293     {
294         block_t *p_block = block_FifoGet( p_input->p_fifo );
295         p_sys->i_data += p_block->i_buffer;
296
297         /* Do the channel reordering */
298         if( p_sys->b_chan_reorder )
299             aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
300                                  p_input->p_fmt->audio.i_channels,
301                                  p_sys->pi_chan_table,
302                                  p_input->p_fmt->audio.i_bitspersample );
303
304         sout_AccessOutWrite( p_mux->p_access, p_block );
305     }
306
307     return VLC_SUCCESS;
308 }