Significant speedup in MPEG2 header parser
This commit is contained in:
parent
1467fda3ec
commit
5b8429d7ee
1 changed files with 23 additions and 3 deletions
26
lib/mpeg.cpp
26
lib/mpeg.cpp
|
@ -3,6 +3,7 @@
|
||||||
namespace Mpeg{
|
namespace Mpeg{
|
||||||
|
|
||||||
MP2Info parseMP2Header(const std::string &hdr){return parseMP2Header(hdr.c_str());}
|
MP2Info parseMP2Header(const std::string &hdr){return parseMP2Header(hdr.c_str());}
|
||||||
|
|
||||||
MP2Info parseMP2Header(const char *hdr){
|
MP2Info parseMP2Header(const char *hdr){
|
||||||
MP2Info res;
|
MP2Info res;
|
||||||
// mpeg version is on the bits 0x18 of header[1], but only 0x08 is important --> 0 is version 2,
|
// 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 std::string &hdr){return parseMPEG2Header(hdr.data());}
|
||||||
|
|
||||||
MPEG2Info parseMPEG2Header(const char *hdr){
|
MPEG2Info parseMPEG2Header(const char *hdr){
|
||||||
MPEG2Info res;
|
MPEG2Info res;
|
||||||
parseMPEG2Header(hdr, res);
|
parseMPEG2Header(hdr, res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool parseMPEG2Header(const char *hdr, MPEG2Info &mpInfo){
|
bool parseMPEG2Header(const char *hdr, MPEG2Info &mpInfo){
|
||||||
// Check for start code
|
// Check for start code
|
||||||
if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1){return false;}
|
if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1){return false;}
|
||||||
|
@ -48,16 +51,33 @@ namespace Mpeg{
|
||||||
// Not parsed
|
// Not parsed
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void parseMPEG2Headers(const char *hdr, uint32_t len, MPEG2Info &mpInfo){
|
void parseMPEG2Headers(const char *hdr, uint32_t len, MPEG2Info &mpInfo){
|
||||||
mpInfo.clear();
|
mpInfo.clear();
|
||||||
for (uint32_t i = 0; i < len-5; ++i){
|
const char *offset = hdr;
|
||||||
parseMPEG2Header(hdr+i, mpInfo);
|
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 parseMPEG2Headers(const char *hdr, uint32_t len){
|
||||||
MPEG2Info res;
|
MPEG2Info res;
|
||||||
parseMPEG2Headers(hdr, len, res);
|
parseMPEG2Headers(hdr, len, res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}// namespace Mpeg
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue