arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
M4AAudioDemuxer.h
1#pragma once
2
3#include "AudioTools/AudioCodecs/M4ACommonDemuxer.h"
4
5namespace audio_tools {
6
12 public:
17
22 void setCallback(FrameCallback cb) override {
25 }
26
30 bool begin() {
31 audio_config.codec = Codec::Unknown;
32 audio_config.alacMagicCookie.clear();
33 resize(default_size);
34
35 stsz_processed = false;
36 stco_processed = false;
37
38 // When codec/sampleSizes/callback/ref change, update the extractor:
39 parser.begin();
41 return true;
42 }
43
49 void write(const uint8_t* data, size_t len) { parser.write(data, len); }
50
56
62
67 void setReference(void* ref) { this->ref = ref; }
68
69 void copyFrom(M4ACommonDemuxer& source) {
70 audio_config = source.getM4AAudioConfig();
71 }
72
73 protected:
74 void* ref = nullptr;
75
79 void setupParser() override {
80 // global box data callback to get sizes
81 parser.setReference(this);
82
83 // parsing for content of stsd (Sample Description Box)
84 parser.setCallback("stsd", [](MP4Parser::Box& box, void* ref) {
85 static_cast<M4AAudioDemuxer*>(ref)->onStsd(box);
86 });
87
88 // parsing for content of stsd (Sample Description Box)
89 parser.setCallback("esds", [](MP4Parser::Box& box, void* ref) {
90 static_cast<M4AAudioDemuxer*>(ref)->onEsds(box);
91 });
92 parser.setCallback("mp4a", [](MP4Parser::Box& box, void* ref) {
93 static_cast<M4AAudioDemuxer*>(ref)->onMp4a(box);
94 });
95 parser.setCallback("alac", [](MP4Parser::Box& box, void* ref) {
96 static_cast<M4AAudioDemuxer*>(ref)->onAlac(box);
97 });
98
99 // mdat
101 "mdat",
102 [](MP4Parser::Box& box, void* ref) {
103 M4AAudioDemuxer& self = *static_cast<M4AAudioDemuxer*>(ref);
104 // mdat must not be buffered
105 LOGI("#%d Box: %s, size: %u of %u bytes", (unsigned) box.seq, box.type,(unsigned) box.available, (unsigned)box.size);
106 if (box.seq == 0) self.sampleExtractor.setMaxSize(box.size);
107 size_t written = self.sampleExtractor.write(box.data, box.available, box.is_complete);
108 assert(written == box.available);
109 },
110 false); // 'false' prevents the generic callback from being executed
111
112 // stsz
114 "stsz",
115 [](MP4Parser::Box& box, void* ref) {
116 M4AAudioDemuxer& self = *static_cast<M4AAudioDemuxer*>(ref);
117 self.onStsz(box);
118 },
119 false); // 'false' prevents the generic callback from being executed
120
121 // parser.setCallback(
122 // "stco",
123 // [](MP4Parser::Box& box, void* ref) {
124 // M4AAudioDemuxer& self = *static_cast<M4AAudioDemuxer*>(ref);
125 // self.onStco(box);
126 // },
127 // false); // 'false' prevents the generic callback from being executed
128 }
129};
130
131} // namespace audio_tools
A simple M4A audio data demuxer which is providing AAC, MP3 and ALAC frames.
Definition M4AAudioDemuxer.h:11
void * ref
Reference pointer for callbacks.
Definition M4AAudioDemuxer.h:74
M4AAudioDemuxer()
Constructor. Sets up parser callbacks.
Definition M4AAudioDemuxer.h:16
void setupParser() override
Setup all parser callbacks.
Definition M4AAudioDemuxer.h:79
Vector< uint8_t > & getALACMagicCookie()
Returns the ALAC magic cookie (codec config).
Definition M4AAudioDemuxer.h:61
bool begin()
Initializes the demuxer and resets state.
Definition M4AAudioDemuxer.h:30
int availableForWrite()
Returns the available space for writing.
Definition M4AAudioDemuxer.h:55
void setReference(void *ref)
Sets a reference pointer for callbacks.
Definition M4AAudioDemuxer.h:67
void write(const uint8_t *data, size_t len)
Writes data to the demuxer for parsing.
Definition M4AAudioDemuxer.h:49
void setCallback(FrameCallback cb) override
Defines the callback that returns the audio frames.
Definition M4AAudioDemuxer.h:22
size_t write(const uint8_t *data, size_t len, bool is_final)
Writes data to the extractor, extracting frames as sample sizes are met. Provides the data via the ca...
Definition M4ACommonDemuxer.h:163
void begin()
Resets the extractor state.
Definition M4ACommonDemuxer.h:122
void setCallback(FrameCallback cb)
Sets the callback to be called for each extracted frame.
Definition M4ACommonDemuxer.h:137
void setReference(void *r)
Sets a reference pointer passed to the callback.
Definition M4ACommonDemuxer.h:143
void setMaxSize(size_t size)
Sets the maximum box size (e.g., for mdat). This is called before the mdat data is posted....
Definition M4ACommonDemuxer.h:151
Abstract base class for M4A/MP4 demuxers. Provides shared functionality for both file-based and strea...
Definition M4ACommonDemuxer.h:23
MP4Parser parser
Underlying MP4 parser.
Definition M4ACommonDemuxer.h:437
void onEsds(const MP4Parser::Box &box)
Handles the esds (Elementary Stream Descriptor) box.
Definition M4ACommonDemuxer.h:530
SampleExtractor sampleExtractor
Extractor for audio samples.
Definition M4ACommonDemuxer.h:435
void onAlac(const MP4Parser::Box &box)
Handles the alac box.
Definition M4ACommonDemuxer.h:585
void onStsz(MP4Parser::Box &box)
Handles the stsz (Sample Size) box.
Definition M4ACommonDemuxer.h:603
void onMp4a(const MP4Parser::Box &box)
Handles the mp4a box.
Definition M4ACommonDemuxer.h:507
size_t default_size
Default buffer size.
Definition M4ACommonDemuxer.h:446
bool stsz_processed
Marks the stsz table as processed.
Definition M4ACommonDemuxer.h:438
bool stco_processed
Marks the stco table as processed.
Definition M4ACommonDemuxer.h:439
bool begin()
Initializes the parser.
Definition MP4Parser.h:107
void setCallback(BoxCallback cb)
Defines the generic callback for all boxes.
Definition MP4Parser.h:75
int availableForWrite()
Returns the available space for writing.
Definition MP4Parser.h:151
void setReference(void *ref)
Defines an optional reference. By default it is the parser itself.
Definition MP4Parser.h:69
size_t write(const uint8_t *data, size_t len)
Provide the data to the parser (in chunks if needed).
Definition MP4Parser.h:130
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Vector< uint8_t > alacMagicCookie
ALAC codec config.
Definition M4ACommonDemuxer.h:39
Codec codec
Current codec.
Definition M4ACommonDemuxer.h:35
Represents an individual box in the MP4 file.
Definition MP4Parser.h:33
const uint8_t * data
Pointer to box payload (not including header)
Definition MP4Parser.h:40
int available
Number of bytes available as data.
Definition MP4Parser.h:47
size_t size
Size of payload including subboxes (not including header)
Definition MP4Parser.h:43
bool is_complete
True if the box data is complete.
Definition MP4Parser.h:48
size_t seq
Sequence number for the box per id.
Definition MP4Parser.h:38
char type[5]
4-character box type (null-terminated)
Definition MP4Parser.h:39