]> git.sesse.net Git - kdenlive/blob - src/clipstabilize.cpp
stabilize has now all option enabled in dialog
[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
24 #include <KDebug>
25 #include <mlt++/Mlt.h>
26 #include "kdenlivesettings.h"
27 #include <KGlobalSettings>
28 #include <KMessageBox>
29 #include <QtConcurrentRun>
30 #include <QTimer>
31 #include <QSlider>
32 #include <KFileDialog>
33
34
35 ClipStabilize::ClipStabilize(KUrl::List urls, const QString &params, Mlt::Filter* filter,QWidget * parent) :
36         QDialog(parent), m_urls(urls), m_duration(0),m_profile(NULL),m_consumer(NULL),m_playlist(NULL),m_filter(filter),vbox(NULL)
37 {
38     setFont(KGlobalSettings::toolBarFont());
39     setupUi(this);
40     setAttribute(Qt::WA_DeleteOnClose);
41     log_text->setHidden(true);
42     setWindowTitle(i18n("Stabilize Clip"));
43     auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
44         m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
45         filtername=params;
46
47     if (m_urls.count() == 1) {
48         QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
49         QString newFile = fileName.append(".mlt");
50         KUrl dest(newFile);
51         source_url->setUrl(m_urls.at(0));
52         dest_url->setMode(KFile::File);
53         dest_url->setUrl(dest);
54         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
55         urls_list->setHidden(true);
56         connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
57     } else {
58         label_source->setHidden(true);
59         source_url->setHidden(true);
60         label_dest->setText(i18n("Destination folder"));
61         dest_url->setMode(KFile::Directory);
62         dest_url->setUrl(KUrl(m_urls.at(0).directory()));
63         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
64         for (int i = 0; i < m_urls.count(); i++)
65             urls_list->addItem(m_urls.at(i).path());
66     }
67     if (!params.isEmpty()) {
68         label_profile->setHidden(true);
69         profile_list->setHidden(true);
70         //ffmpeg_params->setPlainText(params.simplified());
71         /*if (!description.isEmpty()) {
72             transcode_info->setText(description);
73         } else transcode_info->setHidden(true);*/
74     } 
75
76         if (filtername=="videostab"){
77                 QStringList ls;
78                 ls << "shutterangle,type,int,value,0,min,0,max,100,factor,1";
79                 fillParamaters(ls);
80         }else if (filtername=="videostab2"){
81                 QStringList ls;
82                 ls << "accuracy,type,int,value,4,min,1,max,10,factor,1";
83                 ls << "stepsize,type,int,value,6,min,0,max,100,factor,1";
84                 ls << "algo,type,int,value,1,min,0,max,1,factor,1";
85                 ls << "mincontrast,type,int,value,30,min,0,max,100,factor,100";
86                 ls << "show,type,int,value,0,min,0,max,2,factor,1";
87                 ls << "smoothing,type,int,value,10,min,0,max,100,factor,1";
88                 ls << "maxshift,type,int,value,-1,min,-1,max,1000,factor,1";
89                 ls << "maxangle,type,int,value,-1,min,-1,max,1000,factor,1";
90                 ls << "crop,type,int,value,0,min,0,max,1,factor,1";
91                 ls << "invert,type,int,value,0,min,0,max,1,factor,1";
92                 ls << "realtive,type,int,value,1,min,0,max,1,factor,1";
93                 ls << "zoom,type,int,value,0,min,-500,max,500,factor,1";
94                 ls << "optzoom,type,int,value,1,min,0,max,1,factor,1";
95                 ls << "sharpen,type,int,value,8,min,0,max,100,factor,10";
96                 fillParamaters(ls);
97
98         }
99     connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartStabilize()));
100         connect(buttonBox,SIGNAL(rejected()), this, SLOT(slotAbortStabilize()));
101
102         m_timer=new QTimer(this);
103         connect(m_timer, SIGNAL(timeout()), this, SLOT(slotShowStabilizeInfo()));
104
105         vbox=new QVBoxLayout(optionsbox);
106         QHashIterator<QString,QHash<QString,QString> > hi(m_ui_params);
107         while(hi.hasNext()){
108                 hi.next();
109                 QHash<QString,QString> val=hi.value();
110                 QLabel *l=new QLabel(hi.key(),this);
111                 QSlider* s=new QSlider(Qt::Horizontal,this);
112                 if (val["factor"].toInt()>1){
113                         s->setTickInterval(val["factor"].toInt()/10);
114                 }
115                 s->setTickPosition(QSlider::TicksBelow);
116                 s->setMaximum(val["max"].toInt());
117                 s->setMinimum(val["min"].toInt());
118                 s->setValue(val["value"].toInt());
119                 s->setObjectName(hi.key());
120                 connect(s,SIGNAL(sliderMoved(int)),this,SLOT(slotUpdateParams()));
121                 QHBoxLayout *hbox=new QHBoxLayout();
122                 hbox->addWidget(s);
123                 hbox->addWidget(l);
124                 vbox->addLayout(hbox);
125         }
126
127     adjustSize();
128 }
129
130 ClipStabilize::~ClipStabilize()
131 {
132     /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
133         m_stabilizeProcess.close();
134     }*/
135         if (m_stabilizeRun.isRunning()){
136                 if (m_consumer){
137                         m_consumer->stop();
138                 }
139                 m_stabilizeRun.waitForFinished();
140         }
141         if (m_profile) free (m_profile);
142         if (m_consumer) free (m_consumer);
143         if (m_playlist) free (m_playlist);
144 }
145
146 void ClipStabilize::slotStartStabilize()
147 {
148     if (m_consumer && !m_consumer->is_stopped()) {
149         return;
150     }
151     m_duration = 0;
152     QStringList parameters;
153     QString destination;
154     //QString params = ffmpeg_params->toPlainText().simplified();
155     if (urls_list->count() > 0) {
156         source_url->setUrl(m_urls.takeFirst());
157         destination = dest_url->url().path(KUrl::AddTrailingSlash)+ source_url->url().fileName()+".mlt";
158         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
159         if (matching.count() > 0) {
160             matching.at(0)->setFlags(Qt::ItemIsSelectable);
161             urls_list->setCurrentItem(matching.at(0));
162         }
163     } else {
164         destination = dest_url->url().path();
165     }
166     QString s_url = source_url->url().path();
167
168     if (QFile::exists(destination)) {
169                         if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination )) == KMessageBox::No) return;
170     }
171     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
172
173         if (m_profile){
174                 m_playlist= new Mlt::Playlist;
175                 Mlt::Filter filter(*m_profile,filtername.toUtf8().data());
176                 QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
177                 while (it.hasNext()){
178                         it.next();
179                         filter.set(
180                                         it.key().toAscii().data(),
181                                         QString::number(
182                                                 (double)(it.value()["value"].toDouble()/it.value()["factor"].toDouble())
183                                         ).toAscii().data()
184                         );
185                 }
186                 Mlt::Producer p(*m_profile,s_url.toUtf8().data());
187                 if (p.is_valid()) {
188                         m_playlist->append(p);
189                         m_playlist->attach(filter);
190                         m_consumer= new Mlt::Consumer(*m_profile,"xml",destination.toUtf8().data());
191                         m_consumer->set("all","1");
192                         m_consumer->set("real_time","-2");
193                         m_consumer->connect(*m_playlist);
194                         m_stabilizeRun = QtConcurrent::run(this, &ClipStabilize::slotRunStabilize);
195                         m_timer->start(500);
196                         button_start->setEnabled(false);
197                 }
198         }
199
200 }
201
202 void ClipStabilize::slotRunStabilize()
203 {
204         if (m_consumer)
205         {
206                 m_consumer->run();
207         }
208 }
209
210 void ClipStabilize::slotAbortStabilize()
211 {
212         if (m_consumer)
213         {
214                 m_timer->stop();
215                 m_consumer->stop();
216                 slotStabilizeFinished(false);
217         }
218 }
219
220 void ClipStabilize::slotShowStabilizeInfo()
221 {
222         if (m_playlist){
223         job_progress->setValue((int) (100.0 * m_consumer->position()/m_playlist->get_out() ));
224                 if (m_consumer->position()==m_playlist->get_out()){
225                         m_timer->stop();
226                         slotStabilizeFinished(true);
227                 }
228         }
229 }
230
231 void ClipStabilize::slotStabilizeFinished(bool success)
232 {
233     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
234     button_start->setEnabled(true);
235     m_duration = 0;
236     if (m_stabilizeRun.isRunning()){
237         if (m_consumer){
238             m_consumer->stop();
239         }
240         m_stabilizeRun.waitForFinished();
241     }
242
243     if (success) {
244         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilize finished."));
245         if (auto_add->isChecked()) {
246             KUrl url;
247             if (urls_list->count() > 0) {
248                                 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName()+".mlt");
249             } else url = dest_url->url();
250             emit addClip(url);
251         }
252         if (urls_list->count() > 0 && m_urls.count() > 0) {
253             slotStartStabilize();
254             return;
255         } else if (auto_close->isChecked()) accept();
256     } else {
257         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilizing FAILED!"));
258     }
259         if (m_playlist){
260                 free(m_playlist);
261                 m_playlist=NULL;
262         }
263         if (m_consumer){
264                 free(m_consumer);
265                 m_consumer=NULL;
266         }
267
268 }
269
270 void ClipStabilize::slotUpdateParams()
271 {
272         for (int i=0;i<vbox->count();i++){
273                 QLayout *bo=vbox->itemAt(i)->layout();
274                 QWidget* w=bo->itemAt(1)->widget();
275                 QString name=w->objectName();
276                 if (name !="" && m_ui_params.contains(name)){
277                         if (m_ui_params[name]["type"]=="int"){
278                                 QSlider *s=(QSlider*)w;
279                                 m_ui_params[name]["value"]=QString::number((double)(s->value()));
280                         }
281                 }
282         }
283 }
284
285 void ClipStabilize::fillParamaters(QStringList lst)
286 {
287
288         m_ui_params.clear();
289         while (!lst.isEmpty()){
290                 QString vallist=lst.takeFirst();
291                 QStringList cont=vallist.split(",");
292                 QString name=cont.takeFirst();
293                 while (!cont.isEmpty()){
294                         QString valname=cont.takeFirst();
295                         QString val;
296                         if (!cont.isEmpty()){
297                                 val=cont.takeFirst();
298                         }
299                         m_ui_params[name][valname]=val;
300                 }
301         }
302
303 }
304
305
306 #include "clipstabilize.moc"
307
308