Mercurial > mplayer.hg
annotate libao2/pl_surround.c @ 3420:cfc10bc948c4
split surround delay buf into Ls and Rs in prep for active decoding stuff, and fiddled a bit more with surround level
author | steve |
---|---|
date | Mon, 10 Dec 2001 00:10:47 +0000 |
parents | 3eb15016e454 |
children | 439b0b1d50b9 |
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 | |
38 static ao_info_t info = | |
39 { | |
40 "Surround decoder plugin", | |
41 "surround", | |
42 "Steve Davies <steve@daviesfam.org>", | |
43 "" | |
44 }; | |
45 | |
46 LIBAO_PLUGIN_EXTERN(surround) | |
47 | |
48 // local data | |
49 typedef struct pl_surround_s | |
50 { | |
51 int passthrough; // Just be a "NO-OP" | |
52 int msecs; // Rear channel delay in milliseconds | |
53 int16_t* databuf; // Output audio buffer | |
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
|
54 int16_t* delaybuf; // circular buffer to be used for delaying Ls and Rs audio |
3313 | 55 int delaybuf_len; // local buffer length in samples |
56 int delaybuf_ptr; // offset in buffer where we are reading/writing | |
57 int rate; // input data rate | |
58 int format; // input format | |
59 int input_channels; // input channels | |
60 | |
61 } pl_surround_t; | |
62 | |
63 static pl_surround_t pl_surround={0,15,NULL,NULL,0,0,0,0,0}; | |
64 | |
65 // to set/get/query special features/parameters | |
66 static int control(int cmd,int arg){ | |
67 switch(cmd){ | |
68 case AOCONTROL_PLUGIN_SET_LEN: | |
69 if (pl_surround.passthrough) return CONTROL_OK; | |
70 //fprintf(stderr, "pl_surround: AOCONTROL_PLUGIN_SET_LEN with arg=%d\n", arg); | |
71 //fprintf(stderr, "pl_surround: ao_plugin_data.len=%d\n", ao_plugin_data.len); | |
72 // Allocate an output buffer | |
73 if (pl_surround.databuf != NULL) { | |
74 free(pl_surround.databuf); pl_surround.databuf = NULL; | |
75 } | |
76 pl_surround.databuf = calloc(ao_plugin_data.len, 1); | |
77 // Return back smaller len so we don't get overflowed... (??seems the right thing to do?) | |
78 ao_plugin_data.len /= 2; | |
79 return CONTROL_OK; | |
80 } | |
81 return -1; | |
82 } | |
83 | |
84 // open & setup audio device | |
85 // return: 1=success 0=fail | |
86 static int init(){ | |
87 | |
88 fprintf(stderr, "pl_surround: init input rate=%d, channels=%d\n", ao_plugin_data.rate, ao_plugin_data.channels); | |
89 if (ao_plugin_data.channels != 2) { | |
90 fprintf(stderr, "pl_surround: source audio must have 2 channels, using passthrough mode\n"); | |
91 pl_surround.passthrough = 1; | |
92 return 1; | |
93 } | |
94 if (ao_plugin_data.format != AFMT_S16_LE) { | |
95 fprintf(stderr, "pl_surround: I'm dumb and can only handle AFMT_S16_LE audio format, using passthrough mode\n"); | |
96 pl_surround.passthrough = 1; | |
97 return 1; | |
98 } | |
99 | |
100 pl_surround.passthrough = 0; | |
101 | |
102 /* Store info on input format to expect */ | |
103 pl_surround.rate=ao_plugin_data.rate; | |
104 pl_surround.format=ao_plugin_data.format; | |
105 pl_surround.input_channels=ao_plugin_data.channels; | |
106 | |
107 // Input 2 channels, output will be 4 - tell ao_plugin | |
108 ao_plugin_data.channels = 4; | |
109 ao_plugin_data.sz_mult /= 2; | |
110 | |
111 // Figure out buffer space needed for the 15msec delay | |
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
|
112 pl_surround.delaybuf_len = 2 * (pl_surround.rate * pl_surround.msecs / 1000); |
3313 | 113 // Allocate delay buffer |
114 pl_surround.delaybuf=(void*)calloc(pl_surround.delaybuf_len,sizeof(int16_t)); | |
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
|
115 fprintf(stderr, "pl_surround: %dmsec surround delay, rate %d - buffer is %d bytes\n", |
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
|
116 pl_surround.msecs,pl_surround.rate, pl_surround.delaybuf_len*sizeof(int16_t)); |
3313 | 117 pl_surround.delaybuf_ptr = 0; |
118 | |
119 return 1; | |
120 } | |
121 | |
122 // close plugin | |
123 static void uninit(){ | |
124 // fprintf(stderr, "pl_surround: uninit called!\n"); | |
125 if (pl_surround.passthrough) return; | |
126 if(pl_surround.delaybuf) | |
127 free(pl_surround.delaybuf); | |
128 if(pl_surround.databuf) | |
129 free(pl_surround.databuf); | |
130 pl_surround.delaybuf_len=0; | |
131 } | |
132 | |
133 // empty buffers | |
134 static void reset() | |
135 { | |
136 if (pl_surround.passthrough) return; | |
137 //fprintf(stderr, "pl_surround: reset called\n"); | |
138 pl_surround.delaybuf_ptr = 0; | |
139 memset(pl_surround.delaybuf, 0, sizeof(int16_t)*pl_surround.delaybuf_len); | |
140 } | |
141 | |
142 | |
143 // processes 'ao_plugin_data.len' bytes of 'data' | |
144 // called for every block of data | |
145 static int play(){ | |
146 int16_t *in, *out; | |
147 int i, samples; | |
148 int surround; | |
149 | |
150 if (pl_surround.passthrough) return 1; | |
151 | |
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
|
152 // fprintf(stderr, "pl_surround: play %d bytes, %d samples\n", ao_plugin_data.len, samples); |
3313 | 153 |
154 samples = ao_plugin_data.len / sizeof(int16_t) / pl_surround.input_channels; | |
155 | |
156 out = pl_surround.databuf; in = (int16_t *)ao_plugin_data.data; | |
157 for (i=0; i<samples; i++) { | |
3373 | 158 |
3410 | 159 // About volume balancing... |
3373 | 160 // Surround encoding does the following: |
161 // 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
|
162 // 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
|
163 // (Lt-Rt) |
3410 | 164 // 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
|
165 // 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
|
166 // 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
|
167 // Trouble is, Lt could be +32767, Rt -32768, so possibility that S will |
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
|
168 // clip. So to compensate, we cut L/R by 3dB (*.707), and S by 6dB (/2). |
3373 | 169 |
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
|
170 // 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
|
171 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
|
172 out[1] = in[1]*.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
|
173 // output Ls and Rs - from 15msec ago |
3313 | 174 out[2] = pl_surround.delaybuf[pl_surround.delaybuf_ptr]; |
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
|
175 out[3] = pl_surround.delaybuf[pl_surround.delaybuf_ptr+1]; |
3313 | 176 // 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
|
177 surround = (in[0]/2 - in[1]/2); |
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
|
178 pl_surround.delaybuf[pl_surround.delaybuf_ptr++] = surround; |
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
|
179 pl_surround.delaybuf[pl_surround.delaybuf_ptr++] = - surround; |
3313 | 180 pl_surround.delaybuf_ptr %= pl_surround.delaybuf_len; |
181 // next samples... | |
182 in = &in[pl_surround.input_channels]; out = &out[4]; | |
183 } | |
184 | |
185 // Set output block/len | |
186 ao_plugin_data.data=pl_surround.databuf; | |
187 ao_plugin_data.len=samples*sizeof(int16_t)*4; | |
188 return 1; | |
189 } |