@@ -16,19 +16,21 @@ class M4AAudioDemuxer {
16
16
enum class Codec { AAC, ALAC, MP3, Unknown };
17
17
18
18
/* *
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.
20
21
*/
21
22
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).
27
28
};
28
29
29
30
/* *
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.
32
34
*/
33
35
class SampleExtractor {
34
36
public:
@@ -79,7 +81,8 @@ class M4AAudioDemuxer {
79
81
void setMaxSize (size_t size) { box_size = size; }
80
82
81
83
/* *
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.
83
86
* @param data Pointer to input data.
84
87
* @param len Length of input data.
85
88
* @param is_final True if this is the last chunk of the box.
@@ -132,7 +135,8 @@ class M4AAudioDemuxer {
132
135
Vector<uint32_t >& getChunkOffsets () { return chunkOffsets; }
133
136
134
137
/* *
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.
136
140
* @param sampleSize Size of each sample.
137
141
* @param sampleCount Number of samples.
138
142
*/
@@ -154,19 +158,19 @@ class M4AAudioDemuxer {
154
158
}
155
159
156
160
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.
170
174
171
175
/* *
172
176
* @brief Executes the callback for a completed frame.
@@ -225,12 +229,12 @@ class M4AAudioDemuxer {
225
229
* @return Size of the current sample.
226
230
*/
227
231
size_t currentSampleSize () {
228
- // using fixed sizes w/o table
232
+ // using fixed sizes w/o table
229
233
if (fixed_sample_size > 0 && fixed_sample_count > 0 &&
230
234
sampleIndex < fixed_sample_count) {
231
235
return fixed_sample_size;
232
236
}
233
- if (sampleSizes && sampleIndex < sampleSizes.size ()) {
237
+ if (sampleSizes && sampleIndex < sampleSizes.size ()) {
234
238
return sampleSizes[sampleIndex];
235
239
}
236
240
return 0 ;
@@ -272,9 +276,15 @@ class M4AAudioDemuxer {
272
276
parser.setIncrementalDataCallback (incrementalBoxDataCallback);
273
277
274
278
// 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
+ });
278
288
}
279
289
280
290
/* *
@@ -336,55 +346,34 @@ class M4AAudioDemuxer {
336
346
}
337
347
338
348
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
+ }
346
365
347
366
/* *
348
367
* @brief Checks if a box type is relevant for audio demuxing.
349
368
* @param type Box type string.
350
369
* @return True if relevant, false otherwise.
351
370
*/
352
- bool isRelevantBox (const char * type) {
371
+ static bool isRelevantBox (const char * type) {
353
372
// Check if the box is relevant for audio demuxing
354
373
return (StrView (type) == " stsd" || StrView (type) == " stsz" ||
355
374
StrView (type) == " stco" );
356
375
}
357
376
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
-
388
377
/* *
389
378
* @brief Callback for box data setup.
390
379
* @param box MP4 box.
@@ -400,7 +389,7 @@ class M4AAudioDemuxer {
400
389
return ;
401
390
}
402
391
403
- bool is_relevant = self. isRelevantBox (box.type );
392
+ bool is_relevant = isRelevantBox (box.type );
404
393
if (is_relevant) {
405
394
LOGI (" Box: %s, size: %u bytes" , box.type , (unsigned )box.size );
406
395
if (box.data_size == 0 ) {
@@ -422,8 +411,9 @@ class M4AAudioDemuxer {
422
411
* @param is_final True if this is the last chunk.
423
412
* @param ref Reference pointer.
424
413
*/
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) {
427
417
M4AAudioDemuxer& self = *static_cast <M4AAudioDemuxer*>(ref);
428
418
429
419
// mdat must not be buffered
@@ -434,7 +424,7 @@ class M4AAudioDemuxer {
434
424
}
435
425
436
426
// only process relevant boxes
437
- if (!self. isRelevantBox (box.type )) return ;
427
+ if (!isRelevantBox (box.type )) return ;
438
428
439
429
LOGI (" *Box: %s, size: %u bytes" , box.type , (unsigned )len);
440
430
@@ -472,14 +462,6 @@ class M4AAudioDemuxer {
472
462
}
473
463
}
474
464
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
- }
483
465
484
466
/* *
485
467
* @brief Handles the stsd (Sample Description) box.
0 commit comments