1 /*****************************************************************************
2 * coreaudio.c resampler based on CoreAudio's AudioConverter
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Jon Lech Johansen <jon-vl@nanocrew.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <AudioToolbox/AudioConverter.h>
34 #include "audio_output.h"
35 #include "aout_internal.h"
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Create ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
43 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
46 /*****************************************************************************
48 *****************************************************************************/
49 struct aout_filter_sys_t
51 aout_filter_t * p_secondary_resampler;
54 AudioStreamBasicDescription s_src_stream_format;
55 AudioStreamBasicDescription s_dst_stream_format;
56 AudioConverterRef s_converter;
57 unsigned int i_remainder;
58 unsigned int i_first_rate;
60 uint32_t p_bufferized[32768];
62 int32_t p_output[32768];
65 audio_date_t end_date;
68 /*****************************************************************************
70 *****************************************************************************/
72 set_description( _("audio filter using CoreAudio for resampling") );
73 set_capability( "audio filter", 40 );
74 set_callbacks( Create, Close );
77 /*****************************************************************************
78 * Create: allocate resampler
79 *****************************************************************************/
80 static int Create( vlc_object_t *p_this )
82 aout_filter_t * p_filter = (aout_filter_t *)p_this;
83 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
84 unsigned int i_nb_channels;
87 unsigned int i_first_rate;
89 if ( p_filter->input.i_rate == p_filter->output.i_rate
90 || p_filter->input.i_format != p_filter->output.i_format
91 || p_filter->input.i_physical_channels
92 != p_filter->output.i_physical_channels
93 || p_filter->input.i_original_channels
94 != p_filter->output.i_original_channels
95 || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
100 if ( p_filter->input.i_rate >= 48000 * (100 + AOUT_MAX_RESAMPLING) / 100 )
101 i_first_rate = 48000;
103 i_first_rate = 44100;
104 if ( p_filter->output.i_rate == i_first_rate )
109 i_nb_channels = aout_FormatNbChannels( &p_filter->input );
111 /* Allocate the memory needed to store the module's structure */
112 p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
113 if( p_filter->p_sys == NULL )
115 msg_Err( p_filter, "out of memory" );
118 memset( p_filter->p_sys, 0, sizeof(struct aout_filter_sys_t) );
119 p_sys->i_first_rate = i_first_rate;
120 p_sys->i_remainder = 0;
122 p_sys->s_src_stream_format.mFormatID = kAudioFormatLinearPCM;
123 p_sys->s_src_stream_format.mFormatFlags
124 = kLinearPCMFormatFlagIsFloat | kAudioFormatFlagsNativeEndian
125 | kAudioFormatFlagIsPacked;
126 p_sys->s_src_stream_format.mBytesPerPacket = i_nb_channels * 4;
127 p_sys->s_src_stream_format.mFramesPerPacket = 1;
128 p_sys->s_src_stream_format.mBytesPerFrame = i_nb_channels * 4;
129 p_sys->s_src_stream_format.mChannelsPerFrame = i_nb_channels;
130 p_sys->s_src_stream_format.mBitsPerChannel = 32;
132 memcpy( &p_sys->s_dst_stream_format, &p_sys->s_src_stream_format,
133 sizeof(AudioStreamBasicDescription) );
135 p_sys->s_src_stream_format.mSampleRate = p_sys->i_first_rate;
136 p_sys->s_dst_stream_format.mSampleRate = p_filter->output.i_rate;
138 err = AudioConverterNew( &p_sys->s_src_stream_format,
139 &p_sys->s_dst_stream_format,
140 &p_sys->s_converter );
144 msg_Err( p_filter, "AudioConverterNew failed: [%4.4s]",
150 i_prop = kConverterPrimeMethod_None;
151 err = AudioConverterSetProperty( p_sys->s_converter,
152 kAudioConverterPrimeMethod, sizeof(i_prop), &i_prop );
156 msg_Err( p_filter, "AudioConverterSetProperty failed: [%4.4s]",
162 /* Allocate a secondary resampler for the remainder. */
163 p_sys->p_secondary_resampler = vlc_object_create( p_filter,
164 sizeof(aout_filter_t) );
165 if ( p_sys->p_secondary_resampler == NULL )
170 vlc_object_attach( p_sys->p_secondary_resampler, p_filter );
172 memcpy( &p_sys->p_secondary_resampler->input, &p_filter->input,
173 sizeof(audio_sample_format_t) );
174 memcpy( &p_sys->p_secondary_resampler->output, &p_filter->output,
175 sizeof(audio_sample_format_t) );
176 p_sys->p_secondary_resampler->p_module
177 = module_Need( p_sys->p_secondary_resampler, "audio filter",
178 "ugly_resampler", VLC_TRUE );
179 if ( p_sys->p_secondary_resampler->p_module == NULL )
181 vlc_object_detach( p_sys->p_secondary_resampler );
182 vlc_object_destroy( p_sys->p_secondary_resampler );
186 p_sys->p_secondary_resampler->b_continuity = VLC_FALSE;
187 p_sys->alloc.i_alloc_type = AOUT_ALLOC_STACK;
188 p_sys->alloc.i_bytes_per_sec = p_filter->output.i_bytes_per_frame
189 * p_filter->output.i_rate
190 / p_filter->output.i_frame_length;
192 p_filter->pf_do_work = DoWork;
194 /* We don't want a new buffer to be created because we're not sure we'll
195 * actually need to resample anything. */
196 p_filter->b_in_place = VLC_FALSE;
197 p_sys->i_bufferized = 0;
203 /*****************************************************************************
204 * Close: free our resources
205 *****************************************************************************/
206 static void Close( vlc_object_t * p_this )
208 aout_filter_t * p_filter = (aout_filter_t *)p_this;
209 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
212 module_Unneed( p_sys->p_secondary_resampler,
213 p_sys->p_secondary_resampler->p_module );
214 vlc_object_detach( p_sys->p_secondary_resampler );
215 vlc_object_destroy( p_sys->p_secondary_resampler );
217 /* Destroy the AudioConverter */
218 err = AudioConverterDispose( p_sys->s_converter );
222 msg_Err( p_this, "AudioConverterDispose failed: %u", err );
225 free( p_filter->p_sys );
228 /*****************************************************************************
229 * DoWork: convert a buffer
230 *****************************************************************************/
231 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
232 aout_buffer_t * p_in_buf, aout_buffer_t * p_real_out_buf )
234 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
235 int32_t *p_in = p_sys->p_bufferized;
239 unsigned int i_out_nb;
241 unsigned int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
244 if ( !p_filter->b_continuity )
246 err = AudioConverterReset( p_sys->s_converter );
249 msg_Err( p_filter, "AudioConverterReset failed: [%4.4s]",
252 p_filter->b_continuity = VLC_TRUE;
253 p_sys->i_remainder = 0;
254 aout_DateInit( &p_filter->p_sys->end_date, p_filter->output.i_rate );
258 memcpy( p_sys->p_bufferized + p_sys->i_bufferized * i_nb_channels,
260 __MIN(p_in_buf->i_nb_samples * 4 * i_nb_channels,
261 sizeof(p_sys->p_bufferized)
262 - p_sys->i_bufferized * 4 * i_nb_channels) );
263 p_sys->i_bufferized += p_in_buf->i_nb_samples;
264 i_input_samples = p_sys->i_bufferized;
266 if ( i_input_samples >= 512 )
268 aout_buffer_t * p_middle_buf, * p_out_buf;
269 UInt32 i_output_size;
270 unsigned int i_wanted_nb, i_new_rate;
272 i_out_nb = (i_input_samples * p_filter->output.i_rate
273 + p_sys->i_remainder) / p_sys->i_first_rate;
274 p_sys->i_remainder = (i_input_samples * p_filter->output.i_rate
275 + p_sys->i_remainder) % p_sys->i_first_rate;
277 aout_BufferAlloc( &p_filter->output_alloc,
278 i_out_nb * 1000000 / p_filter->output.i_rate,
281 i_output_size = i_out_nb * 4 * i_nb_channels;
282 if ( i_output_size > p_out_buf->i_size )
284 aout_BufferAlloc( &p_sys->alloc,
285 i_out_nb * 1000000 / p_filter->output.i_rate,
286 NULL, p_middle_buf );
290 p_middle_buf = p_out_buf;
292 p_out = (int32_t*)p_middle_buf->p_buffer;
293 err = AudioConverterConvertBuffer( p_sys->s_converter,
294 i_input_samples * 4 * i_nb_channels, p_in,
295 &i_output_size, p_out );
298 msg_Warn( p_filter, "AudioConverterConvertBuffer failed: [%4.4s] (%u:%u)",
299 (char *)&err, i_out_nb * 4 * i_nb_channels, i_output_size );
300 i_output_size = i_out_nb * 4 * i_nb_channels;
301 memset( p_out, 0, i_output_size );
303 memmove( p_sys->p_bufferized,
304 p_sys->p_bufferized + i_input_samples * 4 * i_nb_channels,
305 (p_sys->i_bufferized - i_input_samples) * 4 * i_nb_channels );
306 p_sys->i_bufferized -= i_input_samples;
308 p_middle_buf->i_nb_samples = i_output_size / 4 / i_nb_channels;
309 p_middle_buf->i_nb_bytes = i_output_size;
310 p_middle_buf->start_date = p_in_buf->start_date;
311 p_middle_buf->end_date = p_middle_buf->start_date
312 + p_middle_buf->i_nb_samples * 1000000 / p_filter->output.i_rate;
314 i_wanted_nb = i_input_samples * p_filter->output.i_rate
315 / p_filter->input.i_rate;
316 i_new_rate = p_middle_buf->i_nb_samples * p_filter->output.i_rate
319 p_sys->p_secondary_resampler->input.i_rate = i_new_rate;
320 p_sys->p_secondary_resampler->pf_do_work( p_aout,
321 p_sys->p_secondary_resampler, p_middle_buf, p_out_buf );
323 if ( p_middle_buf != p_out_buf )
325 aout_BufferFree( p_middle_buf );
328 memcpy( p_sys->p_output + p_sys->i_output * i_nb_channels,
330 __MIN(p_out_buf->i_nb_samples * 4 * i_nb_channels,
331 sizeof(p_sys->p_output)
332 - p_sys->i_output * 4 * i_nb_channels) );
333 p_sys->i_output += p_out_buf->i_nb_samples;
335 aout_BufferFree( p_out_buf );
338 i_out_nb = (p_in_buf->i_nb_samples * p_filter->output.i_rate
339 + p_sys->i_first_rate - 1) / p_sys->i_first_rate;
340 if ( i_out_nb > p_sys->i_output )
341 i_out_nb = p_sys->i_output;
343 p_real_out_buf->i_nb_samples = i_out_nb;
344 p_real_out_buf->i_nb_bytes = i_out_nb * 4 * i_nb_channels;
346 if( p_in_buf->start_date !=
347 aout_DateGet( &p_filter->p_sys->end_date ) )
349 aout_DateSet( &p_filter->p_sys->end_date, p_in_buf->start_date );
352 p_real_out_buf->end_date = aout_DateIncrement( &p_filter->p_sys->end_date,
354 memcpy( p_real_out_buf->p_buffer, p_sys->p_output,
355 i_out_nb * 4 * i_nb_channels );
356 memmove( p_sys->p_output, p_sys->p_output + i_out_nb * i_nb_channels,
357 (p_sys->i_output - i_out_nb) * 4 * i_nb_channels );
358 p_sys->i_output -= i_out_nb;