Sanitised mp4 converter code

This commit is contained in:
Oswald Auguste de Bruin 2013-12-03 14:02:13 +01:00
parent 2148e9ce20
commit 559b4cacce

View file

@ -2,10 +2,6 @@
#include <sstream> #include <sstream>
namespace MP4{ namespace MP4{
/*bool keyPartSort(keyPart i, keyPart j){
return (i.time < j.time);
}*/
std::string DTSC2MP4Converter::DTSCMeta2MP4Header(DTSC::Meta & metaData){ std::string DTSC2MP4Converter::DTSCMeta2MP4Header(DTSC::Meta & metaData){
std::stringstream header; std::stringstream header;
//ftyp box //ftyp box
@ -52,257 +48,228 @@ namespace MP4{
//calculate interleaving //calculate interleaving
//putting all metadata in a huge, auto-sorting vector 'keyParts' //putting all metadata in a huge, auto-sorting vector 'keyParts'
//sort by time on keyframes for interleaving
keyParts.clear(); keyParts.clear();
for ( std::map<int,DTSC::Track>::iterator trackIt = metaData.tracks.begin(); trackIt != metaData.tracks.end(); trackIt ++) { for ( std::map<int,DTSC::Track>::iterator trackIt = metaData.tracks.begin(); trackIt != metaData.tracks.end(); trackIt ++) {
if (trackIt->first>0){ if (trackIt->first>0){
std::cerr << "preparing track: " << trackIt->first << std::endl;
int partItNumber = 0; int partItNumber = 0;
for ( std::deque< DTSC::Key>::iterator keyIt = trackIt->second.keys.begin(); keyIt != trackIt->second.keys.end(); keyIt ++) { for ( std::deque< DTSC::Key>::iterator keyIt = trackIt->second.keys.begin(); keyIt != trackIt->second.keys.end(); keyIt ++) {
//if ((*keyIt)->size > 0){ keyPart temp;
keyPart temp; temp.trackID = trackIt->second.trackID;
temp.trackID = trackIt->second.trackID; temp.time = keyIt->getTime();//timeplaats van keyframe
/* temp.len = keyIt->getLength();//duration van keyframe
temp.size = (*keyIt)["size"].asInt(); temp.parts = std::deque<DTSC::Part> (trackIt->second.parts.begin() + partItNumber,trackIt->second.parts.begin() + partItNumber + keyIt->getParts() );//array met bytegrootte van elke aparte part
temp.time = (*keyIt)["time"].asInt(); //calculate total size of parts
temp.len = (*keyIt)["len"].asInt(); int tempSize = 0;
temp.parts = (*keyIt)["parts"].asString(); for (unsigned int di = 0; di < temp.parts.size(); di++){
temp.partsize = (*keyIt)["partsize"].asInt(); tempSize += temp.parts[di].getSize();
*/ }
temp.time = keyIt->getTime();//timeplaats van keyframe temp.size = tempSize;//bytegrootte van keyframe (alle parts bij elkaar)
std::cerr << "time: " << temp.time << std::endl; temp.partsize = keyIt->getParts();//amount of parts in this keyframe
temp.len = keyIt->getLength();//duration van keyframe
//std::cerr << "totalparts, partItNumber, getparts:"<< trackIt->second.parts.size() << ", " << partItNumber << ", " << keyIt->getParts() << std::endl; keyParts.insert(temp);
temp.parts = std::deque<DTSC::Part> (trackIt->second.parts.begin() + partItNumber,trackIt->second.parts.begin() + partItNumber + keyIt->getParts() );//array met bytegrootte van elke aparte part
//calculate total size of parts
int tempSize = 0;
//std::cerr << "keyframe parts: ";
for (unsigned int di = 0; di < temp.parts.size(); di++){
tempSize += temp.parts[di].getSize();
//std::cerr << temp.parts[di].getSize() << " ";
}
//std::cerr << std::endl;
temp.size = tempSize;//bytegrootte van keyframe (alle parts bij elkaar)
temp.partsize = keyIt->getParts();//amount of parts in this keyframe
keyParts.insert(temp);
//}
partItNumber += keyIt->getParts(); partItNumber += keyIt->getParts();
} }
} }
} }
//sort by time on keyframes for interleaving
//std::sort(keyParts.begin(), keyParts.end(), keyPartSort);
//start arbitrary track addition for header //start arbitrary track addition for header
int boxOffset = 1; int boxOffset = 1;
for ( std::map<int,DTSC::Track>::iterator it = metaData.tracks.begin(); it != metaData.tracks.end(); it ++) { for ( std::map<int,DTSC::Track>::iterator it = metaData.tracks.begin(); it != metaData.tracks.end(); it ++) {
if (it->first > 0){ if (it->first > 0){
std::cerr << "track " << it->second.trackID << std::endl; int timescale = 0;
//for (JSON::ObjIter it = metaData["tracks"].ObjBegin(); it != metaData["tracks"].ObjEnd(); it++){ MP4::TRAK trakBox;
int timescale = 0; MP4::TKHD tkhdBox;
MP4::TRAK trakBox; tkhdBox.setVersion(0);
MP4::TKHD tkhdBox; tkhdBox.setFlags(15);
tkhdBox.setVersion(0); tkhdBox.setTrackID(it->second.trackID);
tkhdBox.setFlags(15); ///\TODO duration firstms and lastms fix
tkhdBox.setTrackID(it->second.trackID); tkhdBox.setDuration(it->second.lastms + it->second.firstms);
///\TODO duration firstms and lastms fix
tkhdBox.setDuration(it->second.lastms + it->second.firstms);
if (it->second.type == "video"){
tkhdBox.setWidth(it->second.width << 16);
tkhdBox.setHeight(it->second.height << 16);
tkhdBox.setVolume(0);
}else{
tkhdBox.setVolume(256);
tkhdBox.setAlternateGroup(1);
}
tkhdBox.setMatrix(0x00010000,0);
tkhdBox.setMatrix(0,1);
tkhdBox.setMatrix(0,2);
tkhdBox.setMatrix(0,3);
tkhdBox.setMatrix(0x00010000,4);
tkhdBox.setMatrix(0,5);
tkhdBox.setMatrix(0,6);
tkhdBox.setMatrix(0,7);
tkhdBox.setMatrix(0x40000000,8);
trakBox.setContent(tkhdBox, 0);
MP4::MDIA mdiaBox;
MP4::MDHD mdhdBox(0);/// \todo fix constructor mdhd in lib
mdhdBox.setCreationTime(0);
mdhdBox.setModificationTime(0);
//Calculating media time based on sampledelta. Probably cheating, but it works...
int tmpParts = 0;
for (std::deque< DTSC::Key>::iterator tmpIt = it->second.keys.begin(); tmpIt != it->second.keys.end(); tmpIt ++) {
//for (JSON::ArrIter tmpIt = it->second["keys"].ArrBegin(); tmpIt != it->second["keys"].ArrEnd(); tmpIt++){
tmpParts += tmpIt->getParts();
}
timescale = ((double)(42 * tmpParts) / (it->second.lastms + it->second.firstms)) * 1000;
mdhdBox.setTimeScale(timescale);
///\TODO fix lastms, firstms
mdhdBox.setDuration((it->second.lastms + it->second.firstms) * ((double)timescale / 1000));
mdiaBox.setContent(mdhdBox, 0);
std::string tmpStr = it->second.type; if (it->second.type == "video"){
MP4::HDLR hdlrBox;/// \todo fix constructor hdlr in lib tkhdBox.setWidth(it->second.width << 16);
if (tmpStr == "video"){ tkhdBox.setHeight(it->second.height << 16);
hdlrBox.setHandlerType(0x76696465);//vide tkhdBox.setVolume(0);
}else if (tmpStr == "audio"){ }else{
hdlrBox.setHandlerType(0x736F756E);//soun tkhdBox.setVolume(256);
tkhdBox.setAlternateGroup(1);
} }
hdlrBox.setName(it->second.getIdentifier()); tkhdBox.setMatrix(0x00010000,0);
mdiaBox.setContent(hdlrBox, 1); tkhdBox.setMatrix(0,1);
tkhdBox.setMatrix(0,2);
tkhdBox.setMatrix(0,3);
tkhdBox.setMatrix(0x00010000,4);
tkhdBox.setMatrix(0,5);
tkhdBox.setMatrix(0,6);
tkhdBox.setMatrix(0,7);
tkhdBox.setMatrix(0x40000000,8);
trakBox.setContent(tkhdBox, 0);
MP4::MINF minfBox; MP4::MDIA mdiaBox;
if (tmpStr == "video"){ MP4::MDHD mdhdBox(0);/// \todo fix constructor mdhd in lib
MP4::VMHD vmhdBox; mdhdBox.setCreationTime(0);
vmhdBox.setFlags(1); mdhdBox.setModificationTime(0);
minfBox.setContent(vmhdBox,0); //Calculating media time based on sampledelta. Probably cheating, but it works...
}else if (tmpStr == "audio"){ int tmpParts = 0;
MP4::SMHD smhdBox; for (std::deque< DTSC::Key>::iterator tmpIt = it->second.keys.begin(); tmpIt != it->second.keys.end(); tmpIt ++) {
minfBox.setContent(smhdBox,0); tmpParts += tmpIt->getParts();
} }
MP4::DINF dinfBox; timescale = ((double)(42 * tmpParts) / (it->second.lastms + it->second.firstms)) * 1000;
MP4::DREF drefBox;/// \todo fix constructor dref in lib mdhdBox.setTimeScale(timescale);
drefBox.setVersion(0); ///\TODO fix lastms, firstms
MP4::URL urlBox; mdhdBox.setDuration((it->second.lastms + it->second.firstms) * ((double)timescale / 1000));
urlBox.setFlags(1); mdiaBox.setContent(mdhdBox, 0);
drefBox.setDataEntry(urlBox,0);
dinfBox.setContent(drefBox,0);
minfBox.setContent(dinfBox,1);
MP4::STBL stblBox; std::string tmpStr = it->second.type;
MP4::STSD stsdBox; MP4::HDLR hdlrBox;/// \todo fix constructor hdlr in lib
stsdBox.setVersion(0); if (tmpStr == "video"){
if (tmpStr == "video"){//boxname = codec hdlrBox.setHandlerType(0x76696465);//vide
MP4::VisualSampleEntry vse; }else if (tmpStr == "audio"){
std::string tmpStr2 = it->second.codec; hdlrBox.setHandlerType(0x736F756E);//soun
if (tmpStr2 == "H264"){ }
vse.setCodec("avc1"); hdlrBox.setName(it->second.getIdentifier());
} mdiaBox.setContent(hdlrBox, 1);
vse.setDataReferenceIndex(1);
vse.setWidth(it->second.width); MP4::MINF minfBox;
vse.setHeight(it->second.height); if (tmpStr == "video"){
MP4::AVCC avccBox; MP4::VMHD vmhdBox;
avccBox.setPayload(it->second.init); vmhdBox.setFlags(1);
vse.setCLAP(avccBox); minfBox.setContent(vmhdBox,0);
stsdBox.setEntry(vse,0); }else if (tmpStr == "audio"){
}else if(tmpStr == "audio"){//boxname = codec MP4::SMHD smhdBox;
MP4::AudioSampleEntry ase; minfBox.setContent(smhdBox,0);
std::string tmpStr2 = it->second.codec;
if (tmpStr2 == "AAC"){
ase.setCodec("mp4a");
ase.setDataReferenceIndex(1);
}
ase.setSampleRate(it->second.rate);
ase.setChannelCount(it->second.channels);
ase.setSampleSize(it->second.size);
MP4::ESDS esdsBox;
esdsBox.setESDescriptorTypeLength(32+it->second.init.size());
esdsBox.setESID(2);
esdsBox.setStreamPriority(0);
esdsBox.setDecoderConfigDescriptorTypeLength(18+it->second.init.size());
esdsBox.setByteObjectTypeID(0x40);
esdsBox.setStreamType(5);
esdsBox.setReservedFlag(1);
esdsBox.setBufferSize(1250000);
esdsBox.setMaximumBitRate(10000000);
esdsBox.setAverageBitRate(it->second.bps * 8);
esdsBox.setConfigDescriptorTypeLength(5);
esdsBox.setESHeaderStartCodes(it->second.init);
esdsBox.setSLConfigDescriptorTypeTag(0x6);
esdsBox.setSLConfigExtendedDescriptorTypeTag(0x808080);
esdsBox.setSLDescriptorTypeLength(1);
esdsBox.setSLValue(2);
ase.setCodecBox(esdsBox);
stsdBox.setEntry(ase,0);
}
stblBox.setContent(stsdBox,0);
/// \todo update following stts lines
MP4::STTS sttsBox;//current version probably causes problems
sttsBox.setVersion(0);
MP4::STTSEntry newEntry;
newEntry.sampleCount = tmpParts;
//42, Used as magic number for timescale calculation
newEntry.sampleDelta = 42;
sttsBox.setSTTSEntry(newEntry, 0);
stblBox.setContent(sttsBox,1);
if (it->second.type == "video"){
//STSS Box here
MP4::STSS stssBox;
stssBox.setVersion(0);
int tmpCount = 1;
int tmpItCount = 0;
for ( std::deque< DTSC::Key>::iterator tmpIt = it->second.keys.begin(); tmpIt != it->second.keys.end(); tmpIt ++) {
stssBox.setSampleNumber(tmpCount,tmpItCount);
tmpCount += tmpIt->getParts();
tmpItCount ++;
}
stblBox.setContent(stssBox,2);
} }
MP4::DINF dinfBox;
int offset = (it->second.type == "video"); MP4::DREF drefBox;/// \todo fix constructor dref in lib
drefBox.setVersion(0);
MP4::URL urlBox;
urlBox.setFlags(1);
drefBox.setDataEntry(urlBox,0);
dinfBox.setContent(drefBox,0);
minfBox.setContent(dinfBox,1);
MP4::STSC stscBox; MP4::STBL stblBox;
stscBox.setVersion(0); MP4::STSD stsdBox;
uint32_t total = 0; stsdBox.setVersion(0);
MP4::STSCEntry stscEntry; if (tmpStr == "video"){//boxname = codec
stscEntry.firstChunk = 1; MP4::VisualSampleEntry vse;
stscEntry.samplesPerChunk = 1; std::string tmpStr2 = it->second.codec;
stscEntry.sampleDescriptionIndex = 1; if (tmpStr2 == "H264"){
stscBox.setSTSCEntry(stscEntry, 0); vse.setCodec("avc1");
stblBox.setContent(stscBox,2 + offset); }
vse.setDataReferenceIndex(1);
MP4::STSZ stszBox; vse.setWidth(it->second.width);
stszBox.setVersion(0); vse.setHeight(it->second.height);
total = 0; MP4::AVCC avccBox;
for (std::deque< DTSC::Part>::iterator partIt = it->second.parts.begin(); partIt != it->second.parts.end(); partIt ++) { avccBox.setPayload(it->second.init);
//for (int i = 0; i < it->second["keys"].size(); i++){ vse.setCLAP(avccBox);
//std::deque<long long unsigned int> parsedParts; stsdBox.setEntry(vse,0);
//JSON::decodeVector(it->second["keys"][i]["parts"].asString(), parsedParts); }else if(tmpStr == "audio"){//boxname = codec
//for (unsigned int o = 0; o < tmpIt->parts.size(); o++){ MP4::AudioSampleEntry ase;
stszBox.setEntrySize(partIt->getSize(), total);//in bytes in file std::string tmpStr2 = it->second.codec;
total++; if (tmpStr2 == "AAC"){
} ase.setCodec("mp4a");
stblBox.setContent(stszBox,3 + offset); ase.setDataReferenceIndex(1);
}
ase.setSampleRate(it->second.rate);
ase.setChannelCount(it->second.channels);
ase.setSampleSize(it->second.size);
MP4::ESDS esdsBox;
esdsBox.setESDescriptorTypeLength(32+it->second.init.size());
esdsBox.setESID(2);
esdsBox.setStreamPriority(0);
esdsBox.setDecoderConfigDescriptorTypeLength(18+it->second.init.size());
esdsBox.setByteObjectTypeID(0x40);
esdsBox.setStreamType(5);
esdsBox.setReservedFlag(1);
esdsBox.setBufferSize(1250000);
esdsBox.setMaximumBitRate(10000000);
esdsBox.setAverageBitRate(it->second.bps * 8);
esdsBox.setConfigDescriptorTypeLength(5);
esdsBox.setESHeaderStartCodes(it->second.init);
esdsBox.setSLConfigDescriptorTypeTag(0x6);
esdsBox.setSLConfigExtendedDescriptorTypeTag(0x808080);
esdsBox.setSLDescriptorTypeLength(1);
esdsBox.setSLValue(2);
ase.setCodecBox(esdsBox);
stsdBox.setEntry(ase,0);
}
stblBox.setContent(stsdBox,0);
MP4::STCO stcoBox; /// \TODO update following stts lines
stcoBox.setVersion(1); MP4::STTS sttsBox;//current version probably causes problems
total = 0; sttsBox.setVersion(0);
long long unsigned int totalByteOffset = 0; MP4::STTSEntry newEntry;
//Inserting wrong values on purpose here, will be fixed later. newEntry.sampleCount = tmpParts;
//Current values are actual byte offset without header-sized offset //42, Used as magic number for timescale calculation
std::cerr << "pre-totalByteOffset: " << totalByteOffset << std::endl; newEntry.sampleDelta = 42;
for (std::set<keyPart>::iterator i = keyParts.begin(); i != keyParts.end(); i++){//for all keypart size sttsBox.setSTTSEntry(newEntry, 0);
if(i->trackID == it->second.trackID){//if keypart is of current trackID stblBox.setContent(sttsBox,1);
//std::deque<long long unsigned int> parsedParts;
//JSON::decodeVector(i->parts, parsedParts); if (it->second.type == "video"){
std::deque<DTSC::Part> onowai = i->parts; //STSS Box here
for (unsigned int o = 0; o < onowai.size(); o++){//add all parts to STCO MP4::STSS stssBox;
//for (std::deque<DTSC::Part>::iterator partIt = (*i).parts.begin(); partIt != (*i).parts.end(); partIt++){ stssBox.setVersion(0);
stcoBox.setChunkOffset(totalByteOffset, total); int tmpCount = 1;
total++; int tmpItCount = 0;
totalByteOffset += onowai[o].getSize(); for ( std::deque< DTSC::Key>::iterator tmpIt = it->second.keys.begin(); tmpIt != it->second.keys.end(); tmpIt ++) {
std::cerr << "small totalByteOffset: " << totalByteOffset << std::endl; stssBox.setSampleNumber(tmpCount,tmpItCount);
} tmpCount += tmpIt->getParts();
}else{ tmpItCount ++;
totalByteOffset += i->size; }
std::cerr << "large totalByteOffset: " << totalByteOffset << std::endl; stblBox.setContent(stssBox,2);
} }
}
//calculating the offset where the STCO box will be in the main MOOV box int offset = (it->second.type == "video");
//needed for probable optimise
mdatSize = totalByteOffset;
MP4::STSC stscBox;
stblBox.setContent(stcoBox,4 + offset); stscBox.setVersion(0);
minfBox.setContent(stblBox,2); uint32_t total = 0;
mdiaBox.setContent(minfBox, 2); MP4::STSCEntry stscEntry;
trakBox.setContent(mdiaBox, 1); stscEntry.firstChunk = 1;
moovBox.setContent(trakBox, boxOffset); stscEntry.samplesPerChunk = 1;
boxOffset++; stscEntry.sampleDescriptionIndex = 1;
stscBox.setSTSCEntry(stscEntry, 0);
stblBox.setContent(stscBox,2 + offset);
MP4::STSZ stszBox;
stszBox.setVersion(0);
total = 0;
for (std::deque< DTSC::Part>::iterator partIt = it->second.parts.begin(); partIt != it->second.parts.end(); partIt ++) {
stszBox.setEntrySize(partIt->getSize(), total);//in bytes in file
total++;
}
stblBox.setContent(stszBox,3 + offset);
MP4::STCO stcoBox;
stcoBox.setVersion(1);
total = 0;
long long unsigned int totalByteOffset = 0;
//Inserting wrong values on purpose here, will be fixed later.
//Current values are actual byte offset without header-sized offset
for (std::set<keyPart>::iterator i = keyParts.begin(); i != keyParts.end(); i++){//for all keypart size
if(i->trackID == it->second.trackID){//if keypart is of current trackID
std::deque<DTSC::Part> tempArr = i->parts;
for (unsigned int o = 0; o < tempArr.size(); o++){//add all parts to STCO
stcoBox.setChunkOffset(totalByteOffset, total);
total++;
totalByteOffset += tempArr[o].getSize();
}
}else{
totalByteOffset += i->size;
}
}
//calculating the offset where the STCO box will be in the main MOOV box
//needed for probable optimise
mdatSize = totalByteOffset;
stblBox.setContent(stcoBox,4 + offset);
minfBox.setContent(stblBox,2);
mdiaBox.setContent(minfBox, 2);
trakBox.setContent(mdiaBox, 1);
moovBox.setContent(trakBox, boxOffset);
boxOffset++;
} }
} }
//end arbitrary //end arbitrary
@ -363,9 +330,7 @@ namespace MP4{
//while there are requested packets in the trackBuffer:... //while there are requested packets in the trackBuffer:...
while (!trackBuffer[curKey->trackID].empty()){ while (!trackBuffer[curKey->trackID].empty()){
//output requested packages //output requested packages
//std::deque<DTSC::Part> onowai = curKey->parts;
stringBuffer += trackBuffer[curKey->trackID].front()["data"].asString(); stringBuffer += trackBuffer[curKey->trackID].front()["data"].asString();
//std::cerr << "bufDataSize, antDataSize" << trackBuffer[curKey->trackID].front()["data"].asString().size() << ", " << onowai[curPart].getSize() << std::endl;
trackBuffer[curKey->trackID].pop_front(); trackBuffer[curKey->trackID].pop_front();
curPart++; curPart++;
if(curPart >= curKey->parts.size()){ if(curPart >= curKey->parts.size()){
@ -377,8 +342,6 @@ namespace MP4{
if(curKey->trackID == mediaPart["trackid"].asInt()){ if(curKey->trackID == mediaPart["trackid"].asInt()){
//output JSON packet //output JSON packet
stringBuffer += mediaPart["data"].asStringRef(); stringBuffer += mediaPart["data"].asStringRef();
//std::deque<DTSC::Part> onowai = curKey->parts;
//std::cerr << "dataSize, antDataSize" << mediaPart["data"].asStringRef().size() << ", " << onowai[curPart].getSize() << std::endl;
curPart++; curPart++;
if(curPart >= curKey->parts.size()){ if(curPart >= curKey->parts.size()){
curPart = 0; curPart = 0;