Skip to content

Commit

Permalink
test: more options for decoder. async extract
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Apr 14, 2015
1 parent 6b7c102 commit 1ea9f66
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
28 changes: 23 additions & 5 deletions tests/decoder/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <QCoreApplication>
#include <QtDebug>
#include <QtCore/QElapsedTimer>
#include <QtCore/QStringList>
#include <QtAV/AVDemuxer.h>
Expand All @@ -16,15 +17,35 @@ int main(int argc, char *argv[])
file = a.arguments().at(idx + 1);
QString decName("FFmpeg");
idx = a.arguments().indexOf("-vc");
if (idx < 0)
idx = a.arguments().indexOf("-vd");
if (idx > 0)
decName = a.arguments().at(idx + 1);

QString opt;
QVariantHash decopt;
idx = decName.indexOf(":");
if (idx > 0) {
opt = decName.right(decName.size() - idx -1);
decName = decName.left(idx);
QStringList opts(opt.split(";"));
QVariantHash subopt;
foreach (QString o, opts) {
idx = o.indexOf(":");
subopt[o.left(idx)] = o.right(o.size() - idx - 1);
}
decopt[decName] = subopt;
}
qDebug() << decopt;

VideoDecoderId cid = VideoDecoderFactory::id(decName.toStdString());
if (cid <= 0) {
qWarning("Can not find decoder: %s", decName.toUtf8().constData());
return 1;
}
VideoDecoder *dec = VideoDecoderFactory::create(cid);
if (!decopt.isEmpty())
dec->setOptions(decopt);
AVDemuxer demux;
demux.setMedia(file);
if (!demux.load()) {
Expand All @@ -46,11 +67,8 @@ int main(int argc, char *argv[])
continue;
const Packet pkt = demux.packet();
if (dec->decode(pkt)) {
/*
* TODO: may contains more than 1 frames
* map from gpu or not?
*/
//frame = dec->frame().clone();
VideoFrame frame = dec->frame(); // why is faster to call frame() for hwdec? no frame() is very slow for VDA
Q_UNUSED(frame);
count++;
printf("decode count: %d\r", count);fflush(0);
}
Expand Down
56 changes: 44 additions & 12 deletions tests/extract/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,66 @@
#include <QtAVWidgets>
#include <QtDebug>
#include <QtCore/QElapsedTimer>
#include <QtCore/QTimer>

using namespace QtAV;
class VideoFrameObserver : public QObject
{
Q_OBJECT
public:
VideoFrameObserver(QObject *parent = 0) : QObject(parent)
, pos(0)
, nb(1)
, extracted(0)
{
view = VideoRendererFactory::create(VideoRendererId_GLWidget2);
view->widget()->resize(400, 300);
view->widget()->show();
connect(&extractor, SIGNAL(frameExtracted(QtAV::VideoFrame)), this, SLOT(onVideoFrameExtracted(QtAV::VideoFrame)));
}
void setParameters(qint64 msec, int count) {
pos = msec;
nb = count;
}
void start(const QString& file) {
extractor.setAsync(false);
extractor.setSource(file);
startTimer(20);
timer.start();
extractor.setPosition(pos);
}
void startAsync(const QString& file) {
extractor.setAsync(true);
extractor.setSource(file);
startTimer(20);
timer.start();
extractor.setPosition(pos);
}

public Q_SLOTS:
void onVideoFrameExtracted(const QtAV::VideoFrame& frame) {
view->receive(frame);
qApp->processEvents();
frame.toImage().save(QString::number(frame.timestamp()) + ".png");
qDebug("frame %dx%d @%f", frame.width(), frame.height(), frame.timestamp());
if (++extracted >= nb) {
qDebug("elapsed: %lld.", timer.elapsed());
return;
}
extractor.setPosition(pos + extracted*1000);
}
protected:
void timerEvent(QTimerEvent *) {
qApp->processEvents(); // avoid ui blocking if async is not used
}

private:
VideoRenderer *view;
qint64 pos;
int nb;
int extracted;
VideoFrameExtractor extractor;
QElapsedTimer timer;
};

int main(int argc, char** argv)
Expand All @@ -67,19 +106,12 @@ int main(int argc, char** argv)
bool async = a.arguments().contains("-async");


VideoFrameExtractor extractor;
extractor.setAsync(async);
VideoFrameObserver obs;
QObject::connect(&extractor, SIGNAL(frameExtracted(QtAV::VideoFrame)), &obs, SLOT(onVideoFrameExtracted(QtAV::VideoFrame)));
extractor.setSource(file);

QElapsedTimer timer;
timer.start();
for (int i = 0; i < n; ++i) {
// async does not work. you have to set a new position when frameExtracted is emitted
extractor.setPosition(t + 1000*i);
}
qDebug("elapsed: %lld", timer.elapsed());
obs.setParameters(t*1000, n);
if (async)
obs.startAsync(file);
else
obs.start(file);
return a.exec();
}

Expand Down

0 comments on commit 1ea9f66

Please sign in to comment.