diff --git a/MP4/Makefile b/MP4/Makefile new file mode 100644 index 00000000..132139c8 --- /dev/null +++ b/MP4/Makefile @@ -0,0 +1,18 @@ +SRC = main.cpp box.cpp box_ftyp.cpp +OBJ = $(SRC:.cpp=.o) +OUT = Boxtest +INCLUDES = +CCFLAGS = -Wall -Wextra -funsigned-char -g +CC = $(CROSS)g++ +LD = $(CROSS)ld +AR = $(CROSS)ar +LIBS = +.SUFFIXES: .cpp +.PHONY: clean default +default: $(OUT) +.cpp.o: + $(CC) $(INCLUDES) $(CCFLAGS) $(LIBS) -c $< -o $@ +$(OUT): $(OBJ) + $(CC) $(LIBS) -o $(OUT) $(OBJ) +clean: + rm -rf $(OBJ) $(OUT) Makefile.bak *~ diff --git a/MP4/box.cpp b/MP4/box.cpp new file mode 100644 index 00000000..005012c5 --- /dev/null +++ b/MP4/box.cpp @@ -0,0 +1,75 @@ +#include "box.h" + +Box::Box() { + Payload = NULL; + PayloadSize = 0; +} + +Box::Box(uint32_t BoxType) { + header.BoxType = BoxType; + Payload = NULL; + PayloadSize = 0; +} + +Box::~Box() { +} + +void Box::SetBoxType(uint32_t BoxType) { + header.BoxType = BoxType; +} + +uint32_t Box::GetBoxType() { + return header.BoxType; +} + +void Box::SetPayload(uint32_t Size, uint8_t * Data, uint32_t Index) { + uint8_t * tempchar = NULL; + if ( Index + Size > PayloadSize ) { + if ( Payload ) { + tempchar = new uint8_t[PayloadSize]; + memcpy( tempchar, Payload, PayloadSize ); + delete Payload; + } + PayloadSize = Index + Size; + Payload = new uint8_t[PayloadSize]; + if( tempchar ) { + memcpy( Payload, tempchar, Index ); + } else { + for(uint32_t i = 0; i < Index; i++) { Payload[i] = 0; } + } + memcpy( &Payload[Index], Data, Size ); + header.TotalSize = PayloadSize + 8; + if( tempchar ) { + delete tempchar; + } + } else { + memcpy( &Payload[Index], Data, Size ); + } +} + +uint32_t Box::GetPayloadSize() { + return PayloadSize; +} + +uint8_t * Box::GetPayload() { + uint8_t * temp = new uint8_t[PayloadSize]; + memcpy( temp, Payload, PayloadSize ); + return temp; +} + +uint8_t * Box::GetPayload(uint32_t Index, uint32_t & Size) { + if(Index > PayloadSize) { return NULL; } + if(Index + Size > PayloadSize) { Size = PayloadSize - Index; } + uint8_t * temp = new uint8_t[Size - Index]; + memcpy( temp, &Payload[Index], Size - Index ); + return temp; +} + +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; +} diff --git a/MP4/box.h b/MP4/box.h index bab440b6..f7865ea6 100644 --- a/MP4/box.h +++ b/MP4/box.h @@ -1,4 +1,7 @@ #include "boxheader.h" +#include +#include + class Box { public: @@ -9,50 +12,16 @@ class Box { void SetBoxType(uint32_t BoxType); uint32_t GetBoxType(); - void SetPayload(uint32_t PayloadSize, uint8_t * Data); + void SetPayload(uint32_t Size, uint8_t * Data, uint32_t Index = 0); + uint32_t GetPayloadSize(); uint8_t * GetPayload(); - uint8_t * GetPayload(uint32_t Index, uint32_t Size); + uint8_t * GetPayload(uint32_t Index, uint32_t & Size); + + static uint8_t * uint32_to_uint8( uint32_t data ); private: BoxHeader header; uint8_t * Payload; + + uint32_t PayloadSize; };//Box Class -Box::Box() { - Payload = NULL; -} - -Box::Box(uint32_t BoxType) { - header.BoxType = BoxType; - Payload = NULL; -} - -Box::~Box() { -} - -void Box::SetBoxType(uint32_t BoxType) { - header.BoxType = BoxType; -} - -uint32_t Box::GetBoxType() { - return header.BoxType; -} - -void Box::SetPayload(uint32_t PayloadSize, uint8_t * Data ) { - if ( Payload != NULL ) { delete Payload; } - Payload = new uint8_t[PayloadSize]; - memcpy( Payload, Data, PayloadSize ); - header.TotalSize = PayloadSize + 8; -} - -uint8_t * Box::GetPayload() { - uint8_t * temp = new uint8_t[header.TotalSize - 8]; - memcpy( temp, Payload, header.TotalSize - 8 ); - return temp; -} - -uint8_t * Box::GetPayload(uint32_t Index, uint32_t Size) { - if( - uint8_t * temp = new uint8_t[header.TotalSize - 8]; - memcpy( temp, Payload, header.TotalSize - 8 ); - return temp; -} diff --git a/MP4/box_ftyp.cpp b/MP4/box_ftyp.cpp new file mode 100644 index 00000000..1de5f1b6 --- /dev/null +++ b/MP4/box_ftyp.cpp @@ -0,0 +1,23 @@ +#include "box_ftyp.h" + +Box_ftyp::Box_ftyp( uint32_t MajorBrand, uint32_t MinorBrand ) { + Container = new Box( 0x66747970 ); + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8(MajorBrand)); + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8(MinorBrand),4); +} + +Box_ftyp::~Box_ftyp() { + delete Container; +} + +Box * Box_ftyp::GetBox() { + return Container; +} + +void Box_ftyp::SetMajorBrand( uint32_t MajorBrand ) { + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8(MajorBrand)); +} + +void Box_ftyp::SetMinorBrand( uint32_t MinorBrand ) { + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8(MinorBrand),4); +} diff --git a/MP4/box_ftyp.h b/MP4/box_ftyp.h new file mode 100644 index 00000000..f5b0c746 --- /dev/null +++ b/MP4/box_ftyp.h @@ -0,0 +1,13 @@ +#include "box.h" + +class Box_ftyp { + public: + Box_ftyp( uint32_t MajorBrand = 0x66347620, uint32_t MinorBrand = 0x1); + ~Box_ftyp(); + Box * GetBox(); + void SetMajorBrand( uint32_t MajorBrand ); + void SetMinorBrand( uint32_t MinorBrand ); + private: + Box * Container; +};//Box_ftyp Class + diff --git a/MP4/boxheader.h b/MP4/boxheader.h index b3dbc501..70b674c3 100644 --- a/MP4/boxheader.h +++ b/MP4/boxheader.h @@ -1,3 +1,6 @@ +#include +#include + struct BoxHeader { uint32_t TotalSize; uint32_t BoxType; diff --git a/MP4/main.cpp b/MP4/main.cpp new file mode 100644 index 00000000..dd3c6f8e --- /dev/null +++ b/MP4/main.cpp @@ -0,0 +1,15 @@ +#include +#include "box_ftyp.h" + +int main() { + Box_ftyp * FileType = new Box_ftyp(); + printf("Boxtype: %x\n", FileType->GetBox()->GetBoxType()); + uint8_t * TestPayload = FileType->GetBox()->GetPayload(); + uint32_t TestPayloadSize = FileType->GetBox()->GetPayloadSize(); + printf("PayloadSize: %d\n", TestPayloadSize); + for(uint32_t i = 0; i < TestPayloadSize; i++) { + printf("Payload[%d]: %x\n", i, TestPayload[i]); + } + delete FileType; + return 0; +}