]> git.sesse.net Git - kdenlive/blob - thumbnailer/westleypreview.cpp
Reindent the codebase using 'linux' bracket placement.
[kdenlive] / thumbnailer / westleypreview.cpp
1 /***************************************************************************
2    Copyright (C) 2006-2008
3    by Marco Gulino <marco@kmobiletools.org>
4
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.
9
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.
14
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  ***************************************************************************/
20
21 #include "westleypreview.h"
22
23 #include <qfile.h>
24 #include <qimage.h>
25 #include <QtCore/QVarLengthArray>
26
27 #include <kstandarddirs.h>
28 #include <krandomsequence.h>
29 #include <qdatetime.h>
30 #include <QProcess>
31 #include <kdebug.h>
32 #include <ktempdir.h>
33 #include <kurl.h>
34 #include <qfileinfo.h>
35 #include <KTemporaryFile>
36
37 #include <unistd.h>
38
39
40 #define DBG_AREA
41
42 //#include "config.h"
43 extern "C"
44 {
45     KDE_EXPORT ThumbCreator *new_creator() {
46         return new WestleyPreview;
47     }
48 }
49
50 WestleyPreview::WestleyPreview()
51         : m_rand(0), m_inigoprocess(0)
52 {
53 }
54
55 WestleyPreview::~WestleyPreview()
56 {
57     delete m_rand;
58     delete m_inigoprocess;
59 }
60
61 bool WestleyPreview::startAndWaitProcess(const QStringList &args)
62 {
63     kDebug(DBG_AREA) << "westleypreview: starting process with args: " << args << endl;
64     m_inigoprocess->start(args.join(" "));
65     if (! m_inigoprocess->waitForStarted()) {
66         kDebug(DBG_AREA) << "westleypreview: PROCESS NOT STARTED!!! exiting\n";
67         return false;
68     }
69     if (! m_inigoprocess->waitForFinished()) {
70         kDebug(DBG_AREA) << "westleypreview: PROCESS DIDN'T FINISH!! exiting\n";
71         m_inigoprocess->close();
72         return false;
73     }
74     kDebug() << "westleypreview: process started and ended correctly\n";
75     return true;
76 }
77
78 bool WestleyPreview::create(const QString &path, int width, int /*height*/, QImage &img)
79 {
80     QFileInfo fi(path);
81     playerBin = KStandardDirs::findExe("inigo");
82     if (playerBin.isEmpty()) {
83         kDebug(DBG_AREA) << "westleypreview: inigo not found, exiting.\n";
84         return false;
85     }
86
87     fileinfo.seconds = 0;
88     fileinfo.fps = 0;
89
90     m_rand = new KRandomSequence(QDateTime::currentDateTime().toTime_t());
91     m_inigoprocess = new QProcess();
92     KUrl furl(path);
93     kDebug(DBG_AREA) << "videopreview: url=" << furl << "; local:" << furl.isLocalFile() << endl;
94     fileinfo.towidth = width;
95     fileinfo.toheight = width * 3 / 4;
96     QImage pix;
97 //    if(furl.isLocalFile())
98 //    {
99     QStringList args;
100     //TODO: modify inigo so that it can return some infos about a westley clip (duration, track number,fps,...)
101     // without actually playing the file
102     /*
103         args << playerBin << QString("\"" + path + "\"") << "-file-info";
104
105         kDebug(DBG_AREA) << "videopreview: starting process: --_" << " " << args.join(" ") << "_--\n";
106         if (! startAndWaitProcess(args) ) return NULL;
107
108         QString information=QString(inigoprocess->readAllStandardOutput() );
109         QRegExp findInfos("ID_VIDEO_FPS=([\\d]*).*ID_LENGTH=([\\d]*).*");
110         if(findInfos.indexIn( information) == -1 )
111         {
112             kDebug(DBG_AREA) << "videopreview: No information found, exiting\n";
113             return NULL;
114         }
115         fileinfo.seconds =findInfos.cap(2).toInt();
116         fileinfo.fps=findInfos.cap(1).toInt();
117         */
118     fileinfo.seconds = 250;
119     fileinfo.fps = 25;
120
121     const int LASTTRY = 3;
122     for (int i = 0; i <= LASTTRY; i++) {
123         pix = getFrame(path);
124         if (!pix.isNull()) {
125             uint variance = imageVariance(pix);
126             kDebug(DBG_AREA) << "videopreview: " << QFileInfo(path).fileName() << " frame variance: " << variance << "; " <<
127             ((variance <= 40 && (i != LASTTRY - 1)) ? "!!!DROPPING!!!" : "GOOD :-)") << endl;
128             if (variance > 40 || i == LASTTRY - 1) break;
129         }
130     }
131     if (pix.isNull()) {
132         return false;
133     }
134
135     if (pix.depth() != 32)
136         img = pix.convertToFormat(QImage::Format_RGB32);
137     else img = pix;
138     return true;
139 }
140
141 QImage WestleyPreview::getFrame(const QString &path)
142 {
143     QStringList args;
144     const int START = 25;
145     const int RANGE = 500;
146     args.clear();
147     args << playerBin << "\"" + path + "\"";
148
149     unsigned long start = (unsigned long)(START + (m_rand->getDouble() * RANGE));
150     args << QString("in=%1").arg(start) << QString("out=%1").arg(start) << "-consumer";
151
152     KTemporaryFile temp;
153     temp.setSuffix(".png");
154     temp.open();
155     args << QString("avformat:%1").arg(temp.fileName()) << "vframes=1" << "f=rawvideo" << "vcodec=png" << QString("s=%1x%2").arg(fileinfo.towidth).arg(fileinfo.toheight);
156     if (! startAndWaitProcess(args)) return QImage();
157     QImage retpix(temp.fileName());
158     temp.close();
159     return retpix;
160 }
161
162
163 uint WestleyPreview::imageVariance(QImage image)
164 {
165     uint delta = 0;
166     uint avg = 0;
167     uint bytes = image.numBytes();
168     uint STEPS = bytes / 2;
169     QVarLengthArray<uchar> pivot(STEPS);
170     kDebug(DBG_AREA) << "Using " << STEPS << " steps\n";
171     uchar *bits = image.bits();
172     // First pass: get pivots and taking average
173     for (uint i = 0; i < STEPS ; i++) {
174         pivot[i] = bits[i*(bytes/STEPS)];
175         avg += pivot[i];
176     }
177     avg = avg / STEPS;
178     // Second Step: calculate delta (average?)
179     for (uint i = 0; i < STEPS; i++) {
180         int curdelta = abs(int(avg - pivot[i]));
181         delta += curdelta;
182     }
183     return delta / STEPS;
184 }
185
186 ThumbCreator::Flags WestleyPreview::flags() const
187 {
188     return None;
189 }
190
191
192 #include "westleypreview.moc"
193