annotate libao2/pl_surround.c @ 3394:35b18ed357c2

imported from liba52 CVS
author arpi
date Sun, 09 Dec 2001 15:28:44 +0000
parents e5b881ac961e
children 3eb15016e454
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3313
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
1 /*
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
2 This is an ao2 plugin to do simple decoding of matrixed surround
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
3 sound. This will provide a (basic) surround-sound effect from
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
4 audio encoded for Dolby Surround, Pro Logic etc.
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
5
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
9 * (at your option) any later version.
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
10 *
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful,
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
14 * GNU General Public License for more details.
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
15 *
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
19
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
20 Original author: Steve Davies <steve@daviesfam.org>
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
21 */
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
22
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
23 /* The principle: Make rear channels by extracting anti-phase data
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
24 from the front channels, delay by 15msec and feed to rear in anti-phase
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
25 www.dolby.com has the background
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
26 */
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
27
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
28
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
29 #include <stdio.h>
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
30 #include <stdlib.h>
3317
614b4525d275 unistd.h required at least by FreeBSD
nexus
parents: 3313
diff changeset
31 #include <unistd.h>
3313
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
32
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
33 #include "audio_out.h"
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
34 #include "audio_plugin.h"
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
35 #include "audio_plugin_internal.h"
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
36 #include "afmt.h"
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
37
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
38 static ao_info_t info =
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
39 {
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
40 "Surround decoder plugin",
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
41 "surround",
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
42 "Steve Davies <steve@daviesfam.org>",
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
43 ""
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
44 };
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
45
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
46 LIBAO_PLUGIN_EXTERN(surround)
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
47
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
48 // local data
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
49 typedef struct pl_surround_s
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
50 {
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
51 int passthrough; // Just be a "NO-OP"
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
52 int msecs; // Rear channel delay in milliseconds
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
53 int16_t* databuf; // Output audio buffer
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
54 int16_t* delaybuf; // circular buffer to be used for delaying audio signal
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
55 int delaybuf_len; // local buffer length in samples
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
56 int delaybuf_ptr; // offset in buffer where we are reading/writing
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
57 int rate; // input data rate
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
58 int format; // input format
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
59 int input_channels; // input channels
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
60
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
61 } pl_surround_t;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
62
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
63 static pl_surround_t pl_surround={0,15,NULL,NULL,0,0,0,0,0};
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
64
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
65 // to set/get/query special features/parameters
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
66 static int control(int cmd,int arg){
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
67 switch(cmd){
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
68 case AOCONTROL_PLUGIN_SET_LEN:
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
69 if (pl_surround.passthrough) return CONTROL_OK;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
70 //fprintf(stderr, "pl_surround: AOCONTROL_PLUGIN_SET_LEN with arg=%d\n", arg);
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
71 //fprintf(stderr, "pl_surround: ao_plugin_data.len=%d\n", ao_plugin_data.len);
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
72 // Allocate an output buffer
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
73 if (pl_surround.databuf != NULL) {
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
74 free(pl_surround.databuf); pl_surround.databuf = NULL;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
75 }
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
76 pl_surround.databuf = calloc(ao_plugin_data.len, 1);
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
77 // Return back smaller len so we don't get overflowed... (??seems the right thing to do?)
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
78 ao_plugin_data.len /= 2;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
79 return CONTROL_OK;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
80 }
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
81 return -1;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
82 }
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
83
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
84 // open & setup audio device
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
85 // return: 1=success 0=fail
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
86 static int init(){
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
87
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
88 fprintf(stderr, "pl_surround: init input rate=%d, channels=%d\n", ao_plugin_data.rate, ao_plugin_data.channels);
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
89 if (ao_plugin_data.channels != 2) {
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
90 fprintf(stderr, "pl_surround: source audio must have 2 channels, using passthrough mode\n");
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
91 pl_surround.passthrough = 1;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
92 return 1;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
93 }
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
94 if (ao_plugin_data.format != AFMT_S16_LE) {
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
95 fprintf(stderr, "pl_surround: I'm dumb and can only handle AFMT_S16_LE audio format, using passthrough mode\n");
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
96 pl_surround.passthrough = 1;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
97 return 1;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
98 }
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
99
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
100 pl_surround.passthrough = 0;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
101
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
102 /* Store info on input format to expect */
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
103 pl_surround.rate=ao_plugin_data.rate;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
104 pl_surround.format=ao_plugin_data.format;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
105 pl_surround.input_channels=ao_plugin_data.channels;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
106
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
107 // Input 2 channels, output will be 4 - tell ao_plugin
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
108 ao_plugin_data.channels = 4;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
109 ao_plugin_data.sz_mult /= 2;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
110
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
111 // Figure out buffer space needed for the 15msec delay
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
112 pl_surround.delaybuf_len = pl_surround.rate * pl_surround.msecs / 1000;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
113 // Allocate delay buffer
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
114 pl_surround.delaybuf=(void*)calloc(pl_surround.delaybuf_len,sizeof(int16_t));
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
115 fprintf(stderr, "pl_surround: %dmsec surround delay, rate %d - buffer is %d samples\n",
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
116 pl_surround.msecs,pl_surround.rate, pl_surround.delaybuf_len);
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
117 pl_surround.delaybuf_ptr = 0;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
118
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
119 return 1;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
120 }
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
121
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
122 // close plugin
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
123 static void uninit(){
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
124 // fprintf(stderr, "pl_surround: uninit called!\n");
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
125 if (pl_surround.passthrough) return;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
126 if(pl_surround.delaybuf)
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
127 free(pl_surround.delaybuf);
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
128 if(pl_surround.databuf)
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
129 free(pl_surround.databuf);
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
130 pl_surround.delaybuf_len=0;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
131 }
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
132
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
133 // empty buffers
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
134 static void reset()
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
135 {
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
136 if (pl_surround.passthrough) return;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
137 //fprintf(stderr, "pl_surround: reset called\n");
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
138 pl_surround.delaybuf_ptr = 0;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
139 memset(pl_surround.delaybuf, 0, sizeof(int16_t)*pl_surround.delaybuf_len);
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
140 }
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
141
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
142
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
143 // processes 'ao_plugin_data.len' bytes of 'data'
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
144 // called for every block of data
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
145 static int play(){
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
146 int16_t *in, *out;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
147 int i, samples;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
148 int surround;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
149
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
150 if (pl_surround.passthrough) return 1;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
151
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
152 // fprintf(stderr, "pl_surround: play %d bytes, %d samples\n", ao_plugin_data.len, samples);
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
153
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
154 samples = ao_plugin_data.len / sizeof(int16_t) / pl_surround.input_channels;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
155
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
156 out = pl_surround.databuf; in = (int16_t *)ao_plugin_data.data;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
157 for (i=0; i<samples; i++) {
3373
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
158
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
159 // About the .707 here and the /2 for surround:
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
160 // Surround encoding does the following:
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
161 // Lt=L+.707*C+.707*S, Rt=R+.707*C-.707*S
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
162 // So S needs to be extracted as:
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
163 // .707*(L-R)
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
164 // But L-R could still be as much as 32767-(-32768), way off scale
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
165 // for signed 16 bits, so to avoid running out of bits, whilst still
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
166 // keeping levels in balance, we scale L and R down by 3dB (*.707),
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
167 // and scale the surround down by 6dB (.707*.707=.5)
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
168
3313
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
169 // front left and right
3373
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
170 out[0] = in[0]*.707;
e5b881ac961e Corrected front:surround levels
steve
parents: 3317
diff changeset
171 out[1] = in[1]*.707;
3313
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
172 // surround - from 15msec ago
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
173 out[2] = pl_surround.delaybuf[pl_surround.delaybuf_ptr];
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
174 out[3] = -out[2];
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
175 // calculate and save surround for 15msecs time
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
176 pl_surround.delaybuf[pl_surround.delaybuf_ptr++] = (in[0]/2 - in[1]/2);
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
177 pl_surround.delaybuf_ptr %= pl_surround.delaybuf_len;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
178 // next samples...
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
179 in = &in[pl_surround.input_channels]; out = &out[4];
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
180 }
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
181
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
182 // Set output block/len
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
183 ao_plugin_data.data=pl_surround.databuf;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
184 ao_plugin_data.len=samples*sizeof(int16_t)*4;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
185 return 1;
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
186 }
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
187
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
188
76a3421bc421 Dolby Surround decoding audio plugin
steve
parents:
diff changeset
189