21 uint16_t syncword = 0;
24 uint8_t protection_absent = 0;
26 uint8_t sampling_freq_idx = 0;
27 uint8_t private_bit = 0;
28 uint8_t channel_cfg = 0;
29 uint8_t original_copy = 0;
31 uint8_t copyright_id_bit = 0;
32 uint8_t copyright_id_start = 0;
33 uint16_t frame_length = 0;
34 uint8_t adts_buf_fullness = 0;
35 uint8_t num_rawdata_blocks = 0;
44 bool parse(uint8_t *hdr) {
45 header.syncword = (hdr[0] << 4) | (hdr[1] >> 4);
47 header.id = (hdr[1] >> 3) & 0b1;
48 header.layer = (hdr[1] >> 1) & 0b11;
49 header.protection_absent = (hdr[1]) & 0b1;
50 header.profile = (hdr[2] >> 6) & 0b11;
51 header.sampling_freq_idx = (hdr[2] >> 2) & 0b1111;
52 header.private_bit = (hdr[2] >> 1) & 0b1;
53 header.channel_cfg = ((hdr[2] & 0x01) << 2) | ((hdr[3] & 0xC0) >> 6);
54 header.original_copy = (hdr[3] >> 5) & 0b1;
55 header.home = (hdr[3] >> 4) & 0b1;
57 header.copyright_id_bit = (hdr[3] >> 3) & 0b1;
58 header.copyright_id_start = (hdr[3] >> 2) & 0b1;
59 header.frame_length = ((((
unsigned int)hdr[3] & 0x3)) << 11) |
60 (((
unsigned int)hdr[4]) << 3) | (hdr[5] >> 5);
61 header.adts_buf_fullness = ((hdr[5] & 0b11111) << 6) | (hdr[6] >> 2);
62 header.num_rawdata_blocks = (hdr[6]) & 0b11;
64 LOGD(
"id:%d layer:%d profile:%d freq:%d channel:%d frame_length:%d",
65 header.id, header.layer, header.profile, getSampleRate(),
66 header.channel_cfg, header.frame_length);
73 uint32_t getFrameLength() {
return header.frame_length; };
76 LOGI(
"%s id:%d layer:%d profile:%d freq:%d channel:%d frame_length:%d",
77 is_valid ?
"+" :
"-", header.id, header.layer, header.profile,
78 getSampleRate(), header.channel_cfg, header.frame_length);
82 return header.sampling_freq_idx > 12
83 ? header.sampling_freq_idx
84 : (int)adtsSamplingRates[header.sampling_freq_idx];
87 bool isSyncWord(
const uint8_t *buf) {
88 return ((buf[0] & SYNCWORDH) == SYNCWORDH &&
89 (buf[1] & SYNCWORDL) == SYNCWORDL);
92 int findSyncWord(
const uint8_t *buf,
int nBytes,
int start = 0) {
94 for (
int i = start; i < nBytes - 1; i++) {
95 if (isSyncWord(buf + i))
return i;
100 ADTSHeader &data() {
return header; }
103 const int adtsSamplingRates[13] = {96000, 88200, 64000, 48000, 44100,
104 32000, 24000, 22050, 16000, 12000,
108 ADTSHeader header_ref;
109 bool is_first =
true;
110 bool is_valid =
false;
113 if (header.syncword != 0b111111111111) {
114 LOGW(ERROR_FMT,
"sync", (
int)header.syncword);
118 LOGW(ERROR_FMT,
"id", (
int)header.id);
121 if (header.sampling_freq_idx > 0xb) {
122 LOGW(ERROR_FMT,
"freq", (
int)header.sampling_freq_idx);
127 if (header.channel_cfg > 7) {
128 LOGW(ERROR_FMT,
"channels", (
int)header.channel_cfg);
131 if (header.frame_length > 8191) {
132 LOGW(ERROR_FMT,
"frame_length", (
int)header.frame_length);
137 is_valid = checkRef();
148 bool is_valid =
true;
149 if (header.id != header_ref.id) {
153 if (header.layer != header_ref.layer) {
154 strcat(msg,
"layer ");
157 if (header.profile != header_ref.profile) {
158 strcat(msg,
"profile ");
161 if (header.sampling_freq_idx != header_ref.sampling_freq_idx) {
162 strcat(msg,
"freq ");
165 if (header.channel_cfg != header_ref.channel_cfg) {
166 strcat(msg,
"channel ");
169 if (header.adts_buf_fullness != header_ref.adts_buf_fullness) {
170 strcat(msg,
"fullness");
174 LOGW(ERROR_FMT_CHANGE, msg);
196 bool begin()
override {
198 if (p_dec) p_dec->begin();
202 void end()
override {
207 if (p_dec) p_dec->end();
211 size_t write(
const uint8_t *data,
size_t len)
override {
212 LOGI(
"AACDecoderADTS::write: %d", (
int)len);
219 LOGD(
"buffer size: %d", buffer.
available());
211 size_t write(
const uint8_t *data,
size_t len)
override {
…}
225 operator bool()
override {
return true; }
263 SingleBuffer<uint8_t> out_buffer;
265 AudioDecoder *p_dec =
nullptr;
274 int syncPos = parser.findSyncWord(buffer.
data(), buffer.
available());
281 LOGI(
"Cleared %d bytes", syncPos);
285 if (parser.parse(buffer.
data())) {
287 uint16_t frameLength = parser.getFrameLength();
293 if (out_buffer.size() > 0) {
294 writeDataBuffered(buffer.
data(), frameLength);
296 writeData(buffer.
data(), frameLength);
300 LOGI(
"Invalid ADTS header");
302 int pos = parser.findSyncWord(buffer.
data(), buffer.
available(), 5);
313 size_t writeDataBuffered(uint8_t *data,
size_t size) {
314 LOGI(
"writeDataBuffered: %d", (
int)size);
315 for (
int j = 0; j < size; j++) {
316 out_buffer.write(data[j]);
317 if (out_buffer.isFull()) {
318 writeData(out_buffer.data(), out_buffer.available());
324 size_t writeData(uint8_t *data,
size_t size) {
325 LOGI(
"writeData: %d", (
int)size);
327 size_t len = ::writeData<uint8_t>(p_print, data, size);
329 return (len == size);
332 LOGI(
"write to decoder: %d", (
int)size);
333 size_t len = writeDataT<uint8_t, AudioDecoder>(p_dec, data, size);
335 return (len == size);