1 #include "AACDecoderHelix.h"
2 #include "AudioCodecs/AudioEncoded.h"
12 const int adtsSamplingRates[13] = {96000, 88200, 64000, 48000, 44100,
13 32000, 24000, 22050, 16000, 12000,
16 bool is_valid =
false;
20 uint8_t protection_absent;
22 uint8_t sampling_freq_idx;
25 uint8_t original_copy;
27 uint8_t copyright_id_bit;
28 uint8_t copyright_id_start;
29 uint16_t frame_length;
30 uint8_t adts_buf_fullness;
31 uint8_t num_rawdata_blocks;
32 uint32_t quick_check = 0;
34 bool begin() { quick_check = 0;
return true;}
36 bool parse(uint8_t *hdr) {
37 syncword = (hdr[0] << 4) | (hdr[1] >> 4);
39 id = (hdr[1] >> 3) & 0b1;
40 layer = (hdr[1] >> 1) & 0b11;
41 protection_absent = (hdr[1]) & 0b1;
42 profile = (hdr[2] >> 6) & 0b11;
43 sampling_freq_idx = (hdr[2] >> 2) & 0b1111;
44 private_bit = (hdr[2] >> 1) & 0b1;
45 channel_cfg = ((hdr[2] & 0x01) << 2) | ((hdr[3] & 0xC0) >> 6);
46 original_copy = (hdr[3] >> 5) & 0b1;
47 home = (hdr[3] >> 4) & 0b1;
49 copyright_id_bit = (hdr[3] >> 3) & 0b1;
50 copyright_id_start = (hdr[3] >> 2) & 0b1;
52 frame_length = ((((
unsigned int)hdr[3] & 0x3)) << 11) |
53 (((
unsigned int)hdr[4]) << 3) | (hdr[5] >> 5);
54 adts_buf_fullness = ((hdr[5] & 0b11111) << 6) | (hdr[6] >> 2);
55 num_rawdata_blocks = (hdr[6]) & 0b11;
57 LOGD(
"id:%d layer:%d profile:%d freq:%d channel:%d frame_length:%d",
id,
58 layer, profile, rate(), channel_cfg,
63 if (syncword != 0b111111111111) {
70 if (sampling_freq_idx > 0xb) {
71 LOGD(
"- Invalid sampl.freq");
74 if (channel_cfg > 2) {
75 LOGD(
"- Invalid channels");
78 if (frame_length > 1024) {
79 LOGD(
"- Invalid frame_length");
83 LOGD(
"=> Invalid ADTS");
88 unsigned int size() {
return frame_length; };
91 LOGI(
"%s id:%d layer:%d profile:%d freq:%d channel:%d frame_length:%d",
92 is_valid ?
"+" :
"-",
id, layer, profile,
93 rate(), channel_cfg, frame_length);
97 return sampling_freq_idx>12? sampling_freq_idx : adtsSamplingRates[sampling_freq_idx];
100 bool isSyncWord(uint8_t *buf) {
101 return ((buf[0] & SYNCWORDH) == SYNCWORDH &&
102 (buf[1] & SYNCWORDL) == SYNCWORDL);
105 int findSynchWord(
unsigned char *buf,
int nBytes,
int start = 0) {
107 for (
int i = start; i < nBytes - 1; i++) {
108 if (isSyncWord(buf + i))
return i;
127 bool begin()
override {
129 buffer_write_size = 0;
133 void end()
override { buffer.resize(0); }
136 size_t write(
const void *dataIn,
size_t len)
override {
137 LOGD(
"AACDecoderADTS::write: %d", (
int)len);
140 if (buffer.size() < len) {
145 uint8_t *data = (uint8_t *)dataIn;
147 LOGD(
"buffer size: %d", buffer.
available());
150 if (buffer_write_size == 0) {
154 if (buffer.
available() >= buffer_write_size) {
156 assert(buffer_write_size == parser.size());
158 buffer_write_size = 0;
165 operator bool()
override {
return true; }
170 int buffer_write_size = 0;
174 while (buffer.
available() >= 7 && buffer_write_size == 0) {
175 int pos = parser.findSynchWord(buffer.
data(), buffer.
available());
176 LOGD(
"synchword at %d from %d", pos, buffer.
available());
181 int to_delete = max(7, buffer.
available());
183 LOGW(
"Removed invalid %d bytes", to_delete);
188 void processSync(
int pos) {
191 LOGD(
"Removing %d", pos);
192 assert(parser.isSyncWord(buffer.
data()));
198 if (parser.parse(buffer.
data())) {
203 LOGD(
"Removing invalid synch to restart scanning: %d", buffer.
available());
207 void processValidFrame() {
209 if (buffer.
available() >= parser.size()) {
212 LOGD(
"Expecting more data up to %d", parser.size());
214 buffer_write_size = parser.size();
221 int size = parser.size();
223 LOGD(
"writing ADTS Frame: %d bytes", size);
225 size_t len = p_print->write(buffer.
data(), size);
233 void resizeBuffer() {
234 if (parser.size() > buffer.size()) {
235 LOGI(
"resize buffer %d to %d", (
int)buffer.size(), (
int)parser.size());
236 buffer.resize(parser.size());
237 buffer_write_size = parser.size();