]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
Fix timeline video thumbnail aspect ratio
[kdenlive] / src / kthumb.cpp
1 /***************************************************************************
2                         krender.cpp  -  description
3                            -------------------
4   begin                : Fri Nov 22 2002
5   copyright            : (C) 2002 by Jason Wood
6   email                : jasonwood@blueyonder.co.uk
7   copyright            : (C) 2005 Lcio Fl�io Corr�
8   email                : lucio.correa@gmail.com
9   copyright            : (C) Marco Gittler
10   email                : g.marco@freenet.de
11
12 ***************************************************************************/
13
14 /***************************************************************************
15  *                                                                         *
16  *   This program is free software; you can redistribute it and/or modify  *
17  *   it under the terms of the GNU General Public License as published by  *
18  *   the Free Software Foundation; either version 2 of the License, or     *
19  *   (at your option) any later version.                                   *
20  *                                                                         *
21  ***************************************************************************/
22
23 #include <kio/netaccess.h>
24 #include <kdebug.h>
25 #include <klocale.h>
26 #include <kdenlivesettings.h>
27 #include <kfileitem.h>
28 #include <kmessagebox.h>
29 #include <KStandardDirs>
30
31 #include <mlt++/Mlt.h>
32
33 #include <qxml.h>
34 #include <qimage.h>
35
36 #include <QThread>
37 #include <QApplication>
38 #include <QCryptographicHash>
39
40 #include "clipmanager.h"
41 #include "renderer.h"
42 #include "kthumb.h"
43 #include "kdenlivesettings.h"
44 #include "events.h"
45
46 void MyThread::init(QObject *parent, KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth) {
47     stop_me = false;
48     m_parent = parent;
49     m_isWorking = false;
50     f.setFileName(target);
51     m_url = url;
52     m_frame = frame;
53     m_frameLength = frameLength;
54     m_frequency = frequency;
55     m_channels = channels;
56     m_arrayWidth = arrayWidth;
57 }
58
59 bool MyThread::isWorking() {
60     return m_isWorking;
61 }
62
63 void MyThread::run() {
64
65     if (!f.open(QIODevice::WriteOnly)) {
66         kDebug() << "++++++++  ERROR WRITING TO FILE: " << f.fileName() << endl;
67         kDebug() << "++++++++  DISABLING AUDIO THUMBS" << endl;
68         KdenliveSettings::setAudiothumbnails(false);
69         return;
70     }
71     m_isWorking = true;
72     Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
73     Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
74
75
76     if (KdenliveSettings::normaliseaudiothumbs()) {
77         Mlt::Filter m_convert(prof, "volume");
78         m_convert.set("gain", "normalise");
79         m_producer.attach(m_convert);
80     }
81
82     //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
83
84     int last_val = 0;
85     int val = 0;
86     kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
87     for (int z = (int) m_frame;z < (int)(m_frame + m_frameLength) && m_producer.is_valid();z++) {
88         if (stop_me) break;
89         val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
90         if (last_val != val & val > 1) {
91             QApplication::postEvent(m_parent, new ProgressEvent(val, (QEvent::Type)10005));
92
93             last_val = val;
94         }
95         m_producer.seek(z);
96         Mlt::Frame *mlt_frame = m_producer.get_frame();
97         if (mlt_frame && mlt_frame->is_valid()) {
98             double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer());   //mlt_frame->get_double( "fps" );
99             int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
100             mlt_audio_format m_audioFormat = mlt_audio_pcm;
101
102             int16_t* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples);
103
104             for (int c = 0;c < m_channels;c++) {
105                 QByteArray m_array;
106                 m_array.resize(m_arrayWidth);
107                 for (uint i = 0; i < m_array.size(); i++) {
108                     m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
109                 }
110                 f.write(m_array);
111
112             }
113         } else {
114             f.write(QByteArray(m_arrayWidth, '\x00'));
115         }
116         if (mlt_frame)
117             delete mlt_frame;
118     }
119     kDebug() << "done";
120     f.close();
121     m_isWorking = false;
122     if (stop_me) {
123         f.remove();
124     }
125     QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
126
127 }
128
129
130 #define _S(a)           (a)>255 ? 255 : (a)<0 ? 0 : (a)
131 #define _R(y,u,v) (0x2568*(y)                          + 0x3343*(u)) /0x2000
132 #define _G(y,u,v) (0x2568*(y) - 0x0c92*(v) - 0x1a1e*(u)) /0x2000
133 #define _B(y,u,v) (0x2568*(y) + 0x40cf*(v))                                          /0x2000
134
135 KThumb::KThumb(ClipManager *clipManager, KUrl url, QObject * parent, const char *name): QObject(parent), m_clipManager(clipManager), m_url(url), m_producer(NULL), m_dar(1) {
136     QCryptographicHash context(QCryptographicHash::Sha1);
137     context.addData((KFileItem(m_url, "text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
138     m_thumbFile = KGlobal::dirs()->saveLocation("tmp" , "kdenlive") + context.result().toHex() + ".thumb";
139     kDebug() << "thumbfile=" << m_thumbFile;
140 }
141
142 KThumb::~KThumb() {
143     if (thumbProducer.isRunning()) thumbProducer.exit();
144 }
145
146 void KThumb::setProducer(Mlt::Producer *producer) {
147     m_producer = producer;
148     m_dar = producer->profile()->dar();
149 }
150
151 void KThumb::updateClipUrl(KUrl url) {
152     m_url = url;
153 }
154
155 //static
156 QPixmap KThumb::getImage(KUrl url, int width, int height) {
157     if (url.isEmpty()) return QPixmap();
158     return getImage(url, 0, width, height);
159 }
160
161 void KThumb::extractImage(int frame, int frame2) {
162     if (m_url.isEmpty()) return;
163     if (m_producer == NULL) return;
164     /*char *tmp = Render::decodedString("<westley><playlist><producer resource=\"" + m_url.path() + "\" /></playlist></westley>");
165     Mlt::Producer producer(*m_profile, "westley-xml", tmp);
166     delete[] tmp;*/
167
168     int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
169     if (m_producer->is_blank()) {
170         QPixmap pix(twidth, KdenliveSettings::trackheight());
171         pix.fill(Qt::black);
172         emit thumbReady(frame, pix);
173         return;
174     }
175     if (frame != -1) {
176         QPixmap pix = getFrame(*m_producer, frame, twidth, KdenliveSettings::trackheight());
177         emit thumbReady(frame, pix);
178     }
179     if (frame2 != -1) {
180         QPixmap pix = getFrame(*m_producer, frame2, twidth , KdenliveSettings::trackheight());
181         emit thumbReady(frame2, pix);
182     }
183 }
184
185 //static
186 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height) {
187     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
188     QPixmap pix(width, height);
189     if (url.isEmpty()) return pix;
190
191     char *tmp = Render::decodedString("<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
192     Mlt::Producer producer(profile, "westley-xml", tmp);
193     delete[] tmp;
194
195     if (producer.is_blank()) {
196
197         pix.fill(Qt::black);
198         return pix;
199     }
200     return getFrame(producer, frame, width, height);
201 }
202
203 //static
204 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
205     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
206     QPixmap pix(width, height);
207     QDomDocument doc;
208     QDomElement westley = doc.createElement("westley");
209     QDomElement play = doc.createElement("playlist");
210     doc.appendChild(westley);
211     westley.appendChild(play);
212     play.appendChild(doc.importNode(xml, true));
213     char *tmp = Render::decodedString(doc.toString());
214     Mlt::Producer producer(profile, "westley-xml", tmp);
215     delete[] tmp;
216
217     if (producer.is_blank()) {
218         pix.fill(Qt::black);
219         return pix;
220     }
221     return getFrame(producer, frame, width, height);
222 }
223
224 //static
225 QPixmap KThumb::getFrame(Mlt::Producer producer, int framepos, int width, int height) {
226
227     producer.seek(framepos);
228     Mlt::Frame *frame = producer.get_frame();
229     mlt_image_format format = mlt_image_yuv422;
230     int frame_width = 0;
231     int frame_height = 0;
232     frame->set("normalised_height", height);
233     frame->set("normalised_width", width);
234     QPixmap pix(width, height);
235
236     uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
237     uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
238     mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
239     QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
240
241     if (!image.isNull()) {
242         pix = pix.fromImage(image.rgbSwapped());
243     } else
244         pix.fill(Qt::black);
245
246     mlt_pool_release(new_image);
247     delete frame;
248     return pix;
249 }
250 /*
251 void KThumb::getImage(KUrl url, int frame, int width, int height)
252 {
253     if (url.isEmpty()) return;
254     QPixmap image(width, height);
255     char *tmp = KRender::decodedString(url.path());
256     Mlt::Producer m_producer(tmp);
257     delete tmp;
258     image.fill(Qt::black);
259
260     if (m_producer.is_blank()) {
261  emit thumbReady(frame, image);
262  return;
263     }
264     Mlt::Filter m_convert("avcolour_space");
265     m_convert.set("forced", mlt_image_rgb24a);
266     m_producer.attach(m_convert);
267     m_producer.seek(frame);
268     Mlt::Frame * m_frame = m_producer.get_frame();
269     mlt_image_format format = mlt_image_rgb24a;
270     width = width - 2;
271     height = height - 2;
272     if (m_frame && m_frame->is_valid()) {
273      uint8_t *thumb = m_frame->get_image(format, width, height);
274      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
275      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
276     }
277     if (m_frame) delete m_frame;
278     emit thumbReady(frame, image);
279 }
280
281 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
282 {
283     if (url.isEmpty()) return;
284     QPixmap image(width, height);
285     char *tmp = KRender::decodedString(url.path());
286     Mlt::Producer m_producer(tmp);
287     delete tmp;
288     image.fill(Qt::black);
289
290     if (m_producer.is_blank()) {
291  emit thumbReady(startframe, image);
292  emit thumbReady(endframe, image);
293  return;
294     }
295     Mlt::Filter m_convert("avcolour_space");
296     m_convert.set("forced", mlt_image_rgb24a);
297     m_producer.attach(m_convert);
298     m_producer.seek(startframe);
299     Mlt::Frame * m_frame = m_producer.get_frame();
300     mlt_image_format format = mlt_image_rgb24a;
301     width = width - 2;
302     height = height - 2;
303
304     if (m_frame && m_frame->is_valid()) {
305      uint8_t *thumb = m_frame->get_image(format, width, height);
306      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
307      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
308     }
309     if (m_frame) delete m_frame;
310     emit thumbReady(startframe, image);
311
312     image.fill(Qt::black);
313     m_producer.seek(endframe);
314     m_frame = m_producer.get_frame();
315
316     if (m_frame && m_frame->is_valid()) {
317      uint8_t *thumb = m_frame->get_image(format, width, height);
318      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
319      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
320     }
321     if (m_frame) delete m_frame;
322     emit thumbReady(endframe, image);
323 }
324 */
325 void KThumb::stopAudioThumbs() {
326     if (thumbProducer.isRunning()) thumbProducer.stop_me = true;
327 }
328
329
330 void KThumb::removeAudioThumb() {
331     if (m_thumbFile.isEmpty()) return;
332     stopAudioThumbs();
333     QFile f(m_thumbFile);
334     f.remove();
335 }
336
337 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
338
339     if ((thumbProducer.isRunning() && thumbProducer.isWorking()) || channel == 0) {
340         return;
341     }
342
343     QMap <int, QMap <int, QByteArray> > storeIn;
344     //FIXME: Hardcoded!!!
345     int m_frequency = 48000;
346     int m_channels = channel;
347
348     QFile f(m_thumbFile);
349     if (f.open(QIODevice::ReadOnly)) {
350         QByteArray channelarray = f.readAll();
351         f.close();
352         if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
353             kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
354             f.remove();
355             return;
356         }
357         kDebug() << "reading audio thumbs from file";
358         for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
359             for (int c = 0;c < m_channels;c++) {
360                 QByteArray m_array(arrayWidth, '\x00');
361                 for (int i = 0; i < arrayWidth; i++)
362                     m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
363                 storeIn[z][c] = m_array;
364             }
365         }
366         emit audioThumbReady(storeIn);
367     } else {
368         if (thumbProducer.isRunning()) return;
369         thumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
370         thumbProducer.start(QThread::LowestPriority);
371         kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
372     }
373 }
374
375 void KThumb::customEvent(QEvent * event) {
376     if (event->type() == 10005) {
377         ProgressEvent* p = (ProgressEvent*) event;
378         m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), p->value());
379     }
380 }
381
382
383
384 #include "kthumb.moc"
385