]> git.sesse.net Git - kdenlive/blob - src/mltdevicecapture.cpp
Don't add useless computing when preview is disabled on mlt capture
[kdenlive] / src / mltdevicecapture.cpp
1 /***************************************************************************
2                         mltdevicecapture.cpp  -  description
3                            -------------------
4    begin                : Sun May 21 2011
5    copyright            : (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org)
6
7 ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18
19 #include "mltdevicecapture.h"
20 #include "kdenlivesettings.h"
21 #include "definitions.h"
22 //#include "recmonitor.h"
23 //#include "renderer.h"
24 #include "blackmagic/devices.h"
25
26 #include <mlt++/Mlt.h>
27
28 #include <KDebug>
29 #include <KStandardDirs>
30 #include <KMessageBox>
31 #include <KLocale>
32 #include <KTemporaryFile>
33
34 #include <QTimer>
35 #include <QDir>
36 #include <QString>
37 #include <QApplication>
38 #include <QThread>
39
40 #include <cstdlib>
41 #include <cstdarg>
42
43 #include <QDebug>
44
45
46
47 static void consumer_gl_frame_show(mlt_consumer, MltDeviceCapture * self, mlt_frame frame_ptr)
48 {
49     // detect if the producer has finished playing. Is there a better way to do it?
50     Mlt::Frame frame(frame_ptr);
51     self->showFrame(frame);
52 }
53
54 static void rec_consumer_frame_show(mlt_consumer, MltDeviceCapture * self, mlt_frame frame_ptr)
55 {
56     Mlt::Frame frame(frame_ptr);
57     if (!frame.is_valid()) return;
58     self->gotCapturedFrame(frame);
59 }
60
61 static void rec_consumer_frame_preview(mlt_consumer, MltDeviceCapture * self, mlt_frame frame_ptr)
62 {
63     Mlt::Frame frame(frame_ptr);
64     if (!frame.is_valid()) return;
65     if (self->sendFrameForAnalysis && frame_ptr->convert_image) {
66         self->emitFrameUpdated(frame);
67     }
68     if (self->doCapture > 0) {  
69         self->doCapture --;
70         if (self->doCapture == 0) self->saveFrame(frame);
71     }
72
73     //TODO: connect record monitor to audio scopes
74     /*
75     if (self->analyseAudio) {
76         self->showAudio(frame);
77     }
78     */
79 }
80
81
82 MltDeviceCapture::MltDeviceCapture(QString profile, VideoPreviewContainer *surface, QWidget *parent) :
83     AbstractRender("capture", parent),
84     doCapture(0),
85     sendFrameForAnalysis(false),
86     analyseAudio(KdenliveSettings::monitor_audio()),
87     processingImage(false),
88     m_mltConsumer(NULL),
89     m_mltProducer(NULL),
90     m_mltProfile(NULL),
91     m_droppedFrames(0),
92     m_livePreview(KdenliveSettings::recording_preview()),
93     m_captureDisplayWidget(surface),
94     m_winid((int) surface->winId())
95 {
96     if (profile.isEmpty()) profile = KdenliveSettings::current_profile();
97     buildConsumer(profile);
98     connect(this, SIGNAL(unblockPreview()), this, SLOT(slotPreparePreview()));
99 }
100
101 MltDeviceCapture::~MltDeviceCapture()
102 {
103     if (m_mltConsumer) delete m_mltConsumer;
104     if (m_mltProducer) delete m_mltProducer;
105     if (m_mltProfile) delete m_mltProfile;
106 }
107
108 void MltDeviceCapture::buildConsumer(const QString &profileName)
109 {
110     if (!profileName.isEmpty()) m_activeProfile = profileName;
111
112     if (m_mltProfile) delete m_mltProfile;
113
114     char *tmp = qstrdup(m_activeProfile.toUtf8().constData());
115     setenv("MLT_PROFILE", tmp, 1);
116     m_mltProfile = new Mlt::Profile(tmp);
117     m_mltProfile->set_explicit(true);
118     delete[] tmp;
119
120     QString videoDriver = KdenliveSettings::videodrivername();
121     if (!videoDriver.isEmpty()) {
122         if (videoDriver == "x11_noaccel") {
123             setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
124             videoDriver = "x11";
125         } else {
126             unsetenv("SDL_VIDEO_YUV_HWACCEL");
127         }
128     }
129     setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
130
131     if (m_winid == 0) {
132         // OpenGL monitor
133         m_mltConsumer = new Mlt::Consumer(*m_mltProfile, "sdl_audio");
134         m_mltConsumer->set("preview_off", 1);
135         m_mltConsumer->set("preview_format", mlt_image_rgb24);
136         m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_gl_frame_show);
137     } else {
138         m_mltConsumer = new Mlt::Consumer(*m_mltProfile, "sdl_preview");
139         m_mltConsumer->set("window_id", m_winid);
140         m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) rec_consumer_frame_preview);
141     }
142     //m_mltConsumer->set("resize", 1);
143     //m_mltConsumer->set("terminate_on_pause", 1);
144     m_mltConsumer->set("window_background", KdenliveSettings::window_background().name().toUtf8().constData());
145     //m_mltConsumer->set("rescale", "nearest");
146
147     QString audioDevice = KdenliveSettings::audiodevicename();
148     if (!audioDevice.isEmpty())
149         m_mltConsumer->set("audio_device", audioDevice.toUtf8().constData());
150
151     if (!videoDriver.isEmpty())
152         m_mltConsumer->set("video_driver", videoDriver.toUtf8().constData());
153
154     QString audioDriver = KdenliveSettings::audiodrivername();
155
156     if (!audioDriver.isEmpty())
157         m_mltConsumer->set("audio_driver", audioDriver.toUtf8().constData());
158
159     //m_mltConsumer->set("progressive", 0);
160     //m_mltConsumer->set("buffer", 1);
161     //m_mltConsumer->set("real_time", 0);
162 }
163
164 void MltDeviceCapture::stop()
165 {
166     bool isPlaylist = false;
167     disconnect(this, SIGNAL(imageReady(QImage)), this, SIGNAL(frameUpdated(QImage)));
168     m_captureDisplayWidget->stop();
169     
170     if (m_mltConsumer) {
171         m_mltConsumer->set("refresh", 0);
172         m_mltConsumer->stop();
173         //if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
174     }
175     if (m_mltProducer) {
176         QList <Mlt::Producer *> prods;
177         Mlt::Service service(m_mltProducer->parent().get_service());
178         mlt_service_lock(service.get_service());
179         if (service.type() == tractor_type) {
180             isPlaylist = true;
181             Mlt::Tractor tractor(service);
182             mlt_tractor_close(tractor.get_tractor());
183             Mlt::Field *field = tractor.field();
184             mlt_service nextservice = mlt_service_get_producer(service.get_service());
185             mlt_service nextservicetodisconnect;
186             mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
187             QString mlt_type = mlt_properties_get(properties, "mlt_type");
188             QString resource = mlt_properties_get(properties, "mlt_service");
189             // Delete all transitions
190             while (mlt_type == "transition") {
191                 nextservicetodisconnect = nextservice;
192                 nextservice = mlt_service_producer(nextservice);
193                 mlt_field_disconnect_service(field->get_field(), nextservicetodisconnect);
194                 if (nextservice == NULL) break;
195                 properties = MLT_SERVICE_PROPERTIES(nextservice);
196                 mlt_type = mlt_properties_get(properties, "mlt_type");
197                 resource = mlt_properties_get(properties, "mlt_service");
198             }
199             delete field;
200             field = NULL;
201         }
202         mlt_service_unlock(service.get_service());
203         delete m_mltProducer;
204         m_mltProducer = NULL;
205     }
206     // For some reason, the consumer seems to be deleted by previous stuff when in playlist mode
207     if (!isPlaylist && m_mltConsumer) delete m_mltConsumer;
208     m_mltConsumer = NULL;
209 }
210
211
212 void MltDeviceCapture::doRefresh()
213 {
214     if (m_mltConsumer) m_mltConsumer->set("refresh", 1);
215 }
216
217
218 void MltDeviceCapture::emitFrameUpdated(Mlt::Frame& frame)
219 {
220     /*
221     //TEST: is it better to convert the frame in a thread outside of MLT??
222     if (processingImage) return;
223     mlt_image_format format = (mlt_image_format) frame.get_int("format"); //mlt_image_rgb24;
224     int width = frame.get_int("width");
225     int height = frame.get_int("height");
226     unsigned char *buffer = (unsigned char *) frame.get_data("image");
227     if (format == mlt_image_yuv422) {
228         QtConcurrent::run(this, &MltDeviceCapture::uyvy2rgb, (unsigned char *) buffer, width, height);
229     }
230     */
231
232     mlt_image_format format = mlt_image_rgb24;
233     int width = 0;
234     int height = 0;
235     const uchar* image = frame.get_image(format, width, height);
236     QImage qimage(width, height, QImage::Format_ARGB32);
237     memcpy(qimage.bits(), image, width * height * 3);
238     emit frameUpdated(qimage.rgbSwapped());
239 }
240
241 void MltDeviceCapture::showFrame(Mlt::Frame& frame)
242 {
243     mlt_image_format format = mlt_image_rgb24;
244     int width = 0;
245     int height = 0;
246     const uchar* image = frame.get_image(format, width, height);
247     QImage qimage(width, height, QImage::Format_RGB888);
248     memcpy(qimage.scanLine(0), image, width * height * 3);
249     emit showImageSignal(qimage);
250
251     if (sendFrameForAnalysis && frame.get_frame()->convert_image) {
252         emit frameUpdated(qimage.rgbSwapped());
253     }
254 }
255
256 void MltDeviceCapture::showAudio(Mlt::Frame& frame)
257 {
258     if (!frame.is_valid() || frame.get_int("test_audio") != 0) {
259         return;
260     }
261     mlt_audio_format audio_format = mlt_audio_s16;
262     int freq = 0;
263     int num_channels = 0;
264     int samples = 0;
265     int16_t* data = (int16_t*)frame.get_audio(audio_format, freq, num_channels, samples);
266
267     if (!data) {
268         return;
269     }
270
271     // Data format: [ c00 c10 c01 c11 c02 c12 c03 c13 ... c0{samples-1} c1{samples-1} for 2 channels.
272     // So the vector is of size samples*channels.
273     QVector<int16_t> sampleVector(samples*num_channels);
274     memcpy(sampleVector.data(), data, samples*num_channels*sizeof(int16_t));
275
276     if (samples > 0) {
277         emit audioSamplesSignal(sampleVector, freq, num_channels, samples);
278     }
279 }
280
281 bool MltDeviceCapture::slotStartPreview(const QString &producer, bool xmlFormat)
282 {
283     if (m_mltConsumer == NULL) {
284         buildConsumer();
285     }
286     char *tmp = qstrdup(producer.toUtf8().constData());
287     if (xmlFormat) m_mltProducer = new Mlt::Producer(*m_mltProfile, "xml-string", tmp);
288     else m_mltProducer = new Mlt::Producer(*m_mltProfile, tmp);
289     delete[] tmp;
290
291     if (m_mltProducer == NULL || !m_mltProducer->is_valid()) {
292         if (m_mltProducer) {
293             delete m_mltProducer;
294             m_mltProducer = NULL;
295         }
296         kDebug()<<"//// ERROR CREATRING PROD";
297         return false;
298     }
299     m_mltConsumer->connect(*m_mltProducer);
300     if (m_mltConsumer->start() == -1) {
301         delete m_mltConsumer;
302         m_mltConsumer = NULL;
303         return 0;
304     }
305     connect(this, SIGNAL(imageReady(QImage)), this, SIGNAL(frameUpdated(QImage)));
306     return 1;
307 }
308
309 void MltDeviceCapture::gotCapturedFrame(Mlt::Frame& frame)
310 {
311     if (m_mltProducer) {
312         int dropped = m_mltProducer->get_int("dropped");
313         if (dropped > m_droppedFrames) {
314             m_droppedFrames = dropped;
315             emit droppedFrames(m_droppedFrames);
316         }
317     }
318     m_frameCount++;
319     if (m_livePreview == 2) return;
320     if (m_livePreview == 0 && (m_frameCount % 10 > 0)) return;
321     mlt_image_format format = mlt_image_rgb24;
322     int width = 0;
323     int height = 0;
324     uint8_t *data = frame.get_image(format, width, height, 0);
325     //QImage image(width, height, QImage::Format_RGB888);
326     //memcpy(image.bits(), data, width * height * 3);
327     QImage image((uchar *)data, width, height, QImage::Format_RGB888);
328
329     m_captureDisplayWidget->setImage(image);
330
331     //TEST: is it better to process frame conversion ouside MLT???
332     /*
333     if (!m_livePreview || processingImage) return;
334
335     mlt_image_format format = (mlt_image_format) frame.get_int("format"); //mlt_image_rgb24a;
336     int width = frame.get_int("width");
337     int height = frame.get_int("height");
338     unsigned char *buffer = (unsigned char *) frame.get_data("image");
339     //unsigned char *buffer = frame.get_image(format, width, height);
340     //convert from uyvy422 to rgba
341     if (format == mlt_image_yuv422) {
342         QtConcurrent::run(this, &MltDeviceCapture::uyvy2rgb, (unsigned char *) buffer, width, height);
343         //CaptureHandler::uyvy2rgb((uchar *)frameBytes, (uchar *)image.bits(), videoFrame->GetWidth(), videoFrame->GetHeight());
344     }*/
345 }
346
347 void MltDeviceCapture::saveFrame(Mlt::Frame& frame)
348 {
349     mlt_image_format format = mlt_image_rgb24;
350     int width = 0;
351     int height = 0;
352     const uchar* image = frame.get_image(format, width, height);
353     QImage qimage(width, height, QImage::Format_RGB888);
354     memcpy(qimage.bits(), image, width * height * 3);
355
356     // Re-enable overlay
357     Mlt::Service service(m_mltProducer->parent().get_service());
358     Mlt::Tractor tractor(service);
359     Mlt::Producer trackProducer(tractor.track(0));
360     trackProducer.set("hide", 0);
361     
362     qimage.save(m_capturePath);
363     emit frameSaved(m_capturePath);
364     m_capturePath.clear();
365 }
366
367 void MltDeviceCapture::captureFrame(const QString &path)
368 {
369     if (m_mltProducer == NULL || !m_mltProducer->is_valid()) return;
370
371     // Hide overlay track before doing the capture
372     Mlt::Service service(m_mltProducer->parent().get_service());
373     Mlt::Tractor tractor(service);
374     Mlt::Producer trackProducer(tractor.track(0));
375     mlt_service_lock(service.get_service());
376     trackProducer.set("hide", 1);
377     m_mltConsumer->purge();
378     mlt_service_unlock(service.get_service());
379     m_capturePath = path;
380     // Wait for 5 frames before capture to make sure overlay is gone
381     doCapture = 5;
382 }
383
384 bool MltDeviceCapture::slotStartCapture(const QString &params, const QString &path, const QString &playlist, int livePreview, bool xmlPlaylist)
385 {
386     stop();
387     m_livePreview = livePreview;
388     m_frameCount = 0;
389     m_droppedFrames = 0;
390     if (m_mltProfile) delete m_mltProfile;
391     char *tmp = qstrdup(m_activeProfile.toUtf8().constData());
392     m_mltProfile = new Mlt::Profile(tmp);
393     delete[] tmp;
394     m_mltProfile->get_profile()->is_explicit = 1;
395     kDebug()<<"-- CREATING CAP: "<<params<<", PATH: "<<path;
396     tmp = qstrdup(QString("avformat:" + path).toUtf8().constData());
397     m_mltConsumer = new Mlt::Consumer(*m_mltProfile, tmp);
398     m_mltConsumer->set("real_time", -KdenliveSettings::mltthreads());
399     delete[] tmp;
400
401     QStringList paramList = params.split(" ", QString::SkipEmptyParts);
402     char *tmp2;
403     for (int i = 0; i < paramList.count(); i++) {
404         tmp = qstrdup(paramList.at(i).section("=", 0, 0).toUtf8().constData());
405         QString value = paramList.at(i).section("=", 1, 1);
406         if (value == "%threads") value = QString::number(QThread::idealThreadCount());
407         tmp2 = qstrdup(value.toUtf8().constData());
408         m_mltConsumer->set(tmp, tmp2);
409         delete[] tmp;
410         delete[] tmp2;
411     }
412     
413     if (m_mltConsumer == NULL || !m_mltConsumer->is_valid()) {
414         if (m_mltConsumer) {
415             delete m_mltConsumer;
416             m_mltConsumer = NULL;
417         }
418         return false;
419     }
420     
421     // FIXME: the event object returned by the listen gets leaked...
422     if (m_livePreview < 2) m_mltConsumer->listen("consumer-frame-render", this, (mlt_listener) rec_consumer_frame_show);
423     tmp = qstrdup(playlist.toUtf8().constData());
424     if (xmlPlaylist) {
425         // create an xml producer
426         m_mltProducer = new Mlt::Producer(*m_mltProfile, "xml-string", tmp);
427     }
428     else {
429         // create a producer based on mltproducer parameter
430         m_mltProducer = new Mlt::Producer(*m_mltProfile, tmp);
431     }
432     delete[] tmp;
433
434     if (m_mltProducer == NULL || !m_mltProducer->is_valid()) {
435         kDebug()<<"//// ERROR CREATRING PROD";
436         return false;
437     }
438     
439     m_mltConsumer->connect(*m_mltProducer);
440     if (m_mltConsumer->start() == -1) {
441         delete m_mltConsumer;
442         m_mltConsumer = NULL;
443         return 0;
444     }
445     m_captureDisplayWidget->start();
446     return 1;
447 }
448
449
450 void MltDeviceCapture::setOverlay(const QString &path)
451 {
452     if (m_mltProducer == NULL || !m_mltProducer->is_valid()) return;
453     Mlt::Producer parentProd(m_mltProducer->parent());
454     if (parentProd.get_producer() == NULL) {
455         kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
456         return;
457     }
458
459     Mlt::Service service(parentProd.get_service());
460     if (service.type() != tractor_type) {
461         kWarning() << "// TRACTOR PROBLEM";
462         return;
463     }
464     Mlt::Tractor tractor(service);
465     if ( tractor.count() < 2) {
466         kWarning() << "// TRACTOR PROBLEM";
467         return;
468     }
469     mlt_service_lock(service.get_service());
470     Mlt::Producer trackProducer(tractor.track(0));
471     Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
472
473     trackPlaylist.remove(0);
474     if (path.isEmpty()) {
475         mlt_service_unlock(service.get_service());
476         return;
477     }
478
479     // Add overlay clip
480     char *tmp = qstrdup(path.toUtf8().constData());
481     Mlt::Producer *clip = new Mlt::Producer (*m_mltProfile, "loader", tmp);
482     delete[] tmp;
483     clip->set_in_and_out(0, 99999);
484     trackPlaylist.insert_at(0, clip, 1);
485
486     // Add transition
487     mlt_service serv = m_mltProducer->parent().get_service();
488     mlt_service nextservice = mlt_service_get_producer(serv);
489     mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
490     QString mlt_type = mlt_properties_get(properties, "mlt_type");
491     if (mlt_type != "transition") {
492         // transition does not exist, add it
493         Mlt::Field *field = tractor.field();
494         Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, "composite");
495         transition->set_in_and_out(0, 0);
496         transition->set("geometry", "0/0:100%x100%:70");
497         transition->set("fill", 1);
498         transition->set("operator", "and");
499         transition->set("a_track", 0);
500         transition->set("b_track", 1);
501         field->plant_transition(*transition, 0, 1);
502     }
503     mlt_service_unlock(service.get_service());
504     //delete clip;
505 }
506
507 void MltDeviceCapture::setOverlayEffect(const QString &tag, QStringList parameters)
508 {
509     if (m_mltProducer == NULL || !m_mltProducer->is_valid()) return;
510     Mlt::Service service(m_mltProducer->parent().get_service());
511     Mlt::Tractor tractor(service);
512     Mlt::Producer trackProducer(tractor.track(0));
513     Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
514     Mlt::Service trackService(trackProducer.get_service());
515
516     mlt_service_lock(service.get_service());
517
518     // delete previous effects
519     Mlt::Filter *filter;
520     filter = trackService.filter(0);
521     if (filter && !tag.isEmpty()) {
522         QString currentService = filter->get("mlt_service");
523         if (currentService == tag) {
524             // Effect is already there
525             mlt_service_unlock(service.get_service());
526             return;
527         }
528     }
529     while (filter) {
530         trackService.detach(*filter);
531         delete filter;
532         filter = trackService.filter(0);
533     }
534     
535     if (tag.isEmpty()) {
536         mlt_service_unlock(service.get_service());
537         return;
538     }
539     
540     char *tmp = qstrdup(tag.toUtf8().constData());
541     filter = new Mlt::Filter(*m_mltProfile, tmp);
542     delete[] tmp;
543     if (filter && filter->is_valid()) {
544         for (int j = 0; j < parameters.count(); j++) {
545             filter->set(parameters.at(j).section("=", 0, 0).toUtf8().constData(), parameters.at(j).section("=", 1, 1).toUtf8().constData());
546         }
547         trackService.attach(*filter);
548     }
549     mlt_service_unlock(service.get_service());
550 }
551
552 void MltDeviceCapture::mirror(bool activate)
553 {
554     if (m_mltProducer == NULL || !m_mltProducer->is_valid()) return;
555     Mlt::Service service(m_mltProducer->parent().get_service());
556     Mlt::Tractor tractor(service);
557     Mlt::Producer trackProducer(tractor.track(1));
558     Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
559     Mlt::Service trackService(trackProducer.get_service());
560
561     mlt_service_lock(service.get_service());
562
563     // delete previous effects
564     Mlt::Filter *filter;
565     filter = trackService.filter(0);
566     while (filter) {
567         trackService.detach(*filter);
568         delete filter;
569         filter = trackService.filter(0);
570     }
571
572     if (!activate) {
573         mlt_service_unlock(service.get_service());
574         return;
575     }
576
577     filter = new Mlt::Filter(*m_mltProfile, "mirror");
578     if (filter && filter->is_valid()) {
579         filter->set("mirror", "flip");
580         trackService.attach(*filter);
581     }
582     mlt_service_unlock(service.get_service());
583 }
584
585 void MltDeviceCapture::uyvy2rgb(unsigned char *yuv_buffer, int width, int height)
586 {
587     processingImage = true;
588     QImage image(width, height, QImage::Format_RGB888);
589     unsigned char *rgb_buffer = image.bits();    
590
591     int len;
592     int r, g, b;
593     int Y, U, V, Y2;
594     int rgb_ptr, y_ptr, t;
595
596     len = width * height / 2;
597
598     rgb_ptr = 0;
599     y_ptr = 0;
600
601     for (t = 0; t < len; t++) { 
602       
603
604         Y = yuv_buffer[y_ptr];
605         U = yuv_buffer[y_ptr+1];
606         Y2 = yuv_buffer[y_ptr+2];
607         V = yuv_buffer[y_ptr+3];
608         y_ptr += 4;
609
610         r = ((298 * (Y - 16)               + 409 * (V - 128) + 128) >> 8);
611
612         g = ((298 * (Y - 16) - 100 * (U - 128) - 208 * (V - 128) + 128) >> 8);
613
614         b = ((298 * (Y - 16) + 516 * (U - 128)               + 128) >> 8);
615
616         if (r > 255) r = 255;
617         if (g > 255) g = 255;
618         if (b > 255) b = 255;
619
620         if (r < 0) r = 0;
621         if (g < 0) g = 0;
622         if (b < 0) b = 0;
623
624         rgb_buffer[rgb_ptr] = r;
625         rgb_buffer[rgb_ptr+1] = g;
626         rgb_buffer[rgb_ptr+2] = b;
627         rgb_ptr += 3;
628
629
630         r = ((298 * (Y2 - 16)               + 409 * (V - 128) + 128) >> 8);
631
632         g = ((298 * (Y2 - 16) - 100 * (U - 128) - 208 * (V - 128) + 128) >> 8);
633
634         b = ((298 * (Y2 - 16) + 516 * (U - 128)               + 128) >> 8);
635
636         if (r > 255) r = 255;
637         if (g > 255) g = 255;
638         if (b > 255) b = 255;
639
640         if (r < 0) r = 0;
641         if (g < 0) g = 0;
642         if (b < 0) b = 0;
643
644         rgb_buffer[rgb_ptr] = r;
645         rgb_buffer[rgb_ptr+1] = g;
646         rgb_buffer[rgb_ptr+2] = b;
647         rgb_ptr += 3;
648     }
649     //emit imageReady(image);
650     m_captureDisplayWidget->setImage(image);
651     emit unblockPreview();
652     //processingImage = false;
653 }
654
655 void MltDeviceCapture::slotPreparePreview()
656 {
657     QTimer::singleShot(1000, this, SLOT(slotAllowPreview()));
658 }
659
660 void MltDeviceCapture::slotAllowPreview()
661 {
662     processingImage = false;
663 }
664
665