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