| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2022 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 "wma.h" |
| 17 | #include "config.h" |
| 18 | |
| 19 | const String &BoCA::EncoderWMA::GetComponentSpecs() |
| 20 | { |
| 21 | static String componentSpecs; |
| 22 | |
| 23 | if (wmvcoredll != NIL) |
| 24 | { |
| 25 | componentSpecs = " \ |
| 26 | \ |
| 27 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 28 | <component> \ |
| 29 | <name>Windows Media Audio Encoder</name> \ |
| 30 | <version>1.0</version> \ |
| 31 | <id>wma-enc</id> \ |
| 32 | <type>encoder</type> \ |
| 33 | \ |
| 34 | "; |
| 35 | |
| 36 | Initialize(); |
| 37 | |
| 38 | IWMProfileManager *profileManager = NIL; |
| 39 | IWMCodecInfo3 *codecInfo = NIL; |
| 40 | |
| 41 | ex_WMCreateProfileManager(&profileManager); |
| 42 | |
| 43 | if (SUCCEEDED(profileManager->QueryInterface(IID_IWMCodecInfo3, (void **) &codecInfo))) |
| 44 | { |
| 45 | DWORD numCodecs = 0; |
| 46 | |
| 47 | codecInfo->GetCodecInfoCount(WMMEDIATYPE_Audio, &numCodecs); |
| 48 | |
| 49 | for (DWORD i = 0; i < numCodecs; i++) |
| 50 | { |
| 51 | DWORD nameLen = 0; |
| 52 | |
| 53 | codecInfo->GetCodecName(WMMEDIATYPE_Audio, i, NIL, &nameLen); |
| 54 | |
| 55 | WCHAR *name = new WCHAR [nameLen]; |
| 56 | |
| 57 | codecInfo->GetCodecName(WMMEDIATYPE_Audio, i, name, &nameLen); |
| 58 | |
| 59 | componentSpecs.Append(" \ |
| 60 | \ |
| 61 | <format> \ |
| 62 | <name>").Append(name).Append("</name> \ |
| 63 | <extension>wma</extension> \ |
| 64 | <tag id=\"wma-tag\" mode=\"other\">WMA Metadata</tag> \ |
| 65 | </format> \ |
| 66 | \ |
| 67 | "); |
| 68 | |
| 69 | delete [] name; |
| 70 | } |
| 71 | |
| 72 | codecInfo->Release(); |
| 73 | } |
| 74 | |
| 75 | profileManager->Release(); |
| 76 | |
| 77 | Cleanup(); |
| 78 | |
| 79 | componentSpecs.Append(" \ |
| 80 | \ |
| 81 | <input bits=\"8\" signed=\"false\"/> \ |
| 82 | <input bits=\"16-32\"/> \ |
| 83 | <parameters> \ |
| 84 | <selection name=\"Target format\" argument=\"-f %VALUE\" default=\"standard\"> \ |
| 85 | <option alias=\"WMA Standard\">standard</option> \ |
| 86 | <option alias=\"WMA Pro\">pro</option> \ |
| 87 | <option alias=\"WMA Lossless\">lossless</option> \ |
| 88 | <option alias=\"WMA Voice\">voice</option> \ |
| 89 | <option alias=\"Uncompressed\">uncompressed</option> \ |
| 90 | </selection> \ |
| 91 | <range name=\"VBR quality\" argument=\"-q %VALUE\" default=\"90\"> \ |
| 92 | <min alias=\"min\">0</min> \ |
| 93 | <max alias=\"max\">100</max> \ |
| 94 | </range> \ |
| 95 | <switch name=\"Use CBR mode\" argument=\"--cbr\"/> \ |
| 96 | <selection name=\"CBR bitrate\" argument=\"-b %VALUE\" default=\"128\"> \ |
| 97 | <option alias=\"32 kbps\">32</option> \ |
| 98 | <option alias=\"48 kbps\">48</option> \ |
| 99 | <option alias=\"64 kbps\">64</option> \ |
| 100 | <option alias=\"80 kbps\">80</option> \ |
| 101 | <option alias=\"96 kbps\">96</option> \ |
| 102 | <option alias=\"128 kbps\">128</option> \ |
| 103 | <option alias=\"160 kbps\">160</option> \ |
| 104 | <option alias=\"192 kbps\">192</option> \ |
| 105 | </selection> \ |
| 106 | </parameters> \ |
| 107 | </component> \ |
| 108 | \ |
| 109 | "); |
| 110 | } |
| 111 | |
| 112 | return componentSpecs; |
| 113 | } |
| 114 | |
| 115 | Void smooth::AttachDLL(Void *instance) |
| 116 | { |
| 117 | LoadWMVCoreDLL(); |
| 118 | |
| 119 | /* Register initialization and cleanup handlers. |
| 120 | */ |
| 121 | if (wmvcoredll != NIL) |
| 122 | { |
| 123 | BoCA::Engine *engine = BoCA::Engine::Get(); |
| 124 | |
| 125 | engine->onInitialize.Connect(&BoCA::EncoderWMA::Initialize); |
| 126 | engine->onCleanup.Connect(&BoCA::EncoderWMA::Cleanup); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | Void smooth::DetachDLL() |
| 131 | { |
| 132 | /* Unregister initialization and cleanup handlers. |
| 133 | */ |
| 134 | if (wmvcoredll != NIL) |
| 135 | { |
| 136 | BoCA::Engine *engine = BoCA::Engine::Get(); |
| 137 | |
| 138 | engine->onInitialize.Disconnect(&BoCA::EncoderWMA::Initialize); |
| 139 | engine->onCleanup.Disconnect(&BoCA::EncoderWMA::Cleanup); |
| 140 | } |
| 141 | |
| 142 | FreeWMVCoreDLL(); |
| 143 | } |
| 144 | |
| 145 | Void BoCA::EncoderWMA::Initialize() |
| 146 | { |
| 147 | /* Init the Microsoft COM library. |
| 148 | */ |
| 149 | CoInitialize(NIL); |
| 150 | } |
| 151 | |
| 152 | Void BoCA::EncoderWMA::Cleanup() |
| 153 | { |
| 154 | /* Uninit the Microsoft COM library. |
| 155 | */ |
| 156 | CoUninitialize(); |
| 157 | } |
| 158 | |
| 159 | BoCA::EncoderWMA::EncoderWMA() |
| 160 | { |
| 161 | configLayer = NIL; |
| 162 | config = NIL; |
| 163 | |
| 164 | writer = NIL; |
| 165 | writerAdvanced = NIL; |
| 166 | |
| 167 | writerFileSink = NIL; |
| 168 | |
| 169 | profileManager = NIL; |
| 170 | profile = NIL; |
| 171 | |
| 172 | streamConfig = NIL; |
| 173 | |
| 174 | samplesWritten = 0; |
| 175 | } |
| 176 | |
| 177 | BoCA::EncoderWMA::~EncoderWMA() |
| 178 | { |
| 179 | if (config != NIL) Config::Free(config); |
| 180 | |
| 181 | if (configLayer != NIL) Object::DeleteObject(configLayer); |
| 182 | } |
| 183 | |
| 184 | Int BoCA::EncoderWMA::GetNumberOfPasses() const |
| 185 | { |
| 186 | /* 2-pass encoding is not supported, yet. |
| 187 | */ |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | Bool BoCA::EncoderWMA::IsLossless() const |
| 192 | { |
| 193 | const Config *config = GetConfiguration(); |
| 194 | |
| 195 | if (config->GetIntValue(ConfigureWMA::ConfigID, "Uncompressed", False)) return True; |
| 196 | |
| 197 | /* Create profile manager and get codec info. |
| 198 | */ |
| 199 | IWMProfileManager *pProfileManager = NIL; |
| 200 | IWMCodecInfo3 *pCodecInfo = NIL; |
| 201 | |
| 202 | if (FAILED(ex_WMCreateProfileManager(&pProfileManager))) return False; |
| 203 | |
| 204 | if (FAILED(pProfileManager->QueryInterface(IID_IWMCodecInfo3, (void **) &pCodecInfo))) |
| 205 | { |
| 206 | pProfileManager->Release(); |
| 207 | |
| 208 | return False; |
| 209 | } |
| 210 | |
| 211 | Int defaultCodec = GetDefaultCodec(pCodecInfo); |
| 212 | |
| 213 | /* Get and check codec name. |
| 214 | */ |
| 215 | DWORD nameLen = 0; |
| 216 | |
| 217 | pCodecInfo->GetCodecName(WMMEDIATYPE_Audio, config->GetIntValue(ConfigureWMA::ConfigID, "Codec", defaultCodec), NIL, &nameLen); |
| 218 | |
| 219 | WCHAR *name = new WCHAR [nameLen]; |
| 220 | |
| 221 | pCodecInfo->GetCodecName(WMMEDIATYPE_Audio, config->GetIntValue(ConfigureWMA::ConfigID, "Codec", defaultCodec), name, &nameLen); |
| 222 | |
| 223 | Bool result = False; |
| 224 | |
| 225 | if (String(name).Contains("Lossless")) result = True; |
| 226 | |
| 227 | delete [] name; |
| 228 | |
| 229 | /* Release profile manager and codec info. |
| 230 | */ |
| 231 | pCodecInfo->Release(); |
| 232 | pProfileManager->Release(); |
| 233 | |
| 234 | return result; |
| 235 | } |
| 236 | |
| 237 | Bool BoCA::EncoderWMA::Activate() |
| 238 | { |
| 239 | /* Get configuration. |
| 240 | */ |
| 241 | config = Config::Copy(GetConfiguration()); |
| 242 | |
| 243 | ConvertArguments(config); |
| 244 | |
| 245 | /* Close output file as it will be written directly by Windows Media. |
| 246 | */ |
| 247 | driver->Close(); |
| 248 | |
| 249 | /* Create WMA writer object. |
| 250 | */ |
| 251 | if (FAILED(ex_WMCreateWriter(NIL, &writer))) return False; |
| 252 | |
| 253 | if (FAILED(writer->QueryInterface(IID_IWMWriterAdvanced, (void **) &writerAdvanced))) |
| 254 | { |
| 255 | writer->Release(); |
| 256 | |
| 257 | return False; |
| 258 | } |
| 259 | |
| 260 | /* A temporary file is needed if the path exceeds 255 characters. |
| 261 | */ |
| 262 | String outputFile = Directory::MakeExtendedPath(File(track.outputFile)); |
| 263 | |
| 264 | if (track.outputFile.Length() > 255) outputFile = Utilities::GetNonUnicodeTempFileName(track.outputFile); |
| 265 | |
| 266 | /* Create and open file sink. |
| 267 | */ |
| 268 | ex_WMCreateWriterFileSink(&writerFileSink); |
| 269 | |
| 270 | writerFileSink->Open(outputFile); |
| 271 | |
| 272 | writerAdvanced->AddSink(writerFileSink); |
| 273 | |
| 274 | /* Create profile manager. |
| 275 | */ |
| 276 | ex_WMCreateProfileManager(&profileManager); |
| 277 | |
| 278 | profileManager->CreateEmptyProfile(WMT_VER_9_0, &profile); |
| 279 | |
| 280 | /* Query codec info. |
| 281 | */ |
| 282 | IWMCodecInfo3 *pCodecInfo = NIL; |
| 283 | |
| 284 | profileManager->QueryInterface(IID_IWMCodecInfo3, (void **) &pCodecInfo); |
| 285 | |
| 286 | Int codec = config->GetIntValue(ConfigureWMA::ConfigID, "Codec", GetDefaultCodec(pCodecInfo)); |
| 287 | Int codecFormat = config->GetIntValue(ConfigureWMA::ConfigID, "CodecFormat", 0); |
| 288 | Bool uncompressedFormat = config->GetIntValue(ConfigureWMA::ConfigID, "Uncompressed", False); |
| 289 | Bool autoSelectFormat = config->GetIntValue(ConfigureWMA::ConfigID, "AutoSelectFormat", True); |
| 290 | Bool enableVBR = config->GetIntValue(ConfigureWMA::ConfigID, "EnableVBR", True); |
| 291 | Bool enable2Pass = config->GetIntValue(ConfigureWMA::ConfigID, "Enable2Pass", False); |
| 292 | |
| 293 | DWORD numCodecs = 0; |
| 294 | |
| 295 | pCodecInfo->GetCodecInfoCount(WMMEDIATYPE_Audio, &numCodecs); |
| 296 | |
| 297 | if (codec >= (Int) numCodecs) |
| 298 | { |
| 299 | codec = GetDefaultCodec(pCodecInfo); |
| 300 | autoSelectFormat = True; |
| 301 | } |
| 302 | |
| 303 | if (codec == -1 || numCodecs == 0) uncompressedFormat = True; |
| 304 | |
| 305 | /* Create stream configuration. |
| 306 | */ |
| 307 | if (uncompressedFormat) |
| 308 | { |
| 309 | profile->CreateNewStream(WMMEDIATYPE_Audio, &streamConfig); |
| 310 | } |
| 311 | else if (!autoSelectFormat) |
| 312 | { |
| 313 | BOOL falseValue = FALSE; |
| 314 | BOOL trueValue = TRUE; |
| 315 | DWORD oneValue = 1; |
| 316 | DWORD twoValue = 2; |
| 317 | |
| 318 | if (enableVBR) pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codec, g_wszVBREnabled, WMT_TYPE_BOOL, (BYTE *) &trueValue, sizeof(BOOL)); |
| 319 | else pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codec, g_wszVBREnabled, WMT_TYPE_BOOL, (BYTE *) &falseValue, sizeof(BOOL)); |
| 320 | |
| 321 | if (enable2Pass) pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codec, g_wszNumPasses, WMT_TYPE_DWORD, (BYTE *) &twoValue, sizeof(DWORD)); |
| 322 | else pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codec, g_wszNumPasses, WMT_TYPE_DWORD, (BYTE *) &oneValue, sizeof(DWORD)); |
| 323 | |
| 324 | pCodecInfo->GetCodecFormat(WMMEDIATYPE_Audio, codec, codecFormat, &streamConfig); |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | streamConfig = GetBestCodecFormat(pCodecInfo, codec, track.GetFormat()); |
| 329 | } |
| 330 | |
| 331 | /* Check stream configuration. |
| 332 | */ |
| 333 | if (!streamConfig) |
| 334 | { |
| 335 | errorState = True; |
| 336 | errorString = "Could not create codec configuration."; |
| 337 | } |
| 338 | |
| 339 | /* Set up profile and writer. |
| 340 | */ |
| 341 | if (!errorState) |
| 342 | { |
| 343 | streamConfig->SetStreamNumber(1); |
| 344 | streamConfig->SetStreamName(String("Audio")); |
| 345 | streamConfig->SetConnectionName(String("Audio")); |
| 346 | |
| 347 | profile->AddStream(streamConfig); |
| 348 | |
| 349 | writer->SetProfile(profile); |
| 350 | |
| 351 | if (SetInputFormat(writer, track.GetFormat()) == False) |
| 352 | { |
| 353 | errorState = True; |
| 354 | errorString = "Incompatible sample format."; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | pCodecInfo->Release(); |
| 359 | |
| 360 | /* Start writing output file. |
| 361 | */ |
| 362 | if (!errorState) |
| 363 | { |
| 364 | if (FAILED(writer->BeginWriting())) |
| 365 | { |
| 366 | errorState = True; |
| 367 | errorString = "Could not initialize encoder."; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /* Release objects in case of an error. |
| 372 | */ |
| 373 | if (errorState) |
| 374 | { |
| 375 | if (streamConfig) streamConfig->Release(); |
| 376 | |
| 377 | profile->Release(); |
| 378 | profileManager->Release(); |
| 379 | |
| 380 | writerFileSink->Release(); |
| 381 | |
| 382 | writerAdvanced->Release(); |
| 383 | writer->Release(); |
| 384 | } |
| 385 | |
| 386 | return !errorState; |
| 387 | } |
| 388 | |
| 389 | Bool BoCA::EncoderWMA::Deactivate() |
| 390 | { |
| 391 | String outputFile = track.outputFile; |
| 392 | |
| 393 | if (track.outputFile.Length() > 255) outputFile = Utilities::GetNonUnicodeTempFileName(track.outputFile); |
| 394 | |
| 395 | /* Delete output file in case of an error. |
| 396 | */ |
| 397 | if (errorState) |
| 398 | { |
| 399 | File(outputFile).Delete(); |
| 400 | |
| 401 | return True; |
| 402 | } |
| 403 | |
| 404 | /* Finish writing output file. |
| 405 | */ |
| 406 | HRESULT hr = writer->EndWriting(); |
| 407 | |
| 408 | writerAdvanced->RemoveSink(writerFileSink); |
| 409 | |
| 410 | streamConfig->Release(); |
| 411 | |
| 412 | profile->Release(); |
| 413 | profileManager->Release(); |
| 414 | |
| 415 | writerFileSink->Release(); |
| 416 | |
| 417 | writerAdvanced->Release(); |
| 418 | writer->Release(); |
| 419 | |
| 420 | if (FAILED(hr)) |
| 421 | { |
| 422 | File(outputFile).Delete(); |
| 423 | |
| 424 | return False; |
| 425 | } |
| 426 | |
| 427 | /* Write metadata to file. |
| 428 | */ |
| 429 | if (config->GetIntValue("Tags", "EnableWMAMetadata", True)) |
| 430 | { |
| 431 | const Info &info = track.GetInfo(); |
| 432 | |
| 433 | if (info.HasBasicInfo() || (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True))) |
| 434 | { |
| 435 | AS::Registry &boca = AS::Registry::Get(); |
| 436 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("wma-tag"); |
| 437 | |
| 438 | if (tagger != NIL) |
| 439 | { |
| 440 | tagger->SetConfiguration(config); |
| 441 | tagger->RenderStreamInfo(outputFile, track); |
| 442 | |
| 443 | boca.DeleteComponent(tagger); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /* Move actual output file in place. |
| 449 | */ |
| 450 | if (outputFile != track.outputFile) |
| 451 | { |
| 452 | File(track.outputFile).Delete(); |
| 453 | File(outputFile).Move(track.outputFile); |
| 454 | } |
| 455 | |
| 456 | return True; |
| 457 | } |
| 458 | |
| 459 | Int BoCA::EncoderWMA::WriteData(Buffer<UnsignedByte> &data) |
| 460 | { |
| 461 | INSSBuffer *pSample = NIL; |
| 462 | |
| 463 | if (FAILED(writer->AllocateSample(data.Size(), &pSample))) return -1; |
| 464 | |
| 465 | BYTE *buffer = NIL; |
| 466 | |
| 467 | if (!FAILED(pSample->GetBuffer(&buffer))) |
| 468 | { |
| 469 | memcpy(buffer, data, data.Size()); |
| 470 | |
| 471 | pSample->SetLength(data.Size()); |
| 472 | |
| 473 | const Format &format = track.GetFormat(); |
| 474 | QWORD cnsSampleTime = samplesWritten * 10000000 / format.channels / format.rate; |
| 475 | |
| 476 | HRESULT hr = writer->WriteSample(0, cnsSampleTime, WM_SF_CLEANPOINT, pSample); |
| 477 | |
| 478 | pSample->Release(); |
| 479 | |
| 480 | if (FAILED(hr)) return -1; |
| 481 | |
| 482 | samplesWritten += data.Size() / (format.bits / 8); |
| 483 | } |
| 484 | |
| 485 | return data.Size(); |
| 486 | } |
| 487 | |
| 488 | Bool BoCA::EncoderWMA::NextPass() |
| 489 | { |
| 490 | /* 2-pass encoding is not supported, yet. |
| 491 | */ |
| 492 | return False; |
| 493 | } |
| 494 | |
| 495 | Bool BoCA::EncoderWMA::SetOutputFormat(Int n) |
| 496 | { |
| 497 | Config *config = Config::Get(); |
| 498 | |
| 499 | config->SetIntValue(ConfigureWMA::ConfigID, "Uncompressed", False); |
| 500 | config->SetIntValue(ConfigureWMA::ConfigID, "Codec", n); |
| 501 | |
| 502 | return True; |
| 503 | } |
| 504 | |
| 505 | Bool BoCA::EncoderWMA::ConvertArguments(Config *config) |
| 506 | { |
| 507 | if (!config->GetIntValue("Settings", "EnableConsole", False)) return False; |
| 508 | |
| 509 | static const String encoderID = "wma-enc"; |
| 510 | |
| 511 | /* Create codec info object. |
| 512 | */ |
| 513 | IWMProfileManager *pProfileManager = NIL; |
| 514 | IWMCodecInfo3 *pCodecInfo = NIL; |
| 515 | |
| 516 | if (FAILED(ex_WMCreateProfileManager(&pProfileManager))) return False; |
| 517 | |
| 518 | if (FAILED(pProfileManager->QueryInterface(IID_IWMCodecInfo3, (void **) &pCodecInfo))) |
| 519 | { |
| 520 | pProfileManager->Release(); |
| 521 | |
| 522 | return False; |
| 523 | } |
| 524 | |
| 525 | /* Set default values. |
| 526 | */ |
| 527 | if (!config->GetIntValue("Settings", "UserSpecifiedConfig", False)) |
| 528 | { |
| 529 | config->SetIntValue(ConfigureWMA::ConfigID, "Uncompressed", False); |
| 530 | config->SetIntValue(ConfigureWMA::ConfigID, "AutoSelectFormat", True); |
| 531 | |
| 532 | config->SetIntValue(ConfigureWMA::ConfigID, "EnableVBR", True); |
| 533 | |
| 534 | config->SetIntValue(ConfigureWMA::ConfigID, "Bitrate", 128); |
| 535 | config->SetIntValue(ConfigureWMA::ConfigID, "Quality", 90); |
| 536 | |
| 537 | config->SetIntValue(ConfigureWMA::ConfigID, "Codec", GetDefaultCodec(pCodecInfo)); |
| 538 | } |
| 539 | |
| 540 | /* Get number of codecs. |
| 541 | */ |
| 542 | DWORD numCodecs = 0; |
| 543 | |
| 544 | pCodecInfo->GetCodecInfoCount(WMMEDIATYPE_Audio, &numCodecs); |
| 545 | |
| 546 | /* Get command line settings. |
| 547 | */ |
| 548 | Bool useCBR = config->GetIntValue(encoderID, "Use CBR mode", !config->GetIntValue(ConfigureWMA::ConfigID, "EnableVBR", True)); |
| 549 | |
| 550 | Int bitrate = config->GetIntValue(ConfigureWMA::ConfigID, "Bitrate", 128); |
| 551 | Int quality = config->GetIntValue(ConfigureWMA::ConfigID, "Quality", 90); |
| 552 | |
| 553 | Int format = config->GetIntValue(ConfigureWMA::ConfigID, "Codec", GetDefaultCodec(pCodecInfo)); |
| 554 | String formatName = config->GetIntValue(ConfigureWMA::ConfigID, "Uncompressed", False) ? "uncompressed" : "standard"; |
| 555 | |
| 556 | if (formatName != "uncompressed") |
| 557 | { |
| 558 | DWORD nameLen = 0; |
| 559 | |
| 560 | pCodecInfo->GetCodecName(WMMEDIATYPE_Audio, format, NIL, &nameLen); |
| 561 | |
| 562 | WCHAR *name = new WCHAR [nameLen]; |
| 563 | |
| 564 | pCodecInfo->GetCodecName(WMMEDIATYPE_Audio, format, name, &nameLen); |
| 565 | |
| 566 | if (String(name).Contains("Pro")) formatName = "pro"; |
| 567 | else if (String(name).Contains("Lossless")) formatName = "lossless"; |
| 568 | else if (String(name).Contains("Voice")) formatName = "voice"; |
| 569 | } |
| 570 | |
| 571 | if (config->GetIntValue(encoderID, "Set CBR bitrate", False)) bitrate = config->GetIntValue(encoderID, "CBR bitrate", bitrate); |
| 572 | if (config->GetIntValue(encoderID, "Set VBR quality", False)) quality = config->GetIntValue(encoderID, "VBR quality", quality); |
| 573 | if (config->GetIntValue(encoderID, "Set Target format", False)) formatName = config->GetStringValue(encoderID, "Target format", formatName).ToLower(); |
| 574 | |
| 575 | /* Set configuration values. |
| 576 | */ |
| 577 | config->SetIntValue(ConfigureWMA::ConfigID, "Uncompressed", formatName == "uncompressed"); |
| 578 | |
| 579 | config->SetIntValue(ConfigureWMA::ConfigID, "EnableVBR", !useCBR); |
| 580 | |
| 581 | config->SetIntValue(ConfigureWMA::ConfigID, "Bitrate", Math::Max(32, Math::Min(192, bitrate))); |
| 582 | config->SetIntValue(ConfigureWMA::ConfigID, "Quality", Math::Max(0, Math::Min(100, quality))); |
| 583 | |
| 584 | for (DWORD i = 0; i < numCodecs; i++) |
| 585 | { |
| 586 | DWORD nameLen = 0; |
| 587 | |
| 588 | pCodecInfo->GetCodecName(WMMEDIATYPE_Audio, i, NIL, &nameLen); |
| 589 | |
| 590 | WCHAR *name = new WCHAR [nameLen]; |
| 591 | |
| 592 | pCodecInfo->GetCodecName(WMMEDIATYPE_Audio, i, name, &nameLen); |
| 593 | |
| 594 | if (formatName == "pro" && String(name).Contains("Pro")) format = i; |
| 595 | else if (formatName == "lossless" && String(name).Contains("Lossless")) format = i; |
| 596 | else if (formatName == "voice" && String(name).Contains("Voice")) format = i; |
| 597 | |
| 598 | delete [] name; |
| 599 | } |
| 600 | |
| 601 | config->SetIntValue(ConfigureWMA::ConfigID, "Codec", format); |
| 602 | |
| 603 | /* Release codec info object. |
| 604 | */ |
| 605 | pCodecInfo->Release(); |
| 606 | pProfileManager->Release(); |
| 607 | |
| 608 | return True; |
| 609 | } |
| 610 | |
| 611 | ConfigLayer *BoCA::EncoderWMA::GetConfigurationLayer() |
| 612 | { |
| 613 | if (configLayer == NIL) configLayer = new ConfigureWMA(); |
| 614 | |
| 615 | return configLayer; |
| 616 | } |
| 617 | |
| 618 | /* Select default codec to be used when no codec is set. |
| 619 | */ |
| 620 | Int BoCA::EncoderWMA::GetDefaultCodec(IWMCodecInfo3 *codecInfo) |
| 621 | { |
| 622 | Int index = -1; |
| 623 | |
| 624 | DWORD numCodecs = 0; |
| 625 | HRESULT hr = codecInfo->GetCodecInfoCount(WMMEDIATYPE_Audio, &numCodecs); |
| 626 | |
| 627 | for (DWORD i = 0; i < numCodecs; i++) |
| 628 | { |
| 629 | DWORD nameLen = 0; |
| 630 | |
| 631 | codecInfo->GetCodecName(WMMEDIATYPE_Audio, i, NIL, &nameLen); |
| 632 | |
| 633 | WCHAR *name = new WCHAR [nameLen]; |
| 634 | |
| 635 | codecInfo->GetCodecName(WMMEDIATYPE_Audio, i, name, &nameLen); |
| 636 | |
| 637 | if ( String(name).Contains("Windows Media Audio") && |
| 638 | !String(name).Contains("Voice") && |
| 639 | !String(name).Contains("Lossless") && |
| 640 | !String(name).Contains("Pro")) index = i; |
| 641 | |
| 642 | delete [] name; |
| 643 | } |
| 644 | |
| 645 | if (hr != S_OK) index = -1; |
| 646 | |
| 647 | return index; |
| 648 | } |
| 649 | |
| 650 | /* This method will return the format best matching |
| 651 | * our requirements for a specified codec. |
| 652 | */ |
| 653 | IWMStreamConfig *BoCA::EncoderWMA::GetBestCodecFormat(IWMCodecInfo3 *pCodecInfo, DWORD codecIndex, const Format &format) const |
| 654 | { |
| 655 | BOOL falseValue = FALSE; |
| 656 | BOOL trueValue = TRUE; |
| 657 | DWORD oneValue = 1; |
| 658 | DWORD twoValue = 2; |
| 659 | |
| 660 | Bool supportCBR = False; |
| 661 | Bool supportVBR = False; |
| 662 | Bool support1Pass = True; |
| 663 | Bool support2Pass = False; |
| 664 | |
| 665 | /* Check if CBR is supported. |
| 666 | */ |
| 667 | { |
| 668 | pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codecIndex, g_wszVBREnabled, WMT_TYPE_BOOL, (BYTE *) &falseValue, sizeof(BOOL)); |
| 669 | pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codecIndex, g_wszNumPasses, WMT_TYPE_DWORD, (BYTE *) &oneValue, sizeof(DWORD)); |
| 670 | |
| 671 | DWORD numFormats = 0; |
| 672 | HRESULT hr = pCodecInfo->GetCodecFormatCount(WMMEDIATYPE_Audio, codecIndex, &numFormats); |
| 673 | |
| 674 | if (!FAILED(hr) && numFormats > 0) supportCBR = True; |
| 675 | } |
| 676 | |
| 677 | /* Check if VBR is supported. |
| 678 | */ |
| 679 | { |
| 680 | WMT_ATTR_DATATYPE valueType = WMT_TYPE_BOOL; |
| 681 | DWORD valueSize = sizeof(BOOL); |
| 682 | BOOL isVBRSupported = FALSE; |
| 683 | |
| 684 | pCodecInfo->GetCodecProp(WMMEDIATYPE_Audio, codecIndex, g_wszIsVBRSupported, &valueType, (BYTE *) &isVBRSupported, &valueSize); |
| 685 | |
| 686 | if (isVBRSupported) |
| 687 | { |
| 688 | pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codecIndex, g_wszVBREnabled, WMT_TYPE_BOOL, (BYTE *) &trueValue, sizeof(BOOL)); |
| 689 | pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codecIndex, g_wszNumPasses, WMT_TYPE_DWORD, (BYTE *) &oneValue, sizeof(DWORD)); |
| 690 | |
| 691 | DWORD numFormats = 0; |
| 692 | HRESULT hr = pCodecInfo->GetCodecFormatCount(WMMEDIATYPE_Audio, codecIndex, &numFormats); |
| 693 | |
| 694 | if (!FAILED(hr) && numFormats > 0) supportVBR = True; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | if (supportVBR && (!supportCBR || config->GetIntValue(ConfigureWMA::ConfigID, "EnableVBR", True))) pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codecIndex, g_wszVBREnabled, WMT_TYPE_BOOL, (BYTE *) &trueValue, sizeof(BOOL)); |
| 699 | else pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codecIndex, g_wszVBREnabled, WMT_TYPE_BOOL, (BYTE *) &falseValue, sizeof(BOOL)); |
| 700 | |
| 701 | if (support2Pass && (!support1Pass || config->GetIntValue(ConfigureWMA::ConfigID, "Enable2Pass", False))) pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codecIndex, g_wszNumPasses, WMT_TYPE_DWORD, (BYTE *) &twoValue, sizeof(DWORD)); |
| 702 | else pCodecInfo->SetCodecEnumerationSetting(WMMEDIATYPE_Audio, codecIndex, g_wszNumPasses, WMT_TYPE_DWORD, (BYTE *) &oneValue, sizeof(DWORD)); |
| 703 | |
| 704 | IWMStreamConfig *result = NIL; |
| 705 | |
| 706 | Int targetBitrate = config->GetIntValue(ConfigureWMA::ConfigID, "Bitrate", 128) * 1000; |
| 707 | Int targetQuality = config->GetIntValue(ConfigureWMA::ConfigID, "Quality", 90); |
| 708 | |
| 709 | Bool useVBR = (supportVBR && config->GetIntValue(ConfigureWMA::ConfigID, "EnableVBR", True)); |
| 710 | |
| 711 | DWORD bestMatchBitrate = 100000000; |
| 712 | DWORD bestMatchQuality = 100000000; |
| 713 | DWORD bestMatchSampleRate = 100000000; |
| 714 | DWORD bestMatchBits = 100000000; |
| 715 | DWORD bestMatchChannels = 100000000; |
| 716 | |
| 717 | DWORD numFormats = 0; |
| 718 | |
| 719 | pCodecInfo->GetCodecFormatCount(WMMEDIATYPE_Audio, codecIndex, &numFormats); |
| 720 | |
| 721 | for (DWORD i = 0; i < numFormats; i++) |
| 722 | { |
| 723 | IWMStreamConfig *pStreamConfig = NIL; |
| 724 | IWMMediaProps *pMediaProps = NIL; |
| 725 | |
| 726 | pCodecInfo->GetCodecFormat(WMMEDIATYPE_Audio, codecIndex, i, &pStreamConfig); |
| 727 | pStreamConfig->QueryInterface(IID_IWMMediaProps, (void **) &pMediaProps); |
| 728 | |
| 729 | DWORD formatBitrate = 0; |
| 730 | DWORD formatQuality = 0; |
| 731 | |
| 732 | pStreamConfig->GetBitrate(&formatBitrate); |
| 733 | |
| 734 | DWORD mediaTypeSize = 0; |
| 735 | |
| 736 | pMediaProps->GetMediaType(NIL, &mediaTypeSize); |
| 737 | |
| 738 | WM_MEDIA_TYPE *mediaType = (WM_MEDIA_TYPE *) new BYTE [mediaTypeSize]; |
| 739 | |
| 740 | pMediaProps->GetMediaType(mediaType, &mediaTypeSize); |
| 741 | |
| 742 | if (mediaType->majortype == WMMEDIATYPE_Audio && |
| 743 | mediaType->formattype == WMFORMAT_WaveFormatEx) |
| 744 | { |
| 745 | Bool newBestMatch = False; |
| 746 | WAVEFORMATEX *waveFormat = (WAVEFORMATEX *) mediaType->pbFormat; |
| 747 | |
| 748 | formatQuality = waveFormat->nAvgBytesPerSec & 255; |
| 749 | |
| 750 | if (Math::Abs(Int(waveFormat->nChannels - format.channels)) < Math::Abs(Int(bestMatchChannels - format.channels))) newBestMatch = True; |
| 751 | else if (Math::Abs(Int(waveFormat->nChannels - format.channels)) == Math::Abs(Int(bestMatchChannels - format.channels)) && |
| 752 | Math::Abs(Int(waveFormat->nSamplesPerSec - (Unsigned) format.rate)) < Math::Abs(Int(bestMatchSampleRate - (Unsigned) format.rate))) newBestMatch = True; |
| 753 | else if (Math::Abs(Int(waveFormat->nChannels - format.channels)) == Math::Abs(Int(bestMatchChannels - format.channels)) && |
| 754 | Math::Abs(Int(waveFormat->nSamplesPerSec - (Unsigned) format.rate)) == Math::Abs(Int(bestMatchSampleRate - (Unsigned) format.rate)) && |
| 755 | Math::Abs(Int(waveFormat->wBitsPerSample - format.bits)) < Math::Abs(Int(bestMatchBits - format.bits))) newBestMatch = True; |
| 756 | else if (Math::Abs(Int(waveFormat->nChannels - format.channels)) == Math::Abs(Int(bestMatchChannels - format.channels)) && |
| 757 | Math::Abs(Int(waveFormat->nSamplesPerSec - (Unsigned) format.rate)) == Math::Abs(Int(bestMatchSampleRate - (Unsigned) format.rate)) && |
| 758 | Math::Abs(Int(waveFormat->wBitsPerSample - format.bits)) == Math::Abs(Int(bestMatchBits - format.bits)) && |
| 759 | ((!useVBR && Math::Abs(Int(formatBitrate - targetBitrate)) < Math::Abs(Int(bestMatchBitrate - targetBitrate))) || |
| 760 | ( useVBR && Math::Abs(Int(formatQuality - targetQuality)) < Math::Abs(Int(bestMatchQuality - targetQuality))))) newBestMatch = True; |
| 761 | |
| 762 | if (newBestMatch) |
| 763 | { |
| 764 | if (result != NIL) result->Release(); |
| 765 | |
| 766 | result = pStreamConfig; |
| 767 | result->AddRef(); |
| 768 | |
| 769 | bestMatchChannels = waveFormat->nChannels; |
| 770 | bestMatchBits = waveFormat->wBitsPerSample; |
| 771 | bestMatchSampleRate = waveFormat->nSamplesPerSec; |
| 772 | bestMatchBitrate = formatBitrate; |
| 773 | bestMatchQuality = formatQuality; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | delete [] mediaType; |
| 778 | |
| 779 | pMediaProps->Release(); |
| 780 | pStreamConfig->Release(); |
| 781 | } |
| 782 | |
| 783 | return result; |
| 784 | } |
| 785 | |
| 786 | /* This method will set the input format to the |
| 787 | * specified format. |
| 788 | */ |
| 789 | Bool BoCA::EncoderWMA::SetInputFormat(IWMWriter *pWriter, const Format &format) |
| 790 | { |
| 791 | WM_MEDIA_TYPE mediaType; |
| 792 | |
| 793 | mediaType.majortype = WMMEDIATYPE_Audio; |
| 794 | mediaType.subtype = WMMEDIASUBTYPE_PCM; |
| 795 | mediaType.bFixedSizeSamples = True; |
| 796 | mediaType.bTemporalCompression = False; |
| 797 | mediaType.lSampleSize = (format.bits / 8) * format.channels; |
| 798 | mediaType.formattype = WMFORMAT_WaveFormatEx; |
| 799 | mediaType.pUnk = NIL; |
| 800 | |
| 801 | WAVEFORMATEX waveFormat; |
| 802 | |
| 803 | waveFormat.wFormatTag = WAVE_FORMAT_PCM; |
| 804 | waveFormat.nChannels = format.channels; |
| 805 | waveFormat.nSamplesPerSec = format.rate; |
| 806 | waveFormat.nAvgBytesPerSec = format.rate * (format.bits / 8) * format.channels; |
| 807 | waveFormat.nBlockAlign = (format.bits / 8) * format.channels; |
| 808 | waveFormat.wBitsPerSample = format.bits; |
| 809 | waveFormat.cbSize = 0; |
| 810 | |
| 811 | mediaType.cbFormat = sizeof(waveFormat); |
| 812 | mediaType.pbFormat = (BYTE *) &waveFormat; |
| 813 | |
| 814 | IWMInputMediaProps *pInputProps = NIL; |
| 815 | |
| 816 | if (!FAILED(pWriter->GetInputProps(0, &pInputProps))) |
| 817 | { |
| 818 | pInputProps->SetMediaType(&mediaType); |
| 819 | |
| 820 | HRESULT hr = writer->SetInputProps(0, pInputProps); |
| 821 | |
| 822 | pInputProps->Release(); |
| 823 | |
| 824 | return !FAILED(hr); |
| 825 | } |
| 826 | |
| 827 | return False; |
| 828 | } |