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 <kio/netaccess.h>
26 #include <kdenlivesettings.h>
27 #include <kfileitem.h>
28 #include <kmessagebox.h>
29 #include <KStandardDirs>
31 #include <mlt++/Mlt.h>
37 #include <QApplication>
38 #include <QCryptographicHash>
40 #include "clipmanager.h"
43 #include "kdenlivesettings.h"
46 void MyThread::init(QObject *parent, KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth) {
50 f.setFileName(target);
53 m_frameLength = frameLength;
54 m_frequency = frequency;
55 m_channels = channels;
56 m_arrayWidth = arrayWidth;
59 bool MyThread::isWorking() {
63 void MyThread::run() {
65 if (!f.open(QIODevice::WriteOnly)) {
66 kDebug() << "++++++++ ERROR WRITING TO FILE: " << f.fileName() << endl;
67 kDebug() << "++++++++ DISABLING AUDIO THUMBS" << endl;
68 KdenliveSettings::setAudiothumbnails(false);
72 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
73 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
76 if (KdenliveSettings::normaliseaudiothumbs()) {
77 Mlt::Filter m_convert(prof, "volume");
78 m_convert.set("gain", "normalise");
79 m_producer.attach(m_convert);
82 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
86 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
87 for (int z = (int) m_frame;z < (int)(m_frame + m_frameLength) && m_producer.is_valid();z++) {
89 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
90 if (last_val != val & val > 1) {
91 QApplication::postEvent(m_parent, new ProgressEvent(val, (QEvent::Type)10005));
96 Mlt::Frame *mlt_frame = m_producer.get_frame();
97 if (mlt_frame && mlt_frame->is_valid()) {
98 double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer()); //mlt_frame->get_double( "fps" );
99 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
100 mlt_audio_format m_audioFormat = mlt_audio_pcm;
102 int16_t* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples);
104 for (int c = 0;c < m_channels;c++) {
106 m_array.resize(m_arrayWidth);
107 for (uint i = 0; i < m_array.size(); i++) {
108 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
114 f.write(QByteArray(m_arrayWidth, '\x00'));
125 QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)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(ClipManager *clipManager, KUrl url, QObject * parent, const char *name): QObject(parent), m_clipManager(clipManager), m_url(url), m_producer(NULL) {
137 m_profile = new Mlt::Profile((char*) KdenliveSettings::current_profile().data());
138 QCryptographicHash context(QCryptographicHash::Sha1);
139 context.addData((KFileItem(m_url, "text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
140 m_thumbFile = KGlobal::dirs()->saveLocation("tmp" , "kdenlive") + context.result().toHex() + ".thumb";
141 kDebug() << "thumbfile=" << m_thumbFile;
145 if (m_profile) delete m_profile;
146 if (thumbProducer.isRunning()) thumbProducer.exit();
149 void KThumb::setProducer(Mlt::Producer *producer) {
150 m_producer = producer;
153 void KThumb::updateClipUrl(KUrl url) {
158 QPixmap KThumb::getImage(KUrl url, int width, int height) {
159 if (url.isEmpty()) return QPixmap();
160 return getImage(url, 0, width, height);
163 void KThumb::extractImage(int frame, int frame2) {
164 if (m_url.isEmpty()) return;
165 if (m_producer == NULL) return;
166 /*char *tmp = Render::decodedString("<westley><playlist><producer resource=\"" + m_url.path() + "\" /></playlist></westley>");
167 Mlt::Producer producer(*m_profile, "westley-xml", tmp);
170 int twidth = (int)(KdenliveSettings::trackheight() * m_profile->dar());
171 if (m_producer->is_blank()) {
172 QPixmap pix(twidth, KdenliveSettings::trackheight());
174 emit thumbReady(frame, pix);
178 QPixmap pix = getFrame(*m_producer, frame, twidth, KdenliveSettings::trackheight());
179 emit thumbReady(frame, pix);
182 QPixmap pix = getFrame(*m_producer, frame2, twidth , KdenliveSettings::trackheight());
183 emit thumbReady(frame2, pix);
188 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height) {
189 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
190 QPixmap pix(width, height);
191 if (url.isEmpty()) return pix;
193 char *tmp = Render::decodedString("<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
194 Mlt::Producer producer(profile, "westley-xml", tmp);
197 if (producer.is_blank()) {
202 return getFrame(producer, frame, width, height);
206 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
207 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
208 QPixmap pix(width, height);
210 QDomElement westley = doc.createElement("westley");
211 QDomElement play = doc.createElement("playlist");
212 doc.appendChild(westley);
213 westley.appendChild(play);
214 play.appendChild(doc.importNode(xml, true));
215 char *tmp = Render::decodedString(doc.toString());
216 Mlt::Producer producer(profile, "westley-xml", tmp);
219 if (producer.is_blank()) {
223 return getFrame(producer, frame, width, height);
227 QPixmap KThumb::getFrame(Mlt::Producer producer, int framepos, int width, int height) {
229 producer.seek(framepos);
230 Mlt::Frame *frame = producer.get_frame();
231 mlt_image_format format = mlt_image_yuv422;
233 int frame_height = 0;
234 frame->set("normalised_height", height);
235 frame->set("normalised_width", width);
236 QPixmap pix(width, height);
238 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
239 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
240 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
241 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
243 if (!image.isNull()) {
244 pix = pix.fromImage(image.rgbSwapped());
248 mlt_pool_release(new_image);
253 void KThumb::getImage(KUrl url, int frame, int width, int height)
255 if (url.isEmpty()) return;
256 QPixmap image(width, height);
257 char *tmp = KRender::decodedString(url.path());
258 Mlt::Producer m_producer(tmp);
260 image.fill(Qt::black);
262 if (m_producer.is_blank()) {
263 emit thumbReady(frame, image);
266 Mlt::Filter m_convert("avcolour_space");
267 m_convert.set("forced", mlt_image_rgb24a);
268 m_producer.attach(m_convert);
269 m_producer.seek(frame);
270 Mlt::Frame * m_frame = m_producer.get_frame();
271 mlt_image_format format = mlt_image_rgb24a;
274 if (m_frame && m_frame->is_valid()) {
275 uint8_t *thumb = m_frame->get_image(format, width, height);
276 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
277 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
279 if (m_frame) delete m_frame;
280 emit thumbReady(frame, image);
283 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
285 if (url.isEmpty()) return;
286 QPixmap image(width, height);
287 char *tmp = KRender::decodedString(url.path());
288 Mlt::Producer m_producer(tmp);
290 image.fill(Qt::black);
292 if (m_producer.is_blank()) {
293 emit thumbReady(startframe, image);
294 emit thumbReady(endframe, image);
297 Mlt::Filter m_convert("avcolour_space");
298 m_convert.set("forced", mlt_image_rgb24a);
299 m_producer.attach(m_convert);
300 m_producer.seek(startframe);
301 Mlt::Frame * m_frame = m_producer.get_frame();
302 mlt_image_format format = mlt_image_rgb24a;
306 if (m_frame && m_frame->is_valid()) {
307 uint8_t *thumb = m_frame->get_image(format, width, height);
308 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
309 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
311 if (m_frame) delete m_frame;
312 emit thumbReady(startframe, image);
314 image.fill(Qt::black);
315 m_producer.seek(endframe);
316 m_frame = m_producer.get_frame();
318 if (m_frame && m_frame->is_valid()) {
319 uint8_t *thumb = m_frame->get_image(format, width, height);
320 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
321 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
323 if (m_frame) delete m_frame;
324 emit thumbReady(endframe, image);
327 void KThumb::stopAudioThumbs() {
328 if (thumbProducer.isRunning()) thumbProducer.stop_me = true;
332 void KThumb::removeAudioThumb() {
333 if (m_thumbFile.isEmpty()) return;
335 QFile f(m_thumbFile);
339 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
341 if ((thumbProducer.isRunning() && thumbProducer.isWorking()) || channel == 0) {
345 QMap <int, QMap <int, QByteArray> > storeIn;
346 //FIXME: Hardcoded!!!
347 int m_frequency = 48000;
348 int m_channels = channel;
350 QFile f(m_thumbFile);
351 if (f.open(QIODevice::ReadOnly)) {
352 QByteArray channelarray = f.readAll();
354 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
355 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
359 kDebug() << "reading audio thumbs from file";
360 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
361 for (int c = 0;c < m_channels;c++) {
362 QByteArray m_array(arrayWidth, '\x00');
363 for (int i = 0; i < arrayWidth; i++)
364 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
365 storeIn[z][c] = m_array;
368 emit audioThumbReady(storeIn);
370 if (thumbProducer.isRunning()) return;
371 thumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
372 thumbProducer.start(QThread::LowestPriority);
373 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
377 void KThumb::customEvent(QEvent * event) {
378 if (event->type() == 10005) {
379 ProgressEvent* p = (ProgressEvent*) event;
380 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), p->value());
386 #include "kthumb.moc"