Skip to content

Commit 930bcee

Browse files
committed
cleanup
1 parent 0d62426 commit 930bcee

File tree

1 file changed

+60
-78
lines changed

1 file changed

+60
-78
lines changed

src/AudioTools/AudioCodecs/M4AAudioDemuxer.h

Lines changed: 60 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ class M4AAudioDemuxer {
1616
enum class Codec { AAC, ALAC, MP3, Unknown };
1717

1818
/**
19-
* @brief Represents a frame of audio data with codec, mime type, data and size.
19+
* @brief Represents a frame of audio data with codec, mime type, data and
20+
* size.
2021
*/
2122
struct Frame {
22-
Codec codec; ///< Codec type.
23-
const char* mime = nullptr; ///< MIME type string.
24-
const uint8_t* data; ///< Pointer to frame data.
25-
size_t size; ///< Size of frame data in bytes.
26-
uint64_t timestamp; ///< Timestamp of the frame (if available).
23+
Codec codec; ///< Codec type.
24+
const char* mime = nullptr; ///< MIME type string.
25+
const uint8_t* data; ///< Pointer to frame data.
26+
size_t size; ///< Size of frame data in bytes.
27+
uint64_t timestamp; ///< Timestamp of the frame (if available).
2728
};
2829

2930
/**
30-
* @brief Extracts audio data based on the sample sizes defined in the stsz box.
31-
* It collects the data from the mdat box and calls the callback with the extracted frames.
31+
* @brief Extracts audio data based on the sample sizes defined in the stsz
32+
* box. It collects the data from the mdat box and calls the callback with the
33+
* extracted frames.
3234
*/
3335
class SampleExtractor {
3436
public:
@@ -79,7 +81,8 @@ class M4AAudioDemuxer {
7981
void setMaxSize(size_t size) { box_size = size; }
8082

8183
/**
82-
* @brief Writes data to the extractor, extracting frames as sample sizes are met.
84+
* @brief Writes data to the extractor, extracting frames as sample sizes
85+
* are met.
8386
* @param data Pointer to input data.
8487
* @param len Length of input data.
8588
* @param is_final True if this is the last chunk of the box.
@@ -132,7 +135,8 @@ class M4AAudioDemuxer {
132135
Vector<uint32_t>& getChunkOffsets() { return chunkOffsets; }
133136

134137
/**
135-
* @brief Sets a fixed sample size/count instead of using the sampleSizes table.
138+
* @brief Sets a fixed sample size/count instead of using the sampleSizes
139+
* table.
136140
* @param sampleSize Size of each sample.
137141
* @param sampleCount Number of samples.
138142
*/
@@ -154,19 +158,19 @@ class M4AAudioDemuxer {
154158
}
155159

156160
protected:
157-
Vector<uint32_t> sampleSizes; ///< Table of sample sizes.
158-
Vector<uint32_t> chunkOffsets;///< Table of chunk offsets.
159-
Codec codec = Codec::Unknown; ///< Current codec.
160-
FrameCallback callback = nullptr; ///< Frame callback.
161-
void* ref = nullptr; ///< Reference pointer for callback.
162-
size_t sampleIndex = 0; ///< Current sample index.
163-
SingleBuffer<uint8_t> buffer; ///< Buffer for accumulating sample data.
164-
int aacProfile = 2, sampleRateIdx = 4, channelCfg = 2; ///< AAC config.
165-
uint32_t fixed_sample_size = 0; ///< Fixed sample size (if used).
166-
uint32_t fixed_sample_count = 0;///< Fixed sample count (if used).
167-
size_t current_size = 0; ///< Current sample size.
168-
size_t box_size = 0; ///< Maximum size of the current sample.
169-
size_t box_pos = 0; ///< Current position in the box.
161+
Vector<uint32_t> sampleSizes; ///< Table of sample sizes.
162+
Vector<uint32_t> chunkOffsets; ///< Table of chunk offsets.
163+
Codec codec = Codec::Unknown; ///< Current codec.
164+
FrameCallback callback = nullptr; ///< Frame callback.
165+
void* ref = nullptr; ///< Reference pointer for callback.
166+
size_t sampleIndex = 0; ///< Current sample index.
167+
SingleBuffer<uint8_t> buffer; ///< Buffer for accumulating sample data.
168+
int aacProfile = 2, sampleRateIdx = 4, channelCfg = 2; ///< AAC config.
169+
uint32_t fixed_sample_size = 0; ///< Fixed sample size (if used).
170+
uint32_t fixed_sample_count = 0; ///< Fixed sample count (if used).
171+
size_t current_size = 0; ///< Current sample size.
172+
size_t box_size = 0; ///< Maximum size of the current sample.
173+
size_t box_pos = 0; ///< Current position in the box.
170174

171175
/**
172176
* @brief Executes the callback for a completed frame.
@@ -225,12 +229,12 @@ class M4AAudioDemuxer {
225229
* @return Size of the current sample.
226230
*/
227231
size_t currentSampleSize() {
228-
// using fixed sizes w/o table
232+
// using fixed sizes w/o table
229233
if (fixed_sample_size > 0 && fixed_sample_count > 0 &&
230234
sampleIndex < fixed_sample_count) {
231235
return fixed_sample_size;
232236
}
233-
if (sampleSizes && sampleIndex < sampleSizes.size()) {
237+
if (sampleSizes && sampleIndex < sampleSizes.size()) {
234238
return sampleSizes[sampleIndex];
235239
}
236240
return 0;
@@ -272,9 +276,15 @@ class M4AAudioDemuxer {
272276
parser.setIncrementalDataCallback(incrementalBoxDataCallback);
273277

274278
// parsing for content of stsd (Sample Description Box)
275-
parser.setCallback("esds", esdsCallback);
276-
parser.setCallback("mp4a", mp4aCallback);
277-
parser.setCallback("alac", alacCallback);
279+
parser.setCallback("esds", [](MP4Parser::Box& box, void* ref) {
280+
static_cast<M4AAudioDemuxer*>(ref)->onEsds(box);
281+
});
282+
parser.setCallback("mp4a", [](MP4Parser::Box& box, void* ref) {
283+
static_cast<M4AAudioDemuxer*>(ref)->onMp4a(box);
284+
});
285+
parser.setCallback("alac", [](MP4Parser::Box& box, void* ref) {
286+
static_cast<M4AAudioDemuxer*>(ref)->onAlac(box);
287+
});
278288
}
279289

280290
/**
@@ -336,55 +346,34 @@ class M4AAudioDemuxer {
336346
}
337347

338348
protected:
339-
MP4ParserIncremental parser; ///< Underlying MP4 parser.
340-
Codec codec = Codec::Unknown;///< Current codec.
341-
Vector<uint8_t> alacMagicCookie; ///< ALAC codec config.
342-
SingleBuffer<uint8_t> buffer; ///< Buffer for incremental data.
343-
SampleExtractor sampleExtractor; ///< Extractor for audio samples.
344-
void* ref = nullptr; ///< Reference pointer for callbacks.
345-
size_t default_size = 2 * 1024; ///< Default buffer size.
349+
MP4ParserIncremental parser; ///< Underlying MP4 parser.
350+
Codec codec = Codec::Unknown; ///< Current codec.
351+
Vector<uint8_t> alacMagicCookie; ///< ALAC codec config.
352+
SingleBuffer<uint8_t> buffer; ///< Buffer for incremental data.
353+
SampleExtractor sampleExtractor; ///< Extractor for audio samples.
354+
void* ref = nullptr; ///< Reference pointer for callbacks.
355+
size_t default_size = 2 * 1024; ///< Default buffer size.
356+
357+
/**
358+
* @brief Reads a 32-bit big-endian unsigned integer from a buffer.
359+
* @param p Pointer to buffer.
360+
* @return 32-bit unsigned integer.
361+
*/
362+
static uint32_t readU32(const uint8_t* p) {
363+
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
364+
}
346365

347366
/**
348367
* @brief Checks if a box type is relevant for audio demuxing.
349368
* @param type Box type string.
350369
* @return True if relevant, false otherwise.
351370
*/
352-
bool isRelevantBox(const char* type) {
371+
static bool isRelevantBox(const char* type) {
353372
// Check if the box is relevant for audio demuxing
354373
return (StrView(type) == "stsd" || StrView(type) == "stsz" ||
355374
StrView(type) == "stco");
356375
}
357376

358-
/**
359-
* @brief Callback for mp4a box.
360-
* @param box MP4 box.
361-
* @param ref Reference pointer.
362-
*/
363-
static void mp4aCallback(MP4Parser::Box& box, void* ref) {
364-
M4AAudioDemuxer& self = *static_cast<M4AAudioDemuxer*>(ref);
365-
self.onMp4a(box);
366-
}
367-
368-
/**
369-
* @brief Callback for esds box.
370-
* @param box MP4 box.
371-
* @param ref Reference pointer.
372-
*/
373-
static void esdsCallback(MP4Parser::Box& box, void* ref) {
374-
M4AAudioDemuxer& self = *static_cast<M4AAudioDemuxer*>(ref);
375-
self.onEsds(box);
376-
}
377-
378-
/**
379-
* @brief Callback for alac box.
380-
* @param box MP4 box.
381-
* @param ref Reference pointer.
382-
*/
383-
static void alacCallback(MP4Parser::Box& box, void* ref) {
384-
M4AAudioDemuxer& self = *static_cast<M4AAudioDemuxer*>(ref);
385-
self.onAlac(box);
386-
}
387-
388377
/**
389378
* @brief Callback for box data setup.
390379
* @param box MP4 box.
@@ -400,7 +389,7 @@ class M4AAudioDemuxer {
400389
return;
401390
}
402391

403-
bool is_relevant = self.isRelevantBox(box.type);
392+
bool is_relevant = isRelevantBox(box.type);
404393
if (is_relevant) {
405394
LOGI("Box: %s, size: %u bytes", box.type, (unsigned)box.size);
406395
if (box.data_size == 0) {
@@ -422,8 +411,9 @@ class M4AAudioDemuxer {
422411
* @param is_final True if this is the last chunk.
423412
* @param ref Reference pointer.
424413
*/
425-
static void incrementalBoxDataCallback(MP4Parser::Box& box, const uint8_t* data,
426-
size_t len, bool is_final, void* ref) {
414+
static void incrementalBoxDataCallback(MP4Parser::Box& box,
415+
const uint8_t* data, size_t len,
416+
bool is_final, void* ref) {
427417
M4AAudioDemuxer& self = *static_cast<M4AAudioDemuxer*>(ref);
428418

429419
// mdat must not be buffered
@@ -434,7 +424,7 @@ class M4AAudioDemuxer {
434424
}
435425

436426
// only process relevant boxes
437-
if (!self.isRelevantBox(box.type)) return;
427+
if (!isRelevantBox(box.type)) return;
438428

439429
LOGI("*Box: %s, size: %u bytes", box.type, (unsigned)len);
440430

@@ -472,14 +462,6 @@ class M4AAudioDemuxer {
472462
}
473463
}
474464

475-
/**
476-
* @brief Reads a 32-bit big-endian unsigned integer from a buffer.
477-
* @param p Pointer to buffer.
478-
* @return 32-bit unsigned integer.
479-
*/
480-
static uint32_t readU32(const uint8_t* p) {
481-
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
482-
}
483465

484466
/**
485467
* @brief Handles the stsd (Sample Description) box.

0 commit comments

Comments
 (0)