1 /*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <fcntl.h>
18 #include <gtest/gtest.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <fstream>
24 #include <iostream>
25
26 #include "aptXHDbtenc.h"
27
28 #define BYTES_PER_CODEWORD 24
29
30 class LibAptxHdEncTest : public ::testing::Test {
31 private:
32 protected:
33 void* aptxhdbtenc = nullptr;
SetUp()34 void SetUp() override {
35 aptxhdbtenc = malloc(SizeofAptxhdbtenc());
36 ASSERT_NE(aptxhdbtenc, nullptr);
37 ASSERT_EQ(aptxhdbtenc_init(aptxhdbtenc, 0), 0);
38 }
39
TearDown()40 void TearDown() override { free(aptxhdbtenc); }
41
codeword_cmp(const uint8_t p[BYTES_PER_CODEWORD],const uint32_t codeword[2])42 void codeword_cmp(const uint8_t p[BYTES_PER_CODEWORD],
43 const uint32_t codeword[2]) {
44 uint32_t pcmL[4];
45 uint32_t pcmR[4];
46 for (size_t i = 0; i < 4; i++) {
47 pcmL[i] = ((p[0] << 0) | (p[1] << 8) | (((int8_t)p[2]) << 16));
48 p += 3;
49 pcmR[i] = ((p[0] << 0) | (p[1] << 8) | (((int8_t)p[2]) << 16));
50 p += 3;
51 }
52 uint32_t encoded_sample[2];
53 aptxhdbtenc_encodestereo(aptxhdbtenc, &pcmL, &pcmR, (void*)encoded_sample);
54
55 ASSERT_EQ(encoded_sample[0], codeword[0]);
56 ASSERT_EQ(encoded_sample[1], codeword[1]);
57 }
58 };
59
TEST_F(LibAptxHdEncTest,encode_fake_data)60 TEST_F(LibAptxHdEncTest, encode_fake_data) {
61 const char input[] =
62 "012345678901234567890123456789012345678901234567890123456789012345678901"
63 "234567890123456789012345678901234567890123456789";
64 const uint32_t aptxhd_codeword[] = {7585535, 7585535, 32767, 32767,
65 557055, 557027, 7586105, 7586109,
66 9748656, 10764446};
67
68 ASSERT_EQ((sizeof(input) - 1) % BYTES_PER_CODEWORD, 0);
69 ASSERT_EQ((sizeof(input) - 1) / BYTES_PER_CODEWORD,
70 sizeof(aptxhd_codeword) / sizeof(uint32_t) / 2);
71
72 size_t idx = 0;
73
74 uint8_t pcm[BYTES_PER_CODEWORD];
75 while (idx * BYTES_PER_CODEWORD < sizeof(input) - 1) {
76 memcpy(pcm, input + idx * BYTES_PER_CODEWORD, BYTES_PER_CODEWORD);
77 codeword_cmp(pcm, aptxhd_codeword + idx * 2);
78 ++idx;
79 }
80 }
81