Skip to content

Commit

Permalink
Merge pull request andygrundman#3 from jtbr/mp4_atom_size
Browse files Browse the repository at this point in the history
Smoothly handle small and ignore empty mpeg-4 atoms.
Remove small bit of redundant code
  • Loading branch information
ralph-irving authored Nov 29, 2023
2 parents 12632c5 + b3da627 commit 99f8e7d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
6 changes: 3 additions & 3 deletions include/mp4.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ typedef struct mp4info {
char *file;
Buffer *buf;
uint64_t file_size; // total file size
uint64_t size; // total size
uint8_t hsize; // header size
uint64_t rsize; // remaining size
uint64_t size; // total size of box; 0 => remainder of file
uint8_t hsize; // header size in box
uint64_t rsize; // remaining size in box outside header
uint64_t audio_offset;
uint64_t audio_size;
HV *info;
Expand Down
38 changes: 23 additions & 15 deletions src/mp4.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ _mp4_read_box(mp4info *mp4)

mp4->rsize = 0; // remaining size in box

if ( !_check_buf(mp4->infile, mp4->buf, 16, MP4_BLOCK_SIZE) ) {
if ( !_check_buf(mp4->infile, mp4->buf, 8, MP4_BLOCK_SIZE) ) {
return 0;
}

Expand All @@ -678,27 +678,38 @@ _mp4_read_box(mp4info *mp4)
type[4] = '\0';
buffer_consume(mp4->buf, 4);

mp4->hsize = 8;
// Check for 64-bit size
if (size == 1) {
if ( !_check_buf(mp4->infile, mp4->buf, 8, MP4_BLOCK_SIZE) ) {
return 0;
}
size = buffer_get_int64(mp4->buf);
mp4->hsize = 16;
}
else if (size == 0) {
// XXX: size extends to end of file
mp4->hsize = 8;

if (size == 0) {
// XXX: box extends to end of file
/*nothing to do*/ ; // rsize=size=0
}
else if (size < mp4->hsize) {
PerlIO_printf(PerlIO_stderr(), "Invalid box size in: %s\n", mp4->file);
return 0;
}
else {
mp4->hsize = 8;
}

if (size) {
// set size of the remainder of the box
mp4->rsize = size - mp4->hsize;
}
}

mp4->size = size;

DEBUG_TRACE("%s size %llu\n", type, size);

if (size == mp4->hsize) {
PerlIO_printf(PerlIO_stderr(), "Ignoring empty box of type %s in: %s\n", type, mp4->file);
return size;
}

if (mp4->seekhdr) {
// Copy and adjust header if seeking
char tmp_size[4];
Expand Down Expand Up @@ -791,20 +802,17 @@ _mp4_read_box(mp4info *mp4)
|| FOURCC_EQ(type, "dinf")
|| FOURCC_EQ(type, "stbl")
|| FOURCC_EQ(type, "udta")
|| FOURCC_EQ(type, "trak")
) {
// These boxes are containers for nested boxes, return only the fact that
// we read the header size of the container
// we read the header size of the container. Read the nested box the next call to this fn.
size = mp4->hsize;

if ( FOURCC_EQ(type, "trak") ) {
// Also a container, but we need to increment track_count too
mp4->track_count++;
}
}
else if ( FOURCC_EQ(type, "trak") ) {
// Also a container, but we need to increment track_count too
size = mp4->hsize;
mp4->track_count++;
}
else if ( FOURCC_EQ(type, "mvhd") ) {
mp4->seen_moov = 1;

Expand Down

0 comments on commit 99f8e7d

Please sign in to comment.