]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
f778a853c1a7cd73e577b96db4e819236ba251c5
[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                  if (!f.open( QIODevice::WriteOnly )) {
68                         kDebug()<<"++++++++  ERROR WRITING TO FILE: "<<f.fileName()<<endl;
69                         kDebug()<<"++++++++  DISABLING AUDIO THUMBS"<<endl;
70                         //TODO KdenliveSettings::setAudiothumbnails(false);
71                         return;
72                 }
73                 m_isWorking = true;
74                 Mlt::Profile prof((char*) KdenliveSettings::current_profile().data());
75                 Mlt::Producer m_producer(prof, m_url.path().toAscii().data());
76                 
77
78                 /*TODO if (KdenliveSettings::normaliseaudiothumbs()) {
79                     Mlt::Filter m_convert("volume");
80                     m_convert.set("gain", "normalise");
81                     m_producer.attach(m_convert);
82                 }*/
83
84                 /*TODO if (qApp->mainWidget()) 
85                     QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
86         */
87                 int last_val = 0;
88                 int val = 0;
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                                 //TODO QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(val, 10005));
94                                 last_val=val;
95                         }
96                                 m_producer.seek( z );
97                                 Mlt::Frame *mlt_frame = m_producer.get_frame();
98                                 if ( mlt_frame->is_valid() )
99                                 {
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] =  qAbs((*( m_pcm + c + i * m_samples / m_array.size() ))>>8);
111                                                 }
112                                                 f.write(m_array);
113                                         }
114                                 } else{
115                                         f.write(QByteArray(m_arrayWidth,'\x00'));
116                                 }
117                                 if (mlt_frame)
118                                         delete mlt_frame;
119                 }
120                 f.close();
121                 m_isWorking = false;
122                 if (stop_me) {
123                     f.remove();
124                    //TODO  QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
125                 }
126                 //TODO else QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(0, 10005));
127     }
128
129
130 #define _S(a)           (a)>255 ? 255 : (a)<0 ? 0 : (a)
131 #define _R(y,u,v) (0x2568*(y)                          + 0x3343*(u)) /0x2000
132 #define _G(y,u,v) (0x2568*(y) - 0x0c92*(v) - 0x1a1e*(u)) /0x2000
133 #define _B(y,u,v) (0x2568*(y) + 0x40cf*(v))                                          /0x2000
134
135 KThumb::KThumb(KUrl url, int width, int height, QObject * parent, const char *name):QObject(parent), m_url(url), m_width(width), m_height(height)
136 {
137   kDebug()<<"+++++++++++  CREATING THMB PROD FOR: "<<url;
138   m_profile = new Mlt::Profile((char*) qstrdup(KdenliveSettings::current_profile().toUtf8()));
139 }
140
141 KThumb::~KThumb()
142 {
143     if (m_profile) delete m_profile;
144     //if (thumbProducer.running ()) thumbProducer.exit();
145 }
146
147
148 //static
149 QPixmap KThumb::getImage(KUrl url, int width, int height)
150 {
151     if (url.isEmpty()) return QPixmap();
152     QPixmap pix(width, height);
153   kDebug()<<"+++++++++++  GET THMB IMG FOR: "<<url;
154     char *tmp = Render::decodedString(url.path());
155     Mlt::Profile prof((char*) KdenliveSettings::current_profile().data());
156     Mlt::Producer m_producer(prof, tmp);
157     delete tmp;
158
159     if (m_producer.is_blank()) {
160         pix.fill(Qt::black);
161         return pix;
162     }
163     Mlt::Frame * m_frame;
164     mlt_image_format format = mlt_image_rgb24a;
165     Mlt::Filter m_convert(prof, "avcolour_space");
166     m_convert.set("forced", mlt_image_rgb24a);
167     m_producer.attach(m_convert);
168     //m_producer.seek(frame);
169     m_frame = m_producer.get_frame();
170     if (m_frame && m_frame->is_valid()) {
171       uint8_t *thumb = m_frame->get_image(format, width, height);
172       QImage image(thumb, width, height, QImage::Format_ARGB32);
173       if (!image.isNull()) {
174         pix = pix.fromImage(image);
175       }
176       else pix.fill(Qt::black);
177     }
178     if (m_frame) delete m_frame;
179     return pix;
180 }
181
182 void KThumb::extractImage(int frame, int frame2)
183 {
184     if (m_url.isEmpty()) return;
185     QPixmap pix(m_width, m_height);
186     char *tmp = Render::decodedString(m_url.path());
187     Mlt::Producer m_producer(*m_profile, tmp);
188     delete tmp;
189
190     if (m_producer.is_blank()) {
191         pix.fill(Qt::black);
192         emit thumbReady(frame, pix);
193         return;
194     }
195     Mlt::Frame * m_frame;
196     mlt_image_format format = mlt_image_rgb24a;
197     Mlt::Filter m_convert(*m_profile, "avcolour_space");
198     m_convert.set("forced", mlt_image_rgb24a);
199     m_producer.attach(m_convert);
200     if (frame != -1) {
201       m_producer.seek(frame);
202       m_frame = m_producer.get_frame();
203       if (m_frame && m_frame->is_valid()) {
204         uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
205         QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
206         if (!image.isNull()) {
207           pix = pix.fromImage(image);
208         }
209         else pix.fill(Qt::black);
210       }
211       if (m_frame) delete m_frame;
212       emit thumbReady(frame, pix);
213     }
214     if (frame2 == -1) return;
215     m_producer.seek(frame2);
216     m_frame = m_producer.get_frame();
217     if (m_frame && m_frame->is_valid()) {
218       uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
219       QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
220       if (!image.isNull()) {
221         pix = pix.fromImage(image);
222       }
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 {
306         if (thumbProducer.isRunning ()) thumbProducer.stop_me = true;
307 }
308
309
310 void KThumb::removeAudioThumb()
311 {
312         if (m_thumbFile.isEmpty()) return;
313         stopAudioThumbs();
314         QFile f(m_thumbFile);
315         f.remove();
316 }
317
318 void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLength, int arrayWidth){
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         if (m_url != url) {
328                 m_url = url;
329                 QCryptographicHash context(QCryptographicHash::Sha1);
330                 context.addData((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
331                 
332                 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.result().toHex() + ".thumb";
333         }
334         QFile f(m_thumbFile);
335         if (f.open( QIODevice::ReadOnly )) {
336                 QByteArray channelarray = f.readAll();
337                 f.close();
338                 if (channelarray.size() != arrayWidth*(frame+frameLength)*m_channels) {
339                         kDebug()<<"--- BROKEN THUMB FOR: "<<m_url.fileName()<<" ---------------------- "<<endl;
340                         f.remove();
341                         return;
342                 }
343                 
344                 for (int z=(int) frame;z<(int) (frame+frameLength);z++) {
345                         for (int c=0;c< m_channels;c++){
346                                 QByteArray m_array;
347                                 m_array.resize(arrayWidth);
348                                 for (int i = 0; i < arrayWidth; i++)
349                                         m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
350                                 storeIn[z][c] = m_array;
351                         }
352                 }
353                 emit audioThumbReady(storeIn);
354         }
355         else {
356                 if (thumbProducer.isRunning()) return;
357                 thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
358                 thumbProducer.start(QThread::LowestPriority );
359         }
360 }
361
362
363