Debian Codegraph @

runtime/application/external/win32/decodercomponentfile.cpp

raw symbols SCIP
1 /* BoCA - BonkEnc Component Architecture
2 * Copyright (C) 2007-2024 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 <boca/application/external/decodercomponentfile.h>
14 #include <boca/application/external/utilities.h>
15 #include <boca/application/taggercomponent.h>
16 #include <boca/application/registry.h>
17 #include <boca/common/utilities.h>
18
19 #include <windows.h>
20 #include <mmreg.h>
21
22 using namespace smooth::IO;
23
24 BoCA::AS::DecoderComponentExternalFile::DecoderComponentExternalFile(ComponentSpecs *specs) : DecoderComponentExternal(specs)
25 {
26 in = NIL;
27
28 dataOffset = 0;
29 }
30
31 BoCA::AS::DecoderComponentExternalFile::~DecoderComponentExternalFile()
32 {
33 }
34
35 String BoCA::AS::DecoderComponentExternalFile::GetMD5(const String &encFileName)
36 {
37 return ExternalUtilities::GetMD5(specs, encFileName);
38 }
39
40 Error BoCA::AS::DecoderComponentExternalFile::GetStreamInfo(const String &streamURI, Track &track)
41 {
42 /* Return cached track from previous call.
43 */
44 if (this->track.fileName == streamURI)
45 {
46 track = this->track;
47
48 return Success();
49 }
50
51 /* Create temporary WAVE file.
52 */
53 String wavFileName = Utilities::GetNonUnicodeTempFileName(streamURI).Append(".wav");
54 String encFileName = streamURI;
55
56 /* Copy the file and decode the temporary copy
57 * if the file name contains Unicode characters.
58 */
59 if (String::IsUnicode(streamURI))
60 {
61 encFileName = Utilities::GetNonUnicodeTempFileName(streamURI).Append(".").Append(specs->formats.GetFirst()->GetExtensions().GetFirst());
62
63 File(streamURI).Copy(encFileName);
64 }
65
66 /* Start 3rd party command line decoder.
67 */
68 String command = String("\"").Append(specs->external_command).Append("\"").Replace("/", Directory::GetDirectoryDelimiter());
69 String arguments = String(specs->external_arguments).Replace("%OPTIONS", specs->GetExternalArgumentsString(GetConfiguration()))
70 .Replace("%INFILE", String("\"").Append(encFileName).Append("\""))
71 .Replace("%OUTFILE", String("\"").Append(wavFileName).Append("\""));
72
73 if (specs->debug) AllocConsole();
74
75 STARTUPINFOA startupInfo;
76
77 ZeroMemory(&startupInfo, sizeof(startupInfo));
78
79 startupInfo.cb = sizeof(startupInfo);
80 startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
81 startupInfo.wShowWindow = specs->debug ? SW_SHOW : SW_HIDE;
82 startupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
83 startupInfo.hStdOutput = specs->debug ? GetStdHandle(STD_OUTPUT_HANDLE) : NIL;
84 startupInfo.hStdError = specs->debug ? GetStdHandle(STD_ERROR_HANDLE) : NIL;
85
86 PROCESS_INFORMATION processInfo;
87
88 ZeroMemory(&processInfo, sizeof(processInfo));
89
90 CreateProcessA(NIL, String(command).Append(" ").Append(arguments), NIL, NIL, True, 0, NIL, NIL, &startupInfo, &processInfo);
91
92 /* Check process handle.
93 */
94 if (processInfo.hProcess == NIL)
95 {
96 errorState = True;
97 errorString = String("Unable to run decoder ").Append(command).Append(".");
98
99 /* Remove temporary file if necessary.
100 */
101 if (String::IsUnicode(streamURI))
102 {
103 File(encFileName).Delete();
104 }
105
106 return Error();
107 }
108
109 /* Wait until the decoder exits.
110 */
111 while (WaitForSingleObject(processInfo.hProcess, 0) == WAIT_TIMEOUT) S::System::System::Sleep(10);
112
113 if (specs->debug)
114 {
115 BoCA::Utilities::InfoMessage("Click OK to close console window.");
116
117 FreeConsole();
118 }
119
120 /* Query MD5.
121 */
122 track.md5 = GetMD5(encFileName);
123
124 /* Remove temporary copy if necessary.
125 */
126 if (String::IsUnicode(streamURI))
127 {
128 File(encFileName).Delete();
129 }
130
131 /* Check if anything went wrong.
132 */
133 unsigned long exitCode = 0;
134
135 GetExitCodeProcess(processInfo.hProcess, &exitCode);
136
137 if (!specs->external_ignoreExitCode && exitCode != 0)
138 {
139 /* Remove temporary WAVE file.
140 */
141 File(wavFileName).Delete();
142
143 errorState = True;
144 errorString = String("Decoder returned exit code ").Append(String::FromInt((signed) exitCode)).Append(".");
145
146 return Error();
147 }
148
149 /* Open decoded WAVE file and read header.
150 */
151 InStream *in = new InStream(STREAM_FILE, wavFileName, IS_READ);
152
153 track.fileName = streamURI;
154 track.fileSize = File(streamURI).GetFileSize();
155
156 /* Read RIFF chunk.
157 */
158 if (in->InputString(4) != "RIFF") { errorState = True; errorString = "Unknown file type"; }
159
160 in->RelSeek(4);
161
162 if (in->InputString(4) != "WAVE") { errorState = True; errorString = "Unknown file type"; }
163
164 String chunk;
165
166 do
167 {
168 /* Read next chunk.
169 */
170 chunk = in->InputString(4);
171
172 UnsignedInt32 cSize = in->InputNumber(4);
173
174 if (chunk == "fmt ")
175 {
176 Int waveFormat = in->InputNumber(2);
177
178 if (waveFormat != WAVE_FORMAT_PCM &&
179 waveFormat != WAVE_FORMAT_IEEE_FLOAT &&
180 waveFormat != WAVE_FORMAT_EXTENSIBLE) { errorState = True; errorString = "Unsupported audio format"; }
181
182 Format format = track.GetFormat();
183
184 format.channels = (unsigned short) in->InputNumber(2);
185 format.rate = (unsigned long) in->InputNumber(4);
186
187 in->RelSeek(6);
188
189 format.fp = (waveFormat == WAVE_FORMAT_IEEE_FLOAT);
190 format.bits = (unsigned short) in->InputNumber(2);
191 format.order = BYTE_INTEL;
192
193 if (format.bits == 8) format.sign = False;
194
195 track.SetFormat(format);
196
197 /* Skip rest of chunk.
198 */
199 in->RelSeek(cSize - 16 + cSize % 2);
200 }
201 else if (chunk == "LIST")
202 {
203 /* Copy chunk to separate buffer.
204 */
205 Buffer<UnsignedByte> info(8 + cSize + cSize % 2);
206
207 in->RelSeek(-8);
208 in->InputData(info, info.Size());
209
210 /* Parse LIST INFO chunk.
211 */
212 AS::Registry &boca = AS::Registry::Get();
213 AS::TaggerComponent *riffTagger = (AS::TaggerComponent *) boca.CreateComponentByID("riff-tag");
214
215 if (riffTagger != NIL)
216 {
217 riffTagger->SetConfiguration(GetConfiguration());
218 riffTagger->ParseBuffer(info, track);
219
220 boca.DeleteComponent(riffTagger);
221 }
222 }
223 else if (chunk == "data")
224 {
225 if (cSize == 0xffffffff || cSize == 0) track.length = (in->Size() - in->GetPos()) / track.GetFormat().channels / (track.GetFormat().bits / 8);
226 else track.length = cSize / track.GetFormat().channels / (track.GetFormat().bits / 8);
227 }
228 else
229 {
230 /* Skip chunk.
231 */
232 in->RelSeek(cSize + cSize % 2);
233 }
234 }
235 while (!errorState && chunk != "data");
236
237 /* Close input stream.
238 */
239 delete in;
240
241 /* Remove temporary WAVE file.
242 */
243 File(wavFileName).Delete();
244
245 /* Query tags and update track.
246 */
247 QueryTags(streamURI, track);
248
249 return Success();
250 }
251
252 Bool BoCA::AS::DecoderComponentExternalFile::Activate()
253 {
254 /* Create temporary WAVE file.
255 */
256 wavFileName = Utilities::GetNonUnicodeTempFileName(track.fileName).Append(".wav");
257 encFileName = track.fileName;
258
259 /* Copy the file and decode the temporary copy
260 * if the file name contains Unicode characters.
261 */
262 if (String::IsUnicode(track.fileName))
263 {
264 encFileName = Utilities::GetNonUnicodeTempFileName(track.fileName).Append(".").Append(specs->formats.GetFirst()->GetExtensions().GetFirst());
265
266 File(track.fileName).Copy(encFileName);
267
268 /* Look for a companion file.
269 */
270 File companionFile = GetCompanionFile(track.fileName);
271
272 if (companionFile.Exists()) companionFile.Copy(GetCompanionFile(encFileName));
273 }
274
275 /* Start 3rd party command line decoder.
276 */
277 String command = String("\"").Append(specs->external_command).Append("\"").Replace("/", Directory::GetDirectoryDelimiter());
278 String arguments = String(specs->external_arguments).Replace("%OPTIONS", specs->GetExternalArgumentsString(GetConfiguration()))
279 .Replace("%INFILE", String("\"").Append(encFileName).Append("\""))
280 .Replace("%OUTFILE", String("\"").Append(wavFileName).Append("\""));
281
282 if (specs->debug) AllocConsole();
283
284 STARTUPINFOA startupInfo;
285
286 ZeroMemory(&startupInfo, sizeof(startupInfo));
287
288 startupInfo.cb = sizeof(startupInfo);
289 startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
290 startupInfo.wShowWindow = specs->debug ? SW_SHOW : SW_HIDE;
291 startupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
292 startupInfo.hStdOutput = specs->debug ? GetStdHandle(STD_OUTPUT_HANDLE) : NIL;
293 startupInfo.hStdError = specs->debug ? GetStdHandle(STD_ERROR_HANDLE) : NIL;
294
295 PROCESS_INFORMATION processInfo;
296
297 ZeroMemory(&processInfo, sizeof(processInfo));
298
299 CreateProcessA(NIL, String(command).Append(" ").Append(arguments), NIL, NIL, True, 0, NIL, NIL, &startupInfo, &processInfo);
300
301 /* Check process handle.
302 */
303 if (processInfo.hProcess == NIL)
304 {
305 errorState = True;
306 errorString = String("Unable to run decoder ").Append(command).Append(".");
307
308 /* Remove temporary file if necessary.
309 */
310 if (String::IsUnicode(track.fileName))
311 {
312 File(encFileName).Delete();
313
314 /* Delete companion file too.
315 */
316 File companionFile = GetCompanionFile(encFileName);
317
318 if (companionFile.Exists()) companionFile.Delete();
319 }
320
321 return False;
322 }
323
324 /* Wait until the decoder exits.
325 */
326 while (WaitForSingleObject(processInfo.hProcess, 0) == WAIT_TIMEOUT) S::System::System::Sleep(10);
327
328 if (specs->debug)
329 {
330 BoCA::Utilities::InfoMessage("Click OK to close console window.");
331
332 FreeConsole();
333 }
334
335 /* Remove temporary copy if necessary.
336 */
337 if (String::IsUnicode(track.fileName))
338 {
339 File(encFileName).Delete();
340 }
341
342 /* Check if anything went wrong.
343 */
344 unsigned long exitCode = 0;
345
346 GetExitCodeProcess(processInfo.hProcess, &exitCode);
347
348 if (!specs->external_ignoreExitCode && exitCode != 0)
349 {
350 /* Remove temporary WAVE file.
351 */
352 File(wavFileName).Delete();
353
354 errorState = True;
355 errorString = String("Decoder returned exit code ").Append(String::FromInt((signed) exitCode)).Append(".");
356
357 return False;
358 }
359
360 /* Open decoded WAVE file and skip the header.
361 */
362 in = new InStream(STREAM_FILE, wavFileName, IS_READ);
363
364 in->Seek(12);
365
366 String chunk;
367
368 do
369 {
370 /* Read next chunk.
371 */
372 chunk = in->InputString(4);
373
374 UnsignedInt32 cSize = in->InputNumber(4);
375
376 if (chunk != "data")
377 {
378 /* Skip chunk.
379 */
380 in->RelSeek(cSize + cSize % 2);
381 }
382 }
383 while (chunk != "data");
384
385 dataOffset = in->GetPos();
386
387 return True;
388 }
389
390 Bool BoCA::AS::DecoderComponentExternalFile::Deactivate()
391 {
392 /* Close input stream.
393 */
394 delete in;
395
396 /* Remove temporary WAVE file.
397 */
398 File(wavFileName).Delete();
399
400 return True;
401 }
402
403 Bool BoCA::AS::DecoderComponentExternalFile::Seek(Int64 samplePosition)
404 {
405 in->Seek(dataOffset + samplePosition * track.GetFormat().channels * (track.GetFormat().bits / 8));
406
407 return True;
408 }
409
410 Int BoCA::AS::DecoderComponentExternalFile::ReadData(Buffer<UnsignedByte> &data)
411 {
412 if (in->GetPos() == in->Size()) return -1;
413
414 Int size = Math::Min((Int64) 2048, in->Size() - in->GetPos());
415
416 /* Hand data over from the input file.
417 */
418 data.Resize(size);
419
420 in->InputData(data, size);
421
422 ProcessData(data);
423
424 return size;
425 }