Allow setup of the frame rate that audio content is prepared for.
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2016 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 "timing_panel.h"
21 #include "wx_util.h"
22 #include "film_viewer.h"
23 #include "timecode.h"
24 #include "content_panel.h"
25 #include "lib/content.h"
26 #include "lib/image_content.h"
27 #include "lib/raw_convert.h"
28 #include "lib/subtitle_content.h"
29 #include "lib/dcp_subtitle_content.h"
30 #include "lib/audio_content.h"
31 #include "lib/text_subtitle_content.h"
32 #include <boost/foreach.hpp>
33 #include <set>
34 #include <iostream>
35
36 using std::cout;
37 using std::string;
38 using std::set;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
42
43 TimingPanel::TimingPanel (ContentPanel* p, FilmViewer* viewer)
44         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
45         : ContentSubPanel (p, S_("Timing|Timing"))
46         , _viewer (viewer)
47 {
48         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
49         _sizer->Add (grid, 0, wxALL, 8);
50
51         wxSize size = TimecodeBase::size (this);
52
53         wxSizer* labels = new wxBoxSizer (wxHORIZONTAL);
54         //// TRANSLATORS: this is an abbreviation for "hours"
55         wxStaticText* t = new wxStaticText (this, wxID_ANY, _("h"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
56 #ifdef DCPOMATIC_LINUX
57         /* Hack to work around failure to centre text on GTK */
58         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
59 #endif
60         labels->Add (t, 1, wxEXPAND);
61         add_label_to_sizer (labels, this, wxT (":"), false);
62         //// TRANSLATORS: this is an abbreviation for "minutes"
63         t = new wxStaticText (this, wxID_ANY, _("m"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
64 #ifdef DCPOMATIC_LINUX
65         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
66 #endif
67         labels->Add (t, 1, wxEXPAND);
68         add_label_to_sizer (labels, this, wxT (":"), false);
69         //// TRANSLATORS: this is an abbreviation for "seconds"
70         t = new wxStaticText (this, wxID_ANY, _("s"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
71 #ifdef DCPOMATIC_LINUX
72         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
73 #endif
74         labels->Add (t, 1, wxEXPAND);
75         add_label_to_sizer (labels, this, wxT (":"), false);
76         //// TRANSLATORS: this is an abbreviation for "frames"
77         t = new wxStaticText (this, wxID_ANY, _("f"), wxDefaultPosition, size, wxALIGN_CENTRE_HORIZONTAL);
78 #ifdef DCPOMATIC_LINUX
79         gtk_label_set_line_wrap (GTK_LABEL (t->GetHandle()), FALSE);
80 #endif
81         labels->Add (t, 1, wxEXPAND);
82         grid->Add (new wxStaticText (this, wxID_ANY, wxT ("")));
83         grid->Add (labels);
84
85         add_label_to_sizer (grid, this, _("Position"), true);
86         _position = new Timecode<DCPTime> (this);
87         grid->Add (_position);
88         add_label_to_sizer (grid, this, _("Full length"), true);
89         _full_length = new Timecode<DCPTime> (this);
90         grid->Add (_full_length);
91         add_label_to_sizer (grid, this, _("Trim from start"), true);
92         _trim_start = new Timecode<ContentTime> (this);
93         grid->Add (_trim_start);
94         _trim_start_to_playhead = new wxButton (this, wxID_ANY, _("Trim up to current position"));
95         grid->AddSpacer (0);
96         grid->Add (_trim_start_to_playhead);
97         add_label_to_sizer (grid, this, _("Trim from end"), true);
98         _trim_end = new Timecode<ContentTime> (this);
99         grid->Add (_trim_end);
100         _trim_end_to_playhead = new wxButton (this, wxID_ANY, _("Trim after current position"));
101         grid->AddSpacer (0);
102         grid->Add (_trim_end_to_playhead);
103         add_label_to_sizer (grid, this, _("Play length"), true);
104         _play_length = new Timecode<DCPTime> (this);
105         grid->Add (_play_length);
106
107         {
108                 add_label_to_sizer (grid, this, _("Video frame rate"), true);
109                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
110                 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
111                 s->Add (_video_frame_rate, 1, wxEXPAND);
112                 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
113                 _set_video_frame_rate->Enable (false);
114                 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
115                 grid->Add (s, 1, wxEXPAND);
116         }
117
118         grid->AddSpacer (0);
119
120         /* We can't use Wrap() here as it doesn't work with markup:
121          * http://trac.wxwidgets.org/ticket/13389
122          */
123
124         wxString in = _("<i>Only change this if it the content's frame rate has been read incorrectly.</i>");
125         wxString out;
126         int const width = 20;
127         int current = 0;
128         for (size_t i = 0; i < in.Length(); ++i) {
129                 if (in[i] == ' ' && current >= width) {
130                         out += '\n';
131                         current = 0;
132                 } else {
133                         out += in[i];
134                         ++current;
135                 }
136         }
137
138         t = new wxStaticText (this, wxID_ANY, wxT (""));
139         t->SetLabelMarkup (out);
140         grid->Add (t, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 6);
141
142         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
143         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
144         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
145         _trim_start_to_playhead->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_start_to_playhead_clicked, this));
146         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
147         _trim_end_to_playhead->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::trim_end_to_playhead_clicked, this));
148         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
149         _video_frame_rate->Bind       (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
150         _set_video_frame_rate->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
151
152         _viewer->ImageChanged.connect (boost::bind (&TimingPanel::setup_sensitivity, this));
153
154         setup_sensitivity ();
155 }
156
157 void
158 TimingPanel::update_full_length ()
159 {
160         set<DCPTime> check;
161         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
162                 check.insert (i->full_length ());
163         }
164
165         if (check.size() == 1) {
166                 _full_length->set (_parent->selected().front()->full_length (), _parent->film()->video_frame_rate ());
167         } else {
168                 _full_length->clear ();
169         }
170 }
171
172 void
173 TimingPanel::update_play_length ()
174 {
175         set<DCPTime> check;
176         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
177                 check.insert (i->length_after_trim ());
178         }
179
180         if (check.size() == 1) {
181                 _play_length->set (_parent->selected().front()->length_after_trim (), _parent->film()->video_frame_rate ());
182         } else {
183                 _play_length->clear ();
184         }
185 }
186
187 void
188 TimingPanel::film_content_changed (int property)
189 {
190         int const film_video_frame_rate = _parent->film()->video_frame_rate ();
191
192         /* Here we check to see if we have exactly one different value of various
193            properties, and fill the controls with that value if so.
194         */
195
196         if (property == ContentProperty::POSITION) {
197
198                 set<DCPTime> check;
199                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
200                         check.insert (i->position ());
201                 }
202
203                 if (check.size() == 1) {
204                         _position->set (_parent->selected().front()->position(), film_video_frame_rate);
205                 } else {
206                         _position->clear ();
207                 }
208
209         } else if (
210                 property == ContentProperty::LENGTH ||
211                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
212                 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
213                 property == AudioContentProperty::AUDIO_VIDEO_FRAME_RATE ||
214                 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
215                 ) {
216
217                 update_full_length ();
218
219         } else if (property == ContentProperty::TRIM_START) {
220
221                 set<ContentTime> check;
222                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
223                         check.insert (i->trim_start ());
224                 }
225
226                 if (check.size() == 1) {
227                         _trim_start->set (_parent->selected().front()->trim_start (), film_video_frame_rate);
228                 } else {
229                         _trim_start->clear ();
230                 }
231
232         } else if (property == ContentProperty::TRIM_END) {
233
234                 set<ContentTime> check;
235                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
236                         check.insert (i->trim_end ());
237                 }
238
239                 if (check.size() == 1) {
240                         _trim_end->set (_parent->selected().front()->trim_end (), film_video_frame_rate);
241                 } else {
242                         _trim_end->clear ();
243                 }
244         }
245
246         if (
247                 property == ContentProperty::LENGTH ||
248                 property == ContentProperty::TRIM_START ||
249                 property == ContentProperty::TRIM_END ||
250                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
251                 property == VideoContentProperty::VIDEO_FRAME_TYPE ||
252                 property == AudioContentProperty::AUDIO_VIDEO_FRAME_RATE ||
253                 property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE
254                 ) {
255
256                 update_play_length ();
257         }
258
259         if (property == VideoContentProperty::VIDEO_FRAME_RATE || property == SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE) {
260                 set<double> check_vc;
261                 shared_ptr<const VideoContent> vc;
262                 int count_ac = 0;
263                 shared_ptr<const AudioContent> ac;
264                 int count_sc = 0;
265                 shared_ptr<const SubtitleContent> sc;
266                 BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
267                         shared_ptr<const VideoContent> vt = dynamic_pointer_cast<const VideoContent> (i);
268                         if (vt) {
269                                 check_vc.insert (vt->video_frame_rate ());
270                                 vc = vt;
271                         }
272                         shared_ptr<const AudioContent> at = dynamic_pointer_cast<const AudioContent> (i);
273                         if (at) {
274                                 ++count_ac;
275                                 ac = at;
276                         }
277                         shared_ptr<const SubtitleContent> st = dynamic_pointer_cast<const SubtitleContent> (i);
278                         if (st) {
279                                 ++count_sc;
280                                 sc = st;
281                         }
282
283                 }
284
285                 bool const single_frame_image_content = vc && dynamic_pointer_cast<const ImageContent> (vc) && vc->number_of_paths() == 1;
286
287                 if ((check_vc.size() == 1 || count_ac == 1 || count_sc == 1) && !single_frame_image_content) {
288                         if (vc) {
289                                 checked_set (_video_frame_rate, raw_convert<string> (vc->video_frame_rate (), 5));
290                         } else if (ac) {
291                                 checked_set (_video_frame_rate, raw_convert<string> (ac->audio_video_frame_rate (), 5));
292                         } else if (sc) {
293                                 checked_set (_video_frame_rate, raw_convert<string> (sc->subtitle_video_frame_rate (), 5));
294                         }
295                         _video_frame_rate->Enable (true);
296                 } else {
297                         checked_set (_video_frame_rate, wxT (""));
298                         _video_frame_rate->Enable (false);
299                 }
300         }
301
302         bool have_still = false;
303         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
304                 shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (i);
305                 if (ic && ic->still ()) {
306                         have_still = true;
307                 }
308         }
309
310         _full_length->set_editable (have_still);
311         _play_length->set_editable (!have_still);
312         _set_video_frame_rate->Enable (false);
313         setup_sensitivity ();
314 }
315
316 void
317 TimingPanel::position_changed ()
318 {
319         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
320                 i->set_position (_position->get (_parent->film()->video_frame_rate ()));
321         }
322 }
323
324 void
325 TimingPanel::full_length_changed ()
326 {
327         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
328                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
329                 if (ic && ic->still ()) {
330                         int const vfr = _parent->film()->video_frame_rate ();
331                         ic->set_video_length (_full_length->get (vfr).frames_round (vfr));
332                 }
333         }
334 }
335
336 void
337 TimingPanel::trim_start_changed ()
338 {
339         DCPTime const ph = _viewer->position ();
340
341         _viewer->set_coalesce_player_changes (true);
342
343         shared_ptr<Content> ref;
344         optional<FrameRateChange> ref_frc;
345         optional<DCPTime> ref_ph;
346         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
347                 if (i->position() <= ph && ph < i->end()) {
348                         /* The playhead is in i.  Use it as a reference to work out
349                            where to put the playhead post-trim; we're trying to keep the playhead
350                            at the same frame of content that we're looking at pre-trim.
351                         */
352                         ref = i;
353                         ref_frc = _parent->film()->active_frame_rate_change (i->position ());
354                         ref_ph = ph - i->position() + DCPTime (i->trim_start(), ref_frc.get());
355                 }
356
357                 i->set_trim_start (_trim_start->get (_parent->film()->video_frame_rate ()));
358         }
359
360         if (ref) {
361                 _viewer->set_position (max (DCPTime(), ref_ph.get() + ref->position() - DCPTime (ref->trim_start(), ref_frc.get())));
362         }
363
364         _viewer->set_coalesce_player_changes (false);
365 }
366
367 void
368 TimingPanel::trim_end_changed ()
369 {
370         _viewer->set_coalesce_player_changes (true);
371
372         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
373                 i->set_trim_end (_trim_end->get (_parent->film()->video_frame_rate ()));
374         }
375
376         /* XXX: maybe playhead-off-the-end-of-the-film should be handled elsewhere */
377         if (_viewer->position() >= _parent->film()->length()) {
378                 _viewer->set_position (_parent->film()->length() - DCPTime::from_frames (1, _parent->film()->video_frame_rate()));
379         }
380
381         _viewer->set_coalesce_player_changes (true);
382 }
383
384 void
385 TimingPanel::play_length_changed ()
386 {
387         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
388                 FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
389                 i->set_trim_end (
390                         ContentTime (i->full_length() - _play_length->get (_parent->film()->video_frame_rate()), frc)
391                         - i->trim_start ()
392                         );
393         }
394 }
395
396 void
397 TimingPanel::video_frame_rate_changed ()
398 {
399         _set_video_frame_rate->Enable (true);
400 }
401
402 void
403 TimingPanel::set_video_frame_rate ()
404 {
405         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
406                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (i);
407                 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (i);
408                 shared_ptr<DCPSubtitleContent> dsc = dynamic_pointer_cast<DCPSubtitleContent> (i);
409                 shared_ptr<TextSubtitleContent> tsc = dynamic_pointer_cast<TextSubtitleContent> (i);
410                 double const fr = raw_convert<double> (wx_to_std (_video_frame_rate->GetValue ()));
411                 if (vc) {
412                         vc->set_video_frame_rate (fr);
413                 } else if (ac) {
414                         /* Audio but not video, i.e. SndfileContent */
415                         ac->set_audio_video_frame_rate (fr);
416                 } else if (dsc) {
417                         dsc->set_subtitle_video_frame_rate (fr);
418                 } else if (tsc) {
419                         tsc->set_subtitle_video_frame_rate (fr);
420                 }
421                 _set_video_frame_rate->Enable (false);
422         }
423 }
424
425 void
426 TimingPanel::content_selection_changed ()
427 {
428         setup_sensitivity ();
429
430         film_content_changed (ContentProperty::POSITION);
431         film_content_changed (ContentProperty::LENGTH);
432         film_content_changed (ContentProperty::TRIM_START);
433         film_content_changed (ContentProperty::TRIM_END);
434         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
435         film_content_changed (SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE);
436         film_content_changed (AudioContentProperty::AUDIO_VIDEO_FRAME_RATE);
437 }
438
439 void
440 TimingPanel::film_changed (Film::Property p)
441 {
442         if (p == Film::VIDEO_FRAME_RATE) {
443                 update_full_length ();
444                 update_play_length ();
445         }
446 }
447
448 void
449 TimingPanel::trim_start_to_playhead_clicked ()
450 {
451         DCPTime const ph = _viewer->position ();
452         optional<DCPTime> new_ph;
453
454         _viewer->set_coalesce_player_changes (true);
455
456         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
457                 if (i->position() < ph && ph < i->end ()) {
458                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
459                         i->set_trim_start (i->trim_start() + ContentTime (ph - i->position (), frc));
460                         new_ph = i->position ();
461                 }
462         }
463
464         if (new_ph) {
465                 _viewer->set_position (new_ph.get());
466         }
467
468         _viewer->set_coalesce_player_changes (false);
469 }
470
471 void
472 TimingPanel::trim_end_to_playhead_clicked ()
473 {
474         DCPTime const ph = _viewer->position ();
475         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected ()) {
476                 if (i->position() < ph && ph < i->end ()) {
477                         FrameRateChange const frc = _parent->film()->active_frame_rate_change (i->position ());
478                         i->set_trim_end (ContentTime (i->position() + i->full_length() - ph - DCPTime::from_frames (1, frc.dcp), frc) - i->trim_start());
479                 }
480         }
481 }
482
483 void
484 TimingPanel::setup_sensitivity ()
485 {
486         bool const e = !_parent->selected().empty ();
487
488         _position->Enable (e);
489         _full_length->Enable (e);
490         _trim_start->Enable (e);
491         _trim_end->Enable (e);
492         _play_length->Enable (e);
493         _video_frame_rate->Enable (e);
494
495         DCPTime const ph = _viewer->position ();
496         bool any_over_ph = false;
497         BOOST_FOREACH (shared_ptr<const Content> i, _parent->selected ()) {
498                 if (i->position() <= ph && ph < i->end()) {
499                         any_over_ph = true;
500                 }
501         }
502
503         _trim_start_to_playhead->Enable (any_over_ph);
504         _trim_end_to_playhead->Enable (any_over_ph);
505 }