]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
audio thumb drawn
[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("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                 f.close();
124                 m_isWorking = false;
125                 if (stop_me) {
126                     f.remove();
127                    //TODO  QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
128                 }
129                 //TODO else QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(0, 10005));
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(KUrl url, int width, int height, QObject * parent, const char *name):QObject(parent), m_url(url), m_width(width), m_height(height)
139 {
140   kDebug()<<"+++++++++++  CREATING THMB PROD FOR: "<<url;
141   m_profile = new Mlt::Profile((char*) qstrdup(KdenliveSettings::current_profile().toUtf8()));
142 }
143
144 KThumb::~KThumb()
145 {
146     if (m_profile) delete m_profile;
147     if (thumbProducer.isRunning ()) thumbProducer.exit();
148 }
149
150
151 //static
152 QPixmap KThumb::getImage(KUrl url, int width, int height)
153 {
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       }
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 {
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         }
212         else pix.fill(Qt::black);
213       }
214       if (m_frame) delete m_frame;
215       emit thumbReady(frame, pix);
216     }
217     if (frame2 == -1) return;
218     m_producer.seek(frame2);
219     m_frame = m_producer.get_frame();
220     if (m_frame && m_frame->is_valid()) {
221       uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
222       QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
223       if (!image.isNull()) {
224         pix = pix.fromImage(image);
225       }
226       else pix.fill(Qt::black);
227     }
228     if (m_frame) delete m_frame;
229     emit thumbReady(frame2, pix);
230
231 }
232 /*
233 void KThumb::getImage(KUrl url, int frame, int width, int height)
234 {
235     if (url.isEmpty()) return;
236     QPixmap image(width, height);
237     char *tmp = KRender::decodedString(url.path());
238     Mlt::Producer m_producer(tmp);
239     delete tmp;
240     image.fill(Qt::black);
241
242     if (m_producer.is_blank()) {
243         emit thumbReady(frame, image);
244         return;
245     }
246     Mlt::Filter m_convert("avcolour_space");
247     m_convert.set("forced", mlt_image_rgb24a);
248     m_producer.attach(m_convert);
249     m_producer.seek(frame);
250     Mlt::Frame * m_frame = m_producer.get_frame();
251     mlt_image_format format = mlt_image_rgb24a;
252     width = width - 2;
253     height = height - 2;
254     if (m_frame && m_frame->is_valid()) {
255         uint8_t *thumb = m_frame->get_image(format, width, height);
256         QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
257         if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
258     }
259     if (m_frame) delete m_frame;
260     emit thumbReady(frame, image);
261 }
262
263 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
264 {
265     if (url.isEmpty()) return;
266     QPixmap image(width, height);
267     char *tmp = KRender::decodedString(url.path());
268     Mlt::Producer m_producer(tmp);
269     delete tmp;
270     image.fill(Qt::black);
271
272     if (m_producer.is_blank()) {
273         emit thumbReady(startframe, image);
274         emit thumbReady(endframe, image);
275         return;
276     }
277     Mlt::Filter m_convert("avcolour_space");
278     m_convert.set("forced", mlt_image_rgb24a);
279     m_producer.attach(m_convert);
280     m_producer.seek(startframe);
281     Mlt::Frame * m_frame = m_producer.get_frame();
282     mlt_image_format format = mlt_image_rgb24a;
283     width = width - 2;
284     height = height - 2;
285
286     if (m_frame && m_frame->is_valid()) {
287         uint8_t *thumb = m_frame->get_image(format, width, height);
288         QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
289         if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
290     }
291     if (m_frame) delete m_frame;
292     emit thumbReady(startframe, image);
293
294     image.fill(Qt::black);
295     m_producer.seek(endframe);
296     m_frame = m_producer.get_frame();
297
298     if (m_frame && m_frame->is_valid()) {
299         uint8_t *thumb = m_frame->get_image(format, width, height);
300         QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
301         if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
302     }
303     if (m_frame) delete m_frame;
304     emit thumbReady(endframe, image);
305 }
306 */
307 void KThumb::stopAudioThumbs()
308 {
309         if (thumbProducer.isRunning ()) thumbProducer.stop_me = true;
310 }
311
312
313 void KThumb::removeAudioThumb()
314 {
315         if (m_thumbFile.isEmpty()) return;
316         stopAudioThumbs();
317         QFile f(m_thumbFile);
318         f.remove();
319 }
320
321 void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLength, int arrayWidth){
322         
323         if ((thumbProducer.isRunning () && thumbProducer.isWorking()) || channel == 0) {
324             return;
325         }
326         
327         QMap <int, QMap <int, QByteArray> > storeIn;
328        //FIXME: Hardcoded!!! 
329         int m_frequency = 48000;
330         int m_channels = channel;
331         m_thumbFile="/tmp/testfile";
332         if (m_url != url) {
333                 m_url = url;
334                 QCryptographicHash context(QCryptographicHash::Sha1);
335                 context.addData((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
336                 
337                 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.result().toHex() + ".thumb";
338                 
339         }
340         
341         QFile f(m_thumbFile);
342         if (f.open( QIODevice::ReadOnly )) {
343                 QByteArray channelarray = f.readAll();
344                 f.close();
345                 if (channelarray.size() != arrayWidth*(frame+frameLength)*m_channels) {
346                         kDebug()<<"--- BROKEN THUMB FOR: "<<m_url.fileName()<<" ---------------------- "<<endl;
347                         f.remove();
348                         return;
349                 }
350                 kDebug() << "reading audio thumbs from file";
351                 for (int z=(int) frame;z<(int) (frame+frameLength);z++) {
352                         for (int c=0;c< m_channels;c++){
353                                 QByteArray m_array(arrayWidth,'\x00');
354                                 for (int i = 0; i < arrayWidth; i++)
355                                         m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
356                                 storeIn[z][c] = m_array;
357                         }
358                 }
359                 emit audioThumbReady(storeIn);
360         }
361         else {
362                 if (thumbProducer.isRunning()) return;
363                 thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
364                 thumbProducer.start(QThread::LowestPriority );
365                 
366         }
367 }
368
369
370