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], const uint32_t codeword[2]) {
43     uint32_t pcmL[4];
44     uint32_t pcmR[4];
45     for (size_t i = 0; i < 4; i++) {
46       pcmL[i] = ((p[0] << 0) | (p[1] << 8) | (((int8_t)p[2]) << 16));
47       p += 3;
48       pcmR[i] = ((p[0] << 0) | (p[1] << 8) | (((int8_t)p[2]) << 16));
49       p += 3;
50     }
51     uint32_t encoded_sample[2];
52     aptxhdbtenc_encodestereo(aptxhdbtenc, &pcmL, &pcmR, (void*)encoded_sample);
53 
54     ASSERT_EQ(encoded_sample[0], codeword[0]);
55     ASSERT_EQ(encoded_sample[1], codeword[1]);
56   }
57 };
58 
TEST_F(LibAptxHdEncTest,encode_fake_data)59 TEST_F(LibAptxHdEncTest, encode_fake_data) {
60   const char input[] =
61           "012345678901234567890123456789012345678901234567890123456789012345678901"
62           "234567890123456789012345678901234567890123456789";
63   const uint32_t aptxhd_codeword[] = {7585535, 7585535, 32767,   32767,   557055,
64                                       557027,  7586105, 7586109, 9748656, 10764446};
65 
66   ASSERT_EQ((sizeof(input) - 1) % BYTES_PER_CODEWORD, 0);
67   ASSERT_EQ((sizeof(input) - 1) / BYTES_PER_CODEWORD,
68             sizeof(aptxhd_codeword) / sizeof(uint32_t) / 2);
69 
70   size_t idx = 0;
71 
72   uint8_t pcm[BYTES_PER_CODEWORD];
73   while (idx * BYTES_PER_CODEWORD < sizeof(input) - 1) {
74     memcpy(pcm, input + idx * BYTES_PER_CODEWORD, BYTES_PER_CODEWORD);
75     codeword_cmp(pcm, aptxhd_codeword + idx * 2);
76     ++idx;
77   }
78 }
79