]> git.sesse.net Git - kdenlive/blob - src/clipstabilize.cpp
Merge branch 'master' into stabilize
[kdenlive] / src / clipstabilize.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *   Copyright (C) 2011 by Marco Gittler (marco@gitma.de)                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
19  ***************************************************************************/
20
21
22 #include "clipstabilize.h"
23 #include "doubleparameterwidget.h"
24
25 #include <KDebug>
26 #include <mlt++/Mlt.h>
27 #include "kdenlivesettings.h"
28 #include <KGlobalSettings>
29 #include <KMessageBox>
30 #include <KColorScheme>
31 #include <QtConcurrentRun>
32 #include <QTimer>
33 #include <QSlider>
34 #include <KFileDialog>
35
36 ClipStabilize::ClipStabilize(KUrl::List urls, const QString &params, Mlt::Filter* filter,QWidget * parent) :
37         QDialog(parent), m_urls(urls), m_duration(0),m_profile(NULL),m_consumer(NULL),m_playlist(NULL),m_filter(filter),vbox(NULL)
38 {
39     setFont(KGlobalSettings::toolBarFont());
40     setupUi(this);
41     setAttribute(Qt::WA_DeleteOnClose);
42     log_text->setHidden(true);
43     setWindowTitle(i18n("Stabilize Clip"));
44     auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
45         m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
46         filtername=params;
47
48
49         QPalette p = palette();
50         KColorScheme scheme(p.currentColorGroup(), KColorScheme::View, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
51         QColor dark_bg = scheme.shade(KColorScheme::DarkShade);
52         QColor selected_bg = scheme.decoration(KColorScheme::FocusColor).color();
53         QColor hover_bg = scheme.decoration(KColorScheme::HoverColor).color();
54         QColor light_bg = scheme.shade(KColorScheme::LightShade);
55
56         QString stylesheet(QString("QProgressBar:horizontal {border: 1px solid %1;border-radius:0px;border-top-left-radius: 4px;border-bottom-left-radius: 4px;border-right: 0px;background:%4;padding: 0px;text-align:left center}\
57                                 QProgressBar:horizontal#dragOnly {background: %1} QProgressBar:horizontal:hover#dragOnly {background: %3} QProgressBar:horizontal:hover {border: 1px solid %3;border-right: 0px;}\
58                                 QProgressBar::chunk:horizontal {background: %1;} QProgressBar::chunk:horizontal:hover {background: %3;}\
59                                 QProgressBar:horizontal[inTimeline=\"true\"] { border: 1px solid %2;border-right: 0px;background: %4;padding: 0px;text-align:left center } QProgressBar::chunk:horizontal[inTimeline=\"true\"] {background: %2;}\
60                                 QAbstractSpinBox#dragBox {border: 1px solid %1;border-top-right-radius: 4px;border-bottom-right-radius: 4px;padding-right:0px;} QAbstractSpinBox::down-button#dragBox {width:0px;padding:0px;}\
61                                 QAbstractSpinBox::up-button#dragBox {width:0px;padding:0px;} QAbstractSpinBox[inTimeline=\"true\"]#dragBox { border: 1px solid %2;} QAbstractSpinBox:hover#dragBox {border: 1px solid %3;} ")
62                         .arg(dark_bg.name()).arg(selected_bg.name()).arg(hover_bg.name()).arg(light_bg.name()));
63         setStyleSheet(stylesheet);
64
65     if (m_urls.count() == 1) {
66         QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
67         QString newFile = fileName.append(".mlt");
68         KUrl dest(newFile);
69         source_url->setUrl(m_urls.at(0));
70         dest_url->setMode(KFile::File);
71         dest_url->setUrl(dest);
72         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
73         urls_list->setHidden(true);
74         connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
75     } else {
76         label_source->setHidden(true);
77         source_url->setHidden(true);
78         label_dest->setText(i18n("Destination folder"));
79         dest_url->setMode(KFile::Directory);
80         dest_url->setUrl(KUrl(m_urls.at(0).directory()));
81         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
82         for (int i = 0; i < m_urls.count(); i++)
83             urls_list->addItem(m_urls.at(i).path());
84     }
85     if (!params.isEmpty()) {
86         label_profile->setHidden(true);
87         profile_list->setHidden(true);
88         //ffmpeg_params->setPlainText(params.simplified());
89         /*if (!description.isEmpty()) {
90             transcode_info->setText(description);
91         } else transcode_info->setHidden(true);*/
92     } 
93
94         if (filtername=="videostab"){
95                 QStringList ls;
96                 ls << "shutterangle,type,int,value,0,min,0,max,100,tooltip,Angle that Images could be maximum rotated";
97                 fillParameters(ls);
98         }else if (filtername=="videostab2"){
99                 QStringList ls;
100                 ls << "accuracy,type,int,value,4,min,1,max,10,tooltip,Accuracy of Shakiness detection";
101                 ls << "shakiness,type,int,value,4,min,1,max,10,tooltip,How shaky is the Video";
102                 ls << "stepsize,type,int,value,6,min,0,max,100,tooltip,Stepsize of Detection process minimum around";
103                 ls << "algo,type,bool,value,1,min,0,max,1,tooltip,0 = Bruteforce 1 = small measurement fields";
104                 ls << "mincontrast,type,double,value,0.3,min,0,max,1,factor,1,decimals,2,tooltip,Below this Contrast Field is discarded";
105                 ls << "show,type,int,value,0,min,0,max,2,tooltip,0 = draw nothing. 1 or 2 show fields and transforms";
106                 ls << "smoothing,type,int,value,10,min,0,max,100,tooltip,number of frames for lowpass filtering";
107                 ls << "maxshift,type,int,value,-1,min,-1,max,1000,tooltip,max number of pixels to shift";
108                 ls << "maxangle,type,int,value,-1,min,-1,max,1000,tooltip,max anglen to rotate (in rad)";
109                 ls << "crop,type,bool,value,0,min,0,max,1,tooltip,0 = keep border  1 = black background";
110                 ls << "invert,type,bool,value,0,min,0,max,1,tooltip,invert transform";
111                 ls << "realtive,type,bool,value,1,min,0,max,1,tooltip,0 = absolute transform  1= relative";
112                 ls << "zoom,type,int,value,0,min,-500,max,500,tooltip,additional zoom during transform";
113                 ls << "optzoom,type,bool,value,1,min,0,max,1,tooltip,use optimal zoom (calulated from transforms)";
114                 ls << "sharpen,type,double,value,0.8,min,0,max,1,decimals,1,tooltip,sharpen transformed image";
115                 fillParameters(ls);
116
117         }
118     connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartStabilize()));
119         connect(buttonBox,SIGNAL(rejected()), this, SLOT(slotAbortStabilize()));
120
121         m_timer=new QTimer(this);
122         connect(m_timer, SIGNAL(timeout()), this, SLOT(slotShowStabilizeInfo()));
123
124         vbox=new QVBoxLayout(optionsbox);
125         QHashIterator<QString,QHash<QString,QString> > hi(m_ui_params);
126         while(hi.hasNext()){
127                 hi.next();
128                 QHash<QString,QString> val=hi.value();
129                 if (val["type"]=="int" || val["type"]=="double"){
130                         DoubleParameterWidget *dbl=new DoubleParameterWidget(hi.key(), val["value"].toDouble(),
131                                         val["min"].toDouble(),val["max"].toDouble(),val["value"].toDouble(),
132                                         "",0/*id*/,""/*suffix*/,val["decimals"]!=""?val["decimals"].toInt():0,this);
133                         dbl->setObjectName(hi.key());
134                         dbl->setToolTip(val["tooltip"]);
135                         connect(dbl,SIGNAL(valueChanged(double)),this,SLOT(slotUpdateParams()));
136                         vbox->addWidget(dbl);
137                 }else if (val["type"]=="bool"){
138                         QCheckBox *ch=new QCheckBox(hi.key(),this);
139                         ch->setCheckState(val["value"] == "0" ? Qt::Unchecked : Qt::Checked);
140                         ch->setObjectName(hi.key());
141                         connect(ch, SIGNAL(stateChanged(int)) , this,SLOT(slotUpdateParams()));
142                         ch->setToolTip(val["tooltip"]);
143                         vbox->addWidget(ch);
144                 
145                 }
146         }
147     adjustSize();
148 }
149
150 ClipStabilize::~ClipStabilize()
151 {
152     /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
153         m_stabilizeProcess.close();
154     }*/
155         if (m_stabilizeRun.isRunning()){
156                 if (m_consumer){
157                         m_consumer->stop();
158                 }
159                 m_stabilizeRun.waitForFinished();
160         }
161         if (m_profile) free (m_profile);
162         if (m_consumer) free (m_consumer);
163         if (m_playlist) free (m_playlist);
164 }
165
166 void ClipStabilize::slotStartStabilize()
167 {
168     if (m_consumer && !m_consumer->is_stopped()) {
169         return;
170     }
171     m_duration = 0;
172     QStringList parameters;
173     QString destination;
174     //QString params = ffmpeg_params->toPlainText().simplified();
175     if (urls_list->count() > 0) {
176         source_url->setUrl(m_urls.takeFirst());
177         destination = dest_url->url().path(KUrl::AddTrailingSlash)+ source_url->url().fileName()+".mlt";
178         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
179         if (matching.count() > 0) {
180             matching.at(0)->setFlags(Qt::ItemIsSelectable);
181             urls_list->setCurrentItem(matching.at(0));
182         }
183     } else {
184         destination = dest_url->url().path();
185     }
186     QString s_url = source_url->url().path();
187
188     if (QFile::exists(destination)) {
189                         if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination )) == KMessageBox::No) return;
190     }
191     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
192
193         if (m_profile){
194                 m_playlist= new Mlt::Playlist;
195                 Mlt::Filter filter(*m_profile,filtername.toUtf8().data());
196                 QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
197                 while (it.hasNext()){
198                         it.next();
199                         filter.set(
200                                         it.key().toAscii().data(),
201                                         QString::number(it.value()["value"].toDouble()).toAscii().data());
202                 }
203                 Mlt::Producer p(*m_profile,s_url.toUtf8().data());
204                 if (p.is_valid()) {
205                         m_playlist->append(p);
206                         m_playlist->attach(filter);
207                         m_consumer= new Mlt::Consumer(*m_profile,"xml",destination.toUtf8().data());
208                         m_consumer->set("all","1");
209                         m_consumer->set("real_time","-2");
210                         m_consumer->connect(*m_playlist);
211                         m_stabilizeRun = QtConcurrent::run(this, &ClipStabilize::slotRunStabilize);
212                         m_timer->start(500);
213                         button_start->setEnabled(false);
214                 }
215         }
216
217 }
218
219 void ClipStabilize::slotRunStabilize()
220 {
221         if (m_consumer)
222         {
223                 m_consumer->run();
224         }
225 }
226
227 void ClipStabilize::slotAbortStabilize()
228 {
229         if (m_consumer)
230         {
231                 m_timer->stop();
232                 m_consumer->stop();
233                 slotStabilizeFinished(false);
234         }
235 }
236
237 void ClipStabilize::slotShowStabilizeInfo()
238 {
239         if (m_playlist){
240         job_progress->setValue((int) (100.0 * m_consumer->position()/m_playlist->get_out() ));
241                 if (m_consumer->position()==m_playlist->get_out()){
242                         m_timer->stop();
243                         slotStabilizeFinished(true);
244                 }
245         }
246 }
247
248 void ClipStabilize::slotStabilizeFinished(bool success)
249 {
250     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
251     button_start->setEnabled(true);
252     m_duration = 0;
253     if (m_stabilizeRun.isRunning()){
254         if (m_consumer){
255             m_consumer->stop();
256         }
257         m_stabilizeRun.waitForFinished();
258     }
259
260     if (success) {
261         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilize finished."));
262         if (auto_add->isChecked()) {
263             KUrl url;
264             if (urls_list->count() > 0) {
265                                 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName()+".mlt");
266             } else url = dest_url->url();
267             emit addClip(url);
268         }
269         if (urls_list->count() > 0 && m_urls.count() > 0) {
270             slotStartStabilize();
271             return;
272         } else if (auto_close->isChecked()) accept();
273     } else {
274         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilizing FAILED!"));
275     }
276         if (m_playlist){
277                 free(m_playlist);
278                 m_playlist=NULL;
279         }
280         if (m_consumer){
281                 free(m_consumer);
282                 m_consumer=NULL;
283         }
284
285 }
286
287 void ClipStabilize::slotUpdateParams()
288 {
289         for (int i=0;i<vbox->count();i++){
290                 QWidget* w=vbox->itemAt(i)->widget();
291                 QString name=w->objectName();
292                 if (name !="" && m_ui_params.contains(name)){
293                         if (m_ui_params[name]["type"]=="int" || m_ui_params[name]["type"]=="double"){
294                                 DoubleParameterWidget *dbl=(DoubleParameterWidget*)w;
295                                 m_ui_params[name]["value"]=QString::number((double)(dbl->getValue()));
296                         }else if (m_ui_params[name]["type"]=="bool"){
297                                 QCheckBox *ch=(QCheckBox*)w;
298                                 m_ui_params[name]["value"]= ch->checkState() == Qt::Checked ? "1" : "0" ;
299                         }
300                 }
301         }
302 }
303
304 void ClipStabilize::fillParameters(QStringList lst)
305 {
306
307         m_ui_params.clear();
308         while (!lst.isEmpty()){
309                 QString vallist=lst.takeFirst();
310                 QStringList cont=vallist.split(",");
311                 QString name=cont.takeFirst();
312                 while (!cont.isEmpty()){
313                         QString valname=cont.takeFirst();
314                         QString val;
315                         if (!cont.isEmpty()){
316                                 val=cont.takeFirst();
317                         }
318                         m_ui_params[name][valname]=val;
319                 }
320         }
321
322 }
323
324
325 #include "clipstabilize.moc"
326
327