* all: added a new access_filter module: record + one hotkey to start/stop
[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$
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_remainder;
58     unsigned int i_first_rate;
59
60     uint32_t p_bufferized[32768];
61     int i_bufferized;
62     int32_t p_output[32768];
63     int i_output;
64
65     audio_date_t end_date;
66 };
67
68 /*****************************************************************************
69  * Module descriptor
70  *****************************************************************************/
71 vlc_module_begin();
72     set_description( _("audio filter using CoreAudio for resampling") );
73     set_category( CAT_AUDIO );
74     set_subcategory( SUBCAT_AUDIO_MISC );
75     set_capability( "audio filter", 40 );
76     set_callbacks( Create, Close );
77 vlc_module_end();
78
79 /*****************************************************************************
80  * Create: allocate resampler
81  *****************************************************************************/
82 static int Create( vlc_object_t *p_this )
83 {
84     aout_filter_t * p_filter = (aout_filter_t *)p_this;
85     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
86     unsigned int i_nb_channels;
87     OSStatus err;
88     uint32_t i_prop;
89     unsigned int i_first_rate;
90
91     if ( p_filter->input.i_rate == p_filter->output.i_rate
92           || p_filter->input.i_format != p_filter->output.i_format
93           || p_filter->input.i_physical_channels
94               != p_filter->output.i_physical_channels
95           || p_filter->input.i_original_channels
96               != p_filter->output.i_original_channels
97           || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
98     {
99         return VLC_EGENERIC;
100     }
101
102     if ( p_filter->input.i_rate >= 48000 * (100 + AOUT_MAX_RESAMPLING) / 100 )
103         i_first_rate = 48000;
104     else
105         i_first_rate = 44100;
106     if ( p_filter->output.i_rate == i_first_rate )
107     {
108         return VLC_EGENERIC;
109     }
110
111     i_nb_channels = aout_FormatNbChannels( &p_filter->input );
112
113     /* Allocate the memory needed to store the module's structure */
114     p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
115     if( p_filter->p_sys == NULL )
116     {
117         msg_Err( p_filter, "out of memory" );
118         return VLC_ENOMEM;
119     }
120     memset( p_filter->p_sys, 0, sizeof(struct aout_filter_sys_t) );
121     p_sys->i_first_rate = i_first_rate;
122     p_sys->i_remainder = 0;
123
124     p_sys->s_src_stream_format.mFormatID = kAudioFormatLinearPCM;
125     p_sys->s_src_stream_format.mFormatFlags
126         = kLinearPCMFormatFlagIsFloat | kAudioFormatFlagsNativeEndian
127           | kAudioFormatFlagIsPacked;
128     p_sys->s_src_stream_format.mBytesPerPacket = i_nb_channels * 4;
129     p_sys->s_src_stream_format.mFramesPerPacket = 1;
130     p_sys->s_src_stream_format.mBytesPerFrame = i_nb_channels * 4;
131     p_sys->s_src_stream_format.mChannelsPerFrame = i_nb_channels;
132     p_sys->s_src_stream_format.mBitsPerChannel = 32;
133
134     memcpy( &p_sys->s_dst_stream_format, &p_sys->s_src_stream_format,
135             sizeof(AudioStreamBasicDescription) );
136
137     p_sys->s_src_stream_format.mSampleRate = p_sys->i_first_rate;
138     p_sys->s_dst_stream_format.mSampleRate = p_filter->output.i_rate;
139
140     err = AudioConverterNew( &p_sys->s_src_stream_format,
141                              &p_sys->s_dst_stream_format,
142                              &p_sys->s_converter );
143
144     if( err != noErr )
145     {
146         msg_Err( p_filter, "AudioConverterNew failed: [%4.4s]",
147                  (char *)&err );
148         free(p_sys);
149         return VLC_EGENERIC;
150     }
151
152     i_prop = kConverterPrimeMethod_None;
153     err = AudioConverterSetProperty( p_sys->s_converter,
154             kAudioConverterPrimeMethod, sizeof(i_prop), &i_prop );
155
156     if( err != noErr )
157     {
158         msg_Err( p_filter, "AudioConverterSetProperty failed: [%4.4s]",
159                  (char *)&err );
160         free(p_sys);
161         return VLC_EGENERIC;
162     }
163
164     /* Allocate a secondary resampler for the remainder. */
165     p_sys->p_secondary_resampler = vlc_object_create( p_filter,
166                                                   sizeof(aout_filter_t) );     
167     if ( p_sys->p_secondary_resampler == NULL )
168     {
169         free(p_sys);
170         return VLC_EGENERIC;
171     }
172     vlc_object_attach( p_sys->p_secondary_resampler, p_filter );
173
174     memcpy( &p_sys->p_secondary_resampler->input, &p_filter->input, 
175             sizeof(audio_sample_format_t) );
176     memcpy( &p_sys->p_secondary_resampler->output, &p_filter->output, 
177             sizeof(audio_sample_format_t) );
178     p_sys->p_secondary_resampler->p_module
179         = module_Need( p_sys->p_secondary_resampler, "audio filter",
180                        "ugly_resampler", VLC_TRUE );
181     if ( p_sys->p_secondary_resampler->p_module == NULL )
182     {
183         vlc_object_detach( p_sys->p_secondary_resampler );
184         vlc_object_destroy( p_sys->p_secondary_resampler );
185         free(p_sys);
186         return VLC_EGENERIC;
187     }
188     p_sys->p_secondary_resampler->b_continuity = VLC_FALSE;
189     p_sys->alloc.i_alloc_type = AOUT_ALLOC_STACK;
190     p_sys->alloc.i_bytes_per_sec = p_filter->output.i_bytes_per_frame
191                              * p_filter->output.i_rate
192                              / p_filter->output.i_frame_length;
193
194     p_filter->pf_do_work = DoWork;
195
196     /* We don't want a new buffer to be created because we're not sure we'll
197      * actually need to resample anything. */
198     p_filter->b_in_place = VLC_FALSE;
199     p_sys->i_bufferized = 0;
200     p_sys->i_output = 0;
201
202     return VLC_SUCCESS;
203 }
204
205 /*****************************************************************************
206  * Close: free our resources
207  *****************************************************************************/
208 static void Close( vlc_object_t * p_this )
209 {
210     aout_filter_t * p_filter = (aout_filter_t *)p_this;
211     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
212     OSErr err;
213
214     module_Unneed( p_sys->p_secondary_resampler,
215                    p_sys->p_secondary_resampler->p_module );
216     vlc_object_detach( p_sys->p_secondary_resampler );
217     vlc_object_destroy( p_sys->p_secondary_resampler );
218
219     /* Destroy the AudioConverter */
220     err = AudioConverterDispose( p_sys->s_converter );
221
222     if( err != noErr )
223     {
224         msg_Err( p_this, "AudioConverterDispose failed: %u", err );
225     }
226
227     free( p_filter->p_sys );
228 }
229
230 /*****************************************************************************
231  * DoWork: convert a buffer
232  *****************************************************************************/
233 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
234                     aout_buffer_t * p_in_buf, aout_buffer_t * p_real_out_buf )
235 {
236     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
237     int32_t *p_in = p_sys->p_bufferized;
238     int32_t *p_out;
239     int i_input_samples;
240     OSErr err;
241     unsigned int i_out_nb;
242
243     unsigned int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
244
245 #if 1
246     if ( !p_filter->b_continuity )
247     {
248         err = AudioConverterReset( p_sys->s_converter );
249         if( err != noErr )
250         {
251             msg_Err( p_filter, "AudioConverterReset failed: [%4.4s]",
252                      (char *)&err );
253         }
254         p_filter->b_continuity = VLC_TRUE;
255         p_sys->i_remainder = 0;
256         aout_DateInit( &p_filter->p_sys->end_date, p_filter->output.i_rate ); 
257     }
258 #endif
259
260     memcpy( p_sys->p_bufferized + p_sys->i_bufferized * i_nb_channels,
261             p_in_buf->p_buffer,
262             __MIN(p_in_buf->i_nb_samples * 4 * i_nb_channels,
263                   sizeof(p_sys->p_bufferized)
264                      - p_sys->i_bufferized * 4 * i_nb_channels) );
265     p_sys->i_bufferized += p_in_buf->i_nb_samples;
266     i_input_samples = p_sys->i_bufferized;
267
268     if ( i_input_samples >= 512 )
269     {
270         aout_buffer_t * p_middle_buf, * p_out_buf;
271         UInt32 i_output_size;
272         unsigned int i_wanted_nb, i_new_rate;
273
274         i_out_nb = (i_input_samples * p_filter->output.i_rate
275                      + p_sys->i_remainder) / p_sys->i_first_rate;
276         p_sys->i_remainder = (i_input_samples * p_filter->output.i_rate
277                      + p_sys->i_remainder) % p_sys->i_first_rate;
278     
279         aout_BufferAlloc( &p_filter->output_alloc,
280             i_out_nb * 1000000 / p_filter->output.i_rate,
281             NULL, p_out_buf );
282
283         i_output_size = i_out_nb * 4 * i_nb_channels;
284         if ( i_output_size > p_out_buf->i_size )
285         {
286             aout_BufferAlloc( &p_sys->alloc,
287                 i_out_nb * 1000000 / p_filter->output.i_rate,
288                 NULL, p_middle_buf );
289         }
290         else
291         {
292             p_middle_buf = p_out_buf;
293         }
294         p_out = (int32_t*)p_middle_buf->p_buffer;
295         err = AudioConverterConvertBuffer( p_sys->s_converter,
296             i_input_samples * 4 * i_nb_channels, p_in,
297             &i_output_size, p_out );
298         if( err != noErr )
299         {
300             msg_Warn( p_filter, "AudioConverterConvertBuffer failed: [%4.4s] (%u:%u)",
301                      (char *)&err, i_out_nb * 4 * i_nb_channels, i_output_size );
302             i_output_size = i_out_nb * 4 * i_nb_channels;
303             memset( p_out, 0, i_output_size );
304         }
305         memmove( p_sys->p_bufferized,
306                  p_sys->p_bufferized + i_input_samples * 4 * i_nb_channels,
307                  (p_sys->i_bufferized - i_input_samples) * 4 * i_nb_channels );
308         p_sys->i_bufferized -= i_input_samples;
309
310         p_middle_buf->i_nb_samples = i_output_size / 4 / i_nb_channels;
311         p_middle_buf->i_nb_bytes = i_output_size;
312         p_middle_buf->start_date = p_in_buf->start_date;
313         p_middle_buf->end_date = p_middle_buf->start_date
314             + p_middle_buf->i_nb_samples * 1000000 / p_filter->output.i_rate;
315     
316         i_wanted_nb = i_input_samples * p_filter->output.i_rate
317                                             / p_filter->input.i_rate;
318         i_new_rate = p_middle_buf->i_nb_samples * p_filter->output.i_rate
319                                             / i_wanted_nb;
320     
321         p_sys->p_secondary_resampler->input.i_rate = i_new_rate;
322         p_sys->p_secondary_resampler->pf_do_work( p_aout,
323             p_sys->p_secondary_resampler, p_middle_buf, p_out_buf );
324     
325         if ( p_middle_buf != p_out_buf )
326         {
327             aout_BufferFree( p_middle_buf );
328         }
329
330         memcpy( p_sys->p_output + p_sys->i_output * i_nb_channels,
331                 p_out_buf->p_buffer,
332                 __MIN(p_out_buf->i_nb_samples * 4 * i_nb_channels,
333                       sizeof(p_sys->p_output)
334                          - p_sys->i_output * 4 * i_nb_channels) );
335         p_sys->i_output += p_out_buf->i_nb_samples;
336
337         aout_BufferFree( p_out_buf );
338     }
339
340     i_out_nb = (p_in_buf->i_nb_samples * p_filter->output.i_rate
341                  + p_sys->i_first_rate - 1) / p_sys->i_first_rate;
342     if ( i_out_nb > p_sys->i_output )
343         i_out_nb = p_sys->i_output;
344
345     p_real_out_buf->i_nb_samples = i_out_nb;
346     p_real_out_buf->i_nb_bytes = i_out_nb * 4 * i_nb_channels;
347
348     if( p_in_buf->start_date !=
349         aout_DateGet( &p_filter->p_sys->end_date ) )
350     {
351         aout_DateSet( &p_filter->p_sys->end_date, p_in_buf->start_date );
352     }
353
354     p_real_out_buf->end_date = aout_DateIncrement( &p_filter->p_sys->end_date,
355                                                    i_out_nb );
356     memcpy( p_real_out_buf->p_buffer, p_sys->p_output,
357             i_out_nb * 4 * i_nb_channels );
358     memmove( p_sys->p_output, p_sys->p_output + i_out_nb * i_nb_channels,
359              (p_sys->i_output - i_out_nb) * 4 * i_nb_channels );
360     p_sys->i_output -= i_out_nb;
361 }