]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
cleanup
[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 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) {
130     QCryptographicHash context(QCryptographicHash::Sha1);
131     context.addData((KFileItem(m_url, "text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
132     m_thumbFile = KGlobal::dirs()->saveLocation("tmp" , "kdenlive") + context.result().toHex() + ".thumb";
133     kDebug() << "thumbfile=" << m_thumbFile;
134 }
135
136 KThumb::~KThumb() {
137     if (thumbProducer.isRunning()) thumbProducer.exit();
138 }
139
140 void KThumb::setProducer(Mlt::Producer *producer) {
141     m_producer = producer;
142     m_dar = producer->profile()->dar();
143 }
144
145 void KThumb::updateClipUrl(KUrl url) {
146     m_url = url;
147 }
148
149 //static
150 QPixmap KThumb::getImage(KUrl url, int width, int height) {
151     if (url.isEmpty()) return QPixmap();
152     return getImage(url, 0, width, height);
153 }
154
155 void KThumb::extractImage(int frame, int frame2) {
156     if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
157
158     int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
159     if (m_producer->is_blank()) {
160         QPixmap pix(twidth, KdenliveSettings::trackheight());
161         pix.fill(Qt::black);
162         emit thumbReady(frame, pix);
163         return;
164     }
165     if (frame != -1) {
166         QPixmap pix = getFrame(m_producer, frame, twidth, KdenliveSettings::trackheight());
167         emit thumbReady(frame, pix);
168     }
169     if (frame2 != -1) {
170         QPixmap pix = getFrame(m_producer, frame2, twidth , KdenliveSettings::trackheight());
171         emit thumbReady(frame2, pix);
172     }
173 }
174
175 QPixmap KThumb::extractImage(int frame, int width, int height) {
176     return getFrame(m_producer, frame, width, height);
177 }
178
179 //static
180 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height) {
181     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
182     QPixmap pix(width, height);
183     if (url.isEmpty()) return pix;
184
185     char *tmp = Render::decodedString(url.path());
186     //"<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
187     //Mlt::Producer producer(profile, "westley-xml", tmp);
188     Mlt::Producer *producer = new Mlt::Producer(profile, tmp);
189     delete[] tmp;
190
191     if (producer->is_blank()) {
192         pix.fill(Qt::black);
193         delete producer;
194         return pix;
195     }
196     pix = getFrame(producer, frame, width, height);
197     delete producer;
198     return pix;
199 }
200
201 //static
202 /*
203 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
204     Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
205     QPixmap pix(width, height);
206     QDomDocument doc;
207     QDomElement westley = doc.createElement("westley");
208     QDomElement play = doc.createElement("playlist");
209     doc.appendChild(westley);
210     westley.appendChild(play);
211     play.appendChild(doc.importNode(xml, true));
212     char *tmp = Render::decodedString(doc.toString());
213     Mlt::Producer producer(profile, "westley-xml", tmp);
214     delete[] tmp;
215
216     if (producer.is_blank()) {
217         pix.fill(Qt::black);
218         return pix;
219     }
220     return getFrame(producer, frame, width, height);
221 }*/
222
223 //static
224 QPixmap KThumb::getFrame(Mlt::Producer *producer, int framepos, int width, int height) {
225     kDebug() << "//REQUESTING FRAME: " << framepos;
226     if (producer == NULL) return QPixmap();
227     producer->seek(framepos);
228     Mlt::Frame *frame = producer->get_frame();
229     if (!frame) {
230         kDebug() << "///// BROKEN FRAME";
231         return QPixmap();
232     }
233     kDebug() << "///// FRAME exists";
234     mlt_image_format format = mlt_image_yuv422;
235     int frame_width = 0;
236     int frame_height = 0;
237     frame->set("normalised_height", height);
238     frame->set("normalised_width", width);
239     QPixmap pix(width, height);
240     kDebug() << "///// FRAME exists 2";
241     uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
242     kDebug() << "///// FRAME exists 2a";
243     uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
244     kDebug() << "///// FRAME exists 2b";
245     mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
246     kDebug() << "///// FRAME exists 2c";
247     QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
248     kDebug() << "///// FRAME exists 3";
249     if (!image.isNull()) {
250         pix = pix.fromImage(image.rgbSwapped());
251     } else
252         pix.fill(Qt::black);
253
254     mlt_pool_release(new_image);
255     delete frame;
256     kDebug() << "//REQUESTING FRAME " << framepos << " OVER";
257     return pix;
258 }
259 /*
260 void KThumb::getImage(KUrl url, int frame, int width, int height)
261 {
262     if (url.isEmpty()) return;
263     QPixmap image(width, height);
264     char *tmp = KRender::decodedString(url.path());
265     Mlt::Producer m_producer(tmp);
266     delete tmp;
267     image.fill(Qt::black);
268
269     if (m_producer.is_blank()) {
270  emit thumbReady(frame, image);
271  return;
272     }
273     Mlt::Filter m_convert("avcolour_space");
274     m_convert.set("forced", mlt_image_rgb24a);
275     m_producer.attach(m_convert);
276     m_producer.seek(frame);
277     Mlt::Frame * m_frame = m_producer.get_frame();
278     mlt_image_format format = mlt_image_rgb24a;
279     width = width - 2;
280     height = height - 2;
281     if (m_frame && m_frame->is_valid()) {
282      uint8_t *thumb = m_frame->get_image(format, width, height);
283      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
284      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
285     }
286     if (m_frame) delete m_frame;
287     emit thumbReady(frame, image);
288 }
289
290 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
291 {
292     if (url.isEmpty()) return;
293     QPixmap image(width, height);
294     char *tmp = KRender::decodedString(url.path());
295     Mlt::Producer m_producer(tmp);
296     delete tmp;
297     image.fill(Qt::black);
298
299     if (m_producer.is_blank()) {
300  emit thumbReady(startframe, image);
301  emit thumbReady(endframe, image);
302  return;
303     }
304     Mlt::Filter m_convert("avcolour_space");
305     m_convert.set("forced", mlt_image_rgb24a);
306     m_producer.attach(m_convert);
307     m_producer.seek(startframe);
308     Mlt::Frame * m_frame = m_producer.get_frame();
309     mlt_image_format format = mlt_image_rgb24a;
310     width = width - 2;
311     height = height - 2;
312
313     if (m_frame && m_frame->is_valid()) {
314      uint8_t *thumb = m_frame->get_image(format, width, height);
315      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
316      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
317     }
318     if (m_frame) delete m_frame;
319     emit thumbReady(startframe, image);
320
321     image.fill(Qt::black);
322     m_producer.seek(endframe);
323     m_frame = m_producer.get_frame();
324
325     if (m_frame && m_frame->is_valid()) {
326      uint8_t *thumb = m_frame->get_image(format, width, height);
327      QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
328      if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
329     }
330     if (m_frame) delete m_frame;
331     emit thumbReady(endframe, image);
332 }
333 */
334 void KThumb::stopAudioThumbs() {
335     if (thumbProducer.isRunning()) thumbProducer.stop_me = true;
336 }
337
338
339 void KThumb::removeAudioThumb() {
340     if (m_thumbFile.isEmpty()) return;
341     stopAudioThumbs();
342     QFile f(m_thumbFile);
343     f.remove();
344 }
345
346 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
347
348     if ((thumbProducer.isRunning() && thumbProducer.isWorking()) || channel == 0) {
349         return;
350     }
351
352     QMap <int, QMap <int, QByteArray> > storeIn;
353     //FIXME: Hardcoded!!!
354     int m_frequency = 48000;
355     int m_channels = channel;
356
357     QFile f(m_thumbFile);
358     if (f.open(QIODevice::ReadOnly)) {
359         QByteArray channelarray = f.readAll();
360         f.close();
361         if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
362             kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
363             f.remove();
364             return;
365         }
366         kDebug() << "reading audio thumbs from file";
367         for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
368             for (int c = 0;c < m_channels;c++) {
369                 QByteArray m_array(arrayWidth, '\x00');
370                 for (int i = 0; i < arrayWidth; i++)
371                     m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
372                 storeIn[z][c] = m_array;
373             }
374         }
375         emit audioThumbReady(storeIn);
376     } else {
377         if (thumbProducer.isRunning()) return;
378         thumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
379         thumbProducer.start(QThread::LowestPriority);
380         kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
381     }
382 }
383
384 void KThumb::customEvent(QEvent * event) {
385     if (event->type() == 10005) {
386         ProgressEvent* p = (ProgressEvent*) event;
387         m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), p->value());
388     }
389 }
390
391
392
393 #include "kthumb.moc"
394