]> git.sesse.net Git - kdenlive/blob - thumbnailer/westleypreview.cpp
Westley thumbnailer: reindent + cleanup useless includes
[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 <qfile.h>
22 #include <qpixmap.h>
23 #include <qimage.h>
24 #include <QtCore/QVarLengthArray>
25
26 #include <kstandarddirs.h>
27 #include <krandomsequence.h>
28 #include <qdatetime.h>
29 #include <QProcess>
30 #include <kdebug.h>
31 #include <ktempdir.h>
32 #include <kurl.h>
33 #include <qfileinfo.h>
34 #include <KTemporaryFile>
35
36 #include <unistd.h>
37
38 #include "westleypreview.h"
39
40 #define DBG_AREA
41
42 //#include "config.h"
43 extern "C" {
44     KDE_EXPORT ThumbCreator *new_creator() {
45         return new WestleyPreview;
46     }
47 }
48
49 WestleyPreview::WestleyPreview()
50         : m_rand(0), m_inigoprocess(0) {
51 }
52
53 WestleyPreview::~WestleyPreview() {
54     delete m_rand;
55     delete m_inigoprocess;
56 }
57
58 bool WestleyPreview::startAndWaitProcess(const QStringList &args) {
59     kDebug(DBG_AREA) << "westleypreview: starting process with args: " << args << endl;
60     m_inigoprocess->start(args.join(" "));
61     if (! m_inigoprocess->waitForStarted()) {
62         kDebug(DBG_AREA) << "westleypreview: PROCESS NOT STARTED!!! exiting\n";
63         return false;
64     }
65     if (! m_inigoprocess->waitForFinished()) {
66         kDebug(DBG_AREA) << "westleypreview: PROCESS DIDN'T FINISH!! exiting\n";
67         m_inigoprocess->close();
68         return false;
69     }
70     kDebug() << "westleypreview: process started and ended correctly\n";
71     return true;
72 }
73
74 bool WestleyPreview::create(const QString &path, int width, int height, QImage &img) {
75     QFileInfo fi(path);
76     /*if (fi.suffix().trimmed() != "westley" && fi.suffix().trimmed() != "kdenlive")
77     {
78         kDebug(DBG_AREA) << "westleypreview: matched extension " << fi.suffix().prepend('.') << "; exiting.\n";
79         return false;
80     }*/
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 = height;
96     QPixmap 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     //kDebug(DBG_AREA) << "videopreview: find length=" << fileinfo.seconds << ", fps=" << fileinfo.fps << endl;
122
123     const int LASTTRY = 3;
124     for (int i = 0; i <= LASTTRY; i++) {
125         pix = getFrame(path);
126         if (!pix.isNull()) {
127             uint variance = imageVariance(pix.toImage()/*.bits(),( (width+ 7) & ~0x7), width, height, 1 */);
128             kDebug(DBG_AREA) << "videopreview: " << QFileInfo(path).fileName() << " frame variance: " << variance << "; " <<
129             ((variance <= 40 && (i != LASTTRY - 1)) ? "!!!DROPPING!!!" : "GOOD :-)") << endl;
130             if (variance > 40 || i == LASTTRY - 1) break;
131         }
132     }
133     if (pix.isNull()) {
134         return false;
135     }
136     img = pix.toImage();
137     return true;
138 }
139
140 QPixmap WestleyPreview::getFrame(const QString &path) {
141     QStringList args;
142 #define START ((fileinfo.seconds*15)/100)
143 #define END ((fileinfo.seconds*70)/100)
144     args.clear();
145     args << playerBin << "\"" + path + "\"";
146     if (fileinfo.towidth > fileinfo.toheight) fileinfo.toheight = -2;
147     else fileinfo.towidth = -2;
148 //     switch( flags ){
149 //         case random
150 //     }
151     unsigned long start = (unsigned long)(START + (m_rand->getDouble() * (END - START)));
152     args << QString("in=%1").arg(start) << QString("out=%1").arg(start) << "-consumer";
153
154     KTemporaryFile temp;
155     temp.setSuffix(".png");
156     temp.open();
157     args << QString("avformat:%1").arg(temp.fileName()) << "vframes=1" << "f=rawvideo" << "vcodec=png" << QString("s=%1x%2").arg(fileinfo.towidth).arg(fileinfo.toheight);
158     if (! startAndWaitProcess(args)) return NULL;
159     QPixmap retpix(temp.fileName());
160     temp.close();
161     return retpix;
162 }
163
164
165 uint WestleyPreview::imageVariance(QImage image) {
166     uint delta = 0;
167     uint avg = 0;
168     uint bytes = image.numBytes();
169     uint STEPS = bytes / 2;
170     QVarLengthArray<uchar> pivot(STEPS);
171     kDebug(DBG_AREA) << "Using " << STEPS << " steps\n";
172     uchar *bits = image.bits();
173     // First pass: get pivots and taking average
174     for (uint i = 0; i < STEPS ; i++) {
175         pivot[i] = bits[i*(bytes/STEPS)];
176         avg += pivot[i];
177     }
178     avg = avg / STEPS;
179     // Second Step: calculate delta (average?)
180     for (uint i = 0; i < STEPS; i++) {
181         int curdelta = abs(int(avg - pivot[i]));
182         delta += curdelta;
183     }
184     return delta / STEPS;
185 }
186
187 ThumbCreator::Flags WestleyPreview::flags() const {
188     return DrawFrame;
189 }
190
191 #include "westleypreview.moc"
192