Mercurial > mplayer.hg
annotate libaf/af_surround.c @ 20128:47c45cd077d4
Remove unneeded casts for malloc/realloc calls
author | reimar |
---|---|
date | Mon, 09 Oct 2006 17:41:37 +0000 |
parents | de13fd557440 |
children | cea0eb833758 |
rev | line source |
---|---|
8678 | 1 /* |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
2 This is an libaf filter to do simple decoding of matrixed surround |
8678 | 3 sound. This will provide a (basic) surround-sound effect from |
4 audio encoded for Dolby Surround, Pro Logic etc. | |
5 | |
6 * This program 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. | |
10 * | |
11 * This program 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. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | |
20 Original author: Steve Davies <steve@daviesfam.org> | |
21 */ | |
22 | |
23 /* The principle: Make rear channels by extracting anti-phase data | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
24 from the front channels, delay by 20ms and feed to rear in anti-phase |
8678 | 25 */ |
26 | |
27 | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
28 /* SPLITREAR: Define to decode two distinct rear channels - this |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
29 doesn't work so well in practice because separation in a passive |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
30 matrix is not high. C (dialogue) to Ls and Rs 14dB or so - so |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
31 dialogue leaks to the rear. Still - give it a try and send |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
32 feedback. Comment this define for old behavior of a single |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
33 surround sent to rear in anti-phase */ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
34 #define SPLITREAR 1 |
8678 | 35 |
36 #include <stdio.h> | |
37 #include <stdlib.h> | |
38 #include <string.h> | |
39 #include <unistd.h> | |
40 | |
41 #include "af.h" | |
42 #include "dsp.h" | |
43 | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
44 #define L 32 // Length of fir filter |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
45 #define LD 65536 // Length of delay buffer |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
46 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
47 // 32 Tap fir filter loop unrolled |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
48 #define FIR(x,w,y) \ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
49 y = ( w[0] *x[0] +w[1] *x[1] +w[2] *x[2] +w[3] *x[3] \ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
50 + w[4] *x[4] +w[5] *x[5] +w[6] *x[6] +w[7] *x[7] \ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
51 + w[8] *x[8] +w[9] *x[9] +w[10]*x[10]+w[11]*x[11] \ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
52 + w[12]*x[12]+w[13]*x[13]+w[14]*x[14]+w[15]*x[15] \ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
53 + w[16]*x[16]+w[17]*x[17]+w[18]*x[18]+w[19]*x[19] \ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
54 + w[20]*x[20]+w[21]*x[21]+w[22]*x[22]+w[23]*x[23] \ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
55 + w[24]*x[24]+w[25]*x[25]+w[26]*x[26]+w[27]*x[27] \ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
56 + w[28]*x[28]+w[29]*x[29]+w[30]*x[30]+w[31]*x[31]) |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
57 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
58 // Add to circular queue macro + update index |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
59 #ifdef SPLITREAR |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
60 #define ADDQUE(qi,rq,lq,r,l)\ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
61 lq[qi]=lq[qi+L]=(l);\ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
62 rq[qi]=rq[qi+L]=(r);\ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
63 qi=(qi-1)&(L-1); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
64 #else |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
65 #define ADDQUE(qi,lq,l)\ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
66 lq[qi]=lq[qi+L]=(l);\ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
67 qi=(qi-1)&(L-1); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
68 #endif |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
69 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
70 // Macro for updating queue index in delay queues |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
71 #define UPDATEQI(qi) qi=(qi+1)&(LD-1) |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
72 |
8678 | 73 // instance data |
74 typedef struct af_surround_s | |
75 { | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
76 float lq[2*L]; // Circular queue for filtering left rear channel |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
77 float rq[2*L]; // Circular queue for filtering right rear channel |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
78 float w[L]; // FIR filter coefficients for surround sound 7kHz low-pass |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
79 float* dr; // Delay queue right rear channel |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
80 float* dl; // Delay queue left rear channel |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
81 float d; // Delay time |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
82 int i; // Position in circular buffer |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
83 int wi; // Write index for delay queue |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
84 int ri; // Read index for delay queue |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
85 }af_surround_t; |
8678 | 86 |
87 // Initialization and runtime control | |
88 static int control(struct af_instance_s* af, int cmd, void* arg) | |
89 { | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
90 af_surround_t *s = af->setup; |
8678 | 91 switch(cmd){ |
92 case AF_CONTROL_REINIT:{ | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
93 float fc; |
8678 | 94 af->data->rate = ((af_data_t*)arg)->rate; |
95 af->data->nch = ((af_data_t*)arg)->nch*2; | |
14245 | 96 af->data->format = AF_FORMAT_FLOAT_NE; |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
97 af->data->bps = 4; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
98 |
8678 | 99 if (af->data->nch != 4){ |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
100 af_msg(AF_MSG_ERROR,"[surround] Only stereo input is supported.\n"); |
8678 | 101 return AF_DETACH; |
102 } | |
103 // Surround filer coefficients | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
104 fc = 2.0 * 7000.0/(float)af->data->rate; |
14275
de13fd557440
less namespace pollution #2 (prefixed globals in filter.c with af_filter_)
alex
parents:
14245
diff
changeset
|
105 if (-1 == af_filter_design_fir(L, s->w, &fc, LP|HAMMING, 0)){ |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
106 af_msg(AF_MSG_ERROR,"[surround] Unable to design low-pass filter.\n"); |
8678 | 107 return AF_ERROR; |
108 } | |
109 | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
110 // Free previous delay queues |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
111 if(s->dl) |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
112 free(s->dl); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
113 if(s->dr) |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
114 free(s->dr); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
115 // Allocate new delay queues |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
116 s->dl = calloc(LD,af->data->bps); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
117 s->dr = calloc(LD,af->data->bps); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
118 if((NULL == s->dl) || (NULL == s->dr)) |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
119 af_msg(AF_MSG_FATAL,"[delay] Out of memory\n"); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
120 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
121 // Initialize delay queue index |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
122 if(AF_OK != af_from_ms(1, &s->d, &s->wi, af->data->rate, 0.0, 1000.0)) |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
123 return AF_ERROR; |
12569 | 124 // printf("%i\n",s->wi); |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
125 s->ri = 0; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
126 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
127 if((af->data->format != ((af_data_t*)arg)->format) || |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
128 (af->data->bps != ((af_data_t*)arg)->bps)){ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
129 ((af_data_t*)arg)->format = af->data->format; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
130 ((af_data_t*)arg)->bps = af->data->bps; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
131 return AF_FALSE; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
132 } |
8678 | 133 return AF_OK; |
134 } | |
135 case AF_CONTROL_COMMAND_LINE:{ | |
136 float d = 0; | |
137 sscanf((char*)arg,"%f",&d); | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
138 if ((d < 0) || (d > 1000)){ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
139 af_msg(AF_MSG_ERROR,"[surround] Invalid delay time, valid time values" |
10719
caec33353477
1000l and I imho this would win 'The Bug/Typo of the Year' award
alex
parents:
8763
diff
changeset
|
140 " are 0ms to 1000ms current value is %0.3f ms\n",d); |
8678 | 141 return AF_ERROR; |
142 } | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
143 s->d = d; |
8678 | 144 return AF_OK; |
145 } | |
146 } | |
147 return AF_UNKNOWN; | |
148 } | |
149 | |
150 // Deallocate memory | |
151 static void uninit(struct af_instance_s* af) | |
152 { | |
153 if(af->data->audio) | |
154 free(af->data->audio); | |
155 if(af->data) | |
156 free(af->data); | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
157 if(af->setup) |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
158 free(af->setup); |
8678 | 159 } |
160 | |
161 // The beginnings of an active matrix... | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
162 static float steering_matrix[][12] = { |
8678 | 163 // LL RL LR RR LS RS |
164 // LLs RLs LRs RRs LC RC | |
165 {.707, .0, .0, .707, .5, -.5, | |
166 .5878, -.3928, .3928, -.5878, .5, .5}, | |
167 }; | |
168 | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
169 // Experimental moving average dominance |
8678 | 170 //static int amp_L = 0, amp_R = 0, amp_C = 0, amp_S = 0; |
171 | |
172 // Filter data through filter | |
173 static af_data_t* play(struct af_instance_s* af, af_data_t* data){ | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
174 af_surround_t* s = (af_surround_t*)af->setup; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
175 float* m = steering_matrix[0]; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
176 float* in = data->audio; // Input audio data |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
177 float* out = NULL; // Output audio data |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
178 float* end = in + data->len / sizeof(float); // Loop end |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
179 int i = s->i; // Filter queue index |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
180 int ri = s->ri; // Read index for delay queue |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
181 int wi = s->wi; // Write index for delay queue |
8678 | 182 |
183 if (AF_OK != RESIZE_LOCAL_BUFFER(af, data)) | |
184 return NULL; | |
185 | |
186 out = af->data->audio; | |
187 | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
188 while(in < end){ |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
189 /* Dominance: |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
190 abs(in[0]) abs(in[1]); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
191 abs(in[0]+in[1]) abs(in[0]-in[1]); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
192 10 * log( abs(in[0]) / (abs(in[1])|1) ); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
193 10 * log( abs(in[0]+in[1]) / (abs(in[0]-in[1])|1) ); */ |
8678 | 194 |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
195 /* About volume balancing... |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
196 Surround encoding does the following: |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
197 Lt=L+.707*C+.707*S, Rt=R+.707*C-.707*S |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
198 So S should be extracted as: |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
199 (Lt-Rt) |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
200 But we are splitting the S to two output channels, so we |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
201 must take 3dB off as we split it: |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
202 Ls=Rs=.707*(Lt-Rt) |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
203 Trouble is, Lt could be +1, Rt -1, so possibility that S will |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
204 overflow. So to avoid that, we cut L/R by 3dB (*.707), and S by |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
205 6dB (/2). This keeps the overall balance, but guarantees no |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
206 overflow. */ |
8678 | 207 |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
208 // Output front left and right |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
209 out[0] = m[0]*in[0] + m[1]*in[1]; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
210 out[1] = m[2]*in[0] + m[3]*in[1]; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
211 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
212 // Low-pass output @ 7kHz |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
213 FIR((&s->lq[i]), s->w, s->dl[wi]); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
214 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
215 // Delay output by d ms |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
216 out[2] = s->dl[ri]; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
217 |
8678 | 218 #ifdef SPLITREAR |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
219 // Low-pass output @ 7kHz |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
220 FIR((&s->rq[i]), s->w, s->dr[wi]); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
221 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
222 // Delay output by d ms |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
223 out[3] = s->dr[ri]; |
8678 | 224 #else |
225 out[3] = -out[2]; | |
226 #endif | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
227 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
228 // Update delay queues indexes |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
229 UPDATEQI(ri); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
230 UPDATEQI(wi); |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
231 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
232 // Calculate and save surround in circular queue |
8678 | 233 #ifdef SPLITREAR |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
234 ADDQUE(i, s->rq, s->lq, m[6]*in[0]+m[7]*in[1], m[8]*in[0]+m[9]*in[1]); |
8678 | 235 #else |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
236 ADDQUE(i, s->lq, m[4]*in[0]+m[5]*in[1]); |
8678 | 237 #endif |
238 | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
239 // Next sample... |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
240 in = &in[data->nch]; |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
241 out = &out[af->data->nch]; |
8678 | 242 } |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
243 |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
244 // Save indexes |
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
245 s->i = i; s->ri = ri; s->wi = wi; |
8678 | 246 |
247 // Set output data | |
248 data->audio = af->data->audio; | |
249 data->len = (data->len*af->mul.n)/af->mul.d; | |
250 data->nch = af->data->nch; | |
251 | |
252 return data; | |
253 } | |
254 | |
255 static int open(af_instance_t* af){ | |
256 af->control=control; | |
257 af->uninit=uninit; | |
258 af->play=play; | |
259 af->mul.n=2; | |
260 af->mul.d=1; | |
261 af->data=calloc(1,sizeof(af_data_t)); | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
262 af->setup=calloc(1,sizeof(af_surround_t)); |
8678 | 263 if(af->data == NULL || af->setup == NULL) |
264 return AF_ERROR; | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
265 ((af_surround_t*)af->setup)->d = 20; |
8678 | 266 return AF_OK; |
267 } | |
268 | |
269 af_info_t af_info_surround = | |
270 { | |
271 "Surround decoder filter", | |
272 "surround", | |
273 "Steve Davies <steve@daviesfam.org>", | |
274 "", | |
8763
19e96e60a3d0
Speed optimizations (runs twise as fast) and bugfix (wrong cutoff frequency buffer over run noise and garbeled output when wrong input format)
anders
parents:
8678
diff
changeset
|
275 AF_FLAGS_NOT_REENTRANT, |
8678 | 276 open |
277 }; |