spudec: Fix calc_bbox to always return sensible values
[mplayer/glamo.git] / libaf / af_center.c
blobd3c867c39e6491ce123adbe160650bf1b5ac5616
1 /*
2 * This filter adds a center channel to the audio stream by
3 * averaging the left and right channel.
4 * There are two runtime controls one for setting which channel
5 * to insert the center-audio into called AF_CONTROL_SUB_CH.
7 * FIXME: implement a high-pass filter for better results.
9 * copyright (c) 2005 Alex Beregszaszi
11 * This file is part of MPlayer.
13 * MPlayer is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * MPlayer is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include "af.h"
34 // Data for specific instances of this filter
35 typedef struct af_center_s
37 int ch; // Channel number which to insert the filtered data
38 }af_center_t;
40 // Initialization and runtime control
41 static int control(struct af_instance_s* af, int cmd, void* arg)
43 af_center_t* s = af->setup;
45 switch(cmd){
46 case AF_CONTROL_REINIT:{
47 // Sanity check
48 if(!arg) return AF_ERROR;
50 af->data->rate = ((af_data_t*)arg)->rate;
51 af->data->nch = max(s->ch+1,((af_data_t*)arg)->nch);
52 af->data->format = AF_FORMAT_FLOAT_NE;
53 af->data->bps = 4;
55 return af_test_output(af,(af_data_t*)arg);
57 case AF_CONTROL_COMMAND_LINE:{
58 int ch=1;
59 sscanf(arg,"%i", &ch);
60 return control(af,AF_CONTROL_CENTER_CH | AF_CONTROL_SET, &ch);
62 case AF_CONTROL_CENTER_CH | AF_CONTROL_SET: // Requires reinit
63 // Sanity check
64 if((*(int*)arg >= AF_NCH) || (*(int*)arg < 0)){
65 mp_msg(MSGT_AFILTER, MSGL_ERR, "[sub] Center channel number must be between "
66 " 0 and %i current value is %i\n", AF_NCH-1, *(int*)arg);
67 return AF_ERROR;
69 s->ch = *(int*)arg;
70 return AF_OK;
71 case AF_CONTROL_CENTER_CH | AF_CONTROL_GET:
72 *(int*)arg = s->ch;
73 return AF_OK;
75 return AF_UNKNOWN;
78 // Deallocate memory
79 static void uninit(struct af_instance_s* af)
81 if(af->data)
82 free(af->data);
83 if(af->setup)
84 free(af->setup);
87 // Filter data through filter
88 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
90 af_data_t* c = data; // Current working data
91 af_center_t* s = af->setup; // Setup for this instance
92 float* a = c->audio; // Audio data
93 int len = c->len/4; // Number of samples in current audio block
94 int nch = c->nch; // Number of channels
95 int ch = s->ch; // Channel in which to insert the center audio
96 register int i;
98 // Run filter
99 for(i=0;i<len;i+=nch){
100 // Average left and right
101 a[i+ch] = (a[i]/2) + (a[i+1]/2);
104 return c;
107 // Allocate memory and set function pointers
108 static int af_open(af_instance_t* af){
109 af_center_t* s;
110 af->control=control;
111 af->uninit=uninit;
112 af->play=play;
113 af->mul=1;
114 af->data=calloc(1,sizeof(af_data_t));
115 af->setup=s=calloc(1,sizeof(af_center_t));
116 if(af->data == NULL || af->setup == NULL)
117 return AF_ERROR;
118 // Set default values
119 s->ch = 1; // Channel nr 2
120 return AF_OK;
123 // Description of this filter
124 af_info_t af_info_center = {
125 "Audio filter for adding a center channel",
126 "center",
127 "Alex Beregszaszi",
129 AF_FLAGS_NOT_REENTRANT,
130 af_open