Mercurial > mplayer.hg
annotate libao2/pl_surround.c @ 3546:4e772a3c6b62
sse opt
author | michael |
---|---|
date | Mon, 17 Dec 2001 00:37:55 +0000 |
parents | cc1c879533ee |
children | 26126e5c3532 |
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 | |
24 from the front channels, delay by 15msec and feed to rear in anti-phase | |
25 www.dolby.com has the background | |
26 */ | |
27 | |
28 | |
29 #include <stdio.h> | |
30 #include <stdlib.h> | |
3317 | 31 #include <unistd.h> |
3313 | 32 |
33 #include "audio_out.h" | |
34 #include "audio_plugin.h" | |
35 #include "audio_plugin_internal.h" | |
36 #include "afmt.h" | |
37 | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
38 #include "remez.h" |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
39 #include "firfilter.c" |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
40 |
3313 | 41 static ao_info_t info = |
42 { | |
43 "Surround decoder plugin", | |
44 "surround", | |
45 "Steve Davies <steve@daviesfam.org>", | |
46 "" | |
47 }; | |
48 | |
49 LIBAO_PLUGIN_EXTERN(surround) | |
50 | |
51 // local data | |
52 typedef struct pl_surround_s | |
53 { | |
54 int passthrough; // Just be a "NO-OP" | |
55 int msecs; // Rear channel delay in milliseconds | |
56 int16_t* databuf; // Output audio buffer | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 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
|
61 double* filter_coefs_surround; // FIR filter coefficients for surround sound 7kHz lowpass |
3313 | 62 int rate; // input data rate |
63 int format; // input format | |
64 int input_channels; // input channels | |
65 | |
66 } pl_surround_t; | |
67 | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
68 static pl_surround_t pl_surround={0,15,NULL,NULL,NULL,0,0,NULL,0,0,0}; |
3313 | 69 |
70 // to set/get/query special features/parameters | |
71 static int control(int cmd,int arg){ | |
72 switch(cmd){ | |
73 case AOCONTROL_PLUGIN_SET_LEN: | |
74 if (pl_surround.passthrough) return CONTROL_OK; | |
75 //fprintf(stderr, "pl_surround: AOCONTROL_PLUGIN_SET_LEN with arg=%d\n", arg); | |
76 //fprintf(stderr, "pl_surround: ao_plugin_data.len=%d\n", ao_plugin_data.len); | |
77 // Allocate an output buffer | |
78 if (pl_surround.databuf != NULL) { | |
79 free(pl_surround.databuf); pl_surround.databuf = NULL; | |
80 } | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
81 // Allocate output buffer |
3313 | 82 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
|
83 // Return back smaller len so we don't get overflowed... |
3313 | 84 ao_plugin_data.len /= 2; |
85 return CONTROL_OK; | |
86 } | |
87 return -1; | |
88 } | |
89 | |
90 // open & setup audio device | |
91 // return: 1=success 0=fail | |
92 static int init(){ | |
93 | |
94 fprintf(stderr, "pl_surround: init input rate=%d, channels=%d\n", ao_plugin_data.rate, ao_plugin_data.channels); | |
95 if (ao_plugin_data.channels != 2) { | |
96 fprintf(stderr, "pl_surround: source audio must have 2 channels, using passthrough mode\n"); | |
97 pl_surround.passthrough = 1; | |
98 return 1; | |
99 } | |
100 if (ao_plugin_data.format != AFMT_S16_LE) { | |
101 fprintf(stderr, "pl_surround: I'm dumb and can only handle AFMT_S16_LE audio format, using passthrough mode\n"); | |
102 pl_surround.passthrough = 1; | |
103 return 1; | |
104 } | |
105 | |
106 pl_surround.passthrough = 0; | |
107 | |
108 /* Store info on input format to expect */ | |
109 pl_surround.rate=ao_plugin_data.rate; | |
110 pl_surround.format=ao_plugin_data.format; | |
111 pl_surround.input_channels=ao_plugin_data.channels; | |
112 | |
113 // Input 2 channels, output will be 4 - tell ao_plugin | |
114 ao_plugin_data.channels = 4; | |
115 ao_plugin_data.sz_mult /= 2; | |
116 | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
117 // 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
|
118 // 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
|
119 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
|
120 // Allocate delay buffers |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
121 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
|
122 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
|
123 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
|
124 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
|
125 pl_surround.delaybuf_pos = 0; |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
126 // Surround filer coefficients |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
127 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
|
128 //dump_filter_coefficients(pl_surround.filter_coefs_surround); |
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
129 //testfilter(pl_surround.filter_coefs_surround, 32, pl_surround.rate); |
3313 | 130 return 1; |
131 } | |
132 | |
133 // close plugin | |
134 static void uninit(){ | |
135 // fprintf(stderr, "pl_surround: uninit called!\n"); | |
136 if (pl_surround.passthrough) return; | |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
137 if(pl_surround.Ls_delaybuf) |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
138 free(pl_surround.Ls_delaybuf); |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
139 if(pl_surround.Rs_delaybuf) |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
140 free(pl_surround.Rs_delaybuf); |
3313 | 141 if(pl_surround.databuf) |
142 free(pl_surround.databuf); | |
143 pl_surround.delaybuf_len=0; | |
144 } | |
145 | |
146 // empty buffers | |
147 static void reset() | |
148 { | |
149 if (pl_surround.passthrough) return; | |
150 //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
|
151 pl_surround.delaybuf_pos = 0; |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
152 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
|
153 memset(pl_surround.Rs_delaybuf, 0, sizeof(int16_t)*pl_surround.delaybuf_len); |
3313 | 154 } |
155 | |
156 | |
157 // processes 'ao_plugin_data.len' bytes of 'data' | |
158 // called for every block of data | |
159 static int play(){ | |
160 int16_t *in, *out; | |
161 int i, samples; | |
162 int surround; | |
163 | |
164 if (pl_surround.passthrough) return 1; | |
165 | |
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
|
166 // fprintf(stderr, "pl_surround: play %d bytes, %d samples\n", ao_plugin_data.len, samples); |
3313 | 167 |
168 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
|
169 out = pl_surround.databuf; in = (int16_t *)ao_plugin_data.data; |
3313 | 170 |
3495
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
171 // Testing - place a 1kHz tone in the front channels in anti-phase |
cc1c879533ee
tweaked surround lowpass filter, included some new test code
steve
parents:
3485
diff
changeset
|
172 //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
|
173 //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
|
174 |
3313 | 175 for (i=0; i<samples; i++) { |
3373 | 176 |
3410 | 177 // About volume balancing... |
3373 | 178 // Surround encoding does the following: |
179 // 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
|
180 // 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
|
181 // (Lt-Rt) |
3410 | 182 // 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
|
183 // 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
|
184 // 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
|
185 // Trouble is, Lt could be +32767, Rt -32768, so possibility that S will |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
186 // clip. So to avoid that, we cut L/R by 3dB (*.707), and S by 6dB (/2). |
3373 | 187 |
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
|
188 // output front left and right |
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
|
189 out[0] = in[0]*.707; |
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
|
190 out[1] = in[1]*.707; |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
191 // output Ls and Rs - from 15msec ago, lowpass filtered @ 7kHz |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
192 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
|
193 pl_surround.delaybuf_len, 32, pl_surround.filter_coefs_surround); |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
194 out[3] = - out[2]; |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
195 // out[3] = firfilter(pl_surround.Rs_delaybuf, pl_surround.delaybuf_pos, |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
196 // pl_surround.delaybuf_len, 32, pl_surround.filter_coefs_surround); |
3313 | 197 // calculate and save surround for 15msecs time |
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
|
198 surround = (in[0]/2 - in[1]/2); |
3485
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
199 pl_surround.Ls_delaybuf[pl_surround.delaybuf_pos] = surround; |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
200 pl_surround.Rs_delaybuf[pl_surround.delaybuf_pos++] = - surround; |
439b0b1d50b9
include 7kHz lowpass filter for surround channels, as per Dolby recommendation
steve
parents:
3420
diff
changeset
|
201 pl_surround.delaybuf_pos %= pl_surround.delaybuf_len; |
3313 | 202 // next samples... |
203 in = &in[pl_surround.input_channels]; out = &out[4]; | |
204 } | |
205 | |
206 // Set output block/len | |
207 ao_plugin_data.data=pl_surround.databuf; | |
208 ao_plugin_data.len=samples*sizeof(int16_t)*4; | |
209 return 1; | |
210 } |