]> git.sesse.net Git - vlc/blob - modules/mux/wav.c
* modules/mux/wav.c: small cleanup.
[vlc] / modules / mux / wav.c
1 /*****************************************************************************
2  * wav.c: wav muxer module for vlc
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/aout.h>
31 #include <vlc/sout.h>
32
33 #include "codecs.h"
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int  Open   ( vlc_object_t * );
39 static void Close  ( vlc_object_t * );
40
41 vlc_module_begin();
42     set_description( _("WAV muxer") );
43     set_capability( "sout mux", 5 );
44     set_callbacks( Open, Close );
45     add_shortcut( "wav" );
46 vlc_module_end();
47
48 /*****************************************************************************
49  * Exported prototypes
50  *****************************************************************************/
51 static int Control  ( sout_mux_t *, int, va_list );
52 static int AddStream( sout_mux_t *, sout_input_t * );
53 static int DelStream( sout_mux_t *, sout_input_t * );
54 static int Mux      ( sout_mux_t * );
55
56 #define MAX_CHANNELS 6
57
58 struct sout_mux_sys_t
59 {
60     vlc_bool_t b_used;
61     vlc_bool_t b_header;
62     vlc_bool_t b_ext;
63
64     uint32_t i_data;
65
66     /* Wave header for the output data */
67     uint32_t waveheader[5];
68     WAVEFORMATEXTENSIBLE waveformat;
69     uint32_t waveheader2[2];
70
71     uint32_t i_channel_mask;
72     vlc_bool_t b_chan_reorder;              /* do we need channel reordering */
73     int pi_chan_table[MAX_CHANNELS];
74 };
75
76
77 static const uint32_t pi_channels_in[] =
78     { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
79       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
80       AOUT_CHAN_CENTER, AOUT_CHAN_LFE };
81 static const uint32_t pi_channels_out[] =
82     { WAVE_SPEAKER_FRONT_LEFT, WAVE_SPEAKER_FRONT_RIGHT,
83       WAVE_SPEAKER_BACK_LEFT, WAVE_SPEAKER_BACK_RIGHT,
84       WAVE_SPEAKER_FRONT_CENTER, WAVE_SPEAKER_LOW_FREQUENCY };
85 static const uint32_t pi_channels_ordered[] =
86     { WAVE_SPEAKER_FRONT_LEFT, WAVE_SPEAKER_FRONT_RIGHT,
87       WAVE_SPEAKER_FRONT_CENTER, WAVE_SPEAKER_LOW_FREQUENCY,
88       WAVE_SPEAKER_BACK_LEFT, WAVE_SPEAKER_BACK_RIGHT };
89
90 static void CheckReordering( sout_mux_t *p_mux, int i_nb_channels );
91 static void InterleaveS16( int16_t *p_buf, int i_buf, int *pi_chan_table,
92                            int i_nb_channels );
93 static void InterleaveFloat32( float *p_buf, int i_buf, int *pi_chan_table,
94                                int i_nb_channels );
95
96 /*****************************************************************************
97  * Open:
98  *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
100 {
101     sout_mux_t *p_mux = (sout_mux_t*)p_this;
102     sout_mux_sys_t  *p_sys;
103
104     p_mux->pf_control  = Control;
105     p_mux->pf_addstream = AddStream;
106     p_mux->pf_delstream = DelStream;
107     p_mux->pf_mux       = Mux;
108
109     p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
110     p_sys->b_used   = VLC_FALSE;
111     p_sys->b_header = VLC_TRUE;
112     p_sys->i_data   = 0;
113
114     p_sys->b_chan_reorder = 0;
115
116     return VLC_SUCCESS;
117 }
118
119 /*****************************************************************************
120  * Close:
121  *****************************************************************************/
122 static void Close( vlc_object_t * p_this )
123 {
124     sout_mux_t *p_mux = (sout_mux_t*)p_this;
125     sout_mux_sys_t *p_sys = p_mux->p_sys;
126     free( p_sys );
127 }
128
129 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
130 {
131     vlc_bool_t *pb_bool;
132     char **ppsz;
133
134    switch( i_query )
135    {
136        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
137            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
138            *pb_bool = VLC_FALSE;
139            return VLC_SUCCESS;
140
141        case MUX_GET_ADD_STREAM_WAIT:
142            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
143            *pb_bool = VLC_TRUE;
144            return VLC_SUCCESS;
145
146        case MUX_GET_MIME:
147            ppsz = (char**)va_arg( args, char ** );
148            *ppsz = strdup( "audio/wav" );
149            return VLC_SUCCESS;
150
151         default:
152             return VLC_EGENERIC;
153    }
154 }
155 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
156 {
157     GUID subformat_guid = {0, 0, 0x10,{0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71}};
158     sout_mux_sys_t *p_sys = p_mux->p_sys;
159     WAVEFORMATEX *p_waveformat = &p_sys->waveformat.Format;
160     int i_bytes_per_sample, i_format, i;
161     vlc_bool_t b_ext;
162
163     if( p_input->p_fmt->i_cat != AUDIO_ES )
164     {
165         msg_Dbg( p_mux, "not an audio stream" );
166         return VLC_EGENERIC;
167     }
168
169     if( p_sys->b_used )
170     {
171         msg_Dbg( p_mux, "can't add more than 1 stream" );
172         return VLC_EGENERIC;
173     }
174
175     msg_Dbg( p_mux, "adding input %i channels, %iHz",
176              p_input->p_fmt->audio.i_channels,
177              p_input->p_fmt->audio.i_rate );
178
179     p_sys->i_channel_mask = 0;
180     if( p_input->p_fmt->audio.i_physical_channels )
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_in[i] )
185                 p_sys->i_channel_mask |= pi_channels_out[i];
186         }
187         msg_Dbg( p_mux, "channel mask: %x", p_sys->i_channel_mask );
188         CheckReordering( p_mux, p_input->p_fmt->audio.i_channels );
189     }
190
191     i_format = p_input->p_fmt->i_codec == VLC_FOURCC('f', 'l', '3', '2') ?
192         WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
193     b_ext = p_sys->b_ext = p_input->p_fmt->audio.i_channels > 2;
194
195     /* Build a WAV header for the output data */
196     p_sys->waveheader[0] = VLC_FOURCC('R', 'I', 'F', 'F'); /* MainChunkID */
197     SetDWLE( &p_sys->waveheader[1], 0 ); /* Length */
198     p_sys->waveheader[2] = VLC_FOURCC('W', 'A', 'V', 'E'); /* ChunkTypeID */
199     p_sys->waveheader[3] = VLC_FOURCC('f', 'm', 't', ' '); /* SubChunkID */
200     SetDWLE( &p_sys->waveheader[4], b_ext ? 40 : 16 ); /* SubChunkLength */
201
202     p_sys->waveheader2[0] = VLC_FOURCC('d', 'a', 't', 'a'); /* DataChunkID */
203     SetDWLE( &p_sys->waveheader2[1], 0 ); /* DataLength */
204
205     /* Build a WAVEVFORMAT header for the output data */
206     memset( &p_sys->waveformat, 0, sizeof(WAVEFORMATEXTENSIBLE) );
207     SetWLE( &p_waveformat->wFormatTag,
208             b_ext ? WAVE_FORMAT_EXTENSIBLE : i_format );
209     SetWLE( &p_waveformat->nChannels,
210             p_input->p_fmt->audio.i_channels );
211     SetDWLE( &p_waveformat->nSamplesPerSec, p_input->p_fmt->audio.i_rate );
212     i_bytes_per_sample = p_input->p_fmt->audio.i_channels *
213         p_input->p_fmt->audio.i_bitspersample / 8;
214     SetDWLE( &p_waveformat->nAvgBytesPerSec,
215              i_bytes_per_sample * p_input->p_fmt->audio.i_rate );
216     SetWLE( &p_waveformat->nBlockAlign, i_bytes_per_sample );
217     SetWLE( &p_waveformat->wBitsPerSample,
218             p_input->p_fmt->audio.i_bitspersample );
219     SetWLE( &p_waveformat->cbSize, 22 );
220     SetWLE( &p_sys->waveformat.Samples.wValidBitsPerSample,
221             p_input->p_fmt->audio.i_bitspersample );
222     SetDWLE( &p_sys->waveformat.dwChannelMask,
223              p_sys->i_channel_mask );
224     p_sys->waveformat.SubFormat = subformat_guid;
225     p_sys->waveformat.SubFormat.Data1 = i_format;
226
227
228     p_sys->b_used = VLC_TRUE;
229
230     return VLC_SUCCESS;
231 }
232
233 static block_t *GetHeader( sout_mux_t *p_mux )
234 {
235     sout_mux_sys_t *p_sys = p_mux->p_sys;
236     block_t *p_block =
237         block_New( p_mux, sizeof( WAVEFORMATEXTENSIBLE ) + 7 * 4 );
238
239     SetDWLE( &p_sys->waveheader[1],
240              20 + (p_sys->b_ext ? 40 : 16) + p_sys->i_data ); /* Length */
241     SetDWLE( &p_sys->waveheader2[1], p_sys->i_data ); /* DataLength */
242
243     memcpy( p_block->p_buffer, &p_sys->waveheader, 5 * 4 );
244     memcpy( p_block->p_buffer + 5 * 4, &p_sys->waveformat,
245             sizeof( WAVEFORMATEXTENSIBLE ) );
246     memcpy( p_block->p_buffer + 5 * 4 +
247             (p_sys->b_ext ? sizeof( WAVEFORMATEXTENSIBLE ) : 16),
248             &p_sys->waveheader2, 2 * 4 );
249     if( !p_sys->b_ext ) p_block->i_buffer -= 24;
250     return p_block;
251 }
252
253 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
254 {
255     msg_Dbg( p_mux, "removing input" );
256
257     msg_Dbg( p_mux, "writing header data" );
258     if( !sout_AccessOutSeek( p_mux->p_access, 0 ) )
259     {
260         sout_AccessOutWrite( p_mux->p_access, GetHeader( p_mux ) );
261     }
262
263     return VLC_SUCCESS;
264 }
265
266 static int Mux( sout_mux_t *p_mux )
267 {
268     sout_mux_sys_t *p_sys = p_mux->p_sys;
269     sout_input_t *p_input;
270
271     if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
272
273     if( p_sys->b_header )
274     {
275         msg_Dbg( p_mux, "writing header data" );
276         sout_AccessOutWrite( p_mux->p_access, GetHeader( p_mux ) );
277     }
278     p_sys->b_header = VLC_FALSE;
279
280     p_input = p_mux->pp_inputs[0];
281     while( p_input->p_fifo->i_depth > 0 )
282     {
283         block_t *p_block = block_FifoGet( p_input->p_fifo );
284         p_sys->i_data += p_block->i_buffer;
285
286         /* Do the channel reordering */
287         if( p_sys->b_chan_reorder )
288         {
289             if( p_input->p_fmt->i_codec == VLC_FOURCC('s','1','6','l') )
290                 InterleaveS16( (int16_t *)p_block->p_buffer,
291                                p_block->i_buffer, p_sys->pi_chan_table,
292                                p_input->p_fmt->audio.i_channels );
293             else if( p_input->p_fmt->i_codec == VLC_FOURCC('f','l','3','2') )
294                 InterleaveFloat32( (float *)p_block->p_buffer,
295                                    p_block->i_buffer, p_sys->pi_chan_table,
296                                    p_input->p_fmt->audio.i_channels );
297         }
298
299         sout_AccessOutWrite( p_mux->p_access, p_block );
300     }
301
302     return VLC_SUCCESS;
303 }
304
305 /*****************************************************************************
306  * CheckReordering: Check if we need to do some channel re-ordering
307  *  (our channel order is different from the one chosen by Microsoft).
308  *****************************************************************************/
309 static void CheckReordering( sout_mux_t *p_mux, int i_nb_channels )
310 {
311     sout_mux_sys_t *p_sys = p_mux->p_sys;
312     int i, j, k, l;
313
314     p_sys->b_chan_reorder = VLC_FALSE;
315
316     for( i = 0, j = 0;
317          i < (int)(sizeof(pi_channels_out)/sizeof(uint32_t)); i++ )
318     {
319         if( p_sys->i_channel_mask & pi_channels_out[i] )
320         {
321             for( k = 0, l = 0;
322                  pi_channels_out[i] != pi_channels_ordered[k]; k++ )
323             {
324                 if( p_sys->i_channel_mask & pi_channels_ordered[k] )
325                 {
326                     l++;
327                 }
328             }
329
330             p_sys->pi_chan_table[j] = l;
331
332             j++;
333         }
334     }
335
336     for( i = 0; i < i_nb_channels; i++ )
337     {
338         if( p_sys->pi_chan_table[i] != i ) p_sys->b_chan_reorder = VLC_TRUE;
339     }
340
341     if( p_sys->b_chan_reorder ) msg_Dbg( p_mux, "channel reordering needed" );
342 }
343
344 /*****************************************************************************
345  * InterleaveFloat32/S16: change the channel order to the Microsoft one.
346  *****************************************************************************/
347 static void InterleaveFloat32( float *p_buf, int i_buf, int *pi_chan_table,
348                                int i_nb_channels )
349 {
350     int i, j;
351     float p_tmp[MAX_CHANNELS];
352
353     for( i = 0; i < i_buf / i_nb_channels / sizeof(float); i++ )
354     {
355         for( j = 0; j < i_nb_channels; j++ )
356         {
357             p_tmp[pi_chan_table[j]] = p_buf[i*i_nb_channels + j];
358         }
359
360         memcpy( &p_buf[i*i_nb_channels], p_tmp,
361                 i_nb_channels * sizeof(float) );
362     }
363 }
364
365 static void InterleaveS16( int16_t *p_buf, int i_buf, int *pi_chan_table,
366                            int i_nb_channels )
367 {
368     int i, j;
369     int16_t p_tmp[MAX_CHANNELS];
370
371     for( i = 0; i < i_buf / i_nb_channels / sizeof(int16_t); i++ )
372     {
373         for( j = 0; j < i_nb_channels; j++ )
374         {
375             p_tmp[pi_chan_table[j]] = p_buf[i*i_nb_channels + j];
376         }
377
378         memcpy( &p_buf[i*i_nb_channels], p_tmp,
379                 i_nb_channels * sizeof(int16_t) );
380     }
381 }