Debian Codegraph @

components/encoder/bonk/bonk.cpp

raw symbols SCIP
1 /* BoCA - BonkEnc Component Architecture
2 * Copyright (C) 2007-2021 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include <smooth.h>
14 #include <smooth/dll.h>
15
16 #include "bonk.h"
17 #include "config.h"
18
19 const String &BoCA::EncoderBonk::GetComponentSpecs()
20 {
21 static String componentSpecs;
22
23 if (bonkdll != NIL)
24 {
25 componentSpecs = " \
26 \
27 <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
28 <component> \
29 <name>Bonk Audio Encoder %VERSION%</name> \
30 <version>1.0</version> \
31 <id>bonk-enc</id> \
32 <type>encoder</type> \
33 <format> \
34 <name>Bonk Audio Files</name> \
35 <extension>bonk</extension> \
36 </format> \
37 <input bits=\"16\" channels=\"1-2\"/> \
38 <parameters> \
39 <range name=\"Quantization factor\" argument=\"-q %VALUE\" default=\"0.4\" step=\"0.05\"> \
40 <min alias=\"0\">0</min> \
41 <max alias=\"2\">2</max> \
42 </range> \
43 <range name=\"Predictor size\" argument=\"-s %VALUE\" default=\"32\"> \
44 <min alias=\"min\">0</min> \
45 <max alias=\"max\">512</max> \
46 </range> \
47 <range name=\"Downsampling ratio\" argument=\"-r %VALUE\" default=\"2\"> \
48 <min alias=\"1\">1</min> \
49 <max alias=\"10\">10</max> \
50 </range> \
51 <switch name=\"Use Joint Stereo\" argument=\"--js\"/> \
52 <switch name=\"Use lossless compression\" argument=\"--lossless\"/> \
53 </parameters> \
54 </component> \
55 \
56 ";
57
58 componentSpecs.Replace("%VERSION%", String("v").Append(ex_bonk_get_version_string()));
59 }
60
61 return componentSpecs;
62 }
63
64 Void smooth::AttachDLL(Void *instance)
65 {
66 LoadBonkDLL();
67 }
68
69 Void smooth::DetachDLL()
70 {
71 FreeBonkDLL();
72 }
73
74 BoCA::EncoderBonk::EncoderBonk()
75 {
76 configLayer = NIL;
77 config = NIL;
78
79 encoder = NIL;
80
81 frameSize = 0;
82 }
83
84 BoCA::EncoderBonk::~EncoderBonk()
85 {
86 if (config != NIL) Config::Free(config);
87
88 if (configLayer != NIL) Object::DeleteObject(configLayer);
89 }
90
91 Bool BoCA::EncoderBonk::IsLossless() const
92 {
93 const Config *config = GetConfiguration();
94
95 return config->GetIntValue(ConfigureBonk::ConfigID, "Lossless", 0);
96 }
97
98 Bool BoCA::EncoderBonk::Activate()
99 {
100 const Format &format = track.GetFormat();
101 const Info &info = track.GetInfo();
102
103 /* Get configuration.
104 */
105 config = Config::Copy(GetConfiguration());
106
107 ConvertArguments(config);
108
109 /* Configure and create Bonk encoder.
110 */
111 frameSize = Int(1024.0 * format.rate / 44100) * format.channels * (config->GetIntValue(ConfigureBonk::ConfigID, "Lossless", 0) ? 1 : config->GetIntValue(ConfigureBonk::ConfigID, "Downsampling", 2));
112
113 dataBuffer.Resize(131072);
114
115 encoder = ex_bonk_encoder_create();
116
117 /* Write ID3v2 tag if requested.
118 */
119 if (config->GetIntValue("Tags", "EnableID3v2", True) && (info.HasBasicInfo() || (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True))))
120 {
121 AS::Registry &boca = AS::Registry::Get();
122 AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag");
123
124 if (tagger != NIL)
125 {
126 Buffer<unsigned char> id3Buffer;
127
128 tagger->SetConfiguration(config);
129 tagger->RenderBuffer(id3Buffer, track);
130
131 ex_bonk_encoder_set_id3_data(encoder, id3Buffer, id3Buffer.Size());
132
133 boca.DeleteComponent(tagger);
134
135 dataBuffer.Resize(dataBuffer.Size() + id3Buffer.Size());
136 }
137 }
138
139 /* Init Bonk encoder.
140 */
141 ex_bonk_encoder_init(encoder,
142 (unsigned int) Math::Max(track.length * format.channels, (Int64) 0), format.rate, format.channels,
143 config->GetIntValue(ConfigureBonk::ConfigID, "Lossless", 0), config->GetIntValue(ConfigureBonk::ConfigID, "JointStereo", 0),
144 config->GetIntValue(ConfigureBonk::ConfigID, "Predictor", 32), config->GetIntValue(ConfigureBonk::ConfigID, "Lossless", 0) ? 1 : config->GetIntValue(ConfigureBonk::ConfigID, "Downsampling", 2),
145 int(1024.0 * format.rate / 44100),
146 0.05 * (double) config->GetIntValue(ConfigureBonk::ConfigID, "Quantization", 8));
147
148 return True;
149 }
150
151 Bool BoCA::EncoderBonk::Deactivate()
152 {
153 static Endianness endianness = CPU().GetEndianness();
154
155 /* Output remaining samples.
156 */
157 Int bytes = ex_bonk_encoder_encode_packet(encoder, samplesBuffer, samplesBuffer.Size(), dataBuffer, dataBuffer.Size());
158
159 driver->WriteData(dataBuffer, bytes);
160
161 /* Finalize stream.
162 */
163 bytes = ex_bonk_encoder_finish(encoder, dataBuffer, dataBuffer.Size());
164
165 if (bytes > dataBuffer.Size())
166 {
167 dataBuffer.Resize(bytes);
168
169 bytes = ex_bonk_encoder_finish(encoder, dataBuffer, dataBuffer.Size());
170 }
171
172 driver->WriteData(dataBuffer, bytes);
173
174 if (track.length == -1)
175 {
176 Int sample_count = ex_bonk_encoder_get_sample_count(encoder);
177
178 driver->Seek(ex_bonk_encoder_get_sample_count_offset(encoder));
179
180 if (endianness == EndianLittle) for (Int i = 0; i <= 3; i++) driver->WriteData(((unsigned char *) &sample_count) + i, 1);
181 else for (Int i = 3; i >= 0; i--) driver->WriteData(((unsigned char *) &sample_count) + i, 1);
182 }
183
184 ex_bonk_encoder_close(encoder);
185
186 /* Update ID3v2 tag with correct chapter marks.
187 */
188 if (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True) && config->GetIntValue("Tags", "EnableID3v2", True))
189 {
190 AS::Registry &boca = AS::Registry::Get();
191 AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag");
192
193 if (tagger != NIL)
194 {
195 Buffer<unsigned char> id3Buffer;
196
197 tagger->SetConfiguration(config);
198 tagger->RenderBuffer(id3Buffer, track);
199
200 driver->Seek(2);
201 driver->WriteData(id3Buffer, id3Buffer.Size());
202
203 boca.DeleteComponent(tagger);
204 }
205 }
206
207 return True;
208 }
209
210 Int BoCA::EncoderBonk::WriteData(Buffer<UnsignedByte> &data)
211 {
212 /* Copy data to samples buffer.
213 */
214 Int samples = data.Size() / 2;
215
216 samplesBuffer.Resize(samplesBuffer.Size() + samples);
217
218 memcpy(samplesBuffer + samplesBuffer.Size() - samples, data, data.Size());
219
220 /* Output samples to encoder.
221 */
222 Int dataLength = 0;
223 Int framesProcessed = 0;
224
225 while (samplesBuffer.Size() - framesProcessed * frameSize >= frameSize)
226 {
227 Int bytes = ex_bonk_encoder_encode_packet(encoder, samplesBuffer + framesProcessed * frameSize, frameSize, dataBuffer, dataBuffer.Size());
228
229 dataLength += bytes;
230
231 driver->WriteData(dataBuffer, bytes);
232
233 framesProcessed++;
234 }
235
236 memmove(samplesBuffer, samplesBuffer + framesProcessed * frameSize, sizeof(short) * (samplesBuffer.Size() - framesProcessed * frameSize));
237
238 samplesBuffer.Resize(samplesBuffer.Size() - framesProcessed * frameSize);
239
240 return dataLength;
241 }
242
243 Bool BoCA::EncoderBonk::ConvertArguments(Config *config)
244 {
245 if (!config->GetIntValue("Settings", "EnableConsole", False)) return False;
246
247 static const String encoderID = "bonk-enc";
248
249 /* Set default values.
250 */
251 if (!config->GetIntValue("Settings", "UserSpecifiedConfig", False))
252 {
253 config->SetIntValue(ConfigureBonk::ConfigID, "JointStereo", False);
254 config->SetIntValue(ConfigureBonk::ConfigID, "Lossless", False);
255
256 config->SetIntValue(ConfigureBonk::ConfigID, "Quantization", 8);
257 config->SetIntValue(ConfigureBonk::ConfigID, "Predictor", 32);
258 config->SetIntValue(ConfigureBonk::ConfigID, "Downsampling", 2);
259 }
260
261 /* Get command line settings.
262 */
263 Bool jointStereo = config->GetIntValue(encoderID, "Use Joint Stereo", config->GetIntValue(ConfigureBonk::ConfigID, "JointStereo", False));
264 Bool lossless = config->GetIntValue(encoderID, "Use lossless compression", config->GetIntValue(ConfigureBonk::ConfigID, "Lossless", False));
265
266 Int quantization = config->GetIntValue(ConfigureBonk::ConfigID, "Quantization", 8);
267 Int predictor = config->GetIntValue(ConfigureBonk::ConfigID, "Predictor", 32);
268 Int downsampling = config->GetIntValue(ConfigureBonk::ConfigID, "Downsampling", 2);
269
270 if (config->GetIntValue(encoderID, "Set Quantization factor", False)) quantization = config->GetIntValue(encoderID, "Quantization factor", quantization);
271 if (config->GetIntValue(encoderID, "Set Predictor size", False)) predictor = config->GetIntValue(encoderID, "Predictor size", predictor);
272 if (config->GetIntValue(encoderID, "Set Downsampling ratio", False)) downsampling = config->GetIntValue(encoderID, "Downsampling ratio", downsampling);
273
274 /* Set configuration values.
275 */
276 config->SetIntValue(ConfigureBonk::ConfigID, "JointStereo", jointStereo);
277 config->SetIntValue(ConfigureBonk::ConfigID, "Lossless", lossless);
278
279 config->SetIntValue(ConfigureBonk::ConfigID, "Quantization", Math::Max(0, Math::Min(40, quantization)));
280 config->SetIntValue(ConfigureBonk::ConfigID, "Predictor", Math::Max(0, Math::Min(512, predictor)));
281 config->SetIntValue(ConfigureBonk::ConfigID, "Downsampling", Math::Max(0, Math::Min(10, downsampling)));
282
283 return True;
284 }
285
286 ConfigLayer *BoCA::EncoderBonk::GetConfigurationLayer()
287 {
288 if (configLayer == NIL) configLayer = new ConfigureBonk();
289
290 return configLayer;
291 }