stream.h: Add 2 prototypes instead of declaring them in cache2.c
[mplayer.git] / libaf / af_extrastereo.c
blob7159221d54df2c465fca6c945ee27544e5fe3830
1 /*=============================================================================
2 //
3 // This software has been released under the terms of the GNU General Public
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
5 //
6 // Copyright 2004 Alex Beregszaszi & Pierre Lombard
7 //
8 //=============================================================================
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
15 #include <inttypes.h>
16 #include <math.h>
17 #include <limits.h>
19 #include "af.h"
21 // Data for specific instances of this filter
22 typedef struct af_extrastereo_s
24 float mul;
25 }af_extrastereo_t;
27 static af_data_t* play_s16(struct af_instance_s* af, af_data_t* data);
28 static af_data_t* play_float(struct af_instance_s* af, af_data_t* data);
30 // Initialization and runtime control
31 static int control(struct af_instance_s* af, int cmd, void* arg)
33 af_extrastereo_t* s = (af_extrastereo_t*)af->setup;
35 switch(cmd){
36 case AF_CONTROL_REINIT:{
37 // Sanity check
38 if(!arg) return AF_ERROR;
40 af->data->rate = ((af_data_t*)arg)->rate;
41 af->data->nch = 2;
42 if (((af_data_t*)arg)->format == AF_FORMAT_FLOAT_NE)
44 af->data->format = AF_FORMAT_FLOAT_NE;
45 af->data->bps = 4;
46 af->play = play_float;
47 }// else
49 af->data->format = AF_FORMAT_S16_NE;
50 af->data->bps = 2;
51 af->play = play_s16;
54 return af_test_output(af,(af_data_t*)arg);
56 case AF_CONTROL_COMMAND_LINE:{
57 float f;
58 sscanf((char*)arg,"%f", &f);
59 s->mul = f;
60 return AF_OK;
62 case AF_CONTROL_ES_MUL | AF_CONTROL_SET:
63 s->mul = *(float*)arg;
64 return AF_OK;
65 case AF_CONTROL_ES_MUL | AF_CONTROL_GET:
66 *(float*)arg = s->mul;
67 return AF_OK;
69 return AF_UNKNOWN;
72 // Deallocate memory
73 static void uninit(struct af_instance_s* af)
75 if(af->data)
76 free(af->data);
77 if(af->setup)
78 free(af->setup);
81 // Filter data through filter
82 static af_data_t* play_s16(struct af_instance_s* af, af_data_t* data)
84 af_extrastereo_t *s = af->setup;
85 register int i = 0;
86 int16_t *a = (int16_t*)data->audio; // Audio data
87 int len = data->len/2; // Number of samples
88 int avg, l, r;
90 for (i = 0; i < len; i+=2)
92 avg = (a[i] + a[i + 1]) / 2;
94 l = avg + (int)(s->mul * (a[i] - avg));
95 r = avg + (int)(s->mul * (a[i + 1] - avg));
97 a[i] = clamp(l, SHRT_MIN, SHRT_MAX);
98 a[i + 1] = clamp(r, SHRT_MIN, SHRT_MAX);
101 return data;
104 static af_data_t* play_float(struct af_instance_s* af, af_data_t* data)
106 af_extrastereo_t *s = af->setup;
107 register int i = 0;
108 float *a = (float*)data->audio; // Audio data
109 int len = data->len/4; // Number of samples
110 float avg, l, r;
112 for (i = 0; i < len; i+=2)
114 avg = (a[i] + a[i + 1]) / 2;
116 l = avg + (s->mul * (a[i] - avg));
117 r = avg + (s->mul * (a[i + 1] - avg));
119 a[i] = af_softclip(l);
120 a[i + 1] = af_softclip(r);
123 return data;
126 // Allocate memory and set function pointers
127 static int af_open(af_instance_t* af){
128 af->control=control;
129 af->uninit=uninit;
130 af->play=play_s16;
131 af->mul=1;
132 af->data=calloc(1,sizeof(af_data_t));
133 af->setup=calloc(1,sizeof(af_extrastereo_t));
134 if(af->data == NULL || af->setup == NULL)
135 return AF_ERROR;
137 ((af_extrastereo_t*)af->setup)->mul = 2.5;
138 return AF_OK;
141 // Description of this filter
142 af_info_t af_info_extrastereo = {
143 "Extra stereo",
144 "extrastereo",
145 "Alex Beregszaszi & Pierre Lombard",
147 AF_FLAGS_NOT_REENTRANT,
148 af_open