-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathaudio_mp3.cpp
More file actions
343 lines (281 loc) · 13.2 KB
/
Copy pathaudio_mp3.cpp
File metadata and controls
343 lines (281 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <string.h>
#include "audio_log.h"
#include "audio_mp3.h"
#include "audio_stream_io.h"
static const char *TAG = "mp3";
bool is_mp3(FILE *fp) {
bool is_mp3_file = false;
fseek(fp, 0, SEEK_SET);
// see https://en.wikipedia.org/wiki/List_of_file_signatures
uint8_t magic[3];
if(sizeof(magic) == fread(magic, 1, sizeof(magic), fp)) {
if((magic[0] == 0xFF) &&
(magic[1] == 0xFB))
{
is_mp3_file = true;
} else if((magic[0] == 0xFF) &&
(magic[1] == 0xF3))
{
is_mp3_file = true;
} else if((magic[0] == 0xFF) &&
(magic[1] == 0xF2))
{
is_mp3_file = true;
} else if((magic[0] == 0x49) &&
(magic[1] == 0x44) &&
(magic[2] == 0x33)) /* 'ID3' */
{
fseek(fp, 0, SEEK_SET);
/* Get ID3 head */
mp3_id3_header_v2_t tag;
if (sizeof(mp3_id3_header_v2_t) == fread(&tag, 1, sizeof(mp3_id3_header_v2_t), fp)) {
if (memcmp("ID3", (const void *) &tag, sizeof(tag.header)) == 0) {
is_mp3_file = true;
}
}
}
}
// seek back to the start of the file to avoid
// missing frames upon decode
fseek(fp, 0, SEEK_SET);
return is_mp3_file;
}
/**
* @return true if data remains, false on error or end of file
*/
DECODE_STATUS decode_mp3(HMP3Decoder mp3_decoder, FILE *fp, decode_data *pData, mp3_instance *pInstance) {
MP3FrameInfo frame_info;
size_t unread_bytes = pInstance->bytes_in_data_buf - (pInstance->read_ptr - pInstance->data_buf);
/* somewhat arbitrary trigger to refill buffer - should always be enough for a full frame */
if (unread_bytes < 1.25 * MAINBUF_SIZE && !pInstance->eof_reached) {
uint8_t *write_ptr = pInstance->data_buf + unread_bytes;
size_t free_space = pInstance->data_buf_size - unread_bytes;
/* move last, small chunk from end of buffer to start,
then fill with new data */
memmove(pInstance->data_buf, pInstance->read_ptr, unread_bytes);
size_t nRead = fread(write_ptr, 1, free_space, fp);
pInstance->bytes_in_data_buf = unread_bytes + nRead;
pInstance->read_ptr = pInstance->data_buf;
if ((nRead == 0) || feof(fp)) {
pInstance->eof_reached = true;
}
LOGI_2("pos %ld, nRead %d, eof %d", ftell(fp), nRead, pInstance->eof_reached);
unread_bytes = pInstance->bytes_in_data_buf;
}
LOGI_3("data_buf 0x%p, read 0x%p", pInstance->data_buf, pInstance->read_ptr);
if(unread_bytes == 0) {
LOGI_1("unread_bytes == 0, status done");
return DECODE_STATUS_DONE;
}
/* Find MP3 sync word from read buffer */
int offset = MP3FindSyncWord(pInstance->read_ptr, unread_bytes);
LOGI_2("unread %d, total %d, offset 0x%x(%d)",
unread_bytes, pInstance->bytes_in_data_buf, offset, offset);
if (offset >= 0) {
COMPILE_3(int starting_unread_bytes = unread_bytes);
uint8_t *read_ptr = pInstance->read_ptr + offset; /*!< Data start point */
unread_bytes -= offset;
LOGI_3("read 0x%p, unread %d", read_ptr, unread_bytes);
int mp3_dec_err = MP3Decode(mp3_decoder, &read_ptr, (int*)&unread_bytes, reinterpret_cast<int16_t *>(pData->samples),
0);
pInstance->read_ptr = read_ptr;
if(mp3_dec_err == ERR_MP3_NONE) {
/* Get MP3 frame info */
MP3GetLastFrameInfo(mp3_decoder, &frame_info);
pData->fmt.sample_rate = frame_info.samprate;
pData->fmt.bits_per_sample = frame_info.bitsPerSample;
pData->fmt.channels = frame_info.nChans;
pData->frame_count = (frame_info.outputSamps / frame_info.nChans);
LOGI_3("mp3: channels %d, sr %d, bps %d, frame_count %d, processed %d",
pData->fmt.channels,
pData->fmt.sample_rate,
pData->fmt.bits_per_sample,
frame_info.outputSamps,
starting_unread_bytes - unread_bytes);
} else {
if (pInstance->eof_reached) {
ESP_LOGE(TAG, "status error %d, but EOF", mp3_dec_err);
return DECODE_STATUS_DONE;
} else if (mp3_dec_err == ERR_MP3_MAINDATA_UNDERFLOW) {
// underflow indicates MP3Decode should be called again
LOGI_1("underflow read ptr is 0x%p", read_ptr);
return DECODE_STATUS_NO_DATA_CONTINUE;
} else {
// NOTE: some mp3 files result in misdetection of mp3 frame headers
// and during decode these misdetected frames cannot be
// decoded
//
// Rather than give up on the file by returning
// DECODE_STATUS_ERROR, we ask the caller
// to continue to call us, by returning DECODE_STATUS_NO_DATA_CONTINUE.
//
// The invalid frame data is skipped over as a search for the next frame
// on the subsequent call to this function will start searching
// AFTER the misdetected frmame header, dropping the invalid data.
//
// We may want to consider a more sophisticated approach here at a later time.
ESP_LOGE(TAG, "status error %d", mp3_dec_err);
return DECODE_STATUS_NO_DATA_CONTINUE;
}
}
} else {
// if we are dropping data there were no frames decoded
pData->frame_count = 0;
// drop an even count of words
size_t words_to_drop = unread_bytes / BYTES_IN_WORD;
size_t bytes_to_drop = words_to_drop * BYTES_IN_WORD;
// if the unread bytes is less than BYTES_IN_WORD, we should drop any unread bytes
// to avoid the situation where the file could have a few extra bytes at the end
// of the file that isn't at least BYTES_IN_WORD and decoding would get stuck
if(unread_bytes < BYTES_IN_WORD) {
bytes_to_drop = unread_bytes;
}
// shift the read_ptr to drop the bytes in the buffer
pInstance->read_ptr += bytes_to_drop;
/* Sync word not found in frame. Drop data that was read until a word boundary */
ESP_LOGE(TAG, "MP3 sync word not found, dropping %d bytes", bytes_to_drop);
}
return DECODE_STATUS_CONTINUE;
}
bool is_mp3_io(audio_stream_io_handle_t io) {
bool is_mp3_file = false;
audio_stream_io_seek(io, 0, AUDIO_STREAM_SEEK_SET);
// see https://en.wikipedia.org/wiki/List_of_file_signatures
uint8_t magic[3];
if(sizeof(magic) == audio_stream_io_read(io, magic, sizeof(magic))) {
if((magic[0] == 0xFF) &&
(magic[1] == 0xFB))
{
is_mp3_file = true;
} else if((magic[0] == 0xFF) &&
(magic[1] == 0xF3))
{
is_mp3_file = true;
} else if((magic[0] == 0xFF) &&
(magic[1] == 0xF2))
{
is_mp3_file = true;
} else if((magic[0] == 0x49) &&
(magic[1] == 0x44) &&
(magic[2] == 0x33)) /* 'ID3' */
{
audio_stream_io_seek(io, 0, AUDIO_STREAM_SEEK_SET);
/* Get ID3 head */
mp3_id3_header_v2_t tag;
if (sizeof(mp3_id3_header_v2_t) == audio_stream_io_read(io, &tag, sizeof(mp3_id3_header_v2_t))) {
if (memcmp("ID3", (const void *) &tag, sizeof(tag.header)) == 0) {
is_mp3_file = true;
}
}
}
}
// seek back to the start of the file to avoid
// missing frames upon decode
audio_stream_io_seek(io, 0, AUDIO_STREAM_SEEK_SET);
return is_mp3_file;
}
/**
* @return true if data remains, false on error or end of file
*/
DECODE_STATUS decode_mp3_io(HMP3Decoder mp3_decoder, audio_stream_io_handle_t io, decode_data *pData, mp3_instance *pInstance) {
MP3FrameInfo frame_info;
size_t unread_bytes = pInstance->bytes_in_data_buf - (pInstance->read_ptr - pInstance->data_buf);
/* somewhat arbitrary trigger to refill buffer - should always be enough for a full frame */
if (unread_bytes < 1.25 * MAINBUF_SIZE && !pInstance->eof_reached) {
uint8_t *write_ptr = pInstance->data_buf + unread_bytes;
size_t free_space = pInstance->data_buf_size - unread_bytes;
/* move last, small chunk from end of buffer to start,
then fill with new data */
memmove(pInstance->data_buf, pInstance->read_ptr, unread_bytes);
size_t nRead = audio_stream_io_read(io, write_ptr, free_space);
pInstance->bytes_in_data_buf = unread_bytes + nRead;
pInstance->read_ptr = pInstance->data_buf;
if ((nRead == 0) || audio_stream_io_eof(io)) {
pInstance->eof_reached = true;
}
long pos = 0;
audio_stream_io_tell(io, &pos);
LOGI_2("pos %ld, nRead %d, eof %d", pos, (int)nRead, pInstance->eof_reached);
unread_bytes = pInstance->bytes_in_data_buf;
}
LOGI_3("data_buf 0x%p, read 0x%p", pInstance->data_buf, pInstance->read_ptr);
if(unread_bytes == 0) {
LOGI_1("unread_bytes == 0, status done");
return DECODE_STATUS_DONE;
}
/* Find MP3 sync word from read buffer */
int offset = MP3FindSyncWord(pInstance->read_ptr, unread_bytes);
LOGI_2("unread %d, total %d, offset 0x%x(%d)",
(int)unread_bytes, (int)pInstance->bytes_in_data_buf, offset, offset);
// Debug: log what data we're trying to decode
if (offset >= 0 && offset < unread_bytes && unread_bytes >= 4) {
uint8_t *sync_ptr = pInstance->read_ptr + offset;
LOGI_2(TAG, "MP3 sync found at offset %d: %02X %02X %02X %02X, unread=%d",
offset, sync_ptr[0], sync_ptr[1], sync_ptr[2], sync_ptr[3], (int)unread_bytes);
} else if (offset < 0) {
LOGI_2(TAG, "MP3 sync NOT found in %d bytes, first bytes: %02X %02X %02X %02X",
(int)unread_bytes,
pInstance->read_ptr[0], pInstance->read_ptr[1],
pInstance->read_ptr[2], pInstance->read_ptr[3]);
}
if (offset >= 0) {
COMPILE_3(int starting_unread_bytes = unread_bytes);
uint8_t *read_ptr = pInstance->read_ptr + offset; /*!< Data start point */
unread_bytes -= offset;
LOGI_3("read 0x%p, unread %d", read_ptr, (int)unread_bytes);
int mp3_dec_err = MP3Decode(mp3_decoder, &read_ptr, (int*)&unread_bytes, reinterpret_cast<int16_t *>(pData->samples), 0);
pInstance->read_ptr = read_ptr;
if(mp3_dec_err == ERR_MP3_NONE) {
/* Get MP3 frame info */
MP3GetLastFrameInfo(mp3_decoder, &frame_info);
pData->fmt.sample_rate = frame_info.samprate;
pData->fmt.bits_per_sample = frame_info.bitsPerSample;
pData->fmt.channels = frame_info.nChans;
pData->frame_count = (frame_info.outputSamps / frame_info.nChans);
LOGI_2(TAG, "MP3 decode OK: ch=%d, sr=%d, frames=%d",
pData->fmt.channels, pData->fmt.sample_rate, pData->frame_count);
LOGI_3("mp3: channels %d, sr %d, bps %d, frame_count %d, processed %d",
pData->fmt.channels,
pData->fmt.sample_rate,
pData->fmt.bits_per_sample,
frame_info.outputSamps,
starting_unread_bytes - unread_bytes);
} else {
if (pInstance->eof_reached) {
ESP_LOGE(TAG, "status error %d, but EOF", mp3_dec_err);
return DECODE_STATUS_DONE;
} else if (mp3_dec_err == ERR_MP3_MAINDATA_UNDERFLOW) {
// underflow indicates MP3Decode should be called again
LOGI_1("underflow read ptr is 0x%p", read_ptr);
return DECODE_STATUS_NO_DATA_CONTINUE;
} else {
// Invalid frame header - skip ahead to find next sync word
// Don't stay at the same position forever
ESP_LOGW(TAG, "invalid frame header %d, skipping 1 byte to find next sync", mp3_dec_err);
pInstance->read_ptr += 1; // Skip 1 byte and try again
unread_bytes -= 1;
if (unread_bytes <= 0) {
return DECODE_STATUS_NO_DATA_CONTINUE;
}
return DECODE_STATUS_CONTINUE; // Try to find next sync
}
}
} else {
// if we are dropping data there were no frames decoded
pData->frame_count = 0;
// drop an even count of words
size_t words_to_drop = unread_bytes / BYTES_IN_WORD;
size_t bytes_to_drop = words_to_drop * BYTES_IN_WORD;
// if the unread bytes is less than BYTES_IN_WORD, we should drop any unread bytes
// to avoid the situation where the file could have a few extra bytes at the end
// of the file that isn't at least BYTES_IN_WORD and decoding would get stuck
if(unread_bytes < BYTES_IN_WORD) {
bytes_to_drop = unread_bytes;
}
// shift the read_ptr to drop the bytes in the buffer
pInstance->read_ptr += bytes_to_drop;
/* Sync word not found in frame. Drop data that was read until a word boundary */
ESP_LOGE(TAG, "MP3 sync word not found, dropping %d bytes", (int)bytes_to_drop);
}
return DECODE_STATUS_CONTINUE;
}