1 /***************************************************************************
2 Copyright (C) 2006-2008
3 by Marco Gulino <marco@kmobiletools.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19 ***************************************************************************/
23 #include <QtCore/QVarLengthArray>
25 #include <kstandarddirs.h>
26 #include <krandomsequence.h>
27 #include <qdatetime.h>
32 #include <qfileinfo.h>
33 #include <KTemporaryFile>
37 #include "westleypreview.h"
43 KDE_EXPORT ThumbCreator *new_creator() {
44 return new WestleyPreview;
48 WestleyPreview::WestleyPreview()
49 : m_rand(0), m_inigoprocess(0) {
52 WestleyPreview::~WestleyPreview() {
54 delete m_inigoprocess;
57 bool WestleyPreview::startAndWaitProcess(const QStringList &args) {
58 kDebug(DBG_AREA) << "westleypreview: starting process with args: " << args << endl;
59 m_inigoprocess->start(args.join(" "));
60 if (! m_inigoprocess->waitForStarted()) {
61 kDebug(DBG_AREA) << "westleypreview: PROCESS NOT STARTED!!! exiting\n";
64 if (! m_inigoprocess->waitForFinished()) {
65 kDebug(DBG_AREA) << "westleypreview: PROCESS DIDN'T FINISH!! exiting\n";
66 m_inigoprocess->close();
69 kDebug() << "westleypreview: process started and ended correctly\n";
73 bool WestleyPreview::create(const QString &path, int width, int height, QImage &img) {
75 playerBin = KStandardDirs::findExe("inigo");
76 if (playerBin.isEmpty()) {
77 kDebug(DBG_AREA) << "westleypreview: inigo not found, exiting.\n";
84 m_rand = new KRandomSequence(QDateTime::currentDateTime().toTime_t());
85 m_inigoprocess = new QProcess();
87 kDebug(DBG_AREA) << "videopreview: url=" << furl << "; local:" << furl.isLocalFile() << endl;
88 fileinfo.towidth = width;
89 fileinfo.toheight = width * 3 / 4;
91 // if(furl.isLocalFile())
94 //TODO: modify inigo so that it can return some infos about a westley clip (duration, track number,fps,...)
95 // without actually playing the file
97 args << playerBin << QString("\"" + path + "\"") << "-file-info";
99 kDebug(DBG_AREA) << "videopreview: starting process: --_" << " " << args.join(" ") << "_--\n";
100 if (! startAndWaitProcess(args) ) return NULL;
102 QString information=QString(inigoprocess->readAllStandardOutput() );
103 QRegExp findInfos("ID_VIDEO_FPS=([\\d]*).*ID_LENGTH=([\\d]*).*");
104 if(findInfos.indexIn( information) == -1 )
106 kDebug(DBG_AREA) << "videopreview: No information found, exiting\n";
109 fileinfo.seconds =findInfos.cap(2).toInt();
110 fileinfo.fps=findInfos.cap(1).toInt();
112 fileinfo.seconds = 250;
115 const int LASTTRY = 3;
116 for (int i = 0; i <= LASTTRY; i++) {
117 pix = getFrame(path);
119 uint variance = imageVariance(pix);
120 kDebug(DBG_AREA) << "videopreview: " << QFileInfo(path).fileName() << " frame variance: " << variance << "; " <<
121 ((variance <= 40 && (i != LASTTRY - 1)) ? "!!!DROPPING!!!" : "GOOD :-)") << endl;
122 if (variance > 40 || i == LASTTRY - 1) break;
129 if (pix.depth() != 32)
130 img = pix.convertToFormat( QImage::Format_RGB32 );
135 QImage WestleyPreview::getFrame(const QString &path) {
137 const int START = 25;
138 const int RANGE = 500;
140 args << playerBin << "\"" + path + "\"";
142 unsigned long start = (unsigned long)(START + (m_rand->getDouble() * RANGE));
143 args << QString("in=%1").arg(start) << QString("out=%1").arg(start) << "-consumer";
146 temp.setSuffix(".png");
148 args << QString("avformat:%1").arg(temp.fileName()) << "vframes=1" << "f=rawvideo" << "vcodec=png" << QString("s=%1x%2").arg(fileinfo.towidth).arg(fileinfo.toheight);
149 if (! startAndWaitProcess(args)) return QImage();
150 QImage retpix(temp.fileName());
156 uint WestleyPreview::imageVariance(QImage image) {
159 uint bytes = image.numBytes();
160 uint STEPS = bytes / 2;
161 QVarLengthArray<uchar> pivot(STEPS);
162 kDebug(DBG_AREA) << "Using " << STEPS << " steps\n";
163 uchar *bits = image.bits();
164 // First pass: get pivots and taking average
165 for (uint i = 0; i < STEPS ; i++) {
166 pivot[i] = bits[i*(bytes/STEPS)];
170 // Second Step: calculate delta (average?)
171 for (uint i = 0; i < STEPS; i++) {
172 int curdelta = abs(int(avg - pivot[i]));
175 return delta / STEPS;
178 ThumbCreator::Flags WestleyPreview::flags() const
184 #include "westleypreview.moc"