| 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 <smooth.h> |
| 14 | #include <smooth/dll.h> |
| 15 | |
| 16 | #include "faac.h" |
| 17 | #include "config.h" |
| 18 | |
| 19 | using namespace smooth::IO; |
| 20 | |
| 21 | const String &BoCA::EncoderFAAC::GetComponentSpecs() |
| 22 | { |
| 23 | static String componentSpecs; |
| 24 | |
| 25 | if (faacdll != NIL) |
| 26 | { |
| 27 | componentSpecs = " \ |
| 28 | \ |
| 29 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 30 | <component> \ |
| 31 | <name>FAAC MP4/AAC Encoder %VERSION%</name> \ |
| 32 | <version>1.0</version> \ |
| 33 | <id>faac-enc</id> \ |
| 34 | <type>encoder</type> \ |
| 35 | <replace>voaacenc-enc</replace> \ |
| 36 | \ |
| 37 | "; |
| 38 | |
| 39 | if (mp4v2dll != NIL) |
| 40 | { |
| 41 | componentSpecs.Append(" \ |
| 42 | \ |
| 43 | <format> \ |
| 44 | <name>MPEG-4 AAC Files</name> \ |
| 45 | <extension>m4a</extension> \ |
| 46 | <extension>m4b</extension> \ |
| 47 | <extension>m4r</extension> \ |
| 48 | <extension>mp4</extension> \ |
| 49 | <tag id=\"mp4-tag\" mode=\"other\">MP4 Metadata</tag> \ |
| 50 | </format> \ |
| 51 | \ |
| 52 | "); |
| 53 | } |
| 54 | |
| 55 | componentSpecs.Append(" \ |
| 56 | \ |
| 57 | <format> \ |
| 58 | <name>Raw AAC Files</name> \ |
| 59 | <extension>aac</extension> \ |
| 60 | <tag id=\"id3v2-tag\" mode=\"prepend\">ID3v2</tag> \ |
| 61 | </format> \ |
| 62 | <input bits=\"16\" channels=\"1-6\" \ |
| 63 | rate=\"8000,11025,12000,16000,22050,24000,32000,44100,48000,64000,88200,96000\"/> \ |
| 64 | <parameters> \ |
| 65 | <range name=\"VBR quality\" argument=\"-q %VALUE\" default=\"150\"> \ |
| 66 | <min alias=\"worst\">10</min> \ |
| 67 | <max alias=\"best\">500</max> \ |
| 68 | </range> \ |
| 69 | <range name=\"ABR bitrate per channel\" argument=\"-b %VALUE\" default=\"96\"> \ |
| 70 | <min alias=\"min\">8</min> \ |
| 71 | <max alias=\"max\">256</max> \ |
| 72 | </range> \ |
| 73 | \ |
| 74 | "); |
| 75 | |
| 76 | if (mp4v2dll != NIL) |
| 77 | { |
| 78 | componentSpecs.Append(" \ |
| 79 | \ |
| 80 | <switch name=\"Create ADTS AAC files (no MP4 container)\" argument=\"--adts\"/> \ |
| 81 | \ |
| 82 | "); |
| 83 | } |
| 84 | |
| 85 | componentSpecs.Append(" \ |
| 86 | \ |
| 87 | </parameters> \ |
| 88 | </component> \ |
| 89 | \ |
| 90 | "); |
| 91 | |
| 92 | unsigned long samplesSize; |
| 93 | unsigned long bufferSize; |
| 94 | |
| 95 | faacEncHandle faac = ex_faacEncOpen(44100, 2, &samplesSize, &bufferSize); |
| 96 | |
| 97 | componentSpecs.Replace("%VERSION%", String("v").Append(ex_faacEncGetCurrentConfiguration(faac)->name)); |
| 98 | |
| 99 | ex_faacEncClose(faac); |
| 100 | } |
| 101 | |
| 102 | return componentSpecs; |
| 103 | } |
| 104 | |
| 105 | Void smooth::AttachDLL(Void *instance) |
| 106 | { |
| 107 | LoadFAACDLL(); |
| 108 | LoadMP4v2DLL(); |
| 109 | } |
| 110 | |
| 111 | Void smooth::DetachDLL() |
| 112 | { |
| 113 | FreeFAACDLL(); |
| 114 | FreeMP4v2DLL(); |
| 115 | } |
| 116 | |
| 117 | namespace BoCA |
| 118 | { |
| 119 | int64_t MP4IO_size(void *); |
| 120 | int MP4IO_seek(void *, int64_t); |
| 121 | int MP4IO_write(void *, const void *, int64_t, int64_t *); |
| 122 | |
| 123 | static MP4IOCallbacks mp4Callbacks = { MP4IO_size, MP4IO_seek, NIL, MP4IO_write, NIL }; |
| 124 | }; |
| 125 | |
| 126 | BoCA::EncoderFAAC::EncoderFAAC() |
| 127 | { |
| 128 | configLayer = NIL; |
| 129 | config = NIL; |
| 130 | |
| 131 | mp4File = NIL; |
| 132 | mp4Track = MP4_INVALID_TRACK_ID; |
| 133 | |
| 134 | frameSize = 0; |
| 135 | |
| 136 | blockSize = 128; |
| 137 | overlap = 8; |
| 138 | |
| 139 | totalSamples = 0; |
| 140 | |
| 141 | nextWorker = 0; |
| 142 | } |
| 143 | |
| 144 | BoCA::EncoderFAAC::~EncoderFAAC() |
| 145 | { |
| 146 | if (config != NIL) Config::Free(config); |
| 147 | |
| 148 | if (configLayer != NIL) Object::DeleteObject(configLayer); |
| 149 | } |
| 150 | |
| 151 | Bool BoCA::EncoderFAAC::Activate() |
| 152 | { |
| 153 | const Format &format = track.GetFormat(); |
| 154 | |
| 155 | /* Get configuration. |
| 156 | */ |
| 157 | config = Config::Copy(GetConfiguration()); |
| 158 | |
| 159 | ConvertArguments(config); |
| 160 | |
| 161 | if (mp4v2dll == NIL) config->SetIntValue(ConfigureFAAC::ConfigID, "MP4Container", False); |
| 162 | |
| 163 | Bool mp4Container = config->GetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True); |
| 164 | Int mpegVersion = config->GetIntValue(ConfigureFAAC::ConfigID, "MPEGVersion", 0); |
| 165 | |
| 166 | /* Create FAAC encoder. |
| 167 | */ |
| 168 | unsigned long samplesSize = 0; |
| 169 | unsigned long bufferSize = 0; |
| 170 | |
| 171 | faacEncHandle handle = ex_faacEncOpen(format.rate, format.channels, &samplesSize, &bufferSize); |
| 172 | |
| 173 | /* Set encoder parameters. |
| 174 | */ |
| 175 | faacEncConfigurationPtr fConfig = ex_faacEncGetCurrentConfiguration(handle); |
| 176 | |
| 177 | fConfig->mpegVersion = mp4Container ? MPEG4 : mpegVersion; |
| 178 | fConfig->aacObjectType = LOW; |
| 179 | |
| 180 | ex_faacEncSetConfiguration(handle, fConfig); |
| 181 | |
| 182 | frameSize = samplesSize / format.channels; |
| 183 | |
| 184 | /* Check whether to use MP4 container. |
| 185 | */ |
| 186 | if (mp4Container) |
| 187 | { |
| 188 | /* Create MP4 file. |
| 189 | */ |
| 190 | uint32_t flags = (track.length >= 0xFFFF0000 || |
| 191 | track.approxLength >= 0xFFFF0000) ? MP4_CREATE_64BIT_DATA | MP4_CREATE_64BIT_TIME : 0; |
| 192 | |
| 193 | mp4File = ex_MP4CreateCallbacks(&mp4Callbacks, driver, flags); |
| 194 | mp4Track = ex_MP4AddAudioTrack(mp4File, format.rate, MP4_INVALID_DURATION, MP4_MPEG4_AUDIO_TYPE); |
| 195 | |
| 196 | ex_MP4SetAudioProfileLevel(mp4File, 0x0F); |
| 197 | ex_MP4SetTrackIntegerProperty(mp4File, mp4Track, "mdia.minf.stbl.stsd.mp4a.channels", format.channels); |
| 198 | |
| 199 | unsigned char *buffer = NIL; |
| 200 | |
| 201 | ex_faacEncGetDecoderSpecificInfo(handle, &buffer, &bufferSize); |
| 202 | |
| 203 | ex_MP4SetTrackESConfiguration(mp4File, mp4Track, (uint8_t *) buffer, bufferSize); |
| 204 | |
| 205 | totalSamples = 0; |
| 206 | } |
| 207 | |
| 208 | ex_faacEncClose(handle); |
| 209 | |
| 210 | /* Write ID3v2 tag if requested. |
| 211 | */ |
| 212 | if (mp4File == NIL && config->GetIntValue("Tags", "EnableID3v2", True) && config->GetIntValue(ConfigureFAAC::ConfigID, "AllowID3v2", False)) |
| 213 | { |
| 214 | const Info &info = track.GetInfo(); |
| 215 | |
| 216 | if (info.HasBasicInfo() || (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True))) |
| 217 | { |
| 218 | AS::Registry &boca = AS::Registry::Get(); |
| 219 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); |
| 220 | |
| 221 | if (tagger != NIL) |
| 222 | { |
| 223 | Buffer<unsigned char> id3Buffer; |
| 224 | |
| 225 | tagger->SetConfiguration(config); |
| 226 | tagger->RenderBuffer(id3Buffer, track); |
| 227 | |
| 228 | driver->WriteData(id3Buffer, id3Buffer.Size()); |
| 229 | |
| 230 | boca.DeleteComponent(tagger); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /* Get number of threads to use. |
| 236 | */ |
| 237 | Bool enableParallel = config->GetIntValue("Resources", "EnableParallelConversions", True); |
| 238 | Bool enableSuperFast = config->GetIntValue("Resources", "EnableSuperFastMode", True); |
| 239 | Int numberOfThreads = enableParallel && enableSuperFast ? config->GetIntValue("Resources", "NumberOfConversionThreads", 0) : 1; |
| 240 | |
| 241 | if (enableParallel && enableSuperFast && numberOfThreads <= 1) numberOfThreads = CPU().GetNumCores() + (CPU().GetNumLogicalCPUs() - CPU().GetNumCores()) / 2; |
| 242 | |
| 243 | /* Disable overlap if we use only one thread. |
| 244 | */ |
| 245 | if (numberOfThreads == 1) overlap = 0; |
| 246 | else overlap = 8; |
| 247 | |
| 248 | /* Start up worker threads. |
| 249 | */ |
| 250 | for (Int i = 0; i < numberOfThreads; i++) workers.Add(new SuperWorker(config, format)); |
| 251 | |
| 252 | foreach (SuperWorker *worker, workers) worker->Start(); |
| 253 | |
| 254 | return True; |
| 255 | } |
| 256 | |
| 257 | Bool BoCA::EncoderFAAC::Deactivate() |
| 258 | { |
| 259 | /* Output remaining samples to encoder. |
| 260 | */ |
| 261 | EncodeFrames(True); |
| 262 | |
| 263 | /* Tear down worker threads. |
| 264 | */ |
| 265 | foreach (SuperWorker *worker, workers) worker->Quit(); |
| 266 | foreach (SuperWorker *worker, workers) worker->Wait(); |
| 267 | foreach (SuperWorker *worker, workers) delete worker; |
| 268 | |
| 269 | workers.RemoveAll(); |
| 270 | |
| 271 | /* Finish MP4 writing. |
| 272 | */ |
| 273 | if (mp4File != NIL) |
| 274 | { |
| 275 | /* Write iTunes metadata with gapless information. |
| 276 | */ |
| 277 | MP4ItmfItem *item = ex_MP4ItmfItemAlloc("----", 1); |
| 278 | String value = String().Append(" 00000000") |
| 279 | .Append(" ").Append(Number((Int64) frameSize).ToHexString(8).ToUpper()) |
| 280 | .Append(" ").Append(Number((Int64) frameSize - totalSamples % frameSize).ToHexString(8).ToUpper()) |
| 281 | .Append(" ").Append(Number((Int64) totalSamples).ToHexString(16).ToUpper()) |
| 282 | .Append(" 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000"); |
| 283 | |
| 284 | item->mean = (char *) "com.apple.iTunes"; |
| 285 | item->name = (char *) "iTunSMPB"; |
| 286 | |
| 287 | item->dataList.elements[0].typeCode = MP4_ITMF_BT_UTF8; |
| 288 | item->dataList.elements[0].value = (uint8_t *) value.ConvertTo("UTF-8"); |
| 289 | item->dataList.elements[0].valueSize = value.Length(); |
| 290 | |
| 291 | ex_MP4ItmfAddItem(mp4File, item); |
| 292 | |
| 293 | item->mean = NIL; |
| 294 | item->name = NIL; |
| 295 | |
| 296 | item->dataList.elements[0].typeCode = MP4_ITMF_BT_IMPLICIT; |
| 297 | item->dataList.elements[0].value = NIL; |
| 298 | item->dataList.elements[0].valueSize = 0; |
| 299 | |
| 300 | ex_MP4ItmfItemFree(item); |
| 301 | |
| 302 | ex_MP4Close(mp4File, 0); |
| 303 | |
| 304 | /* Close output file as tagger needs to modify it. |
| 305 | */ |
| 306 | driver->Close(); |
| 307 | |
| 308 | /* Write metadata to file. |
| 309 | */ |
| 310 | const Info &info = track.GetInfo(); |
| 311 | |
| 312 | if (config->GetIntValue("Tags", "EnableMP4Metadata", True) && (info.HasBasicInfo() || (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True)))) |
| 313 | { |
| 314 | AS::Registry &boca = AS::Registry::Get(); |
| 315 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("mp4-tag"); |
| 316 | |
| 317 | if (tagger != NIL) |
| 318 | { |
| 319 | tagger->SetConfiguration(config); |
| 320 | tagger->RenderStreamInfo(track.outputFile, track); |
| 321 | |
| 322 | boca.DeleteComponent(tagger); |
| 323 | } |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | /* Optimize file even when no tags are written. |
| 328 | */ |
| 329 | String tempFile = String(track.outputFile).Append(".temp"); |
| 330 | |
| 331 | ex_MP4Optimize(track.outputFile.ConvertTo("UTF-8"), tempFile.ConvertTo("UTF-8")); |
| 332 | |
| 333 | File(track.outputFile).Delete(); |
| 334 | File(tempFile).Move(track.outputFile); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /* Write ID3v1 tag if requested. |
| 339 | */ |
| 340 | if (mp4File == NIL && config->GetIntValue("Tags", "EnableID3v1", False)) |
| 341 | { |
| 342 | const Info &info = track.GetInfo(); |
| 343 | |
| 344 | if (info.HasBasicInfo()) |
| 345 | { |
| 346 | AS::Registry &boca = AS::Registry::Get(); |
| 347 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v1-tag"); |
| 348 | |
| 349 | if (tagger != NIL) |
| 350 | { |
| 351 | Buffer<unsigned char> id3Buffer; |
| 352 | |
| 353 | tagger->SetConfiguration(config); |
| 354 | tagger->RenderBuffer(id3Buffer, track); |
| 355 | |
| 356 | driver->WriteData(id3Buffer, id3Buffer.Size()); |
| 357 | |
| 358 | boca.DeleteComponent(tagger); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* Update ID3v2 tag with correct chapter marks. |
| 364 | */ |
| 365 | if (mp4File == NIL && config->GetIntValue("Tags", "EnableID3v2", True) && config->GetIntValue(ConfigureFAAC::ConfigID, "AllowID3v2", False)) |
| 366 | { |
| 367 | if (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True)) |
| 368 | { |
| 369 | AS::Registry &boca = AS::Registry::Get(); |
| 370 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); |
| 371 | |
| 372 | if (tagger != NIL) |
| 373 | { |
| 374 | Buffer<unsigned char> id3Buffer; |
| 375 | |
| 376 | tagger->SetConfiguration(config); |
| 377 | tagger->RenderBuffer(id3Buffer, track); |
| 378 | |
| 379 | driver->Seek(0); |
| 380 | driver->WriteData(id3Buffer, id3Buffer.Size()); |
| 381 | |
| 382 | boca.DeleteComponent(tagger); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | return True; |
| 388 | } |
| 389 | |
| 390 | Int BoCA::EncoderFAAC::WriteData(Buffer<UnsignedByte> &data) |
| 391 | { |
| 392 | const Format &format = track.GetFormat(); |
| 393 | |
| 394 | /* Change to AAC channel order. |
| 395 | */ |
| 396 | if (format.channels == 3) Utilities::ChangeChannelOrder(data, format, Channel::Default_3_0, Channel::AAC_3_0); |
| 397 | else if (format.channels == 5) Utilities::ChangeChannelOrder(data, format, Channel::Default_5_0, Channel::AAC_5_0); |
| 398 | else if (format.channels == 6) Utilities::ChangeChannelOrder(data, format, Channel::Default_5_1, Channel::AAC_5_1); |
| 399 | |
| 400 | /* Copy data to samples buffer. |
| 401 | */ |
| 402 | Int samples = data.Size() / 2; |
| 403 | |
| 404 | samplesBuffer.Resize(samplesBuffer.Size() + samples); |
| 405 | |
| 406 | memcpy(samplesBuffer + samplesBuffer.Size() - samples, data, data.Size()); |
| 407 | |
| 408 | /* Output samples to encoder. |
| 409 | */ |
| 410 | totalSamples += data.Size() / format.channels / (format.bits / 8); |
| 411 | |
| 412 | return EncodeFrames(False); |
| 413 | } |
| 414 | |
| 415 | Int BoCA::EncoderFAAC::EncodeFrames(Bool flush) |
| 416 | { |
| 417 | const Format &format = track.GetFormat(); |
| 418 | |
| 419 | /* Pass samples to workers. |
| 420 | */ |
| 421 | Int framesToProcess = blockSize; |
| 422 | Int framesProcessed = 0; |
| 423 | Int dataLength = 0; |
| 424 | |
| 425 | Int samplesPerFrame = frameSize * format.channels; |
| 426 | |
| 427 | if (flush) framesToProcess = Math::Floor(samplesBuffer.Size() / samplesPerFrame); |
| 428 | |
| 429 | while (samplesBuffer.Size() - framesProcessed * samplesPerFrame >= samplesPerFrame * framesToProcess) |
| 430 | { |
| 431 | SuperWorker *workerToUse = workers.GetNth(nextWorker % workers.Length()); |
| 432 | |
| 433 | workerToUse->WaitUntilReady(); |
| 434 | |
| 435 | /* See if the worker has some packets for us. |
| 436 | */ |
| 437 | if (workerToUse->GetPacketSizes().Length() != 0) dataLength += ProcessPackets(workerToUse->GetPackets(), workerToUse->GetPacketSizes(), nextWorker == workers.Length()); |
| 438 | |
| 439 | /* Pass new frames to worker. |
| 440 | */ |
| 441 | workerToUse->Encode(samplesBuffer, framesProcessed * samplesPerFrame, flush ? samplesBuffer.Size() : samplesPerFrame * framesToProcess, flush); |
| 442 | |
| 443 | framesProcessed += framesToProcess - (flush ? 0 : overlap); |
| 444 | |
| 445 | nextWorker++; |
| 446 | |
| 447 | if (flush) break; |
| 448 | } |
| 449 | |
| 450 | memmove(samplesBuffer, samplesBuffer + framesProcessed * samplesPerFrame, sizeof(int16_t) * (samplesBuffer.Size() - framesProcessed * samplesPerFrame)); |
| 451 | |
| 452 | samplesBuffer.Resize(samplesBuffer.Size() - framesProcessed * samplesPerFrame); |
| 453 | |
| 454 | if (!flush) return dataLength; |
| 455 | |
| 456 | /* Wait for workers to finish and process packets. |
| 457 | */ |
| 458 | for (Int i = 0; i < workers.Length(); i++) |
| 459 | { |
| 460 | SuperWorker *workerToUse = workers.GetNth(nextWorker % workers.Length()); |
| 461 | |
| 462 | workerToUse->WaitUntilReady(); |
| 463 | |
| 464 | /* See if the worker has some packets for us. |
| 465 | */ |
| 466 | if (workerToUse->GetPacketSizes().Length() != 0) dataLength += ProcessPackets(workerToUse->GetPackets(), workerToUse->GetPacketSizes(), nextWorker == workers.Length()); |
| 467 | |
| 468 | nextWorker++; |
| 469 | } |
| 470 | |
| 471 | return dataLength; |
| 472 | } |
| 473 | |
| 474 | Int BoCA::EncoderFAAC::ProcessPackets(const Buffer<unsigned char> &packets, const Array<Int> &packetSizes, Bool first) |
| 475 | { |
| 476 | Int offset = 0; |
| 477 | Int dataLength = 0; |
| 478 | |
| 479 | if (!first) for (Int i = 0; i < overlap; i++) offset += packetSizes.GetNth(i); |
| 480 | |
| 481 | for (Int i = 0; i < packetSizes.Length(); i++) |
| 482 | { |
| 483 | if (i < overlap && !first) continue; |
| 484 | if (packetSizes.GetNth(i) == 0) continue; |
| 485 | |
| 486 | if (mp4File != NIL) ex_MP4WriteSample(mp4File, mp4Track, (uint8_t *) (unsigned char *) packets + offset, packetSizes.GetNth(i), frameSize, 0, true); |
| 487 | else driver->WriteData(packets + offset, packetSizes.GetNth(i)); |
| 488 | |
| 489 | offset += packetSizes.GetNth(i); |
| 490 | dataLength += packetSizes.GetNth(i); |
| 491 | } |
| 492 | |
| 493 | return dataLength; |
| 494 | } |
| 495 | |
| 496 | Bool BoCA::EncoderFAAC::SetOutputFormat(Int n) |
| 497 | { |
| 498 | Config *config = Config::Get(); |
| 499 | |
| 500 | if (n == 0 && mp4v2dll != NIL) |
| 501 | { |
| 502 | config->SetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True); |
| 503 | config->SetIntValue(ConfigureFAAC::ConfigID, "MPEGVersion", 0); |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | config->SetIntValue(ConfigureFAAC::ConfigID, "MP4Container", False); |
| 508 | } |
| 509 | |
| 510 | return True; |
| 511 | } |
| 512 | |
| 513 | String BoCA::EncoderFAAC::GetOutputFileExtension() const |
| 514 | { |
| 515 | const Config *config = GetConfiguration(); |
| 516 | |
| 517 | if (config->GetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True)) |
| 518 | { |
| 519 | switch (config->GetIntValue(ConfigureFAAC::ConfigID, "MP4FileExtension", 0)) |
| 520 | { |
| 521 | default: |
| 522 | case 0: return "m4a"; |
| 523 | case 1: return "m4b"; |
| 524 | case 2: return "m4r"; |
| 525 | case 3: return "mp4"; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | return "aac"; |
| 530 | } |
| 531 | |
| 532 | Bool BoCA::EncoderFAAC::ConvertArguments(Config *config) |
| 533 | { |
| 534 | if (!config->GetIntValue("Settings", "EnableConsole", False)) return False; |
| 535 | |
| 536 | static const String encoderID = "faac-enc"; |
| 537 | |
| 538 | /* Set default values. |
| 539 | */ |
| 540 | if (!config->GetIntValue("Settings", "UserSpecifiedConfig", False)) |
| 541 | { |
| 542 | config->SetIntValue(ConfigureFAAC::ConfigID, "MPEGVersion", 0); |
| 543 | |
| 544 | config->SetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True); |
| 545 | config->SetIntValue(ConfigureFAAC::ConfigID, "SetQuality", True); |
| 546 | |
| 547 | config->SetIntValue(ConfigureFAAC::ConfigID, "AACQuality", 150); |
| 548 | config->SetIntValue(ConfigureFAAC::ConfigID, "Bitrate", 96); |
| 549 | } |
| 550 | |
| 551 | /* Get command line settings. |
| 552 | */ |
| 553 | Bool rawAAC = config->GetIntValue(encoderID, "Create ADTS AAC files (no MP4 container)", !config->GetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True)); |
| 554 | Bool useABR = config->GetIntValue(encoderID, "Set ABR bitrate per channel", !config->GetIntValue(ConfigureFAAC::ConfigID, "SetQuality", True)); |
| 555 | |
| 556 | Int quality = config->GetIntValue(ConfigureFAAC::ConfigID, "AACQuality", 150); |
| 557 | Int bitrate = config->GetIntValue(ConfigureFAAC::ConfigID, "Bitrate", 96); |
| 558 | |
| 559 | if (config->GetIntValue(encoderID, "Set VBR quality", False)) quality = config->GetIntValue(encoderID, "VBR quality", quality); |
| 560 | if (config->GetIntValue(encoderID, "Set ABR bitrate per channel", False)) bitrate = config->GetIntValue(encoderID, "ABR bitrate per channel", bitrate); |
| 561 | |
| 562 | /* Set configuration values. |
| 563 | */ |
| 564 | config->SetIntValue(ConfigureFAAC::ConfigID, "MP4Container", !rawAAC); |
| 565 | config->SetIntValue(ConfigureFAAC::ConfigID, "SetQuality", !useABR); |
| 566 | |
| 567 | config->SetIntValue(ConfigureFAAC::ConfigID, "AACQuality", Math::Max(10, Math::Min(500, quality))); |
| 568 | config->SetIntValue(ConfigureFAAC::ConfigID, "Bitrate", Math::Max(8, Math::Min(256, bitrate))); |
| 569 | |
| 570 | return True; |
| 571 | } |
| 572 | |
| 573 | ConfigLayer *BoCA::EncoderFAAC::GetConfigurationLayer() |
| 574 | { |
| 575 | if (configLayer == NIL) configLayer = new ConfigureFAAC(); |
| 576 | |
| 577 | return configLayer; |
| 578 | } |
| 579 | |
| 580 | int64_t BoCA::MP4IO_size(void *handle) |
| 581 | { |
| 582 | Driver *driver = (Driver *) handle; |
| 583 | |
| 584 | return driver->GetSize(); |
| 585 | } |
| 586 | |
| 587 | int BoCA::MP4IO_seek(void *handle, int64_t pos) |
| 588 | { |
| 589 | Driver *driver = (Driver *) handle; |
| 590 | |
| 591 | if (driver->Seek(pos) == -1) return 1; |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | int BoCA::MP4IO_write(void *handle, const void *buffer, int64_t size, int64_t *nout) |
| 597 | { |
| 598 | Driver *driver = (Driver *) handle; |
| 599 | |
| 600 | *nout = driver->WriteData((const UnsignedByte *) buffer, size); |
| 601 | |
| 602 | if (*nout == 0) return 1; |
| 603 | |
| 604 | return 0; |
| 605 | } |