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