]> git.sesse.net Git - kdenlive/blob - src/kthumb.cpp
rewrote move and resize clip, video thumbs now update correctly
[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     Mlt::Frame * m_frame;
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     if (frame != -1) {
162       m_producer.seek(frame);
163       m_frame = m_producer.get_frame();
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         if (!image.isNull()) {
168           pix = pix.fromImage(image);
169         }
170         else pix.fill(Qt::black);
171       }
172       if (m_frame) delete m_frame;
173       emit thumbReady(frame, pix);
174     }
175     if (frame2 == -1) return;
176     m_producer.seek(frame2);
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     emit thumbReady(frame2, pix);
188
189 }
190 /*
191 void KThumb::getImage(KUrl url, int frame, int width, int height)
192 {
193     if (url.isEmpty()) return;
194     QPixmap image(width, height);
195     char *tmp = KRender::decodedString(url.path());
196     Mlt::Producer m_producer(tmp);
197     delete tmp;
198     image.fill(Qt::black);
199
200     if (m_producer.is_blank()) {
201         emit thumbReady(frame, image);
202         return;
203     }
204     Mlt::Filter m_convert("avcolour_space");
205     m_convert.set("forced", mlt_image_rgb24a);
206     m_producer.attach(m_convert);
207     m_producer.seek(frame);
208     Mlt::Frame * m_frame = m_producer.get_frame();
209     mlt_image_format format = mlt_image_rgb24a;
210     width = width - 2;
211     height = height - 2;
212     if (m_frame && m_frame->is_valid()) {
213         uint8_t *thumb = m_frame->get_image(format, width, height);
214         QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
215         if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
216     }
217     if (m_frame) delete m_frame;
218     emit thumbReady(frame, image);
219 }
220
221 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
222 {
223     if (url.isEmpty()) return;
224     QPixmap image(width, height);
225     char *tmp = KRender::decodedString(url.path());
226     Mlt::Producer m_producer(tmp);
227     delete tmp;
228     image.fill(Qt::black);
229
230     if (m_producer.is_blank()) {
231         emit thumbReady(startframe, image);
232         emit thumbReady(endframe, image);
233         return;
234     }
235     Mlt::Filter m_convert("avcolour_space");
236     m_convert.set("forced", mlt_image_rgb24a);
237     m_producer.attach(m_convert);
238     m_producer.seek(startframe);
239     Mlt::Frame * m_frame = m_producer.get_frame();
240     mlt_image_format format = mlt_image_rgb24a;
241     width = width - 2;
242     height = height - 2;
243
244     if (m_frame && m_frame->is_valid()) {
245         uint8_t *thumb = m_frame->get_image(format, width, height);
246         QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
247         if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
248     }
249     if (m_frame) delete m_frame;
250     emit thumbReady(startframe, image);
251
252     image.fill(Qt::black);
253     m_producer.seek(endframe);
254     m_frame = m_producer.get_frame();
255
256     if (m_frame && m_frame->is_valid()) {
257         uint8_t *thumb = m_frame->get_image(format, width, height);
258         QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
259         if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
260     }
261     if (m_frame) delete m_frame;
262     emit thumbReady(endframe, image);
263 }
264
265 void KThumb::stopAudioThumbs()
266 {
267         if (thumbProducer.running ()) thumbProducer.stop_me = true;
268 }
269
270
271 void KThumb::removeAudioThumb()
272 {
273         if (m_thumbFile.isEmpty()) return;
274         stopAudioThumbs();
275         QFile f(m_thumbFile);
276         f.remove();
277 }
278
279 void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLength, int arrayWidth){
280         if ((thumbProducer.running () && thumbProducer.isWorking()) || channel == 0) {
281             return;
282         }
283
284         QMap <int, QMap <int, QByteArray> > storeIn;
285        //FIXME: Hardcoded!!! 
286         int m_frequency = 48000;
287         int m_channels = channel;
288         if (m_url != url) {
289                 m_url = url;
290                 KMD5 context ((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).ascii());
291                 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.hexDigest().data() + ".thumb";
292         }
293         QFile f(m_thumbFile);
294         if (f.open( IO_ReadOnly )) {
295                 QByteArray channelarray = f.readAll();
296                 f.close();
297                 if (channelarray.size() != arrayWidth*(frame+frameLength)*m_channels) {
298                         kdDebug()<<"--- BROKEN THUMB FOR: "<<m_url.filename()<<" ---------------------- "<<endl;
299                         f.remove();
300                         return;
301                 }
302                 
303                 for (int z=(int) frame;z<(int) (frame+frameLength);z++) {
304                         for (int c=0;c< m_channels;c++){
305                                 QByteArray m_array(arrayWidth);
306                                 for (int i = 0; i < arrayWidth; i++)
307                                         m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
308                                 storeIn[z][c] = m_array;
309                         }
310                 }
311                 emit audioThumbReady(storeIn);
312         }
313         else {
314                 if (thumbProducer.running()) return;
315                 thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
316                 thumbProducer.start(QThread::LowestPriority );
317         }
318 }
319 */
320
321