forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subtitle.cpp
560 lines (522 loc) · 14.9 KB
/
Subtitle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2014 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/Subtitle.h"
#include "QtAV/private/SubtitleProcessor.h"
#include <algorithm>
#include <QtDebug>
#include <QtCore/QBuffer>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QIODevice>
#include <QtCore/QLinkedList>
#include <QtCore/QRegExp>
#include <QtCore/QRunnable>
#include <QtCore/QThreadPool>
#include <QtCore/QTextCodec>
#include <QtCore/QTextStream>
#include <QtCore/QMutexLocker>
namespace QtAV {
const int kMaxSubtitleSize = 10 * 1024 * 1024;
class Subtitle::Private {
public:
Private()
: loaded(false)
, fuzzy_match(true)
, update_image(true)
, processor(0)
, t(0)
{}
void reset() {
QMutexLocker lock(&mutex);
Q_UNUSED(lock);
loaded = false;
processor = 0;
update_image = true;
t = 0;
frame = SubtitleFrame();
frames.clear();
itf = frames.begin();
}
// width/height == 0: do not create image
// return true if both frame time and content(currently is text) changed
bool prepareCurrentFrame();
QStringList find();
/*!
* \brief readFromFile
* read subtilte content from path
* \param path
* \return utf8 encoded content
*/
QByteArray readFromFile(const QString& path);
/*!
* \brief processRawData
* process utf8 encoded subtitle content. maybe a temp file is created if subtitle processor does not
* support raw data
* \param data utf8 subtitle content
*/
bool processRawData(const QByteArray& data);
bool processRawData(SubtitleProcessor* sp, const QByteArray& data);
bool loaded;
bool fuzzy_match;
bool update_image;
SubtitleProcessor *processor;
QList<SubtitleProcessor*> processors;
QByteArray codec;
QStringList engine_names;
QLinkedList<SubtitleFrame> frames;
QUrl url;
QByteArray raw_data;
QString file_name;
QStringList suffixes;
QStringList supported_suffixes;
QIODevice *dev;
// last time image
qreal t;
SubtitleFrame frame;
QLinkedList<SubtitleFrame>::iterator itf;
QMutex mutex;
};
Subtitle::Subtitle(QObject *parent) :
QObject(parent)
, priv(new Private())
{
// TODO: use factory.registedNames() and the order
setEngines(QStringList() << "FFmpeg" << "LibAss");
}
Subtitle::~Subtitle()
{}
bool Subtitle::isLoaded() const
{
return priv->loaded;
}
void Subtitle::setCodec(const QByteArray &value)
{
if (priv->codec == value)
return;
priv->codec = value;
emit codecChanged();
}
QByteArray Subtitle::codec() const
{
return priv->codec;
}
void Subtitle::setEngines(const QStringList &value)
{
if (priv->engine_names == value)
return;
priv->processor = 0;
priv->supported_suffixes.clear();
priv->engine_names = value;
emit enginesChanged();
QList<SubtitleProcessor*> sps;
if (priv->engine_names.isEmpty()) {
return;
}
foreach (QString e, priv->engine_names) {
QList<SubtitleProcessor*>::iterator it = priv->processors.begin();
while (it != priv->processors.end()) {
if (!(*it)) {
it = priv->processors.erase(it);
continue;
}
if ((*it)->name() != e) {
it;
continue;
}
sps.append(*it);
it = priv->processors.erase(it);
break;
}
if (it == priv->processors.end()) {
SubtitleProcessor* sp = SubtitleProcessorFactory::create(SubtitleProcessorFactory::id(e.toStdString(), false));
if (sp)
sps.append(sp);
}
}
// release the processors not wanted
qDeleteAll(priv->processors);
priv->processors = sps;
if (sps.isEmpty())
return;
foreach (SubtitleProcessor* sp, sps) {
priv->supported_suffixes.append(sp->supportedTypes());
}
// TODO: remove duplicates
}
QStringList Subtitle::engines() const
{
return priv->engine_names;
}
void Subtitle::setFuzzyMatch(bool value)
{
if (priv->fuzzy_match == value)
return;
priv->fuzzy_match = value;
emit fuzzyMatchChanged();
}
bool Subtitle::fuzzyMatch() const
{
return priv->fuzzy_match;
}
void Subtitle::setRawData(const QByteArray &data)
{
// compare the whole content is not a good idea
if (priv->raw_data.size() == data.size())
return;
priv->raw_data = data;
emit rawDataChanged();
priv->url.clear();
priv->file_name.clear();
}
QByteArray Subtitle::rawData() const
{
return priv->raw_data;
}
void Subtitle::setFileName(const QString &name)
{
if (priv->file_name == name)
return;
priv->url.clear();
priv->raw_data.clear();
priv->file_name = name;
// FIXME: "/C:/movies/xxx" => "C:/movies/xxx"
if (priv->file_name.startsWith("/") && priv->file_name.contains(":")) {
int slash = priv->file_name.indexOf(":");
slash = priv->file_name.lastIndexOf("/", slash);
priv->file_name = priv->file_name.mid(slash 1);
}
emit fileNameChanged();
}
QString Subtitle::fileName() const
{
return priv->file_name;
}
void Subtitle::setSuffixes(const QStringList &value)
{
if (priv->suffixes == value)
return;
priv->suffixes = value;
emit suffixesChanged();
}
QStringList Subtitle::suffixes() const
{
return priv->suffixes;
}
void Subtitle::setTimestamp(qreal t)
{
{
QMutexLocker lock(&priv->mutex);
Q_UNUSED(lock);
priv->t = t;
if (!isLoaded())
return;
if (!priv->prepareCurrentFrame())
return;
priv->update_image = true;
}
emit contentChanged();
}
qreal Subtitle::timestamp() const
{
return priv->t;
}
void Subtitle::load()
{
priv->reset();
emit contentChanged(); //notify user to update subtitle
// lock is not needed because it's not loaded now
if (!priv->url.isEmpty()) {
// need qt network module network
return;
}
// raw data is set, file name and url are empty
QByteArray u8 = priv->raw_data;
if (!u8.isEmpty()) {
priv->loaded = priv->processRawData(u8);
return;
}
// read from a url
QFile f(priv->url.toString());//QUrl::FullyDecoded));
if (f.exists()) {
u8 = priv->readFromFile(f.fileName());
if (u8.isEmpty())
return;
priv->loaded = priv->processRawData(u8);
return;
}
// read from a file
QStringList paths = priv->find();
if (paths.isEmpty())
return;
foreach (QString path, paths) {
if (path.isEmpty())
continue;
u8 = priv->readFromFile(path);
if (u8.isEmpty())
continue;
if (!priv->processRawData(u8))
continue;
priv->loaded = true;
return;
}
}
void Subtitle::loadAsync()
{
class Loader : public QRunnable {
public:
Loader(Subtitle *sub) : m_sub(sub) {}
void run() {
if (m_sub)
m_sub->load();
}
private:
Subtitle *m_sub;
};
QThreadPool::globalInstance()->start(new Loader(this));
}
QString Subtitle::getText() const
{
QMutexLocker lock(&priv->mutex);
Q_UNUSED(lock);
if (!isLoaded())
return QString();
return priv->frame.text;
}
QImage Subtitle::getImage(int width, int height)
{
QMutexLocker lock(&priv->mutex);
Q_UNUSED(lock);
if (!isLoaded())
return QImage();
if (!priv->frame || width == 0 || height == 0)
return QImage();
if (!priv->update_image
&& width == priv->frame.image.width() && height == priv->frame.image.height())
return priv->frame.image;
priv->update_image = false;
if (!priv->processor)
return QImage();
priv->frame.image = priv->processor->getImage(priv->t, width, height);
return priv->frame.image;
}
// DO NOT set frame's image to reduce memory usage
// assume frame.text is already set
// check previous text if now no subtitle
bool Subtitle::Private::prepareCurrentFrame()
{
QLinkedList<SubtitleFrame>::iterator it = itf;
bool found = false;
if (t < it->begin) {
while (it != frames.begin()) {
--it;
if (t > it->end) {
// no subtitle at that time. check previous text
if (frame.isValid()) {
frame = SubtitleFrame();
return true;
}
return false;
}
if (t >= (*it).begin) {
found = true;
itf = it;
break;
}
}
if (found) {
frame = *it;
return true;
}
// no subtitle at that time. it == begin(). check previous text
if (frame.isValid()) {
frame = SubtitleFrame();
return true;
}
return false;
}
// t >= (*it)->begin, time does not change, frame does not change
if (t <= (*it).end) {
found = true;
if (frame.isValid()) //previous has no subtitle, itf was not changed
return false;
frame = *it;
return true;
}
// t >= (*it)->end, already t >= (*it)->begin
while (it != frames.end()) {
it;
if (t < it->begin) {
// no subtitle at that time. check previous text
if (frame.isValid()) {
frame = SubtitleFrame();
return true;
}
return false;
}
if (t <= (*it).end) {
found = true;
itf = it;
break;
}
}
if (found) {
frame = *it;
return true;
}
// no subtitle at that time, it == end(). check previous text
if (frame.isValid()) {
frame = SubtitleFrame();
return true;
}
return false;
}
QStringList Subtitle::Private::find()
{
// !fuzzyMatch: return the file
if (!fuzzy_match) {
if (file_name.isEmpty())
return QStringList();
return QStringList() << file_name;
}
// found files will be sorted by extensions in sfx order
QStringList sfx(suffixes);
if (sfx.isEmpty())
sfx = supported_suffixes;
if (sfx.isEmpty())
return QStringList() << file_name;
QFileInfo fi(file_name);
QString name = fi.fileName();
QStringList filters;
foreach (QString suf, sfx) {
filters.append(QString("%1*.%2").arg(name).arg(suf));
}
QDir dir(fi.dir());
QStringList list = dir.entryList(filters, QDir::Files, QDir::Unsorted);
if (list.isEmpty()) {
filters.clear();
// a.mp4 => a
name = fi.completeBaseName(); // video suffix has only 1 dot
foreach (QString suf, sfx) {
filters.append(QString("%1*.%2").arg(name).arg(suf));
}
list = dir.entryList(filters, QDir::Files, QDir::Unsorted);
}
if (list.isEmpty())
return QStringList();
// TODO: sort. get entryList from nameFilters 1 by 1 is slower?
QStringList sorted;
// sfx is not empty, sort to the given order (sfx's order)
foreach (QString suf, sfx) {
if (list.isEmpty())
break;
QRegExp rx("*." suf);
rx.setPatternSyntax(QRegExp::Wildcard);
QStringList::iterator it = list.begin();
while (it != list.end()) {
if (!it->startsWith(name)) {// why it happens?
it = list.erase(it);
continue;
}
if (!rx.exactMatch(*it)) {
it;
continue;
}
sorted.append(dir.absoluteFilePath(*it));
it = list.erase(it);
}
}
// the given name is at the highest priority.
if (QFile(file_name).exists()) {
sorted.removeAll(file_name);
sorted.prepend(file_name);
}
qDebug() << "subtitles found: " << sorted;
return sorted;
}
QByteArray Subtitle::Private::readFromFile(const QString &path)
{
qDebug() << "read subtitle from: " << path;
QFile f(path);
if (f.size() > kMaxSubtitleSize)
return QByteArray();
if (!f.open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open subtitle [" << path << "]: " << f.errorString();
return QByteArray();
}
QTextStream ts(&f);
ts.setAutoDetectUnicode(true);
if (!codec.isEmpty()) {
ts.setCodec(QTextCodec::codecForName(codec));
}
return ts.readAll().toUtf8();
}
bool Subtitle::Private::processRawData(const QByteArray &data)
{
processor = 0;
frames.clear();
if (data.size() > kMaxSubtitleSize)
return false;
foreach (SubtitleProcessor* sp, processors) {
if (processRawData(sp, data)) {
processor = sp;
break;
}
}
if (!processor)
return false;
QList<SubtitleFrame> fs(processor->frames());
std::sort(fs.begin(), fs.end());
foreach (SubtitleFrame f, fs) {
frames.push_back(f);
}
itf = frames.begin();
frame = *itf;
return true;
}
bool Subtitle::Private::processRawData(SubtitleProcessor *sp, const QByteArray &data)
{
qDebug("processing subtitle from raw data...");
QByteArray u8(data);
QBuffer buf(&u8);
if (buf.open(QIODevice::ReadOnly)) {
const bool ok = sp->process(&buf);
if (buf.isOpen())
buf.close();
if (ok)
return true;
} else {
qWarning() << "open subtitle qbuffer error: " << buf.errorString();
}
qDebug("processing subtitle from a tmp utf8 file...");
QString name = url.toLocalFile().section('/', -1);
if (name.isEmpty())
name = QFileInfo(file_name).fileName(); //priv->name.section('/', -1); // if no seperator?
if (name.isEmpty())
name = "QtAV_u8_sub_cache";
name.append(QString("_%1").arg((quintptr)this));
QFile w(QDir::temp().absoluteFilePath(name));
if (w.open(QIODevice::WriteOnly)) {
w.write(data);
w.close();
} else {
if (!w.exists())
return false;
}
return sp->process(w.fileName());
}
} //namespace QtAV