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