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>
|
|
31
|
|
32 #include "audio_out.h"
|
|
33 #include "audio_plugin.h"
|
|
34 #include "audio_plugin_internal.h"
|
|
35 #include "afmt.h"
|
|
36
|
|
37 static ao_info_t info =
|
|
38 {
|
|
39 "Surround decoder plugin",
|
|
40 "surround",
|
|
41 "Steve Davies <steve@daviesfam.org>",
|
|
42 ""
|
|
43 };
|
|
44
|
|
45 LIBAO_PLUGIN_EXTERN(surround)
|
|
46
|
|
47 // local data
|
|
48 typedef struct pl_surround_s
|
|
49 {
|
|
50 int passthrough; // Just be a "NO-OP"
|
|
51 int msecs; // Rear channel delay in milliseconds
|
|
52 int16_t* databuf; // Output audio buffer
|
|
53 int16_t* delaybuf; // circular buffer to be used for delaying audio signal
|
|
54 int delaybuf_len; // local buffer length in samples
|
|
55 int delaybuf_ptr; // offset in buffer where we are reading/writing
|
|
56 int rate; // input data rate
|
|
57 int format; // input format
|
|
58 int input_channels; // input channels
|
|
59
|
|
60 } pl_surround_t;
|
|
61
|
|
62 static pl_surround_t pl_surround={0,15,NULL,NULL,0,0,0,0,0};
|
|
63
|
|
64 // to set/get/query special features/parameters
|
|
65 static int control(int cmd,int arg){
|
|
66 switch(cmd){
|
|
67 case AOCONTROL_PLUGIN_SET_LEN:
|
|
68 if (pl_surround.passthrough) return CONTROL_OK;
|
|
69 //fprintf(stderr, "pl_surround: AOCONTROL_PLUGIN_SET_LEN with arg=%d\n", arg);
|
|
70 //fprintf(stderr, "pl_surround: ao_plugin_data.len=%d\n", ao_plugin_data.len);
|
|
71 // Allocate an output buffer
|
|
72 if (pl_surround.databuf != NULL) {
|
|
73 free(pl_surround.databuf); pl_surround.databuf = NULL;
|
|
74 }
|
|
75 pl_surround.databuf = calloc(ao_plugin_data.len, 1);
|
|
76 // Return back smaller len so we don't get overflowed... (??seems the right thing to do?)
|
|
77 ao_plugin_data.len /= 2;
|
|
78 return CONTROL_OK;
|
|
79 }
|
|
80 return -1;
|
|
81 }
|
|
82
|
|
83 // open & setup audio device
|
|
84 // return: 1=success 0=fail
|
|
85 static int init(){
|
|
86
|
|
87 fprintf(stderr, "pl_surround: init input rate=%d, channels=%d\n", ao_plugin_data.rate, ao_plugin_data.channels);
|
|
88 if (ao_plugin_data.channels != 2) {
|
|
89 fprintf(stderr, "pl_surround: source audio must have 2 channels, using passthrough mode\n");
|
|
90 pl_surround.passthrough = 1;
|
|
91 return 1;
|
|
92 }
|
|
93 if (ao_plugin_data.format != AFMT_S16_LE) {
|
|
94 fprintf(stderr, "pl_surround: I'm dumb and can only handle AFMT_S16_LE audio format, using passthrough mode\n");
|
|
95 pl_surround.passthrough = 1;
|
|
96 return 1;
|
|
97 }
|
|
98
|
|
99 pl_surround.passthrough = 0;
|
|
100
|
|
101 /* Store info on input format to expect */
|
|
102 pl_surround.rate=ao_plugin_data.rate;
|
|
103 pl_surround.format=ao_plugin_data.format;
|
|
104 pl_surround.input_channels=ao_plugin_data.channels;
|
|
105
|
|
106 // Input 2 channels, output will be 4 - tell ao_plugin
|
|
107 ao_plugin_data.channels = 4;
|
|
108 ao_plugin_data.sz_mult /= 2;
|
|
109
|
|
110 // Figure out buffer space needed for the 15msec delay
|
|
111 pl_surround.delaybuf_len = pl_surround.rate * pl_surround.msecs / 1000;
|
|
112 // Allocate delay buffer
|
|
113 pl_surround.delaybuf=(void*)calloc(pl_surround.delaybuf_len,sizeof(int16_t));
|
|
114 fprintf(stderr, "pl_surround: %dmsec surround delay, rate %d - buffer is %d samples\n",
|
|
115 pl_surround.msecs,pl_surround.rate, pl_surround.delaybuf_len);
|
|
116 pl_surround.delaybuf_ptr = 0;
|
|
117
|
|
118 return 1;
|
|
119 }
|
|
120
|
|
121 // close plugin
|
|
122 static void uninit(){
|
|
123 // fprintf(stderr, "pl_surround: uninit called!\n");
|
|
124 if (pl_surround.passthrough) return;
|
|
125 if(pl_surround.delaybuf)
|
|
126 free(pl_surround.delaybuf);
|
|
127 if(pl_surround.databuf)
|
|
128 free(pl_surround.databuf);
|
|
129 pl_surround.delaybuf_len=0;
|
|
130 }
|
|
131
|
|
132 // empty buffers
|
|
133 static void reset()
|
|
134 {
|
|
135 if (pl_surround.passthrough) return;
|
|
136 //fprintf(stderr, "pl_surround: reset called\n");
|
|
137 pl_surround.delaybuf_ptr = 0;
|
|
138 memset(pl_surround.delaybuf, 0, sizeof(int16_t)*pl_surround.delaybuf_len);
|
|
139 }
|
|
140
|
|
141
|
|
142 // processes 'ao_plugin_data.len' bytes of 'data'
|
|
143 // called for every block of data
|
|
144 static int play(){
|
|
145 int16_t *in, *out;
|
|
146 int i, samples;
|
|
147 int surround;
|
|
148
|
|
149 if (pl_surround.passthrough) return 1;
|
|
150
|
|
151 // fprintf(stderr, "pl_surround: play %d bytes, %d samples\n", ao_plugin_data.len, samples);
|
|
152
|
|
153 samples = ao_plugin_data.len / sizeof(int16_t) / pl_surround.input_channels;
|
|
154
|
|
155 out = pl_surround.databuf; in = (int16_t *)ao_plugin_data.data;
|
|
156 for (i=0; i<samples; i++) {
|
|
157 // front left and right
|
|
158 out[0] = in[0];
|
|
159 out[1] = in[1];
|
|
160 // surround - from 15msec ago
|
|
161 out[2] = pl_surround.delaybuf[pl_surround.delaybuf_ptr];
|
|
162 out[3] = -out[2];
|
|
163 // calculate and save surround for 15msecs time
|
|
164 pl_surround.delaybuf[pl_surround.delaybuf_ptr++] = (in[0]/2 - in[1]/2);
|
|
165 pl_surround.delaybuf_ptr %= pl_surround.delaybuf_len;
|
|
166 // next samples...
|
|
167 in = &in[pl_surround.input_channels]; out = &out[4];
|
|
168 }
|
|
169
|
|
170 // Set output block/len
|
|
171 ao_plugin_data.data=pl_surround.databuf;
|
|
172 ao_plugin_data.len=samples*sizeof(int16_t)*4;
|
|
173 return 1;
|
|
174 }
|
|
175
|
|
176
|
|
177
|