Merge master.
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2014 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/filter.h"
22 #include "lib/ffmpeg_content.h"
23 #include "lib/colour_conversion.h"
24 #include "lib/config.h"
25 #include "lib/util.h"
26 #include "lib/ratio.h"
27 #include "filter_dialog.h"
28 #include "video_panel.h"
29 #include "wx_util.h"
30 #include "film_editor.h"
31 #include "content_colour_conversion_dialog.h"
32 #include "content_widget.h"
33
34 using std::vector;
35 using std::string;
36 using std::pair;
37 using std::cout;
38 using std::list;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::bind;
42 using boost::optional;
43
44 static VideoContentScale
45 index_to_scale (int n)
46 {
47         vector<VideoContentScale> scales = VideoContentScale::all ();
48         assert (n >= 0);
49         assert (n < int (scales.size ()));
50         return scales[n];
51 }
52
53 static int
54 scale_to_index (VideoContentScale scale)
55 {
56         vector<VideoContentScale> scales = VideoContentScale::all ();
57         for (size_t i = 0; i < scales.size(); ++i) {
58                 if (scales[i] == scale) {
59                         return i;
60                 }
61         }
62
63         assert (false);
64 }
65
66 VideoPanel::VideoPanel (FilmEditor* e)
67         : FilmEditorPanel (e, _("Video"))
68 {
69         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
70         _sizer->Add (grid, 0, wxALL, 8);
71
72         int r = 0;
73
74         add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
75         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
76                 this,
77                 new wxChoice (this, wxID_ANY),
78                 VideoContentProperty::VIDEO_FRAME_TYPE,
79                 boost::mem_fn (&VideoContent::video_frame_type),
80                 boost::mem_fn (&VideoContent::set_video_frame_type),
81                 &caster<int, VideoFrameType>,
82                 &caster<VideoFrameType, int>
83                 );
84         _frame_type->add (grid, wxGBPosition (r, 1));
85         ++r;
86         
87         add_label_to_grid_bag_sizer (grid, this, _("Left crop"), true, wxGBPosition (r, 0));
88         _left_crop = new ContentSpinCtrl<VideoContent> (
89                 this,
90                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
91                 VideoContentProperty::VIDEO_CROP,
92                 boost::mem_fn (&VideoContent::left_crop),
93                 boost::mem_fn (&VideoContent::set_left_crop)
94                 );
95         _left_crop->add (grid, wxGBPosition (r, 1));
96         ++r;
97
98         add_label_to_grid_bag_sizer (grid, this, _("Right crop"), true, wxGBPosition (r, 0));
99         _right_crop = new ContentSpinCtrl<VideoContent> (
100                 this,
101                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
102                 VideoContentProperty::VIDEO_CROP,
103                 boost::mem_fn (&VideoContent::right_crop),
104                 boost::mem_fn (&VideoContent::set_right_crop)
105                 );
106         _right_crop->add (grid, wxGBPosition (r, 1));
107         ++r;
108         
109         add_label_to_grid_bag_sizer (grid, this, _("Top crop"), true, wxGBPosition (r, 0));
110         _top_crop = new ContentSpinCtrl<VideoContent> (
111                 this,
112                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
113                 VideoContentProperty::VIDEO_CROP,
114                 boost::mem_fn (&VideoContent::top_crop),
115                 boost::mem_fn (&VideoContent::set_top_crop)
116                 );
117         _top_crop->add (grid, wxGBPosition (r,1 ));
118         ++r;
119         
120         add_label_to_grid_bag_sizer (grid, this, _("Bottom crop"), true, wxGBPosition (r, 0));
121         _bottom_crop = new ContentSpinCtrl<VideoContent> (
122                 this,
123                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
124                 VideoContentProperty::VIDEO_CROP,
125                 boost::mem_fn (&VideoContent::bottom_crop),
126                 boost::mem_fn (&VideoContent::set_bottom_crop)
127                 );
128         _bottom_crop->add (grid, wxGBPosition (r, 1));
129         ++r;
130
131         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
132         _scale = new ContentChoice<VideoContent, VideoContentScale> (
133                 this,
134                 new wxChoice (this, wxID_ANY),
135                 VideoContentProperty::VIDEO_SCALE,
136                 boost::mem_fn (&VideoContent::scale),
137                 boost::mem_fn (&VideoContent::set_scale),
138                 &index_to_scale,
139                 &scale_to_index
140                 );
141         _scale->add (grid, wxGBPosition (r, 1));
142         ++r;
143
144         {
145                 add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
146                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
147
148                 wxClientDC dc (this);
149                 wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
150                 size.SetHeight (-1);
151                 
152                 _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
153                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
154                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
155                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
156                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
157         }
158         ++r;
159         
160         {
161                 add_label_to_grid_bag_sizer (grid, this, _("Colour conversion"), true, wxGBPosition (r, 0));
162                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
163
164                 wxClientDC dc (this);
165                 wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
166                 size.SetHeight (-1);
167                 
168                 _colour_conversion = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size);
169
170                 s->Add (_colour_conversion, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
171                 _colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
172                 s->Add (_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
173                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
174         }
175         ++r;
176
177         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
178         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
179         wxFont font = _description->GetFont();
180         font.SetStyle(wxFONTSTYLE_ITALIC);
181         font.SetPointSize(font.GetPointSize() - 1);
182         _description->SetFont(font);
183         ++r;
184
185         _left_crop->wrapped()->SetRange (0, 1024);
186         _top_crop->wrapped()->SetRange (0, 1024);
187         _right_crop->wrapped()->SetRange (0, 1024);
188         _bottom_crop->wrapped()->SetRange (0, 1024);
189
190         vector<VideoContentScale> scales = VideoContentScale::all ();
191         _scale->wrapped()->Clear ();
192         for (vector<VideoContentScale>::iterator i = scales.begin(); i != scales.end(); ++i) {
193                 _scale->wrapped()->Append (std_to_wx (i->name ()));
194         }
195
196         _frame_type->wrapped()->Append (_("2D"));
197         _frame_type->wrapped()->Append (_("3D left/right"));
198         _frame_type->wrapped()->Append (_("3D top/bottom"));
199
200         _filters_button->Bind           (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
201         _colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
202 }
203
204 void
205 VideoPanel::film_changed (Film::Property property)
206 {
207         switch (property) {
208         case Film::CONTAINER:
209         case Film::VIDEO_FRAME_RATE:
210         case Film::RESOLUTION:
211                 setup_description ();
212                 break;
213         default:
214                 break;
215         }
216 }
217
218 void
219 VideoPanel::film_content_changed (int property)
220 {
221         VideoContentList vc = _editor->selected_video_content ();
222         shared_ptr<VideoContent> vcs;
223         shared_ptr<FFmpegContent> fcs;
224         if (!vc.empty ()) {
225                 vcs = vc.front ();
226                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
227         }
228         
229         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
230                 setup_description ();
231         } else if (property == VideoContentProperty::VIDEO_CROP) {
232                 setup_description ();
233         } else if (property == VideoContentProperty::VIDEO_SCALE) {
234                 setup_description ();
235         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
236                 setup_description ();
237         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
238                 optional<size_t> preset = vcs ? vcs->colour_conversion().preset () : optional<size_t> ();
239                 vector<PresetColourConversion> cc = Config::instance()->colour_conversions ();
240                 _colour_conversion->SetLabel (preset ? std_to_wx (cc[preset.get()].name) : _("Custom"));
241         } else if (property == FFmpegContentProperty::FILTERS) {
242                 if (fcs) {
243                         string const p = Filter::ffmpeg_string (fcs->filters ());
244                         if (p.empty ()) {
245                                 _filters->SetLabel (_("None"));
246                         } else {
247                                 _filters->SetLabel (std_to_wx (p));
248                         }
249                 }
250         }
251 }
252
253 /** Called when the `Edit filters' button has been clicked */
254 void
255 VideoPanel::edit_filters_clicked ()
256 {
257         FFmpegContentList c = _editor->selected_ffmpeg_content ();
258         if (c.size() != 1) {
259                 return;
260         }
261
262         FilterDialog* d = new FilterDialog (this, c.front()->filters());
263         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
264         d->ShowModal ();
265         d->Destroy ();
266 }
267
268 void
269 VideoPanel::setup_description ()
270 {
271         VideoContentList vc = _editor->selected_video_content ();
272         if (vc.empty ()) {
273                 _description->SetLabel ("");
274                 return;
275         } else if (vc.size() > 1) {
276                 _description->SetLabel (_("Multiple content selected"));
277                 return;
278         }
279
280         shared_ptr<VideoContent> vcs = vc.front ();
281
282         wxString d;
283
284         int lines = 0;
285
286         if (vcs->video_size().width && vcs->video_size().height) {
287                 d << wxString::Format (
288                         _("Content video is %dx%d (%.2f:1)\n"),
289                         vcs->video_size_after_3d_split().width,
290                         vcs->video_size_after_3d_split().height,
291                         vcs->video_size_after_3d_split().ratio ()
292                         );
293                 ++lines;
294         }
295
296         Crop const crop = vcs->crop ();
297         if ((crop.left || crop.right || crop.top || crop.bottom) && vcs->video_size() != dcp::Size (0, 0)) {
298                 dcp::Size cropped = vcs->video_size_after_crop ();
299                 d << wxString::Format (
300                         _("Cropped to %dx%d (%.2f:1)\n"),
301                         cropped.width, cropped.height,
302                         cropped.ratio ()
303                         );
304                 ++lines;
305         }
306
307         dcp::Size const container_size = _editor->film()->frame_size ();
308         dcp::Size const scaled = vcs->scale().size (vcs, container_size, container_size);
309
310         if (scaled != vcs->video_size_after_crop ()) {
311                 d << wxString::Format (
312                         _("Scaled to %dx%d (%.2f:1)\n"),
313                         scaled.width, scaled.height,
314                         scaled.ratio ()
315                         );
316                 ++lines;
317         }
318         
319         if (scaled != container_size) {
320                 d << wxString::Format (
321                         _("Padded with black to %dx%d (%.2f:1)\n"),
322                         container_size.width, container_size.height,
323                         container_size.ratio ()
324                         );
325                 ++lines;
326         }
327
328         d << wxString::Format (_("Content frame rate %.4f\n"), vcs->video_frame_rate ());
329         ++lines;
330         FrameRateChange frc (vcs->video_frame_rate(), _editor->film()->video_frame_rate ());
331         d << std_to_wx (frc.description) << "\n";
332         ++lines;
333
334         for (int i = lines; i < 6; ++i) {
335                 d << wxT ("\n ");
336         }
337
338         _description->SetLabel (d);
339         _sizer->Layout ();
340 }
341
342 void
343 VideoPanel::edit_colour_conversion_clicked ()
344 {
345         VideoContentList vc = _editor->selected_video_content ();
346         if (vc.size() != 1) {
347                 return;
348         }
349
350         ColourConversion conversion = vc.front()->colour_conversion ();
351         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
352         d->set (conversion);
353         d->ShowModal ();
354
355         vc.front()->set_colour_conversion (d->get ());
356         d->Destroy ();
357 }
358
359 void
360 VideoPanel::content_selection_changed ()
361 {
362         VideoContentList sel = _editor->selected_video_content ();
363         bool const single = sel.size() == 1;
364
365         _left_crop->set_content (sel);
366         _right_crop->set_content (sel);
367         _top_crop->set_content (sel);
368         _bottom_crop->set_content (sel);
369         _frame_type->set_content (sel);
370         _scale->set_content (sel);
371
372         /* Things that are only allowed with single selections */
373         _filters_button->Enable (single);
374         _colour_conversion_button->Enable (single);
375
376         film_content_changed (VideoContentProperty::VIDEO_CROP);
377         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
378         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
379         film_content_changed (FFmpegContentProperty::FILTERS);
380 }