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