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