Significant speedup in MPEG2 header parser

This commit is contained in:
Thulinma 2017-12-13 14:22:53 +01:00
parent 1467fda3ec
commit 5b8429d7ee

View file

@ -3,6 +3,7 @@
namespace Mpeg{
MP2Info parseMP2Header(const std::string &hdr){return parseMP2Header(hdr.c_str());}
MP2Info parseMP2Header(const char *hdr){
MP2Info res;
// mpeg version is on the bits 0x18 of header[1], but only 0x08 is important --> 0 is version 2,
@ -25,11 +26,13 @@ namespace Mpeg{
}
MPEG2Info parseMPEG2Header(const std::string &hdr){return parseMPEG2Header(hdr.data());}
MPEG2Info parseMPEG2Header(const char *hdr){
MPEG2Info res;
parseMPEG2Header(hdr, res);
return res;
}
bool parseMPEG2Header(const char *hdr, MPEG2Info &mpInfo){
// Check for start code
if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1){return false;}
@ -48,16 +51,33 @@ namespace Mpeg{
// Not parsed
return false;
}
void parseMPEG2Headers(const char *hdr, uint32_t len, MPEG2Info &mpInfo){
mpInfo.clear();
for (uint32_t i = 0; i < len-5; ++i){
parseMPEG2Header(hdr+i, mpInfo);
const char *offset = hdr;
char *maxData = (char*)hdr + len - 5;
if (maxData - hdr > 250){maxData = (char*)hdr + 250;}
while (offset < maxData){
if (offset[2] > 1){
// We have no zero in the third byte, so we need to skip at least 3 bytes forward
offset += 3;
continue;
}
if (!offset[2]){
// We skip forward 1 or 2 bytes depending on contents of the second byte
offset += (offset[1] ? 2 : 1);
continue;
}
if (!offset[0] && !offset[1]){parseMPEG2Header(offset, mpInfo);}
// We have no zero in the third byte, so we need to skip at least 3 bytes forward
offset += 3;
}
}
MPEG2Info parseMPEG2Headers(const char *hdr, uint32_t len){
MPEG2Info res;
parseMPEG2Headers(hdr, len, res);
return res;
}
}
}// namespace Mpeg