Skip to content

Commit fc48beb

Browse files
committed
Cleanup decoding logic
1 parent 17c28c8 commit fc48beb

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

src/AACDecoderHelix.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,14 @@ class AACDecoderHelix : public CommonHelix {
111111
uint8_t *data = frame_buffer.data();
112112
int rc = AACDecode(decoder, &data, &bytes_left, (short *)pcm_buffer.data());
113113
if (rc == 0) {
114-
processed = data - frame_buffer.data();
114+
int processed = data - frame_buffer.data();
115115
// return the decoded result
116116
_AACFrameInfo info;
117117
AACGetLastFrameInfo(decoder, &info);
118118
provideResult(info);
119+
rc = processed;
119120
}
120-
return processed;
121+
return rc;
121122
}
122123

123124
// return the result PCM data

src/CommonHelix.h

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,34 +168,39 @@ class CommonHelix {
168168
return rc;
169169
}
170170

171-
/// advance on invalid data, returns true if we need to continue the
171+
/// advance data, returns true if we need to continue the
172172
/// processing
173173
bool resynch(int rc) {
174174
LOGD_HELIX("resynch: %d", rc);
175-
// reset 0 result counter
176-
if (rc != 0) parse_0_count = 0;
177-
if (rc <= 0) {
178-
if (rc == 0) {
179-
parse_0_count++;
180-
int pos = findSynchWord(SYNCH_WORD_LEN);
181-
LOGD_HELIX("rc: %d - available %d - pos %d", rc,
182-
frame_buffer.available(), pos);
183-
// if we are stuck, request more data and if this does not help we
184-
// remove the invalid data
185-
if (parse_0_count > 2) {
186-
return removeInvalidData(pos);
187-
}
188-
return false;
189-
} else if (rc == -1) {
190-
// underflow
191-
LOGD_HELIX("rc: %d - available %d", rc, frame_buffer.available());
192-
return false;
193-
} else {
194-
// generic error handling: remove the data until the next synch word
195-
int pos = findSynchWord(SYNCH_WORD_LEN + 1);
196-
removeInvalidData(pos);
175+
if (rc > 0) {
176+
// remove processed data
177+
LOGD_HELIX("removing %d bytes", rc);
178+
frame_buffer.clearArray(rc);
179+
// reset 0 result counter on success
180+
parse_0_count = 0;
181+
return true;
182+
} else if (rc == 0) {
183+
// if we are stuck, request more data and if this does not help we
184+
// remove the invalid data
185+
parse_0_count++;
186+
int pos = findSynchWord(SYNCH_WORD_LEN);
187+
LOGD_HELIX("rc: %d - available %d - pos %d", rc,
188+
frame_buffer.available(), pos);
189+
if (parse_0_count > 2) {
190+
return removeInvalidData(pos);
197191
}
192+
return false;
193+
} else if (rc == -1) {
194+
// underflow
195+
LOGD_HELIX("rc: %d - available %d", rc, frame_buffer.available());
196+
return false;
197+
} else if (rc < -1) {
198+
// generic error handling: remove the data until the next synch word
199+
int pos = findSynchWord(SYNCH_WORD_LEN + 1);
200+
removeInvalidData(pos);
201+
return true;
198202
}
203+
199204
return true;
200205
}
201206

@@ -226,8 +231,6 @@ class CommonHelix {
226231
if (!presync()) break;
227232
int rc = decode();
228233
if (!resynch(rc)) break;
229-
// remove processed data
230-
frame_buffer.clearArray(rc);
231234

232235
LOGI_HELIX("rc: %d - available %d", rc, frame_buffer.available());
233236
}

src/MP3DecoderHelix.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,26 @@ class MP3DecoderHelix : public CommonHelix {
109109
}
110110

111111
/// decods the data and removes the decoded frame from the buffer
112+
/// returns the number of bytes that have been processed or a negative
113+
/// error code
112114
int decode() override {
113-
int processed = 0;
114115
int available = frame_buffer.available();
115116
int bytes_left = frame_buffer.available();
116117
LOGI_HELIX( "decode: %d (left:%d)", available, bytes_left);
117118
uint8_t *data = frame_buffer.data();
118119
int rc = MP3Decode(decoder, &data, &bytes_left, (short *)pcm_buffer.data(),
119120
mp3_type);
120121
if (rc == 0) {
121-
processed = data - frame_buffer.data();
122+
int processed = data - frame_buffer.data();
122123
// return the decoded result
123124
MP3FrameInfo info;
124125
MP3GetLastFrameInfo(decoder, &info);
125126
provideResult(info);
127+
rc = processed;
126128
} else {
127129
LOGI_HELIX( "MP3Decode rc: %d", rc);
128130
}
129-
return processed;
131+
return rc;
130132
}
131133

132134
// return the resulting PCM data

src/utils/Buffers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ class SingleBuffer : public BaseBuffer<T> {
221221

222222
/// consumes len bytes and moves current data to the beginning
223223
int clearArray(int len) override{
224+
if (len<=0) return 0;
224225
int len_available = available();
225226
if (len>available()) {
226227
reset();

0 commit comments

Comments
 (0)