sync with en/mplayer.1 r29555
[mplayer/glamo.git] / libaf / af_extrastereo.c
blobf0af96ff06c7936f16242b466e2381ccf49b38a4
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 if(af->data)
86 free(af->data);
87 if(af->setup)
88 free(af->setup);
91 // Filter data through filter
92 static af_data_t* play_s16(struct af_instance_s* af, af_data_t* data)
94 af_extrastereo_t *s = af->setup;
95 register int i = 0;
96 int16_t *a = (int16_t*)data->audio; // Audio data
97 int len = data->len/2; // Number of samples
98 int avg, l, r;
100 for (i = 0; i < len; i+=2)
102 avg = (a[i] + a[i + 1]) / 2;
104 l = avg + (int)(s->mul * (a[i] - avg));
105 r = avg + (int)(s->mul * (a[i + 1] - avg));
107 a[i] = clamp(l, SHRT_MIN, SHRT_MAX);
108 a[i + 1] = clamp(r, SHRT_MIN, SHRT_MAX);
111 return data;
114 static af_data_t* play_float(struct af_instance_s* af, af_data_t* data)
116 af_extrastereo_t *s = af->setup;
117 register int i = 0;
118 float *a = (float*)data->audio; // Audio data
119 int len = data->len/4; // Number of samples
120 float avg, l, r;
122 for (i = 0; i < len; i+=2)
124 avg = (a[i] + a[i + 1]) / 2;
126 l = avg + (s->mul * (a[i] - avg));
127 r = avg + (s->mul * (a[i + 1] - avg));
129 a[i] = af_softclip(l);
130 a[i + 1] = af_softclip(r);
133 return data;
136 // Allocate memory and set function pointers
137 static int af_open(af_instance_t* af){
138 af->control=control;
139 af->uninit=uninit;
140 af->play=play_s16;
141 af->mul=1;
142 af->data=calloc(1,sizeof(af_data_t));
143 af->setup=calloc(1,sizeof(af_extrastereo_t));
144 if(af->data == NULL || af->setup == NULL)
145 return AF_ERROR;
147 ((af_extrastereo_t*)af->setup)->mul = 2.5;
148 return AF_OK;
151 // Description of this filter
152 af_info_t af_info_extrastereo = {
153 "Extra stereo",
154 "extrastereo",
155 "Alex Beregszaszi & Pierre Lombard",
157 AF_FLAGS_NOT_REENTRANT,
158 af_open