arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
CodecADTS.h
1#pragma once
2#include "AudioTools/AudioCodecs/AudioCodecsBase.h"
3
4namespace audio_tools {
5
6#ifndef SYNCWORDH
7#define SYNCWORDH 0xff
8#define SYNCWORDL 0xf0
9#endif
10
11#define ERROR_FMT_CHANGE "- Invalid ADTS change: %s"
12#define ERROR_FMT "- Invalid ADTS: %s (0x%x)"
13
19 public:
20 struct ADTSHeader {
21 uint16_t syncword = 0;
22 uint8_t id = 0;
23 uint8_t layer = 0;
24 uint8_t protection_absent = 0;
25 uint8_t profile = 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;
30 uint8_t home = 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;
36 };
37
38 bool begin() {
39 is_first = true;
40 is_valid = true;
41 return true;
42 }
43
44 bool parse(uint8_t *hdr) {
45 header.syncword = (hdr[0] << 4) | (hdr[1] >> 4);
46 // parse fixed header
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;
56 // parse variable header
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;
63
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);
67
68 // check
69 is_valid = check();
70 return is_valid;
71 }
72
73 uint32_t getFrameLength() { return header.frame_length; };
74
75 void log() {
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);
79 }
80
81 int getSampleRate() {
82 return header.sampling_freq_idx > 12
83 ? header.sampling_freq_idx
84 : (int)adtsSamplingRates[header.sampling_freq_idx];
85 }
86
87 bool isSyncWord(const uint8_t *buf) {
88 return ((buf[0] & SYNCWORDH) == SYNCWORDH &&
89 (buf[1] & SYNCWORDL) == SYNCWORDL);
90 }
91
92 int findSyncWord(const uint8_t *buf, int nBytes, int start = 0) {
93 /* find byte-aligned syncword (12 bits = 0xFFF) */
94 for (int i = start; i < nBytes - 1; i++) {
95 if (isSyncWord(buf + i)) return i;
96 }
97 return -1;
98 }
99
100 ADTSHeader &data() { return header; }
101
102 protected:
103 const int adtsSamplingRates[13] = {96000, 88200, 64000, 48000, 44100,
104 32000, 24000, 22050, 16000, 12000,
105 11025, 8000, 7350};
106
107 ADTSHeader header;
108 ADTSHeader header_ref;
109 bool is_first = true;
110 bool is_valid = false;
111
112 bool check() {
113 if (header.syncword != 0b111111111111) {
114 LOGW(ERROR_FMT, "sync", (int)header.syncword);
115 is_valid = false;
116 }
117 if (header.id > 6) {
118 LOGW(ERROR_FMT, "id", (int)header.id);
119 is_valid = false;
120 }
121 if (header.sampling_freq_idx > 0xb) {
122 LOGW(ERROR_FMT, "freq", (int)header.sampling_freq_idx);
123 is_valid = false;
124 }
125 // valid value 0-7
126 // if (header.channel_cfg == 0 || header.channel_cfg > 7) {
127 if (header.channel_cfg > 7) {
128 LOGW(ERROR_FMT, "channels", (int)header.channel_cfg);
129 is_valid = false;
130 }
131 if (header.frame_length > 8191) { // tymically <= 768
132 LOGW(ERROR_FMT, "frame_length", (int)header.frame_length);
133 is_valid = false;
134 }
135 // on subsequent checks we need to compare with the first header
136 if (!is_first) {
137 is_valid = checkRef();
138 }
139 if (is_valid) {
140 is_first = false;
141 header_ref = header;
142 }
143 return is_valid;
144 }
145
146 bool checkRef() {
147 char msg[200] = "";
148 bool is_valid = true;
149 if (header.id != header_ref.id) {
150 strcat(msg, "id ");
151 is_valid = false;
152 }
153 if (header.layer != header_ref.layer) {
154 strcat(msg, "layer ");
155 is_valid = false;
156 }
157 if (header.profile != header_ref.profile) {
158 strcat(msg, "profile ");
159 is_valid = false;
160 }
161 if (header.sampling_freq_idx != header_ref.sampling_freq_idx) {
162 strcat(msg, "freq ");
163 is_valid = false;
164 }
165 if (header.channel_cfg != header_ref.channel_cfg) {
166 strcat(msg, "channel ");
167 is_valid = false;
168 }
169 if (header.adts_buf_fullness != header_ref.adts_buf_fullness) {
170 strcat(msg, "fullness");
171 is_valid = false;
172 }
173 if (!is_valid) {
174 LOGW(ERROR_FMT_CHANGE, msg);
175 }
176 return is_valid;
177 }
178};
179
191class ADTSDecoder : public AudioDecoder {
192 public:
193 ADTSDecoder() = default;
194 ADTSDecoder(AudioDecoder &dec) { p_dec = &dec; };
195
196 bool begin() override {
197 parser.begin();
198 if (p_dec) p_dec->begin();
199 return true;
200 }
201
202 void end() override {
203 parseBuffer();
204 writeData(out_buffer.data(), out_buffer.available());
205 out_buffer.reset();
206 buffer.resize(0);
207 if (p_dec) p_dec->end();
208 }
209
211 size_t write(const uint8_t *data, size_t len) override {
212 LOGI("AACDecoderADTS::write: %d", (int)len);
213
214 parseBuffer();
215
216 // write data to buffer
217 size_t result = buffer.writeArray(data, len);
218 // assert(result == len);
219 LOGD("buffer size: %d", buffer.available());
220
221 return result;
222 }
223
225 operator bool() override { return true; }
226
229 void setOutputBufferSize(int size) { out_buffer.resize(size); }
230
232 void setParseBufferSize(int size) { buffer.resize(size); }
233
235 void setOutput(AudioStream &out_stream) override {
236 if (p_dec) {
237 p_dec->setOutput(out_stream);
238 } else {
239 AudioDecoder::setOutput(out_stream);
240 }
241 }
242
244 void setOutput(AudioOutput &out_stream) override {
245 if (p_dec) {
246 p_dec->setOutput(out_stream);
247 } else {
248 AudioDecoder::setOutput(out_stream);
249 }
250 }
251
253 void setOutput(Print &out_stream) override {
254 if (p_dec) {
255 p_dec->setOutput(out_stream);
256 } else {
257 AudioDecoder::setOutput(out_stream);
258 }
259 }
260
261 protected:
262 SingleBuffer<uint8_t> buffer{DEFAULT_BUFFER_SIZE};
263 SingleBuffer<uint8_t> out_buffer;
264 ADTSParser parser;
265 AudioDecoder *p_dec = nullptr;
266
267 void parseBuffer() {
268 TRACED();
269
270 // Need at least 7 bytes for a valid ADTS header
271 while (true) {
272 if (buffer.available() <= 5) return;
273 // Needs to contain sync word
274 int syncPos = parser.findSyncWord(buffer.data(), buffer.available());
275 if (syncPos < 0) {
276 return;
277 }
278 // buffer needs to start with sync word
279 if (syncPos > 0) {
280 buffer.clearArray(syncPos);
281 LOGI("Cleared %d bytes", syncPos);
282 }
283 // assert(parser.findSyncWord(buffer.data(), buffer.available()) == 0);
284 // Try to parse the header
285 if (parser.parse(buffer.data())) {
286 // Get the frame length which includes the header
287 uint16_t frameLength = parser.getFrameLength();
288 if (frameLength > buffer.available()) {
289 // not enough data
290 return;
291 }
292 // write data to decoder
293 if (out_buffer.size() > 0) {
294 writeDataBuffered(buffer.data(), frameLength);
295 } else {
296 writeData(buffer.data(), frameLength);
297 }
298 buffer.clearArray(frameLength);
299 } else {
300 LOGI("Invalid ADTS header");
301 // ignore data and move to next synch word
302 int pos = parser.findSyncWord(buffer.data(), buffer.available(), 5);
303 if (pos < 0) {
304 // no more sync word found
305 buffer.reset();
306 } else {
307 buffer.clearArray(pos);
308 }
309 }
310 }
311 }
312
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());
319 out_buffer.reset();
320 }
321 }
322 return size;
323 }
324 size_t writeData(uint8_t *data, size_t size) {
325 LOGI("writeData: %d", (int)size);
326 if (p_print) {
327 size_t len = ::writeData<uint8_t>(p_print, data, size);
328 assert(len == size);
329 return (len == size);
330 }
331 if (p_dec) {
332 LOGI("write to decoder: %d", (int)size);
333 size_t len = writeDataT<uint8_t, AudioDecoder>(p_dec, data, size);
334 assert(len == size);
335 return (len == size);
336 }
337 return 0;
338 }
339};
340
341} // namespace audio_tools
Audio Data Transport Stream (ADTS) is a format similar to Audio Data Interchange Format (ADIF),...
Definition CodecADTS.h:191
void setOutput(Print &out_stream) override
Defines where the decoded result is written to.
Definition CodecADTS.h:253
void setParseBufferSize(int size)
Defines the parse buffer size: default is 1024.
Definition CodecADTS.h:232
size_t write(const uint8_t *data, size_t len) override
Write AAC data to decoder.
Definition CodecADTS.h:211
void setOutputBufferSize(int size)
Definition CodecADTS.h:229
void setOutput(AudioStream &out_stream) override
Defines where the decoded result is written to.
Definition CodecADTS.h:235
void setOutput(AudioOutput &out_stream) override
Defines where the decoded result is written to.
Definition CodecADTS.h:244
Structure to hold ADTS header field values.
Definition CodecADTS.h:18
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:18
virtual void setOutput(AudioStream &out_stream)
Defines where the decoded result is written to.
Definition AudioCodecsBase.h:36
Abstract Audio Ouptut class.
Definition AudioOutput.h:22
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:119
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:59
Definition NoArduino.h:62
A simple Buffer implementation which just uses a (dynamically sized) array.
Definition Buffers.h:175
int available() override
provides the number of entries that are available to read
Definition Buffers.h:227
T * data()
Provides address of actual data.
Definition Buffers.h:261
void reset() override
clears the buffer
Definition Buffers.h:263
int clearArray(int len) override
consumes len bytes and moves current data to the beginning
Definition Buffers.h:237
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Definition CodecADTS.h:20