|
arduino-audio-tools
|
MP4Parser is a class that parses MP4 container files and extracts boxes (atoms). It provides a callback mechanism to process each box as it is parsed. You can define specific callbacks for individual box types or use a generic callback for the undefined boxes: By default it just prints the box information to Serial. If a container box contains data, it will be processed recursively and if it contains data itself, it might be reported in a second callback call. More...
#include <MP4Parser.h>
Classes | |
| struct | Box |
| Represents an individual box in the MP4 file. More... | |
| struct | CallbackEntry |
| Structure for type-specific callbacks. More... | |
| struct | ContainerInfo |
| Structure for container box information. More... | |
Public Types | |
| using | BoxCallback = std::function< void(Box &, void *ref)> |
Public Member Functions | |
| void | addContainer (const char *name, int start=0) |
| Adds a box name that will be interpreted as a container. | |
| int | availableForWrite () |
| Returns the available space for writing. | |
| bool | begin () |
| Initializes the parser. | |
| bool | findBox (const char *name, const uint8_t *data, size_t len, Box &result) |
| find box in box | |
| int | parseString (const uint8_t *str, int len, int fileOffset=0, int level=0) |
| Trigger separate parsing (and callbacks) on the indicated string. | |
| bool | resize (size_t size) |
| Defines a specific buffer size. | |
| void | setCallback (BoxCallback cb) |
| Defines the generic callback for all boxes. | |
| void | setCallback (const char *type, BoxCallback cb, bool callGeneric=true) |
| Defines a specific callback for a box type. | |
| void | setReference (void *ref) |
| Defines an optional reference. By default it is the parser itself. | |
| size_t | write (const char *data, size_t len) |
| Provide the data to the parser (in chunks if needed). | |
| size_t | write (const uint8_t *data, size_t len) |
| Provide the data to the parser (in chunks if needed). | |
Static Public Member Functions | |
| static void | defaultCallback (const Box &box, void *ref) |
| Default callback that prints box information to Serial. | |
Protected Member Functions | |
| size_t | checkParseOffset () |
| Checks and adjusts the parse offset for valid box types. | |
| bool | continueIncrementalBox () |
| Continue filling an incremental box. Returns false if not enough data. | |
| uint64_t | currentFileOffset () |
| Returns the current file offset (absolute position in file). | |
| void | finalizeParse () |
| Finalizes parsing, updating file offset and clearing buffer. | |
| int | getContainerDataLength (const char *type) |
| Gets the start offset for a subcontainer. | |
| void | handleCompleteBox (const char *type, const uint8_t *p, size_t headerSize, size_t payload_size, int level) |
| Handles a complete (non-incremental) box. | |
| void | handleContainerBox (const char *type, uint64_t boxSize, int level) |
| Handles a container box (box with children). | |
| bool | isContainerBox (const char *type) |
| Checks if a box type is a container box. | |
| bool | isValidType (const char *type, int offset=0) const |
| Checks if a type string is a valid 4-character box type. | |
| void | parse () |
| Main parsing loop. Handles incremental and complete boxes. | |
| void | popLevels () |
| Pops levels from the stack if we've passed their bounds. | |
| void | processCallback (Box &box) |
| Processes the callback for a box. Calls the type-specific callback if present, and the generic callback if allowed. | |
| void | startIncrementalBox (const char *type, const uint8_t *p, size_t headerSize, size_t payload_size, int level, size_t bufferSize) |
| Starts parsing a box incrementally. | |
| bool | tryStartNewBox (size_t bufferSize) |
| Try to start parsing a new box. Returns false if not enough data. | |
Static Protected Member Functions | |
| static uint32_t | readU32 (const uint8_t *p) |
| Reads a 32-bit big-endian unsigned integer from a buffer. | |
| static uint64_t | readU64 (const uint8_t *p) |
| Reads a 64-bit big-endian unsigned integer from a buffer. | |
Protected Attributes | |
| Box | box |
| Current box being processed. | |
| size_t | box_bytes_expected = 0 |
| Total expected bytes for the current box. | |
| size_t | box_bytes_received = 0 |
| Bytes received so far for the current box. | |
| bool | box_in_progress |
| True if currently parsing a box incrementally. | |
| int | box_level = 0 |
| Current box level (nesting) | |
| int | box_seq = 0 |
| char | box_type [5] = {0} |
| Current box type. | |
| SingleBuffer< uint8_t > | buffer |
| Buffer for incoming data. | |
| BoxCallback | callback = defaultCallback |
| Generic callback for all boxes. | |
| Vector< CallbackEntry > | callbacks |
| List of type-specific callbacks. | |
| Vector< ContainerInfo > | containers |
| List of container box info. | |
| uint64_t | fileOffset = 0 |
| Current file offset. | |
| size_t | incremental_offset = 0 |
| bool | is_error = false |
| True if an error occurred. | |
| Vector< size_t > | levelStack |
| Stack for container box levels. | |
| size_t | parseOffset = 0 |
| Current parse offset in buffer. | |
| void * | ref = this |
| Reference pointer for callbacks. | |
MP4Parser is a class that parses MP4 container files and extracts boxes (atoms). It provides a callback mechanism to process each box as it is parsed. You can define specific callbacks for individual box types or use a generic callback for the undefined boxes: By default it just prints the box information to Serial. If a container box contains data, it will be processed recursively and if it contains data itself, it might be reported in a second callback call.
Adds a box name that will be interpreted as a container.
| name | Name of the container box. |
| start | Offset of child boxes (default 0). |
|
inline |
Returns the available space for writing.
|
inline |
Initializes the parser.
|
inlineprotected |
Checks and adjusts the parse offset for valid box types.
|
inlineprotected |
Continue filling an incremental box. Returns false if not enough data.
|
inlineprotected |
Returns the current file offset (absolute position in file).
Default callback that prints box information to Serial.
| box | The box being processed. |
| ref | Optional reference pointer. |
|
inlineprotected |
Finalizes parsing, updating file offset and clearing buffer.
find box in box
Gets the start offset for a subcontainer.
| type | Box type string. |
|
inlineprotected |
Handles a complete (non-incremental) box.
| type | Box type string. |
| p | Pointer to the start of the box in the buffer. |
| headerSize | Size of the box header. |
| payload_size | Size of the box payload. |
| level | Nesting level of the box. |
Handles a container box (box with children).
| type | Box type string. |
| boxSize | Size of the box. |
| level | Nesting level of the box. |
Checks if a box type is a container box.
| type | Box type string. |
Checks if a type string is a valid 4-character box type.
| type | Pointer to type string. |
| offset | Offset in the string. |
|
inlineprotected |
Main parsing loop. Handles incremental and complete boxes.
Trigger separate parsing (and callbacks) on the indicated string.
| str | Pointer to the string data. |
| len | Length of the string data. |
|
inlineprotected |
Pops levels from the stack if we've passed their bounds.
Processes the callback for a box. Calls the type-specific callback if present, and the generic callback if allowed.
| box | The box being processed. |
call generic callback if allowed
Reads a 32-bit big-endian unsigned integer from a buffer.
| p | Pointer to buffer. |
Reads a 64-bit big-endian unsigned integer from a buffer.
| p | Pointer to buffer. |
Defines a specific buffer size.
| size | Buffer size in bytes. |
|
inline |
Defines the generic callback for all boxes.
| cb | Callback function for all boxes. |
Defines a specific callback for a box type.
| type | 4-character box type (e.g. "moov", "mdat"). |
| cb | Callback function for this box type. |
| callGeneric | If true, the generic callback will also be called after the type-specific callback. |
Defines an optional reference. By default it is the parser itself.
| ref | Pointer to reference object. |
|
inlineprotected |
Starts parsing a box incrementally.
| type | Box type string. |
| p | Pointer to the start of the box in the buffer. |
| headerSize | Size of the box header. |
| payload_size | Size of the box payload. |
| level | Nesting level of the box. |
| bufferSize | Number of bytes available in the buffer. |
Try to start parsing a new box. Returns false if not enough data.
| bufferSize | Number of bytes available in the buffer. |
Provide the data to the parser (in chunks if needed).
| data | Pointer to input data (char*). |
| len | Length of input data. |
Provide the data to the parser (in chunks if needed).
| data | Pointer to input data. |
| len | Length of input data. |
|
protected |
Current box being processed.
|
protected |
Total expected bytes for the current box.
|
protected |
Bytes received so far for the current box.
|
protected |
True if currently parsing a box incrementally.
|
protected |
Current box level (nesting)
|
protected |
|
protected |
Current box type.
|
protected |
Buffer for incoming data.
|
protected |
Generic callback for all boxes.
|
protected |
List of type-specific callbacks.
|
protected |
List of container box info.
|
protected |
Current file offset.
|
protected |
|
protected |
Current parse offset in buffer.
|
protected |
Reference pointer for callbacks.