| 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 "fdkaac.h" |
| 17 | #include "config.h" |
| 18 | |
| 19 | using namespace smooth::IO; |
| 20 | |
| 21 | const String &BoCA::EncoderFDKAAC::GetComponentSpecs() |
| 22 | { |
| 23 | static String componentSpecs; |
| 24 | |
| 25 | if (fdkaacdll != NIL) |
| 26 | { |
| 27 | LIB_INFO info[FDK_MODULE_LAST]; |
| 28 | |
| 29 | FDKinitLibInfo(info); |
| 30 | ex_aacEncGetLibInfo(info); |
| 31 | |
| 32 | UINT sbrFlags = FDKlibInfo_getCapabilities(info, FDK_SBRENC); |
| 33 | |
| 34 | componentSpecs = " \ |
| 35 | \ |
| 36 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 37 | <component> \ |
| 38 | <name>FDK-AAC Encoder %VERSION%</name> \ |
| 39 | <version>1.0</version> \ |
| 40 | <id>fdkaac-enc</id> \ |
| 41 | <type>encoder</type> \ |
| 42 | <replace>faac-enc</replace> \ |
| 43 | <replace>voaacenc-enc</replace> \ |
| 44 | \ |
| 45 | "; |
| 46 | |
| 47 | if (mp4v2dll != NIL) |
| 48 | { |
| 49 | componentSpecs.Append(" \ |
| 50 | \ |
| 51 | <format> \ |
| 52 | <name>MPEG-4 AAC Files</name> \ |
| 53 | <extension>m4a</extension> \ |
| 54 | <extension>m4b</extension> \ |
| 55 | <extension>m4r</extension> \ |
| 56 | <extension>mp4</extension> \ |
| 57 | <tag id=\"mp4-tag\" mode=\"other\">MP4 Metadata</tag> \ |
| 58 | </format> \ |
| 59 | \ |
| 60 | "); |
| 61 | } |
| 62 | |
| 63 | componentSpecs.Append(" \ |
| 64 | \ |
| 65 | <format> \ |
| 66 | <name>Raw AAC Files</name> \ |
| 67 | <extension>aac</extension> \ |
| 68 | <tag id=\"id3v2-tag\" mode=\"prepend\">ID3v2</tag> \ |
| 69 | </format> \ |
| 70 | <input bits=\"16\" channels=\"1-6,8\" \ |
| 71 | rate=\"8000,11025,12000,16000,22050,24000,32000,44100,48000,64000,88200,96000\"/> \ |
| 72 | <parameters> \ |
| 73 | <selection name=\"AAC encoding mode\" argument=\"-m %VALUE\" default=\"LC\"> \ |
| 74 | <option alias=\"Low Complexity\">LC</option> \ |
| 75 | \ |
| 76 | "); |
| 77 | |
| 78 | if (sbrFlags) componentSpecs.Append("<option alias=\"High Efficiency\">HE</option>"); |
| 79 | if (sbrFlags & CAPF_SBR_PS_MPEG) componentSpecs.Append("<option alias=\"High Efficiency v2\">HEv2</option>"); |
| 80 | |
| 81 | componentSpecs.Append(" \ |
| 82 | \ |
| 83 | <option alias=\"Low Delay\">LD</option> \ |
| 84 | \ |
| 85 | "); |
| 86 | |
| 87 | if (sbrFlags) componentSpecs.Append("<option alias=\"Enhanced Low Delay\">ELD</option>"); |
| 88 | |
| 89 | componentSpecs.Append(" \ |
| 90 | \ |
| 91 | </selection> \ |
| 92 | <range name=\"Bitrate per channel\" argument=\"-b %VALUE\" default=\"64\"> \ |
| 93 | <min alias=\"min\">8</min> \ |
| 94 | <max alias=\"max\">256</max> \ |
| 95 | </range> \ |
| 96 | <switch name=\"Disable Afterburner processing\" argument=\"--no-afterburner\"/> \ |
| 97 | \ |
| 98 | "); |
| 99 | |
| 100 | if (mp4v2dll != NIL) |
| 101 | { |
| 102 | componentSpecs.Append(" \ |
| 103 | \ |
| 104 | <switch name=\"Create ADTS AAC files (no MP4 container)\" argument=\"--adts\"/> \ |
| 105 | \ |
| 106 | "); |
| 107 | } |
| 108 | |
| 109 | componentSpecs.Append(" \ |
| 110 | \ |
| 111 | </parameters> \ |
| 112 | </component> \ |
| 113 | \ |
| 114 | "); |
| 115 | |
| 116 | UnsignedInt32 version = GetEncoderVersion(); |
| 117 | |
| 118 | componentSpecs.Replace("%VERSION%", String("v").Append(String::FromInt((version >> 24) & 0xff)).Append(".") |
| 119 | .Append(String::FromInt((version >> 16) & 0xff)).Append(".") |
| 120 | .Append(String::FromInt((version >> 8) & 0xff))); |
| 121 | } |
| 122 | |
| 123 | return componentSpecs; |
| 124 | } |
| 125 | |
| 126 | Void smooth::AttachDLL(Void *instance) |
| 127 | { |
| 128 | LoadFDKAACDLL(); |
| 129 | LoadMP4v2DLL(); |
| 130 | } |
| 131 | |
| 132 | Void smooth::DetachDLL() |
| 133 | { |
| 134 | FreeFDKAACDLL(); |
| 135 | FreeMP4v2DLL(); |
| 136 | } |
| 137 | |
| 138 | namespace BoCA |
| 139 | { |
| 140 | int64_t MP4IO_size(void *); |
| 141 | int MP4IO_seek(void *, int64_t); |
| 142 | int MP4IO_write(void *, const void *, int64_t, int64_t *); |
| 143 | |
| 144 | static MP4IOCallbacks mp4Callbacks = { MP4IO_size, MP4IO_seek, NIL, MP4IO_write, NIL }; |
| 145 | }; |
| 146 | |
| 147 | BoCA::EncoderFDKAAC::EncoderFDKAAC() |
| 148 | { |
| 149 | configLayer = NIL; |
| 150 | config = NIL; |
| 151 | |
| 152 | mp4File = NIL; |
| 153 | mp4Track = MP4_INVALID_TRACK_ID; |
| 154 | |
| 155 | frameSize = 0; |
| 156 | granuleSize = 0; |
| 157 | |
| 158 | blockSize = 128; |
| 159 | overlap = 12; |
| 160 | |
| 161 | totalFrames = 0; |
| 162 | totalSamples = 0; |
| 163 | delaySamples = 0; |
| 164 | |
| 165 | nextWorker = 0; |
| 166 | } |
| 167 | |
| 168 | BoCA::EncoderFDKAAC::~EncoderFDKAAC() |
| 169 | { |
| 170 | if (config != NIL) Config::Free(config); |
| 171 | |
| 172 | if (configLayer != NIL) Object::DeleteObject(configLayer); |
| 173 | } |
| 174 | |
| 175 | UnsignedInt32 BoCA::EncoderFDKAAC::GetEncoderVersion() |
| 176 | { |
| 177 | LIB_INFO info[FDK_MODULE_LAST]; |
| 178 | |
| 179 | FDKinitLibInfo(info); |
| 180 | ex_aacEncGetLibInfo(info); |
| 181 | |
| 182 | for (Int i = 0; i < FDK_MODULE_LAST; i++) |
| 183 | { |
| 184 | if (info[i].module_id != FDK_AACENC) continue; |
| 185 | |
| 186 | return info[i].version; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | Bool BoCA::EncoderFDKAAC::Activate() |
| 193 | { |
| 194 | const Format &format = track.GetFormat(); |
| 195 | |
| 196 | /* Get configuration. |
| 197 | */ |
| 198 | config = Config::Copy(GetConfiguration()); |
| 199 | |
| 200 | ConvertArguments(config); |
| 201 | |
| 202 | if (mp4v2dll == NIL) config->SetIntValue(ConfigureFDKAAC::ConfigID, "MP4Container", False); |
| 203 | |
| 204 | Bool mp4Container = config->GetIntValue(ConfigureFDKAAC::ConfigID, "MP4Container", True); |
| 205 | Int mpegVersion = config->GetIntValue(ConfigureFDKAAC::ConfigID, "MPEGVersion", 0); |
| 206 | Int aacType = config->GetIntValue(ConfigureFDKAAC::ConfigID, "AACType", AOT_AAC_LC); |
| 207 | Int mode = config->GetIntValue(ConfigureFDKAAC::ConfigID, "Mode", 0); |
| 208 | Int bitrate = config->GetIntValue(ConfigureFDKAAC::ConfigID, "Bitrate", 64); |
| 209 | Int quality = config->GetIntValue(ConfigureFDKAAC::ConfigID, "Quality", 4); |
| 210 | |
| 211 | /* Fall back to HE if HEv2 is selected for non-stereo input. |
| 212 | */ |
| 213 | if (aacType == AOT_PS && format.channels != 2) aacType = AOT_SBR; |
| 214 | |
| 215 | /* Create and configure FDK AAC encoder. |
| 216 | */ |
| 217 | HANDLE_AACENCODER handle = NIL; |
| 218 | |
| 219 | ex_aacEncOpen(&handle, 0x07, format.channels); |
| 220 | |
| 221 | Int channelMode = MODE_UNKNOWN; |
| 222 | |
| 223 | switch (format.channels) |
| 224 | { |
| 225 | case 1: channelMode = MODE_1; break; |
| 226 | case 2: channelMode = MODE_2; break; |
| 227 | case 3: channelMode = MODE_1_2; break; |
| 228 | case 4: channelMode = MODE_1_2_1; break; |
| 229 | case 5: channelMode = MODE_1_2_2; break; |
| 230 | case 6: channelMode = MODE_1_2_2_1; break; |
| 231 | case 8: channelMode = MODE_1_2_2_2_1; break; |
| 232 | } |
| 233 | |
| 234 | ex_aacEncoder_SetParam(handle, AACENC_SAMPLERATE, format.rate); |
| 235 | ex_aacEncoder_SetParam(handle, AACENC_CHANNELMODE, channelMode); |
| 236 | |
| 237 | if (mode == 0) ex_aacEncoder_SetParam(handle, AACENC_BITRATE, bitrate * 1000 * format.channels); |
| 238 | else ex_aacEncoder_SetParam(handle, AACENC_BITRATEMODE, quality); |
| 239 | |
| 240 | ex_aacEncoder_SetParam(handle, AACENC_AOT, mpegVersion + aacType); |
| 241 | ex_aacEncoder_SetParam(handle, AACENC_TRANSMUX, mp4Container ? TT_MP4_RAW : TT_MP4_ADTS); |
| 242 | |
| 243 | if ( mp4Container && (aacType == AOT_SBR || aacType == AOT_PS )) ex_aacEncoder_SetParam(handle, AACENC_SIGNALING_MODE, 1); |
| 244 | if (!mp4Container && (aacType == AOT_ER_AAC_LD || aacType == AOT_ER_AAC_ELD)) ex_aacEncoder_SetParam(handle, AACENC_TRANSMUX, TT_MP4_LOAS); |
| 245 | |
| 246 | AACENC_InfoStruct aacInfo; |
| 247 | AACENC_InfoStruct_Old &aacInfoOld = *(AACENC_InfoStruct_Old *) &aacInfo; |
| 248 | |
| 249 | if (ex_aacEncEncode(handle, NULL, NULL, NULL, NULL) != AACENC_OK) |
| 250 | { |
| 251 | ex_aacEncClose(&handle); |
| 252 | return False; |
| 253 | } |
| 254 | |
| 255 | ex_aacEncInfo(handle, &aacInfo); |
| 256 | |
| 257 | frameSize = aacInfo.frameLength; |
| 258 | granuleSize = ex_aacEncoder_GetParam(handle, AACENC_GRANULE_LENGTH); |
| 259 | |
| 260 | #if AACENCODER_LIB_VL0 >= 4 |
| 261 | if (GetEncoderVersion() >= LIB_VERSION(4, 0, 0)) |
| 262 | { |
| 263 | delaySamples = aacInfo.nDelayCore; |
| 264 | } |
| 265 | else |
| 266 | #endif |
| 267 | { |
| 268 | delaySamples = aacInfoOld.encoderDelay; |
| 269 | |
| 270 | /* Adjust delay for LD/ELD object types. |
| 271 | */ |
| 272 | if (aacType == AOT_ER_AAC_LD) delaySamples = frameSize; |
| 273 | if (aacType == AOT_ER_AAC_ELD) delaySamples = frameSize / 4; |
| 274 | } |
| 275 | |
| 276 | /* Check whether to use MP4 container. |
| 277 | */ |
| 278 | if (mp4Container) |
| 279 | { |
| 280 | /* Create MP4 file. |
| 281 | */ |
| 282 | uint32_t flags = (track.length >= 0xFFFF0000 || |
| 283 | track.approxLength >= 0xFFFF0000) ? MP4_CREATE_64BIT_DATA | MP4_CREATE_64BIT_TIME : 0; |
| 284 | |
| 285 | mp4File = ex_MP4CreateCallbacks(&mp4Callbacks, driver, flags); |
| 286 | mp4Track = ex_MP4AddAudioTrack(mp4File, format.rate * granuleSize / frameSize, MP4_INVALID_DURATION, MP4_MPEG4_AUDIO_TYPE); |
| 287 | |
| 288 | ex_MP4SetAudioProfileLevel(mp4File, 0x0F); |
| 289 | ex_MP4SetTrackIntegerProperty(mp4File, mp4Track, "mdia.minf.stbl.stsd.mp4a.channels", format.channels); |
| 290 | |
| 291 | if (GetEncoderVersion() >= LIB_VERSION(4, 0, 0)) ex_MP4SetTrackESConfiguration(mp4File, mp4Track, aacInfo.confBuf, aacInfo.confSize); |
| 292 | else ex_MP4SetTrackESConfiguration(mp4File, mp4Track, aacInfoOld.confBuf, aacInfoOld.confSize); |
| 293 | |
| 294 | totalFrames = 0; |
| 295 | totalSamples = 0; |
| 296 | } |
| 297 | |
| 298 | ex_aacEncClose(&handle); |
| 299 | |
| 300 | /* Write ID3v2 tag if requested. |
| 301 | */ |
| 302 | if (mp4File == NIL && config->GetIntValue("Tags", "EnableID3v2", True) && config->GetIntValue(ConfigureFDKAAC::ConfigID, "AllowID3v2", False)) |
| 303 | { |
| 304 | const Info &info = track.GetInfo(); |
| 305 | |
| 306 | if (info.HasBasicInfo() || (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True))) |
| 307 | { |
| 308 | AS::Registry &boca = AS::Registry::Get(); |
| 309 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); |
| 310 | |
| 311 | if (tagger != NIL) |
| 312 | { |
| 313 | Buffer<unsigned char> id3Buffer; |
| 314 | |
| 315 | tagger->SetConfiguration(config); |
| 316 | tagger->RenderBuffer(id3Buffer, track); |
| 317 | |
| 318 | driver->WriteData(id3Buffer, id3Buffer.Size()); |
| 319 | |
| 320 | boca.DeleteComponent(tagger); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* Get number of threads to use. |
| 326 | */ |
| 327 | Bool enableParallel = config->GetIntValue("Resources", "EnableParallelConversions", True); |
| 328 | Bool enableSuperFast = config->GetIntValue("Resources", "EnableSuperFastMode", True); |
| 329 | Int numberOfThreads = enableParallel && enableSuperFast ? config->GetIntValue("Resources", "NumberOfConversionThreads", 0) : 1; |
| 330 | |
| 331 | if (enableParallel && enableSuperFast && numberOfThreads <= 1) numberOfThreads = CPU().GetNumCores() + (CPU().GetNumLogicalCPUs() - CPU().GetNumCores()) / 2; |
| 332 | |
| 333 | /* Disable overlap if we use only one thread. |
| 334 | */ |
| 335 | if (numberOfThreads == 1) overlap = 0; |
| 336 | else overlap = 12; |
| 337 | |
| 338 | /* Start up worker threads. |
| 339 | */ |
| 340 | for (Int i = 0; i < numberOfThreads; i++) workers.Add(new SuperWorker(config, format)); |
| 341 | |
| 342 | foreach (SuperWorker *worker, workers) worker->Start(); |
| 343 | |
| 344 | return True; |
| 345 | } |
| 346 | |
| 347 | Bool BoCA::EncoderFDKAAC::Deactivate() |
| 348 | { |
| 349 | /* Output remaining samples to encoder. |
| 350 | */ |
| 351 | EncodeFrames(True); |
| 352 | |
| 353 | /* Tear down worker threads. |
| 354 | */ |
| 355 | foreach (SuperWorker *worker, workers) worker->Quit(); |
| 356 | foreach (SuperWorker *worker, workers) worker->Wait(); |
| 357 | foreach (SuperWorker *worker, workers) delete worker; |
| 358 | |
| 359 | workers.RemoveAll(); |
| 360 | |
| 361 | /* Finish MP4 writing. |
| 362 | */ |
| 363 | if (mp4File != NIL) |
| 364 | { |
| 365 | /* Write iTunes metadata with gapless information. |
| 366 | */ |
| 367 | Float sbrRatio = Float(frameSize) / granuleSize; |
| 368 | Int64 delayValue = Math::Floor(delaySamples / sbrRatio); |
| 369 | Int64 totalValue = Math::Ceil(totalSamples / sbrRatio); |
| 370 | MP4ItmfItem *item = ex_MP4ItmfItemAlloc("----", 1); |
| 371 | String value = String().Append(" 00000000") |
| 372 | .Append(" ").Append(Number(delayValue).ToHexString(8).ToUpper()) |
| 373 | .Append(" ").Append(Number(totalFrames * granuleSize - totalValue - delayValue).ToHexString(8).ToUpper()) |
| 374 | .Append(" ").Append(Number(totalValue).ToHexString(16).ToUpper()) |
| 375 | .Append(" 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000"); |
| 376 | |
| 377 | item->mean = (char *) "com.apple.iTunes"; |
| 378 | item->name = (char *) "iTunSMPB"; |
| 379 | |
| 380 | item->dataList.elements[0].typeCode = MP4_ITMF_BT_UTF8; |
| 381 | item->dataList.elements[0].value = (uint8_t *) value.ConvertTo("UTF-8"); |
| 382 | item->dataList.elements[0].valueSize = value.Length(); |
| 383 | |
| 384 | ex_MP4ItmfAddItem(mp4File, item); |
| 385 | |
| 386 | item->mean = NIL; |
| 387 | item->name = NIL; |
| 388 | |
| 389 | item->dataList.elements[0].typeCode = MP4_ITMF_BT_IMPLICIT; |
| 390 | item->dataList.elements[0].value = NIL; |
| 391 | item->dataList.elements[0].valueSize = 0; |
| 392 | |
| 393 | ex_MP4ItmfItemFree(item); |
| 394 | |
| 395 | ex_MP4Close(mp4File, 0); |
| 396 | |
| 397 | /* Close output file as tagger needs to modify it. |
| 398 | */ |
| 399 | driver->Close(); |
| 400 | |
| 401 | /* Write metadata to file. |
| 402 | */ |
| 403 | const Info &info = track.GetInfo(); |
| 404 | |
| 405 | if (config->GetIntValue("Tags", "EnableMP4Metadata", True) && (info.HasBasicInfo() || (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True)))) |
| 406 | { |
| 407 | AS::Registry &boca = AS::Registry::Get(); |
| 408 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("mp4-tag"); |
| 409 | |
| 410 | if (tagger != NIL) |
| 411 | { |
| 412 | tagger->SetConfiguration(config); |
| 413 | tagger->RenderStreamInfo(track.outputFile, track); |
| 414 | |
| 415 | boca.DeleteComponent(tagger); |
| 416 | } |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | /* Optimize file even when no tags are written. |
| 421 | */ |
| 422 | String tempFile = String(track.outputFile).Append(".temp"); |
| 423 | |
| 424 | ex_MP4Optimize(track.outputFile.ConvertTo("UTF-8"), tempFile.ConvertTo("UTF-8")); |
| 425 | |
| 426 | File(track.outputFile).Delete(); |
| 427 | File(tempFile).Move(track.outputFile); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* Write ID3v1 tag if requested. |
| 432 | */ |
| 433 | if (mp4File == NIL && config->GetIntValue("Tags", "EnableID3v1", False)) |
| 434 | { |
| 435 | const Info &info = track.GetInfo(); |
| 436 | |
| 437 | if (info.HasBasicInfo()) |
| 438 | { |
| 439 | AS::Registry &boca = AS::Registry::Get(); |
| 440 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v1-tag"); |
| 441 | |
| 442 | if (tagger != NIL) |
| 443 | { |
| 444 | Buffer<unsigned char> id3Buffer; |
| 445 | |
| 446 | tagger->SetConfiguration(config); |
| 447 | tagger->RenderBuffer(id3Buffer, track); |
| 448 | |
| 449 | driver->WriteData(id3Buffer, id3Buffer.Size()); |
| 450 | |
| 451 | boca.DeleteComponent(tagger); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* Update ID3v2 tag with correct chapter marks. |
| 457 | */ |
| 458 | if (mp4File == NIL && config->GetIntValue("Tags", "EnableID3v2", True) && config->GetIntValue(ConfigureFDKAAC::ConfigID, "AllowID3v2", False)) |
| 459 | { |
| 460 | if (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True)) |
| 461 | { |
| 462 | AS::Registry &boca = AS::Registry::Get(); |
| 463 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); |
| 464 | |
| 465 | if (tagger != NIL) |
| 466 | { |
| 467 | Buffer<unsigned char> id3Buffer; |
| 468 | |
| 469 | tagger->SetConfiguration(config); |
| 470 | tagger->RenderBuffer(id3Buffer, track); |
| 471 | |
| 472 | driver->Seek(0); |
| 473 | driver->WriteData(id3Buffer, id3Buffer.Size()); |
| 474 | |
| 475 | boca.DeleteComponent(tagger); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return True; |
| 481 | } |
| 482 | |
| 483 | Int BoCA::EncoderFDKAAC::WriteData(Buffer<UnsignedByte> &data) |
| 484 | { |
| 485 | const Format &format = track.GetFormat(); |
| 486 | |
| 487 | /* Copy data to samples buffer. |
| 488 | */ |
| 489 | Int samples = data.Size() / 2; |
| 490 | |
| 491 | samplesBuffer.Resize(samplesBuffer.Size() + samples); |
| 492 | |
| 493 | memcpy(samplesBuffer + samplesBuffer.Size() - samples, data, data.Size()); |
| 494 | |
| 495 | /* Output samples to encoder. |
| 496 | */ |
| 497 | totalSamples += data.Size() / format.channels / (format.bits / 8); |
| 498 | |
| 499 | return EncodeFrames(False); |
| 500 | } |
| 501 | |
| 502 | Int BoCA::EncoderFDKAAC::EncodeFrames(Bool flush) |
| 503 | { |
| 504 | const Format &format = track.GetFormat(); |
| 505 | |
| 506 | /* Pass samples to workers. |
| 507 | */ |
| 508 | Int framesToProcess = blockSize; |
| 509 | Int framesProcessed = 0; |
| 510 | Int dataLength = 0; |
| 511 | |
| 512 | Int samplesPerFrame = frameSize * format.channels; |
| 513 | |
| 514 | if (flush) framesToProcess = Math::Floor(samplesBuffer.Size() / samplesPerFrame); |
| 515 | |
| 516 | while (samplesBuffer.Size() - framesProcessed * samplesPerFrame >= samplesPerFrame * framesToProcess) |
| 517 | { |
| 518 | SuperWorker *workerToUse = workers.GetNth(nextWorker % workers.Length()); |
| 519 | |
| 520 | workerToUse->WaitUntilReady(); |
| 521 | |
| 522 | /* See if the worker has some packets for us. |
| 523 | */ |
| 524 | if (workerToUse->GetPacketSizes().Length() != 0) dataLength += ProcessPackets(workerToUse->GetPackets(), workerToUse->GetPacketSizes(), nextWorker == workers.Length()); |
| 525 | |
| 526 | /* Pass new frames to worker. |
| 527 | */ |
| 528 | workerToUse->Encode(samplesBuffer, framesProcessed * samplesPerFrame, flush ? samplesBuffer.Size() : samplesPerFrame * framesToProcess, flush); |
| 529 | |
| 530 | framesProcessed += framesToProcess - (flush ? 0 : overlap); |
| 531 | |
| 532 | nextWorker++; |
| 533 | |
| 534 | if (flush) break; |
| 535 | } |
| 536 | |
| 537 | memmove(samplesBuffer, samplesBuffer + framesProcessed * samplesPerFrame, sizeof(int16_t) * (samplesBuffer.Size() - framesProcessed * samplesPerFrame)); |
| 538 | |
| 539 | samplesBuffer.Resize(samplesBuffer.Size() - framesProcessed * samplesPerFrame); |
| 540 | |
| 541 | if (!flush) return dataLength; |
| 542 | |
| 543 | /* Wait for workers to finish and process packets. |
| 544 | */ |
| 545 | for (Int i = 0; i < workers.Length(); i++) |
| 546 | { |
| 547 | SuperWorker *workerToUse = workers.GetNth(nextWorker % workers.Length()); |
| 548 | |
| 549 | workerToUse->WaitUntilReady(); |
| 550 | |
| 551 | /* See if the worker has some packets for us. |
| 552 | */ |
| 553 | if (workerToUse->GetPacketSizes().Length() != 0) dataLength += ProcessPackets(workerToUse->GetPackets(), workerToUse->GetPacketSizes(), nextWorker == workers.Length()); |
| 554 | |
| 555 | nextWorker++; |
| 556 | } |
| 557 | |
| 558 | return dataLength; |
| 559 | } |
| 560 | |
| 561 | Int BoCA::EncoderFDKAAC::ProcessPackets(const Buffer<unsigned char> &packets, const Array<Int> &packetSizes, Bool first) |
| 562 | { |
| 563 | Int offset = 0; |
| 564 | Int dataLength = 0; |
| 565 | |
| 566 | if (!first) for (Int i = 0; i < overlap; i++) offset += packetSizes.GetNth(i); |
| 567 | |
| 568 | for (Int i = 0; i < packetSizes.Length(); i++) |
| 569 | { |
| 570 | if (i < overlap && !first) continue; |
| 571 | if (packetSizes.GetNth(i) == 0) continue; |
| 572 | |
| 573 | if (mp4File != NIL) ex_MP4WriteSample(mp4File, mp4Track, (uint8_t *) (unsigned char *) packets + offset, packetSizes.GetNth(i), granuleSize, 0, true); |
| 574 | else driver->WriteData(packets + offset, packetSizes.GetNth(i)); |
| 575 | |
| 576 | totalFrames++; |
| 577 | |
| 578 | offset += packetSizes.GetNth(i); |
| 579 | dataLength += packetSizes.GetNth(i); |
| 580 | } |
| 581 | |
| 582 | return dataLength; |
| 583 | } |
| 584 | |
| 585 | Bool BoCA::EncoderFDKAAC::SetOutputFormat(Int n) |
| 586 | { |
| 587 | Config *config = Config::Get(); |
| 588 | |
| 589 | if (n == 0 && mp4v2dll != NIL) |
| 590 | { |
| 591 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "MP4Container", True); |
| 592 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "MPEGVersion", 0); |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "MP4Container", False); |
| 597 | } |
| 598 | |
| 599 | return True; |
| 600 | } |
| 601 | |
| 602 | String BoCA::EncoderFDKAAC::GetOutputFileExtension() const |
| 603 | { |
| 604 | const Config *config = GetConfiguration(); |
| 605 | |
| 606 | if (config->GetIntValue(ConfigureFDKAAC::ConfigID, "MP4Container", True)) |
| 607 | { |
| 608 | switch (config->GetIntValue(ConfigureFDKAAC::ConfigID, "MP4FileExtension", 0)) |
| 609 | { |
| 610 | default: |
| 611 | case 0: return "m4a"; |
| 612 | case 1: return "m4b"; |
| 613 | case 2: return "m4r"; |
| 614 | case 3: return "mp4"; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | return "aac"; |
| 619 | } |
| 620 | |
| 621 | Bool BoCA::EncoderFDKAAC::ConvertArguments(Config *config) |
| 622 | { |
| 623 | if (!config->GetIntValue("Settings", "EnableConsole", False)) return False; |
| 624 | |
| 625 | static const String encoderID = "fdkaac-enc"; |
| 626 | |
| 627 | /* Set default values. |
| 628 | */ |
| 629 | if (!config->GetIntValue("Settings", "UserSpecifiedConfig", False)) |
| 630 | { |
| 631 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "MPEGVersion", 0); |
| 632 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "MP4Container", True); |
| 633 | |
| 634 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "AACType", AOT_AAC_LC); |
| 635 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "Bitrate", 64); |
| 636 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "Afterburner", True); |
| 637 | } |
| 638 | |
| 639 | /* Get command line settings. |
| 640 | */ |
| 641 | Bool rawAAC = config->GetIntValue(encoderID, "Create ADTS AAC files (no MP4 container)", !config->GetIntValue(ConfigureFDKAAC::ConfigID, "MP4Container", True)); |
| 642 | Bool afterburner = !config->GetIntValue(encoderID, "Disable Afterburner processing", False); |
| 643 | |
| 644 | Int bitrate = config->GetIntValue(ConfigureFDKAAC::ConfigID, "Bitrate", 64); |
| 645 | Int aacType = config->GetIntValue(ConfigureFDKAAC::ConfigID, "AACType", AOT_AAC_LC); |
| 646 | String aacTypeString = "LC"; |
| 647 | |
| 648 | if (aacType == AOT_SBR) aacTypeString = "HE"; |
| 649 | else if (aacType == AOT_PS) aacTypeString = "HEv2"; |
| 650 | else if (aacType == AOT_ER_AAC_LD) aacTypeString = "LD"; |
| 651 | else if (aacType == AOT_ER_AAC_ELD) aacTypeString = "ELD"; |
| 652 | |
| 653 | if (config->GetIntValue(encoderID, "Set Bitrate per channel", False)) bitrate = config->GetIntValue(encoderID, "Bitrate per channel", bitrate); |
| 654 | if (config->GetIntValue(encoderID, "Set AAC encoding mode", False)) aacTypeString = config->GetStringValue(encoderID, "AAC encoding mode", aacTypeString).ToUpper(); |
| 655 | |
| 656 | /* Set configuration values. |
| 657 | */ |
| 658 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "MP4Container", !rawAAC); |
| 659 | |
| 660 | if (aacTypeString == "LC" ) aacType = AOT_AAC_LC; |
| 661 | else if (aacTypeString == "HE" ) aacType = AOT_SBR; |
| 662 | else if (aacTypeString == "HEV2") aacType = AOT_PS; |
| 663 | else if (aacTypeString == "LD" ) aacType = AOT_ER_AAC_LD; |
| 664 | else if (aacTypeString == "ELD" ) aacType = AOT_ER_AAC_ELD; |
| 665 | |
| 666 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "AACType", aacType); |
| 667 | config->SetIntValue(ConfigureFDKAAC::ConfigID, "Bitrate", Math::Max(8, Math::Min(256, bitrate))); |
| 668 | |
| 669 | if (!afterburner) config->SetIntValue(ConfigureFDKAAC::ConfigID, "Afterburner", False); |
| 670 | |
| 671 | return True; |
| 672 | } |
| 673 | |
| 674 | ConfigLayer *BoCA::EncoderFDKAAC::GetConfigurationLayer() |
| 675 | { |
| 676 | if (configLayer == NIL) configLayer = new ConfigureFDKAAC(); |
| 677 | |
| 678 | return configLayer; |
| 679 | } |
| 680 | |
| 681 | int64_t BoCA::MP4IO_size(void *handle) |
| 682 | { |
| 683 | Driver *driver = (Driver *) handle; |
| 684 | |
| 685 | return driver->GetSize(); |
| 686 | } |
| 687 | |
| 688 | int BoCA::MP4IO_seek(void *handle, int64_t pos) |
| 689 | { |
| 690 | Driver *driver = (Driver *) handle; |
| 691 | |
| 692 | if (driver->Seek(pos) == -1) return 1; |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | int BoCA::MP4IO_write(void *handle, const void *buffer, int64_t size, int64_t *nout) |
| 698 | { |
| 699 | Driver *driver = (Driver *) handle; |
| 700 | |
| 701 | *nout = driver->WriteData((const UnsignedByte *) buffer, size); |
| 702 | |
| 703 | if (*nout == 0) return 1; |
| 704 | |
| 705 | return 0; |
| 706 | } |