575228cac0ff6ed0f4eb9684c2238e651ae913c3
[vlc.git] / modules / audio_filter / resampler / coreaudio.c
1 /*****************************************************************************
2  * coreaudio.c resampler based on CoreAudio's AudioConverter
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: coreaudio.c,v 1.2 2003/03/31 22:39:27 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Jon Lech Johansen <jon-vl@nanocrew.net>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <AudioToolbox/AudioConverter.h>
32
33 #include <vlc/vlc.h>
34 #include "audio_output.h"
35 #include "aout_internal.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Create    ( vlc_object_t * );
41 static void Close     ( vlc_object_t * );
42
43 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
44                         aout_buffer_t * );
45
46 /*****************************************************************************
47  * Local structures
48  *****************************************************************************/
49 struct aout_filter_sys_t
50 {
51     aout_filter_t * p_secondary_resampler;
52     aout_alloc_t alloc;
53
54     AudioStreamBasicDescription s_src_stream_format;
55     AudioStreamBasicDescription s_dst_stream_format;
56     AudioConverterRef   s_converter;
57     unsigned int i_first_rate;
58 };
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 vlc_module_begin();
64     set_description( _("audio filter using CoreAudio for resampling") );
65     set_capability( "audio filter", 0 );
66     set_callbacks( Create, Close );
67 vlc_module_end();
68
69 /*****************************************************************************
70  * Create: allocate resampler
71  *****************************************************************************/
72 static int Create( vlc_object_t *p_this )
73 {
74     aout_filter_t * p_filter = (aout_filter_t *)p_this;
75     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
76     unsigned int i_nb_channels;
77     OSStatus err;
78     uint32_t i_prop;
79     unsigned int i_first_rate;
80
81     if ( p_filter->input.i_rate == p_filter->output.i_rate
82           || p_filter->input.i_format != p_filter->output.i_format
83           || p_filter->input.i_physical_channels
84               != p_filter->output.i_physical_channels
85           || p_filter->input.i_original_channels
86               != p_filter->output.i_original_channels
87           || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
88     {
89         return VLC_EGENERIC;
90     }
91
92     if ( p_filter->input.i_rate >= 48000 * (100 + AOUT_MAX_RESAMPLING) / 100 )
93         i_first_rate = 48000;
94     else
95         i_first_rate = 44100;
96     if ( p_filter->output.i_rate == i_first_rate )
97     {
98         return VLC_EGENERIC;
99     }
100
101     i_nb_channels = aout_FormatNbChannels( &p_filter->input );
102
103     /* Allocate the memory needed to store the module's structure */
104     p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
105     if( p_filter->p_sys == NULL )
106     {
107         msg_Err( p_filter, "out of memory" );
108         return VLC_ENOMEM;
109     }
110     memset( p_filter->p_sys, 0, sizeof(struct aout_filter_sys_t) );
111     p_sys->i_first_rate = i_first_rate;
112
113     p_sys->s_src_stream_format.mFormatID = kAudioFormatLinearPCM;
114     p_sys->s_src_stream_format.mFormatFlags
115         = kLinearPCMFormatFlagIsFloat | kAudioFormatFlagsNativeEndian
116           | kAudioFormatFlagIsPacked;
117     p_sys->s_src_stream_format.mBytesPerPacket = i_nb_channels * 4;
118     p_sys->s_src_stream_format.mFramesPerPacket = 1;
119     p_sys->s_src_stream_format.mBytesPerFrame = i_nb_channels * 4;
120     p_sys->s_src_stream_format.mChannelsPerFrame = i_nb_channels;
121     p_sys->s_src_stream_format.mBitsPerChannel = 32;
122
123     memcpy( &p_sys->s_dst_stream_format, &p_sys->s_src_stream_format,
124             sizeof(AudioStreamBasicDescription) );
125
126     p_sys->s_src_stream_format.mSampleRate = p_sys->i_first_rate;
127     p_sys->s_dst_stream_format.mSampleRate = p_filter->output.i_rate;
128
129     err = AudioConverterNew( &p_sys->s_src_stream_format,
130                              &p_sys->s_dst_stream_format,
131                              &p_sys->s_converter );
132
133     if( err != noErr )
134     {
135         msg_Err( p_filter, "AudioConverterNew failed: [%4.4s]",
136                  (char *)&err );
137         free(p_sys);
138         return VLC_EGENERIC;
139     }
140
141     i_prop = kConverterPrimeMethod_None;
142     err = AudioConverterSetProperty( p_sys->s_converter,
143             kAudioConverterPrimeMethod, sizeof(i_prop), &i_prop );
144
145     if( err != noErr )
146     {
147         msg_Err( p_filter, "AudioConverterSetProperty failed: [%4.4s]",
148                  (char *)&err );
149         free(p_sys);
150         return VLC_EGENERIC;
151     }
152
153     /* Allocate a secondary resampler for the remainder. */
154     p_sys->p_secondary_resampler = vlc_object_create( p_filter,
155                                                   sizeof(aout_filter_t) );     
156     if ( p_sys->p_secondary_resampler == NULL )
157     {
158         free(p_sys);
159         return VLC_EGENERIC;
160     }
161     vlc_object_attach( p_sys->p_secondary_resampler, p_filter );
162
163     memcpy( &p_sys->p_secondary_resampler->input, &p_filter->input, 
164             sizeof(audio_sample_format_t) );
165     memcpy( &p_sys->p_secondary_resampler->output, &p_filter->output, 
166             sizeof(audio_sample_format_t) );
167     p_sys->p_secondary_resampler->p_module
168         = module_Need( p_sys->p_secondary_resampler, "audio filter",
169                        "ugly_resampler" );
170     if ( p_sys->p_secondary_resampler->p_module == NULL )
171     {
172         vlc_object_detach( p_sys->p_secondary_resampler );
173         vlc_object_destroy( p_sys->p_secondary_resampler );
174         free(p_sys);
175         return VLC_EGENERIC;
176     }
177     p_sys->p_secondary_resampler->b_continuity = VLC_FALSE;
178     p_sys->alloc.i_alloc_type = AOUT_ALLOC_STACK;
179     p_sys->alloc.i_bytes_per_sec = p_filter->output.i_bytes_per_frame
180                              * p_filter->output.i_rate
181                              / p_filter->output.i_frame_length;
182
183     p_filter->pf_do_work = DoWork;
184
185     /* We don't want a new buffer to be created because we're not sure we'll
186      * actually need to resample anything. */
187     p_filter->b_in_place = VLC_FALSE;
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * Close: free our resources
194  *****************************************************************************/
195 static void Close( vlc_object_t * p_this )
196 {
197     aout_filter_t * p_filter = (aout_filter_t *)p_this;
198     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
199     OSErr err;
200
201     module_Unneed( p_sys->p_secondary_resampler,
202                    p_sys->p_secondary_resampler->p_module );
203     vlc_object_detach( p_sys->p_secondary_resampler );
204     vlc_object_destroy( p_sys->p_secondary_resampler );
205
206     /* Destroy the AudioConverter */
207     err = AudioConverterDispose( p_sys->s_converter );
208
209     if( err != noErr )
210     {
211         msg_Err( p_this, "AudioConverterDispose failed: %u", err );
212     }
213
214     free( p_filter->p_sys );
215 }
216
217 /*****************************************************************************
218  * DoWork: convert a buffer
219  *****************************************************************************/
220 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
221                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
222 {
223     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
224     int32_t *p_in = (int32_t *)p_in_buf->p_buffer;
225     int32_t *p_out;
226     UInt32 i_output_size;
227     unsigned int i_out_nb, i_wanted_nb, i_new_rate;
228     OSErr err;
229     aout_buffer_t * p_middle_buf;
230
231     unsigned int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
232
233 #if 1
234     if ( !p_filter->b_continuity )
235     {
236         err = AudioConverterReset( p_sys->s_converter );
237         if( err != noErr )
238         {
239             msg_Err( p_filter, "AudioConverterReset failed: [%4.4s]",
240                      (char *)&err );
241         }
242         p_filter->b_continuity = VLC_TRUE;
243     }
244 #endif
245
246     i_out_nb = p_in_buf->i_nb_samples * p_filter->output.i_rate
247                                     / p_sys->i_first_rate;
248
249     i_output_size = i_out_nb * 4 * i_nb_channels;
250     if ( i_output_size > p_out_buf->i_size )
251     {
252         aout_BufferAlloc( &p_sys->alloc,
253             i_out_nb * 1000000 / p_filter->output.i_rate,
254             NULL, p_middle_buf );
255     }
256     else
257     {
258         p_middle_buf = p_out_buf;
259     }
260     p_out = (int32_t*)p_middle_buf->p_buffer;
261     err = AudioConverterConvertBuffer( p_sys->s_converter,
262         p_in_buf->i_nb_samples * 4 * i_nb_channels, p_in,
263         &i_output_size, p_out );
264     if( err != noErr )
265     {
266         msg_Warn( p_filter, "AudioConverterConvertBuffer failed: [%4.4s] (%u:%u)",
267                  (char *)&err, i_out_nb * 4 * i_nb_channels, i_output_size );
268         i_output_size = i_out_nb * 4 * i_nb_channels;
269         memset( p_out, 0, i_output_size );
270     }
271
272     p_middle_buf->i_nb_samples = i_output_size / 4 / i_nb_channels;
273     p_middle_buf->i_nb_bytes = i_output_size;
274     p_middle_buf->start_date = p_in_buf->start_date;
275     p_middle_buf->end_date = p_middle_buf->start_date + p_middle_buf->i_nb_samples *
276         1000000 / p_filter->output.i_rate;
277
278     i_wanted_nb = p_in_buf->i_nb_samples * p_filter->output.i_rate
279                                         / p_filter->input.i_rate;
280     i_new_rate = p_middle_buf->i_nb_samples * p_filter->output.i_rate
281                                         / i_wanted_nb;
282
283     p_sys->p_secondary_resampler->input.i_rate = i_new_rate;
284     p_sys->p_secondary_resampler->pf_do_work( p_aout,
285         p_sys->p_secondary_resampler, p_middle_buf, p_out_buf );
286
287     if ( p_middle_buf != p_out_buf )
288     {
289         aout_BufferFree( p_middle_buf );
290     }
291 }