From 5b3a53e9f5804dc320a88c1e676cadb00cda3d6a Mon Sep 17 00:00:00 2001 From: Erik Zandvliet Date: Tue, 25 Sep 2012 12:46:49 +0200 Subject: [PATCH] Added MOOF and TRAF boxes --- lib/mp4.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++-- lib/mp4.h | 16 ++++++-- 2 files changed, 114 insertions(+), 8 deletions(-) diff --git a/lib/mp4.cpp b/lib/mp4.cpp index b55d9e45..1018dcc5 100644 --- a/lib/mp4.cpp +++ b/lib/mp4.cpp @@ -100,6 +100,8 @@ namespace MP4{ case 0x61627374: return ((ABST*)this)->toPrettyString(indent); break; case 0x61667274: return ((AFRT*)this)->toPrettyString(indent); break; case 0x61737274: return ((ASRT*)this)->toPrettyString(indent); break; + case 0x7472756E: return ((TRUN*)this)->toPrettyString(indent); break; + case 0x74726166: return ((TRAF*)this)->toPrettyString(indent); break; default: return std::string(indent, ' ')+"Unimplemented pretty-printing for box "+std::string(data+4,4)+"\n"; break; } } @@ -944,8 +946,43 @@ namespace MP4{ memcpy(data + 4, "moof", 4); } - void MOOF::addContent( Box* newContent ) { - content.push_back( newContent ); + long MOOF::getContentCount() { + int res = 0; + int tempLoc = 0; + while( tempLoc < boxedSize()-8 ){ + res ++; + tempLoc += Box(data+8+tempLoc, false).boxedSize(); + } + return res; + } + + void MOOF::setContent( Box newContent, long no ) { + int tempLoc = 0; + int contentCount = getContentCount(); + for (int i = 0; i < no; i++){ + if (i < contentCount){ + tempLoc += Box(data+8+tempLoc,false).boxedSize(); + } else { + if(!reserve(tempLoc, 0, (no - contentCount)*8)){return;}; + memset(data+tempLoc, 0, (no - contentCount)*8); + tempLoc += (no - contentCount)*8; + break; + } + } + Box oldContent = Box( data+8+tempLoc, false ); + if( !reserve( tempLoc, oldContent.boxedSize(), newContent.boxedSize() ) ) { return; } + memcpy( data+8+tempLoc, newContent.asBox(), newContent.boxedSize() ); + } + + Box MOOF::getContent( long no ){ + if( no > getContentCount() ) { return Box(); } + int i = 0; + int tempLoc = 0; + while( i < no ) { + tempLoc += Box( data+8+tempLoc, false).boxedSize(); + i++; + } + return Box(data+8+tempLoc, false); } std::string MOOF::toPrettyString( int indent ) { @@ -953,14 +990,75 @@ namespace MP4{ r << std::string(indent, ' ') << "[moof] Movie Fragment Box" << std::endl; Box curBox; int tempLoc = 0; - while( tempLoc < boxedSize()-8 ){ - curBox = Box( data+8+tempLoc, false ); + int contentCount = getContentCount(); + for( int i = 0; i < contentCount; i++ ) { + curBox = getContent(i); r << curBox.toPrettyString(indent+1); tempLoc += curBox.boxedSize(); } return r.str(); } + TRAF::TRAF(){ + memcpy(data + 4, "traf", 4); + } + + long TRAF::getContentCount() { + int res = 0; + int tempLoc = 0; + while( tempLoc < boxedSize()-8 ){ + res ++; + tempLoc += Box(data+8+tempLoc, false).boxedSize(); + } + return res; + } + + void TRAF::setContent( Box newContent, long no ) { + int tempLoc = 0; + int contentCount = getContentCount(); + for (int i = 0; i < no; i++){ + if (i < contentCount){ + tempLoc += Box(data+8+tempLoc,false).boxedSize(); + } else { + if(!reserve(tempLoc, 0, (no - contentCount)*8)){return;}; + memset(data+tempLoc, 0, (no - contentCount)*8); + tempLoc += (no - contentCount)*8; + break; + } + } + Box oldContent = Box( data+8+tempLoc, false ); + if( !reserve( tempLoc, oldContent.boxedSize(), newContent.boxedSize() ) ) { return; } + memcpy( data+8+tempLoc, newContent.asBox(), newContent.boxedSize() ); + } + + Box TRAF::getContent( long no ){ + if( no > getContentCount() ) { return Box(); } + int i = 0; + int tempLoc = 0; + while( i < no ) { + tempLoc += Box( data+8+tempLoc, false).boxedSize(); + i++; + } + return Box(data+8+tempLoc, false); + } + + std::string TRAF::toPrettyString( int indent ) { + std::stringstream r; + r << std::string(indent, ' ') << "[traf] Track Fragment Box" << std::endl; + Box curBox; + int tempLoc = 0; + int contentCount = getContentCount(); + for( int i = 0; i < contentCount; i++ ) { + curBox = getContent(i); + r << curBox.toPrettyString(indent+1); + tempLoc += curBox.boxedSize(); + } + return r.str(); + } + + + + TRUN::TRUN(){ memcpy(data + 4, "trun", 4); } diff --git a/lib/mp4.h b/lib/mp4.h index 2cf44f27..2cb8b243 100644 --- a/lib/mp4.h +++ b/lib/mp4.h @@ -150,13 +150,21 @@ namespace MP4{ class MOOF : public Box { public: MOOF(); - void addContent( Box* newContent ); - void regenerate( ); + long getContentCount(); + void setContent( Box newContent, long no ); + Box getContent( long no ); std::string toPrettyString(int indent = 0); - private: - std::deque content; };//MOOF Box + class TRAF : public Box { + public: + TRAF(); + long getContentCount(); + void setContent( Box newContent, long no ); + Box getContent( long no ); + std::string toPrettyString(int indent = 0); + };//TRAF Box + struct trunSampleInformation { long sampleDuration; long sampleSize;