Add statistics audio filter that prints information about the audio stream.
[mplayer/glamo.git] / libaf / af.c
blob77f30a4d456624e56c7fa8132c1a4f3bf4156c99
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #if HAVE_MALLOC_H
24 #include <malloc.h>
25 #endif
27 #include "af.h"
29 // Static list of filters
30 extern af_info_t af_info_dummy;
31 extern af_info_t af_info_delay;
32 extern af_info_t af_info_channels;
33 extern af_info_t af_info_format;
34 extern af_info_t af_info_resample;
35 extern af_info_t af_info_volume;
36 extern af_info_t af_info_equalizer;
37 extern af_info_t af_info_gate;
38 extern af_info_t af_info_comp;
39 extern af_info_t af_info_pan;
40 extern af_info_t af_info_surround;
41 extern af_info_t af_info_sub;
42 extern af_info_t af_info_export;
43 extern af_info_t af_info_volnorm;
44 extern af_info_t af_info_extrastereo;
45 extern af_info_t af_info_lavcac3enc;
46 extern af_info_t af_info_lavcresample;
47 extern af_info_t af_info_sweep;
48 extern af_info_t af_info_hrtf;
49 extern af_info_t af_info_ladspa;
50 extern af_info_t af_info_center;
51 extern af_info_t af_info_sinesuppress;
52 extern af_info_t af_info_karaoke;
53 extern af_info_t af_info_scaletempo;
54 extern af_info_t af_info_stats;
56 static af_info_t* filter_list[]={
57 &af_info_dummy,
58 &af_info_delay,
59 &af_info_channels,
60 &af_info_format,
61 &af_info_resample,
62 &af_info_volume,
63 &af_info_equalizer,
64 &af_info_gate,
65 &af_info_comp,
66 &af_info_pan,
67 &af_info_surround,
68 &af_info_sub,
69 #ifdef HAVE_SYS_MMAN_H
70 &af_info_export,
71 #endif
72 &af_info_volnorm,
73 &af_info_extrastereo,
74 #ifdef CONFIG_LIBAVCODEC_A
75 &af_info_lavcac3enc,
76 #endif
77 #ifdef CONFIG_LIBAVCODEC
78 &af_info_lavcresample,
79 #endif
80 &af_info_sweep,
81 &af_info_hrtf,
82 #ifdef CONFIG_LADSPA
83 &af_info_ladspa,
84 #endif
85 &af_info_center,
86 &af_info_sinesuppress,
87 &af_info_karaoke,
88 &af_info_scaletempo,
89 &af_info_stats,
90 NULL
93 // Message printing
94 af_msg_cfg_t af_msg_cfg={0,NULL,NULL};
96 // CPU speed
97 int* af_cpu_speed = NULL;
99 /* Find a filter in the static list of filters using it's name. This
100 function is used internally */
101 static af_info_t* af_find(char*name)
103 int i=0;
104 while(filter_list[i]){
105 if(!strcmp(filter_list[i]->name,name))
106 return filter_list[i];
107 i++;
109 af_msg(AF_MSG_ERROR,"Couldn't find audio filter '%s'\n",name);
110 return NULL;
113 /* Find filter in the dynamic filter list using it's name This
114 function is used for finding already initialized filters */
115 af_instance_t* af_get(af_stream_t* s, char* name)
117 af_instance_t* af=s->first;
118 // Find the filter
119 while(af != NULL){
120 if(!strcmp(af->info->name,name))
121 return af;
122 af=af->next;
124 return NULL;
127 /*/ Function for creating a new filter of type name. The name may
128 contain the commandline parameters for the filter */
129 static af_instance_t* af_create(af_stream_t* s, const char* name_with_cmd)
131 char* name = strdup(name_with_cmd);
132 char* cmdline = name;
134 // Allocate space for the new filter and reset all pointers
135 af_instance_t* new=malloc(sizeof(af_instance_t));
136 if (!name || !new) {
137 af_msg(AF_MSG_ERROR,"[libaf] Could not allocate memory\n");
138 goto err_out;
140 memset(new,0,sizeof(af_instance_t));
142 // Check for commandline parameters
143 strsep(&cmdline, "=");
145 // Find filter from name
146 if(NULL == (new->info=af_find(name)))
147 goto err_out;
149 /* Make sure that the filter is not already in the list if it is
150 non-reentrant */
151 if(new->info->flags & AF_FLAGS_NOT_REENTRANT){
152 if(af_get(s,name)){
153 af_msg(AF_MSG_ERROR,"[libaf] There can only be one instance of"
154 " the filter '%s' in each stream\n",name);
155 goto err_out;
159 af_msg(AF_MSG_VERBOSE,"[libaf] Adding filter %s \n",name);
161 // Initialize the new filter
162 if(AF_OK == new->info->open(new) &&
163 AF_ERROR < new->control(new,AF_CONTROL_POST_CREATE,&s->cfg)){
164 if(cmdline){
165 if(AF_ERROR>=new->control(new,AF_CONTROL_COMMAND_LINE,cmdline))
166 goto err_out;
168 free(name);
169 return new;
172 err_out:
173 free(new);
174 af_msg(AF_MSG_ERROR,"[libaf] Couldn't create or open audio filter '%s'\n",
175 name);
176 free(name);
177 return NULL;
180 /* Create and insert a new filter of type name before the filter in the
181 argument. This function can be called during runtime, the return
182 value is the new filter */
183 static af_instance_t* af_prepend(af_stream_t* s, af_instance_t* af, char* name)
185 // Create the new filter and make sure it is OK
186 af_instance_t* new=af_create(s,name);
187 if(!new)
188 return NULL;
189 // Update pointers
190 new->next=af;
191 if(af){
192 new->prev=af->prev;
193 af->prev=new;
195 else
196 s->last=new;
197 if(new->prev)
198 new->prev->next=new;
199 else
200 s->first=new;
201 return new;
204 /* Create and insert a new filter of type name after the filter in the
205 argument. This function can be called during runtime, the return
206 value is the new filter */
207 static af_instance_t* af_append(af_stream_t* s, af_instance_t* af, char* name)
209 // Create the new filter and make sure it is OK
210 af_instance_t* new=af_create(s,name);
211 if(!new)
212 return NULL;
213 // Update pointers
214 new->prev=af;
215 if(af){
216 new->next=af->next;
217 af->next=new;
219 else
220 s->first=new;
221 if(new->next)
222 new->next->prev=new;
223 else
224 s->last=new;
225 return new;
228 // Uninit and remove the filter "af"
229 void af_remove(af_stream_t* s, af_instance_t* af)
231 if(!af) return;
233 // Print friendly message
234 af_msg(AF_MSG_VERBOSE,"[libaf] Removing filter %s \n",af->info->name);
236 // Notify filter before changing anything
237 af->control(af,AF_CONTROL_PRE_DESTROY,0);
239 // Detach pointers
240 if(af->prev)
241 af->prev->next=af->next;
242 else
243 s->first=af->next;
244 if(af->next)
245 af->next->prev=af->prev;
246 else
247 s->last=af->prev;
249 // Uninitialize af and free memory
250 af->uninit(af);
251 free(af);
254 /* Reinitializes all filters downstream from the filter given in the
255 argument the return value is AF_OK if success and AF_ERROR if
256 failure */
257 static int af_reinit(af_stream_t* s, af_instance_t* af)
260 af_data_t in; // Format of the input to current filter
261 int rv=0; // Return value
263 // Check if there are any filters left in the list
264 if(NULL == af){
265 if(!(af=af_append(s,s->first,"dummy")))
266 return AF_UNKNOWN;
267 else
268 return AF_ERROR;
271 // Check if this is the first filter
272 if(!af->prev)
273 memcpy(&in,&(s->input),sizeof(af_data_t));
274 else
275 memcpy(&in,af->prev->data,sizeof(af_data_t));
276 // Reset just in case...
277 in.audio=NULL;
278 in.len=0;
280 rv = af->control(af,AF_CONTROL_REINIT,&in);
281 switch(rv){
282 case AF_OK:
283 af = af->next;
284 break;
285 case AF_FALSE:{ // Configuration filter is needed
286 // Do auto insertion only if force is not specified
287 if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){
288 af_instance_t* new = NULL;
289 // Insert channels filter
290 if((af->prev?af->prev->data->nch:s->input.nch) != in.nch){
291 // Create channels filter
292 if(NULL == (new = af_prepend(s,af,"channels")))
293 return AF_ERROR;
294 // Set number of output channels
295 if(AF_OK != (rv = new->control(new,AF_CONTROL_CHANNELS,&in.nch)))
296 return rv;
297 // Initialize channels filter
298 if(!new->prev)
299 memcpy(&in,&(s->input),sizeof(af_data_t));
300 else
301 memcpy(&in,new->prev->data,sizeof(af_data_t));
302 if(AF_OK != (rv = new->control(new,AF_CONTROL_REINIT,&in)))
303 return rv;
305 // Insert format filter
306 if((af->prev?af->prev->data->format:s->input.format) != in.format){
307 // Create format filter
308 if(NULL == (new = af_prepend(s,af,"format")))
309 return AF_ERROR;
310 // Set output bits per sample
311 in.format |= af_bits2fmt(in.bps*8);
312 if(AF_OK != (rv = new->control(new,AF_CONTROL_FORMAT_FMT,&in.format)))
313 return rv;
314 // Initialize format filter
315 if(!new->prev)
316 memcpy(&in,&(s->input),sizeof(af_data_t));
317 else
318 memcpy(&in,new->prev->data,sizeof(af_data_t));
319 if(AF_OK != (rv = new->control(new,AF_CONTROL_REINIT,&in)))
320 return rv;
322 if(!new){ // Should _never_ happen
323 af_msg(AF_MSG_ERROR,"[libaf] Unable to correct audio format. "
324 "This error should never uccur, please send bugreport.\n");
325 return AF_ERROR;
327 af=new->next;
329 else {
330 af_msg(AF_MSG_ERROR, "[libaf] Automatic filter insertion disabled "
331 "but formats do not match. Giving up.\n");
332 return AF_ERROR;
334 break;
336 case AF_DETACH:{ // Filter is redundant and wants to be unloaded
337 // Do auto remove only if force is not specified
338 if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){
339 af_instance_t* aft=af->prev;
340 af_remove(s,af);
341 if(aft)
342 af=aft->next;
343 else
344 af=s->first; // Restart configuration
346 break;
348 default:
349 af_msg(AF_MSG_ERROR,"[libaf] Reinitialization did not work, audio"
350 " filter '%s' returned error code %i\n",af->info->name,rv);
351 return AF_ERROR;
353 }while(af);
354 return AF_OK;
357 // Uninit and remove all filters
358 void af_uninit(af_stream_t* s)
360 while(s->first)
361 af_remove(s,s->first);
364 /* Initialize the stream "s". This function creates a new filter list
365 if necessary according to the values set in input and output. Input
366 and output should contain the format of the current movie and the
367 formate of the preferred output respectively. The function is
368 reentrant i.e. if called with an already initialized stream the
369 stream will be reinitialized.
370 If one of the prefered output parameters is 0 the one that needs
371 no conversion is used (i.e. the output format in the last filter).
372 The return value is 0 if success and -1 if failure */
373 int af_init(af_stream_t* s)
375 int i=0;
377 // Sanity check
378 if(!s) return -1;
380 // Precaution in case caller is misbehaving
381 s->input.audio = s->output.audio = NULL;
382 s->input.len = s->output.len = 0;
384 // Figure out how fast the machine is
385 if(AF_INIT_AUTO == (AF_INIT_TYPE_MASK & s->cfg.force))
386 s->cfg.force = (s->cfg.force & ~AF_INIT_TYPE_MASK) | AF_INIT_TYPE;
388 // Check if this is the first call
389 if(!s->first){
390 // Add all filters in the list (if there are any)
391 if(!s->cfg.list){ // To make automatic format conversion work
392 if(!af_append(s,s->first,"dummy"))
393 return -1;
395 else{
396 while(s->cfg.list[i]){
397 if(!af_append(s,s->last,s->cfg.list[i++]))
398 return -1;
403 // Init filters
404 if(AF_OK != af_reinit(s,s->first))
405 return -1;
407 // make sure the chain is not empty and valid (e.g. because of AF_DETACH)
408 if (!s->first)
409 if (!af_append(s,s->first,"dummy") || AF_OK != af_reinit(s,s->first))
410 return -1;
412 // Check output format
413 if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){
414 af_instance_t* af = NULL; // New filter
415 // Check output frequency if not OK fix with resample
416 if(s->output.rate && s->last->data->rate!=s->output.rate){
417 // try to find a filter that can change samplrate
418 af = af_control_any_rev(s, AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET,
419 &(s->output.rate));
420 if (!af) {
421 char *resampler = "resample";
422 #ifdef CONFIG_LIBAVCODEC
423 if ((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_SLOW)
424 resampler = "lavcresample";
425 #endif
426 if((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_SLOW){
427 if(!strcmp(s->first->info->name,"format"))
428 af = af_append(s,s->first,resampler);
429 else
430 af = af_prepend(s,s->first,resampler);
432 else{
433 if(!strcmp(s->last->info->name,"format"))
434 af = af_prepend(s,s->last,resampler);
435 else
436 af = af_append(s,s->last,resampler);
438 // Init the new filter
439 if(!af || (AF_OK != af->control(af,AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET,
440 &(s->output.rate))))
441 return -1;
442 // Use lin int if the user wants fast
443 if ((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_FAST) {
444 char args[32];
445 sprintf(args, "%d", s->output.rate);
446 #ifdef CONFIG_LIBAVCODEC
447 if (strcmp(resampler, "lavcresample") == 0)
448 strcat(args, ":1");
449 else
450 #endif
451 strcat(args, ":0:0");
452 af->control(af, AF_CONTROL_COMMAND_LINE, args);
455 if(AF_OK != af_reinit(s,af))
456 return -1;
459 // Check number of output channels fix if not OK
460 // If needed always inserted last -> easy to screw up other filters
461 if(s->output.nch && s->last->data->nch!=s->output.nch){
462 if(!strcmp(s->last->info->name,"format"))
463 af = af_prepend(s,s->last,"channels");
464 else
465 af = af_append(s,s->last,"channels");
466 // Init the new filter
467 if(!af || (AF_OK != af->control(af,AF_CONTROL_CHANNELS,&(s->output.nch))))
468 return -1;
469 if(AF_OK != af_reinit(s,af))
470 return -1;
473 // Check output format fix if not OK
474 if(s->output.format != AF_FORMAT_UNKNOWN &&
475 s->last->data->format != s->output.format){
476 if(strcmp(s->last->info->name,"format"))
477 af = af_append(s,s->last,"format");
478 else
479 af = s->last;
480 // Init the new filter
481 s->output.format |= af_bits2fmt(s->output.bps*8);
482 if(!af || (AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT,&(s->output.format))))
483 return -1;
484 if(AF_OK != af_reinit(s,af))
485 return -1;
488 // Re init again just in case
489 if(AF_OK != af_reinit(s,s->first))
490 return -1;
492 if (s->output.format == AF_FORMAT_UNKNOWN)
493 s->output.format = s->last->data->format;
494 if (!s->output.nch) s->output.nch = s->last->data->nch;
495 if (!s->output.rate) s->output.rate = s->last->data->rate;
496 if((s->last->data->format != s->output.format) ||
497 (s->last->data->nch != s->output.nch) ||
498 (s->last->data->rate != s->output.rate)) {
499 // Something is stuffed audio out will not work
500 af_msg(AF_MSG_ERROR,"[libaf] Unable to setup filter system can not"
501 " meet sound-card demands, please send bugreport. \n");
502 af_uninit(s);
503 return -1;
506 return 0;
509 /* Add filter during execution. This function adds the filter "name"
510 to the stream s. The filter will be inserted somewhere nice in the
511 list of filters. The return value is a pointer to the new filter,
512 If the filter couldn't be added the return value is NULL. */
513 af_instance_t* af_add(af_stream_t* s, char* name){
514 af_instance_t* new;
515 // Sanity check
516 if(!s || !s->first || !name)
517 return NULL;
518 // Insert the filter somwhere nice
519 if(!strcmp(s->first->info->name,"format"))
520 new = af_append(s, s->first, name);
521 else
522 new = af_prepend(s, s->first, name);
523 if(!new)
524 return NULL;
526 // Reinitalize the filter list
527 if(AF_OK != af_reinit(s, s->first)){
528 free(new);
529 return NULL;
531 return new;
534 // Filter data chunk through the filters in the list
535 af_data_t* af_play(af_stream_t* s, af_data_t* data)
537 af_instance_t* af=s->first;
538 // Iterate through all filters
540 if (data->len <= 0) break;
541 data=af->play(af,data);
542 af=af->next;
543 }while(af && data);
544 return data;
547 /* Calculate the minimum output buffer size for given input data d
548 * when using the RESIZE_LOCAL_BUFFER macro. The +t+1 part ensures the
549 * value is >= len*mul rounded upwards to whole samples even if the
550 * double 'mul' is inexact. */
551 int af_lencalc(double mul, af_data_t* d)
553 int t = d->bps * d->nch;
554 return d->len * mul + t + 1;
557 // Calculate average ratio of filter output size to input size
558 double af_calc_filter_multiplier(af_stream_t* s)
560 af_instance_t* af=s->first;
561 double mul = 1;
562 // Iterate through all filters and calculate total multiplication factor
564 mul *= af->mul;
565 af=af->next;
566 }while(af);
568 return mul;
571 /* Calculate the total delay [bytes output] caused by the filters */
572 double af_calc_delay(af_stream_t* s)
574 af_instance_t* af=s->first;
575 register double delay = 0.0;
576 // Iterate through all filters
577 while(af){
578 delay += af->delay;
579 delay *= af->mul;
580 af=af->next;
582 return delay;
585 /* Helper function called by the macro with the same name this
586 function should not be called directly */
587 int af_resize_local_buffer(af_instance_t* af, af_data_t* data)
589 // Calculate new length
590 register int len = af_lencalc(af->mul,data);
591 af_msg(AF_MSG_VERBOSE,"[libaf] Reallocating memory in module %s, "
592 "old len = %i, new len = %i\n",af->info->name,af->data->len,len);
593 // If there is a buffer free it
594 if(af->data->audio)
595 free(af->data->audio);
596 // Create new buffer and check that it is OK
597 af->data->audio = malloc(len);
598 if(!af->data->audio){
599 af_msg(AF_MSG_FATAL,"[libaf] Could not allocate memory \n");
600 return AF_ERROR;
602 af->data->len=len;
603 return AF_OK;
606 // documentation in af.h
607 af_instance_t *af_control_any_rev (af_stream_t* s, int cmd, void* arg) {
608 int res = AF_UNKNOWN;
609 af_instance_t* filt = s->last;
610 while (filt) {
611 res = filt->control(filt, cmd, arg);
612 if (res == AF_OK)
613 return filt;
614 filt = filt->prev;
616 return NULL;
619 void af_help (void) {
620 int i = 0;
621 af_msg(AF_MSG_INFO, "Available audio filters:\n");
622 while (filter_list[i]) {
623 if (filter_list[i]->comment && filter_list[i]->comment[0])
624 af_msg(AF_MSG_INFO, " %-15s: %s (%s)\n", filter_list[i]->name, filter_list[i]->info, filter_list[i]->comment);
625 else
626 af_msg(AF_MSG_INFO, " %-15s: %s\n", filter_list[i]->name, filter_list[i]->info);
627 i++;
631 void af_fix_parameters(af_data_t *data)
633 data->bps = af_fmt2bits(data->format)/8;