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