/* BoCA - BonkEnc Component Architecture * Copyright (C) 2007-2026 Robert Kausch * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of * the License, or (at your option) any later version. * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ #include #include #include "faac.h" #include "config.h" using namespace smooth::IO; const String &BoCA::EncoderFAAC::GetComponentSpecs() { static String componentSpecs; if (faacdll != NIL) { componentSpecs = " \ \ \ \ FAAC MP4/AAC Encoder %VERSION% \ 1.0 \ faac-enc \ encoder \ voaacenc-enc \ \ "; if (mp4v2dll != NIL) { componentSpecs.Append(" \ \ \ MPEG-4 AAC Files \ m4a \ m4b \ m4r \ mp4 \ MP4 Metadata \ \ \ "); } componentSpecs.Append(" \ \ \ Raw AAC Files \ aac \ ID3v2 \ \ \ \ \ 10 \ 500 \ \ \ 8 \ 256 \ \ \ "); if (mp4v2dll != NIL) { componentSpecs.Append(" \ \ \ \ "); } componentSpecs.Append(" \ \ \ \ \ "); unsigned long samplesSize; unsigned long bufferSize; faacEncHandle faac = ex_faacEncOpen(44100, 2, &samplesSize, &bufferSize); componentSpecs.Replace("%VERSION%", String("v").Append(ex_faacEncGetCurrentConfiguration(faac)->name)); ex_faacEncClose(faac); } return componentSpecs; } Void smooth::AttachDLL(Void *instance) { LoadFAACDLL(); LoadMP4v2DLL(); } Void smooth::DetachDLL() { FreeFAACDLL(); FreeMP4v2DLL(); } namespace BoCA { int64_t MP4IO_size(void *); int MP4IO_seek(void *, int64_t); int MP4IO_write(void *, const void *, int64_t, int64_t *); static MP4IOCallbacks mp4Callbacks = { MP4IO_size, MP4IO_seek, NIL, MP4IO_write, NIL }; }; BoCA::EncoderFAAC::EncoderFAAC() { configLayer = NIL; config = NIL; mp4File = NIL; mp4Track = MP4_INVALID_TRACK_ID; frameSize = 0; blockSize = 128; overlap = 8; totalSamples = 0; nextWorker = 0; } BoCA::EncoderFAAC::~EncoderFAAC() { if (config != NIL) Config::Free(config); if (configLayer != NIL) Object::DeleteObject(configLayer); } Bool BoCA::EncoderFAAC::Activate() { const Format &format = track.GetFormat(); /* Get configuration. */ config = Config::Copy(GetConfiguration()); ConvertArguments(config); if (mp4v2dll == NIL) config->SetIntValue(ConfigureFAAC::ConfigID, "MP4Container", False); Bool mp4Container = config->GetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True); Int mpegVersion = config->GetIntValue(ConfigureFAAC::ConfigID, "MPEGVersion", 0); /* Create FAAC encoder. */ unsigned long samplesSize = 0; unsigned long bufferSize = 0; faacEncHandle handle = ex_faacEncOpen(format.rate, format.channels, &samplesSize, &bufferSize); /* Set encoder parameters. */ faacEncConfigurationPtr fConfig = ex_faacEncGetCurrentConfiguration(handle); fConfig->mpegVersion = mp4Container ? MPEG4 : mpegVersion; fConfig->aacObjectType = LOW; ex_faacEncSetConfiguration(handle, fConfig); frameSize = samplesSize / format.channels; /* Check whether to use MP4 container. */ if (mp4Container) { /* Create MP4 file. */ uint32_t flags = (track.length >= 0xFFFF0000 || track.approxLength >= 0xFFFF0000) ? MP4_CREATE_64BIT_DATA | MP4_CREATE_64BIT_TIME : 0; mp4File = ex_MP4CreateCallbacks(&mp4Callbacks, driver, flags); mp4Track = ex_MP4AddAudioTrack(mp4File, format.rate, MP4_INVALID_DURATION, MP4_MPEG4_AUDIO_TYPE); ex_MP4SetAudioProfileLevel(mp4File, 0x0F); ex_MP4SetTrackIntegerProperty(mp4File, mp4Track, "mdia.minf.stbl.stsd.mp4a.channels", format.channels); unsigned char *buffer = NIL; ex_faacEncGetDecoderSpecificInfo(handle, &buffer, &bufferSize); ex_MP4SetTrackESConfiguration(mp4File, mp4Track, (uint8_t *) buffer, bufferSize); totalSamples = 0; } ex_faacEncClose(handle); /* Write ID3v2 tag if requested. */ if (mp4File == NIL && config->GetIntValue("Tags", "EnableID3v2", True) && config->GetIntValue(ConfigureFAAC::ConfigID, "AllowID3v2", False)) { const Info &info = track.GetInfo(); if (info.HasBasicInfo() || (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True))) { AS::Registry &boca = AS::Registry::Get(); AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); if (tagger != NIL) { Buffer id3Buffer; tagger->SetConfiguration(config); tagger->RenderBuffer(id3Buffer, track); driver->WriteData(id3Buffer, id3Buffer.Size()); boca.DeleteComponent(tagger); } } } /* Get number of threads to use. */ Bool enableParallel = config->GetIntValue("Resources", "EnableParallelConversions", True); Bool enableSuperFast = config->GetIntValue("Resources", "EnableSuperFastMode", True); Int numberOfThreads = enableParallel && enableSuperFast ? config->GetIntValue("Resources", "NumberOfConversionThreads", 0) : 1; if (enableParallel && enableSuperFast && numberOfThreads <= 1) numberOfThreads = CPU().GetNumCores() + (CPU().GetNumLogicalCPUs() - CPU().GetNumCores()) / 2; /* Disable overlap if we use only one thread. */ if (numberOfThreads == 1) overlap = 0; else overlap = 8; /* Start up worker threads. */ for (Int i = 0; i < numberOfThreads; i++) workers.Add(new SuperWorker(config, format)); foreach (SuperWorker *worker, workers) worker->Start(); return True; } Bool BoCA::EncoderFAAC::Deactivate() { /* Output remaining samples to encoder. */ EncodeFrames(True); /* Tear down worker threads. */ foreach (SuperWorker *worker, workers) worker->Quit(); foreach (SuperWorker *worker, workers) worker->Wait(); foreach (SuperWorker *worker, workers) delete worker; workers.RemoveAll(); /* Finish MP4 writing. */ if (mp4File != NIL) { /* Write iTunes metadata with gapless information. */ MP4ItmfItem *item = ex_MP4ItmfItemAlloc("----", 1); String value = String().Append(" 00000000") .Append(" ").Append(Number((Int64) frameSize).ToHexString(8).ToUpper()) .Append(" ").Append(Number((Int64) frameSize - totalSamples % frameSize).ToHexString(8).ToUpper()) .Append(" ").Append(Number((Int64) totalSamples).ToHexString(16).ToUpper()) .Append(" 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000"); item->mean = (char *) "com.apple.iTunes"; item->name = (char *) "iTunSMPB"; item->dataList.elements[0].typeCode = MP4_ITMF_BT_UTF8; item->dataList.elements[0].value = (uint8_t *) value.ConvertTo("UTF-8"); item->dataList.elements[0].valueSize = value.Length(); ex_MP4ItmfAddItem(mp4File, item); item->mean = NIL; item->name = NIL; item->dataList.elements[0].typeCode = MP4_ITMF_BT_IMPLICIT; item->dataList.elements[0].value = NIL; item->dataList.elements[0].valueSize = 0; ex_MP4ItmfItemFree(item); ex_MP4Close(mp4File, 0); /* Close output file as tagger needs to modify it. */ driver->Close(); /* Write metadata to file. */ const Info &info = track.GetInfo(); if (config->GetIntValue("Tags", "EnableMP4Metadata", True) && (info.HasBasicInfo() || (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True)))) { AS::Registry &boca = AS::Registry::Get(); AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("mp4-tag"); if (tagger != NIL) { tagger->SetConfiguration(config); tagger->RenderStreamInfo(track.outputFile, track); boca.DeleteComponent(tagger); } } else { /* Optimize file even when no tags are written. */ String tempFile = String(track.outputFile).Append(".temp"); ex_MP4Optimize(track.outputFile.ConvertTo("UTF-8"), tempFile.ConvertTo("UTF-8")); File(track.outputFile).Delete(); File(tempFile).Move(track.outputFile); } } /* Write ID3v1 tag if requested. */ if (mp4File == NIL && config->GetIntValue("Tags", "EnableID3v1", False)) { const Info &info = track.GetInfo(); if (info.HasBasicInfo()) { AS::Registry &boca = AS::Registry::Get(); AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v1-tag"); if (tagger != NIL) { Buffer id3Buffer; tagger->SetConfiguration(config); tagger->RenderBuffer(id3Buffer, track); driver->WriteData(id3Buffer, id3Buffer.Size()); boca.DeleteComponent(tagger); } } } /* Update ID3v2 tag with correct chapter marks. */ if (mp4File == NIL && config->GetIntValue("Tags", "EnableID3v2", True) && config->GetIntValue(ConfigureFAAC::ConfigID, "AllowID3v2", False)) { if (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True)) { AS::Registry &boca = AS::Registry::Get(); AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); if (tagger != NIL) { Buffer id3Buffer; tagger->SetConfiguration(config); tagger->RenderBuffer(id3Buffer, track); driver->Seek(0); driver->WriteData(id3Buffer, id3Buffer.Size()); boca.DeleteComponent(tagger); } } } return True; } Int BoCA::EncoderFAAC::WriteData(Buffer &data) { const Format &format = track.GetFormat(); /* Change to AAC channel order. */ if (format.channels == 3) Utilities::ChangeChannelOrder(data, format, Channel::Default_3_0, Channel::AAC_3_0); else if (format.channels == 5) Utilities::ChangeChannelOrder(data, format, Channel::Default_5_0, Channel::AAC_5_0); else if (format.channels == 6) Utilities::ChangeChannelOrder(data, format, Channel::Default_5_1, Channel::AAC_5_1); /* Copy data to samples buffer. */ Int samples = data.Size() / 2; samplesBuffer.Resize(samplesBuffer.Size() + samples); memcpy(samplesBuffer + samplesBuffer.Size() - samples, data, data.Size()); /* Output samples to encoder. */ totalSamples += data.Size() / format.channels / (format.bits / 8); return EncodeFrames(False); } Int BoCA::EncoderFAAC::EncodeFrames(Bool flush) { const Format &format = track.GetFormat(); /* Pass samples to workers. */ Int framesToProcess = blockSize; Int framesProcessed = 0; Int dataLength = 0; Int samplesPerFrame = frameSize * format.channels; if (flush) framesToProcess = Math::Floor(samplesBuffer.Size() / samplesPerFrame); while (samplesBuffer.Size() - framesProcessed * samplesPerFrame >= samplesPerFrame * framesToProcess) { SuperWorker *workerToUse = workers.GetNth(nextWorker % workers.Length()); workerToUse->WaitUntilReady(); /* See if the worker has some packets for us. */ if (workerToUse->GetPacketSizes().Length() != 0) dataLength += ProcessPackets(workerToUse->GetPackets(), workerToUse->GetPacketSizes(), nextWorker == workers.Length()); /* Pass new frames to worker. */ workerToUse->Encode(samplesBuffer, framesProcessed * samplesPerFrame, flush ? samplesBuffer.Size() : samplesPerFrame * framesToProcess, flush); framesProcessed += framesToProcess - (flush ? 0 : overlap); nextWorker++; if (flush) break; } memmove(samplesBuffer, samplesBuffer + framesProcessed * samplesPerFrame, sizeof(int16_t) * (samplesBuffer.Size() - framesProcessed * samplesPerFrame)); samplesBuffer.Resize(samplesBuffer.Size() - framesProcessed * samplesPerFrame); if (!flush) return dataLength; /* Wait for workers to finish and process packets. */ for (Int i = 0; i < workers.Length(); i++) { SuperWorker *workerToUse = workers.GetNth(nextWorker % workers.Length()); workerToUse->WaitUntilReady(); /* See if the worker has some packets for us. */ if (workerToUse->GetPacketSizes().Length() != 0) dataLength += ProcessPackets(workerToUse->GetPackets(), workerToUse->GetPacketSizes(), nextWorker == workers.Length()); nextWorker++; } return dataLength; } Int BoCA::EncoderFAAC::ProcessPackets(const Buffer &packets, const Array &packetSizes, Bool first) { Int offset = 0; Int dataLength = 0; if (!first) for (Int i = 0; i < overlap; i++) offset += packetSizes.GetNth(i); for (Int i = 0; i < packetSizes.Length(); i++) { if (i < overlap && !first) continue; if (packetSizes.GetNth(i) == 0) continue; if (mp4File != NIL) ex_MP4WriteSample(mp4File, mp4Track, (uint8_t *) (unsigned char *) packets + offset, packetSizes.GetNth(i), frameSize, 0, true); else driver->WriteData(packets + offset, packetSizes.GetNth(i)); offset += packetSizes.GetNth(i); dataLength += packetSizes.GetNth(i); } return dataLength; } Bool BoCA::EncoderFAAC::SetOutputFormat(Int n) { Config *config = Config::Get(); if (n == 0 && mp4v2dll != NIL) { config->SetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True); config->SetIntValue(ConfigureFAAC::ConfigID, "MPEGVersion", 0); } else { config->SetIntValue(ConfigureFAAC::ConfigID, "MP4Container", False); } return True; } String BoCA::EncoderFAAC::GetOutputFileExtension() const { const Config *config = GetConfiguration(); if (config->GetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True)) { switch (config->GetIntValue(ConfigureFAAC::ConfigID, "MP4FileExtension", 0)) { default: case 0: return "m4a"; case 1: return "m4b"; case 2: return "m4r"; case 3: return "mp4"; } } return "aac"; } Bool BoCA::EncoderFAAC::ConvertArguments(Config *config) { if (!config->GetIntValue("Settings", "EnableConsole", False)) return False; static const String encoderID = "faac-enc"; /* Set default values. */ if (!config->GetIntValue("Settings", "UserSpecifiedConfig", False)) { config->SetIntValue(ConfigureFAAC::ConfigID, "MPEGVersion", 0); config->SetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True); config->SetIntValue(ConfigureFAAC::ConfigID, "SetQuality", True); config->SetIntValue(ConfigureFAAC::ConfigID, "AACQuality", 150); config->SetIntValue(ConfigureFAAC::ConfigID, "Bitrate", 96); } /* Get command line settings. */ Bool rawAAC = config->GetIntValue(encoderID, "Create ADTS AAC files (no MP4 container)", !config->GetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True)); Bool useABR = config->GetIntValue(encoderID, "Set ABR bitrate per channel", !config->GetIntValue(ConfigureFAAC::ConfigID, "SetQuality", True)); Int quality = config->GetIntValue(ConfigureFAAC::ConfigID, "AACQuality", 150); Int bitrate = config->GetIntValue(ConfigureFAAC::ConfigID, "Bitrate", 96); if (config->GetIntValue(encoderID, "Set VBR quality", False)) quality = config->GetIntValue(encoderID, "VBR quality", quality); if (config->GetIntValue(encoderID, "Set ABR bitrate per channel", False)) bitrate = config->GetIntValue(encoderID, "ABR bitrate per channel", bitrate); /* Set configuration values. */ config->SetIntValue(ConfigureFAAC::ConfigID, "MP4Container", !rawAAC); config->SetIntValue(ConfigureFAAC::ConfigID, "SetQuality", !useABR); config->SetIntValue(ConfigureFAAC::ConfigID, "AACQuality", Math::Max(10, Math::Min(500, quality))); config->SetIntValue(ConfigureFAAC::ConfigID, "Bitrate", Math::Max(8, Math::Min(256, bitrate))); return True; } ConfigLayer *BoCA::EncoderFAAC::GetConfigurationLayer() { if (configLayer == NIL) configLayer = new ConfigureFAAC(); return configLayer; } int64_t BoCA::MP4IO_size(void *handle) { Driver *driver = (Driver *) handle; return driver->GetSize(); } int BoCA::MP4IO_seek(void *handle, int64_t pos) { Driver *driver = (Driver *) handle; if (driver->Seek(pos) == -1) return 1; return 0; } int BoCA::MP4IO_write(void *handle, const void *buffer, int64_t size, int64_t *nout) { Driver *driver = (Driver *) handle; *nout = driver->WriteData((const UnsignedByte *) buffer, size); if (*nout == 0) return 1; return 0; }