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 ***************************************************************************/
25 #include <QApplication>
26 #include <QCryptographicHash>
28 #include <kio/netaccess.h>
31 #include <kdenlivesettings.h>
32 #include <kfileitem.h>
33 #include <kmessagebox.h>
34 #include <KStandardDirs>
36 #include <mlt++/Mlt.h>
38 #include "clipmanager.h"
41 #include "kdenlivesettings.h"
45 void MyThread::init(QObject *parent, KUrl url, QString target, double frame, double frameLength, int frequency, int channels, int arrayWidth) {
49 f.setFileName(target);
52 m_frameLength = frameLength;
53 m_frequency = frequency;
54 m_channels = channels;
55 m_arrayWidth = arrayWidth;
58 bool MyThread::isWorking() {
62 void MyThread::run() {
64 if (!f.open(QIODevice::WriteOnly)) {
65 kDebug() << "++++++++ ERROR WRITING TO FILE: " << f.fileName() << endl;
66 kDebug() << "++++++++ DISABLING AUDIO THUMBS" << endl;
67 KdenliveSettings::setAudiothumbnails(false);
71 Mlt::Profile prof((char*) KdenliveSettings::current_profile().toUtf8().data());
72 Mlt::Producer m_producer(prof, m_url.path().toUtf8().data());
75 if (KdenliveSettings::normaliseaudiothumbs()) {
76 Mlt::Filter m_convert(prof, "volume");
77 m_convert.set("gain", "normalise");
78 m_producer.attach(m_convert);
81 //QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)10005));
85 kDebug() << "for " << m_frame << " " << m_frameLength << " " << m_producer.is_valid();
86 for (int z = (int) m_frame;z < (int)(m_frame + m_frameLength) && m_producer.is_valid();z++) {
88 val = (int)((z - m_frame) / (m_frame + m_frameLength) * 100.0);
89 if (last_val != val & val > 1) {
90 QApplication::postEvent(m_parent, new ProgressEvent(val, (QEvent::Type)10005));
95 Mlt::Frame *mlt_frame = m_producer.get_frame();
96 if (mlt_frame && mlt_frame->is_valid()) {
97 double m_framesPerSecond = mlt_producer_get_fps(m_producer.get_producer()); //mlt_frame->get_double( "fps" );
98 int m_samples = mlt_sample_calculator(m_framesPerSecond, m_frequency, mlt_frame_get_position(mlt_frame->get_frame()));
99 mlt_audio_format m_audioFormat = mlt_audio_pcm;
101 int16_t* m_pcm = mlt_frame->get_audio(m_audioFormat, m_frequency, m_channels, m_samples);
103 for (int c = 0;c < m_channels;c++) {
105 m_array.resize(m_arrayWidth);
106 for (uint i = 0; i < m_array.size(); i++) {
107 m_array[i] = ((*(m_pcm + c + i * m_samples / m_array.size())) >> 9) + 127 / 2 ;
113 f.write(QByteArray(m_arrayWidth, '\x00'));
124 QApplication::postEvent(m_parent, new ProgressEvent(-1, (QEvent::Type)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(ClipManager *clipManager, KUrl url, QObject * parent, const char *name): QObject(parent), m_clipManager(clipManager), m_url(url), m_producer(NULL), m_dar(1) {
135 QCryptographicHash context(QCryptographicHash::Sha1);
136 context.addData((KFileItem(m_url, "text/plain", S_IFREG).timeString() + m_url.fileName()).toAscii().data());
137 m_thumbFile = KGlobal::dirs()->saveLocation("tmp" , "kdenlive") + context.result().toHex() + ".thumb";
138 kDebug() << "thumbfile=" << m_thumbFile;
142 if (thumbProducer.isRunning()) thumbProducer.exit();
145 void KThumb::setProducer(Mlt::Producer *producer) {
146 m_producer = producer;
147 m_dar = producer->profile()->dar();
150 void KThumb::updateClipUrl(KUrl url) {
155 QPixmap KThumb::getImage(KUrl url, int width, int height) {
156 if (url.isEmpty()) return QPixmap();
157 return getImage(url, 0, width, height);
160 void KThumb::extractImage(int frame, int frame2) {
161 if (m_url.isEmpty() || !KdenliveSettings::videothumbnails() || m_producer == NULL) return;
163 int twidth = (int)(KdenliveSettings::trackheight() * m_dar);
164 if (m_producer->is_blank()) {
165 QPixmap pix(twidth, KdenliveSettings::trackheight());
167 emit thumbReady(frame, pix);
171 QPixmap pix = getFrame(*m_producer, frame, twidth, KdenliveSettings::trackheight());
172 emit thumbReady(frame, pix);
175 QPixmap pix = getFrame(*m_producer, frame2, twidth , KdenliveSettings::trackheight());
176 emit thumbReady(frame2, pix);
181 QPixmap KThumb::getImage(KUrl url, int frame, int width, int height) {
182 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
183 QPixmap pix(width, height);
184 if (url.isEmpty()) return pix;
186 char *tmp = Render::decodedString("<westley><playlist><producer resource=\"" + url.path() + "\" /></playlist></westley>");
187 Mlt::Producer producer(profile, "westley-xml", tmp);
190 if (producer.is_blank()) {
195 return getFrame(producer, frame, width, height);
199 QPixmap KThumb::getImage(QDomElement xml, int frame, int width, int height) {
200 Mlt::Profile profile((char*) KdenliveSettings::current_profile().data());
201 QPixmap pix(width, height);
203 QDomElement westley = doc.createElement("westley");
204 QDomElement play = doc.createElement("playlist");
205 doc.appendChild(westley);
206 westley.appendChild(play);
207 play.appendChild(doc.importNode(xml, true));
208 char *tmp = Render::decodedString(doc.toString());
209 Mlt::Producer producer(profile, "westley-xml", tmp);
212 if (producer.is_blank()) {
216 return getFrame(producer, frame, width, height);
220 QPixmap KThumb::getFrame(Mlt::Producer producer, int framepos, int width, int height) {
221 producer.seek(framepos);
222 Mlt::Frame *frame = producer.get_frame();
223 mlt_image_format format = mlt_image_yuv422;
225 int frame_height = 0;
226 frame->set("normalised_height", height);
227 frame->set("normalised_width", width);
228 QPixmap pix(width, height);
230 uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
231 uint8_t *new_image = (uint8_t *)mlt_pool_alloc(frame_width * (frame_height + 1) * 4);
232 mlt_convert_yuv422_to_rgb24a((uint8_t *)data, new_image, frame_width * frame_height);
233 QImage image((uchar *)new_image, frame_width, frame_height, QImage::Format_ARGB32);
235 if (!image.isNull()) {
236 pix = pix.fromImage(image.rgbSwapped());
240 mlt_pool_release(new_image);
245 void KThumb::getImage(KUrl url, int frame, int width, int height)
247 if (url.isEmpty()) return;
248 QPixmap image(width, height);
249 char *tmp = KRender::decodedString(url.path());
250 Mlt::Producer m_producer(tmp);
252 image.fill(Qt::black);
254 if (m_producer.is_blank()) {
255 emit thumbReady(frame, image);
258 Mlt::Filter m_convert("avcolour_space");
259 m_convert.set("forced", mlt_image_rgb24a);
260 m_producer.attach(m_convert);
261 m_producer.seek(frame);
262 Mlt::Frame * m_frame = m_producer.get_frame();
263 mlt_image_format format = mlt_image_rgb24a;
266 if (m_frame && m_frame->is_valid()) {
267 uint8_t *thumb = m_frame->get_image(format, width, height);
268 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
269 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width + 2, height + 2);
271 if (m_frame) delete m_frame;
272 emit thumbReady(frame, image);
275 void KThumb::getThumbs(KUrl url, int startframe, int endframe, int width, int height)
277 if (url.isEmpty()) return;
278 QPixmap image(width, height);
279 char *tmp = KRender::decodedString(url.path());
280 Mlt::Producer m_producer(tmp);
282 image.fill(Qt::black);
284 if (m_producer.is_blank()) {
285 emit thumbReady(startframe, image);
286 emit thumbReady(endframe, image);
289 Mlt::Filter m_convert("avcolour_space");
290 m_convert.set("forced", mlt_image_rgb24a);
291 m_producer.attach(m_convert);
292 m_producer.seek(startframe);
293 Mlt::Frame * m_frame = m_producer.get_frame();
294 mlt_image_format format = mlt_image_rgb24a;
298 if (m_frame && m_frame->is_valid()) {
299 uint8_t *thumb = m_frame->get_image(format, width, height);
300 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
301 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
303 if (m_frame) delete m_frame;
304 emit thumbReady(startframe, image);
306 image.fill(Qt::black);
307 m_producer.seek(endframe);
308 m_frame = m_producer.get_frame();
310 if (m_frame && m_frame->is_valid()) {
311 uint8_t *thumb = m_frame->get_image(format, width, height);
312 QImage tmpimage(thumb, width, height, 32, NULL, 0, QImage::IgnoreEndian);
313 if (!tmpimage.isNull()) bitBlt(&image, 1, 1, &tmpimage, 0, 0, width - 2, height - 2);
315 if (m_frame) delete m_frame;
316 emit thumbReady(endframe, image);
319 void KThumb::stopAudioThumbs() {
320 if (thumbProducer.isRunning()) thumbProducer.stop_me = true;
324 void KThumb::removeAudioThumb() {
325 if (m_thumbFile.isEmpty()) return;
327 QFile f(m_thumbFile);
331 void KThumb::getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth) {
333 if ((thumbProducer.isRunning() && thumbProducer.isWorking()) || channel == 0) {
337 QMap <int, QMap <int, QByteArray> > storeIn;
338 //FIXME: Hardcoded!!!
339 int m_frequency = 48000;
340 int m_channels = channel;
342 QFile f(m_thumbFile);
343 if (f.open(QIODevice::ReadOnly)) {
344 QByteArray channelarray = f.readAll();
346 if (channelarray.size() != arrayWidth*(frame + frameLength)*m_channels) {
347 kDebug() << "--- BROKEN THUMB FOR: " << m_url.fileName() << " ---------------------- " << endl;
351 kDebug() << "reading audio thumbs from file";
352 for (int z = (int) frame;z < (int)(frame + frameLength);z++) {
353 for (int c = 0;c < m_channels;c++) {
354 QByteArray m_array(arrayWidth, '\x00');
355 for (int i = 0; i < arrayWidth; i++)
356 m_array[i] = channelarray[z*arrayWidth*m_channels + c*arrayWidth + i];
357 storeIn[z][c] = m_array;
360 emit audioThumbReady(storeIn);
362 if (thumbProducer.isRunning()) return;
363 thumbProducer.init(this, m_url, m_thumbFile, frame, frameLength, m_frequency, m_channels, arrayWidth);
364 thumbProducer.start(QThread::LowestPriority);
365 kDebug() << "STARTING GENERATE THMB FOR: " << m_url << " ................................";
369 void KThumb::customEvent(QEvent * event) {
370 if (event->type() == 10005) {
371 ProgressEvent* p = (ProgressEvent*) event;
372 m_clipManager->setThumbsProgress(i18n("Creating thumbnail for %1", m_url.fileName()), p->value());
378 #include "kthumb.moc"