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 ***************************************************************************/
21 #include "westleypreview.h"
25 #include <QtCore/QVarLengthArray>
27 #include <kstandarddirs.h>
28 #include <krandomsequence.h>
29 #include <qdatetime.h>
34 #include <qfileinfo.h>
35 #include <KTemporaryFile>
45 KDE_EXPORT ThumbCreator *new_creator() {
46 return new WestleyPreview;
50 WestleyPreview::WestleyPreview() :
58 WestleyPreview::~WestleyPreview()
61 delete m_inigoprocess;
64 bool WestleyPreview::startAndWaitProcess(const QStringList &args)
66 kDebug(DBG_AREA) << "westleypreview: starting process with args: " << args << endl;
67 m_inigoprocess->start(args.join(" "));
68 if (! m_inigoprocess->waitForStarted()) {
69 kDebug(DBG_AREA) << "westleypreview: PROCESS NOT STARTED!!! exiting\n";
72 if (! m_inigoprocess->waitForFinished()) {
73 kDebug(DBG_AREA) << "westleypreview: PROCESS DIDN'T FINISH!! exiting\n";
74 m_inigoprocess->close();
77 kDebug() << "westleypreview: process started and ended correctly\n";
81 bool WestleyPreview::create(const QString &path, int width, int /*height*/, QImage &img)
84 playerBin = KStandardDirs::findExe("inigo");
85 if (playerBin.isEmpty()) {
86 kDebug(DBG_AREA) << "westleypreview: inigo not found, exiting.\n";
93 m_rand = new KRandomSequence(QDateTime::currentDateTime().toTime_t());
94 m_inigoprocess = new QProcess();
96 kDebug(DBG_AREA) << "videopreview: url=" << furl << "; local:" << furl.isLocalFile() << endl;
97 fileinfo.towidth = width;
98 fileinfo.toheight = width * 3 / 4;
100 // if(furl.isLocalFile())
103 //TODO: modify inigo so that it can return some infos about a westley clip (duration, track number,fps,...)
104 // without actually playing the file
106 args << playerBin << QString("\"" + path + "\"") << "-file-info";
108 kDebug(DBG_AREA) << "videopreview: starting process: --_" << " " << args.join(" ") << "_--\n";
109 if (! startAndWaitProcess(args) ) return NULL;
111 QString information=QString(inigoprocess->readAllStandardOutput() );
112 QRegExp findInfos("ID_VIDEO_FPS=([\\d]*).*ID_LENGTH=([\\d]*).*");
113 if(findInfos.indexIn( information) == -1 )
115 kDebug(DBG_AREA) << "videopreview: No information found, exiting\n";
118 fileinfo.seconds =findInfos.cap(2).toInt();
119 fileinfo.fps=findInfos.cap(1).toInt();
121 fileinfo.seconds = 250;
124 const int LASTTRY = 3;
125 for (int i = 0; i <= LASTTRY; i++) {
126 pix = getFrame(path);
128 uint variance = imageVariance(pix);
129 kDebug(DBG_AREA) << "videopreview: " << QFileInfo(path).fileName() << " frame variance: " << variance << "; " <<
130 ((variance <= 40 && (i != LASTTRY - 1)) ? "!!!DROPPING!!!" : "GOOD :-)") << endl;
131 if (variance > 40 || i == LASTTRY - 1) break;
138 if (pix.depth() != 32)
139 img = pix.convertToFormat(QImage::Format_RGB32);
144 QImage WestleyPreview::getFrame(const QString &path)
147 const int START = 25;
148 const int RANGE = 500;
150 args << playerBin << "\"" + path + "\"";
152 unsigned long start = (unsigned long)(START + (m_rand->getDouble() * RANGE));
153 args << QString("in=%1").arg(start) << QString("out=%1").arg(start) << "-consumer";
156 temp.setSuffix(".png");
158 args << QString("avformat:%1").arg(temp.fileName()) << "vframes=1" << "f=rawvideo" << "vcodec=png" << QString("s=%1x%2").arg(fileinfo.towidth).arg(fileinfo.toheight);
159 if (! startAndWaitProcess(args)) return QImage();
160 QImage retpix(temp.fileName());
166 uint WestleyPreview::imageVariance(QImage image)
170 uint bytes = image.numBytes();
171 uint STEPS = bytes / 2;
172 QVarLengthArray<uchar> pivot(STEPS);
173 kDebug(DBG_AREA) << "Using " << STEPS << " steps\n";
174 uchar *bits = image.bits();
175 // First pass: get pivots and taking average
176 for (uint i = 0; i < STEPS ; i++) {
177 pivot[i] = bits[i*(bytes/STEPS)];
181 // Second Step: calculate delta (average?)
182 for (uint i = 0; i < STEPS; i++) {
183 int curdelta = abs(int(avg - pivot[i]));
186 return delta / STEPS;
189 ThumbCreator::Flags WestleyPreview::flags() const
195 #include "westleypreview.moc"