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