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();
143 void KThumb::extractImage(int frame, int frame2, int width, int height)
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);
152 if (m_producer.is_blank()) {
153 emit thumbReady(frame, pix);
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();
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);
168 if (!image.isNull()) {
169 pix = pix.fromImage(image);
171 else pix.fill(Qt::black);
173 if (m_frame) delete m_frame;
174 emit thumbReady(frame, pix);
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);
183 if (!image.isNull()) {
184 pix = pix.fromImage(image);
186 else pix.fill(Qt::black);
188 if (m_frame) delete m_frame;
189 emit thumbReady(frame2, pix);
193 void KThumb::getImage(KUrl url, int frame, int width, int height)
195 if (url.isEmpty()) return;
196 QPixmap image(width, height);
197 char *tmp = KRender::decodedString(url.path());
198 Mlt::Producer m_producer(tmp);
200 image.fill(Qt::black);
202 if (m_producer.is_blank()) {
203 emit thumbReady(frame, image);
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;
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);
219 if (m_frame) delete m_frame;
220 emit thumbReady(frame, image);
223 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
225 if (url.isEmpty()) return;
226 QPixmap image(width, height);
227 char *tmp = KRender::decodedString(url.path());
228 Mlt::Producer m_producer(tmp);
230 image.fill(Qt::black);
232 if (m_producer.is_blank()) {
233 emit thumbReady(startframe, image);
234 emit thumbReady(endframe, 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(startframe);
241 Mlt::Frame * m_frame = m_producer.get_frame();
242 mlt_image_format format = mlt_image_rgb24a;
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);
251 if (m_frame) delete m_frame;
252 emit thumbReady(startframe, image);
254 image.fill(Qt::black);
255 m_producer.seek(endframe);
256 m_frame = m_producer.get_frame();
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);
263 if (m_frame) delete m_frame;
264 emit thumbReady(endframe, image);
267 void KThumb::stopAudioThumbs()
269 if (thumbProducer.running ()) thumbProducer.stop_me = true;
273 void KThumb::removeAudioThumb()
275 if (m_thumbFile.isEmpty()) return;
277 QFile f(m_thumbFile);
281 void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLength, int arrayWidth){
282 if ((thumbProducer.running () && thumbProducer.isWorking()) || channel == 0) {
286 QMap <int, QMap <int, QByteArray> > storeIn;
287 //FIXME: Hardcoded!!!
288 int m_frequency = 48000;
289 int m_channels = channel;
292 KMD5 context ((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).ascii());
293 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.hexDigest().data() + ".thumb";
295 QFile f(m_thumbFile);
296 if (f.open( IO_ReadOnly )) {
297 QByteArray channelarray = f.readAll();
299 if (channelarray.size() != arrayWidth*(frame+frameLength)*m_channels) {
300 kdDebug()<<"--- BROKEN THUMB FOR: "<<m_url.filename()<<" ---------------------- "<<endl;
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;
313 emit audioThumbReady(storeIn);
316 if (thumbProducer.running()) return;
317 thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
318 thumbProducer.start(QThread::LowestPriority );