MSVC - accommodate some newly introduced calls to 'rintf()'
[ardour.git] / gtk2_ardour / export_video_dialog.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3     Author: Robin Gareus <robin@gareus.org>
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 Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20 #include <cstdio>
21 #include <string>
22 #include <sstream>
23 #include <iomanip>
24
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29
30 #include <sigc++/bind.h>
31
32 #include <glib/gstdio.h>
33
34 #include "pbd/error.h"
35 #include "pbd/convert.h"
36 #include "gtkmm2ext/keyboard.h"
37 #include "gtkmm2ext/utils.h"
38 #include "ardour/session_directory.h"
39 #include "ardour/profile.h"
40 #include "ardour/template_utils.h"
41 #include "ardour/session.h"
42 #include "ardour_ui.h"
43 #include "gui_thread.h"
44
45 #include "ardour/export_handler.h"
46 #include "ardour/export_status.h"
47 #include "ardour/export_timespan.h"
48 #include "ardour/export_channel_configuration.h"
49 #include "ardour/export_format_specification.h"
50 #include "ardour/export_filename.h"
51 #include "ardour/route.h"
52 #include "ardour/session_metadata.h"
53 #include "ardour/broadcast_info.h"
54
55 #include "opts.h"
56 #include "export_video_dialog.h"
57 #include "utils_videotl.h"
58 #include "i18n.h"
59
60 #ifdef COMPILER_MSVC
61 #define rintf(x) round((x) + 0.5)
62 #endif
63
64 using namespace Gtk;
65 using namespace std;
66 using namespace PBD;
67 using namespace ARDOUR;
68 using namespace VideoUtils;
69
70 ExportVideoDialog::ExportVideoDialog ()
71         : ArdourDialog (_("Export Video File "))
72         , _aborted(false)
73         , _twopass(false)
74         , _firstpass(false)
75         , _normalize(false)
76         , _previous_progress(0)
77         , _transcoder(0)
78         , _video_source_aspect_ratio(-1)
79         , _suspend_signals(false)
80         , outfn_path_label (_("File:"), Gtk::ALIGN_LEFT)
81         , outfn_browse_button (_("Browse"))
82         , invid_path_label (_("Video:"), Gtk::ALIGN_LEFT)
83         , invid_browse_button (_("Browse"))
84         , transcode_button (_("Export"))
85         , abort_button (_("Abort"))
86         , scale_checkbox (_("Scale Video (W x H):"))
87         , scale_aspect (_("Retain Aspect"))
88         , width_adjustment (768, 128, 1920, 1, 16, 0)
89         , width_spinner (width_adjustment)
90         , height_adjustment (576, 128, 1920, 1, 16, 0)
91         , height_spinner (height_adjustment)
92         , aspect_checkbox (_("Set Aspect Ratio:"))
93         , normalize_checkbox (_("Normalize Audio"))
94         , twopass_checkbox (_("2 Pass Encoding"))
95         , optimizations_checkbox (_("Codec Optimizations:"))
96         , optimizations_label ("-")
97         , deinterlace_checkbox (_("Deinterlace"))
98         , bframes_checkbox (_("Use [2] B-frames (MPEG 2 or 4 only)"))
99         , fps_checkbox (_("Override FPS (Default is to retain FPS from the input video file):"))
100         , meta_checkbox (_("Include Session Metadata"))
101 #if 1 /* tentative debug mode */
102         , debug_checkbox (_("Debug Mode: Print ffmpeg command and output to stdout."))
103 #endif
104 {
105         set_name ("ExportVideoDialog");
106         set_modal (true);
107         set_skip_taskbar_hint (true);
108         set_resizable (false);
109
110         Gtk::Label* l;
111         vbox = manage (new VBox);
112         VBox* options_box = manage (new VBox);
113         HBox* path_hbox;
114
115         /* check if ffmpeg can be found */
116         _transcoder = new TranscodeFfmpeg("");
117         if (!_transcoder->ffexec_ok()) {
118                 l = manage (new Label (_("No ffprobe or ffmpeg executables could be found on this system. Video Export is not possible until you install those tools. See the Log window for more information."), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
119                 l->set_line_wrap();
120                 vbox->pack_start (*l, false, false, 8);
121                 get_vbox()->pack_start (*vbox, false, false);
122                 add_button (Stock::OK, RESPONSE_CANCEL);
123                 show_all_children ();
124                 return;
125         }
126         delete _transcoder; _transcoder = 0;
127
128         l = manage (new Label (_("<b>Output:</b> (file extension defines format)"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
129         l->set_use_markup ();
130         vbox->pack_start (*l, false, false, 4);
131
132         path_hbox = manage (new HBox);
133         path_hbox->pack_start (outfn_path_label, false, false, 3);
134         path_hbox->pack_start (outfn_path_entry, true, true, 3);
135         path_hbox->pack_start (outfn_browse_button, false, false, 3);
136         vbox->pack_start (*path_hbox, false, false, 2);
137
138         l = manage (new Label (_("<b>Input Video:</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
139         l->set_use_markup ();
140         vbox->pack_start (*l, false, false, 4);
141
142         path_hbox = manage (new HBox);
143         path_hbox->pack_start (invid_path_label, false, false, 3);
144         path_hbox->pack_start (invid_path_entry, true, true, 3);
145         path_hbox->pack_start (invid_browse_button, false, false, 3);
146         vbox->pack_start (*path_hbox, false, false, 2);
147
148         path_hbox = manage (new HBox);
149         l = manage (new Label (_("Audio:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
150         path_hbox->pack_start (*l, false, false, 3);
151         l = manage (new Label (_("Master Bus"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
152         path_hbox->pack_start (*l, false, false, 2);
153         vbox->pack_start (*path_hbox, false, false, 2);
154
155         insnd_combo.set_name ("PaddedButton");
156         insnd_combo.append_text (string_compose (_("from the %1 session's start to the session's end"), PROGRAM_NAME));
157         outfn_path_entry.set_width_chars(38);
158
159         l = manage (new Label (_("<b>Settings:</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
160         l->set_use_markup ();
161         options_box->pack_start (*l, false, true, 4);
162
163         Table* t = manage (new Table (4, 12));
164         t->set_spacings (4);
165         int ty = 0;
166         options_box->pack_start (*t, true, true, 4);
167         l = manage (new Label (_("Range:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
168         t->attach (*l, 0, 1, ty, ty+1);
169         t->attach (insnd_combo, 1, 4, ty, ty+1); ty++;
170         l = manage (new Label (_("Preset:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
171         t->attach (*l, 0, 1, ty, ty+1);
172         t->attach (preset_combo, 1, 4, ty, ty+1); ty++;
173         l = manage (new Label (_("Video Codec:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
174         t->attach (*l, 0, 1, ty, ty+1);
175         t->attach (video_codec_combo, 1, 2, ty, ty+1);
176         l = manage (new Label (_("Video KBit/s:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
177         t->attach (*l, 2, 3, ty, ty+1);
178         t->attach (video_bitrate_combo, 3, 4, ty, ty+1); ty++;
179         l = manage (new Label (_("Audio Codec:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
180         t->attach (*l, 0, 1, ty, ty+1);
181         t->attach (audio_codec_combo, 1, 2, ty, ty+1);
182         l = manage (new Label (_("Audio KBit/s:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
183         t->attach (*l, 2, 3, ty, ty+1);
184         t->attach (audio_bitrate_combo, 3, 4, ty, ty+1); ty++;
185         l = manage (new Label (_("Audio Samplerate:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
186         t->attach (*l, 0, 1, ty, ty+1);
187         t->attach (audio_samplerate_combo, 1, 2, ty, ty+1);
188         t->attach (normalize_checkbox, 2, 4, ty, ty+1); ty++;
189         t->attach (scale_checkbox, 0, 1, ty, ty+1);
190         t->attach (scale_aspect, 1, 2, ty, ty+1);
191         t->attach (width_spinner, 2, 3, ty, ty+1);
192         t->attach (height_spinner, 3, 4, ty, ty+1); ty++;
193         t->attach (fps_checkbox, 0, 3, ty, ty+1);
194         t->attach (fps_combo, 3, 4, ty, ty+1); ty++;
195         t->attach (twopass_checkbox, 0, 2, ty, ty+1);
196         t->attach (aspect_checkbox, 2, 3, ty, ty+1);
197         t->attach (aspect_combo, 3, 4, ty, ty+1); ty++;
198         t->attach (bframes_checkbox, 0, 2, ty, ty+1);
199         t->attach (deinterlace_checkbox, 2, 4, ty, ty+1); ty++;
200         t->attach (meta_checkbox, 2, 4, ty, ty+1); ty++;
201         t->attach (optimizations_checkbox, 0, 1, ty, ty+1);
202         t->attach (optimizations_label, 1, 4, ty, ty+1); ty++;
203 #if 1 /* tentative debug mode */
204         t->attach (debug_checkbox, 0, 4, ty, ty+1); ty++;
205 #endif
206
207         preset_combo.set_name ("PaddedButton");
208         preset_combo.append_text("none");
209         preset_combo.append_text("dvd-mp2");
210         preset_combo.append_text("dvd-NTSC");
211         preset_combo.append_text("dvd-PAL");
212         preset_combo.append_text("flv");
213         preset_combo.append_text("mpeg4");
214         preset_combo.append_text("mp4/h264/aac");
215         preset_combo.append_text("ogg");
216         preset_combo.append_text("webm");
217         preset_combo.append_text("you-tube");
218
219         audio_codec_combo.set_name ("PaddedButton");
220         audio_codec_combo.append_text(_("(default for format)"));
221         audio_codec_combo.append_text("ac3");
222         audio_codec_combo.append_text("aac");
223         audio_codec_combo.append_text("libmp3lame");
224         audio_codec_combo.append_text("libvorbis");
225         audio_codec_combo.append_text("mp2");
226         audio_codec_combo.append_text("pcm_s16le");
227
228         video_codec_combo.set_name ("PaddedButton");
229         video_codec_combo.append_text(_("(default for format)"));
230         video_codec_combo.append_text("flv");
231         video_codec_combo.append_text("libtheora");
232         video_codec_combo.append_text("mjpeg");
233         video_codec_combo.append_text("mpeg2video");
234         video_codec_combo.append_text("mpeg4");
235         video_codec_combo.append_text("h264");
236         video_codec_combo.append_text("vpx (webm)");
237         video_codec_combo.append_text("copy");
238
239         audio_bitrate_combo.set_name ("PaddedButton");
240         audio_bitrate_combo.append_text(_("(default)"));
241         audio_bitrate_combo.append_text("64k");
242         audio_bitrate_combo.append_text("128k");
243         audio_bitrate_combo.append_text("192k");
244         audio_bitrate_combo.append_text("256k");
245         audio_bitrate_combo.append_text("320k");
246
247         audio_samplerate_combo.set_name ("PaddedButton");
248         audio_samplerate_combo.append_text("22050");
249         audio_samplerate_combo.append_text("44100");
250         audio_samplerate_combo.append_text("48000");
251
252         video_bitrate_combo.set_name ("PaddedButton");
253         video_bitrate_combo.append_text(_("(default)"));
254         video_bitrate_combo.append_text(_("(retain)"));
255         video_bitrate_combo.append_text("200k");
256         video_bitrate_combo.append_text("800k");
257         video_bitrate_combo.append_text("2000k");
258         video_bitrate_combo.append_text("5000k");
259         video_bitrate_combo.append_text("8000k");
260
261         fps_combo.set_name ("PaddedButton");
262         fps_combo.append_text("23.976");
263         fps_combo.append_text("24");
264         fps_combo.append_text("24.976");
265         fps_combo.append_text("25");
266         fps_combo.append_text("29.97");
267         fps_combo.append_text("30");
268         fps_combo.append_text("59.94");
269         fps_combo.append_text("60");
270
271         aspect_combo.set_name ("PaddedButton");
272         aspect_combo.append_text("4:3");
273         aspect_combo.append_text("16:9");
274
275         vbox->pack_start (*options_box, false, true, 4);
276         get_vbox()->set_spacing (4);
277         get_vbox()->pack_start (*vbox, false, false);
278
279         progress_box = manage (new VBox);
280         progress_box->pack_start (pbar, false, false);
281         progress_box->pack_start (abort_button, false, false);
282         get_vbox()->pack_start (*progress_box, false, false);
283
284         scale_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportVideoDialog::scale_checkbox_toggled));
285         aspect_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportVideoDialog::aspect_checkbox_toggled));
286         fps_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportVideoDialog::fps_checkbox_toggled));
287         preset_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::preset_combo_changed));
288         video_codec_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::video_codec_combo_changed));
289         outfn_browse_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::open_outfn_dialog));
290         invid_browse_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::open_invid_dialog));
291         transcode_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::launch_export));
292         abort_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::abort_clicked));
293
294         invid_path_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::set_original_file_information));
295         width_spinner.signal_value_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::width_value_changed));
296         height_spinner.signal_value_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::height_value_changed));
297
298         cancel_button = add_button (Stock::CANCEL, RESPONSE_CANCEL);
299         get_action_area()->pack_start (transcode_button, false, false);
300         show_all_children ();
301         progress_box->hide();
302 }
303
304 ExportVideoDialog::~ExportVideoDialog ()
305 {
306         if (_transcoder) { delete _transcoder; _transcoder = 0;}
307 }
308
309 void
310 ExportVideoDialog::set_original_file_information()
311 {
312         assert(_transcoder == 0);
313         std::string infile = invid_path_entry.get_text();
314
315         if (scale_checkbox.get_active()) {
316                 // user may have set custom values already, don't touch.
317                 return;
318         }
319         if (infile == "" || !Glib::file_test(infile, Glib::FILE_TEST_EXISTS)) {
320                 return;
321         }
322
323         _transcoder = new TranscodeFfmpeg(infile);
324         if (_transcoder->probe_ok()) {
325                 _video_source_aspect_ratio = -1;
326                 width_spinner.set_value(_transcoder->get_width());
327                 height_spinner.set_value(_transcoder->get_height());
328                 _video_source_aspect_ratio = _transcoder->get_aspect();
329         }
330
331         delete _transcoder; _transcoder = 0;
332 }
333 void
334 ExportVideoDialog::apply_state (TimeSelection &tme, bool range)
335 {
336         _suspend_dirty = true; // TODO really just queue 'dirty' and mark session dirty on "Export"
337
338         export_range = tme;
339         _video_source_aspect_ratio = -1;
340
341         outfn_path_entry.set_text (_session->session_directory().export_path() + G_DIR_SEPARATOR +"export.avi");
342
343         // TODO remember setting for export-range.. somehow, (let explicit range override)
344         frameoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
345         if (av_offset < 0 ) {
346                 insnd_combo.append_text (_("from 00:00:00:00 to the video's end"));
347         } else {
348                 insnd_combo.append_text (_("from the video's start to the video's end"));
349         }
350         if (!export_range.empty()) {
351                 insnd_combo.append_text (_("Selected range"));  // TODO show export_range.start() -> export_range.end_frame()
352         }
353         if (range) {
354                 insnd_combo.set_active(2);
355         } else {
356                 insnd_combo.set_active(0);
357         }
358
359         preset_combo.set_active(0);
360         audio_codec_combo.set_active(0);
361         video_codec_combo.set_active(0);
362         audio_bitrate_combo.set_active(0);
363         audio_samplerate_combo.set_active(2);
364         video_bitrate_combo.set_active(0);
365         aspect_combo.set_active(1);
366
367         scale_checkbox.set_active(false);
368         scale_aspect.set_active(true);
369         aspect_checkbox.set_active(false);
370         normalize_checkbox.set_active(false);
371         twopass_checkbox.set_active(false);
372         optimizations_checkbox.set_active(false);
373         deinterlace_checkbox.set_active(false);
374         bframes_checkbox.set_active(false);
375         fps_checkbox.set_active(false);
376         meta_checkbox.set_active(false);
377
378         float tcfps = _session->timecode_frames_per_second();
379
380         LocaleGuard lg (X_("C"));
381
382         XMLNode* node = _session->extra_xml (X_("Videotimeline"));
383         if (node) {
384                 bool filenameset = false;
385                 if (node->property(X_("OriginalVideoFile"))) {
386                         std::string filename = node->property(X_("OriginalVideoFile"))->value();
387                         if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS)) {
388                                 invid_path_entry.set_text (filename);
389                                 filenameset = true;
390                         }
391                 }
392                 if (!filenameset
393                                 && node->property(X_("Filename"))
394                                 && node->property(X_("LocalFile"))
395                                 && node->property(X_("LocalFile"))->value() == X_("1")
396                    )
397                 {
398                         std::string filename = node->property(X_("Filename"))->value();
399                         if (filename.at(0) != G_DIR_SEPARATOR)
400                         {
401                                 filename = Glib::build_filename (_session->session_directory().video_path(), filename);
402                         }
403                         if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS))
404                         {
405                                 invid_path_entry.set_text (filename);
406                                 filenameset = true;
407                         }
408                 }
409                 if (!filenameset) {
410                         invid_path_entry.set_text (X_(""));
411                 }
412         }
413
414         node = _session->extra_xml (X_("Videoexport"));
415         if (node) {
416                 const XMLProperty* prop;
417                 prop = node->property (X_("ChangeGeometry"));
418                 if (prop) { scale_checkbox.set_active(atoi(prop->value())?true:false); }
419                 prop = node->property (X_("KeepAspect"));
420                 if (prop) { scale_aspect.set_active(atoi(prop->value())?true:false); }
421                 prop = node->property (X_("ChangeAspect"));
422                 if (prop) { aspect_checkbox.set_active(atoi(prop->value())?true:false); }
423                 prop = node->property (X_("NormalizeAudio"));
424                 if (prop) { normalize_checkbox.set_active(atoi(prop->value())?true:false); }
425                 prop = node->property (X_("TwoPassEncode"));
426                 if (prop) { twopass_checkbox.set_active(atoi(prop->value())?true:false); }
427                 prop = node->property (X_("CodecOptimzations"));
428                 if (prop) { optimizations_checkbox.set_active(atoi(prop->value())?true:false); }
429                 prop = node->property (X_("Deinterlace"));
430                 if (prop) { deinterlace_checkbox.set_active(atoi(prop->value())?true:false); }
431                 prop = node->property (X_("BFrames"));
432                 if (prop) { bframes_checkbox.set_active(atoi(prop->value())?true:false); }
433                 prop = node->property (X_("ChangeFPS"));
434                 if (prop) { fps_checkbox.set_active(atoi(prop->value())?true:false); }
435                 prop = node->property (X_("Metadata"));
436                 if (prop) { meta_checkbox.set_active(atoi(prop->value())?true:false); }
437
438                 prop = node->property (X_("Format"));
439                 if (prop && !prop->value().empty()) { change_file_extension( "." + prop->value()); }
440
441                 _suspend_signals = true;
442                 prop = node->property (X_("Width"));
443                 if (prop) { width_spinner.set_value(atoi(prop->value())); }
444                 prop = node->property (X_("Height"));
445                 if (prop) { height_spinner.set_value(atoi(prop->value())); }
446                 _suspend_signals = false;
447
448                 prop = node->property (X_("FPS"));
449                 if (prop && fps_checkbox.get_active()) { tcfps = atof(prop->value()); }
450
451                 prop = node->property (X_("Preset"));
452                 if (prop) { preset_combo.set_active_text(prop->value()); }
453                 prop = node->property (X_("VCodec"));
454                 if (prop) { video_codec_combo.set_active_text(prop->value()); }
455                 prop = node->property (X_("ACodec"));
456                 if (prop) { audio_codec_combo.set_active_text(prop->value()); }
457                 prop = node->property (X_("VBitrate"));
458                 if (prop) { video_bitrate_combo.set_active_text(prop->value()); }
459                 prop = node->property (X_("ABitrate"));
460                 if (prop) { audio_bitrate_combo.set_active_text(prop->value()); }
461                 prop = node->property (X_("AspectRatio"));
462                 if (prop) { aspect_combo.set_active_text(prop->value()); }
463                 prop = node->property (X_("SampleRate"));
464                 if (prop) { audio_samplerate_combo.set_active_text(prop->value()); }
465         }
466
467         if      (fabs(tcfps - 23.976) < 0.01) { fps_combo.set_active(0); }
468         else if (fabs(tcfps - 24.0  ) < 0.01) { fps_combo.set_active(1); }
469         else if (fabs(tcfps - 24.976) < 0.01) { fps_combo.set_active(2); }
470         else if (fabs(tcfps - 25.0  ) < 0.01) { fps_combo.set_active(3); }
471         else if (fabs(tcfps - 29.97 ) < 0.01) { fps_combo.set_active(4); }
472         else if (fabs(tcfps - 30.0  ) < 0.01) { fps_combo.set_active(5); }
473         else if (fabs(tcfps - 59.94 ) < 0.01) { fps_combo.set_active(6); }
474         else if (fabs(tcfps - 60.0  ) < 0.01) { fps_combo.set_active(7); }
475         else { fps_combo.set_active(5); }
476
477         set_original_file_information();
478
479         /* update sensitivity */
480         scale_checkbox_toggled();
481         aspect_checkbox_toggled();
482         fps_checkbox_toggled();
483         video_codec_combo_changed();
484
485         _suspend_dirty = false;
486
487         show_all_children ();
488         progress_box->hide();
489 }
490
491 XMLNode&
492 ExportVideoDialog::get_state ()
493 {
494         LocaleGuard lg (X_("C"));
495         XMLNode* node = new XMLNode (X_("Videoexport"));
496         node->add_property (X_("ChangeGeometry"), scale_checkbox.get_active() ? X_("1") : X_("0"));
497         node->add_property (X_("KeepAspect"), scale_aspect.get_active() ? X_("1") : X_("0"));
498         node->add_property (X_("ChangeAspect"), aspect_checkbox.get_active() ? X_("1") : X_("0"));
499         node->add_property (X_("NormalizeAudio"), normalize_checkbox.get_active() ? X_("1") : X_("0"));
500         node->add_property (X_("TwoPassEncode"), twopass_checkbox.get_active() ? X_("1") : X_("0"));
501         node->add_property (X_("CodecOptimzations"), optimizations_checkbox.get_active() ? X_("1") : X_("0"));
502         node->add_property (X_("Deinterlace"), deinterlace_checkbox.get_active() ? X_("1") : X_("0"));
503         node->add_property (X_("BFrames"), bframes_checkbox.get_active() ? X_("1") : X_("0"));
504         node->add_property (X_("ChangeFPS"), fps_checkbox.get_active() ? X_("1") : X_("0"));
505         node->add_property (X_("Metadata"), meta_checkbox.get_active() ? X_("1") : X_("0"));
506
507         node->add_property (X_("Format"), get_file_extension(outfn_path_entry.get_text()));
508
509         node->add_property (X_("Width"), width_spinner.get_value());
510         node->add_property (X_("Height"), height_spinner.get_value());
511
512         node->add_property (X_("Preset"), preset_combo.get_active_text());
513         node->add_property (X_("VCodec"), video_codec_combo.get_active_text());
514         node->add_property (X_("ACodec"), audio_codec_combo.get_active_text());
515         node->add_property (X_("VBitrate"), video_bitrate_combo.get_active_text());
516         node->add_property (X_("ABitrate"), audio_bitrate_combo.get_active_text());
517         node->add_property (X_("AspectRatio"), aspect_combo.get_active_text());
518         node->add_property (X_("SampleRate"), audio_samplerate_combo.get_active_text());
519         node->add_property (X_("FPS"), fps_combo.get_active_text());
520
521         return *node;
522 }
523
524 void
525 ExportVideoDialog::set_state (const XMLNode &)
526 {
527 }
528
529 void
530 ExportVideoDialog::on_show ()
531 {
532         Dialog::on_show ();
533 }
534
535 bool
536 ExportVideoDialog::on_focus_in_event (GdkEventFocus *ev)
537 {
538         Dialog::on_focus_in_event (ev);
539         Gtkmm2ext::Keyboard::magic_widget_grab_focus ();
540         return true;
541 }
542
543 bool
544 ExportVideoDialog::on_focus_out_event (GdkEventFocus *ev)
545 {
546         Dialog::on_focus_out_event (ev);
547         Gtkmm2ext::Keyboard::magic_widget_drop_focus ();
548         return true;
549 }
550
551 void
552 ExportVideoDialog::abort_clicked ()
553 {
554         _aborted = true;
555         if (_transcoder) {
556                 _transcoder->cancel();
557         }
558 }
559
560 void
561 ExportVideoDialog::update_progress (framecnt_t c, framecnt_t a)
562 {
563         if (a == 0 || c > a) {
564                 pbar.set_pulse_step(.1);
565                 pbar.pulse();
566         } else {
567                 double progress = (double)c / (double) a;
568                 progress = progress / ((_twopass ? 2.0 : 1.0) + (_normalize ? 2.0 : 1.0));
569                 if (_normalize && _twopass) progress += (_firstpass ? .5 : .75);
570                 else if (_normalize) progress += 2.0/3.0;
571                 else if (_twopass) progress += (_firstpass ? 1.0/3.0 : 2.0/3.0);
572                 else progress += .5;
573
574                 pbar.set_fraction (progress);
575         }
576 }
577
578
579 gint
580 ExportVideoDialog::audio_progress_display ()
581 {
582         std::string status_text;
583         double progress = 0.0;
584                 if (status->normalizing) {
585                         pbar.set_text (_("Normalizing audio"));
586                         progress = ((float) status->current_normalize_cycle) / status->total_normalize_cycles;
587                         progress = progress / (_twopass ? 4.0 : 3.0) + (_twopass ? .25 : 1.0/3.0);
588                 } else {
589                         pbar.set_text (_("Exporting audio"));
590                         progress = ((float) status->processed_frames_current_timespan) / status->total_frames_current_timespan;
591                         progress = progress / ((_twopass ? 2.0 : 1.0) + (_normalize ? 2.0 : 1.0));
592                 }
593                 if (progress < _previous_progress) {
594                         // Work around gtk bug
595                         pbar.hide();
596                         pbar.show();
597                 }
598                 _previous_progress = progress;
599                 pbar.set_fraction (progress);
600         return TRUE;
601 }
602
603 void
604 ExportVideoDialog::finished ()
605 {
606         if (_aborted) {
607                 ::g_unlink(outfn_path_entry.get_text().c_str());
608                 ::g_unlink (_insnd.c_str());
609                 delete _transcoder; _transcoder = 0;
610                 Gtk::Dialog::response(RESPONSE_CANCEL);
611         } else if (_twopass && _firstpass) {
612                 _firstpass = false;
613                 if (_transcoder) { delete _transcoder; _transcoder = 0;}
614                 encode_pass(2);
615         } else {
616                 if (twopass_checkbox.get_active()) {
617                         std::string outfn = outfn_path_entry.get_text();
618                         std::string p2log = Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
619                         ::g_unlink (p2log.c_str());
620                 }
621                 ::g_unlink (_insnd.c_str());
622                 delete _transcoder; _transcoder = 0;
623                 Gtk::Dialog::response(RESPONSE_ACCEPT);
624         }
625 }
626
627 void
628 ExportVideoDialog::launch_export ()
629 {
630         /* remember current settings.
631          * needed because apply_state() acts on both:
632          * "Videotimeline" and "Video Export" extra XML
633          * as well as current _session settings
634          */
635         _session->add_extra_xml (get_state());
636
637         std::string outfn = outfn_path_entry.get_text();
638         if (!confirm_video_outfn(outfn)) { return; }
639
640         vbox->hide();
641         cancel_button->hide();
642         transcode_button.hide();
643         pbar.set_size_request(300,-1);
644         pbar.set_text(_("Exporting Audio..."));
645         progress_box->show();
646         _aborted = false;
647         _twopass = twopass_checkbox.get_active();
648         _firstpass = true;
649         _normalize = normalize_checkbox.get_active();
650
651         /* export audio track */
652         ExportTimespanPtr tsp = _session->get_export_handler()->add_timespan();
653         boost::shared_ptr<ExportChannelConfiguration> ccp = _session->get_export_handler()->add_channel_config();
654         boost::shared_ptr<ARDOUR::ExportFilename> fnp = _session->get_export_handler()->add_filename();
655         boost::shared_ptr<AudioGrapher::BroadcastInfo> b;
656         XMLTree tree;
657         std::string vtl_samplerate = audio_samplerate_combo.get_active_text();
658         std::string vtl_normalize = _normalize ? "true" : "false";
659         tree.read_buffer(std::string(
660 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
661 "<ExportFormatSpecification name=\"VTL-WAV-16\" id=\"3094591e-ccb9-4385-a93f-c9955ffeb1f0\">"
662 "  <Encoding id=\"F_WAV\" type=\"T_Sndfile\" extension=\"wav\" name=\"WAV\" has-sample-format=\"true\" channel-limit=\"256\"/>"
663 "  <SampleRate rate=\""+ vtl_samplerate +"\"/>"
664 "  <SRCQuality quality=\"SRC_SincBest\"/>"
665 "  <EncodingOptions>"
666 "    <Option name=\"sample-format\" value=\"SF_16\"/>"
667 "    <Option name=\"dithering\" value=\"D_None\"/>"
668 "    <Option name=\"tag-metadata\" value=\"true\"/>"
669 "    <Option name=\"tag-support\" value=\"false\"/>"
670 "    <Option name=\"broadcast-info\" value=\"false\"/>"
671 "  </EncodingOptions>"
672 "  <Processing>"
673 "    <Normalize enabled=\""+ vtl_normalize +"\" target=\"0\"/>"
674 "    <Silence>"
675 "      <Start>"
676 "        <Trim enabled=\"false\"/>"
677 "        <Add enabled=\"false\">"
678 "          <Duration format=\"Timecode\" hours=\"0\" minutes=\"0\" seconds=\"0\" frames=\"0\"/>"
679 "        </Add>"
680 "      </Start>"
681 "      <End>"
682 "        <Trim enabled=\"false\"/>"
683 "        <Add enabled=\"false\">"
684 "          <Duration format=\"Timecode\" hours=\"0\" minutes=\"0\" seconds=\"0\" frames=\"0\"/>"
685 "        </Add>"
686 "      </End>"
687 "    </Silence>"
688 "  </Processing>"
689 "</ExportFormatSpecification>"
690 ));
691         boost::shared_ptr<ExportFormatSpecification> fmp = _session->get_export_handler()->add_format(*tree.root());
692
693         /* set up range */
694         framepos_t start, end;
695         start = end = 0;
696         if (insnd_combo.get_active_row_number() == 1) {
697                 _transcoder = new TranscodeFfmpeg(invid_path_entry.get_text());
698                 if (_transcoder->probe_ok() && _transcoder->get_fps() > 0) {
699                         end = _transcoder->get_duration() * _session->nominal_frame_rate() / _transcoder->get_fps();
700                 } else {
701                         warning << _("Export Video: Cannot query duration of video-file, using duration from timeline instead.") << endmsg;
702                         end = ARDOUR_UI::instance()->video_timeline->get_duration();
703                 }
704                 if (_transcoder) {delete _transcoder; _transcoder = 0;}
705
706                 frameoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
707 #if 0 /* DEBUG */
708                 printf("audio-range -- AV offset: %lld\n", av_offset);
709 #endif
710                 if (av_offset > 0) {
711                         start = av_offset;
712                 }
713                 end += av_offset;
714         }
715         else if (insnd_combo.get_active_row_number() == 2) {
716                 start = ARDOUR_UI::instance()->video_timeline->quantify_frames_to_apv(export_range.start());
717                 end   = ARDOUR_UI::instance()->video_timeline->quantify_frames_to_apv(export_range.end_frame());
718         }
719         if (end <= 0) {
720                 start = _session->current_start_frame();
721                 end   = _session->current_end_frame();
722         }
723 #if 0 /* DEBUG */
724         printf("audio export-range %lld -> %lld\n", start, end);
725 #endif
726
727         const frameoffset_t vstart = ARDOUR_UI::instance()->video_timeline->get_offset();
728         const frameoffset_t vend   = vstart + ARDOUR_UI::instance()->video_timeline->get_duration();
729
730         if ( (start >= end) || (end < vstart) || (start > vend)) {
731                 warning << _("Export Video: export-range does not include video.") << endmsg;
732                 delete _transcoder; _transcoder = 0;
733                 Gtk::Dialog::response(RESPONSE_CANCEL);
734                 return;
735         }
736
737         tsp->set_range (start, end);
738         tsp->set_name ("mysession");
739         tsp->set_range_id ("session");
740
741         /* add master outs as default */
742         IO* master_out = _session->master_out()->output().get();
743         if (!master_out) {
744                 warning << _("Export Video: No Master Out Ports to Connect for Audio Export") << endmsg;
745                 delete _transcoder; _transcoder = 0;
746                 Gtk::Dialog::response(RESPONSE_CANCEL);
747                 return;
748         }
749         for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
750                 PortExportChannel * channel = new PortExportChannel ();
751                 channel->add_port (master_out->audio (n));
752                 ExportChannelPtr chan_ptr (channel);
753                 ccp->register_channel (chan_ptr);
754         }
755
756         /* outfile */
757         fnp->set_timespan(tsp);
758         fnp->set_label("vtl");
759         fnp->include_label = true;
760         _insnd = fnp->get_path(fmp);
761
762         /* do sound export */
763         fmp->set_soundcloud_upload(false);
764         _session->get_export_handler()->add_export_config (tsp, ccp, fmp, fnp, b);
765         _session->get_export_handler()->do_export();
766         status = _session->get_export_status ();
767
768         audio_progress_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ExportVideoDialog::audio_progress_display), 100);
769         _previous_progress = 0.0;
770         while (status->running) {
771                 if (_aborted) { status->abort(); }
772                 if (gtk_events_pending()) {
773                         gtk_main_iteration ();
774                 } else {
775                         Glib::usleep (10000);
776                 }
777         }
778         audio_progress_connection.disconnect();
779         status->finish ();
780         if (status->aborted()) {
781                 ::g_unlink (_insnd.c_str());
782                 delete _transcoder; _transcoder = 0;
783                 Gtk::Dialog::response(RESPONSE_CANCEL);
784                 return;
785         }
786         pbar.set_text (_("Encoding Video..."));
787         encode_pass(1);
788 }
789
790 void
791 ExportVideoDialog::encode_pass (int pass)
792 {
793         std::string outfn = outfn_path_entry.get_text();
794         std::string invid = invid_path_entry.get_text();
795
796         _transcoder = new TranscodeFfmpeg(invid);
797         if (!_transcoder->ffexec_ok()) {
798                 /* ffmpeg binary was not found. TranscodeFfmpeg prints a warning */
799                 ::g_unlink (_insnd.c_str());
800                 delete _transcoder; _transcoder = 0;
801                 Gtk::Dialog::response(RESPONSE_CANCEL);
802                 return;
803         }
804         if (!_transcoder->probe_ok()) {
805                 /* video input file can not be read */
806                 warning << _("Export Video: Video input file cannot be read.") << endmsg;
807                 ::g_unlink (_insnd.c_str());
808                 delete _transcoder; _transcoder = 0;
809                 Gtk::Dialog::response(RESPONSE_CANCEL);
810                 return;
811         }
812
813         std::string preset = preset_combo.get_active_text();
814         TranscodeFfmpeg::FFSettings ffs ; /* = transcoder->default_encoder_settings(); */
815         ffs.clear();
816
817         if (fps_checkbox.get_active()) {
818                 ffs["-r"] = fps_combo.get_active_text();
819                 _transcoder->set_fps(atof(fps_combo.get_active_text()));
820         }
821
822         if (scale_checkbox.get_active()) {
823                 ffs["-s"] = string_compose("%1x%2", width_spinner.get_value(), height_spinner.get_value());
824         }
825
826         if (video_codec_combo.get_active_text() != _("(default for format)")) {
827                 ffs["-vcodec"] = video_codec_combo.get_active_text();
828         }
829         if (audio_codec_combo.get_active_text() != _("(default for format)")) {
830                 ffs["-acodec"] = audio_codec_combo.get_active_text();
831         }
832
833         if (video_bitrate_combo.get_active_text() == _("(default)") ) {
834                 ;
835         }
836         else if (video_bitrate_combo.get_active_text() == _("(retain)") ) {
837                 ffs["-qscale"]  = "0";
838         } else {
839                 ffs["-b:v"]  = video_bitrate_combo.get_active_text();
840         }
841
842         if (audio_bitrate_combo.get_active_text() != _("(default)") ) {
843                 ffs["-b:a"] = audio_bitrate_combo.get_active_text();
844         }
845
846         if (audio_codec_combo.get_active_text() == "aac" ) {
847                 ffs["-strict"] = "-2";
848         }
849
850         if (video_codec_combo.get_active_text() == "h264" ) {
851                 ffs["-vcodec"] = "libx264";
852         }
853         else if (video_codec_combo.get_active_text() == "vpx (webm)" ) {
854                 ffs["-vcodec"] = "libvpx";
855                 ffs["-g"] = "120";
856                 ffs["-qmin"] = "11";
857                 ffs["-qmax"] = "51";
858         }
859
860         if (optimizations_checkbox.get_active()) {
861           if (video_codec_combo.get_active_text() == "mpeg2video") {
862                         ffs["-mbd"] = "rd";
863                         ffs["-trellis"] = "2";
864                         ffs["-cmp"] = "2";
865                         ffs["-subcmp"] = "2";
866                 }
867                 else if (video_codec_combo.get_active_text() == "mpeg4") {
868                         ffs["-mbd"] = "rd";
869                         ffs["-flags"] = "+mv4+aic";
870                         ffs["-trellis"] = "2";
871                         ffs["-cmp"] = "2";
872                         ffs["-subcmp"] = "2";
873                         ffs["-g"] = "300";
874                 }
875                 else if (video_codec_combo.get_active_text() == "flv") {
876                         ffs["-mbd"] = "2";
877                         ffs["-cmp"] = "2";
878                         ffs["-subcmp"] = "2";
879                         ffs["-trellis"] = "2";
880                         ffs["-flags"] = "+aic+mv0+mv4";
881                         ffs["-g"] = "160";
882                 }
883         }
884
885         if (bframes_checkbox.get_active() && (
886                    video_codec_combo.get_active_text() == "mpeg2video"
887                 || video_codec_combo.get_active_text() == "mpeg4"
888                 )) {
889                 ffs["-bf"] = "2";
890         }
891
892         if (preset == "dvd-PAL") {
893                 ffs.clear(); /* ignore all prev settings */
894                 ffs["-target"] = "pal-dvd";
895                 ffs["-aspect"] = "4:3"; /* required for DVD - may be overridden below */
896         }
897         else if (preset == "dvd-NTSC") {
898                 ffs.clear(); /* ignore all prev settings */
899                 ffs["-target"] = "ntsc-dvd";
900                 ffs["-aspect"] = "4:3"; /* required for DVD - may be overridden below */
901         }
902
903         if (aspect_checkbox.get_active()) {
904                 ffs["-aspect"] = aspect_combo.get_active_text();
905         }
906         if (deinterlace_checkbox.get_active()) {
907                 ffs["-deinterlace"] = "-y"; // we use '-y' as dummy parameter for non key/value options
908         }
909
910         bool map = true;
911         if (pass == 1 && _twopass) {
912                 pbar.set_text (_("Encoding Video.. Pass 1/2"));
913                 map = false;
914                 ffs["-pass"] = "1";
915                 ffs["-an"] = "-y";
916                 ffs["-passlogfile"] =  Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
917                 ffs["-f"] = get_file_extension(invid).empty()?"mov":get_file_extension(invid);
918 #ifdef PLATFORM_WINDOWS
919                 outfn = "NUL";
920 #else
921                 outfn = "/dev/null";
922 #endif
923         } else if (pass == 2) {
924                 pbar.set_text (_("Encoding Video.. Pass 2/2"));
925                 ffs["-pass"] = "2";
926                 ffs["-passlogfile"] =  Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
927         }
928
929         frameoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
930         double duration_s  = 0;
931
932         if (insnd_combo.get_active_row_number() == 0) {
933                 /* session start to session end */
934                 framecnt_t duration_f = _session->current_end_frame() - _session->current_start_frame();
935                 duration_s = (double)duration_f / (double)_session->nominal_frame_rate();
936         } else if (insnd_combo.get_active_row_number() == 2) {
937                 /* selected range */
938                 duration_s = export_range.length() / (double)_session->nominal_frame_rate();
939         } else {
940                 /* video start to end */
941                 framecnt_t duration_f = ARDOUR_UI::instance()->video_timeline->get_duration();
942                 if (av_offset < 0 ) {
943                         duration_f += av_offset;
944                 }
945                 duration_s = (double)duration_f / (double)_session->nominal_frame_rate();
946         }
947
948         std::ostringstream osstream; osstream << duration_s;
949         ffs["-t"] = osstream.str();
950         _transcoder->set_duration(duration_s * _transcoder->get_fps());
951
952         if (insnd_combo.get_active_row_number() == 0 || insnd_combo.get_active_row_number() == 2) {
953                 framepos_t start, snend;
954                 const frameoffset_t vid_duration = ARDOUR_UI::instance()->video_timeline->get_duration();
955                 if (insnd_combo.get_active_row_number() == 0) {
956                         start = _session->current_start_frame();
957                         snend = _session->current_end_frame();
958                 } else {
959                         start = export_range.start();
960                         snend = export_range.end_frame();
961                 }
962
963 #if 0 /* DEBUG */
964                 printf("AV offset: %lld Vid-len: %lld Vid-end: %lld || start:%lld || end:%lld\n",
965                                 av_offset, vid_duration, av_offset+vid_duration, start, snend); // XXX
966 #endif
967
968                 if (av_offset > start && av_offset + vid_duration < snend) {
969                         _transcoder->set_leadinout((av_offset - start) / (double)_session->nominal_frame_rate(),
970                                 (snend - (av_offset + vid_duration)) / (double)_session->nominal_frame_rate());
971                 } else if (av_offset > start) {
972                         _transcoder->set_leadinout((av_offset - start) / (double)_session->nominal_frame_rate(), 0);
973                 } else if (av_offset + vid_duration < snend) {
974                         _transcoder->set_leadinout(0, (snend - (av_offset + vid_duration)) / (double)_session->nominal_frame_rate());
975                         _transcoder->set_avoffset((av_offset - start) / (double)_session->nominal_frame_rate());
976                 }
977 #if 0
978                 else if (start > av_offset) {
979                         std::ostringstream osstream; osstream << ((start - av_offset) / (double)_session->nominal_frame_rate());
980                         ffs["-ss"] = osstream.str();
981                 }
982 #endif
983                 else {
984                         _transcoder->set_avoffset((av_offset - start) / (double)_session->nominal_frame_rate());
985                 }
986
987         } else if (av_offset < 0) {
988                 /* from 00:00:00:00 to video-end */
989                 _transcoder->set_avoffset(av_offset / (double)_session->nominal_frame_rate());
990         }
991
992         TranscodeFfmpeg::FFSettings meta = _transcoder->default_meta_data();
993         if (meta_checkbox.get_active()) {
994                 ARDOUR::SessionMetadata * session_data = ARDOUR::SessionMetadata::Metadata();
995                 if (session_data->year() > 0 ) {
996                         std::ostringstream osstream; osstream << session_data->year();
997                         meta["year"] = osstream.str();
998                 }
999                 if (session_data->track_number() > 0 ) {
1000                         std::ostringstream osstream; osstream << session_data->track_number();
1001                         meta["track"] = osstream.str();
1002                 }
1003                 if (session_data->disc_number() > 0 ) {
1004                         std::ostringstream osstream; osstream << session_data->disc_number();
1005                         meta["disc"] = osstream.str();
1006                 }
1007                 if (!session_data->title().empty())     {meta["title"] = session_data->title();}
1008                 if (!session_data->artist().empty())    {meta["author"] = session_data->artist();}
1009                 if (!session_data->album_artist().empty()) {meta["album_artist"] = session_data->album_artist();}
1010                 if (!session_data->album().empty())     {meta["album"] = session_data->album();}
1011                 if (!session_data->genre().empty())     {meta["genre"] = session_data->genre();}
1012                 if (!session_data->composer().empty())  {meta["composer"] = session_data->composer();}
1013                 if (!session_data->comment().empty())   {meta["comment"] = session_data->comment();}
1014                 if (!session_data->copyright().empty()) {meta["copyright"] = session_data->copyright();}
1015                 if (!session_data->subtitle().empty())  {meta["description"] = session_data->subtitle();}
1016         }
1017
1018 #if 1 /* tentative debug mode */
1019         if (debug_checkbox.get_active()) {
1020                 _transcoder->set_debug(true);
1021         }
1022 #endif
1023
1024         _transcoder->Progress.connect(*this, invalidator (*this), boost::bind (&ExportVideoDialog::update_progress , this, _1, _2), gui_context());
1025         _transcoder->Finished.connect(*this, invalidator (*this), boost::bind (&ExportVideoDialog::finished, this), gui_context());
1026         if (!_transcoder->encode(outfn, _insnd, invid, ffs, meta, map)) {
1027                 ARDOUR_UI::instance()->popup_error(_("Transcoding failed."));
1028                 delete _transcoder; _transcoder = 0;
1029                 Gtk::Dialog::response(RESPONSE_CANCEL);
1030                 return;
1031         }
1032 }
1033
1034 void
1035 ExportVideoDialog::change_file_extension (std::string ext)
1036 {
1037         if (ext == "") return;
1038         outfn_path_entry.set_text (
1039                 strip_file_extension(outfn_path_entry.get_text()) + ext
1040         );
1041 }
1042
1043 void
1044 ExportVideoDialog::width_value_changed ()
1045 {
1046         if (_suspend_signals) {
1047                 return;
1048         }
1049         if (_session && !_suspend_dirty) _session->set_dirty ();
1050         if (!scale_checkbox.get_active() || !scale_aspect.get_active()) {
1051                 return;
1052         }
1053         if (_video_source_aspect_ratio <= 0) {
1054                 return;
1055         }
1056         _suspend_signals = true;
1057         height_spinner.set_value(rintf(width_spinner.get_value() / _video_source_aspect_ratio));
1058         _suspend_signals = false;
1059 }
1060
1061 void
1062 ExportVideoDialog::height_value_changed ()
1063 {
1064         if (_suspend_signals) {
1065                 return;
1066         }
1067         if (_session && !_suspend_dirty) _session->set_dirty ();
1068         if (!scale_checkbox.get_active() || !scale_aspect.get_active()) {
1069                 return;
1070         }
1071         if (_video_source_aspect_ratio <= 0) {
1072                 return;
1073         }
1074         _suspend_signals = true;
1075         width_spinner.set_value(rintf(height_spinner.get_value() * _video_source_aspect_ratio));
1076         _suspend_signals = false;
1077 }
1078
1079 void
1080 ExportVideoDialog::scale_checkbox_toggled ()
1081 {
1082         scale_aspect.set_sensitive(scale_checkbox.get_active());
1083         width_spinner.set_sensitive(scale_checkbox.get_active());
1084         height_spinner.set_sensitive(scale_checkbox.get_active());
1085         if (_session && !_suspend_dirty) _session->set_dirty ();
1086 }
1087
1088 void
1089 ExportVideoDialog::fps_checkbox_toggled ()
1090 {
1091         fps_combo.set_sensitive(fps_checkbox.get_active());
1092         if (_session && !_suspend_dirty) _session->set_dirty ();
1093 }
1094
1095 void
1096 ExportVideoDialog::aspect_checkbox_toggled ()
1097 {
1098         aspect_combo.set_sensitive(aspect_checkbox.get_active());
1099         if (_session && !_suspend_dirty) _session->set_dirty ();
1100 }
1101
1102 void
1103 ExportVideoDialog::video_codec_combo_changed ()
1104 {
1105         if ((  video_codec_combo.get_active_text() == "mpeg4"
1106              ||video_codec_combo.get_active_text() == "mpeg2video"
1107                         ) && !(
1108                preset_combo.get_active_text() == "dvd-PAL"
1109              ||preset_combo.get_active_text() == "dvd-NTSC"
1110            )) {
1111                 bframes_checkbox.set_sensitive(true);
1112                 optimizations_checkbox.set_sensitive(true);
1113                 if (video_codec_combo.get_active_text() == "mpeg2video") {
1114                         optimizations_label.set_text("-mbd rd -trellis 2 -cmp 2 -subcmp 2"); // mpeg2
1115                 } else if (video_codec_combo.get_active_text() == "mpeg4") {
1116                         optimizations_label.set_text("-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300"); // mpeg4
1117                 } else {
1118                         optimizations_label.set_text("-mbd 2 -cmp 2 -subcmp 2 -trellis 2 -flags +aic+mv0+mv4 -g 160"); // flv
1119                 }
1120         } else {
1121                 bframes_checkbox.set_sensitive(false);
1122                 bframes_checkbox.set_active(false);
1123                 optimizations_checkbox.set_sensitive(false);
1124                 optimizations_checkbox.set_active(false);
1125                 optimizations_label.set_text("-");
1126         }
1127         if (_session && !_suspend_dirty) _session->set_dirty ();
1128 }
1129
1130 void
1131 ExportVideoDialog::preset_combo_changed ()
1132 {
1133         std::string p = preset_combo.get_active_text();
1134         scale_checkbox.set_sensitive(true);
1135
1136         if (p == "flv") {
1137                 change_file_extension(".flv");
1138                 audio_codec_combo.set_active(2);
1139                 video_codec_combo.set_active(1);
1140                 audio_bitrate_combo.set_active(2);
1141                 video_bitrate_combo.set_active(3);
1142                 audio_samplerate_combo.set_active(1);
1143         }
1144         else if (p == "you-tube") {
1145                 change_file_extension(".avi");
1146                 audio_codec_combo.set_active(3);
1147                 video_codec_combo.set_active(6);
1148                 audio_bitrate_combo.set_active(2);
1149                 video_bitrate_combo.set_active(4);
1150                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1151                         audio_samplerate_combo.set_active(2);
1152                 } else {
1153                         audio_samplerate_combo.set_active(1);
1154                 }
1155         }
1156         else if (p == "ogg") {
1157                 change_file_extension(".ogv");
1158                 audio_codec_combo.set_active(4);
1159                 video_codec_combo.set_active(2);
1160                 audio_bitrate_combo.set_active(3);
1161                 video_bitrate_combo.set_active(4);
1162                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1163                         audio_samplerate_combo.set_active(2);
1164                 } else {
1165                         audio_samplerate_combo.set_active(1);
1166                 }
1167         }
1168         else if (p == "webm") {
1169                 change_file_extension(".webm");
1170                 audio_codec_combo.set_active(4);
1171                 video_codec_combo.set_active(7);
1172                 audio_bitrate_combo.set_active(3);
1173                 video_bitrate_combo.set_active(4);
1174                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1175                         audio_samplerate_combo.set_active(2);
1176                 } else {
1177                         audio_samplerate_combo.set_active(1);
1178                 }
1179         }
1180         else if (p == "dvd-mp2") {
1181                 change_file_extension(".mpg");
1182                 audio_codec_combo.set_active(5);
1183                 video_codec_combo.set_active(4);
1184                 audio_bitrate_combo.set_active(4);
1185                 video_bitrate_combo.set_active(5);
1186                 audio_samplerate_combo.set_active(2);
1187         }
1188         else if (p == "dvd-NTSC" || p == "dvd-PAL") {
1189                 change_file_extension(".mpg");
1190                 audio_codec_combo.set_active(6);
1191                 video_codec_combo.set_active(4);
1192                 audio_bitrate_combo.set_active(4);
1193                 video_bitrate_combo.set_active(5);
1194                 audio_samplerate_combo.set_active(2);
1195
1196                 scale_checkbox.set_active(false);
1197                 scale_checkbox.set_sensitive(false);
1198         }
1199         else if (p == "mpeg4") {
1200                 change_file_extension(".mp4");
1201                 audio_codec_combo.set_active(1);
1202                 video_codec_combo.set_active(5);
1203                 audio_bitrate_combo.set_active(4);
1204                 video_bitrate_combo.set_active(5);
1205                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1206                         audio_samplerate_combo.set_active(2);
1207                 } else {
1208                         audio_samplerate_combo.set_active(1);
1209                 }
1210         }
1211         else if (p == "mp4/h264/aac") {
1212                 change_file_extension(".mp4");
1213                 audio_codec_combo.set_active(2);
1214                 video_codec_combo.set_active(6);
1215                 audio_bitrate_combo.set_active(0);
1216                 video_bitrate_combo.set_active(0);
1217                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1218                         audio_samplerate_combo.set_active(2);
1219                 } else {
1220                         audio_samplerate_combo.set_active(1);
1221                 }
1222         }
1223
1224         if (p == "none") {
1225                 audio_codec_combo.set_sensitive(true);
1226                 video_codec_combo.set_sensitive(true);
1227                 audio_bitrate_combo.set_sensitive(true);
1228                 video_bitrate_combo.set_sensitive(true);
1229                 audio_samplerate_combo.set_sensitive(true);
1230         } else {
1231                 audio_codec_combo.set_sensitive(false);
1232                 video_codec_combo.set_sensitive(false);
1233                 audio_bitrate_combo.set_sensitive(false);
1234                 video_bitrate_combo.set_sensitive(false);
1235                 audio_samplerate_combo.set_sensitive(false);
1236         }
1237
1238         Gtk::Table *t = (Gtk::Table*) preset_combo.get_parent();
1239         Gtk::Table_Helpers::TableList c = t->children();
1240         Gtk::Table_Helpers::TableList::iterator it;
1241         if (p == "dvd-PAL" || p == "dvd-NTSC") {
1242                 for (it = c.begin(); it != c.end(); ++it) {
1243                         int row = it->get_top_attach();
1244                         if (row == 2 || row == 3 || row== 5 || row== 6 || row == 9) {
1245                                 it->get_widget()->hide();
1246                         }
1247                 }
1248         } else {
1249                 for (it = c.begin(); it != c.end(); ++it) {
1250                         int row = it->get_top_attach();
1251                         if (row == 2 || row == 3 || row== 5 || row== 6 || row == 9) {
1252                                 it->get_widget()->show();
1253                         }
1254                 }
1255         }
1256
1257         video_codec_combo_changed();
1258 }
1259
1260 void
1261 ExportVideoDialog::open_outfn_dialog ()
1262 {
1263         Gtk::FileChooserDialog dialog(_("Save Exported Video File"), Gtk::FILE_CHOOSER_ACTION_SAVE);
1264         dialog.set_filename (outfn_path_entry.get_text());
1265
1266         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1267         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
1268
1269         int result = dialog.run();
1270
1271         if (result == Gtk::RESPONSE_OK) {
1272                 std::string filename = dialog.get_filename();
1273
1274                 if (filename.length()) {
1275                         outfn_path_entry.set_text (filename);
1276                 }
1277         }
1278 }
1279
1280 void
1281 ExportVideoDialog::open_invid_dialog ()
1282 {
1283         Gtk::FileChooserDialog dialog(_("Save Exported Video File"), Gtk::FILE_CHOOSER_ACTION_SAVE);
1284         dialog.set_filename (invid_path_entry.get_text());
1285
1286         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1287         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
1288
1289         int result = dialog.run();
1290
1291         if (result == Gtk::RESPONSE_OK) {
1292                 std::string filename = dialog.get_filename();
1293
1294                 if (filename.length()) {
1295                         invid_path_entry.set_text (filename);
1296                 }
1297         }
1298 }