MP4 rework

This commit is contained in:
Erik Zandvliet 2017-07-19 13:24:54 +02:00 committed by Thulinma
parent 266248a67e
commit f5293ea29f
6 changed files with 362 additions and 242 deletions

View file

@ -65,13 +65,25 @@ namespace MP4 {
}
}
void Box::copyFrom(const Box & rs){
clear();
if (data) {
free(data);
data = 0;
}
data = (char*)malloc(rs.data_size);
memcpy(data, rs.data, rs.data_size);
data_size = rs.data_size;
managed = true;
payloadOffset = rs.payloadOffset;
}
/// Returns the values at byte positions 4 through 7.
std::string Box::getType() {
return std::string(data + 4, 4);
}
/// Returns true if the given 4-byte boxtype is equal to the values at byte positions 4 through 7.
bool Box::isType(const char * boxType) {
bool Box::isType(const char * boxType) const {
return !memcmp(boxType, data + 4, 4);
}
@ -742,6 +754,29 @@ namespace MP4 {
return getBox(tempLoc);
}
Box containerBox::getChild(const char * boxName){
uint32_t count = getContentCount();
for (uint32_t i = 0; i < count; i++){
Box & thisChild = getContent(i);
if (thisChild.isType(boxName)){
return Box(thisChild.asBox(), false);
}
}
return Box((char*)"\000\000\000\010erro", false);
}
std::deque<Box> containerBox::getChildren(const char * boxName){
std::deque<Box> res;
uint32_t count = getContentCount();
for (uint32_t i = 0; i < count; i++){
Box & thisChild = getContent(i);
if (thisChild.isType(boxName)){
res.push_back(Box(thisChild.asBox(), false));
}
}
return res;
}
std::string containerBox::toPrettyString(uint32_t indent) {
std::stringstream r;
r << std::string(indent, ' ') << "[" << getType() << "] Container Box (" << boxedSize() << ")" << std::endl;

View file

@ -26,10 +26,16 @@ namespace MP4 {
Box(const Box & rs);
Box & operator = (const Box & rs);
~Box();
operator bool() const {
return data && data_size >= 8 && !isType("erro");
}
void copyFrom(const Box & rs);
std::string getType();
bool isType(const char * boxType);
bool isType(const char * boxType) const;
bool read(FILE * newData);
bool read(std::string & newData);
uint64_t boxedSize();
uint64_t payloadSize();
char * asBox();
@ -84,6 +90,24 @@ namespace MP4 {
void setContent(Box & newContent, uint32_t no);
Box & getContent(uint32_t no, bool unsafe = false);
std::string toPrettyString(uint32_t indent = 0);
Box getChild(const char * boxName);
template <typename T>
T getChild(){
T a;
MP4::Box r = getChild(a.getType().c_str());
return (T&)r;
}
std::deque<Box> getChildren(const char * boxName);
template <typename T>
std::deque<T> getChildren(){
T a;
std::deque<Box> tmpRes = getChildren(a.getType().c_str());
std::deque<T> res;
for (std::deque<Box>::iterator it = tmpRes.begin(); it != tmpRes.end(); it++){
res.push_back((T&)*it);
}
return res;
}
};
class containerFullBox: public fullBox {

View file

@ -581,6 +581,26 @@ namespace MP4 {
return payload() + 8;
}
std::string AVCC::hexSPS(){
std::stringstream res;
char * data = getSPS();
uint32_t len = getSPSLen();
for (int i = 0; i < len; i++){
res << std::hex << std::setw(2) << std::setfill('0') << (int)data[i];
}
return res.str();
}
std::string AVCC::hexPPS(){
std::stringstream res;
char * data = getPPS();
uint32_t len = getPPSLen();
for (int i = 0; i < len; i++){
res << std::hex << std::setw(2) << std::setfill('0') << (int)data[i];
}
return res.str();
}
void AVCC::setPPSNumber(uint32_t newPPSNumber) {
int offset = 8 + getSPSLen();
setInt8(newPPSNumber, offset);
@ -617,9 +637,9 @@ namespace MP4 {
r << std::string(indent + 1, ' ') << "Compatible Profiles: " << getCompatibleProfiles() << std::endl;
r << std::string(indent + 1, ' ') << "Level: " << getLevel() << std::endl;
r << std::string(indent + 1, ' ') << "SPS Number: " << getSPSNumber() << std::endl;
r << std::string(indent + 2, ' ') << getSPSLen() << " of SPS data" << std::endl;
r << std::string(indent + 2, ' ') << getSPSLen() << " of SPS data: " << hexSPS() << std::endl;
r << std::string(indent + 1, ' ') << "PPS Number: " << getPPSNumber() << std::endl;
r << std::string(indent + 2, ' ') << getPPSLen() << " of PPS data" << std::endl;
r << std::string(indent + 2, ' ') << getPPSLen() << " of PPS data: " << hexPPS() << std::endl;
return r.str();
}
@ -634,7 +654,7 @@ namespace MP4 {
void AVCC::setPayload(std::string newPayload) {
if (!reserve(0, payloadSize(), newPayload.size())) {
DEBUG_MSG(DLVL_ERROR, "Cannot allocate enough memory for payload");
ERROR_MSG("Cannot allocate enough memory for payload");
return;
}
memcpy((char *)payload(), (char *)newPayload.c_str(), newPayload.size());
@ -1243,7 +1263,7 @@ namespace MP4 {
return r.str();
}
HDLR::HDLR(std::string & type, std::string name) {
HDLR::HDLR(const std::string & type, const std::string & name) {
memcpy(data + 4, "hdlr", 4);
//reserve an entire box, except for the string part at the end
if (!reserve(0, 8, 32)) {
@ -2336,9 +2356,7 @@ namespace MP4 {
for (unsigned int i = 0; i < getEntryCount(); i++) {
static STTSEntry temp;
temp = getSTTSEntry(i);
r << std::string(indent + 1, ' ') << "Entry[" << i << "]:" << std::endl;
r << std::string(indent + 2, ' ') << "SampleCount: " << temp.sampleCount << std::endl;
r << std::string(indent + 2, ' ') << "SampleDelta: " << temp.sampleDelta << std::endl;
r << std::string(indent + 1, ' ') << "Entry[" << i << "]: " << temp.sampleCount << " sample(s) of " << temp.sampleDelta << "ms each" << std::endl;
}
return r.str();

View file

@ -112,11 +112,13 @@ namespace MP4 {
void setSPS(std::string newSPS);
uint32_t getSPSLen();
char * getSPS();
std::string hexSPS();
void setPPSNumber(uint32_t newPPSNumber);
uint32_t getPPSNumber();
void setPPS(std::string newPPS);
uint32_t getPPSLen();
char * getPPS();
std::string hexPPS();
std::string asAnnexB();
void setPayload(std::string newPayload);
std::string toPrettyString(uint32_t indent = 0);
@ -281,7 +283,7 @@ namespace MP4 {
class HDLR: public Box {
public:
HDLR(std::string & type, std::string name);
HDLR(const std::string & type = "", const std::string & name = "");
void setHandlerType(const char * newHandlerType);
std::string getHandlerType();
void setName(std::string newName);
@ -420,7 +422,7 @@ namespace MP4 {
class TKHD: public fullBox {
public:
TKHD(uint32_t trackId, uint64_t duration, uint32_t width, uint32_t height);
TKHD(uint32_t trackId = 0, uint64_t duration = 0, uint32_t width = 0, uint32_t height = 0);
TKHD(DTSC::Track & track, bool fragmented);
void setCreationTime(uint64_t newCreationTime);
@ -454,7 +456,7 @@ namespace MP4 {
class MDHD: public fullBox {
public:
MDHD(uint64_t duration);
MDHD(uint64_t duration = 0);
void setCreationTime(uint64_t newCreationTime);
uint64_t getCreationTime();
void setModificationTime(uint64_t newModificationTime);