103 strncpy(entry.
type, type, 4);
104 entry.
type[4] =
'\0';
129 bool begin(uint64_t startFileOffset = 0) {
160 size_t write(
const uint8_t* data,
size_t len) {
169 size_t total_written = 0;
170 while (total_written < len) {
175 if (avail == 0)
break;
177 size_t chunk = std::min(avail, len - total_written);
179 if (written == 0)
break;
180 total_written += written;
183 return total_written;
192 size_t write(
const char* data,
size_t len) {
193 return write(
reinterpret_cast<const uint8_t*
>(data), len);
237 size_t box_size =
readU32(str + idx) - 8;
245 strncpy(
box.
type, (
char*)(str + idx + 4), 4);
249 if (idx >= len)
break;
255 bool findBox(
const char* name,
const uint8_t* data,
size_t len,
Box& result) {
256 for (
int j = 0; j < len - 4; j++) {
260 size_t box_size =
readU32(data + j) - 8;
261 if (box_size < 8)
continue;
266 strncpy(
box.
type, (
char*)(data + j + 4), 4);
283 char str_buffer[200];
286 snprintf(str_buffer,
sizeof(str_buffer),
287 "%s- #%u %u) %s, Offset: %u, Size: %u, Data Size: %u, Available: %u", space,
291 Serial.println(str_buffer);
293 printf(
"%s\n", str_buffer);
358 strncpy(type, (
char*)(p + 4), 4);
361 uint64_t boxSize = size32;
362 size_t headerSize = 8;
365 bool unbounded =
false;
371 }
else if (size32 == 0) {
377 if (!unbounded && boxSize < headerSize)
return false;
383 if (level == 0 && strcmp(type,
"mdat") == 0 && !
moov_found) {
386 "mdat box found before moov box: moov must precede mdat (e.g. "
387 "use 'ffmpeg -movflags +faststart') or this file will not play "
391 "mdat box found before moov box: moov should precede mdat (e.g. "
392 "use 'ffmpeg -movflags +faststart') - continuing anyway since "
393 "the requirement was opted out of");
401 LOGE(
"Unsupported: container box '%s' with size 0 (extends to EOF)",
409 size_t payload_size =
410 unbounded ? SIZE_MAX :
static_cast<size_t>(boxSize - headerSize);
411 if (!unbounded &&
parseOffset + boxSize <= bufferSize) {
429 size_t headerSize,
int level) {
433 box.
size =
static_cast<size_t>(boxSize - headerSize);
443 if (strcmp(type,
"moov") == 0)
moov_found =
true;
461 size_t payload_size,
int level) {
488 size_t headerSize,
size_t payload_size,
int level,
497 size_t available_payload = bufferSize -
parseOffset - headerSize;
499 if (available_payload > 0) {
516 if (payload_size == SIZE_MAX) {
536 if (to_read == 0)
return true;
584 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
615 bool is_called =
false;
616 bool call_generic =
true;
618 if (strncmp(entry.type,
box.
type, 4) == 0) {
621 if (!entry.callGeneric) call_generic =
false;
637 static const char* containers_str[] = {
638 "moov",
"trak",
"mdia",
"minf",
"stbl",
"edts",
"dinf",
"udta",
639 "ilst",
"moof",
"traf",
"mfra",
"tref",
"iprp",
"sinf",
"schi"};
640 for (
const char* c : containers_str) {
654 if (
StrView(type) == cont.name)
return true;
666 if (
StrView(type) == cont.name)
return cont.start;
679 return (type !=
nullptr && isalnum(type[offset]) &&
680 isalnum(type[offset + 1]) && isalnum(type[offset + 2]) &&
681 isalnum(type[offset + 3]));
static HardwareSerial Serial
Definition Arduino.h:179
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Structure for type-specific callbacks.
Definition MP4Parser.h:75
BoxCallback cb
Callback function.
Definition MP4Parser.h:77
char type[5]
4-character box type
Definition MP4Parser.h:76
bool callGeneric
If true, also call the generic callback after this one.
Definition MP4Parser.h:78