1 #include "AACDecoderHelix.h"
2 #include "AudioTools/AudioCodecs/AudioCodecsBase.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 uint8_t *data,
size_t len)
override {
137 LOGD(
"AACDecoderADTS::write: %d", (
int)len);
140 if (buffer.size() < len) {
146 LOGD(
"buffer size: %d", buffer.
available());
149 if (buffer_write_size == 0) {
153 if (buffer.
available() >= buffer_write_size) {
155 assert(buffer_write_size == parser.size());
157 buffer_write_size = 0;
164 operator bool()
override {
return true; }
169 int buffer_write_size = 0;
173 while (buffer.
available() >= 7 && buffer_write_size == 0) {
174 int pos = parser.findSynchWord(buffer.
data(), buffer.
available());
175 LOGD(
"synchword at %d from %d", pos, buffer.
available());
180 int to_delete = max(7, buffer.
available());
182 LOGW(
"Removed invalid %d bytes", to_delete);
187 void processSync(
int pos) {
190 LOGD(
"Removing %d", pos);
191 assert(parser.isSyncWord(buffer.
data()));
197 if (parser.parse(buffer.
data())) {
202 LOGD(
"Removing invalid synch to restart scanning: %d", buffer.
available());
206 void processValidFrame() {
208 if (buffer.
available() >= parser.size()) {
211 LOGD(
"Expecting more data up to %d", parser.size());
213 buffer_write_size = parser.size();
220 int size = parser.size();
222 LOGD(
"writing ADTS Frame: %d bytes", size);
224 size_t len = p_print->write(buffer.
data(), size);
232 void resizeBuffer() {
233 if (parser.size() > buffer.size()) {
234 LOGI(
"resize buffer %d to %d", (
int)buffer.size(), (
int)parser.size());
235 buffer.resize(parser.size());
236 buffer_write_size = parser.size();