qt: playlist: use item title if available
[vlc.git] / modules / mux / wav.c
bloba80e916e8971ace12cbf24f71b5c35bbb84cb5bb
1 /*****************************************************************************
2 * wav.c: wav muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2004, 2006 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_aout.h>
34 #include <vlc_sout.h>
35 #include <vlc_block.h>
36 #include <vlc_codecs.h>
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close ( vlc_object_t * );
44 vlc_module_begin ()
45 set_description( N_("WAV muxer") )
46 set_capability( "sout mux", 5 )
47 set_category( CAT_SOUT )
48 set_subcategory( SUBCAT_SOUT_MUX )
49 set_callbacks( Open, Close )
50 add_shortcut( "wav" )
51 vlc_module_end ()
53 /*****************************************************************************
54 * Exported prototypes
55 *****************************************************************************/
56 static int Control ( sout_mux_t *, int, va_list );
57 static int AddStream( sout_mux_t *, sout_input_t * );
58 static void DelStream( sout_mux_t *, sout_input_t * );
59 static int Mux ( sout_mux_t * );
61 #define MAX_CHANNELS 6
63 typedef struct
65 bool b_used;
66 bool b_header;
67 bool b_ext;
69 uint32_t i_data;
71 /* Wave header for the output data */
72 uint32_t waveheader[5];
73 WAVEFORMATEXTENSIBLE waveformat;
74 uint32_t waveheader2[2];
76 uint32_t i_channel_mask;
77 uint8_t i_chans_to_reorder; /* do we need channel reordering */
78 uint8_t pi_chan_table[AOUT_CHAN_MAX];
79 } sout_mux_sys_t;
81 static const uint32_t pi_channels_in[] =
82 { WAVE_SPEAKER_FRONT_LEFT, WAVE_SPEAKER_FRONT_RIGHT,
83 WAVE_SPEAKER_SIDE_LEFT, WAVE_SPEAKER_SIDE_RIGHT,
84 WAVE_SPEAKER_BACK_LEFT, WAVE_SPEAKER_BACK_RIGHT, WAVE_SPEAKER_BACK_CENTER,
85 WAVE_SPEAKER_FRONT_CENTER, WAVE_SPEAKER_LOW_FREQUENCY, 0 };
86 static const uint32_t pi_channels_out[] =
87 { WAVE_SPEAKER_FRONT_LEFT, WAVE_SPEAKER_FRONT_RIGHT,
88 WAVE_SPEAKER_FRONT_CENTER, WAVE_SPEAKER_LOW_FREQUENCY,
89 WAVE_SPEAKER_BACK_LEFT, WAVE_SPEAKER_BACK_RIGHT,
90 WAVE_SPEAKER_BACK_CENTER,
91 WAVE_SPEAKER_SIDE_LEFT, WAVE_SPEAKER_SIDE_RIGHT, 0 };
93 /*****************************************************************************
94 * Open:
95 *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
98 sout_mux_t *p_mux = (sout_mux_t*)p_this;
99 sout_mux_sys_t *p_sys;
101 p_mux->pf_control = Control;
102 p_mux->pf_addstream = AddStream;
103 p_mux->pf_delstream = DelStream;
104 p_mux->pf_mux = Mux;
106 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
107 if( !p_sys )
108 return VLC_ENOMEM;
109 p_sys->b_used = false;
110 p_sys->b_header = true;
111 p_sys->i_data = 0;
113 p_sys->i_chans_to_reorder = 0;
115 return VLC_SUCCESS;
118 /*****************************************************************************
119 * Close:
120 *****************************************************************************/
121 static void Close( vlc_object_t * p_this )
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 );
128 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
130 VLC_UNUSED(p_mux);
131 bool *pb_bool;
132 char **ppsz;
134 switch( i_query )
136 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
137 pb_bool = va_arg( args, bool * );
138 *pb_bool = false;
139 return VLC_SUCCESS;
141 case MUX_GET_MIME:
142 ppsz = va_arg( args, char ** );
143 *ppsz = strdup( "audio/wav" );
144 return VLC_SUCCESS;
146 default:
147 return VLC_EGENERIC;
150 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
152 GUID subformat_guid = {0, 0, 0x10,{0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71}};
153 sout_mux_sys_t *p_sys = p_mux->p_sys;
154 WAVEFORMATEX *p_waveformat = &p_sys->waveformat.Format;
155 int i_bytes_per_sample;
156 uint16_t i_format;
157 bool b_ext;
159 if( p_input->p_fmt->i_cat != AUDIO_ES )
161 msg_Dbg( p_mux, "not an audio stream" );
162 return VLC_EGENERIC;
165 if( p_sys->b_used )
167 msg_Dbg( p_mux, "can't add more than 1 stream" );
168 return VLC_EGENERIC;
171 msg_Dbg( p_mux, "adding %i input channels, %iHz",
172 p_input->p_fmt->audio.i_channels,
173 p_input->p_fmt->audio.i_rate );
175 p_sys->i_channel_mask = 0;
176 if( p_input->p_fmt->audio.i_physical_channels )
178 for( unsigned i = 0; i < pi_vlc_chan_order_wg4[i]; i++ )
179 if( p_input->p_fmt->audio.i_physical_channels & pi_vlc_chan_order_wg4[i])
180 p_sys->i_channel_mask |= pi_channels_in[i];
182 p_sys->i_chans_to_reorder =
183 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
184 p_sys->i_channel_mask,
185 p_sys->pi_chan_table );
187 msg_Dbg( p_mux, "channel mask: %x, reordering: %u",
188 p_sys->i_channel_mask, p_sys->i_chans_to_reorder );
191 fourcc_to_wf_tag( p_input->p_fmt->i_codec, &i_format );
192 b_ext = p_sys->b_ext = p_input->p_fmt->audio.i_channels > 2;
194 /* Build a WAV header for the output data */
195 p_sys->waveheader[0] = VLC_FOURCC('R', 'I', 'F', 'F'); /* MainChunkID */
196 SetDWLE( &p_sys->waveheader[1], 0 ); /* Length */
197 p_sys->waveheader[2] = VLC_FOURCC('W', 'A', 'V', 'E'); /* ChunkTypeID */
198 p_sys->waveheader[3] = VLC_FOURCC('f', 'm', 't', ' '); /* SubChunkID */
199 SetDWLE( &p_sys->waveheader[4], b_ext ? 40 : 16 ); /* SubChunkLength */
201 p_sys->waveheader2[0] = VLC_FOURCC('d', 'a', 't', 'a'); /* DataChunkID */
202 SetDWLE( &p_sys->waveheader2[1], 0 ); /* DataLength */
204 /* Build a WAVEVFORMAT header for the output data */
205 memset( &p_sys->waveformat, 0, sizeof(WAVEFORMATEXTENSIBLE) );
206 SetWLE( &p_waveformat->wFormatTag,
207 b_ext ? WAVE_FORMAT_EXTENSIBLE : i_format );
208 SetWLE( &p_waveformat->nChannels,
209 p_input->p_fmt->audio.i_channels );
210 SetDWLE( &p_waveformat->nSamplesPerSec, p_input->p_fmt->audio.i_rate );
211 i_bytes_per_sample = p_input->p_fmt->audio.i_channels *
212 p_input->p_fmt->audio.i_bitspersample / 8;
213 SetDWLE( &p_waveformat->nAvgBytesPerSec,
214 i_bytes_per_sample * p_input->p_fmt->audio.i_rate );
215 SetWLE( &p_waveformat->nBlockAlign, i_bytes_per_sample );
216 SetWLE( &p_waveformat->wBitsPerSample,
217 p_input->p_fmt->audio.i_bitspersample );
218 SetWLE( &p_waveformat->cbSize, 22 );
219 SetWLE( &p_sys->waveformat.Samples.wValidBitsPerSample,
220 p_input->p_fmt->audio.i_bitspersample );
221 SetDWLE( &p_sys->waveformat.dwChannelMask,
222 p_sys->i_channel_mask );
223 p_sys->waveformat.SubFormat = subformat_guid;
224 p_sys->waveformat.SubFormat.Data1 = i_format;
227 p_sys->b_used = true;
229 return VLC_SUCCESS;
232 static block_t *GetHeader( sout_mux_t *p_mux )
234 sout_mux_sys_t *p_sys = p_mux->p_sys;
235 block_t *p_block =
236 block_Alloc( sizeof( WAVEFORMATEXTENSIBLE ) + 7 * 4 );
238 SetDWLE( &p_sys->waveheader[1],
239 20 + (p_sys->b_ext ? 40 : 16) + p_sys->i_data ); /* Length */
240 SetDWLE( &p_sys->waveheader2[1], p_sys->i_data ); /* DataLength */
242 memcpy( p_block->p_buffer, &p_sys->waveheader, 5 * 4 );
243 memcpy( p_block->p_buffer + 5 * 4, &p_sys->waveformat,
244 sizeof( WAVEFORMATEXTENSIBLE ) );
245 memcpy( p_block->p_buffer + 5 * 4 +
246 (p_sys->b_ext ? sizeof( WAVEFORMATEXTENSIBLE ) : 16),
247 &p_sys->waveheader2, 2 * 4 );
248 if( !p_sys->b_ext ) p_block->i_buffer -= 24;
249 return p_block;
252 static void DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
254 VLC_UNUSED(p_input);
255 msg_Dbg( p_mux, "removing input" );
257 msg_Dbg( p_mux, "writing header data" );
258 if( sout_AccessOutSeek( p_mux->p_access, 0 ) == VLC_SUCCESS )
260 sout_AccessOutWrite( p_mux->p_access, GetHeader( p_mux ) );
264 static int Mux( sout_mux_t *p_mux )
266 sout_mux_sys_t *p_sys = p_mux->p_sys;
267 sout_input_t *p_input;
269 if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
271 if( p_sys->b_header )
273 msg_Dbg( p_mux, "writing header data" );
274 sout_AccessOutWrite( p_mux->p_access, GetHeader( p_mux ) );
276 p_sys->b_header = false;
278 p_input = p_mux->pp_inputs[0];
279 while( block_FifoCount( p_input->p_fifo ) > 0 )
281 block_t *p_block = block_FifoGet( p_input->p_fifo );
282 p_sys->i_data += p_block->i_buffer;
284 /* Do the channel reordering */
285 if( p_sys->i_chans_to_reorder )
286 aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
287 p_sys->i_chans_to_reorder,
288 p_sys->pi_chan_table, p_input->p_fmt->i_codec );
290 sout_AccessOutWrite( p_mux->p_access, p_block );
293 return VLC_SUCCESS;