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 = Mlt::Profile((char*) KdenliveSettings::current_profile().data());
143 //if (thumbProducer.running ()) thumbProducer.exit();
148 QPixmap KThumb::getImage(KUrl url, int width, int height)
150 if (url.isEmpty()) return QPixmap();
151 QPixmap pix(width, height);
152 kDebug()<<"+++++++++++ GET THMB IMG FOR: "<<url;
153 char *tmp = Render::decodedString(url.path());
154 Mlt::Profile prof((char*) KdenliveSettings::current_profile().data());
155 Mlt::Producer m_producer(prof, tmp);
158 if (m_producer.is_blank()) {
162 Mlt::Frame * m_frame;
163 mlt_image_format format = mlt_image_rgb24a;
164 Mlt::Filter m_convert(prof, "avcolour_space");
165 m_convert.set("forced", mlt_image_rgb24a);
166 m_producer.attach(m_convert);
167 //m_producer.seek(frame);
168 m_frame = m_producer.get_frame();
169 if (m_frame && m_frame->is_valid()) {
170 uint8_t *thumb = m_frame->get_image(format, width, height);
171 QImage image(thumb, width, height, QImage::Format_ARGB32);
172 if (!image.isNull()) {
173 pix = pix.fromImage(image);
175 else pix.fill(Qt::black);
177 if (m_frame) delete m_frame;
181 void KThumb::extractImage(int frame, int frame2)
183 if (m_url.isEmpty()) return;
184 QPixmap pix(m_width, m_height);
185 char *tmp = Render::decodedString(m_url.path());
186 Mlt::Producer m_producer(m_profile, tmp);
189 if (m_producer.is_blank()) {
191 emit thumbReady(frame, pix);
194 Mlt::Frame * m_frame;
195 mlt_image_format format = mlt_image_rgb24a;
196 Mlt::Filter m_convert(m_profile, "avcolour_space");
197 m_convert.set("forced", mlt_image_rgb24a);
198 m_producer.attach(m_convert);
200 m_producer.seek(frame);
201 m_frame = m_producer.get_frame();
202 if (m_frame && m_frame->is_valid()) {
203 uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
204 QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
205 if (!image.isNull()) {
206 pix = pix.fromImage(image);
208 else pix.fill(Qt::black);
210 if (m_frame) delete m_frame;
211 emit thumbReady(frame, pix);
213 if (frame2 == -1) return;
214 m_producer.seek(frame2);
215 m_frame = m_producer.get_frame();
216 if (m_frame && m_frame->is_valid()) {
217 uint8_t *thumb = m_frame->get_image(format, m_width, m_height);
218 QImage image(thumb, m_width, m_height, QImage::Format_ARGB32);
219 if (!image.isNull()) {
220 pix = pix.fromImage(image);
222 else pix.fill(Qt::black);
224 if (m_frame) delete m_frame;
225 emit thumbReady(frame2, pix);
229 void KThumb::getImage(KUrl url, int frame, int width, int height)
231 if (url.isEmpty()) return;
232 QPixmap image(width, height);
233 char *tmp = KRender::decodedString(url.path());
234 Mlt::Producer m_producer(tmp);
236 image.fill(Qt::black);
238 if (m_producer.is_blank()) {
239 emit thumbReady(frame, image);
242 Mlt::Filter m_convert("avcolour_space");
243 m_convert.set("forced", mlt_image_rgb24a);
244 m_producer.attach(m_convert);
245 m_producer.seek(frame);
246 Mlt::Frame * m_frame = m_producer.get_frame();
247 mlt_image_format format = mlt_image_rgb24a;
250 if (m_frame && m_frame->is_valid()) {
251 uint8_t *thumb = m_frame->get_image(format, width, height);
252 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
253 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
255 if (m_frame) delete m_frame;
256 emit thumbReady(frame, image);
259 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
261 if (url.isEmpty()) return;
262 QPixmap image(width, height);
263 char *tmp = KRender::decodedString(url.path());
264 Mlt::Producer m_producer(tmp);
266 image.fill(Qt::black);
268 if (m_producer.is_blank()) {
269 emit thumbReady(startframe, image);
270 emit thumbReady(endframe, image);
273 Mlt::Filter m_convert("avcolour_space");
274 m_convert.set("forced", mlt_image_rgb24a);
275 m_producer.attach(m_convert);
276 m_producer.seek(startframe);
277 Mlt::Frame * m_frame = m_producer.get_frame();
278 mlt_image_format format = mlt_image_rgb24a;
282 if (m_frame && m_frame->is_valid()) {
283 uint8_t *thumb = m_frame->get_image(format, width, height);
284 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
285 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
287 if (m_frame) delete m_frame;
288 emit thumbReady(startframe, image);
290 image.fill(Qt::black);
291 m_producer.seek(endframe);
292 m_frame = m_producer.get_frame();
294 if (m_frame && m_frame->is_valid()) {
295 uint8_t *thumb = m_frame->get_image(format, width, height);
296 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
297 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
299 if (m_frame) delete m_frame;
300 emit thumbReady(endframe, image);
303 void KThumb::stopAudioThumbs()
305 if (thumbProducer.running ()) thumbProducer.stop_me = true;
309 void KThumb::removeAudioThumb()
311 if (m_thumbFile.isEmpty()) return;
313 QFile f(m_thumbFile);
317 void KThumb::getAudioThumbs(KUrl url, int channel, double frame, double frameLength, int arrayWidth){
318 if ((thumbProducer.running () && thumbProducer.isWorking()) || channel == 0) {
322 QMap <int, QMap <int, QByteArray> > storeIn;
323 //FIXME: Hardcoded!!!
324 int m_frequency = 48000;
325 int m_channels = channel;
328 KMD5 context ((KFileItem(m_url,"text/plain", S_IFREG).timeString() + m_url.fileName()).ascii());
329 m_thumbFile = KdenliveSettings::currenttmpfolder() + context.hexDigest().data() + ".thumb";
331 QFile f(m_thumbFile);
332 if (f.open( IO_ReadOnly )) {
333 QByteArray channelarray = f.readAll();
335 if (channelarray.size() != arrayWidth*(frame+frameLength)*m_channels) {
336 kdDebug()<<"--- BROKEN THUMB FOR: "<<m_url.filename()<<" ---------------------- "<<endl;
341 for (int z=(int) frame;z<(int) (frame+frameLength);z++) {
342 for (int c=0;c< m_channels;c++){
343 QByteArray m_array(arrayWidth);
344 for (int i = 0; i < arrayWidth; i++)
345 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
346 storeIn[z][c] = m_array;
349 emit audioThumbReady(storeIn);
352 if (thumbProducer.running()) return;
353 thumbProducer.init(m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
354 thumbProducer.start(QThread::LowestPriority );