MP4 output can now almost be considered fast. Almost.

This commit is contained in:
Thulinma 2014-12-13 16:41:12 +01:00
parent fac1727e10
commit 2e48687e8e

View file

@ -27,7 +27,7 @@ namespace Mist {
std::stringstream header; std::stringstream header;
//ftyp box //ftyp box
MP4::FTYP ftypBox; MP4::FTYP ftypBox;
header << std::string(ftypBox.asBox(),ftypBox.boxedSize()); header.write(ftypBox.asBox(),ftypBox.boxedSize());
uint64_t mdatSize = 0; uint64_t mdatSize = 0;
//moov box //moov box
@ -49,30 +49,31 @@ namespace Mist {
moovBox.setContent(mvhdBox, moovOffset++); moovBox.setContent(mvhdBox, moovOffset++);
} }
for (std::set<long unsigned int>::iterator it = selectedTracks.begin(); it != selectedTracks.end(); it++) { for (std::set<long unsigned int>::iterator it = selectedTracks.begin(); it != selectedTracks.end(); it++) {
DTSC::Track & thisTrack = myMeta.tracks[*it];
MP4::TRAK trakBox; MP4::TRAK trakBox;
{ {
{ {
MP4::TKHD tkhdBox(*it, myMeta.tracks[*it].lastms - myMeta.tracks[*it].firstms, myMeta.tracks[*it].width, myMeta.tracks[*it].height); MP4::TKHD tkhdBox(*it, thisTrack.lastms - thisTrack.firstms, thisTrack.width, thisTrack.height);
trakBox.setContent(tkhdBox, 0); trakBox.setContent(tkhdBox, 0);
}{ }{
MP4::MDIA mdiaBox; MP4::MDIA mdiaBox;
unsigned int mdiaOffset = 0; unsigned int mdiaOffset = 0;
{ {
MP4::MDHD mdhdBox(myMeta.tracks[*it].lastms - myMeta.tracks[*it].firstms); MP4::MDHD mdhdBox(thisTrack.lastms - thisTrack.firstms);
mdiaBox.setContent(mdhdBox, mdiaOffset++); mdiaBox.setContent(mdhdBox, mdiaOffset++);
}//MDHD box }//MDHD box
{ {
MP4::HDLR hdlrBox(myMeta.tracks[*it].type, myMeta.tracks[*it].getIdentifier()); MP4::HDLR hdlrBox(thisTrack.type, thisTrack.getIdentifier());
mdiaBox.setContent(hdlrBox, mdiaOffset++); mdiaBox.setContent(hdlrBox, mdiaOffset++);
}//hdlr box }//hdlr box
{ {
MP4::MINF minfBox; MP4::MINF minfBox;
unsigned int minfOffset = 0; unsigned int minfOffset = 0;
if (myMeta.tracks[*it].type== "video"){ if (thisTrack.type== "video"){
MP4::VMHD vmhdBox; MP4::VMHD vmhdBox;
vmhdBox.setFlags(1); vmhdBox.setFlags(1);
minfBox.setContent(vmhdBox,minfOffset++); minfBox.setContent(vmhdBox,minfOffset++);
}else if (myMeta.tracks[*it].type == "audio"){ }else if (thisTrack.type == "audio"){
MP4::SMHD smhdBox; MP4::SMHD smhdBox;
minfBox.setContent(smhdBox,minfOffset++); minfBox.setContent(smhdBox,minfOffset++);
}//type box }//type box
@ -88,35 +89,40 @@ namespace Mist {
{ {
MP4::STSD stsdBox; MP4::STSD stsdBox;
stsdBox.setVersion(0); stsdBox.setVersion(0);
if (myMeta.tracks[*it].type == "video"){//boxname = codec if (thisTrack.type == "video"){//boxname = codec
MP4::VisualSampleEntry vse; MP4::VisualSampleEntry vse;
if (myMeta.tracks[*it].codec == "H264"){ if (thisTrack.codec == "H264"){
vse.setCodec("avc1"); vse.setCodec("avc1");
} }
if (thisTrack.codec == "HEVC"){
vse.setCodec("hev1");
}
vse.setDataReferenceIndex(1); vse.setDataReferenceIndex(1);
vse.setWidth(myMeta.tracks[*it].width); vse.setWidth(thisTrack.width);
vse.setHeight(myMeta.tracks[*it].height); vse.setHeight(thisTrack.height);
MP4::AVCC avccBox; if (thisTrack.codec == "H264"){
avccBox.setPayload(myMeta.tracks[*it].init); MP4::AVCC avccBox;
vse.setCLAP(avccBox); avccBox.setPayload(thisTrack.init);
vse.setCLAP(avccBox);
}
stsdBox.setEntry(vse,0); stsdBox.setEntry(vse,0);
}else if(myMeta.tracks[*it].type == "audio"){//boxname = codec }else if(thisTrack.type == "audio"){//boxname = codec
MP4::AudioSampleEntry ase; MP4::AudioSampleEntry ase;
if (myMeta.tracks[*it].codec == "AAC"){ if (thisTrack.codec == "AAC"){
ase.setCodec("mp4a"); ase.setCodec("mp4a");
ase.setDataReferenceIndex(1); ase.setDataReferenceIndex(1);
}else if (myMeta.tracks[*it].codec == "MP3"){ }else if (thisTrack.codec == "MP3"){
ase.setCodec("mp4a"); ase.setCodec("mp4a");
ase.setDataReferenceIndex(1); ase.setDataReferenceIndex(1);
} }
ase.setSampleRate(myMeta.tracks[*it].rate); ase.setSampleRate(thisTrack.rate);
ase.setChannelCount(myMeta.tracks[*it].channels); ase.setChannelCount(thisTrack.channels);
ase.setSampleSize(myMeta.tracks[*it].size); ase.setSampleSize(thisTrack.size);
//MP4::ESDS esdsBox(myMeta.tracks[*it].init, myMeta.tracks[*it].bps); //MP4::ESDS esdsBox(thisTrack.init, thisTrack.bps);
MP4::ESDS esdsBox; MP4::ESDS esdsBox;
//outputting these values first, so malloc isn't called as often. //outputting these values first, so malloc isn't called as often.
if (myMeta.tracks[*it].codec == "MP3"){ if (thisTrack.codec == "MP3"){
esdsBox.setESHeaderStartCodes("\002"); esdsBox.setESHeaderStartCodes("\002");
esdsBox.setConfigDescriptorTypeLength(1); esdsBox.setConfigDescriptorTypeLength(1);
esdsBox.setSLConfigExtendedDescriptorTypeTag(0); esdsBox.setSLConfigExtendedDescriptorTypeTag(0);
@ -130,14 +136,14 @@ namespace Mist {
esdsBox.setByteObjectTypeID(0x6b); esdsBox.setByteObjectTypeID(0x6b);
}else{ }else{
//AAC //AAC
esdsBox.setESHeaderStartCodes(myMeta.tracks[*it].init); esdsBox.setESHeaderStartCodes(thisTrack.init);
esdsBox.setConfigDescriptorTypeLength(myMeta.tracks[*it].init.size()); esdsBox.setConfigDescriptorTypeLength(thisTrack.init.size());
esdsBox.setSLConfigExtendedDescriptorTypeTag(0x808080); esdsBox.setSLConfigExtendedDescriptorTypeTag(0x808080);
esdsBox.setSLDescriptorTypeLength(1); esdsBox.setSLDescriptorTypeLength(1);
esdsBox.setESDescriptorTypeLength(32+myMeta.tracks[*it].init.size()); esdsBox.setESDescriptorTypeLength(32+thisTrack.init.size());
esdsBox.setSLConfigDescriptorTypeTag(0x6); esdsBox.setSLConfigDescriptorTypeTag(0x6);
esdsBox.setSLValue(2); esdsBox.setSLValue(2);
esdsBox.setDecoderConfigDescriptorTypeLength(18 + myMeta.tracks[*it].init.size()); esdsBox.setDecoderConfigDescriptorTypeLength(18 + thisTrack.init.size());
esdsBox.setByteObjectTypeID(0x40); esdsBox.setByteObjectTypeID(0x40);
} }
esdsBox.setESID(2); esdsBox.setESID(2);
@ -145,7 +151,7 @@ namespace Mist {
esdsBox.setStreamType(5); esdsBox.setStreamType(5);
esdsBox.setReservedFlag(1); esdsBox.setReservedFlag(1);
esdsBox.setMaximumBitRate(10000000); esdsBox.setMaximumBitRate(10000000);
esdsBox.setAverageBitRate(myMeta.tracks[*it].bps * 8); esdsBox.setAverageBitRate(thisTrack.bps * 8);
esdsBox.setBufferSize(1250000); esdsBox.setBufferSize(1250000);
ase.setCodecBox(esdsBox); ase.setCodecBox(esdsBox);
stsdBox.setEntry(ase,0); stsdBox.setEntry(ase,0);
@ -155,23 +161,23 @@ namespace Mist {
{ {
MP4::STTS sttsBox; MP4::STTS sttsBox;
sttsBox.setVersion(0); sttsBox.setVersion(0);
if (myMeta.tracks[*it].parts.size()){ if (thisTrack.parts.size()){
for (unsigned int part = 0; part < myMeta.tracks[*it].parts.size(); part++){ for (unsigned int part = thisTrack.parts.size(); part > 0; --part){
MP4::STTSEntry newEntry; MP4::STTSEntry newEntry;
newEntry.sampleCount = 1; newEntry.sampleCount = 1;
newEntry.sampleDelta = myMeta.tracks[*it].parts[part].getDuration(); newEntry.sampleDelta = thisTrack.parts[part-1].getDuration();
sttsBox.setSTTSEntry(newEntry, part); sttsBox.setSTTSEntry(newEntry, part-1);
} }
} }
stblBox.setContent(sttsBox,offset++); stblBox.setContent(sttsBox,offset++);
}//stts box }//stts box
if (myMeta.tracks[*it].type == "video"){ if (thisTrack.type == "video"){
//STSS Box here //STSS Box here
MP4::STSS stssBox; MP4::STSS stssBox;
stssBox.setVersion(0); stssBox.setVersion(0);
int tmpCount = 0; int tmpCount = 0;
int tmpItCount = 0; int tmpItCount = 0;
for ( std::deque< DTSC::Key>::iterator tmpIt = myMeta.tracks[*it].keys.begin(); tmpIt != myMeta.tracks[*it].keys.end(); tmpIt ++) { for ( std::deque< DTSC::Key>::iterator tmpIt = thisTrack.keys.begin(); tmpIt != thisTrack.keys.end(); tmpIt ++) {
stssBox.setSampleNumber(tmpCount,tmpItCount); stssBox.setSampleNumber(tmpCount,tmpItCount);
tmpCount += tmpIt->getParts(); tmpCount += tmpIt->getParts();
tmpItCount ++; tmpItCount ++;
@ -189,24 +195,25 @@ namespace Mist {
stblBox.setContent(stscBox,offset++); stblBox.setContent(stscBox,offset++);
}//stsc box }//stsc box
{ {
uint32_t total = 0;
MP4::STSZ stszBox; MP4::STSZ stszBox;
stszBox.setVersion(0); stszBox.setVersion(0);
total = 0; if (thisTrack.parts.size()){
for (std::deque< DTSC::Part>::iterator partIt = myMeta.tracks[*it].parts.begin(); partIt != myMeta.tracks[*it].parts.end(); partIt ++) { std::deque<DTSC::Part>::reverse_iterator tmpIt = thisTrack.parts.rbegin();
stszBox.setEntrySize(partIt->getSize(), total);//in bytes in file for (unsigned int part = thisTrack.parts.size(); part > 0; --part){
size += partIt->getSize(); unsigned int partSize = tmpIt->getSize();
total++; tmpIt++;
stszBox.setEntrySize(partSize, part-1);//in bytes in file
size += partSize;
}
} }
stblBox.setContent(stszBox,offset++); stblBox.setContent(stszBox,offset++);
}//stsz box }//stsz box
//add STCO boxes here
{ {
MP4::STCO stcoBox; MP4::STCO stcoBox;
stcoBox.setVersion(1); stcoBox.setVersion(1);
//Inserting empty values on purpose here, will be fixed later. //Inserting empty values on purpose here, will be fixed later.
if (myMeta.tracks[*it].parts.size() != 0){ if (thisTrack.parts.size() != 0){
stcoBox.setChunkOffset(0, myMeta.tracks[*it].parts.size() - 1);//this inserts all empty entries at once stcoBox.setChunkOffset(0, thisTrack.parts.size() - 1);//this inserts all empty entries at once
} }
stblBox.setContent(stcoBox,offset++); stblBox.setContent(stcoBox,offset++);
}//stco box }//stco box
@ -218,7 +225,7 @@ namespace Mist {
} }
}//trak Box }//trak Box
moovBox.setContent(trakBox, moovOffset++); moovBox.setContent(trakBox, moovOffset++);
} }//for each selected track
//initial offset length ftyp, length moov + 8 //initial offset length ftyp, length moov + 8
unsigned long long int byteOffset = ftypBox.boxedSize() + moovBox.boxedSize() + 8; unsigned long long int byteOffset = ftypBox.boxedSize() + moovBox.boxedSize() + 8;
//update all STCO from the following map; //update all STCO from the following map;
@ -276,26 +283,28 @@ namespace Mist {
sortSet.insert(temp); sortSet.insert(temp);
} }
while (!sortSet.empty()){ while (!sortSet.empty()){
std::set<keyPart>::iterator keyBegin = sortSet.begin();
//setting the right STCO size in the STCO box //setting the right STCO size in the STCO box
checkStcoBoxes[sortSet.begin()->trackID].setChunkOffset(totalByteOffset + byteOffset, sortSet.begin()->index); checkStcoBoxes[keyBegin->trackID].setChunkOffset(totalByteOffset + byteOffset, keyBegin->index);
totalByteOffset += sortSet.begin()->size; totalByteOffset += keyBegin->size;
//add keyPart to sortSet //add keyPart to sortSet
keyPart temp; keyPart temp;
temp.index = sortSet.begin()->index + 1; temp.index = keyBegin->index + 1;
temp.trackID = sortSet.begin()->trackID; temp.trackID = keyBegin->trackID;
if(temp.index < myMeta.tracks[temp.trackID].parts.size() ){//only insert when there are parts left DTSC::Track & thisTrack = myMeta.tracks[temp.trackID];
temp.time = sortSet.begin()->endTime;//timeplace of frame if(temp.index < thisTrack.parts.size() ){//only insert when there are parts left
temp.endTime = sortSet.begin()->endTime + myMeta.tracks[temp.trackID].parts[temp.index].getDuration(); temp.time = keyBegin->endTime;//timeplace of frame
temp.size = myMeta.tracks[temp.trackID].parts[temp.index].getSize();//bytesize of frame temp.endTime = keyBegin->endTime + thisTrack.parts[temp.index].getDuration();
temp.size = thisTrack.parts[temp.index].getSize();//bytesize of frame
sortSet.insert(temp); sortSet.insert(temp);
} }
//remove highest keyPart //remove highest keyPart
sortSet.erase(sortSet.begin()); sortSet.erase(keyBegin);
} }
mdatSize = totalByteOffset+8; mdatSize = totalByteOffset+8;
header << std::string(moovBox.asBox(),moovBox.boxedSize()); header.write(moovBox.asBox(),moovBox.boxedSize());
header << (char)((mdatSize>>24) & 0xFF) << (char)((mdatSize>>16) & 0xFF) << (char)((mdatSize>>8) & 0xFF) << (char)(mdatSize & 0xFF) << "mdat"; header << (char)((mdatSize>>24) & 0xFF) << (char)((mdatSize>>16) & 0xFF) << (char)((mdatSize>>8) & 0xFF) << (char)(mdatSize & 0xFF) << "mdat";
//end of header //end of header