forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoFrameExtractor.cpp
519 lines (489 loc) · 15.3 KB
/
VideoFrameExtractor.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
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2014-2015 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/VideoFrameExtractor.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QQueue>
#include <QtCore/QRunnable>
#include <QtCore/QScopedPointer>
#include <QtCore/QStringList>
#include <QtCore/QThread>
#include "QtAV/VideoCapture.h"
#include "QtAV/VideoDecoder.h"
#include "QtAV/AVDemuxer.h"
#include "QtAV/Packet.h"
#include "utils/BlockingQueue.h"
#include "utils/Logger.h"
// TODO: event and signal do not work
#define ASYNC_SIGNAL 0
#define ASYNC_EVENT 0
#define ASYNC_TASK 1
namespace QtAV {
class ExtractThread : public QThread {
public:
ExtractThread(QObject *parent = 0)
: QThread(parent)
, stop(false)
{
tasks.setCapacity(1); // avoid too frequent
}
~ExtractThread() {
waitStop();
}
void waitStop() {
if (!isRunning())
return;
scheduleStop();
wait();
}
void addTask(QRunnable* t) {
if (tasks.size() >= tasks.capacity()) {
QRunnable *task = tasks.take();
if (task->autoDelete())
delete task;
}
tasks.put(t);
}
void scheduleStop() {
class StopTask : public QRunnable {
public:
StopTask(ExtractThread* t) : thread(t) {}
void run() { thread->stop = true;}
private:
ExtractThread *thread;
};
addTask(new StopTask(this));
}
protected:
virtual void run() {
#if ASYNC_TASK
while (!stop) {
QRunnable *task = tasks.take();
if (!task)
return;
task->run();
if (task->autoDelete())
delete task;
}
#else
exec();
#endif //ASYNC_TASK
}
public:
volatile bool stop;
private:
BlockingQueue<QRunnable*> tasks;
};
// FIXME: avcodec_close() crash
const int kDefaultPrecision = 500;
class VideoFrameExtractorPrivate : public DPtrPrivate<VideoFrameExtractor>
{
public:
VideoFrameExtractorPrivate()
: extracted(false)
, async(true)
, has_video(true)
, auto_extract(true)
, auto_precision(true)
, seek_count(0)
, position(-2*kDefaultPrecision)
, precision(kDefaultPrecision)
, decoder(0)
{
QVariantHash opt;
opt["skip_frame"] = 8; // 8 for "avcodec", "NoRef" for "FFmpeg". see AVDiscard
dec_opt_framedrop["avcodec"] = opt;
opt["skip_frame"] = 0; // 0 for "avcodec", "Default" for "FFmpeg". see AVDiscard
dec_opt_normal["avcodec"] = opt; // avcodec need correct string or value in libavcodec
codecs
#if QTAV_HAVE(DXVA)
// << "DXVA"
#endif //QTAV_HAVE(DXVA)
#if QTAV_HAVE(VAAPI)
// << "VAAPI"
#endif //QTAV_HAVE(VAAPI)
#if QTAV_HAVE(CEDARV)
//<< "Cedarv"
#endif //QTAV_HAVE(CEDARV)
#if QTAV_HAVE(VDA)
// << "VDA" // only 1 app can use VDA at a given time
#endif //QTAV_HAVE(VDA)
<< "FFmpeg";
}
~VideoFrameExtractorPrivate() {
// stop first before demuxer and decoder close to avoid running new seek task after demuxer is closed.
thread.waitStop();
// close codec context first.
decoder.reset(0);
demuxer.unload();
}
bool checkAndOpen() {
const bool loaded = demuxer.fileName() == source && demuxer.isLoaded();
if (loaded && decoder && !demuxer.atEnd())
return true;
seek_count = 0;
if (decoder) { // new source
decoder->close();
decoder.reset(0);
}
if (!loaded || demuxer.atEnd()) {
demuxer.unload();
demuxer.setMedia(source);
if (!demuxer.load()) {
return false;
}
}
has_video = demuxer.videoStreams().size() > 0;
if (!has_video) {
demuxer.unload();
return false;
}
if (codecs.isEmpty())
return false;
if (auto_precision) {
if (demuxer.duration() < 10*1000)
precision = kDefaultPrecision/2;
else
precision = kDefaultPrecision;
}
foreach (const QString& c, codecs) {
VideoDecoderId cid = VideoDecoderFactory::id(c.toUtf8().constData());
VideoDecoder *vd = VideoDecoderFactory::create(cid);
if (!vd)
continue;
decoder.reset(vd);
decoder->setCodecContext(demuxer.videoCodecContext());
if (!decoder->prepare()) {
decoder.reset(0);
continue;
}
if (!decoder->open()) {
decoder.reset(0);
continue;
}
QVariantHash opt, va;
va["display"] = "X11"; // to support swscale
opt["vaapi"] = va;
decoder->setOptions(opt);
break;
}
return !!decoder;
}
// return the key frame position
bool extractInPrecision(qint64 value, int range) {
frame = VideoFrame();
if (value < demuxer.startTime())
value = demuxer.startTime();
demuxer.seek(value);
const int vstream = demuxer.videoStream();
Packet pkt;
qint64 pts0 = -1;
bool warn_bad_seek = true;
bool warn_out_of_range = true;
while (!demuxer.atEnd()) {
if (!demuxer.readFrame())
continue;
if (demuxer.stream() != vstream)
continue;
pkt = demuxer.packet();
if (pts0 < 0LL)
pts0 = (qint64)(pkt.pts*1000.0);
if ((qint64)(pkt.pts*1000.0) - value > (qint64)range) {
if (warn_out_of_range)
qDebug("read packet out of range");
warn_out_of_range = false;
// No return because decoder needs more packets before the desired frame is decoded
//return false;
}
//qDebug("video packet: %f", pkt.pts);
// TODO: always key frame?
if (pkt.hasKeyFrame)
break;
if (warn_bad_seek)
qWarning("Not seek to key frame!!!");
warn_bad_seek = false;
}
// enlarge range if seek to key-frame failed
const qint64 key_pts = (qint64)(pkt.pts*1000.0);
const bool enlarge_range = pts0 >= 0LL && key_pts - pts0 > 0LL;
if (enlarge_range) {
range = qMax<qint64>(key_pts - value, range);
qDebug() << "enlarge range ==>>>> " << range;
}
if (!pkt.isValid()) {
qWarning("VideoFrameExtractor failed to get a packet at %lld", value);
return false;
}
decoder->flush(); //must flush otherwise old frames will be decoded at the beginning
decoder->setOptions(dec_opt_normal);
// must decode key frame
int k = 0;
while (k < 2 && !frame.isValid()) {
//qWarning("invalid key frame!!!!! undecoded: %d", decoder->undecodedSize());
if (decoder->decode(pkt)) {
frame = decoder->frame();
}
k;
}
// if seek backward correctly to key frame, diff0 = t - value <= 0
// but sometimes seek to no-key frame(and range is enlarged), diff0 >= 0
// decode key frame
const int diff0 = qint64(frame.timestamp()*1000.0) - value;
if (qAbs(diff0) <= range) { //TODO: flag forward: result pts must >= value
if (frame.isValid()) {
qDebug() << "VideoFrameExtractor: key frame found @" << frame.timestamp() <<" diff=" << diff0 << ". format: " << frame.format();
return true;
}
}
QVariantHash* dec_opt = &dec_opt_normal; // 0: default, 1: framedrop
// decode at the given position
while (!demuxer.atEnd()) {
if (!demuxer.readFrame())
continue;
if (demuxer.stream() != vstream)
continue;
pkt = demuxer.packet();
const qreal t = pkt.pts;
//qDebug("video packet: %f, delta=%lld", t, value - qint64(t*1000.0));
if (!pkt.isValid()) {
qWarning("invalid packet. no decode");
continue;
}
if (pkt.hasKeyFrame) {
// FIXME:
//qCritical("Internal error. Can not be a key frame!!!!");
//return false; //??
}
qint64 diff = qint64(t*1000.0) - value;
QVariantHash *dec_opt_old = dec_opt;
if (seek_count == 0 || diff >= 0)
dec_opt = &dec_opt_normal;
else
dec_opt = &dec_opt_framedrop;
if (dec_opt != dec_opt_old)
decoder->setOptions(*dec_opt);
// invalid packet?
if (!decoder->decode(pkt)) {
qWarning("!!!!!!!!!decode failed!!!!");
frame = VideoFrame();
return false;
}
// store the last decoded frame because next frame may be out of range
const VideoFrame f = decoder->frame();
if (!f.isValid()) {
//qDebug("VideoFrameExtractor: invalid frame!!!");
continue;
}
frame = f;
const qreal pts = frame.timestamp();
const qint64 pts_ms = pts*1000.0;
if (pts_ms < value)
continue; //
diff = pts_ms - value;
if (qAbs(diff) <= (qint64)range) {
qDebug("got frame at %fs, diff=%lld", pts, diff);
break;
}
// if decoder was not flushed, we may get old frame which is acceptable
if (diff > range && t > pts) {
qWarning("out pts out of range. diff=%lld, range=%d", diff, range);
frame = VideoFrame();
return false;
}
}
seek_count;
// now we get the final frame
return true;
}
void releaseResourceInternal() {
decoder.reset(0);
demuxer.unload();
}
void safeReleaseResource() {
class Cleaner : public QRunnable {
VideoFrameExtractorPrivate *p;
public:
Cleaner(VideoFrameExtractorPrivate* pri) : p(pri) {}
void run() {
p->releaseResourceInternal();
}
};
thread.addTask(new Cleaner(this));
}
bool extracted;
bool async;
bool has_video;
bool loading;
bool auto_extract;
bool auto_precision;
int seek_count;
qint64 position;
int precision;
QString source;
AVDemuxer demuxer;
QScopedPointer<VideoDecoder> decoder;
VideoFrame frame;
QStringList codecs;
ExtractThread thread;
static QVariantHash dec_opt_framedrop, dec_opt_normal;
};
QVariantHash VideoFrameExtractorPrivate::dec_opt_framedrop;
QVariantHash VideoFrameExtractorPrivate::dec_opt_normal;
VideoFrameExtractor::VideoFrameExtractor(QObject *parent) :
QObject(parent)
{
DPTR_D(VideoFrameExtractor);
moveToThread(&d.thread);
d.thread.start();
connect(this, SIGNAL(aboutToExtract(qint64)), SLOT(extractInternal(qint64)));
}
void VideoFrameExtractor::setSource(const QString value)
{
DPTR_D(VideoFrameExtractor);
if (value == d.source)
return;
d.source = value;
d.has_video = true;
emit sourceChanged();
d.frame = VideoFrame();
d.safeReleaseResource();
}
QString VideoFrameExtractor::source() const
{
return d_func().source;
}
void VideoFrameExtractor::setAsync(bool value)
{
DPTR_D(VideoFrameExtractor);
if (d.async == value)
return;
d.async = value;
emit asyncChanged();
}
bool VideoFrameExtractor::async() const
{
return d_func().async;
}
void VideoFrameExtractor::setAutoExtract(bool value)
{
DPTR_D(VideoFrameExtractor);
if (d.auto_extract == value)
return;
d.auto_extract = value;
emit autoExtractChanged();
}
bool VideoFrameExtractor::autoExtract() const
{
return d_func().auto_extract;
}
void VideoFrameExtractor::setPosition(qint64 value)
{
DPTR_D(VideoFrameExtractor);
if (!d.has_video)
return;
if (qAbs(value - d.position) < precision()) {
return;
}
d.frame = VideoFrame();
d.extracted = false;
d.position = value;
emit positionChanged();
if (!autoExtract())
return;
extract();
}
qint64 VideoFrameExtractor::position() const
{
return d_func().position;
}
void VideoFrameExtractor::setPrecision(int value)
{
DPTR_D(VideoFrameExtractor);
if (d.precision == value)
return;
d.auto_precision = value < 0;
// explain why value (p0) is used but not the actual decoded position (p)
// it's key frame finding rule
if (value >= 0)
d.precision = value;
emit precisionChanged();
}
int VideoFrameExtractor::precision() const
{
return d_func().precision;
}
bool VideoFrameExtractor::event(QEvent *e)
{
//qDebug("event: %d", e->type());
if (e->type() != QEvent::User)
return QObject::event(e);
extractInternal(position()); // FIXME: wrong position
return true;
}
void VideoFrameExtractor::extract()
{
DPTR_D(VideoFrameExtractor);
if (!d.async) {
extractInternal(position());
return;
}
#if ASYNC_SIGNAL
else {
emit aboutToExtract(position());
return;
}
#endif
#if ASYNC_TASK
class ExtractTask : public QRunnable {
public:
ExtractTask(VideoFrameExtractor *e, qint64 t)
: extractor(e)
, position(t)
{}
void run() {
extractor->extractInternal(position);
}
private:
VideoFrameExtractor *extractor;
qint64 position;
};
d.thread.addTask(new ExtractTask(this, position()));
return;
#endif
#if ASYNC_EVENT
qApp->postEvent(this, new QEvent(QEvent::User));
#endif //ASYNC_EVENT
}
void VideoFrameExtractor::extractInternal(qint64 pos)
{
DPTR_D(VideoFrameExtractor);
int precision_old = precision();
if (!d.checkAndOpen()) {
emit error();
//qWarning("can not open decoder....");
return; // error handling
}
if (precision_old != precision()) {
emit precisionChanged();
}
d.extracted = d.extractInPrecision(pos, precision());
if (!d.extracted) {
emit error();
return;
}
emit frameExtracted(d.frame);
}
} //namespace QtAV