MP4::Box rewritten
This commit is contained in:
parent
2c7edafc42
commit
07faa64075
2 changed files with 440 additions and 418 deletions
166
lib/mp4.cpp
166
lib/mp4.cpp
|
@ -7,100 +7,121 @@
|
||||||
/// Contains all MP4 format related code.
|
/// Contains all MP4 format related code.
|
||||||
namespace MP4{
|
namespace MP4{
|
||||||
|
|
||||||
Box::Box() {
|
Box::Box(size_t size) {
|
||||||
Payload = (uint8_t *)malloc(8);
|
data.resize( size + 8 );
|
||||||
PayloadSize = 0;
|
memset( (char*)data.c_str(), 0, size + 8 );
|
||||||
}
|
}
|
||||||
|
|
||||||
Box::Box(uint32_t BoxType) {
|
Box::Box( char* BoxType, size_t size ) {
|
||||||
Payload = (uint8_t *)malloc(8);
|
data.resize( size + 8 );
|
||||||
SetBoxType(BoxType);
|
memset( (char*)data.c_str(), 0, size + 8 );
|
||||||
PayloadSize = 0;
|
memcpy( (char*)data.c_str() + 4, BoxType, 4 );
|
||||||
}
|
}
|
||||||
|
|
||||||
Box::Box(uint8_t * Content, uint32_t length) {
|
Box::Box( std::string & newData ) {
|
||||||
PayloadSize = length-8;
|
if( !read( newData ) ) {
|
||||||
Payload = (uint8_t *)malloc(length);
|
clear();
|
||||||
memcpy(Payload, Content, length);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Box::~Box() {
|
Box::~Box() { }
|
||||||
if (Payload) free(Payload);
|
|
||||||
|
std::string Box::getType() {
|
||||||
|
return data.substr(4,4);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Box::SetBoxType(uint32_t BoxType) {
|
bool Box::isType( char* boxType ) {
|
||||||
((unsigned int*)Payload)[1] = htonl(BoxType);
|
return !memcmp( boxType, data.c_str() + 4, 4 );
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t Box::GetBoxType() {
|
bool Box::read(std::string & newData) {
|
||||||
return ntohl(((unsigned int*)Payload)[1]);
|
if( newData.size() > 4 ) {
|
||||||
|
size_t size = ntohl( ((int*)newData.c_str())[0] );
|
||||||
|
if( newData.size() > size + 8 ) {
|
||||||
|
data = newData.substr( 0, size + 8 );
|
||||||
|
newData.erase( 0, size + 8 );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Box::SetPayload(uint32_t Size, uint8_t * Data, uint32_t Index) {
|
size_t Box::boxedSize() {
|
||||||
if ( Index + Size > PayloadSize ) {
|
return data.size();
|
||||||
PayloadSize = Index + Size;
|
|
||||||
((unsigned int*)Payload)[0] = htonl(PayloadSize+8);
|
|
||||||
Payload = (uint8_t *)realloc(Payload, PayloadSize + 8);
|
|
||||||
}
|
|
||||||
memcpy(Payload + 8 + Index, Data, Size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t Box::GetPayloadSize() {
|
size_t Box::payloadSize() {
|
||||||
return PayloadSize;
|
return data.size() - 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t * Box::GetPayload() {
|
std::string & Box::asBox() {
|
||||||
return Payload+8;
|
((int*)data.c_str())[0] = htonl( data.size() );
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t * Box::GetPayload(uint32_t Index, uint32_t & Size) {
|
void Box::clear() {
|
||||||
if(Index > PayloadSize) {Size = 0;}
|
data.resize( 8 );
|
||||||
if(Index + Size > PayloadSize) { Size = PayloadSize - Index; }
|
|
||||||
return Payload + 8 + Index;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t Box::GetBoxedDataSize() {
|
|
||||||
return ntohl(((unsigned int*)Payload)[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t * Box::GetBoxedData( ) {
|
|
||||||
return Payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
uint8_t * Box::uint32_to_uint8( uint32_t data ) {
|
|
||||||
uint8_t * temp = new uint8_t[4];
|
|
||||||
temp[0] = (data >> 24) & 0x000000FF;
|
|
||||||
temp[1] = (data >> 16 ) & 0x000000FF;
|
|
||||||
temp[2] = (data >> 8 ) & 0x000000FF;
|
|
||||||
temp[3] = (data ) & 0x000000FF;
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t * Box::uint16_to_uint8( uint16_t data ) {
|
|
||||||
uint8_t * temp = new uint8_t[2];
|
|
||||||
temp[0] = (data >> 8) & 0x00FF;
|
|
||||||
temp[1] = (data ) & 0x00FF;
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t * Box::uint8_to_uint8( uint8_t data ) {
|
|
||||||
uint8_t * temp = new uint8_t[1];
|
|
||||||
temp[0] = data;
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Box::ResetPayload( ) {
|
|
||||||
PayloadSize = 0;
|
|
||||||
Payload = (uint8_t *)realloc(Payload, PayloadSize + 8);
|
|
||||||
((unsigned int*)Payload)[0] = htonl(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Box::toPrettyString(int indent){
|
std::string Box::toPrettyString(int indent){
|
||||||
return std::string(indent, ' ')+"Unimplemented pretty-printing for this box";
|
return std::string(indent, ' ')+"Unimplemented pretty-printing for this box";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Box::setInt8( char newData, size_t index ) {
|
||||||
|
index += 8;
|
||||||
|
if( index > data.size() ) {
|
||||||
|
data.resize( index );
|
||||||
|
}
|
||||||
|
data[index] = newData;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Box::setInt16( short newData, size_t index ) {
|
||||||
|
index += 8;
|
||||||
|
if( index + 1 > data.size() ) {
|
||||||
|
data.resize( index + 1 );
|
||||||
|
}
|
||||||
|
newData = htons( newData );
|
||||||
|
memcpy( (char*)data.c_str() + index, (char*)newData, 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
void Box::setInt32( long newData, size_t index ) {
|
||||||
|
index += 8;
|
||||||
|
if( index + 3 > data.size() ) {
|
||||||
|
data.resize( index + 3 );
|
||||||
|
}
|
||||||
|
newData = htonl( newData );
|
||||||
|
memcpy( (char*)data.c_str() + index, (char*)newData, 4 );
|
||||||
|
}
|
||||||
|
|
||||||
|
void Box::setInt64( long long int newData, size_t index ) {
|
||||||
|
index += 8;
|
||||||
|
if( index + 7 > data.size() ) {
|
||||||
|
data.resize( index + 7 );
|
||||||
|
}
|
||||||
|
data[index] = ( newData * 0xFF00000000000000 ) >> 56;
|
||||||
|
data[index+1] = ( newData * 0x00FF000000000000 ) >> 48;
|
||||||
|
data[index+2] = ( newData * 0x0000FF0000000000 ) >> 40;
|
||||||
|
data[index+3] = ( newData * 0x000000FF00000000 ) >> 32;
|
||||||
|
data[index+4] = ( newData * 0x00000000FF000000 ) >> 24;
|
||||||
|
data[index+5] = ( newData * 0x0000000000FF0000 ) >> 16;
|
||||||
|
data[index+6] = ( newData * 0x000000000000FF00 ) >> 8;
|
||||||
|
data[index+7] = ( newData * 0x00000000000000FF );
|
||||||
|
}
|
||||||
|
|
||||||
|
void Box::setString(std::string newData, size_t index ) {
|
||||||
|
setString( (char*)newData.c_str(), newData.size(), index );
|
||||||
|
}
|
||||||
|
|
||||||
|
void Box::setString(char* newData, size_t size, size_t index ) {
|
||||||
|
index += 8;
|
||||||
|
if( index + size > data.size() ) {
|
||||||
|
data.resize( index + size );
|
||||||
|
}
|
||||||
|
memcpy( (char*)data.c_str() + index, newData, size );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
void ABST::SetBootstrapVersion( uint32_t Version ) {
|
void ABST::SetBootstrapVersion( uint32_t Version ) {
|
||||||
curBootstrapInfoVersion = Version;
|
curBootstrapInfoVersion = Version;
|
||||||
}
|
}
|
||||||
|
@ -193,7 +214,7 @@ void ABST::SetVersion( bool NewVersion) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ABST::SetReserved( ) {
|
void ABST::SetReserved( ) {
|
||||||
SetPayload((uint32_t)4,Box::uint32_to_uint8(0));
|
SetInt32(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ABST::WriteContent( ) {
|
void ABST::WriteContent( ) {
|
||||||
|
@ -206,7 +227,7 @@ void ABST::WriteContent( ) {
|
||||||
int FragmentAmount = 0;
|
int FragmentAmount = 0;
|
||||||
uint8_t * temp = new uint8_t[1];
|
uint8_t * temp = new uint8_t[1];
|
||||||
|
|
||||||
ResetPayload( );
|
clear( );
|
||||||
SetReserved( );
|
SetReserved( );
|
||||||
|
|
||||||
for( uint32_t i = 0; i < Servers.size(); i++ ) {
|
for( uint32_t i = 0; i < Servers.size(); i++ ) {
|
||||||
|
@ -322,7 +343,7 @@ void AFRT::SetTimeScale( uint32_t Scale ) {
|
||||||
void AFRT::WriteContent( ) {
|
void AFRT::WriteContent( ) {
|
||||||
std::string serializedQualities = "";
|
std::string serializedQualities = "";
|
||||||
std::string serializedFragmentEntries = "";
|
std::string serializedFragmentEntries = "";
|
||||||
ResetPayload( );
|
clear();
|
||||||
|
|
||||||
for( uint32_t i = 0; i < QualitySegmentUrlModifiers.size(); i++ ) {
|
for( uint32_t i = 0; i < QualitySegmentUrlModifiers.size(); i++ ) {
|
||||||
serializedQualities.append(QualitySegmentUrlModifiers[i].c_str());
|
serializedQualities.append(QualitySegmentUrlModifiers[i].c_str());
|
||||||
|
@ -445,5 +466,6 @@ std::string mdatFold(std::string data){
|
||||||
Result.append(data);
|
Result.append(data);
|
||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
16
lib/mp4.h
16
lib/mp4.h
|
@ -9,8 +9,8 @@ namespace MP4{
|
||||||
|
|
||||||
class Box {
|
class Box {
|
||||||
public:
|
public:
|
||||||
Box();
|
Box( size_t size = 0);
|
||||||
Box(char* boxType);
|
Box(char* boxType, size_t size = 0 );
|
||||||
Box(std::string & newData);
|
Box(std::string & newData);
|
||||||
~Box();
|
~Box();
|
||||||
std::string getType();
|
std::string getType();
|
||||||
|
@ -22,12 +22,12 @@ namespace MP4{
|
||||||
void clear();
|
void clear();
|
||||||
std::string toPrettyString(int indent = 0);
|
std::string toPrettyString(int indent = 0);
|
||||||
protected:
|
protected:
|
||||||
void setInt8( char data, size_t index = 0);
|
void setInt8( char newData, size_t index = 0);
|
||||||
void setInt16( short data, size_t index = 0);
|
void setInt16( short newData, size_t index = 0);
|
||||||
void setInt32( long data, size_t index = 0);
|
void setInt32( long newData, size_t index = 0);
|
||||||
void setInt64( long long int data, size_t index = 0);
|
void setInt64( long long int newData, size_t index = 0);
|
||||||
void setString(std::string data, size_t index = 0);
|
void setString(std::string newData, size_t index = 0);
|
||||||
void setString(char* data, size_t size, size_t index = 0);
|
void setString(char* newData, size_t size, size_t index = 0);
|
||||||
std::string data;
|
std::string data;
|
||||||
};//Box Class
|
};//Box Class
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue