From c93a0bb815777af70aaeac956e8611f854b40eff Mon Sep 17 00:00:00 2001 From: Erik Zandvliet Date: Mon, 27 Dec 2010 16:52:58 +0100 Subject: [PATCH] Changes till now --- MP4/Makefile | 2 +- MP4/box.cpp | 20 ++++++++++++++++ MP4/box.h | 7 +++++- MP4/box_h264.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ MP4/box_h264.h | 20 ++++++++++++++++ MP4/box_stsd.cpp | 48 +++++++++++++++++++++++++++++++++++++ MP4/box_stsd.h | 18 ++++++++++++++ MP4/main.cpp | 24 +++++++++++-------- 8 files changed, 188 insertions(+), 12 deletions(-) create mode 100644 MP4/box_h264.cpp create mode 100644 MP4/box_h264.h create mode 100644 MP4/box_stsd.cpp create mode 100644 MP4/box_stsd.h diff --git a/MP4/Makefile b/MP4/Makefile index 132139c8..52b3cd7f 100644 --- a/MP4/Makefile +++ b/MP4/Makefile @@ -1,4 +1,4 @@ -SRC = main.cpp box.cpp box_ftyp.cpp +SRC = main.cpp box.cpp box_h264.cpp box_stsd.cpp OBJ = $(SRC:.cpp=.o) OUT = Boxtest INCLUDES = diff --git a/MP4/box.cpp b/MP4/box.cpp index 005012c5..297f05d5 100644 --- a/MP4/box.cpp +++ b/MP4/box.cpp @@ -73,3 +73,23 @@ uint8_t * Box::uint32_to_uint8( uint32_t data ) { 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; +} + +BoxHeader Box::GetHeader( ) { + return header; +} + +void Box::ResetPayload( ) { + header.TotalSize -= PayloadSize; + PayloadSize = 0; + if(Payload) { + delete Payload; + Payload = NULL; + } +} diff --git a/MP4/box.h b/MP4/box.h index f7865ea6..3e7ce466 100644 --- a/MP4/box.h +++ b/MP4/box.h @@ -1,7 +1,9 @@ +#pragma once #include "boxheader.h" #include #include - +#include +#include class Box { public: @@ -18,6 +20,9 @@ class Box { uint8_t * GetPayload(uint32_t Index, uint32_t & Size); static uint8_t * uint32_to_uint8( uint32_t data ); + static uint8_t * uint16_to_uint8( uint16_t data ); + BoxHeader GetHeader( ); + void ResetPayload( ); private: BoxHeader header; uint8_t * Payload; diff --git a/MP4/box_h264.cpp b/MP4/box_h264.cpp new file mode 100644 index 00000000..ba25b338 --- /dev/null +++ b/MP4/box_h264.cpp @@ -0,0 +1,61 @@ +#include "box_h264.h" + +Box_h264::Box_h264( ) { + Container = new Box( 0x68323634 ); + SetReserved(); + SetDefaults(); +} + +Box_h264::~Box_h264() { + delete Container; +} + +Box * Box_h264::GetBox() { + return Container; +} + +void Box_h264::SetDataReferenceIndex( uint16_t DataReferenceIndex ) { + Container->SetPayload((uint32_t)2,Box::uint16_to_uint8( DataReferenceIndex ),6); +} + +void Box_h264::SetDimensions ( uint16_t Width, uint16_t Height ) { + Container->SetPayload((uint32_t)2,Box::uint16_to_uint8( Height ),26); + Container->SetPayload((uint32_t)2,Box::uint16_to_uint8( Width ),24); +} + +void Box_h264::SetResolution ( uint32_t Horizontal, uint32_t Vertical ) { + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8( Vertical ),32); + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8( Horizontal ),28); +} + +void Box_h264::SetFrameCount ( uint16_t FrameCount ) { + Container->SetPayload((uint32_t)2,Box::uint16_to_uint8( FrameCount ),40); +} + +void Box_h264::SetCompressorName ( std::string CompressorName ) { + uint8_t * Printable = new uint8_t[1]; + Printable[0] = std::min( (unsigned int)31, CompressorName.size() ); + Container->SetPayload((uint32_t)Printable[0],(uint8_t*)CompressorName.c_str(),43); + Container->SetPayload((uint32_t)1, Printable ,42); +} + +void Box_h264::SetDepth ( uint16_t Depth ) { + Container->SetPayload((uint32_t)2,Box::uint16_to_uint8( Depth ),74); +} + +void Box_h264::SetReserved( ) { + Container->SetPayload((uint32_t)2,Box::uint16_to_uint8( (uint16_t)-1 ),76); + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8(0),36); + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8(0),20); + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8(0),16); + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8(0),12); + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8(0),8); + Container->SetPayload((uint32_t)4,Box::uint16_to_uint8(0),4); + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8(0)); +} + +void Box_h264::SetDefaults( ) { + SetResolution ( ); + SetFrameCount ( ); + SetDepth ( ); +} diff --git a/MP4/box_h264.h b/MP4/box_h264.h new file mode 100644 index 00000000..f0f4920f --- /dev/null +++ b/MP4/box_h264.h @@ -0,0 +1,20 @@ +#include "box.h" +#include + +class Box_h264 { + public: + Box_h264( ); + ~Box_h264(); + Box * GetBox(); + void SetReserved( ); + void SetDataReferenceIndex( uint16_t DataReferenceIndex ); + void SetDimensions ( uint16_t Width, uint16_t Height ); + void SetResolution ( uint32_t Horizontal = 0x00480000, uint32_t Vertical = 0x00480000 ); + void SetFrameCount ( uint16_t FrameCount = 1 ); + void SetCompressorName ( std::string CompressorName ); + void SetDepth ( uint16_t Depth = 0x0018 ); + void SetDefaults( ); + private: + Box * Container; +};//Box_ftyp Class + diff --git a/MP4/box_stsd.cpp b/MP4/box_stsd.cpp new file mode 100644 index 00000000..48c5cc66 --- /dev/null +++ b/MP4/box_stsd.cpp @@ -0,0 +1,48 @@ +#include "box_stsd.h" + +Box_stsd::Box_stsd( ) { + Container = new Box( 0x73747364 ); + SetDefaults(); +} + +Box_stsd::~Box_stsd() { + delete Container; +} + +Box * Box_stsd::GetBox() { + return Container; +} + +void Box_stsd::AddContent( Box * newcontent, uint32_t offset ) { + if( offset >= Content.size() ) { + Content.resize(offset+1); + } + if( Content[offset] ) { + delete Content[offset]; + } + Content[offset] = newcontent; + WriteContent(); +} + +void Box_stsd::SetDefaults( ) { + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8( 1 ),4); + Container->SetPayload((uint32_t)4,Box::uint32_to_uint8( 0 ),0); +} + +void Box_stsd::WriteContent( ) { + Container->ResetPayload( ); + SetDefaults( ); + Box * current; + BoxHeader currentheader; + std::string serializedbox = ""; + for( uint32_t i = 0; i < Content.size(); i++ ) { + current=Content[i]; + if( current ) { + currentheader = current->GetHeader(); + serializedbox.append((char*)Box::uint32_to_uint8(currentheader.TotalSize),4); + serializedbox.append((char*)Box::uint32_to_uint8(currentheader.BoxType),4); + serializedbox.append((char*)current->GetPayload(),current->GetPayloadSize()); + } + } + Container->SetPayload((uint32_t)serializedbox.size(),(uint8_t*)serializedbox.c_str(),8); +} diff --git a/MP4/box_stsd.h b/MP4/box_stsd.h new file mode 100644 index 00000000..287060de --- /dev/null +++ b/MP4/box_stsd.h @@ -0,0 +1,18 @@ +#include "box.h" +#include +#include + +class Box_stsd { + public: + Box_stsd( ); + ~Box_stsd(); + Box * GetBox(); + void AddContent( Box * newcontent, uint32_t offset = 0 ); + private: + Box * Container; + + void SetDefaults(); + void WriteContent(); + std::vector Content; +};//Box_ftyp Class + diff --git a/MP4/main.cpp b/MP4/main.cpp index dd3c6f8e..23b45efa 100644 --- a/MP4/main.cpp +++ b/MP4/main.cpp @@ -1,15 +1,19 @@ #include -#include "box_ftyp.h" +#include "box_h264.h" +#include "box_stsd.h" +#include 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; + Box_h264 * Testing = new Box_h264(); + Testing->SetCompressorName( "Test123" ); + std::cout << "H264::Size: " << Testing->GetBox()->GetHeader().TotalSize << "\n"; + Box_stsd * Testsample = new Box_stsd(); + std::cout << "STSD::Before Content: " << Testsample->GetBox()->GetHeader().TotalSize << "\n"; + Testsample->AddContent( Testing->GetBox() ); + std::cout << "STSD::After 1 Content: " << Testsample->GetBox()->GetHeader().TotalSize << "\n"; + Testsample->AddContent( Testing->GetBox(), 1 ); + std::cout << "STSD::After 2 Content: " << Testsample->GetBox()->GetHeader().TotalSize << "\n"; + delete Testsample; + delete Testing; return 0; }