| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2021 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 | |
| 15 | #include "worker.h" |
| 16 | #include "config.h" |
| 17 | |
| 18 | BoCA::SuperWorker::SuperWorker(const Config *config, const Format &iFormat) : processSignal(1), readySignal(1) |
| 19 | { |
| 20 | processSignal.Wait(); |
| 21 | |
| 22 | flush = False; |
| 23 | quit = False; |
| 24 | |
| 25 | format = iFormat; |
| 26 | |
| 27 | threadMain.Connect(&SuperWorker::Run, this); |
| 28 | |
| 29 | /* Get configuration. |
| 30 | */ |
| 31 | Bool mp4Container = config->GetIntValue(ConfigureFAAC::ConfigID, "MP4Container", True); |
| 32 | Int mpegVersion = config->GetIntValue(ConfigureFAAC::ConfigID, "MPEGVersion", 0); |
| 33 | Bool setQuality = config->GetIntValue(ConfigureFAAC::ConfigID, "SetQuality", True); |
| 34 | Int aacQuality = config->GetIntValue(ConfigureFAAC::ConfigID, "AACQuality", 150); |
| 35 | Int bitrate = config->GetIntValue(ConfigureFAAC::ConfigID, "Bitrate", 96); |
| 36 | Bool allowJS = config->GetIntValue(ConfigureFAAC::ConfigID, "AllowJS", True); |
| 37 | Bool useTNS = config->GetIntValue(ConfigureFAAC::ConfigID, "UseTNS", False); |
| 38 | Int bandwidth = config->GetIntValue(ConfigureFAAC::ConfigID, "BandWidth", 18500); |
| 39 | |
| 40 | /* Create FAAC encoder. |
| 41 | */ |
| 42 | unsigned long samplesSize = 0; |
| 43 | unsigned long bufferSize = 0; |
| 44 | |
| 45 | handle = ex_faacEncOpen(format.rate, format.channels, &samplesSize, &bufferSize); |
| 46 | |
| 47 | /* Set encoder parameters. |
| 48 | */ |
| 49 | faacEncConfigurationPtr fConfig = ex_faacEncGetCurrentConfiguration(handle); |
| 50 | |
| 51 | fConfig->inputFormat = FAAC_INPUT_16BIT; |
| 52 | fConfig->outputFormat = mp4Container ? RAW_STREAM : ADTS_STREAM; |
| 53 | fConfig->mpegVersion = mp4Container ? MPEG4 : mpegVersion; |
| 54 | fConfig->aacObjectType = LOW; |
| 55 | fConfig->quantqual = setQuality ? aacQuality : 0; |
| 56 | fConfig->bitRate = setQuality ? 0 : bitrate * 1000; |
| 57 | fConfig->bandWidth = bandwidth; |
| 58 | fConfig->jointmode = allowJS ? JOINT_IS : JOINT_NONE; |
| 59 | fConfig->useTns = useTNS; |
| 60 | |
| 61 | ex_faacEncSetConfiguration(handle, fConfig); |
| 62 | |
| 63 | frameSize = samplesSize / format.channels; |
| 64 | maxPacketSize = bufferSize; |
| 65 | } |
| 66 | |
| 67 | BoCA::SuperWorker::~SuperWorker() |
| 68 | { |
| 69 | /* Destroy FAAC encoder. |
| 70 | */ |
| 71 | ex_faacEncClose(handle); |
| 72 | } |
| 73 | |
| 74 | Int BoCA::SuperWorker::Run() |
| 75 | { |
| 76 | while (!Threads::Access::Value(quit)) |
| 77 | { |
| 78 | processSignal.Wait(); |
| 79 | |
| 80 | if (Threads::Access::Value(quit)) break; |
| 81 | |
| 82 | packetBuffer.Resize(0); |
| 83 | packetSizes.RemoveAll(); |
| 84 | |
| 85 | Int samplesLeft = samplesBuffer.Size(); |
| 86 | Int samplesPerFrame = frameSize * format.channels; |
| 87 | |
| 88 | Int framesProcessed = 0; |
| 89 | |
| 90 | while (flush || samplesLeft >= samplesPerFrame) |
| 91 | { |
| 92 | packetBuffer.Resize(packetBuffer.Size() + maxPacketSize); |
| 93 | |
| 94 | Int dataLength = 0; |
| 95 | |
| 96 | if (samplesLeft > 0) dataLength = ex_faacEncEncode(handle, (int32_t *) (int16_t *) (samplesBuffer + framesProcessed * samplesPerFrame), Math::Min(samplesLeft, samplesPerFrame), packetBuffer + packetBuffer.Size() - maxPacketSize, maxPacketSize); |
| 97 | else dataLength = ex_faacEncEncode(handle, NULL, 0, packetBuffer + packetBuffer.Size() - maxPacketSize, maxPacketSize); |
| 98 | |
| 99 | packetBuffer.Resize(packetBuffer.Size() - maxPacketSize + dataLength); |
| 100 | |
| 101 | if (samplesLeft < 0 && dataLength == 0) break; |
| 102 | |
| 103 | packetSizes.Add(dataLength); |
| 104 | |
| 105 | framesProcessed++; |
| 106 | samplesLeft -= samplesPerFrame; |
| 107 | } |
| 108 | |
| 109 | readySignal.Release(); |
| 110 | } |
| 111 | |
| 112 | return Success(); |
| 113 | } |
| 114 | |
| 115 | Void BoCA::SuperWorker::Encode(const Buffer<int16_t> &buffer, Int offset, Int size, Bool last) |
| 116 | { |
| 117 | samplesBuffer.Resize(size); |
| 118 | |
| 119 | memcpy(samplesBuffer, buffer + offset, size * sizeof(int16_t)); |
| 120 | |
| 121 | flush = last; |
| 122 | |
| 123 | processSignal.Release(); |
| 124 | } |
| 125 | |
| 126 | Void BoCA::SuperWorker::WaitUntilReady() |
| 127 | { |
| 128 | readySignal.Wait(); |
| 129 | } |
| 130 | |
| 131 | Int BoCA::SuperWorker::Quit() |
| 132 | { |
| 133 | Threads::Access::Set(quit, True); |
| 134 | |
| 135 | processSignal.Release(); |
| 136 | |
| 137 | return Success(); |
| 138 | } |