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