| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2026 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.h> |
| 14 | #include "dllinterface.h" |
| 15 | |
| 16 | #ifdef __WIN32__ |
| 17 | # include <windows.h> |
| 18 | # include <shlobj.h> |
| 19 | #endif |
| 20 | |
| 21 | using namespace BoCA; |
| 22 | |
| 23 | using namespace smooth::IO; |
| 24 | using namespace smooth::XML; |
| 25 | |
| 26 | #ifndef __APPLE__ |
| 27 | using namespace CA; |
| 28 | |
| 29 | namespace CA |
| 30 | { |
| 31 | AUDIOFILEINITIALIZEWITHCALLBACKS AudioFileInitializeWithCallbacks = NIL; |
| 32 | AUDIOFILECLOSE AudioFileClose = NIL; |
| 33 | AUDIOFILESETPROPERTY AudioFileSetProperty = NIL; |
| 34 | AUDIOFILEWRITEPACKETS AudioFileWritePackets = NIL; |
| 35 | |
| 36 | AUDIOCONVERTERNEW AudioConverterNew = NIL; |
| 37 | AUDIOCONVERTERDISPOSE AudioConverterDispose = NIL; |
| 38 | AUDIOCONVERTERGETPROPERTY AudioConverterGetProperty = NIL; |
| 39 | AUDIOCONVERTERGETPROPERTYINFO AudioConverterGetPropertyInfo = NIL; |
| 40 | AUDIOCONVERTERSETPROPERTY AudioConverterSetProperty = NIL; |
| 41 | AUDIOCONVERTERFILLCOMPLEXBUFFER AudioConverterFillComplexBuffer = NIL; |
| 42 | |
| 43 | AUDIOFORMATGETPROPERTY AudioFormatGetProperty = NIL; |
| 44 | AUDIOFORMATGETPROPERTYINFO AudioFormatGetPropertyInfo = NIL; |
| 45 | }; |
| 46 | #endif |
| 47 | |
| 48 | MP4MODIFY ex_MP4Modify = NIL; |
| 49 | MP4CLOSE ex_MP4Close = NIL; |
| 50 | MP4OPTIMIZE ex_MP4Optimize = NIL; |
| 51 | |
| 52 | MP4FINDTRACKID ex_MP4FindTrackId = NIL; |
| 53 | MP4SETTRACKINTEGERPROPERTY ex_MP4SetTrackIntegerProperty = NIL; |
| 54 | |
| 55 | DynamicLoader *coreaudiodll = NIL; |
| 56 | DynamicLoader *mp4v2dll = NIL; |
| 57 | |
| 58 | #ifdef __WIN32__ |
| 59 | static const String coreAudioToolbox = "CoreAudioToolbox.dll"; |
| 60 | |
| 61 | static String GetSystemDirectory(int id) |
| 62 | { |
| 63 | String commonFilesDir; |
| 64 | |
| 65 | ITEMIDLIST *idlist; |
| 66 | Buffer<wchar_t> buffer(32768 + 1); |
| 67 | |
| 68 | SHGetSpecialFolderLocation(NIL, id, &idlist); |
| 69 | SHGetPathFromIDList(idlist, buffer); |
| 70 | |
| 71 | commonFilesDir = buffer; |
| 72 | |
| 73 | CoTaskMemFree(idlist); |
| 74 | |
| 75 | if (commonFilesDir != NIL && !commonFilesDir.EndsWith("\\")) commonFilesDir.Append("\\"); |
| 76 | |
| 77 | return commonFilesDir; |
| 78 | } |
| 79 | |
| 80 | # if defined __i386__ || defined _M_IX86 |
| 81 | static const String architecture = "x86"; |
| 82 | #elif defined __x86_64__ || defined _M_AMD64 |
| 83 | static const String architecture = "x64"; |
| 84 | #elif defined __arm__ || defined _M_ARM |
| 85 | static const String architecture = "arm"; |
| 86 | #elif defined __aarch64__ || defined _M_ARM64 |
| 87 | static const String architecture = "arm64"; |
| 88 | #endif |
| 89 | |
| 90 | static Void GetPackageFolders(const String &, Array<String> &); |
| 91 | |
| 92 | static Void FindDependenciesFolders(const String &packageFolder, Array<String> &packageFolders) |
| 93 | { |
| 94 | if (!File(packageFolder.Append("AppxManifest.xml")).Exists()) return; |
| 95 | |
| 96 | /* Load app manifest. |
| 97 | */ |
| 98 | Document document; |
| 99 | |
| 100 | document.LoadFile(packageFolder.Append("AppxManifest.xml")); |
| 101 | |
| 102 | /* Get root node. |
| 103 | */ |
| 104 | Node *root = document.GetRootNode(); |
| 105 | |
| 106 | if (root == NIL) return; |
| 107 | |
| 108 | /* Get Dependencies node. |
| 109 | */ |
| 110 | Node *dependencies = root->GetNodeByName("Dependencies"); |
| 111 | |
| 112 | if (dependencies == NIL) return; |
| 113 | |
| 114 | for (Int i = 0; i < dependencies->GetNOfNodes(); i++) |
| 115 | { |
| 116 | Node *node = dependencies->GetNthNode(i); |
| 117 | |
| 118 | if (node->GetName() != "PackageDependency") continue; |
| 119 | |
| 120 | Attribute *name = node->GetAttributeByName("Name"); |
| 121 | |
| 122 | if (name == NIL) continue; |
| 123 | |
| 124 | GetPackageFolders(name->GetContent(), packageFolders); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static Void GetPackageFolders(const String &packageName, Array<String> &packageFolders) |
| 129 | { |
| 130 | /* Open Packages key. |
| 131 | */ |
| 132 | HKEY packagesKey; |
| 133 | |
| 134 | if (RegOpenKey(HKEY_CLASSES_ROOT, L"Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion\\AppModel\\Repository\\Packages", &packagesKey) != ERROR_SUCCESS) return; |
| 135 | |
| 136 | /* Enumerate registered packages. |
| 137 | */ |
| 138 | Int index = 0; |
| 139 | wchar_t name[256]; |
| 140 | |
| 141 | while (RegEnumKey(packagesKey, index++, name, sizeof(name) / sizeof(wchar_t)) == ERROR_SUCCESS) |
| 142 | { |
| 143 | if (!String(name).StartsWith(packageName)) continue; |
| 144 | if (!String(name).Contains(String("_").Append(architecture).Append("_"))) continue; |
| 145 | |
| 146 | /* Found package, open key. |
| 147 | */ |
| 148 | HKEY packageKey; |
| 149 | |
| 150 | if (RegOpenKey(packagesKey, name, &packageKey) != ERROR_SUCCESS) continue; |
| 151 | |
| 152 | /* Query folder. |
| 153 | */ |
| 154 | const DWORD size = 2048; |
| 155 | DWORD bytes = size * sizeof(wchar_t); |
| 156 | wchar_t folder[size] = { 0 }; |
| 157 | |
| 158 | RegQueryValueEx(packageKey, L"PackageRootFolder", 0, 0, (BYTE *) folder, &bytes); |
| 159 | RegCloseKey(packageKey); |
| 160 | |
| 161 | /* Add folder and dependencies to list. |
| 162 | */ |
| 163 | String packageFolder = folder; |
| 164 | |
| 165 | if (!packageFolder.EndsWith("\\")) packageFolder.Append("\\"); |
| 166 | |
| 167 | if (packageFolders.Add(packageFolder, packageFolder.ComputeCRC32())) FindDependenciesFolders(packageFolder, packageFolders); |
| 168 | |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | RegCloseKey(packagesKey); |
| 173 | } |
| 174 | |
| 175 | static Void CopyLibrary(const String &libName, const Array<String> &packageFolders, const String &cacheFolder) |
| 176 | { |
| 177 | /* Find source and target file names. |
| 178 | */ |
| 179 | String sourceFile; |
| 180 | String targetFile = cacheFolder.Append(libName); |
| 181 | |
| 182 | foreach (const String &packageFolder, packageFolders) |
| 183 | { |
| 184 | if (!File(packageFolder.Append(libName)).Exists()) continue; |
| 185 | |
| 186 | sourceFile = packageFolder.Append(libName); |
| 187 | } |
| 188 | |
| 189 | /* Check file existence and skip already existing files. |
| 190 | */ |
| 191 | if (!File(sourceFile).Exists() || |
| 192 | File(targetFile).Exists()) return; |
| 193 | |
| 194 | /* Prepare input and output files. |
| 195 | */ |
| 196 | InStream in(STREAM_FILE, sourceFile); |
| 197 | OutStream out(STREAM_FILE, targetFile, OS_REPLACE); |
| 198 | |
| 199 | /* Copy chunks of 128kB. |
| 200 | */ |
| 201 | Buffer<UnsignedByte> buffer(131072); |
| 202 | |
| 203 | Int overlap = 0; |
| 204 | Bool foundSelf = False; |
| 205 | |
| 206 | for (Int i = 0; i < in.Size(); i += buffer.Size() - overlap) |
| 207 | { |
| 208 | memcpy(buffer, buffer + buffer.Size() - overlap, overlap); |
| 209 | |
| 210 | Int bytes = in.InputData(buffer + overlap, Math::Min(buffer.Size() - overlap, in.Size() - in.GetPos())); |
| 211 | |
| 212 | out.OutputData(buffer + overlap, bytes); |
| 213 | |
| 214 | /* Check for referenced DLLs. |
| 215 | */ |
| 216 | for (Int n = 0; n < overlap + bytes - 5; n++) |
| 217 | { |
| 218 | if (buffer[n] != '.' || buffer[n + 1] != 'd' || buffer[n + 2] != 'l' || buffer[n + 3] != 'l' || buffer[n + 4] != 0) continue; |
| 219 | |
| 220 | for (Int m = n; m >= 0; m--) |
| 221 | { |
| 222 | if (buffer[m] >= 0x20) continue; |
| 223 | |
| 224 | /* Get DLL name and copy it if it looks like a reference. |
| 225 | */ |
| 226 | String dllName = (char *) (UnsignedByte *) buffer + m + 1; |
| 227 | |
| 228 | if (foundSelf) CopyLibrary(dllName, packageFolders, cacheFolder); |
| 229 | else if (dllName == libName) foundSelf = True; |
| 230 | |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /* Overlap 128 bytes when wrapping around so we do not miss any relevant DLLs. |
| 236 | */ |
| 237 | overlap = 128; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static Void CacheCoreAudioLibraries(const String &cacheFolder) |
| 242 | { |
| 243 | Array<String> iTunesFolders; |
| 244 | |
| 245 | GetPackageFolders("AppleInc.iTunes", iTunesFolders); |
| 246 | |
| 247 | if (iTunesFolders.Length() == 0) return; |
| 248 | |
| 249 | /* Check if package ID has changed. |
| 250 | */ |
| 251 | const String packageId = Directory(iTunesFolders.GetFirst()).GetDirectoryName(); |
| 252 | const String packageIdFile = cacheFolder.Append("PackageId"); |
| 253 | |
| 254 | if (File(packageIdFile).Exists()) |
| 255 | { |
| 256 | InStream in(STREAM_FILE, packageIdFile); |
| 257 | |
| 258 | if (in.InputLine() == packageId) return; |
| 259 | } |
| 260 | |
| 261 | /* Create cache folder and make sure it's empty. |
| 262 | */ |
| 263 | Directory(cacheFolder).Create(); |
| 264 | Directory(cacheFolder).Empty(); |
| 265 | |
| 266 | /* Recursively copy CoreAudioToolbox.dll and referenced DLLs. |
| 267 | */ |
| 268 | CopyLibrary(coreAudioToolbox, iTunesFolders, cacheFolder); |
| 269 | |
| 270 | /* Store package ID. |
| 271 | */ |
| 272 | OutStream out(STREAM_FILE, packageIdFile, OS_REPLACE); |
| 273 | |
| 274 | out.OutputLine(packageId); |
| 275 | } |
| 276 | #endif |
| 277 | |
| 278 | Bool LoadCoreAudioDLL() |
| 279 | { |
| 280 | #ifndef __APPLE__ |
| 281 | # ifdef __WIN32__ |
| 282 | String coreAudioDir = GetSystemDirectory(CSIDL_PROGRAM_FILES_COMMON).Append("Apple\\Apple Application Support\\"); |
| 283 | |
| 284 | if (!File(String(coreAudioDir).Append(coreAudioToolbox)).Exists()) |
| 285 | { |
| 286 | coreAudioDir = GetSystemDirectory(CSIDL_PROGRAM_FILES).Append("iTunes\\"); |
| 287 | } |
| 288 | |
| 289 | if (!File(String(coreAudioDir).Append(coreAudioToolbox)).Exists()) |
| 290 | { |
| 291 | coreAudioDir = String(BoCA::Config::Get()->cacheDir).Append("boca\\boca.encoder.coreaudio\\").Append(architecture).Append("\\"); |
| 292 | |
| 293 | CacheCoreAudioLibraries(coreAudioDir); |
| 294 | } |
| 295 | |
| 296 | /* Add Apple Application Services directory to DLL search path. |
| 297 | */ |
| 298 | SetDllDirectory(coreAudioDir); |
| 299 | |
| 300 | coreaudiodll = new DynamicLoader(String(coreAudioDir).Append(coreAudioToolbox)); |
| 301 | # endif |
| 302 | |
| 303 | if (coreaudiodll == NIL) return False; |
| 304 | |
| 305 | AudioFileInitializeWithCallbacks = (AUDIOFILEINITIALIZEWITHCALLBACKS) coreaudiodll->GetFunctionAddress("AudioFileInitializeWithCallbacks"); |
| 306 | AudioFileClose = (AUDIOFILECLOSE) coreaudiodll->GetFunctionAddress("AudioFileClose"); |
| 307 | AudioFileSetProperty = (AUDIOFILESETPROPERTY) coreaudiodll->GetFunctionAddress("AudioFileSetProperty"); |
| 308 | AudioFileWritePackets = (AUDIOFILEWRITEPACKETS) coreaudiodll->GetFunctionAddress("AudioFileWritePackets"); |
| 309 | |
| 310 | AudioConverterNew = (AUDIOCONVERTERNEW) coreaudiodll->GetFunctionAddress("AudioConverterNew"); |
| 311 | AudioConverterDispose = (AUDIOCONVERTERDISPOSE) coreaudiodll->GetFunctionAddress("AudioConverterDispose"); |
| 312 | AudioConverterGetProperty = (AUDIOCONVERTERGETPROPERTY) coreaudiodll->GetFunctionAddress("AudioConverterGetProperty"); |
| 313 | AudioConverterGetPropertyInfo = (AUDIOCONVERTERGETPROPERTYINFO) coreaudiodll->GetFunctionAddress("AudioConverterGetPropertyInfo"); |
| 314 | AudioConverterSetProperty = (AUDIOCONVERTERSETPROPERTY) coreaudiodll->GetFunctionAddress("AudioConverterSetProperty"); |
| 315 | AudioConverterFillComplexBuffer = (AUDIOCONVERTERFILLCOMPLEXBUFFER) coreaudiodll->GetFunctionAddress("AudioConverterFillComplexBuffer"); |
| 316 | |
| 317 | AudioFormatGetProperty = (AUDIOFORMATGETPROPERTY) coreaudiodll->GetFunctionAddress("AudioFormatGetProperty"); |
| 318 | AudioFormatGetPropertyInfo = (AUDIOFORMATGETPROPERTYINFO) coreaudiodll->GetFunctionAddress("AudioFormatGetPropertyInfo"); |
| 319 | |
| 320 | if (AudioFileInitializeWithCallbacks == NIL || |
| 321 | AudioFileClose == NIL || |
| 322 | AudioFileSetProperty == NIL || |
| 323 | AudioFileWritePackets == NIL || |
| 324 | |
| 325 | AudioConverterNew == NIL || |
| 326 | AudioConverterDispose == NIL || |
| 327 | AudioConverterGetProperty == NIL || |
| 328 | AudioConverterGetPropertyInfo == NIL || |
| 329 | AudioConverterSetProperty == NIL || |
| 330 | AudioConverterFillComplexBuffer == NIL || |
| 331 | |
| 332 | AudioFormatGetProperty == NIL || |
| 333 | AudioFormatGetPropertyInfo == NIL) { FreeCoreAudioDLL(); return False; } |
| 334 | #endif |
| 335 | |
| 336 | return True; |
| 337 | } |
| 338 | |
| 339 | Void FreeCoreAudioDLL() |
| 340 | { |
| 341 | #ifndef __APPLE__ |
| 342 | BoCA::Utilities::FreeCodecDLL(coreaudiodll); |
| 343 | #endif |
| 344 | |
| 345 | coreaudiodll = NIL; |
| 346 | } |
| 347 | |
| 348 | Bool LoadMP4v2DLL() |
| 349 | { |
| 350 | mp4v2dll = BoCA::Utilities::LoadCodecDLL("mp4v2"); |
| 351 | |
| 352 | if (mp4v2dll == NIL) return False; |
| 353 | |
| 354 | ex_MP4Modify = (MP4MODIFY) mp4v2dll->GetFunctionAddress("MP4Modify"); |
| 355 | ex_MP4Close = (MP4CLOSE) mp4v2dll->GetFunctionAddress("MP4Close"); |
| 356 | ex_MP4Optimize = (MP4OPTIMIZE) mp4v2dll->GetFunctionAddress("MP4Optimize"); |
| 357 | |
| 358 | ex_MP4FindTrackId = (MP4FINDTRACKID) mp4v2dll->GetFunctionAddress("MP4FindTrackId"); |
| 359 | ex_MP4SetTrackIntegerProperty = (MP4SETTRACKINTEGERPROPERTY) mp4v2dll->GetFunctionAddress("MP4SetTrackIntegerProperty"); |
| 360 | |
| 361 | if (ex_MP4Modify == NIL || |
| 362 | ex_MP4Close == NIL || |
| 363 | ex_MP4Optimize == NIL || |
| 364 | |
| 365 | ex_MP4FindTrackId == NIL || |
| 366 | ex_MP4SetTrackIntegerProperty == NIL) { FreeMP4v2DLL(); return False; } |
| 367 | |
| 368 | return True; |
| 369 | } |
| 370 | |
| 371 | Void FreeMP4v2DLL() |
| 372 | { |
| 373 | BoCA::Utilities::FreeCodecDLL(mp4v2dll); |
| 374 | |
| 375 | mp4v2dll = NIL; |
| 376 | } |