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