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