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>
44 #include "kdenlivesettings.h"
47 void MyThread::init(KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth)
54 m_frameLength = frameLength;
55 m_frequency = frequency;
56 m_channels = channels;
57 m_arrayWidth = arrayWidth;
61 bool MyThread::isWorking()
68 if (!f.open( IO_WriteOnly )) {
69 kdDebug()<<"++++++++ ERROR WRITING TO FILE: "<<f.name()<<endl;
70 kdDebug()<<"++++++++ DISABLING AUDIO THUMBS"<<endl;
71 KdenliveSettings::setAudiothumbnails(false);
75 char *tmp = KRender::decodedString(m_url.path());
76 Mlt::Producer m_producer(tmp);
79 if (KdenliveSettings::normaliseaudiothumbs()) {
80 Mlt::Filter m_convert("volume");
81 m_convert.set("gain", "normalise");
82 m_producer.attach(m_convert);
85 if (qApp->mainWidget())
86 QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
90 for (int z=(int) m_frame;z<(int) (m_frame+m_frameLength) && m_producer.is_valid();z++){
92 val=(int)((z-m_frame)/(m_frame+m_frameLength)*100.0);
93 if (last_val!=val & val > 1){
94 QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(val, 10005));
98 Mlt::Frame *mlt_frame = m_producer.get_frame();
99 if ( mlt_frame->is_valid() )
101 double m_framesPerSecond = mlt_producer_get_fps( m_producer.get_producer() ); //mlt_frame->get_double( "fps" );
102 int m_samples = mlt_sample_calculator( m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()) );
103 mlt_audio_format m_audioFormat = mlt_audio_pcm;
105 int16_t* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples );
107 for (int c=0;c< m_channels;c++){
108 QByteArray m_array(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);
112 f.writeBlock(m_array);
115 f.writeBlock(QByteArray(m_arrayWidth));
124 QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(-1, 10005));
126 else QApplication::postEvent(qApp->mainWidget(), new ProgressEvent(0, 10005));
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
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)
137 kDebug()<<"+++++++++++ CREATING THMB PROD FOR: "<<url;
138 m_profile = new Mlt::Profile((char*) qstrdup(KdenliveSettings::current_profile().toUtf8()));
143 if (m_profile) delete m_profile;
144 //if (thumbProducer.running ()) thumbProducer.exit();
149 QPixmap KThumb::getImage(KUrl url, int width, int height)
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);
159 if (m_producer.is_blank()) {
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);
176 else pix.fill(Qt::black);
178 if (m_frame) delete m_frame;
182 void KThumb::extractImage(int frame, int frame2)
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);
190 if (m_producer.is_blank()) {
192 emit thumbReady(frame, pix);
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);
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);
209 else pix.fill(Qt::black);
211 if (m_frame) delete m_frame;
212 emit thumbReady(frame, pix);
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);
223 else pix.fill(Qt::black);
225 if (m_frame) delete m_frame;
226 emit thumbReady(frame2, pix);
230 void KThumb::getImage(KUrl url, int frame, int width, int height)
232 if (url.isEmpty()) return;
233 QPixmap image(width, height);
234 char *tmp = KRender::decodedString(url.path());
235 Mlt::Producer m_producer(tmp);
237 image.fill(Qt::black);
239 if (m_producer.is_blank()) {
240 emit thumbReady(frame, image);
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;
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);
256 if (m_frame) delete m_frame;
257 emit thumbReady(frame, image);
260 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
262 if (url.isEmpty()) return;
263 QPixmap image(width, height);
264 char *tmp = KRender::decodedString(url.path());
265 Mlt::Producer m_producer(tmp);
267 image.fill(Qt::black);
269 if (m_producer.is_blank()) {
270 emit thumbReady(startframe, image);
271 emit thumbReady(endframe, image);
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;
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);
288 if (m_frame) delete m_frame;
289 emit thumbReady(startframe, image);
291 image.fill(Qt::black);
292 m_producer.seek(endframe);
293 m_frame = m_producer.get_frame();
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);
300 if (m_frame) delete m_frame;
301 emit thumbReady(endframe, image);
304 void KThumb::stopAudioThumbs()
306 if (thumbProducer.running ()) thumbProducer.stop_me = true;
310 void KThumb::removeAudioThumb()
312 if (m_thumbFile.isEmpty()) return;
314 QFile f(m_thumbFile);
318 void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLength, int arrayWidth){
319 if ((thumbProducer.running () && thumbProducer.isWorking()) || channel == 0) {
323 QMap <int, QMap <int, QByteArray> > storeIn;
324 //FIXME: Hardcoded!!!
325 int m_frequency = 48000;
326 int m_channels = channel;
329 KMD5 context ((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).ascii());
330 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.hexDigest().data() + ".thumb";
332 QFile f(m_thumbFile);
333 if (f.open( IO_ReadOnly )) {
334 QByteArray channelarray = f.readAll();
336 if (channelarray.size() != arrayWidth*(frame+frameLength)*m_channels) {
337 kdDebug()<<"--- BROKEN THUMB FOR: "<<m_url.filename()<<" ---------------------- "<<endl;
342 for (int z=(int) frame;z<(int) (frame+frameLength);z++) {
343 for (int c=0;c< m_channels;c++){
344 QByteArray m_array(arrayWidth);
345 for (int i = 0; i < arrayWidth; i++)
346 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
347 storeIn[z][c] = m_array;
350 emit audioThumbReady(storeIn);
353 if (thumbProducer.running()) return;
354 thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
355 thumbProducer.start(QThread::LowestPriority );