]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
More work on timeline widget
[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 #include <qlabel.h>
37 #include <qthread.h>
38 #include <qapplication.h>
39
40
41
42 #include "renderer.h"
43 #include "kthumb.h"
44
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.setName(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( IO_WriteOnly )) {
68                         kdDebug()<<"++++++++  ERROR WRITING TO FILE: "<<f.name()<<endl;
69                         kdDebug()<<"++++++++  DISABLING AUDIO THUMBS"<<endl;
70                         KdenliveSettings::setAudiothumbnails(false);
71                         return;
72                 }
73                 m_isWorking = true;
74                 char *tmp = KRender::decodedString(m_url.path());
75                 Mlt::Producer m_producer(tmp);
76                 delete tmp;
77
78                 if (KdenliveSettings::normaliseaudiothumbs()) {
79                     Mlt::Filter m_convert("volume");
80                     m_convert.set("gain", "normalise");
81                     m_producer.attach(m_convert);
82                 }
83
84                 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                                 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(m_arrayWidth);
108                                                 for (uint i = 0; i < m_array.size(); i++){
109                                                         m_array[i] =  QABS((*( m_pcm + c + i * m_samples / m_array.size() ))>>8);
110                                                 }
111                                                 f.writeBlock(m_array);
112                                         }
113                                 } else{
114                                         f.writeBlock(QByteArray(m_arrayWidth));
115                                 }
116                                 if (mlt_frame)
117                                         delete mlt_frame;
118                 }
119                 f.close();
120                 m_isWorking = false;
121                 if (stop_me) {
122                     f.remove();
123                     QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
124                 }
125                 else QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(0, 10005));
126     }
127
128
129 #define _S(a)           (a)>255 ? 255 : (a)<0 ? 0 : (a)
130 #define _R(y,u,v) (0x2568*(y)                          + 0x3343*(u)) /0x2000
131 #define _G(y,u,v) (0x2568*(y) - 0x0c92*(v) - 0x1a1e*(u)) /0x2000
132 #define _B(y,u,v) (0x2568*(y) + 0x40cf*(v))                                          /0x2000
133 */
134 KThumb::KThumb(KUrl url, QObject * parent, const char *name):QObject(parent), m_url(url)
135 {
136 }
137
138 KThumb::~KThumb()
139 {
140     //if (thumbProducer.running ()) thumbProducer.exit();
141 }
142
143 void KThumb::extractImage(int frame, int frame2, int width, int height)
144 {
145     if (m_url.isEmpty()) return;
146     QPixmap pix(width, height);
147     char *tmp = Render::decodedString(m_url.path());
148     Mlt::Producer m_producer(tmp);
149     delete tmp;
150     pix.fill(Qt::black);
151
152     if (m_producer.is_blank()) {
153         emit thumbReady(frame, pix);
154         return;
155     }
156
157     mlt_image_format format = mlt_image_rgb24a;
158     Mlt::Filter m_convert("avcolour_space");
159     m_convert.set("forced", mlt_image_rgb24a);
160     m_producer.attach(m_convert);
161     m_producer.seek(frame);
162     Mlt::Frame * m_frame = m_producer.get_frame();
163
164     if (m_frame && m_frame->is_valid()) {
165       uint8_t *thumb = m_frame->get_image(format, width, height);
166       QImage image(thumb, width, height, QImage::Format_ARGB32);
167   
168       if (!image.isNull()) {
169         pix = pix.fromImage(image);
170       }
171       else pix.fill(Qt::black);
172     }
173     if (m_frame) delete m_frame;
174     emit thumbReady(frame, pix);
175
176     if (frame2 == -1) return;
177     m_producer.seek(frame2);
178     m_frame = m_producer.get_frame();
179     if (m_frame && m_frame->is_valid()) {
180       uint8_t *thumb = m_frame->get_image(format, width, height);
181       QImage image(thumb, width, height, QImage::Format_ARGB32);
182   
183       if (!image.isNull()) {
184         pix = pix.fromImage(image);
185       }
186       else pix.fill(Qt::black);
187     }
188     if (m_frame) delete m_frame;
189     emit thumbReady(frame2, pix);
190
191 }
192 /*
193 void KThumb::getImage(KUrl url, int frame, int width, int height)
194 {
195     if (url.isEmpty()) return;
196     QPixmap image(width, height);
197     char *tmp = KRender::decodedString(url.path());
198     Mlt::Producer m_producer(tmp);
199     delete tmp;
200     image.fill(Qt::black);
201
202     if (m_producer.is_blank()) {
203         emit thumbReady(frame, image);
204         return;
205     }
206     Mlt::Filter m_convert("avcolour_space");
207     m_convert.set("forced", mlt_image_rgb24a);
208     m_producer.attach(m_convert);
209     m_producer.seek(frame);
210     Mlt::Frame * m_frame = m_producer.get_frame();
211     mlt_image_format format = mlt_image_rgb24a;
212     width = width - 2;
213     height = height - 2;
214     if (m_frame && m_frame->is_valid()) {
215         uint8_t *thumb = m_frame->get_image(format, width, height);
216         QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
217         if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
218     }
219     if (m_frame) delete m_frame;
220     emit thumbReady(frame, image);
221 }
222
223 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
224 {
225     if (url.isEmpty()) return;
226     QPixmap image(width, height);
227     char *tmp = KRender::decodedString(url.path());
228     Mlt::Producer m_producer(tmp);
229     delete tmp;
230     image.fill(Qt::black);
231
232     if (m_producer.is_blank()) {
233         emit thumbReady(startframe, image);
234         emit thumbReady(endframe, image);
235         return;
236     }
237     Mlt::Filter m_convert("avcolour_space");
238     m_convert.set("forced", mlt_image_rgb24a);
239     m_producer.attach(m_convert);
240     m_producer.seek(startframe);
241     Mlt::Frame * m_frame = m_producer.get_frame();
242     mlt_image_format format = mlt_image_rgb24a;
243     width = width - 2;
244     height = height - 2;
245
246     if (m_frame && m_frame->is_valid()) {
247         uint8_t *thumb = m_frame->get_image(format, width, height);
248         QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
249         if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
250     }
251     if (m_frame) delete m_frame;
252     emit thumbReady(startframe, image);
253
254     image.fill(Qt::black);
255     m_producer.seek(endframe);
256     m_frame = m_producer.get_frame();
257
258     if (m_frame && m_frame->is_valid()) {
259         uint8_t *thumb = m_frame->get_image(format, width, height);
260         QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
261         if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
262     }
263     if (m_frame) delete m_frame;
264     emit thumbReady(endframe, image);
265 }
266
267 void KThumb::stopAudioThumbs()
268 {
269         if (thumbProducer.running ()) thumbProducer.stop_me = true;
270 }
271
272
273 void KThumb::removeAudioThumb()
274 {
275         if (m_thumbFile.isEmpty()) return;
276         stopAudioThumbs();
277         QFile f(m_thumbFile);
278         f.remove();
279 }
280
281 void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLength, int arrayWidth){
282         if ((thumbProducer.running () && thumbProducer.isWorking()) || channel == 0) {
283             return;
284         }
285
286         QMap <int, QMap <int, QByteArray> > storeIn;
287        //FIXME: Hardcoded!!! 
288         int m_frequency = 48000;
289         int m_channels = channel;
290         if (m_url != url) {
291                 m_url = url;
292                 KMD5 context ((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).ascii());
293                 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.hexDigest().data() + ".thumb";
294         }
295         QFile f(m_thumbFile);
296         if (f.open( IO_ReadOnly )) {
297                 QByteArray channelarray = f.readAll();
298                 f.close();
299                 if (channelarray.size() != arrayWidth*(frame+frameLength)*m_channels) {
300                         kdDebug()<<"--- BROKEN THUMB FOR: "<<m_url.filename()<<" ---------------------- "<<endl;
301                         f.remove();
302                         return;
303                 }
304                 
305                 for (int z=(int) frame;z<(int) (frame+frameLength);z++) {
306                         for (int c=0;c< m_channels;c++){
307                                 QByteArray m_array(arrayWidth);
308                                 for (int i = 0; i < arrayWidth; i++)
309                                         m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
310                                 storeIn[z][c] = m_array;
311                         }
312                 }
313                 emit audioThumbReady(storeIn);
314         }
315         else {
316                 if (thumbProducer.running()) return;
317                 thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
318                 thumbProducer.start(QThread::LowestPriority );
319         }
320 }
321 */
322
323