VIDEO: Initial implementation of SRT parser

This commit is contained in:
Eugene Sandulenko 2018-05-20 14:23:33 +02:00
parent d35b69d48b
commit b45599fdb2
3 changed files with 231 additions and 0 deletions

View File

@ -13,6 +13,7 @@ MODULE_OBJS := \
psx_decoder.o \
qt_decoder.o \
smk_decoder.o \
srt_parser.o \
video_decoder.o
ifdef USE_BINK

175
video/srt_parser.cpp Normal file
View File

@ -0,0 +1,175 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/file.h"
#include "video/srt_parser.h"
namespace Video {
SRTParser::SRTParser() {
}
SRTParser::~SRTParser() {
}
bool parseTime(const char **pptr, uint32 *res) {
int hours, mins, secs, msecs;
const char *ptr = *pptr;
hours = (*ptr++ - '0') * 10;
hours += *ptr++ - '0';
if (hours > 24 || hours < 0)
return false;
if (*ptr++ != ':')
return false;
mins = (*ptr++ - '0') * 10;
mins += *ptr++ - '0';
if (mins > 60 || mins < 0)
return false;
if (*ptr++ != ':')
return false;
secs = (*ptr++ - '0') * 10;
secs += *ptr++ - '0';
if (secs > 60 || secs < 0)
return false;
if (*ptr++ != ',')
return false;
msecs = (*ptr++ - '0') * 100;
msecs += (*ptr++ - '0') * 10;
msecs += *ptr++ - '0';
if (msecs > 1000 || msecs < 0)
return false;
*res = 1000 * (3600 * hours + 60 * mins + secs) + msecs;
*pptr = ptr;
return true;
}
bool SRTParser::parseFile(const char *fname) {
Common::File f;
if (!f.open(fname))
return false;
byte buf[3];
f.read(buf, 3);
int line = 0;
// Skip UTF header if present
if (buf[0] != 0xef || buf[1] != 0xbb || buf[2] != 0xbf)
f.seek(0);
while (!f.eos()) {
Common::String sseq, stimespec, stmp, text;
sseq = f.readLine(); line++;
stimespec = f.readLine(); line++;
text = f.readLine(); line++;
if (sseq.empty()) {
if (f.eos()) {
// Normal end of stream
break;
} else {
warning("Bad SRT file format (spec): %s at line %d", fname, line);
break;
}
}
if (stimespec.empty() || text.empty()) {
warning("Bad SRT file format (spec): %s at line %d", fname, line);
break;
}
// Read all multiline text
while (!f.eos()) {
stmp = f.readLine(); line++;
if (!stmp.empty()) {
text += '\n';
text += stmp;
} else {
break;
}
}
uint32 seq = atol(sseq.c_str());
if (seq == 0) {
warning("Bad SRT file format (seq): %s at line %d", fname, line);
break;
}
// 00:20:41,150 --> 00:20:45,109
if (stimespec.size() < 29) {
warning("Bad SRT file format (timespec length %d): %s at line %d", stimespec.size(), fname, line);
break;
}
const char *ptr = stimespec.c_str();
uint32 start, end;
if (!parseTime(&ptr, &start)) {
warning("Bad SRT file format (timespec start): %s at line %d", fname, line);
break;
}
while (*ptr == ' ')
ptr++;
while (*ptr == '-')
ptr++;
if (*ptr != '>') {
warning("Bad SRT file format (timespec middle ('%c')): %s at line %d", *ptr, fname, line);
break;
}
ptr++;
while (*ptr == ' ')
ptr++;
if (!parseTime(&ptr, &end)) {
warning("Bad SRT file format (timespec end): %s at line %d", fname, line);
break;
}
_entries.push_back(SRTEntry(seq, start, end, text));
}
return true;
}
} // End of namespace Video

55
video/srt_parser.h Normal file
View File

@ -0,0 +1,55 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef SRT_PARSER_H
#define SRT_PARSER_H
#include "common/str.h"
#include "common/array.h"
namespace Video {
struct SRTEntry {
uint seq;
uint32 start;
uint32 end;
Common::String text;
SRTEntry(uint seq_, uint32 start_, uint32 end_, Common::String text_) {
seq = seq_; start = start_; end = end_; text = text_;
}
};
class SRTParser {
public:
SRTParser();
~SRTParser();
bool parseFile(const char *fname);
private:
Common::Array<SRTEntry> _entries;
};
} // End of namespace Video
#endif