1 /***************************************************************************
2 krender.cpp - description
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
12 ***************************************************************************/
14 /***************************************************************************
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. *
21 ***************************************************************************/
23 #include <qapplication.h>
25 #include <kio/netaccess.h>
28 #include <kdenlivesettings.h>
29 #include <kfileitem.h>
30 #include <kmessagebox.h>
32 #include <mlt++/Mlt.h>
38 #include <qapplication.h>
46 void MyThread::init(KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth)
53 m_frameLength = frameLength;
54 m_frequency = frequency;
55 m_channels = channels;
56 m_arrayWidth = arrayWidth;
60 bool MyThread::isWorking()
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);
74 char *tmp = KRender::decodedString(m_url.path());
75 Mlt::Producer m_producer(tmp);
78 if (KdenliveSettings::normaliseaudiothumbs()) {
79 Mlt::Filter m_convert("volume");
80 m_convert.set("gain", "normalise");
81 m_producer.attach(m_convert);
84 if (qApp->mainWidget())
85 QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
89 for (int z=(int) m_frame;z<(int) (m_frame+m_frameLength) && m_producer.is_valid();z++){
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));
97 Mlt::Frame *mlt_frame = m_producer.get_frame();
98 if ( mlt_frame->is_valid() )
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;
104 int16_t* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples );
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);
111 f.writeBlock(m_array);
114 f.writeBlock(QByteArray(m_arrayWidth));
123 QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
125 else QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(0, 10005));
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
134 KThumb::KThumb(KUrl url, QObject * parent, const char *name):QObject(parent), m_url(url)
140 //if (thumbProducer.running ()) thumbProducer.exit();
145 QPixmap KThumb::getImage(KUrl url, int width, int height)
147 if (url.isEmpty()) return QPixmap();
148 QPixmap pix(width, height);
149 char *tmp = Render::decodedString(url.path());
150 Mlt::Producer m_producer(tmp);
153 if (m_producer.is_blank()) {
157 Mlt::Frame * m_frame;
158 mlt_image_format format = mlt_image_rgb24a;
159 Mlt::Filter m_convert("avcolour_space");
160 m_convert.set("forced", mlt_image_rgb24a);
161 m_producer.attach(m_convert);
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);
170 else pix.fill(Qt::black);
172 if (m_frame) delete m_frame;
176 void KThumb::extractImage(int frame, int frame2, int width, int height)
178 if (m_url.isEmpty()) return;
179 QPixmap pix(width, height);
180 char *tmp = Render::decodedString(m_url.path());
181 Mlt::Producer m_producer(tmp);
184 if (m_producer.is_blank()) {
186 emit thumbReady(frame, pix);
189 Mlt::Frame * m_frame;
190 mlt_image_format format = mlt_image_rgb24a;
191 Mlt::Filter m_convert("avcolour_space");
192 m_convert.set("forced", mlt_image_rgb24a);
193 m_producer.attach(m_convert);
195 m_producer.seek(frame);
196 m_frame = m_producer.get_frame();
197 if (m_frame && m_frame->is_valid()) {
198 uint8_t *thumb = m_frame->get_image(format, width, height);
199 QImage image(thumb, width, height, QImage::Format_ARGB32);
200 if (!image.isNull()) {
201 pix = pix.fromImage(image);
203 else pix.fill(Qt::black);
205 if (m_frame) delete m_frame;
206 emit thumbReady(frame, pix);
208 if (frame2 == -1) return;
209 m_producer.seek(frame2);
210 m_frame = m_producer.get_frame();
211 if (m_frame && m_frame->is_valid()) {
212 uint8_t *thumb = m_frame->get_image(format, width, height);
213 QImage image(thumb, width, height, QImage::Format_ARGB32);
214 if (!image.isNull()) {
215 pix = pix.fromImage(image);
217 else pix.fill(Qt::black);
219 if (m_frame) delete m_frame;
220 emit thumbReady(frame2, pix);
224 void KThumb::getImage(KUrl url, int frame, int width, int height)
226 if (url.isEmpty()) return;
227 QPixmap image(width, height);
228 char *tmp = KRender::decodedString(url.path());
229 Mlt::Producer m_producer(tmp);
231 image.fill(Qt::black);
233 if (m_producer.is_blank()) {
234 emit thumbReady(frame, image);
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(frame);
241 Mlt::Frame * m_frame = m_producer.get_frame();
242 mlt_image_format format = mlt_image_rgb24a;
245 if (m_frame && m_frame->is_valid()) {
246 uint8_t *thumb = m_frame->get_image(format, width, height);
247 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
248 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
250 if (m_frame) delete m_frame;
251 emit thumbReady(frame, image);
254 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
256 if (url.isEmpty()) return;
257 QPixmap image(width, height);
258 char *tmp = KRender::decodedString(url.path());
259 Mlt::Producer m_producer(tmp);
261 image.fill(Qt::black);
263 if (m_producer.is_blank()) {
264 emit thumbReady(startframe, image);
265 emit thumbReady(endframe, image);
268 Mlt::Filter m_convert("avcolour_space");
269 m_convert.set("forced", mlt_image_rgb24a);
270 m_producer.attach(m_convert);
271 m_producer.seek(startframe);
272 Mlt::Frame * m_frame = m_producer.get_frame();
273 mlt_image_format format = mlt_image_rgb24a;
277 if (m_frame && m_frame->is_valid()) {
278 uint8_t *thumb = m_frame->get_image(format, width, height);
279 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
280 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
282 if (m_frame) delete m_frame;
283 emit thumbReady(startframe, image);
285 image.fill(Qt::black);
286 m_producer.seek(endframe);
287 m_frame = m_producer.get_frame();
289 if (m_frame && m_frame->is_valid()) {
290 uint8_t *thumb = m_frame->get_image(format, width, height);
291 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
292 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
294 if (m_frame) delete m_frame;
295 emit thumbReady(endframe, image);
298 void KThumb::stopAudioThumbs()
300 if (thumbProducer.running ()) thumbProducer.stop_me = true;
304 void KThumb::removeAudioThumb()
306 if (m_thumbFile.isEmpty()) return;
308 QFile f(m_thumbFile);
312 void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLength, int arrayWidth){
313 if ((thumbProducer.running () && thumbProducer.isWorking()) || channel == 0) {
317 QMap <int, QMap <int, QByteArray> > storeIn;
318 //FIXME: Hardcoded!!!
319 int m_frequency = 48000;
320 int m_channels = channel;
323 KMD5 context ((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).ascii());
324 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.hexDigest().data() + ".thumb";
326 QFile f(m_thumbFile);
327 if (f.open( IO_ReadOnly )) {
328 QByteArray channelarray = f.readAll();
330 if (channelarray.size() != arrayWidth*(frame+frameLength)*m_channels) {
331 kdDebug()<<"--- BROKEN THUMB FOR: "<<m_url.filename()<<" ---------------------- "<<endl;
336 for (int z=(int) frame;z<(int) (frame+frameLength);z++) {
337 for (int c=0;c< m_channels;c++){
338 QByteArray m_array(arrayWidth);
339 for (int i = 0; i < arrayWidth; i++)
340 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
341 storeIn[z][c] = m_array;
344 emit audioThumbReady(storeIn);
347 if (thumbProducer.running()) return;
348 thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
349 thumbProducer.start(QThread::LowestPriority );