From 9ed5a5e28c08039a08f16a9becb3e0f5991aca74 Mon Sep 17 00:00:00 2001 From: Oswald Auguste de Bruin Date: Tue, 16 Jul 2013 16:23:33 +0200 Subject: [PATCH] Vorbis started on prettyprint --- lib/ogg.cpp | 11 ++++ lib/ogg.h | 1 + lib/vorbis.cpp | 143 ++++++++++++++++++++++++++++++++++++++++++++++++- lib/vorbis.h | 16 ++++++ 4 files changed, 170 insertions(+), 1 deletion(-) diff --git a/lib/ogg.cpp b/lib/ogg.cpp index debba28c..7ccdc474 100644 --- a/lib/ogg.cpp +++ b/lib/ogg.cpp @@ -299,6 +299,17 @@ namespace OGG{ } offset += len; } + }else if(codec == "vorbis"){ + r << "Vorbis Data" << std::endl; + int offset = 0; + for (int i = 0; i < getSegmentTableDeque().size(); i++){ + vorbis::header tmpHeader; + int len = getSegmentTableDeque()[i]; + if (tmpHeader.read(getFullPayload()+offset,len)){ + r << tmpHeader.toPrettyString(indent + 4); + } + offset += len; + } } return r.str(); } diff --git a/lib/ogg.h b/lib/ogg.h index 628487e2..c983e079 100644 --- a/lib/ogg.h +++ b/lib/ogg.h @@ -4,6 +4,7 @@ #include #include"dtsc.h" #include "theora.h" +#include "vorbis.h" namespace OGG{ class Page{ diff --git a/lib/vorbis.cpp b/lib/vorbis.cpp index 9b58fb77..9d95f6a8 100644 --- a/lib/vorbis.cpp +++ b/lib/vorbis.cpp @@ -1,13 +1,93 @@ #include"vorbis.h" #include #include +#include #include namespace vorbis{ + header::header(){ data = NULL; datasize = 0; } + + int header::getHeaderType(){ + return (int)(data[0]); + } + + long unsigned int header::getVorbisVersion(){ + if (getHeaderType() == 1){ + return getInt32(7); + }else{ + return 0; + } + } + + char header::getAudioChannels(){ + if (getHeaderType() == 1){ + return data[11]; + }else{ + return 0; + } + } + + long unsigned int header::getAudioSampleRate(){ + if (getHeaderType() == 1){ + return getInt32(12); + }else{ + return 0; + } + } + + long unsigned int header::getBitrateMaximum(){ + if (getHeaderType() == 1){ + return getInt32(16); + }else{ + return 0; + } + } + + long unsigned int header::getBitrateNominal(){ + if (getHeaderType() == 1){ + return getInt32(20); + }else{ + return 0; + } + } + + long unsigned int header::getBitrateMinimum(){ + if (getHeaderType() == 1){ + return getInt32(24); + }else{ + return 0; + } + + } + + char header::getBlockSize0(){ + if (getHeaderType() == 1){ + return data[28]>>4; + }else{ + return 0; + } + } + + char header::getBlockSize1(){ + if (getHeaderType() == 1){ + return data[28] & 0x0F; + }else{ + return 0; + } + } + + char header::getFramingFlag(){ + if (getHeaderType() == 1){ + return data[29]; + }else{ + return 0; + } + } + bool header::checkDataSize(unsigned int size){ if (size > datasize){ void* tmp = realloc(data,size); @@ -23,12 +103,73 @@ namespace vorbis{ } } + bool header::validate(){ + switch(getHeaderType()){ + case 1://ID header + if (datasize!=29) return false; + //if (getVorbisVersion()!=0) return false; + //if (getAudioChannels()<=0) return false; + //if (getAudioSampleRate()<=0) return false; + //if (getBlockSize0()>getBlockSize1()) return false; + //if (getFramingFlag()!=1) return false; + break; + case 3://comment header + break; + case 5://init header + break; + default: + return false; + break; + } + return true; + } + bool header::read(char* newData, unsigned int length){ + if (length < 7){ + return false; + } + /*if (! (newData[0] & 0x80)){ + return false; + }*/ + if(memcmp(newData+1, "vorbis", 6)!=0){ + return false; + } + if (checkDataSize(length)){ memcpy(data, newData, length); }else{ return false; } - return true; + return validate(); } + + + + uint32_t header::getInt32(size_t index){ + if (datasize >= (index + 3)){ + return (data[index] << 24) + (data[index + 1] << 16) + (data[index + 2] << 8) + data[index + 3]; + } + return 0; + } + + uint32_t header::getInt24(size_t index){ + if (datasize >= (index + 2)){ + return 0 + (data[index] << 16) + (data[index + 1] << 8) + data[index + 2]; + } + return 0; + } + + uint16_t header::getInt16(size_t index){ + if (datasize >= (index + 1)){ + return 0 + (data[index] << 8) + data[index + 1]; + } + return 0; + } + + std::string header::toPrettyString(size_t indent){ + std::stringstream r; + r << "Testing vorbis Pretty string" << std::endl; + return r.str(); + } + } diff --git a/lib/vorbis.h b/lib/vorbis.h index 194bc812..3ef5c57a 100644 --- a/lib/vorbis.h +++ b/lib/vorbis.h @@ -8,9 +8,25 @@ namespace vorbis{ public: header(); bool read(char* newData, unsigned int length); + int getHeaderType(); + long unsigned int getVorbisVersion(); + char getAudioChannels(); + long unsigned int getAudioSampleRate(); + long unsigned int getBitrateMaximum(); + long unsigned int getBitrateNominal(); + long unsigned int getBitrateMinimum(); + char getBlockSize0(); + char getBlockSize1(); + char getFramingFlag(); + std::string toPrettyString(size_t indent = 0); + protected: + uint32_t getInt32(size_t index); + uint32_t getInt24(size_t index); + uint16_t getInt16(size_t index); private: char* data; unsigned int datasize; bool checkDataSize(unsigned int size); + bool validate(); }; }