gl_common: minor cleanup/refactor
[mplayer.git] / libaf / af_extrastereo.c
blob347c257137fa35c29bd03c256eb554b8feee647a
1 /*
2 * Copyright (C) 2004 Alex Beregszaszi & Pierre Lombard
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <inttypes.h>
26 #include <math.h>
27 #include <limits.h>
29 #include "af.h"
31 // Data for specific instances of this filter
32 typedef struct af_extrastereo_s
34 float mul;
35 }af_extrastereo_t;
37 static af_data_t* play_s16(struct af_instance_s* af, af_data_t* data);
38 static af_data_t* play_float(struct af_instance_s* af, af_data_t* data);
40 // Initialization and runtime control
41 static int control(struct af_instance_s* af, int cmd, void* arg)
43 af_extrastereo_t* s = (af_extrastereo_t*)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 = 2;
52 if (((af_data_t*)arg)->format == AF_FORMAT_FLOAT_NE)
54 af->data->format = AF_FORMAT_FLOAT_NE;
55 af->data->bps = 4;
56 af->play = play_float;
57 }// else
59 af->data->format = AF_FORMAT_S16_NE;
60 af->data->bps = 2;
61 af->play = play_s16;
64 return af_test_output(af,(af_data_t*)arg);
66 case AF_CONTROL_COMMAND_LINE:{
67 float f;
68 sscanf((char*)arg,"%f", &f);
69 s->mul = f;
70 return AF_OK;
72 case AF_CONTROL_ES_MUL | AF_CONTROL_SET:
73 s->mul = *(float*)arg;
74 return AF_OK;
75 case AF_CONTROL_ES_MUL | AF_CONTROL_GET:
76 *(float*)arg = s->mul;
77 return AF_OK;
79 return AF_UNKNOWN;
82 // Deallocate memory
83 static void uninit(struct af_instance_s* af)
85 free(af->data);
86 free(af->setup);
89 // Filter data through filter
90 static af_data_t* play_s16(struct af_instance_s* af, af_data_t* data)
92 af_extrastereo_t *s = af->setup;
93 register int i = 0;
94 int16_t *a = (int16_t*)data->audio; // Audio data
95 int len = data->len/2; // Number of samples
96 int avg, l, r;
98 for (i = 0; i < len; i+=2)
100 avg = (a[i] + a[i + 1]) / 2;
102 l = avg + (int)(s->mul * (a[i] - avg));
103 r = avg + (int)(s->mul * (a[i + 1] - avg));
105 a[i] = clamp(l, SHRT_MIN, SHRT_MAX);
106 a[i + 1] = clamp(r, SHRT_MIN, SHRT_MAX);
109 return data;
112 static af_data_t* play_float(struct af_instance_s* af, af_data_t* data)
114 af_extrastereo_t *s = af->setup;
115 register int i = 0;
116 float *a = (float*)data->audio; // Audio data
117 int len = data->len/4; // Number of samples
118 float avg, l, r;
120 for (i = 0; i < len; i+=2)
122 avg = (a[i] + a[i + 1]) / 2;
124 l = avg + (s->mul * (a[i] - avg));
125 r = avg + (s->mul * (a[i + 1] - avg));
127 a[i] = af_softclip(l);
128 a[i + 1] = af_softclip(r);
131 return data;
134 // Allocate memory and set function pointers
135 static int af_open(af_instance_t* af){
136 af->control=control;
137 af->uninit=uninit;
138 af->play=play_s16;
139 af->mul=1;
140 af->data=calloc(1,sizeof(af_data_t));
141 af->setup=calloc(1,sizeof(af_extrastereo_t));
142 if(af->data == NULL || af->setup == NULL)
143 return AF_ERROR;
145 ((af_extrastereo_t*)af->setup)->mul = 2.5;
146 return AF_OK;
149 // Description of this filter
150 af_info_t af_info_extrastereo = {
151 "Increase difference between audio channels",
152 "extrastereo",
153 "Alex Beregszaszi & Pierre Lombard",
155 AF_FLAGS_NOT_REENTRANT,
156 af_open