1 /*****************************************************************************
2 * coreaudio.c resampler based on CoreAudio's AudioConverter
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: coreaudio.c,v 1.5 2004/03/03 20:39:51 gbazin Exp $
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;
61 /*****************************************************************************
63 *****************************************************************************/
65 set_description( _("audio filter using CoreAudio for resampling") );
66 set_capability( "audio filter", 40 );
67 set_callbacks( Create, Close );
70 /*****************************************************************************
71 * Create: allocate resampler
72 *****************************************************************************/
73 static int Create( vlc_object_t *p_this )
75 aout_filter_t * p_filter = (aout_filter_t *)p_this;
76 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
77 unsigned int i_nb_channels;
80 unsigned int i_first_rate;
82 if ( p_filter->input.i_rate == p_filter->output.i_rate
83 || p_filter->input.i_format != p_filter->output.i_format
84 || p_filter->input.i_physical_channels
85 != p_filter->output.i_physical_channels
86 || p_filter->input.i_original_channels
87 != p_filter->output.i_original_channels
88 || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
93 if ( p_filter->input.i_rate >= 48000 * (100 + AOUT_MAX_RESAMPLING) / 100 )
97 if ( p_filter->output.i_rate == i_first_rate )
102 i_nb_channels = aout_FormatNbChannels( &p_filter->input );
104 /* Allocate the memory needed to store the module's structure */
105 p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
106 if( p_filter->p_sys == NULL )
108 msg_Err( p_filter, "out of memory" );
111 memset( p_filter->p_sys, 0, sizeof(struct aout_filter_sys_t) );
112 p_sys->i_first_rate = i_first_rate;
113 p_sys->i_remainder = 0;
115 p_sys->s_src_stream_format.mFormatID = kAudioFormatLinearPCM;
116 p_sys->s_src_stream_format.mFormatFlags
117 = kLinearPCMFormatFlagIsFloat | kAudioFormatFlagsNativeEndian
118 | kAudioFormatFlagIsPacked;
119 p_sys->s_src_stream_format.mBytesPerPacket = i_nb_channels * 4;
120 p_sys->s_src_stream_format.mFramesPerPacket = 1;
121 p_sys->s_src_stream_format.mBytesPerFrame = i_nb_channels * 4;
122 p_sys->s_src_stream_format.mChannelsPerFrame = i_nb_channels;
123 p_sys->s_src_stream_format.mBitsPerChannel = 32;
125 memcpy( &p_sys->s_dst_stream_format, &p_sys->s_src_stream_format,
126 sizeof(AudioStreamBasicDescription) );
128 p_sys->s_src_stream_format.mSampleRate = p_sys->i_first_rate;
129 p_sys->s_dst_stream_format.mSampleRate = p_filter->output.i_rate;
131 err = AudioConverterNew( &p_sys->s_src_stream_format,
132 &p_sys->s_dst_stream_format,
133 &p_sys->s_converter );
137 msg_Err( p_filter, "AudioConverterNew failed: [%4.4s]",
143 i_prop = kConverterPrimeMethod_None;
144 err = AudioConverterSetProperty( p_sys->s_converter,
145 kAudioConverterPrimeMethod, sizeof(i_prop), &i_prop );
149 msg_Err( p_filter, "AudioConverterSetProperty failed: [%4.4s]",
155 /* Allocate a secondary resampler for the remainder. */
156 p_sys->p_secondary_resampler = vlc_object_create( p_filter,
157 sizeof(aout_filter_t) );
158 if ( p_sys->p_secondary_resampler == NULL )
163 vlc_object_attach( p_sys->p_secondary_resampler, p_filter );
165 memcpy( &p_sys->p_secondary_resampler->input, &p_filter->input,
166 sizeof(audio_sample_format_t) );
167 memcpy( &p_sys->p_secondary_resampler->output, &p_filter->output,
168 sizeof(audio_sample_format_t) );
169 p_sys->p_secondary_resampler->p_module
170 = module_Need( p_sys->p_secondary_resampler, "audio filter",
171 "ugly_resampler", VLC_TRUE );
172 if ( p_sys->p_secondary_resampler->p_module == NULL )
174 vlc_object_detach( p_sys->p_secondary_resampler );
175 vlc_object_destroy( p_sys->p_secondary_resampler );
179 p_sys->p_secondary_resampler->b_continuity = VLC_FALSE;
180 p_sys->alloc.i_alloc_type = AOUT_ALLOC_STACK;
181 p_sys->alloc.i_bytes_per_sec = p_filter->output.i_bytes_per_frame
182 * p_filter->output.i_rate
183 / p_filter->output.i_frame_length;
185 p_filter->pf_do_work = DoWork;
187 /* We don't want a new buffer to be created because we're not sure we'll
188 * actually need to resample anything. */
189 p_filter->b_in_place = VLC_FALSE;
194 /*****************************************************************************
195 * Close: free our resources
196 *****************************************************************************/
197 static void Close( vlc_object_t * p_this )
199 aout_filter_t * p_filter = (aout_filter_t *)p_this;
200 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
203 module_Unneed( p_sys->p_secondary_resampler,
204 p_sys->p_secondary_resampler->p_module );
205 vlc_object_detach( p_sys->p_secondary_resampler );
206 vlc_object_destroy( p_sys->p_secondary_resampler );
208 /* Destroy the AudioConverter */
209 err = AudioConverterDispose( p_sys->s_converter );
213 msg_Err( p_this, "AudioConverterDispose failed: %u", err );
216 free( p_filter->p_sys );
219 /*****************************************************************************
220 * DoWork: convert a buffer
221 *****************************************************************************/
222 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
223 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
225 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
226 int32_t *p_in = (int32_t *)p_in_buf->p_buffer;
228 UInt32 i_output_size;
229 unsigned int i_out_nb, i_wanted_nb, i_new_rate;
231 aout_buffer_t * p_middle_buf;
233 unsigned int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
236 if ( !p_filter->b_continuity )
238 err = AudioConverterReset( p_sys->s_converter );
241 msg_Err( p_filter, "AudioConverterReset failed: [%4.4s]",
244 p_filter->b_continuity = VLC_TRUE;
245 p_sys->i_remainder = 0;
249 i_out_nb = (p_in_buf->i_nb_samples * p_filter->output.i_rate
250 + p_sys->i_remainder) / p_sys->i_first_rate;
251 p_sys->i_remainder = (p_in_buf->i_nb_samples * p_filter->output.i_rate
252 + p_sys->i_remainder) % p_sys->i_first_rate;
254 i_output_size = i_out_nb * 4 * i_nb_channels;
255 if ( i_output_size > p_out_buf->i_size )
257 aout_BufferAlloc( &p_sys->alloc,
258 i_out_nb * 1000000 / p_filter->output.i_rate,
259 NULL, p_middle_buf );
263 p_middle_buf = p_out_buf;
265 p_out = (int32_t*)p_middle_buf->p_buffer;
266 err = AudioConverterConvertBuffer( p_sys->s_converter,
267 p_in_buf->i_nb_samples * 4 * i_nb_channels, p_in,
268 &i_output_size, p_out );
271 msg_Warn( p_filter, "AudioConverterConvertBuffer failed: [%4.4s] (%u:%u)",
272 (char *)&err, i_out_nb * 4 * i_nb_channels, i_output_size );
273 i_output_size = i_out_nb * 4 * i_nb_channels;
274 memset( p_out, 0, i_output_size );
277 p_middle_buf->i_nb_samples = i_output_size / 4 / i_nb_channels;
278 p_middle_buf->i_nb_bytes = i_output_size;
279 p_middle_buf->start_date = p_in_buf->start_date;
280 p_middle_buf->end_date = p_middle_buf->start_date + p_middle_buf->i_nb_samples *
281 1000000 / p_filter->output.i_rate;
283 i_wanted_nb = p_in_buf->i_nb_samples * p_filter->output.i_rate
284 / p_filter->input.i_rate;
285 i_new_rate = p_middle_buf->i_nb_samples * p_filter->output.i_rate
288 p_sys->p_secondary_resampler->input.i_rate = i_new_rate;
289 p_sys->p_secondary_resampler->pf_do_work( p_aout,
290 p_sys->p_secondary_resampler, p_middle_buf, p_out_buf );
292 if ( p_middle_buf != p_out_buf )
294 aout_BufferFree( p_middle_buf );