Mercurial > mplayer.hg
annotate libao2/pl_surround.c @ 4130:c0b9e4a12df0
CYUV credits to Tim Ferguson
author | gabucino |
---|---|
date | Sun, 13 Jan 2002 09:37:35 +0000 |
parents | d358dc143a9e |
children | 8336b1cf8d70 |
rev | line source |
---|---|
3313 | 1 /* |
2 This is an ao2 plugin to do simple decoding of matrixed surround | |
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 | |
3681 | 24 from the front channels, delay by 20msec and feed to rear in anti-phase |
3313 | 25 */ |
26 | |
27 | |
3681 | 28 // SPLITREAR: Define to decode two distinct rear channels - |
29 // this doesn't work so well in practice because | |
30 // separation in a passive matrix is not high. | |
31 // C (dialogue) to Ls and Rs 14dB or so - | |
32 // so dialogue leaks to the rear. | |
33 // Still - give it a try and send feedback. | |
34 // comment this define for old behaviour of a single | |
35 // surround sent to rear in anti-phase | |
36 #define SPLITREAR | |
37 | |
38 | |
3313 | 39 #include <stdio.h> |
40 #include <stdlib.h> | |
3317 | 41 #include <unistd.h> |
3313 | 42 |
43 #include "audio_out.h" | |
44 #include "audio_plugin.h" | |
45 #include "audio_plugin_internal.h" | |
46 #include "afmt.h" | |
47 | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
48 #include "remez.h" |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
49 #include "firfilter.c" |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
50 |
3313 | 51 static ao_info_t info = |
52 { | |
53 "Surround decoder plugin", | |
54 "surround", | |
55 "Steve Davies <steve@daviesfam.org>", | |
56 "" | |
57 }; | |
58 | |
59 LIBAO_PLUGIN_EXTERN(surround) | |
60 | |
61 // local data | |
62 typedef struct pl_surround_s | |
63 { | |
64 int passthrough; // Just be a "NO-OP" | |
65 int msecs; // Rear channel delay in milliseconds | |
66 int16_t* databuf; // Output audio buffer | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
67 int16_t* Ls_delaybuf; // circular buffer to be used for delaying Ls audio |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
68 int16_t* Rs_delaybuf; // circular buffer to be used for delaying Rs audio |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
69 int delaybuf_len; // delaybuf buffer length in samples |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
70 int delaybuf_pos; // offset in buffer where we are reading/writing |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
71 double* filter_coefs_surround; // FIR filter coefficients for surround sound 7kHz lowpass |
3313 | 72 int rate; // input data rate |
73 int format; // input format | |
74 int input_channels; // input channels | |
75 | |
76 } pl_surround_t; | |
77 | |
3681 | 78 static pl_surround_t pl_surround={0,20,NULL,NULL,NULL,0,0,NULL,0,0,0}; |
3313 | 79 |
80 // to set/get/query special features/parameters | |
81 static int control(int cmd,int arg){ | |
82 switch(cmd){ | |
83 case AOCONTROL_PLUGIN_SET_LEN: | |
84 if (pl_surround.passthrough) return CONTROL_OK; | |
85 //fprintf(stderr, "pl_surround: AOCONTROL_PLUGIN_SET_LEN with arg=%d\n", arg); | |
86 //fprintf(stderr, "pl_surround: ao_plugin_data.len=%d\n", ao_plugin_data.len); | |
87 // Allocate an output buffer | |
88 if (pl_surround.databuf != NULL) { | |
89 free(pl_surround.databuf); pl_surround.databuf = NULL; | |
90 } | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
91 // Allocate output buffer |
3313 | 92 pl_surround.databuf = calloc(ao_plugin_data.len, 1); |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
93 // Return back smaller len so we don't get overflowed... |
3313 | 94 ao_plugin_data.len /= 2; |
95 return CONTROL_OK; | |
96 } | |
97 return -1; | |
98 } | |
99 | |
100 // open & setup audio device | |
101 // return: 1=success 0=fail | |
102 static int init(){ | |
103 | |
104 fprintf(stderr, "pl_surround: init input rate=%d, channels=%d\n", ao_plugin_data.rate, ao_plugin_data.channels); | |
105 if (ao_plugin_data.channels != 2) { | |
106 fprintf(stderr, "pl_surround: source audio must have 2 channels, using passthrough mode\n"); | |
107 pl_surround.passthrough = 1; | |
108 return 1; | |
109 } | |
110 if (ao_plugin_data.format != AFMT_S16_LE) { | |
111 fprintf(stderr, "pl_surround: I'm dumb and can only handle AFMT_S16_LE audio format, using passthrough mode\n"); | |
112 pl_surround.passthrough = 1; | |
113 return 1; | |
114 } | |
115 | |
116 pl_surround.passthrough = 0; | |
117 | |
118 /* Store info on input format to expect */ | |
119 pl_surround.rate=ao_plugin_data.rate; | |
120 pl_surround.format=ao_plugin_data.format; | |
121 pl_surround.input_channels=ao_plugin_data.channels; | |
122 | |
123 // Input 2 channels, output will be 4 - tell ao_plugin | |
124 ao_plugin_data.channels = 4; | |
125 ao_plugin_data.sz_mult /= 2; | |
126 | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
127 // Figure out buffer space (in int16_ts) needed for the 15msec delay |
3495
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
128 // Extra 31 samples allow for lowpass filter delay (taps-1) |
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
129 pl_surround.delaybuf_len = (pl_surround.rate * pl_surround.msecs / 1000) + 31; |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
130 // Allocate delay buffers |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
131 pl_surround.Ls_delaybuf=(void*)calloc(pl_surround.delaybuf_len,sizeof(int16_t)); |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
132 pl_surround.Rs_delaybuf=(void*)calloc(pl_surround.delaybuf_len,sizeof(int16_t)); |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
133 fprintf(stderr, "pl_surround: %dmsec surround delay, rate %d - buffers are %d bytes each\n", |
3420
cfc10bc948c4
split surround delay buf into Ls and Rs in prep for active decoding stuff, and fiddled a bit more with surround level
steve
parents:
3410
diff
changeset
|
134 pl_surround.msecs,pl_surround.rate, pl_surround.delaybuf_len*sizeof(int16_t)); |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
135 pl_surround.delaybuf_pos = 0; |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
136 // Surround filer coefficients |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
137 pl_surround.filter_coefs_surround = calc_coefficients_7kHz_lowpass(pl_surround.rate); |
3495
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
138 //dump_filter_coefficients(pl_surround.filter_coefs_surround); |
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
139 //testfilter(pl_surround.filter_coefs_surround, 32, pl_surround.rate); |
3313 | 140 return 1; |
141 } | |
142 | |
143 // close plugin | |
144 static void uninit(){ | |
145 // fprintf(stderr, "pl_surround: uninit called!\n"); | |
146 if (pl_surround.passthrough) return; | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
147 if(pl_surround.Ls_delaybuf) |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
148 free(pl_surround.Ls_delaybuf); |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
149 if(pl_surround.Rs_delaybuf) |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
150 free(pl_surround.Rs_delaybuf); |
3313 | 151 if(pl_surround.databuf) |
152 free(pl_surround.databuf); | |
153 pl_surround.delaybuf_len=0; | |
154 } | |
155 | |
156 // empty buffers | |
157 static void reset() | |
158 { | |
159 if (pl_surround.passthrough) return; | |
160 //fprintf(stderr, "pl_surround: reset called\n"); | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
161 pl_surround.delaybuf_pos = 0; |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
162 memset(pl_surround.Ls_delaybuf, 0, sizeof(int16_t)*pl_surround.delaybuf_len); |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
163 memset(pl_surround.Rs_delaybuf, 0, sizeof(int16_t)*pl_surround.delaybuf_len); |
3313 | 164 } |
165 | |
3681 | 166 // The beginnings of an active matrix... |
167 static double steering_matrix[][12] = { | |
168 // LL RL LR RR LS RS LLs RLs LRs RRs LC RC | |
169 {.707, .0, .0, .707, .5, -.5, .5878, -.3928, .3928, -.5878, .5, .5}, | |
170 }; | |
171 | |
172 // Experimental moving average dominances | |
173 static int amp_L = 0, amp_R = 0, amp_C = 0, amp_S = 0; | |
3313 | 174 |
175 // processes 'ao_plugin_data.len' bytes of 'data' | |
176 // called for every block of data | |
177 static int play(){ | |
178 int16_t *in, *out; | |
179 int i, samples; | |
3681 | 180 double *matrix = steering_matrix[0]; // later we'll index based on detected dominance |
3313 | 181 |
182 if (pl_surround.passthrough) return 1; | |
183 | |
3420
cfc10bc948c4
split surround delay buf into Ls and Rs in prep for active decoding stuff, and fiddled a bit more with surround level
steve
parents:
3410
diff
changeset
|
184 // fprintf(stderr, "pl_surround: play %d bytes, %d samples\n", ao_plugin_data.len, samples); |
3313 | 185 |
186 samples = ao_plugin_data.len / sizeof(int16_t) / pl_surround.input_channels; | |
3495
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
187 out = pl_surround.databuf; in = (int16_t *)ao_plugin_data.data; |
3313 | 188 |
3681 | 189 // Testing - place a 1kHz tone on Lt and Rt in anti-phase: should decode in S |
3495
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
190 //sinewave(in, samples, pl_surround.input_channels, 1000, 0.0, pl_surround.rate); |
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
191 //sinewave(&in[1], samples, pl_surround.input_channels, 1000, PI, pl_surround.rate); |
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
192 |
3313 | 193 for (i=0; i<samples; i++) { |
3373 | 194 |
3681 | 195 // Dominance: |
196 //abs(in[0]) abs(in[1]); | |
197 //abs(in[0]+in[1]) abs(in[0]-in[1]); | |
198 //10 * log( abs(in[0]) / (abs(in[1])|1) ); | |
199 //10 * log( abs(in[0]+in[1]) / (abs(in[0]-in[1])|1) ); | |
200 | |
3410 | 201 // About volume balancing... |
3373 | 202 // Surround encoding does the following: |
203 // Lt=L+.707*C+.707*S, Rt=R+.707*C-.707*S | |
3420
cfc10bc948c4
split surround delay buf into Ls and Rs in prep for active decoding stuff, and fiddled a bit more with surround level
steve
parents:
3410
diff
changeset
|
204 // So S should be extracted as: |
cfc10bc948c4
split surround delay buf into Ls and Rs in prep for active decoding stuff, and fiddled a bit more with surround level
steve
parents:
3410
diff
changeset
|
205 // (Lt-Rt) |
3410 | 206 // But we are splitting the S to two output channels, so we |
3420
cfc10bc948c4
split surround delay buf into Ls and Rs in prep for active decoding stuff, and fiddled a bit more with surround level
steve
parents:
3410
diff
changeset
|
207 // must take 3dB off as we split it: |
cfc10bc948c4
split surround delay buf into Ls and Rs in prep for active decoding stuff, and fiddled a bit more with surround level
steve
parents:
3410
diff
changeset
|
208 // Ls=Rs=.707*(Lt-Rt) |
cfc10bc948c4
split surround delay buf into Ls and Rs in prep for active decoding stuff, and fiddled a bit more with surround level
steve
parents:
3410
diff
changeset
|
209 // Trouble is, Lt could be +32767, Rt -32768, so possibility that S will |
3681 | 210 // overflow. So to avoid that, we cut L/R by 3dB (*.707), and S by 6dB (/2). |
211 // this keeps the overall balance, but guarantees no overflow. | |
3373 | 212 |
3420
cfc10bc948c4
split surround delay buf into Ls and Rs in prep for active decoding stuff, and fiddled a bit more with surround level
steve
parents:
3410
diff
changeset
|
213 // output front left and right |
3681 | 214 out[0] = matrix[0]*in[0] + matrix[1]*in[1]; |
215 out[1] = matrix[2]*in[0] + matrix[3]*in[1]; | |
216 // output Ls and Rs - from 20msec ago, lowpass filtered @ 7kHz | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
217 out[2] = firfilter(pl_surround.Ls_delaybuf, pl_surround.delaybuf_pos, |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
218 pl_surround.delaybuf_len, 32, pl_surround.filter_coefs_surround); |
3681 | 219 #ifdef SPLITREAR |
220 out[3] = firfilter(pl_surround.Rs_delaybuf, pl_surround.delaybuf_pos, | |
221 pl_surround.delaybuf_len, 32, pl_surround.filter_coefs_surround); | |
222 #else | |
223 out[3] = -out[2]; | |
224 #endif | |
225 // calculate and save surround for 20msecs time | |
226 #ifdef SPLITREAR | |
227 pl_surround.Ls_delaybuf[pl_surround.delaybuf_pos] = | |
228 matrix[6]*in[0] + matrix[7]*in[1]; | |
229 pl_surround.Rs_delaybuf[pl_surround.delaybuf_pos++] = | |
230 matrix[8]*in[0] + matrix[9]*in[1]; | |
231 #else | |
3718 | 232 pl_surround.Ls_delaybuf[pl_surround.delaybuf_pos++] = |
3681 | 233 matrix[4]*in[0] + matrix[5]*in[1]; |
234 #endif | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
235 pl_surround.delaybuf_pos %= pl_surround.delaybuf_len; |
3681 | 236 |
3313 | 237 // next samples... |
238 in = &in[pl_surround.input_channels]; out = &out[4]; | |
239 } | |
3681 | 240 |
241 // Show some state | |
242 //printf("\npl_surround: delaybuf_pos=%d, samples=%d\r\033[A", pl_surround.delaybuf_pos, samples); | |
3313 | 243 |
244 // Set output block/len | |
245 ao_plugin_data.data=pl_surround.databuf; | |
246 ao_plugin_data.len=samples*sizeof(int16_t)*4; | |
247 return 1; | |
248 } |