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