Debian Codegraph @

components/encoder/bladeenc/bladeenc.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 <time.h>
17
18 #include "bladeenc.h"
19 #include "config.h"
20
21 const String &BoCA::EncoderBlade::GetComponentSpecs()
22 {
23 static String componentSpecs;
24
25 if (bladedll != NIL)
26 {
27 componentSpecs = " \
28 \
29 <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
30 <component> \
31 <name>BladeEnc MP3 Encoder %VERSION%</name> \
32 <version>1.0</version> \
33 <id>bladeenc-enc</id> \
34 <type threadSafe=\"false\">encoder</type> \
35 <format> \
36 <name>MPEG 1 Audio Layer 3</name> \
37 <extension>mp3</extension> \
38 <tag id=\"id3v1-tag\" mode=\"append\">ID3v1</tag> \
39 <tag id=\"id3v2-tag\" mode=\"prepend\">ID3v2</tag> \
40 </format> \
41 <input bits=\"16\" channels=\"1-2\" rate=\"32000,44100,48000\"/> \
42 <parameters> \
43 <selection name=\"Bitrate\" argument=\"-b %VALUE\" default=\"192\"> \
44 <option alias=\"32 kbps\">32</option> \
45 <option alias=\"40 kbps\">40</option> \
46 <option alias=\"48 kbps\">48</option> \
47 <option alias=\"56 kbps\">56</option> \
48 <option alias=\"64 kbps\">64</option> \
49 <option alias=\"80 kbps\">80</option> \
50 <option alias=\"96 kbps\">96</option> \
51 <option alias=\"112 kbps\">112</option> \
52 <option alias=\"128 kbps\">128</option> \
53 <option alias=\"160 kbps\">160</option> \
54 <option alias=\"192 kbps\">192</option> \
55 <option alias=\"224 kbps\">224</option> \
56 <option alias=\"256 kbps\">256</option> \
57 <option alias=\"320 kbps\">320</option> \
58 </selection> \
59 </parameters> \
60 </component> \
61 \
62 ";
63
64 BE_VERSION beVer;
65
66 ex_beVersion(&beVer);
67
68 componentSpecs.Replace("%VERSION%", String("v").Append(String::FromInt(beVer.byMajorVersion)).Append(".").Append(String::FromInt(beVer.byMinorVersion)));
69 }
70
71 return componentSpecs;
72 }
73
74 Void smooth::AttachDLL(Void *instance)
75 {
76 LoadBladeDLL();
77 }
78
79 Void smooth::DetachDLL()
80 {
81 FreeBladeDLL();
82 }
83
84 BoCA::EncoderBlade::EncoderBlade() : beConfig()
85 {
86 configLayer = NIL;
87 config = NIL;
88
89 handle = NIL;
90
91 frameSize = 0;
92 }
93
94 BoCA::EncoderBlade::~EncoderBlade()
95 {
96 if (config != NIL) Config::Free(config);
97
98 if (configLayer != NIL) Object::DeleteObject(configLayer);
99 }
100
101 Bool BoCA::EncoderBlade::Activate()
102 {
103 const Format &format = track.GetFormat();
104 const Info &info = track.GetInfo();
105
106 /* Get configuration.
107 */
108 config = Config::Copy(GetConfiguration());
109
110 ConvertArguments(config);
111
112 /* Create and configure BladeEnc encoder.
113 */
114 beConfig.dwConfig = BE_CONFIG_MP3;
115 beConfig.format.mp3.dwSampleRate = format.rate;
116
117 if (format.channels == 2)
118 {
119 if (config->GetIntValue(ConfigureBlade::ConfigID, "DualChannel", 0)) beConfig.format.mp3.byMode = BE_MP3_MODE_DUALCHANNEL;
120 else beConfig.format.mp3.byMode = BE_MP3_MODE_STEREO;
121 }
122 else if (format.channels == 1)
123 {
124 beConfig.format.mp3.byMode = BE_MP3_MODE_MONO;
125 }
126
127 beConfig.format.mp3.wBitrate = config->GetIntValue(ConfigureBlade::ConfigID, "Bitrate", 0);
128 beConfig.format.mp3.bCopyright = config->GetIntValue(ConfigureBlade::ConfigID, "Copyright", 0);
129 beConfig.format.mp3.bCRC = config->GetIntValue(ConfigureBlade::ConfigID, "CRC", 0);
130 beConfig.format.mp3.bOriginal = config->GetIntValue(ConfigureBlade::ConfigID, "Original", 1);
131 beConfig.format.mp3.bPrivate = config->GetIntValue(ConfigureBlade::ConfigID, "Private", 0);
132
133 unsigned long bufferSize = 0;
134
135 ex_beInitStream(&beConfig, &frameSize, &bufferSize, &handle);
136
137 outBuffer.Resize(bufferSize);
138
139 /* Write ID3v2 tag if requested.
140 */
141 if (config->GetIntValue("Tags", "EnableID3v2", True) && (info.HasBasicInfo() || (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True))))
142 {
143 AS::Registry &boca = AS::Registry::Get();
144 AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag");
145
146 if (tagger != NIL)
147 {
148 Buffer<unsigned char> id3Buffer;
149
150 tagger->SetConfiguration(config);
151 tagger->RenderBuffer(id3Buffer, track);
152
153 driver->WriteData(id3Buffer, id3Buffer.Size());
154
155 boca.DeleteComponent(tagger);
156 }
157 }
158
159 return True;
160 }
161
162 Bool BoCA::EncoderBlade::Deactivate()
163 {
164 const Info &info = track.GetInfo();
165
166 /* Output remaining samples.
167 */
168 unsigned long bytes = 0;
169
170 if (ex_beEncodeChunk(handle, samplesBuffer.Size(), (short *) samplesBuffer, outBuffer, &bytes) == BE_ERR_SUCCESSFUL)
171 {
172 driver->WriteData(outBuffer, bytes);
173 }
174
175 /* Finalize stream.
176 */
177 if (ex_beDeinitStream(handle, outBuffer, &bytes) == BE_ERR_SUCCESSFUL)
178 {
179 driver->WriteData(outBuffer, bytes);
180 }
181
182 ex_beCloseStream(handle);
183
184 /* Write ID3v1 tag if requested.
185 */
186 if (config->GetIntValue("Tags", "EnableID3v1", False) && info.HasBasicInfo())
187 {
188 AS::Registry &boca = AS::Registry::Get();
189 AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v1-tag");
190
191 if (tagger != NIL)
192 {
193 Buffer<unsigned char> id3Buffer;
194
195 tagger->SetConfiguration(config);
196 tagger->RenderBuffer(id3Buffer, track);
197
198 driver->WriteData(id3Buffer, id3Buffer.Size());
199
200 boca.DeleteComponent(tagger);
201 }
202 }
203
204 /* Update ID3v2 tag with correct chapter marks.
205 */
206 if (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True) && config->GetIntValue("Tags", "EnableID3v2", True))
207 {
208 AS::Registry &boca = AS::Registry::Get();
209 AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag");
210
211 if (tagger != NIL)
212 {
213 Buffer<unsigned char> id3Buffer;
214
215 tagger->SetConfiguration(config);
216 tagger->RenderBuffer(id3Buffer, track);
217
218 driver->Seek(0);
219 driver->WriteData(id3Buffer, id3Buffer.Size());
220
221 boca.DeleteComponent(tagger);
222 }
223 }
224
225 return True;
226 }
227
228 Int BoCA::EncoderBlade::WriteData(Buffer<UnsignedByte> &data)
229 {
230 /* Copy data to samples buffer.
231 */
232 Int samples = data.Size() / 2;
233
234 samplesBuffer.Resize(samplesBuffer.Size() + samples);
235
236 memcpy(samplesBuffer + samplesBuffer.Size() - samples, data, data.Size());
237
238 /* Output samples to encoder.
239 */
240 Int dataLength = 0;
241 Int framesProcessed = 0;
242
243 while (samplesBuffer.Size() - framesProcessed * frameSize >= frameSize)
244 {
245 unsigned long bytes = 0;
246
247 if (ex_beEncodeChunk(handle, frameSize, (short *) samplesBuffer + framesProcessed * frameSize, outBuffer, &bytes) == BE_ERR_SUCCESSFUL)
248 {
249 dataLength += bytes;
250
251 driver->WriteData(outBuffer, bytes);
252 }
253
254 framesProcessed++;
255 }
256
257 memmove(samplesBuffer, samplesBuffer + framesProcessed * frameSize, sizeof(short) * (samplesBuffer.Size() - framesProcessed * frameSize));
258
259 samplesBuffer.Resize(samplesBuffer.Size() - framesProcessed * frameSize);
260
261 return dataLength;
262 }
263
264 Bool BoCA::EncoderBlade::ConvertArguments(Config *config)
265 {
266 if (!config->GetIntValue("Settings", "EnableConsole", False)) return False;
267
268 static const String encoderID = "bladeenc-enc";
269
270 /* Set default values.
271 */
272 if (!config->GetIntValue("Settings", "UserSpecifiedConfig", False))
273 {
274 config->SetIntValue(ConfigureBlade::ConfigID, "Bitrate", 192);
275 }
276
277 /* Get command line settings.
278 */
279 Int bitrate = config->GetIntValue(ConfigureBlade::ConfigID, "Bitrate", 192);
280
281 if (config->GetIntValue(encoderID, "Set Bitrate", False)) bitrate = config->GetIntValue(encoderID, "Bitrate", bitrate);
282
283 /* Set configuration values.
284 */
285 config->SetIntValue(ConfigureBlade::ConfigID, "Bitrate", Math::Max(32, Math::Min(320, bitrate)));
286
287 return True;
288 }
289
290 ConfigLayer *BoCA::EncoderBlade::GetConfigurationLayer()
291 {
292 if (configLayer == NIL) configLayer = new ConfigureBlade();
293
294 return configLayer;
295 }