Merge branch '1.0' into 1.0-seek
[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
203         _filters_button->Bind           (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
204         _colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
205 }
206
207 void
208 VideoPanel::film_changed (Film::Property property)
209 {
210         switch (property) {
211         case Film::CONTAINER:
212         case Film::VIDEO_FRAME_RATE:
213                 setup_description ();
214                 break;
215         default:
216                 break;
217         }
218 }
219
220 void
221 VideoPanel::film_content_changed (int property)
222 {
223         VideoContentList vc = _editor->selected_video_content ();
224         shared_ptr<VideoContent> vcs;
225         shared_ptr<FFmpegContent> fcs;
226         if (!vc.empty ()) {
227                 vcs = vc.front ();
228                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
229         }
230         
231         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
232                 setup_description ();
233         } else if (property == VideoContentProperty::VIDEO_CROP) {
234                 setup_description ();
235         } else if (property == VideoContentProperty::VIDEO_RATIO) {
236                 setup_description ();
237         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
238                 setup_description ();
239         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
240                 optional<size_t> preset = vcs ? vcs->colour_conversion().preset () : optional<size_t> ();
241                 vector<PresetColourConversion> cc = Config::instance()->colour_conversions ();
242                 _colour_conversion->SetLabel (preset ? std_to_wx (cc[preset.get()].name) : _("Custom"));
243         } else if (property == FFmpegContentProperty::FILTERS) {
244                 if (fcs) {
245                         pair<string, string> p = Filter::ffmpeg_strings (fcs->filters ());
246                         if (p.first.empty () && p.second.empty ()) {
247                                 _filters->SetLabel (_("None"));
248                         } else {
249                                 string const b = p.first + " " + p.second;
250                                 _filters->SetLabel (std_to_wx (b));
251                         }
252                 }
253         }
254 }
255
256 /** Called when the `Edit filters' button has been clicked */
257 void
258 VideoPanel::edit_filters_clicked ()
259 {
260         FFmpegContentList c = _editor->selected_ffmpeg_content ();
261         if (c.size() != 1) {
262                 return;
263         }
264
265         FilterDialog* d = new FilterDialog (this, c.front()->filters());
266         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
267         d->ShowModal ();
268         d->Destroy ();
269 }
270
271 void
272 VideoPanel::setup_description ()
273 {
274         VideoContentList vc = _editor->selected_video_content ();
275         if (vc.empty ()) {
276                 _description->SetLabel ("");
277                 return;
278         } else if (vc.size() > 1) {
279                 _description->SetLabel (_("Multiple content selected"));
280                 return;
281         }
282
283         shared_ptr<VideoContent> vcs = vc.front ();
284
285         wxString d;
286
287         int lines = 0;
288
289         if (vcs->video_size().width && vcs->video_size().height) {
290                 d << wxString::Format (
291                         _("Content video is %dx%d (%.2f:1)\n"),
292                         vcs->video_size_after_3d_split().width,
293                         vcs->video_size_after_3d_split().height,
294                         vcs->video_size_after_3d_split().ratio ()
295                         );
296                 ++lines;
297         }
298
299         Crop const crop = vcs->crop ();
300         if ((crop.left || crop.right || crop.top || crop.bottom) && vcs->video_size() != libdcp::Size (0, 0)) {
301                 libdcp::Size cropped = vcs->video_size_after_crop ();
302                 d << wxString::Format (
303                         _("Cropped to %dx%d (%.2f:1)\n"),
304                         cropped.width, cropped.height,
305                         cropped.ratio ()
306                         );
307                 ++lines;
308         }
309
310         Ratio const * ratio = vcs->ratio ();
311         libdcp::Size container_size = fit_ratio_within (_editor->film()->container()->ratio (), _editor->film()->full_frame ());
312         float const ratio_value = ratio ? ratio->ratio() : vcs->video_size_after_crop().ratio ();
313
314         /* We have a specified ratio to scale to */
315         libdcp::Size const scaled = fit_ratio_within (ratio_value, container_size);
316         
317         d << wxString::Format (
318                 _("Scaled to %dx%d (%.2f:1)\n"),
319                 scaled.width, scaled.height,
320                 scaled.ratio ()
321                 );
322         ++lines;
323         
324         if (scaled != container_size) {
325                 d << wxString::Format (
326                         _("Padded with black to %dx%d (%.2f:1)\n"),
327                         container_size.width, container_size.height,
328                         container_size.ratio ()
329                         );
330                 ++lines;
331         }
332
333         d << wxString::Format (_("Content frame rate %.4f\n"), vcs->video_frame_rate ());
334         ++lines;
335         FrameRateChange frc (vcs->video_frame_rate(), _editor->film()->video_frame_rate ());
336         d << frc.description << "\n";
337         ++lines;
338
339         for (int i = lines; i < 6; ++i) {
340                 d << wxT ("\n ");
341         }
342
343         _description->SetLabel (d);
344         _sizer->Layout ();
345 }
346
347 void
348 VideoPanel::edit_colour_conversion_clicked ()
349 {
350         VideoContentList vc = _editor->selected_video_content ();
351         if (vc.size() != 1) {
352                 return;
353         }
354
355         ColourConversion conversion = vc.front()->colour_conversion ();
356         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
357         d->set (conversion);
358         d->ShowModal ();
359
360         vc.front()->set_colour_conversion (d->get ());
361         d->Destroy ();
362 }
363
364 void
365 VideoPanel::content_selection_changed ()
366 {
367         VideoContentList sel = _editor->selected_video_content ();
368         bool const single = sel.size() == 1;
369
370         _left_crop->set_content (sel);
371         _right_crop->set_content (sel);
372         _top_crop->set_content (sel);
373         _bottom_crop->set_content (sel);
374         _frame_type->set_content (sel);
375         _ratio->set_content (sel);
376
377         /* Things that are only allowed with single selections */
378         _filters_button->Enable (single);
379         _colour_conversion_button->Enable (single);
380
381         film_content_changed (VideoContentProperty::VIDEO_CROP);
382         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
383         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
384         film_content_changed (FFmpegContentProperty::FILTERS);
385 }