arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
MP4Parser Class Reference

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< CallbackEntrycallbacks
 List of type-specific callbacks.
 
Vector< ContainerInfocontainers
 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.
 

Detailed Description

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.

Note
This parser expect the mdat box to be the last box in the file. This can be achieve with the following ffmpeg commands:
  • ffmpeg -i ../sine.wav -c:a alac -movflags +faststart alac.m4a
  • ffmpeg -i ../sine.wav -c:a aac -movflags +faststart aac.m4a
Author
Phil Schatzmann

Member Function Documentation

◆ addContainer()

void addContainer ( const char *  name,
int  start = 0 
)
inline

Adds a box name that will be interpreted as a container.

Parameters
nameName of the container box.
startOffset of child boxes (default 0).

◆ availableForWrite()

int availableForWrite ( )
inline

Returns the available space for writing.

Returns
Number of bytes available for writing.

◆ begin()

bool begin ( )
inline

Initializes the parser.

Returns
true on success.

◆ checkParseOffset()

size_t checkParseOffset ( )
inlineprotected

Checks and adjusts the parse offset for valid box types.

Returns
Adjusted parse offset.

◆ continueIncrementalBox()

bool continueIncrementalBox ( )
inlineprotected

Continue filling an incremental box. Returns false if not enough data.

Returns
False if more data was processed, true otherwise.

◆ currentFileOffset()

uint64_t currentFileOffset ( )
inlineprotected

Returns the current file offset (absolute position in file).

Returns
Current file offset.

◆ defaultCallback()

static void defaultCallback ( const Box box,
void *  ref 
)
inlinestatic

Default callback that prints box information to Serial.

Parameters
boxThe box being processed.
refOptional reference pointer.

◆ getContainerDataLength()

int getContainerDataLength ( const char *  type)
inlineprotected

Gets the start offset for a subcontainer.

Parameters
typeBox type string.
Returns
Offset of the subcontainer.

◆ handleCompleteBox()

void handleCompleteBox ( const char *  type,
const uint8_t *  p,
size_t  headerSize,
size_t  payload_size,
int  level 
)
inlineprotected

Handles a complete (non-incremental) box.

Parameters
typeBox type string.
pPointer to the start of the box in the buffer.
headerSizeSize of the box header.
payload_sizeSize of the box payload.
levelNesting level of the box.

◆ handleContainerBox()

void handleContainerBox ( const char *  type,
uint64_t  boxSize,
int  level 
)
inlineprotected

Handles a container box (box with children).

Parameters
typeBox type string.
boxSizeSize of the box.
levelNesting level of the box.

◆ isContainerBox()

bool isContainerBox ( const char *  type)
inlineprotected

Checks if a box type is a container box.

Parameters
typeBox type string.
Returns
true if container box, false otherwise.

◆ isValidType()

bool isValidType ( const char *  type,
int  offset = 0 
) const
inlineprotected

Checks if a type string is a valid 4-character box type.

Parameters
typePointer to type string.
offsetOffset in the string.
Returns
true if valid, false otherwise.

◆ parseString()

int parseString ( const uint8_t *  str,
int  len,
int  fileOffset = 0,
int  level = 0 
)
inline

Trigger separate parsing (and callbacks) on the indicated string.

Parameters
strPointer to the string data.
lenLength of the string data.
Returns
Number of bytes parsed.

◆ processCallback()

void processCallback ( Box box)
inlineprotected

Processes the callback for a box. Calls the type-specific callback if present, and the generic callback if allowed.

Parameters
boxThe box being processed.

call generic callback if allowed

◆ readU32()

static uint32_t readU32 ( const uint8_t *  p)
inlinestaticprotected

Reads a 32-bit big-endian unsigned integer from a buffer.

Parameters
pPointer to buffer.
Returns
32-bit unsigned integer.

◆ readU64()

static uint64_t readU64 ( const uint8_t *  p)
inlinestaticprotected

Reads a 64-bit big-endian unsigned integer from a buffer.

Parameters
pPointer to buffer.
Returns
64-bit unsigned integer.

◆ resize()

bool resize ( size_t  size)
inline

Defines a specific buffer size.

Parameters
sizeBuffer size in bytes.
Returns
true if the buffer was resized successfully.

◆ setCallback() [1/2]

void setCallback ( BoxCallback  cb)
inline

Defines the generic callback for all boxes.

Parameters
cbCallback function for all boxes.

◆ setCallback() [2/2]

void setCallback ( const char *  type,
BoxCallback  cb,
bool  callGeneric = true 
)
inline

Defines a specific callback for a box type.

Parameters
type4-character box type (e.g. "moov", "mdat").
cbCallback function for this box type.
callGenericIf true, the generic callback will also be called after the type-specific callback.

◆ setReference()

void setReference ( void *  ref)
inline

Defines an optional reference. By default it is the parser itself.

Parameters
refPointer to reference object.

◆ startIncrementalBox()

void startIncrementalBox ( const char *  type,
const uint8_t *  p,
size_t  headerSize,
size_t  payload_size,
int  level,
size_t  bufferSize 
)
inlineprotected

Starts parsing a box incrementally.

Parameters
typeBox type string.
pPointer to the start of the box in the buffer.
headerSizeSize of the box header.
payload_sizeSize of the box payload.
levelNesting level of the box.
bufferSizeNumber of bytes available in the buffer.

◆ tryStartNewBox()

bool tryStartNewBox ( size_t  bufferSize)
inlineprotected

Try to start parsing a new box. Returns false if not enough data.

Parameters
bufferSizeNumber of bytes available in the buffer.
Returns
True if a box was started, false otherwise.

◆ write() [1/2]

size_t write ( const char *  data,
size_t  len 
)
inline

Provide the data to the parser (in chunks if needed).

Parameters
dataPointer to input data (char*).
lenLength of input data.
Returns
Number of bytes written to the buffer.

◆ write() [2/2]

size_t write ( const uint8_t *  data,
size_t  len 
)
inline

Provide the data to the parser (in chunks if needed).

Parameters
dataPointer to input data.
lenLength of input data.
Returns
Number of bytes written to the buffer.

Member Data Documentation

◆ box_in_progress

bool box_in_progress
protected
Initial value:
=
false

True if currently parsing a box incrementally.


The documentation for this class was generated from the following file: