Skip to content

Commit b1646a9

Browse files
committed
Correct allocation logic
1 parent b142ff4 commit b1646a9

File tree

4 files changed

+85
-66
lines changed

4 files changed

+85
-66
lines changed

src/AACDecoderHelix.h

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include "CommonHelix.h"
44
#include "libhelix-aac/aacdec.h"
55

6-
#define AAC_MAX_OUTPUT_SIZE 2048
7-
#define AAC_MAX_FRAME_SIZE 1600
6+
#define AAC_MAX_OUTPUT_SIZE 1024 * 3
7+
#define AAC_MAX_FRAME_SIZE 2100
88

99
namespace libhelix {
1010

@@ -19,24 +19,20 @@ typedef void (*AACDataCallback)(_AACFrameInfo &info,short *pwm_buffer, size_t le
1919
*/
2020
class AACDecoderHelix : public CommonHelix {
2121
public:
22-
AACDecoderHelix() {
23-
decoder = AACInitDecoder();
24-
}
22+
AACDecoderHelix() = default;
2523

2624
#ifdef ARDUINO
2725
AACDecoderHelix(Print &output, AACInfoCallback infoCallback=nullptr){
28-
decoder = AACInitDecoder();
2926
this->out = &output;
3027
this->infoCallback = infoCallback;
3128
}
3229
#endif
3330
AACDecoderHelix(AACDataCallback dataCallback){
34-
decoder = AACInitDecoder();
3531
this->pwmCallback = dataCallback;
3632
}
3733

38-
~AACDecoderHelix(){
39-
AACFreeDecoder(decoder);
34+
virtual ~AACDecoderHelix(){
35+
end();
4036
}
4137

4238

@@ -50,10 +46,11 @@ class AACDecoderHelix : public CommonHelix {
5046

5147

5248
/// Releases the reserved memory
53-
void end(){
54-
LOG(Debug, "end");
55-
if (CommonHelix::active){
49+
virtual void end() override {
50+
LOG_HELIX(Debug, "end");
51+
if (decoder!=nullptr){
5652
AACFreeDecoder(decoder);
53+
decoder = nullptr;
5754
}
5855
CommonHelix::end();
5956
}
@@ -69,22 +66,29 @@ class AACDecoderHelix : public CommonHelix {
6966
AACInfoCallback infoCallback = nullptr;
7067
_AACFrameInfo aacFrameInfo;
7168

72-
size_t maxFrameSize(){
69+
/// Allocate the decoder
70+
virtual void allocateDecoder() override {
71+
if (decoder==nullptr){
72+
decoder = AACInitDecoder();
73+
}
74+
}
75+
76+
size_t maxFrameSize() override {
7377
return max_frame_size == 0 ? AAC_MAX_FRAME_SIZE : max_frame_size;
7478
}
7579

76-
size_t maxPWMSize() {
80+
size_t maxPWMSize() override {
7781
return max_pwm_size == 0 ? AAC_MAX_OUTPUT_SIZE : max_pwm_size;
7882
}
7983

80-
int findSynchWord(int offset=0) {
84+
int findSynchWord(int offset=0) override {
8185
int result = AACFindSyncWord(frame_buffer+offset, buffer_size)+offset;
8286
return result < 0 ? result : result + offset;
8387
}
8488

8589
/// decods the data and removes the decoded frame from the buffer
86-
void decode(Range r) {
87-
LOG(Debug, "decode %d", r.end);
90+
void decode(Range r) override {
91+
LOG_HELIX(Debug, "decode %d", r.end);
8892
int len = buffer_size - r.start;
8993
int bytesLeft = len;
9094
uint8_t* ptr = frame_buffer + r.start;
@@ -93,8 +97,8 @@ class AACDecoderHelix : public CommonHelix {
9397
int decoded = len - bytesLeft;
9498
assert(decoded == ptr-(frame_buffer + r.start));
9599
if (result==0){
96-
LOG(Debug, "-> bytesLeft %d -> %d = %d ", buffer_size, bytesLeft, decoded);
97-
LOG(Debug, "-> End of frame (%d) vs end of decoding (%d)", r.end, decoded)
100+
LOG_HELIX(Debug, "-> bytesLeft %d -> %d = %d ", buffer_size, bytesLeft, decoded);
101+
LOG_HELIX(Debug, "-> End of frame (%d) vs end of decoding (%d)", r.end, decoded)
98102

99103
// return the decoded result
100104
_AACFrameInfo info;
@@ -106,14 +110,14 @@ class AACDecoderHelix : public CommonHelix {
106110
buffer_size -= decoded;
107111
//assert(buffer_size<=maxFrameSize());
108112
memmove(frame_buffer, frame_buffer+r.start+decoded, buffer_size);
109-
LOG(Debug, " -> decoded %d bytes - remaining buffer_size: %d", decoded, buffer_size);
113+
LOG_HELIX(Debug, " -> decoded %d bytes - remaining buffer_size: %d", decoded, buffer_size);
110114
} else {
111-
LOG(Warning, " -> decoded %d > buffersize %d", decoded, buffer_size);
115+
LOG_HELIX(Warning, " -> decoded %d > buffersize %d", decoded, buffer_size);
112116
buffer_size = 0;
113117
}
114118
} else {
115119
// decoding error
116-
LOG(Debug, " -> decode error: %d - removing frame!", result);
120+
LOG_HELIX(Debug, " -> decode error: %d - removing frame!", result);
117121
int ignore = decoded;
118122
if (ignore == 0) ignore = r.end;
119123
// We advance to the next synch world
@@ -123,13 +127,12 @@ class AACDecoderHelix : public CommonHelix {
123127
} else {
124128
buffer_size = 0;
125129
}
126-
127130
}
128131
}
129132

130133
// return the result PWM data
131134
void provideResult(_AACFrameInfo &info){
132-
LOG(Debug, "provideResult: %d samples",info.outputSamps);
135+
LOG_HELIX(Debug, "provideResult: %d samples",info.outputSamps);
133136
if (info.outputSamps>0){
134137
// provide result
135138
if(pwmCallback!=nullptr){

src/CommonHelix.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,19 @@ class CommonHelix {
7777
if (active){
7878
end();
7979
}
80+
81+
allocateDecoder();
82+
8083
if (frame_buffer == nullptr) {
81-
LOG(Info,"allocating frame_buffer with %zu bytes", maxFrameSize());
84+
LOG_HELIX(Info,"allocating frame_buffer with %zu bytes", maxFrameSize());
8285
frame_buffer = new uint8_t[maxFrameSize()];
8386
}
8487
if (pwm_buffer == nullptr) {
85-
LOG(Info,"allocating pwm_buffer with %zu bytes", maxPWMSize());
88+
LOG_HELIX(Info,"allocating pwm_buffer with %zu bytes", maxPWMSize());
8689
pwm_buffer = new short[maxPWMSize()];
8790
}
8891
if (pwm_buffer==nullptr || frame_buffer==nullptr){
89-
LOG(Error, "Not enough memory for buffers");
92+
LOG_HELIX(Error, "Not enough memory for buffers");
9093
active = false;
9194
return;
9295
}
@@ -108,7 +111,7 @@ class CommonHelix {
108111
*/
109112

110113
virtual size_t write(const void *in_ptr, size_t in_size) {
111-
LOG(Debug, "write %zu", in_size);
114+
LOG_HELIX(Debug, "write %zu", in_size);
112115
size_t start = 0;
113116
if (active){
114117
uint8_t* ptr8 = (uint8_t* )in_ptr;
@@ -118,15 +121,15 @@ class CommonHelix {
118121
// we have some space left in the buffer
119122
int written_len = writeFrame(ptr8+start, write_len);
120123
start += written_len;
121-
LOG(Info,"-> Written %zu of %zu - Counter %zu", start, in_size, frame_counter);
124+
LOG_HELIX(Info,"-> Written %zu of %zu - Counter %zu", start, in_size, frame_counter);
122125
write_len = min(in_size - start, static_cast<size_t>(maxFrameSize()-buffer_size));
123126
// add delay - e.g. needed by esp32 and esp8266
124127
if (delay_ms>0){
125128
delay(delay_ms);
126129
}
127130
}
128131
} else {
129-
LOG(Warning, "CommonHelix not active");
132+
LOG_HELIX(Warning, "CommonHelix not active");
130133
}
131134

132135
return start;
@@ -156,6 +159,8 @@ class CommonHelix {
156159
Print *out = nullptr;
157160
#endif
158161

162+
virtual void allocateDecoder() = 0;
163+
159164
/// Provides the maximum frame size - this is allocated on the heap and you can reduce the heap size my minimizing this value
160165
virtual size_t maxFrameSize() = 0;
161166

@@ -180,23 +185,23 @@ class CommonHelix {
180185

181186
/// we add the data to the buffer until it is full
182187
size_t appendToBuffer(const void *in_ptr, int in_size){
183-
LOG(Info, "appendToBuffer: %d (at %p)", in_size, frame_buffer);
188+
LOG_HELIX(Info, "appendToBuffer: %d (at %p)", in_size, frame_buffer);
184189
int buffer_size_old = buffer_size;
185190
int process_size = min((int)(maxFrameSize() - buffer_size), in_size);
186191
memmove(frame_buffer+buffer_size, in_ptr, process_size);
187192
buffer_size += process_size;
188193
if (buffer_size>maxFrameSize()){
189-
LOG(Error, "Increase MAX_FRAME_SIZE > %zu", buffer_size);
194+
LOG_HELIX(Error, "Increase MAX_FRAME_SIZE > %zu", buffer_size);
190195
}
191196
assert(buffer_size<=maxFrameSize());
192197

193-
LOG(Debug, "appendToBuffer %d + %d -> %u", buffer_size_old, process_size, buffer_size );
198+
LOG_HELIX(Debug, "appendToBuffer %d + %d -> %u", buffer_size_old, process_size, buffer_size );
194199
return process_size;
195200
}
196201

197202
/// appends the data to the frame buffer and decodes
198203
size_t writeFrame(const void *in_ptr, size_t in_size){
199-
LOG(Debug, "writeFrame %zu", in_size);
204+
LOG_HELIX(Debug, "writeFrame %zu", in_size);
200205
size_t result = 0;
201206
// in the beginning we ingnore all data until we found the first synch word
202207
result = appendToBuffer(in_ptr, in_size);
@@ -205,39 +210,39 @@ class CommonHelix {
205210
if(r.isValid(maxFrameSize())){
206211
decode(r);
207212
} else {
208-
LOG(Warning, " -> invalid frame size: %d / max: %d", (int) r.end-r.start, (int) maxFrameSize());
213+
LOG_HELIX(Warning, " -> invalid frame size: %d / max: %d", (int) r.end-r.start, (int) maxFrameSize());
209214
}
210215
frame_counter++;
211216
return result;
212217
}
213218

214219
/// returns valid start and end synch word.
215220
Range synchronizeFrame() {
216-
LOG(Debug, "synchronizeFrame");
221+
LOG_HELIX(Debug, "synchronizeFrame");
217222
Range range = frameRange();
218223
if (range.start<0){
219224
// there is no Synch in the buffer at all -> we can ignore all data
220225
range.end = -1;
221-
LOG(Debug, "-> no synch")
226+
LOG_HELIX(Debug, "-> no synch")
222227
if (buffer_size==maxFrameSize()) {
223228
buffer_size = 0;
224-
LOG(Debug, "-> buffer cleared");
229+
LOG_HELIX(Debug, "-> buffer cleared");
225230
}
226231
} else if (range.start>0) {
227232
// make sure that buffer starts with a synch word
228-
LOG(Debug, "-> moving to new start %d",range.start);
233+
LOG_HELIX(Debug, "-> moving to new start %d",range.start);
229234
buffer_size -= range.start;
230235
assert(buffer_size<=maxFrameSize());
231236

232237
memmove(frame_buffer, frame_buffer + range.start, buffer_size);
233238
range.end -= range.start;
234239
range.start = 0;
235-
LOG(Debug, "-> we are at beginning of synch word");
240+
LOG_HELIX(Debug, "-> we are at beginning of synch word");
236241
} else if (range.start==0) {
237-
LOG(Debug, "-> we are at beginning of synch word");
242+
LOG_HELIX(Debug, "-> we are at beginning of synch word");
238243
if (range.end<0 && buffer_size == maxFrameSize()){
239244
buffer_size = 0;
240-
LOG(Debug, "-> buffer cleared");
245+
LOG_HELIX(Debug, "-> buffer cleared");
241246
}
242247
}
243248
return range;
@@ -248,7 +253,7 @@ class CommonHelix {
248253
Range result;
249254
result.start = findSynchWord(0);
250255
result.end = findSynchWord(result.start+SYNCH_WORD_LEN);
251-
LOG(Debug, "-> frameRange -> %d - %d", result.start, result.end);
256+
LOG_HELIX(Debug, "-> frameRange -> %d - %d", result.start, result.end);
252257
return result;
253258
}
254259

0 commit comments

Comments
 (0)