Jack2: JackAC3Encoder.cpp Source File

Jack2 1.9.12

JackAC3Encoder.cpp
1 /*
2 Copyright (C) 2006 Jesse Chappell <jesse@essej.net> (AC3Jack)
3 Copyright (C) 2012 Grame
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "JackAC3Encoder.h"
22 #include "JackError.h"
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdio.h>
26
27 #define max(x,y) (((x)>(y)) ? (x) : (y))
28 #define min(x,y) (((x)<(y)) ? (x) : (y))
29
30 namespace Jack
31 {
32
33 #ifndef __ppc__
34
35 JackAC3Encoder::JackAC3Encoder(const JackAC3EncoderParams& params)
36 {
37 aften_set_defaults(&fAftenContext);
38
39 fAftenContext.channels = params.channels;
40 fAftenContext.samplerate = params.sample_rate;
41 fAftenContext.params.bitrate = params.bitrate;
42
43 int acmod = A52_ACMOD_MONO;
44 int lfe = params.lfe;
45
46 switch (params.channels) {
47
48 case 1: acmod = A52_ACMOD_MONO; break;
49 case 2: acmod = A52_ACMOD_STEREO; break;
50 case 3: acmod = A52_ACMOD_3_0; break;
51 case 4: acmod = A52_ACMOD_2_2; break;
52 case 5: acmod = A52_ACMOD_3_2; break;
53 break;
54
55 default:
56 break;
57 }
58
59 if (lfe) {
60 fAftenContext.channels += 1;
61 }
62
63 fAftenContext.acmod = acmod;
64 fAftenContext.lfe = lfe;
65 fAftenContext.sample_format = A52_SAMPLE_FMT_FLT;
66 fAftenContext.verbose = 1;
67
68 fAftenContext.system.n_threads = 1;
69
70 // create interleaved framebuffer for MAX_AC3_CHANNELS
71 fSampleBuffer = new float[MAX_AC3_CHANNELS * A52_SAMPLES_PER_FRAME];
72
73 // create AC3 buffer
74 fAC3Buffer = new unsigned char[A52_MAX_CODED_FRAME_SIZE];
75 memset(fAC3Buffer, 0, A52_MAX_CODED_FRAME_SIZE);
76
77 fZeroBuffer = new unsigned char[SPDIF_FRAME_SIZE];
78 memset(fZeroBuffer, 0, SPDIF_FRAME_SIZE);
79
80 fRingBuffer = jack_ringbuffer_create(32768);
81
82 fOutSizeByte = 0;
83 fFramePos = 0;
84
85 fSampleRate = 0;
86 fByteRate = 0;
87 }
88
89 bool JackAC3Encoder::Init(jack_nframes_t sample_rate)
90 {
91 fSampleRate = sample_rate;
92 fByteRate = fSampleRate * sizeof(short) * 2;
93 return (aften_encode_init(&fAftenContext) == 0);
94 }
95
96 JackAC3Encoder::~JackAC3Encoder()
97 {
98 aften_encode_close(&fAftenContext);
99
100 delete [] fSampleBuffer;
101 delete [] fAC3Buffer;
102 delete [] fZeroBuffer;
103
104 if (fRingBuffer) {
105 jack_ringbuffer_free(fRingBuffer);
106 }
107 }
108
109 void JackAC3Encoder::Process(float** inputs_buffer, float** outputs_buffer, int nframes)
110 {
111 // fill and process frame buffers as appropriate
112 jack_nframes_t frames_left = A52_SAMPLES_PER_FRAME - fFramePos;
113 jack_nframes_t offset = 0;
114
115 while (offset < nframes)
116 {
117 if ((nframes - offset) >= frames_left) {
118
119 // copy only frames_left more data
120 jack_nframes_t pos = fFramePos * fAftenContext.channels;
121 for (jack_nframes_t spos = offset; spos < offset + frames_left; ++spos) {
122 for (size_t i = 0; i < fAftenContext.channels; ++i) {
123 fSampleBuffer[pos + i] = inputs_buffer[i][spos];
124 }
125 pos += fAftenContext.channels;
126 }
127
128 // use interleaved version
129 int res = aften_encode_frame(&fAftenContext, fAC3Buffer + SPDIF_HEADER_SIZE, fSampleBuffer);
130 if (res < 0) {
131 jack_error("aften_encode_frame error !!");
132 return;
133 }
134
135 fOutSizeByte = res;
136
137 FillSpdifHeader(fAC3Buffer, fOutSizeByte + SPDIF_HEADER_SIZE);
138
139 // push AC3 output to SPDIF ring buffer
140 float calc_ac3byterate = (fOutSizeByte * fSampleRate / (float) A52_SAMPLES_PER_FRAME);
141 jack_nframes_t silencebytes = (jack_nframes_t) (fOutSizeByte * (fByteRate / calc_ac3byterate)) - fOutSizeByte - SPDIF_HEADER_SIZE;
142
143 jack_ringbuffer_write(fRingBuffer, (const char *)fAC3Buffer, fOutSizeByte + SPDIF_HEADER_SIZE);
144
145 // write the proper remainder of zero padding (inefficient, should be memsetting)
146 jack_ringbuffer_write(fRingBuffer, (const char *)fZeroBuffer, silencebytes);
147
148 offset += frames_left;
149 frames_left = A52_SAMPLES_PER_FRAME;
150 fFramePos = 0;
151
152 } else {
153
154 // copy incoming data into frame buffers without processing
155 jack_nframes_t pos = fFramePos * fAftenContext.channels;
156 for (jack_nframes_t spos = offset; spos < nframes; ++spos) {
157 for (size_t i = 0; i < fAftenContext.channels; ++i) {
158 fSampleBuffer[pos + i] = inputs_buffer[i][spos];
159 }
160 pos += fAftenContext.channels;
161 }
162
163 fFramePos += (nframes - offset);
164 offset += (nframes-offset);
165 }
166 }
167
168 Output2Driver(outputs_buffer, nframes);
169 }
170
171 void JackAC3Encoder::FillSpdifHeader(unsigned char* buf, int outsize)
172 {
173 // todo, use outsize and not assume the fixed frame size?
174 int ac3outsize = outsize - SPDIF_HEADER_SIZE;
175
176 buf[0] = 0x72; buf[1] = 0xf8; /* spdif syncword */
177 buf[2] = 0x1f; buf[3] = 0x4e; /* .............. */
178 buf[4] = 0x01; /* AC3 data */
179 buf[5] = buf[13] & 7; /* bsmod, stream = 0 */
180 buf[6] = (ac3outsize << 3) & 0xff;
181 buf[7] = (ac3outsize >> 5) & 0xff;
182
183 #if !IS_BIGENDIAN
184 swab(buf+SPDIF_HEADER_SIZE, buf + SPDIF_HEADER_SIZE, ac3outsize);
185 #endif
186 }
187
188 int JackAC3Encoder::Output2Driver(float** outputs, jack_nframes_t nframes)
189 {
190 int wrotebytes = 0;
191 jack_nframes_t nframes_left = nframes;
192
193 if (jack_ringbuffer_read_space(fRingBuffer) == 0) {
194
195 // just write silence
196 memset(outputs[0], 0, nframes * sizeof(jack_default_audio_sample_t));
197 memset(outputs[1], 0, nframes * sizeof(jack_default_audio_sample_t));
198
199 } else {
200
201 jack_ringbuffer_data_t rb_data[2];
202
203 jack_ringbuffer_get_read_vector(fRingBuffer, rb_data);
204
205 while (nframes_left > 0 && rb_data[0].len > 4) {
206
207 jack_nframes_t towrite_frames = (rb_data[0].len) / (sizeof(short) * 2);
208 towrite_frames = min(towrite_frames, nframes_left);
209
210 // write and deinterleave into the two channels
211 #if 1
212 sample_move_dS_s16(outputs[0] + (nframes - nframes_left), (char *) rb_data[0].buf, towrite_frames, sizeof(short) * 2);
213 sample_move_dS_s16(outputs[1] + (nframes - nframes_left), (char *) rb_data[0].buf + sizeof(short), towrite_frames, sizeof(short) * 2);
214 #else
215 sample_move_dS_s16_24ph(outputs[0] + (nframes - nframes_left), (char *) rb_data[0].buf, towrite_frames, sizeof(short) * 2);
216 sample_move_dS_s16_24ph(outputs[1] + (nframes - nframes_left), (char *) rb_data[0].buf + sizeof(short), towrite_frames, sizeof(short) * 2);
217 #endif
218 wrotebytes = towrite_frames * sizeof(short) * 2;
219 nframes_left -= towrite_frames;
220
221 jack_ringbuffer_read_advance(fRingBuffer, wrotebytes);
222 jack_ringbuffer_get_read_vector(fRingBuffer, rb_data);
223 }
224
225 if (nframes_left > 0) {
226 // write silence
227 memset(outputs[0] + (nframes - nframes_left), 0, (nframes_left) * sizeof(jack_default_audio_sample_t));
228 memset(outputs[1] + (nframes - nframes_left), 0, (nframes_left) * sizeof(jack_default_audio_sample_t));
229 }
230 }
231
232 return wrotebytes;
233 }
234
235 void JackAC3Encoder::sample_move_dS_s16(jack_default_audio_sample_t* dst, char *src, jack_nframes_t nsamples, unsigned long src_skip)
236 {
237 /* ALERT: signed sign-extension portability !!! */
238 while (nsamples--) {
239 *dst = (*((short *) src)) / SAMPLE_MAX_16BIT;
240 dst++;
241 src += src_skip;
242 }
243 }
244
245 void JackAC3Encoder::sample_move_dS_s16_24ph(jack_default_audio_sample_t* dst, char *src, jack_nframes_t nsamples, unsigned long src_skip)
246 {
247 /* ALERT: signed sign-extension portability !!! */
248 while (nsamples--) {
249 *dst = (((int)(*((short *) src))) << 8) / SAMPLE_MAX_24BIT;
250 dst++;
251 src += src_skip;
252 }
253 }
254
255 void JackAC3Encoder::GetChannelName(const char* name, const char* alias, char* portname, int channel)
256 {
257 /*
258 * 2 channels = L, R
259 * 3 channels = L, C, R
260 * 4 channels = L, R, LS, RS
261 * 5 ch = L, C, R, LS, RS
262 * 6 ch = L, C, R, LS, RS, LFE
263 */
264
265 const char* AC3_name = "";
266
267 switch (channel) {
268
269 case 0:
270 AC3_name = "AC3_1_Left";
271 break;
272
273 case 1:
274 if (fAftenContext.channels == 2 || fAftenContext.channels == 4) {
275 AC3_name = "AC3_2_Right";
276 } else {
277 AC3_name = "AC3_2_Center";
278 }
279 break;
280
281 case 2:
282 if (fAftenContext.channels == 4) {
283 AC3_name = "AC3_3_LeftSurround";
284 } else {
285 AC3_name = "AC3_3_Right";
286 }
287 break;
288
289 case 3:
290 if (fAftenContext.channels == 4) {
291 AC3_name = "AC3_4_RightSurround";
292 } else {
293 AC3_name = "AC3_4_LeftSurround";
294 }
295 break;
296
297 case 4:
298 if (fAftenContext.channels > 4) {
299 AC3_name = "AC3_5_RightSurround";
300 }
301 break;
302
303 default:
304 break;
305 }
306
307 // Last channel
308 if (fAftenContext.lfe && (channel == fAftenContext.channels - 1)) {
309 sprintf(portname, "%s:%s:AC3_%d_LFE", name, alias, fAftenContext.channels);
310 } else {
311 sprintf(portname, "%s:%s:%s", name, alias, AC3_name);
312 }
313 }
314
315 #endif
316
317 } // end of namespace
jack_ringbuffer_read_space
size_t jack_ringbuffer_read_space(const jack_ringbuffer_t *rb)
Jack
Definition: JackAC3Encoder.cpp:30
jack_ringbuffer_create
jack_ringbuffer_t * jack_ringbuffer_create(size_t sz)
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_ringbuffer_free
void jack_ringbuffer_free(jack_ringbuffer_t *rb)
jack_ringbuffer_get_read_vector
void jack_ringbuffer_get_read_vector(const jack_ringbuffer_t *rb, jack_ringbuffer_data_t *vec)
jack_ringbuffer_write
size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, const char *src, size_t cnt)
jack_ringbuffer_read_advance
void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt)
jack_ringbuffer_data_t
Definition: ringbuffer.h:45