libaac-next/source/examples/aacdec/aacdec.cpp
wrapper 5972a6a952
Some checks are pending
C++ CI / build (push) Waiting to run
C++ CI / test (push) Blocked by required conditions
C++ CI / check (push) Blocked by required conditions
memcpy buffer based
2025-08-08 09:20:53 +07:00

111 lines
No EOL
3.1 KiB
C++

#include <aacnext/libaac.h>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <ios>
#include <iostream>
class AACFileReader {
private:
int bufSize;
std::ifstream file;
uint8_t *buf;
int dataRem;
public:
bool ok;
AACFileReader(std::string inFile, int bufferSize) {
bufSize = bufferSize;
file = std::ifstream(inFile, std::ios_base::binary);
ok = file.is_open();
if (!ok) return;
buf = new uint8_t[bufferSize];
file.read((char *)buf, bufferSize);
dataRem = file.gcount();
}
void Get(uint8_t *out, int max, uint32_t *len) {
if (len != NULL) *len = dataRem;
memcpy(out, buf, max > dataRem ? dataRem : max);
}
void Next(uint32_t count) {
memmove(buf, buf + count, dataRem - count);
file.read((char *)(buf + (dataRem - count)), count);
dataRem = (dataRem - count) + file.gcount();
}
void Close() {
file.close();
delete buf;
}
};
int main(int argc, char *argv[]) {
#if 0
AACFileReader inData("asc.es", 4096);
if (!inData.ok) return 1;
uint8_t *asc = new uint8_t[2];
inData.Get((char *)asc, 2, NULL);
AACDecodeSettings aacConf = {};
aacConf.asc = asc;
aacConf.ascSize = 2;
aacConf.bitsPerSamples = 16;
auto aac = aac_decode_open(aacConf);
#else
AACFileReader inData("adts.aac", 4096);
if (!inData.ok) return 1;
AACDecodeSettings aacConf = {};
aacConf.ascSize = 0;
aacConf.bitsPerSamples = 16;
auto aac = aac_decode_open(aacConf);
#endif
uint8_t *tempBuf = new uint8_t[4096];
uint32_t tempBufReadSize;
uint8_t *tempBufOut = new uint8_t[aac->outBufSize];
uint32_t tempBufOutSize;
uint32_t bytesNext;
std::cout << aac->inBufSize << std::endl;
//uint8_t *last_okay;
std::ofstream testOut("aac_decode_test.bin", std::ios_base::binary);
while (1) {
inData.Get(tempBuf, 4096, &tempBufReadSize);
if (!tempBufReadSize) break;
// std::cout << "sync: " << std::hex << (int)tempBuf[0] << std::endl;
// std::cout << "sync (2): " << std::hex << (int)tempBuf[1] << std::endl;
auto ret = aac_decode(aac, tempBuf, tempBufReadSize, tempBufOut, &tempBufOutSize, &bytesNext);
if (ret == -1) break;
testOut.write((char *)tempBufOut, tempBufOutSize);
inData.Next(bytesNext);
}
// inData.Get(tempBuf, 4096, &tempBufReadSize);
// std::cout << "last (4095):" << std::hex << (int)tempBuf[4094] << std::endl;
// std::cout << "last (4096):" << std::hex << (int)tempBuf[4095] << std::endl;
// inData.Next(2);
// inData.Get(tempBuf, 4096, &tempBufReadSize);
// std::cout << "last (4095, 2):" << std::hex << (int)tempBuf[4094] << std::endl;
// std::cout << "last (4096, 2):" << std::hex << (int)tempBuf[4095] << std::endl;
// aac_decode(aac, tempBuf, tempBufReadSize, &tempBufOut, &tempBufOutSize, &bytesNext);
// inData.Next(tempBufOutSize);
}