Merge branch '1.0' of ssh://carlh.dnsalias.org/home/carl/git/dvdomatic into 1.0
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <wx/spinctrl.h>
21 #include "lib/ratio.h"
22 #include "lib/filter.h"
23 #include "filter_dialog.h"
24 #include "video_panel.h"
25 #include "wx_util.h"
26 #include "film_editor.h"
27
28 using std::vector;
29 using std::string;
30 using std::pair;
31 using std::cout;
32 using boost::shared_ptr;
33 using boost::dynamic_pointer_cast;
34 using boost::bind;
35
36 VideoPanel::VideoPanel (FilmEditor* e)
37         : FilmEditorPanel (e, _("Video"))
38 {
39         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
40         _sizer->Add (grid, 0, wxALL, 8);
41
42         int r = 0;
43
44         add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
45         _frame_type = new wxChoice (this, wxID_ANY);
46         grid->Add (_frame_type, wxGBPosition (r, 1));
47         ++r;
48         
49         add_label_to_grid_bag_sizer (grid, this, _("Left crop"), true, wxGBPosition (r, 0));
50         _left_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
51         grid->Add (_left_crop, wxGBPosition (r, 1));
52         ++r;
53
54         add_label_to_grid_bag_sizer (grid, this, _("Right crop"), true, wxGBPosition (r, 0));
55         _right_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
56         grid->Add (_right_crop, wxGBPosition (r, 1));
57         ++r;
58         
59         add_label_to_grid_bag_sizer (grid, this, _("Top crop"), true, wxGBPosition (r, 0));
60         _top_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
61         grid->Add (_top_crop, wxGBPosition (r, 1));
62         ++r;
63         
64         add_label_to_grid_bag_sizer (grid, this, _("Bottom crop"), true, wxGBPosition (r, 0));
65         _bottom_crop = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
66         grid->Add (_bottom_crop, wxGBPosition (r, 1));
67         ++r;
68
69         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
70         _ratio = new wxChoice (this, wxID_ANY);
71         grid->Add (_ratio, wxGBPosition (r, 1));
72         ++r;
73
74         {
75                 add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
76                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
77                 _filters = new wxStaticText (this, wxID_ANY, _("None"));
78                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
79                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
80                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
81                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
82         }
83         ++r;
84
85         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
86         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
87         wxFont font = _description->GetFont();
88         font.SetStyle(wxFONTSTYLE_ITALIC);
89         font.SetPointSize(font.GetPointSize() - 1);
90         _description->SetFont(font);
91         ++r;
92
93         _left_crop->SetRange (0, 1024);
94         _top_crop->SetRange (0, 1024);
95         _right_crop->SetRange (0, 1024);
96         _bottom_crop->SetRange (0, 1024);
97
98         vector<Ratio const *> ratios = Ratio::all ();
99         _ratio->Clear ();
100         for (vector<Ratio const *>::iterator i = ratios.begin(); i != ratios.end(); ++i) {
101                 _ratio->Append (std_to_wx ((*i)->nickname ()));
102         }
103
104         _frame_type->Append (_("2D"));
105         _frame_type->Append (_("3D left/right"));
106
107         _frame_type->Bind     (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&VideoPanel::frame_type_changed, this));
108         _left_crop->Bind      (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::left_crop_changed, this));
109         _right_crop->Bind     (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::right_crop_changed, this));
110         _top_crop->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::top_crop_changed, this));
111         _bottom_crop->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::bottom_crop_changed, this));
112         _ratio->Bind          (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&VideoPanel::ratio_changed, this));
113         _filters_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
114 }
115
116
117 /** Called when the left crop widget has been changed */
118 void
119 VideoPanel::left_crop_changed ()
120 {
121         shared_ptr<VideoContent> c = _editor->selected_video_content ();
122         if (!c) {
123                 return;
124         }
125
126         c->set_left_crop (_left_crop->GetValue ());
127 }
128
129 /** Called when the right crop widget has been changed */
130 void
131 VideoPanel::right_crop_changed ()
132 {
133         shared_ptr<VideoContent> c = _editor->selected_video_content ();
134         if (!c) {
135                 return;
136         }
137
138         c->set_right_crop (_right_crop->GetValue ());
139 }
140
141 /** Called when the top crop widget has been changed */
142 void
143 VideoPanel::top_crop_changed ()
144 {
145         shared_ptr<VideoContent> c = _editor->selected_video_content ();
146         if (!c) {
147                 return;
148         }
149
150         c->set_top_crop (_top_crop->GetValue ());
151 }
152
153 /** Called when the bottom crop value has been changed */
154 void
155 VideoPanel::bottom_crop_changed ()
156 {
157         shared_ptr<VideoContent> c = _editor->selected_video_content ();
158         if (!c) {
159                 return;
160         }
161
162         c->set_bottom_crop (_bottom_crop->GetValue ());
163 }
164
165 void
166 VideoPanel::film_changed (Film::Property property)
167 {
168         switch (property) {
169         case Film::CONTAINER:
170         case Film::VIDEO_FRAME_RATE:
171                 setup_description ();
172                 break;
173         default:
174                 break;
175         }
176 }
177
178 void
179 VideoPanel::film_content_changed (shared_ptr<Content> c, int property)
180 {
181         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c);
182         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
183
184         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
185                 checked_set (_frame_type, vc ? vc->video_frame_type () : VIDEO_FRAME_TYPE_2D);
186                 setup_description ();
187         } else if (property == VideoContentProperty::VIDEO_CROP) {
188                 checked_set (_left_crop,   vc ? vc->crop().left : 0);
189                 checked_set (_right_crop,  vc ? vc->crop().right        : 0);
190                 checked_set (_top_crop,    vc ? vc->crop().top  : 0);
191                 checked_set (_bottom_crop, vc ? vc->crop().bottom : 0);
192                 setup_description ();
193         } else if (property == VideoContentProperty::VIDEO_RATIO) {
194                 if (vc) {
195                         int n = 0;
196                         vector<Ratio const *> ratios = Ratio::all ();
197                         vector<Ratio const *>::iterator i = ratios.begin ();
198                         while (i != ratios.end() && *i != vc->ratio()) {
199                                 ++i;
200                                 ++n;
201                         }
202
203                         if (i == ratios.end()) {
204                                 checked_set (_ratio, -1);
205                         } else {
206                                 checked_set (_ratio, n);
207                         }
208                 } else {
209                         checked_set (_ratio, -1);
210                 }
211                 setup_description ();
212         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
213                 setup_description ();
214         } else if (property == FFmpegContentProperty::FILTERS) {
215                 if (fc) {
216                         pair<string, string> p = Filter::ffmpeg_strings (fc->filters ());
217                         if (p.first.empty () && p.second.empty ()) {
218                                 _filters->SetLabel (_("None"));
219                         } else {
220                                 string const b = p.first + " " + p.second;
221                                 _filters->SetLabel (std_to_wx (b));
222                         }
223                 }
224         }
225 }
226
227 /** Called when the `Edit filters' button has been clicked */
228 void
229 VideoPanel::edit_filters_clicked ()
230 {
231         shared_ptr<Content> c = _editor->selected_content ();
232         if (!c) {
233                 return;
234         }
235
236         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c);
237         if (!fc) {
238                 return;
239         }
240         
241         FilterDialog* d = new FilterDialog (this, fc->filters());
242         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, fc, _1));
243         d->ShowModal ();
244         d->Destroy ();
245 }
246
247 void
248 VideoPanel::setup_description ()
249 {
250         shared_ptr<VideoContent> vc = _editor->selected_video_content ();
251         if (!vc) {
252                 _description->SetLabel ("");
253                 return;
254         }
255
256         wxString d;
257
258         int lines = 0;
259
260         if (vc->video_size().width && vc->video_size().height) {
261                 d << wxString::Format (
262                         _("Content video is %dx%d (%.2f:1)\n"),
263                         vc->video_size_after_3d_split().width, vc->video_size_after_3d_split().height,
264                         float (vc->video_size_after_3d_split().width) / vc->video_size_after_3d_split().height
265                         );
266                 ++lines;
267         }
268
269         Crop const crop = vc->crop ();
270         if ((crop.left || crop.right || crop.top || crop.bottom) && vc->video_size() != libdcp::Size (0, 0)) {
271                 libdcp::Size cropped = vc->video_size_after_3d_split ();
272                 cropped.width -= crop.left + crop.right;
273                 cropped.height -= crop.top + crop.bottom;
274                 d << wxString::Format (
275                         _("Cropped to %dx%d (%.2f:1)\n"),
276                         cropped.width, cropped.height,
277                         float (cropped.width) / cropped.height
278                         );
279                 ++lines;
280         }
281
282         Ratio const * ratio = vc->ratio ();
283         if (ratio) {
284                 libdcp::Size container_size = _editor->film()->container()->size (_editor->film()->full_frame ());
285                 
286                 libdcp::Size const scaled = ratio->size (container_size);
287                 d << wxString::Format (
288                         _("Scaled to %dx%d (%.2f:1)\n"),
289                         scaled.width, scaled.height,
290                         float (scaled.width) / scaled.height
291                         );
292                 ++lines;
293
294                 if (scaled != container_size) {
295                         d << wxString::Format (
296                                 _("Padded with black to %dx%d (%.2f:1)\n"),
297                                 container_size.width, container_size.height,
298                                 float (container_size.width) / container_size.height
299                                 );
300                         ++lines;
301                 }
302         }
303
304         d << wxString::Format (_("Content frame rate %.4f\n"), vc->video_frame_rate ());
305         ++lines;
306         FrameRateConversion frc (vc->video_frame_rate(), _editor->film()->video_frame_rate ());
307         d << frc.description << "\n";
308         ++lines;
309
310         for (int i = lines; i < 6; ++i) {
311                 d << wxT ("\n ");
312         }
313
314         _description->SetLabel (d);
315         _sizer->Layout ();
316 }
317
318
319 void
320 VideoPanel::ratio_changed ()
321 {
322         if (!_editor->film ()) {
323                 return;
324         }
325
326         shared_ptr<VideoContent> vc = _editor->selected_video_content ();
327         
328         int const n = _ratio->GetSelection ();
329         if (n >= 0) {
330                 vector<Ratio const *> ratios = Ratio::all ();
331                 assert (n < int (ratios.size()));
332                 vc->set_ratio (ratios[n]);
333         }
334 }
335
336 void
337 VideoPanel::frame_type_changed ()
338 {
339         shared_ptr<VideoContent> vc = _editor->selected_video_content ();
340         if (vc) {
341                 vc->set_video_frame_type (static_cast<VideoFrameType> (_frame_type->GetSelection ()));
342         }
343 }