arduino-audio-tools
Loading...
Searching...
No Matches
M4AAudioDemuxer.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace audio_tools {
6
12 public:
17
26
30 bool begin() {
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) {
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 parser.setCallback("ac-3", [](MP4Parser::Box& box, void* ref) {
99 static_cast<M4AAudioDemuxer*>(ref)->onAc3(box);
100 });
101
102 // mdat
104 "mdat",
105 [](MP4Parser::Box& box, void* ref) {
106 M4AAudioDemuxer& self = *static_cast<M4AAudioDemuxer*>(ref);
107 // mdat must not be buffered
108 LOGI("#%d Box: %s, size: %u of %u bytes", (unsigned) box.seq, box.type,(unsigned) box.available, (unsigned)box.size);
109 if (box.seq == 0) self.sampleExtractor.setMaxSize(box.size);
110 // write() legitimately returns less than box.available when the
111 // last sample completes exactly at the end of the mdat box - the
112 // remaining bytes (if any) belong past the box and are not lost
113 self.sampleExtractor.write(box.data, box.available, box.is_complete);
114 },
115 false); // 'false' prevents the generic callback from being executed
116
117 // stsz
119 "stsz",
120 [](MP4Parser::Box& box, void* ref) {
121 M4AAudioDemuxer& self = *static_cast<M4AAudioDemuxer*>(ref);
122 self.onStsz(box);
123 },
124 false); // 'false' prevents the generic callback from being executed
125
126 // parser.setCallback(
127 // "stco",
128 // [](MP4Parser::Box& box, void* ref) {
129 // M4AAudioDemuxer& self = *static_cast<M4AAudioDemuxer*>(ref);
130 // self.onStco(box);
131 // },
132 // false); // 'false' prevents the generic callback from being executed
133 }
134};
135
136} // namespace audio_tools
#define LOGI(...)
Definition AudioLoggerIDF.h:28
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
void copyFrom(M4ACommonDemuxer &source)
Definition M4AAudioDemuxer.h:69
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:165
void begin()
Resets the extractor state.
Definition M4ACommonDemuxer.h:124
void setCallback(FrameCallback cb)
Sets the callback to be called for each extracted frame.
Definition M4ACommonDemuxer.h:139
void setReference(void *r)
Sets a reference pointer passed to the callback.
Definition M4ACommonDemuxer.h:145
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:153
Abstract base class for M4A/MP4 demuxers. Provides shared functionality for both file-based and strea...
Definition M4ACommonDemuxer.h:25
MP4Parser parser
Underlying MP4 parser.
Definition M4ACommonDemuxer.h:451
void onEsds(const MP4Parser::Box &box)
Handles the esds (Elementary Stream Descriptor) box.
Definition M4ACommonDemuxer.h:544
void onAc3(const MP4Parser::Box &box)
Handles the ac-3 box (AC-3/Dolby Digital audio sample entry). Unlike AAC/ALAC, AC-3 frames are self-s...
Definition M4ACommonDemuxer.h:621
void onStsd(const MP4Parser::Box &box)
Definition M4ACommonDemuxer.h:498
SampleExtractor sampleExtractor
Extractor for audio samples.
Definition M4ACommonDemuxer.h:449
void onAlac(const MP4Parser::Box &box)
Handles the alac box.
Definition M4ACommonDemuxer.h:599
void onStsz(MP4Parser::Box &box)
Handles the stsz (Sample Size) box.
Definition M4ACommonDemuxer.h:630
void onMp4a(const MP4Parser::Box &box)
Handles the mp4a box.
Definition M4ACommonDemuxer.h:521
M4AAudioConfig audio_config
Definition M4ACommonDemuxer.h:455
size_t default_size
Default buffer size.
Definition M4ACommonDemuxer.h:460
bool stsz_processed
Marks the stsz table as processed.
Definition M4ACommonDemuxer.h:452
bool resize(size_t size)
Definition M4ACommonDemuxer.h:423
std::function< void(const Frame &, void *ref)> FrameCallback
Definition M4ACommonDemuxer.h:372
M4AAudioConfig getM4AAudioConfig()
Definition M4ACommonDemuxer.h:421
bool stco_processed
Marks the stco table as processed.
Definition M4ACommonDemuxer.h:453
void setCallback(BoxCallback cb)
Defines the generic callback for all boxes.
Definition MP4Parser.h:92
int availableForWrite()
Returns the available space for writing.
Definition MP4Parser.h:200
bool begin(uint64_t startFileOffset=0)
Initializes the parser.
Definition MP4Parser.h:129
void setReference(void *ref)
Defines an optional reference. By default it is the parser itself.
Definition MP4Parser.h:86
size_t write(const uint8_t *data, size_t len)
Provide the data to the parser (in chunks if needed).
Definition MP4Parser.h:160
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
void clear()
Definition Vector.h:176
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Vector< uint8_t > alacMagicCookie
ALAC codec config.
Definition M4ACommonDemuxer.h:41
Codec codec
Current codec.
Definition M4ACommonDemuxer.h:37
Represents an individual box in the MP4 file.
Definition MP4Parser.h:50
const uint8_t * data
Pointer to box payload (not including header)
Definition MP4Parser.h:57
int available
Number of bytes available as data.
Definition MP4Parser.h:64
size_t size
Size of payload including subboxes (not including header)
Definition MP4Parser.h:60
bool is_complete
True if the box data is complete.
Definition MP4Parser.h:65
size_t seq
Sequence number for the box per id.
Definition MP4Parser.h:55
char type[5]
4-character box type (null-terminated)
Definition MP4Parser.h:56