Add FLAC.

This commit is contained in:
2022-05-28 13:38:09 +01:00
parent 0d6e731cf0
commit db5b14a56c
13 changed files with 766 additions and 22 deletions

View File

@@ -8,7 +8,13 @@ include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/random
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/flac.flac
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/audio.bin
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
# 'Google_Tests_run' is the target name
# 'test1.cpp tests2.cpp' are source files with tests
add_executable(tests_run crc64.cpp spamsum.cpp)
add_executable(tests_run crc64.cpp spamsum.cpp crc32.c crc32.h flac.cpp)
target_link_libraries(tests_run gtest gtest_main "aaruformat")

53
tests/crc32.c Normal file
View File

@@ -0,0 +1,53 @@
/*
* This file is part of the Aaru Data Preservation Suite.
* Copyright (c) 2019-2022 Natalia Portillo.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#define CRC32_ISO_POLY 0xEDB88320
#define CRC32_ISO_SEED 0xFFFFFFFF
uint32_t crc32_data(const uint8_t* data, uint32_t len)
{
uint32_t localHashInt = CRC32_ISO_SEED;
uint32_t localTable[256];
int i, j;
for(i = 0; i < 256; i++)
{
uint32_t entry = (uint32_t)i;
for(j = 0; j < 8; j++)
if((entry & 1) == 1) entry = (entry >> 1) ^ CRC32_ISO_POLY;
else
entry >>= 1;
localTable[i] = entry;
}
for(i = 0; i < len; i++) localHashInt = (localHashInt >> 8) ^ localTable[data[i] ^ (localHashInt & 0xff)];
localHashInt ^= CRC32_ISO_SEED;
return localHashInt;
}
#ifdef __cplusplus
}
#endif

31
tests/crc32.h Normal file
View File

@@ -0,0 +1,31 @@
/*
* This file is part of the Aaru Data Preservation Suite.
* Copyright (c) 2019-2022 Natalia Portillo.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBAARUFORMAT_TESTS_CRC32_H_
#define LIBAARUFORMAT_TESTS_CRC32_H_
#ifdef __cplusplus
extern "C"
{
#endif
uint32_t crc32_data(const uint8_t* data, uint32_t len);
#ifdef __cplusplus
}
#endif
#endif // LIBAARUFORMAT_TESTS_CRC32_H_

BIN
tests/data/audio.bin Normal file

Binary file not shown.

BIN
tests/data/flac.flac Normal file

Binary file not shown.

145
tests/flac.cpp Normal file
View File

@@ -0,0 +1,145 @@
/*
* This file is part of the Aaru Data Preservation Suite.
* Copyright (c) 2019-2022 Natalia Portillo.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <climits>
#include <cstddef>
#include <cstdint>
#include <aaruformat.h>
#include "../include/aaruformat/flac.h"
#include "crc32.h"
#include "gtest/gtest.h"
#define EXPECTED_CRC32 0xdfbc99bb
static const uint8_t* buffer;
class flacFixture : public ::testing::Test
{
public:
flacFixture()
{
// initialization;
// can also be done in SetUp()
}
protected:
void SetUp()
{
char path[PATH_MAX];
char filename[PATH_MAX];
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/flac.flac", path);
FILE* file = fopen(filename, "rb");
buffer = (const uint8_t*)malloc(6534197);
fread((void*)buffer, 1, 6534197, file);
fclose(file);
}
void TearDown() { free((void*)buffer); }
~flacFixture()
{
// resources cleanup, no exceptions allowed
}
// shared user data
};
TEST_F(flacFixture, flac)
{
auto* outBuf = (uint8_t*)malloc(9633792);
auto decoded = aaruf_flac_decode_redbook_buffer(outBuf, 9633792, buffer, 6534197);
EXPECT_EQ(decoded, 9633792);
auto crc = crc32_data(outBuf, 9633792);
free(outBuf);
EXPECT_EQ(crc, EXPECTED_CRC32);
}
TEST_F(flacFixture, flacCompress)
{
size_t original_len = 9633792;
uint cmp_len = original_len;
uint decmp_len = original_len;
char path[PATH_MAX];
char filename[PATH_MAX * 2];
FILE* file;
uint32_t original_crc, decmp_crc;
const uint8_t* original;
uint8_t* cmp_buffer;
uint8_t* decmp_buffer;
size_t newSize;
// Allocate buffers
original = (const uint8_t*)malloc(original_len);
cmp_buffer = (uint8_t*)malloc(cmp_len);
decmp_buffer = (uint8_t*)malloc(decmp_len);
// Read the file
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/audio.bin", path);
file = fopen(filename, "rb");
fread((void*)original, 1, original_len, file);
fclose(file);
// Calculate the CRC
original_crc = crc32_data(original, original_len);
// Compress
newSize = aaruf_flac_encode_redbook_buffer(cmp_buffer,
cmp_len,
original,
original_len,
4608,
1,
0,
"partial_tukey(0/1.0/1.0)",
12,
0,
1,
false,
0,
8,
"Aaru.Compression.Native.Tests",
strlen("Aaru.Compression.Native.Tests"));
cmp_len = newSize;
// Decompress
newSize = aaruf_flac_decode_redbook_buffer(decmp_buffer, decmp_len, cmp_buffer, cmp_len);
decmp_len = newSize;
EXPECT_EQ(decmp_len, original_len);
decmp_crc = crc32_data(decmp_buffer, decmp_len);
// Free buffers
free((void*)original);
free(cmp_buffer);
free(decmp_buffer);
EXPECT_EQ(decmp_crc, original_crc);
}