Make jump-to-content-start on selection in the content list optional.
[dcpomatic.git] / src / wx / film_viewer.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/film_viewer.cc
22  *  @brief A wx widget to view a preview of a Film.
23  */
24
25 #include "film_viewer.h"
26 #include "playhead_to_timecode_dialog.h"
27 #include "playhead_to_frame_dialog.h"
28 #include "wx_util.h"
29 #include "lib/film.h"
30 #include "lib/ratio.h"
31 #include "lib/util.h"
32 #include "lib/job_manager.h"
33 #include "lib/image.h"
34 #include "lib/exceptions.h"
35 #include "lib/examine_content_job.h"
36 #include "lib/filter.h"
37 #include "lib/player.h"
38 #include "lib/player_video.h"
39 #include "lib/video_content.h"
40 #include "lib/video_decoder.h"
41 #include "lib/timer.h"
42 #include "lib/log.h"
43 #include "lib/config.h"
44 extern "C" {
45 #include <libavutil/pixfmt.h>
46 }
47 #include <dcp/exceptions.h>
48 #include <wx/tglbtn.h>
49 #include <iostream>
50 #include <iomanip>
51
52 using std::string;
53 using std::pair;
54 using std::min;
55 using std::max;
56 using std::cout;
57 using std::list;
58 using std::bad_alloc;
59 using std::make_pair;
60 using std::exception;
61 using boost::shared_ptr;
62 using boost::dynamic_pointer_cast;
63 using boost::weak_ptr;
64 using boost::optional;
65 using dcp::Size;
66
67 FilmViewer::FilmViewer (wxWindow* p)
68         : wxPanel (p)
69         , _panel (new wxPanel (this))
70         , _outline_content (new wxCheckBox (this, wxID_ANY, _("Outline content")))
71         , _left_eye (new wxRadioButton (this, wxID_ANY, _("Left eye"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP))
72         , _right_eye (new wxRadioButton (this, wxID_ANY, _("Right eye")))
73         , _jump_to_selected (new wxCheckBox (this, wxID_ANY, _("Jump to selected content")))
74         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
75         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
76         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
77         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
78         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
79         , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
80         , _coalesce_player_changes (false)
81         , _pending_player_change (false)
82         , _last_get_accurate (true)
83 {
84 #ifndef __WXOSX__
85         _panel->SetDoubleBuffered (true);
86 #endif
87
88         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
89
90         _v_sizer = new wxBoxSizer (wxVERTICAL);
91         SetSizer (_v_sizer);
92
93         _v_sizer->Add (_panel, 1, wxEXPAND);
94
95         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
96         view_options->Add (_outline_content, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
97         view_options->Add (_left_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
98         view_options->Add (_right_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
99         view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
100         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
101
102         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
103
104         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
105         time_sizer->Add (_frame_number, 0, wxEXPAND);
106         time_sizer->Add (_timecode, 0, wxEXPAND);
107
108         h_sizer->Add (_back_button, 0, wxALL, 2);
109         h_sizer->Add (time_sizer, 0, wxEXPAND);
110         h_sizer->Add (_forward_button, 0, wxALL, 2);
111         h_sizer->Add (_play_button, 0, wxEXPAND);
112         h_sizer->Add (_slider, 1, wxEXPAND);
113
114         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
115
116         _frame_number->SetMinSize (wxSize (84, -1));
117         _back_button->SetMinSize (wxSize (32, -1));
118         _forward_button->SetMinSize (wxSize (32, -1));
119
120         _panel->Bind            (wxEVT_PAINT,             boost::bind (&FilmViewer::paint_panel,     this));
121         _panel->Bind            (wxEVT_SIZE,              boost::bind (&FilmViewer::panel_sized,     this, _1));
122         _outline_content->Bind  (wxEVT_CHECKBOX,          boost::bind (&FilmViewer::refresh_panel,   this));
123         _left_eye->Bind         (wxEVT_RADIOBUTTON,       boost::bind (&FilmViewer::refresh,         this));
124         _right_eye->Bind        (wxEVT_RADIOBUTTON,       boost::bind (&FilmViewer::refresh,         this));
125         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK, boost::bind (&FilmViewer::slider_moved,    this));
126         _slider->Bind           (wxEVT_SCROLL_PAGEUP,     boost::bind (&FilmViewer::slider_moved,    this));
127         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,   boost::bind (&FilmViewer::slider_moved,    this));
128         _play_button->Bind      (wxEVT_TOGGLEBUTTON,      boost::bind (&FilmViewer::play_clicked,    this));
129         _timer.Bind             (wxEVT_TIMER,             boost::bind (&FilmViewer::timer,           this));
130         _back_button->Bind      (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::back_clicked,    this, _1));
131         _forward_button->Bind   (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::forward_clicked, this, _1));
132         _frame_number->Bind     (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::frame_number_clicked, this));
133         _timecode->Bind         (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::timecode_clicked, this));
134         _jump_to_selected->Bind (wxEVT_CHECKBOX,          boost::bind (&FilmViewer::jump_to_selected_clicked, this));
135
136         _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
137
138         set_film (shared_ptr<Film> ());
139
140         JobManager::instance()->ActiveJobsChanged.connect (
141                 bind (&FilmViewer::active_jobs_changed, this, _2)
142                 );
143
144         setup_sensitivity ();
145 }
146
147 void
148 FilmViewer::set_film (shared_ptr<Film> film)
149 {
150         if (_film == film) {
151                 return;
152         }
153
154         _film = film;
155
156         _frame.reset ();
157
158         update_position_slider ();
159         update_position_label ();
160
161         if (!_film) {
162                 return;
163         }
164
165         try {
166                 _player.reset (new Player (_film, _film->playlist ()));
167                 _player->set_fast ();
168         } catch (bad_alloc) {
169                 error_dialog (this, _("There is not enough free memory to do that."));
170                 _film.reset ();
171                 return;
172         }
173
174         /* Always burn in subtitles, even if content is set not to, otherwise we won't see them
175            in the preview.
176         */
177         _player->set_always_burn_subtitles (true);
178         _player->set_ignore_audio ();
179         _player->set_play_referenced ();
180
181         _film_connection = _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
182
183         _player_connection = _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
184
185         calculate_sizes ();
186         refresh ();
187
188         setup_sensitivity ();
189 }
190
191 void
192 FilmViewer::refresh_panel ()
193 {
194         _panel->Refresh ();
195         _panel->Update ();
196 }
197
198 void
199 FilmViewer::get (DCPTime p, bool accurate)
200 {
201         if (!_player) {
202                 return;
203         }
204
205         list<shared_ptr<PlayerVideo> > all_pv;
206         try {
207                 all_pv = _player->get_video (p, accurate);
208         } catch (exception& e) {
209                 error_dialog (this, wxString::Format (_("Could not get video for view (%s)"), std_to_wx(e.what()).data()));
210         }
211
212         if (!all_pv.empty ()) {
213                 try {
214                         shared_ptr<PlayerVideo> pv;
215                         if (all_pv.size() == 2) {
216                                 /* We have 3D; choose the correct eye */
217                                 if (_left_eye->GetValue()) {
218                                         if (all_pv.front()->eyes() == EYES_LEFT) {
219                                                 pv = all_pv.front();
220                                         } else {
221                                                 pv = all_pv.back();
222                                         }
223                                 } else {
224                                         if (all_pv.front()->eyes() == EYES_RIGHT) {
225                                                 pv = all_pv.front();
226                                         } else {
227                                                 pv = all_pv.back();
228                                         }
229                                 }
230                         } else {
231                                 /* 2D; no choice to make */
232                                 pv = all_pv.front ();
233                         }
234
235                         /* In an ideal world, what we would do here is:
236                          *
237                          * 1. convert to XYZ exactly as we do in the DCP creation path.
238                          * 2. convert back to RGB for the preview display, compensating
239                          *    for the monitor etc. etc.
240                          *
241                          * but this is inefficient if the source is RGB.  Since we don't
242                          * (currently) care too much about the precise accuracy of the preview's
243                          * colour mapping (and we care more about its speed) we try to short-
244                          * circuit this "ideal" situation in some cases.
245                          *
246                          * The content's specified colour conversion indicates the colourspace
247                          * which the content is in (according to the user).
248                          *
249                          * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
250                          * image and convert it (from whatever the user has said it is) to RGB.
251                          */
252
253                         _frame = pv->image (
254                                 bind (&Log::dcp_log, _film->log().get(), _1, _2),
255                                 bind (&PlayerVideo::always_rgb, _1),
256                                 false, true
257                                 );
258
259                         ImageChanged (pv);
260
261                         _position = pv->time ();
262                         _inter_position = pv->inter_position ();
263                         _inter_size = pv->inter_size ();
264                 } catch (dcp::DCPReadError& e) {
265                         /* This can happen on the following sequence of events:
266                          * - load encrypted DCP
267                          * - add KDM
268                          * - DCP is examined again, which sets its "playable" flag to 1
269                          * - as a side effect of the exam, the viewer is updated using the old pieces
270                          * - the DCPDecoder in the old piece gives us an encrypted frame
271                          * - then, the pieces are re-made (but too late).
272                          *
273                          * I hope there's a better way to handle this ...
274                          */
275                         _frame.reset ();
276                         _position = p;
277                 }
278         } else {
279                 _frame.reset ();
280                 _position = p;
281         }
282
283         refresh_panel ();
284
285         _last_get_accurate = accurate;
286 }
287
288 void
289 FilmViewer::timer ()
290 {
291         DCPTime const frame = DCPTime::from_frames (1, _film->video_frame_rate ());
292
293         if ((_position + frame) >= _film->length ()) {
294                 _play_button->SetValue (false);
295                 check_play_state ();
296         } else {
297                 get (_position + frame, true);
298         }
299
300         update_position_label ();
301         update_position_slider ();
302 }
303
304 void
305 FilmViewer::paint_panel ()
306 {
307         wxPaintDC dc (_panel);
308
309         if (!_frame || !_film || !_out_size.width || !_out_size.height) {
310                 dc.Clear ();
311                 return;
312         }
313
314         wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
315         wxBitmap frame_bitmap (frame);
316         dc.DrawBitmap (frame_bitmap, 0, 0);
317
318         if (_out_size.width < _panel_size.width) {
319                 wxPen p (GetBackgroundColour ());
320                 wxBrush b (GetBackgroundColour ());
321                 dc.SetPen (p);
322                 dc.SetBrush (b);
323                 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
324         }
325
326         if (_out_size.height < _panel_size.height) {
327                 wxPen p (GetBackgroundColour ());
328                 wxBrush b (GetBackgroundColour ());
329                 dc.SetPen (p);
330                 dc.SetBrush (b);
331                 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
332         }
333
334         if (_outline_content->GetValue ()) {
335                 wxPen p (wxColour (255, 0, 0), 2);
336                 dc.SetPen (p);
337                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
338                 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
339         }
340 }
341
342 void
343 FilmViewer::slider_moved ()
344 {
345         if (!_film) {
346                 return;
347         }
348
349         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
350         /* Ensure that we hit the end of the film at the end of the slider */
351         if (t >= _film->length ()) {
352                 t = _film->length() - DCPTime::from_frames (1, _film->video_frame_rate ());
353         }
354         get (t, false);
355         update_position_label ();
356 }
357
358 void
359 FilmViewer::panel_sized (wxSizeEvent& ev)
360 {
361         _panel_size.width = ev.GetSize().GetWidth();
362         _panel_size.height = ev.GetSize().GetHeight();
363
364         calculate_sizes ();
365         refresh ();
366         update_position_label ();
367         update_position_slider ();
368 }
369
370 void
371 FilmViewer::calculate_sizes ()
372 {
373         if (!_film || !_player) {
374                 return;
375         }
376
377         Ratio const * container = _film->container ();
378
379         float const panel_ratio = _panel_size.ratio ();
380         float const film_ratio = container ? container->ratio () : 1.78;
381
382         if (panel_ratio < film_ratio) {
383                 /* panel is less widscreen than the film; clamp width */
384                 _out_size.width = _panel_size.width;
385                 _out_size.height = lrintf (_out_size.width / film_ratio);
386         } else {
387                 /* panel is more widescreen than the film; clamp height */
388                 _out_size.height = _panel_size.height;
389                 _out_size.width = lrintf (_out_size.height * film_ratio);
390         }
391
392         /* Catch silly values */
393         _out_size.width = max (64, _out_size.width);
394         _out_size.height = max (64, _out_size.height);
395
396         _player->set_video_container_size (_out_size);
397 }
398
399 void
400 FilmViewer::play_clicked ()
401 {
402         check_play_state ();
403 }
404
405 void
406 FilmViewer::check_play_state ()
407 {
408         if (!_film || _film->video_frame_rate() == 0) {
409                 return;
410         }
411
412         if (_play_button->GetValue()) {
413                 _timer.Start (1000 / _film->video_frame_rate());
414         } else {
415                 _timer.Stop ();
416         }
417 }
418
419 void
420 FilmViewer::update_position_slider ()
421 {
422         if (!_film) {
423                 _slider->SetValue (0);
424                 return;
425         }
426
427         DCPTime const len = _film->length ();
428
429         if (len.get ()) {
430                 int const new_slider_position = 4096 * _position.get() / len.get();
431                 if (new_slider_position != _slider->GetValue()) {
432                         _slider->SetValue (new_slider_position);
433                 }
434         }
435 }
436
437 void
438 FilmViewer::update_position_label ()
439 {
440         if (!_film) {
441                 _frame_number->SetLabel ("0");
442                 _timecode->SetLabel ("0:0:0.0");
443                 return;
444         }
445
446         double const fps = _film->video_frame_rate ();
447         /* Count frame number from 1 ... not sure if this is the best idea */
448         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_position.seconds() * fps) + 1));
449         _timecode->SetLabel (time_to_timecode (_position, fps));
450 }
451
452 void
453 FilmViewer::active_jobs_changed (optional<string> j)
454 {
455         /* examine content is the only job which stops the viewer working */
456         bool const a = !j || *j != "examine_content";
457         _slider->Enable (a);
458         _play_button->Enable (a);
459 }
460
461 DCPTime
462 FilmViewer::nudge_amount (wxMouseEvent& ev)
463 {
464         DCPTime amount = DCPTime::from_frames (1, _film->video_frame_rate ());
465
466         if (ev.ShiftDown() && !ev.ControlDown()) {
467                 amount = DCPTime::from_seconds (1);
468         } else if (!ev.ShiftDown() && ev.ControlDown()) {
469                 amount = DCPTime::from_seconds (10);
470         } else if (ev.ShiftDown() && ev.ControlDown()) {
471                 amount = DCPTime::from_seconds (60);
472         }
473
474         return amount;
475 }
476
477 void
478 FilmViewer::go_to (DCPTime t)
479 {
480         if (t < DCPTime ()) {
481                 t = DCPTime ();
482         }
483
484         if (t >= _film->length ()) {
485                 t = _film->length ();
486         }
487
488         get (t, true);
489         update_position_label ();
490         update_position_slider ();
491 }
492
493 void
494 FilmViewer::back_clicked (wxMouseEvent& ev)
495 {
496         go_to (_position - nudge_amount (ev));
497         ev.Skip ();
498 }
499
500 void
501 FilmViewer::forward_clicked (wxMouseEvent& ev)
502 {
503         go_to (_position + nudge_amount (ev));
504         ev.Skip ();
505 }
506
507 void
508 FilmViewer::player_changed (bool frequent)
509 {
510         if (frequent) {
511                 return;
512         }
513
514         if (_coalesce_player_changes) {
515                 _pending_player_change = true;
516                 return;
517         }
518
519         calculate_sizes ();
520         refresh ();
521         update_position_label ();
522         update_position_slider ();
523 }
524
525 void
526 FilmViewer::setup_sensitivity ()
527 {
528         bool const c = _film && !_film->content().empty ();
529
530         _slider->Enable (c);
531         _back_button->Enable (c);
532         _forward_button->Enable (c);
533         _play_button->Enable (c);
534         _outline_content->Enable (c);
535         _frame_number->Enable (c);
536         _timecode->Enable (c);
537
538         _left_eye->Enable (c && _film->three_d ());
539         _right_eye->Enable (c && _film->three_d ());
540 }
541
542 void
543 FilmViewer::film_changed (Film::Property p)
544 {
545         if (p == Film::CONTENT || p == Film::THREE_D) {
546                 setup_sensitivity ();
547         }
548 }
549
550 /** Re-get the current frame */
551 void
552 FilmViewer::refresh ()
553 {
554         get (_position, _last_get_accurate);
555 }
556
557 void
558 FilmViewer::set_position (DCPTime p)
559 {
560         _position = p;
561         get (_position, true);
562         update_position_label ();
563         update_position_slider ();
564 }
565
566 void
567 FilmViewer::set_coalesce_player_changes (bool c)
568 {
569         _coalesce_player_changes = c;
570
571         if (c) {
572                 _pending_player_change = false;
573         } else {
574                 if (_pending_player_change) {
575                         player_changed (false);
576                 }
577         }
578 }
579
580 void
581 FilmViewer::timecode_clicked ()
582 {
583         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
584         if (dialog->ShowModal() == wxID_OK) {
585                 go_to (dialog->get ());
586         }
587         dialog->Destroy ();
588 }
589
590 void
591 FilmViewer::frame_number_clicked ()
592 {
593         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
594         if (dialog->ShowModal() == wxID_OK) {
595                 go_to (dialog->get ());
596         }
597         dialog->Destroy ();
598 }
599
600 void
601 FilmViewer::jump_to_selected_clicked ()
602 {
603         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
604 }