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