2023-03-12 21:03:47 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: BrotliCompressor.hpp
|
|
|
|
Date: 2023-3-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "brotli/encode.h"
|
|
|
|
|
|
|
|
namespace Aurora::Compression
|
|
|
|
{
|
|
|
|
struct BrotliDeflate : BaseStream
|
|
|
|
{
|
|
|
|
CompressInfo meta;
|
|
|
|
|
|
|
|
BrotliDeflate(const CompressInfo &meta) : meta(meta), BaseStream(meta.uInternalStreamSize)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~BrotliDeflate()
|
|
|
|
{
|
|
|
|
if (this->pState)
|
|
|
|
{
|
|
|
|
BrotliEncoderDestroyInstance(this->pState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Init(const AuSPtr<IO::IStreamReader> &reader) override
|
|
|
|
{
|
|
|
|
this->pReader_ = reader;
|
|
|
|
|
|
|
|
if (!this->IsValid())
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-27 19:45:49 +00:00
|
|
|
if (meta.uCompressionLevel > BROTLI_MAX_QUALITY)
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Invalid brotli compression level");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-12 21:03:47 +00:00
|
|
|
this->pState = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr);
|
|
|
|
if (!this->pState)
|
|
|
|
{
|
|
|
|
SysPushErrorMem("No brotli encoder");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-27 19:45:49 +00:00
|
|
|
if (meta.uBlockSize)
|
|
|
|
{
|
|
|
|
if (!BrotliEncoderSetParameter(this->pState, BrotliEncoderParameter::BROTLI_PARAM_LGBLOCK, meta.uBlockSize))
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Compressor couldn't set block size");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-07-27 21:55:28 +00:00
|
|
|
|
2023-07-28 07:10:08 +00:00
|
|
|
if (meta.uOptQuality)
|
2023-07-27 21:55:28 +00:00
|
|
|
{
|
2023-07-28 07:10:08 +00:00
|
|
|
if (meta.uOptQuality.value() <= BROTLI_MAX_QUALITY &&
|
|
|
|
meta.uOptQuality.value() >= BROTLI_MIN_QUALITY)
|
2023-07-27 21:55:28 +00:00
|
|
|
{
|
2023-07-28 07:10:08 +00:00
|
|
|
if (!BrotliEncoderSetParameter(this->pState, BrotliEncoderParameter::BROTLI_PARAM_QUALITY, meta.uOptQuality.value()))
|
2023-07-27 21:55:28 +00:00
|
|
|
{
|
|
|
|
SysPushErrorArg("Compressor couldn't set quality");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Compressor couldn't set quality");
|
2023-07-29 08:18:02 +00:00
|
|
|
return false;
|
2023-07-27 21:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-28 07:55:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!BrotliEncoderSetParameter(this->pState, BrotliEncoderParameter::BROTLI_PARAM_QUALITY, meta.uCompressionLevel))
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Compressor couldn't set quality from compression level");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-07-27 21:55:28 +00:00
|
|
|
|
2023-07-28 07:10:08 +00:00
|
|
|
if (meta.uOptWindowBits)
|
2023-07-27 21:55:28 +00:00
|
|
|
{
|
2023-07-28 07:10:08 +00:00
|
|
|
if (meta.uOptWindowBits.value() <= BROTLI_MAX_INPUT_BLOCK_BITS &&
|
|
|
|
meta.uOptWindowBits.value() >= BROTLI_MIN_INPUT_BLOCK_BITS)
|
2023-07-27 21:55:28 +00:00
|
|
|
{
|
2023-07-28 07:10:08 +00:00
|
|
|
if (!BrotliEncoderSetParameter(this->pState, BrotliEncoderParameter::BROTLI_PARAM_LGWIN, meta.uOptWindowBits))
|
2023-07-27 21:55:28 +00:00
|
|
|
{
|
|
|
|
SysPushErrorArg("Compressor couldn't set window bits");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Compressor couldn't set window bits");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-07-27 19:45:49 +00:00
|
|
|
|
|
|
|
for (const auto &[a, b] : meta.options)
|
|
|
|
{
|
|
|
|
if (!BrotliEncoderSetParameter(this->pState, (BrotliEncoderParameter)a, b))
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Compressor argument assignment {} = {}", a, b);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:55:40 +00:00
|
|
|
this->InitByDesc(this->meta);
|
2023-03-12 21:03:47 +00:00
|
|
|
this->SetArray(this->din_);
|
2023-06-05 13:55:40 +00:00
|
|
|
this->SetOutArray(this->dout_);
|
2023-03-12 21:03:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuStreamReadWrittenPair_t Ingest_s(AuUInt32 input) override
|
|
|
|
{
|
|
|
|
AuUInt32 done {}, read {};
|
|
|
|
|
|
|
|
if (!this->pReader_)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->pState)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
while (read < input)
|
|
|
|
{
|
2023-06-10 19:03:29 +00:00
|
|
|
read += IngestForInPointer<uint8_t, size_t>(this->pReader_, this->pInBuffer, this->uAvailIn, input - read, this);
|
2023-03-12 21:03:47 +00:00
|
|
|
|
|
|
|
if (!this->uAvailIn)
|
|
|
|
{
|
|
|
|
return { read, done };
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:55:40 +00:00
|
|
|
auto [pMainDOut, uMainDOutLength] = this->GetDOutPair();
|
2023-03-12 21:03:47 +00:00
|
|
|
size_t outNext {};
|
|
|
|
uint8_t *pOut {};
|
|
|
|
do
|
|
|
|
{
|
2023-06-05 13:55:40 +00:00
|
|
|
outNext = uMainDOutLength;
|
|
|
|
pOut = (uint8_t *)pMainDOut;
|
2023-03-12 21:03:47 +00:00
|
|
|
|
|
|
|
auto ret = BrotliEncoderCompressStream(this->pState, BrotliEncoderOperation::BROTLI_OPERATION_PROCESS, &this->uAvailIn, (const uint8_t **) & this->pInBuffer, &outNext, &pOut, nullptr);
|
|
|
|
if (ret == BROTLI_FALSE)
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Brotli Error");
|
|
|
|
this->pReader_.reset();
|
|
|
|
return AuMakePair(read, 0);
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:55:40 +00:00
|
|
|
auto have = uMainDOutLength - outNext;
|
2023-03-12 21:03:47 +00:00
|
|
|
done += have;
|
|
|
|
|
2023-06-05 13:55:40 +00:00
|
|
|
if (!Write2(reinterpret_cast<const AuUInt8 *>(pMainDOut), have))
|
2023-03-12 21:03:47 +00:00
|
|
|
{
|
|
|
|
this->pReader_.reset();
|
|
|
|
SysPushErrorIO("Compression Out of Overhead");
|
|
|
|
return AuMakePair(read, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (outNext == 0 || BrotliEncoderHasMoreOutput(this->pState));
|
|
|
|
}
|
|
|
|
|
|
|
|
return { read, done };
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Flush() override
|
|
|
|
{
|
|
|
|
return RunFlush(BrotliEncoderOperation::BROTLI_OPERATION_FLUSH);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Finish() override
|
|
|
|
{
|
|
|
|
return RunFlush(BrotliEncoderOperation::BROTLI_OPERATION_FINISH);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RunFlush(BrotliEncoderOperation type)
|
|
|
|
{
|
|
|
|
if (!this->pReader_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!this->pState)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt read {};
|
|
|
|
{
|
|
|
|
size_t outNext {};
|
|
|
|
uint8_t *pOut {};
|
2023-08-11 15:51:42 +00:00
|
|
|
while (outNext == 0 || BrotliEncoderHasMoreOutput(this->pState))
|
2023-03-12 21:03:47 +00:00
|
|
|
{
|
2023-06-05 13:55:40 +00:00
|
|
|
auto [pMainDOut, uMainDOutLength] = this->GetDOutPair();
|
|
|
|
|
|
|
|
outNext = uMainDOutLength;
|
|
|
|
pOut = (uint8_t *)pMainDOut;
|
2023-03-12 21:03:47 +00:00
|
|
|
|
|
|
|
auto ret = BrotliEncoderCompressStream(this->pState, type, &this->uAvailIn, (const uint8_t **)&this->pInBuffer, &outNext, &pOut, nullptr);
|
|
|
|
if (ret == BROTLI_FALSE)
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Brotli Error");
|
|
|
|
this->pReader_.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:55:40 +00:00
|
|
|
auto have = uMainDOutLength - outNext;
|
|
|
|
if (!Write2(reinterpret_cast<const AuUInt8 *>(pMainDOut), have))
|
2023-03-12 21:03:47 +00:00
|
|
|
{
|
|
|
|
this->pReader_.reset();
|
|
|
|
SysPushErrorIO("Compression Out of Overhead");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AuSPtr<IO::IStreamReader> pReader_;
|
|
|
|
unsigned char din_[kChunkSize];
|
|
|
|
unsigned char dout_[kChunkSize];
|
|
|
|
uint8_t *pInBuffer;
|
|
|
|
size_t uAvailIn {};
|
|
|
|
BrotliEncoderState *pState {};
|
|
|
|
};
|
|
|
|
}
|