Optimise image scaling for the preview.
[dcpomatic.git] / src / wx / film_viewer.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 /** @file  src/film_viewer.cc
21  *  @brief A wx widget to view a preview of a Film.
22  */
23
24 #include "lib/film.h"
25 #include "lib/ratio.h"
26 #include "lib/util.h"
27 #include "lib/job_manager.h"
28 #include "lib/image.h"
29 #include "lib/exceptions.h"
30 #include "lib/examine_content_job.h"
31 #include "lib/filter.h"
32 #include "lib/player.h"
33 #include "lib/player_video.h"
34 #include "lib/video_content.h"
35 #include "lib/video_decoder.h"
36 #include "lib/timer.h"
37 #include "lib/log.h"
38 #include "film_viewer.h"
39 #include "wx_util.h"
40 extern "C" {
41 #include <libavutil/pixfmt.h>
42 }
43 #include <dcp/exceptions.h>
44 #include <wx/tglbtn.h>
45 #include <iostream>
46 #include <iomanip>
47
48 using std::string;
49 using std::pair;
50 using std::min;
51 using std::max;
52 using std::cout;
53 using std::list;
54 using std::bad_alloc;
55 using std::make_pair;
56 using std::exception;
57 using boost::shared_ptr;
58 using boost::dynamic_pointer_cast;
59 using boost::weak_ptr;
60 using boost::optional;
61 using dcp::Size;
62
63 FilmViewer::FilmViewer (wxWindow* p)
64         : wxPanel (p)
65         , _panel (new wxPanel (this))
66         , _outline_content (new wxCheckBox (this, wxID_ANY, _("Outline content")))
67         , _left_eye (new wxRadioButton (this, wxID_ANY, _("Left eye"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP))
68         , _right_eye (new wxRadioButton (this, wxID_ANY, _("Right eye")))
69         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
70         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
71         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
72         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
73         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
74         , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
75         , _coalesce_player_changes (false)
76         , _pending_player_change (false)
77         , _last_get_accurate (true)
78 {
79 #ifndef __WXOSX__
80         _panel->SetDoubleBuffered (true);
81 #endif
82
83         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
84
85         _v_sizer = new wxBoxSizer (wxVERTICAL);
86         SetSizer (_v_sizer);
87
88         _v_sizer->Add (_panel, 1, wxEXPAND);
89
90         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
91         view_options->Add (_outline_content, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
92         view_options->Add (_left_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
93         view_options->Add (_right_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
94         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
95
96         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
97
98         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
99         time_sizer->Add (_frame_number, 0, wxEXPAND);
100         time_sizer->Add (_timecode, 0, wxEXPAND);
101
102         h_sizer->Add (_back_button, 0, wxALL, 2);
103         h_sizer->Add (time_sizer, 0, wxEXPAND);
104         h_sizer->Add (_forward_button, 0, wxALL, 2);
105         h_sizer->Add (_play_button, 0, wxEXPAND);
106         h_sizer->Add (_slider, 1, wxEXPAND);
107
108         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
109
110         _frame_number->SetMinSize (wxSize (84, -1));
111         _back_button->SetMinSize (wxSize (32, -1));
112         _forward_button->SetMinSize (wxSize (32, -1));
113
114         _panel->Bind          (wxEVT_PAINT,                        boost::bind (&FilmViewer::paint_panel,     this));
115         _panel->Bind          (wxEVT_SIZE,                         boost::bind (&FilmViewer::panel_sized,     this, _1));
116         _outline_content->Bind(wxEVT_COMMAND_CHECKBOX_CLICKED,     boost::bind (&FilmViewer::refresh_panel,   this));
117         _left_eye->Bind       (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&FilmViewer::refresh,         this));
118         _right_eye->Bind      (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&FilmViewer::refresh,         this));
119         _slider->Bind         (wxEVT_SCROLL_THUMBTRACK,            boost::bind (&FilmViewer::slider_moved,    this));
120         _slider->Bind         (wxEVT_SCROLL_PAGEUP,                boost::bind (&FilmViewer::slider_moved,    this));
121         _slider->Bind         (wxEVT_SCROLL_PAGEDOWN,              boost::bind (&FilmViewer::slider_moved,    this));
122         _play_button->Bind    (wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, boost::bind (&FilmViewer::play_clicked,    this));
123         _timer.Bind           (wxEVT_TIMER,                        boost::bind (&FilmViewer::timer,           this));
124         _back_button->Bind    (wxEVT_COMMAND_BUTTON_CLICKED,       boost::bind (&FilmViewer::back_clicked,    this));
125         _forward_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,       boost::bind (&FilmViewer::forward_clicked, this));
126
127         set_film (shared_ptr<Film> ());
128
129         JobManager::instance()->ActiveJobsChanged.connect (
130                 bind (&FilmViewer::active_jobs_changed, this, _2)
131                 );
132
133         setup_sensitivity ();
134 }
135
136 void
137 FilmViewer::set_film (shared_ptr<Film> film)
138 {
139         if (_film == film) {
140                 return;
141         }
142
143         _film = film;
144
145         _frame.reset ();
146
147         update_position_slider ();
148         update_position_label ();
149
150         if (!_film) {
151                 return;
152         }
153
154         try {
155                 _player.reset (new Player (_film, _film->playlist ()));
156                 _player->set_fast ();
157         } catch (bad_alloc) {
158                 error_dialog (this, _("There is not enough free memory to do that."));
159                 _film.reset ();
160                 return;
161         }
162
163         /* Always burn in subtitles, even if content is set not to, otherwise we won't see them
164            in the preview.
165         */
166         _player->set_always_burn_subtitles (true);
167         _player->set_ignore_audio ();
168         _player->set_play_referenced ();
169
170         _film_connection = _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
171
172         _player_connection = _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
173
174         calculate_sizes ();
175         refresh ();
176
177         setup_sensitivity ();
178 }
179
180 void
181 FilmViewer::refresh_panel ()
182 {
183         _panel->Refresh ();
184         _panel->Update ();
185 }
186
187 void
188 FilmViewer::get (DCPTime p, bool accurate)
189 {
190         if (!_player) {
191                 return;
192         }
193
194         list<shared_ptr<PlayerVideo> > all_pv;
195         try {
196                 all_pv = _player->get_video (p, accurate);
197         } catch (exception& e) {
198                 error_dialog (this, wxString::Format (_("Could not get video for view (%s)"), std_to_wx(e.what()).data()));
199         }
200
201         if (!all_pv.empty ()) {
202                 try {
203                         shared_ptr<PlayerVideo> pv;
204                         if (all_pv.size() == 2) {
205                                 /* We have 3D; choose the correct eye */
206                                 if (_left_eye->GetValue()) {
207                                         if (all_pv.front()->eyes() == EYES_LEFT) {
208                                                 pv = all_pv.front();
209                                         } else {
210                                                 pv = all_pv.back();
211                                         }
212                                 } else {
213                                         if (all_pv.front()->eyes() == EYES_RIGHT) {
214                                                 pv = all_pv.front();
215                                         } else {
216                                                 pv = all_pv.back();
217                                         }
218                                 }
219                         } else {
220                                 /* 2D; no choice to make */
221                                 pv = all_pv.front ();
222                         }
223
224                         _frame = pv->image (
225                                 bind (&Log::dcp_log, _film->log().get(), _1, _2), bind (&PlayerVideo::always_rgb, _1), false, true
226                                 );
227
228                         ImageChanged (pv);
229
230                         _position = pv->time ();
231                         _inter_position = pv->inter_position ();
232                         _inter_size = pv->inter_size ();
233                 } catch (dcp::DCPReadError& e) {
234                         /* This can happen on the following sequence of events:
235                          * - load encrypted DCP
236                          * - add KDM
237                          * - DCP is examined again, which sets its "playable" flag to 1
238                          * - as a side effect of the exam, the viewer is updated using the old pieces
239                          * - the DCPDecoder in the old piece gives us an encrypted frame
240                          * - then, the pieces are re-made (but too late).
241                          *
242                          * I hope there's a better way to handle this ...
243                          */
244                         _frame.reset ();
245                         _position = p;
246                 }
247         } else {
248                 _frame.reset ();
249                 _position = p;
250         }
251
252         refresh_panel ();
253
254         _last_get_accurate = accurate;
255 }
256
257 void
258 FilmViewer::timer ()
259 {
260         DCPTime const frame = DCPTime::from_frames (1, _film->video_frame_rate ());
261
262         if ((_position + frame) >= _film->length ()) {
263                 _play_button->SetValue (false);
264                 check_play_state ();
265         } else {
266                 get (_position + frame, true);
267         }
268
269         update_position_label ();
270         update_position_slider ();
271 }
272
273 void
274 FilmViewer::paint_panel ()
275 {
276         wxPaintDC dc (_panel);
277
278         if (!_frame || !_film || !_out_size.width || !_out_size.height) {
279                 dc.Clear ();
280                 return;
281         }
282
283         wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
284         wxBitmap frame_bitmap (frame);
285         dc.DrawBitmap (frame_bitmap, 0, 0);
286
287         if (_out_size.width < _panel_size.width) {
288                 wxPen p (GetBackgroundColour ());
289                 wxBrush b (GetBackgroundColour ());
290                 dc.SetPen (p);
291                 dc.SetBrush (b);
292                 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
293         }
294
295         if (_out_size.height < _panel_size.height) {
296                 wxPen p (GetBackgroundColour ());
297                 wxBrush b (GetBackgroundColour ());
298                 dc.SetPen (p);
299                 dc.SetBrush (b);
300                 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
301         }
302
303         if (_outline_content->GetValue ()) {
304                 wxPen p (wxColour (255, 0, 0), 2);
305                 dc.SetPen (p);
306                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
307                 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
308         }
309 }
310
311 void
312 FilmViewer::slider_moved ()
313 {
314         if (!_film) {
315                 return;
316         }
317
318         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
319         /* Ensure that we hit the end of the film at the end of the slider */
320         if (t >= _film->length ()) {
321                 t = _film->length() - DCPTime::from_frames (1, _film->video_frame_rate ());
322         }
323         get (t, false);
324         update_position_label ();
325 }
326
327 void
328 FilmViewer::panel_sized (wxSizeEvent& ev)
329 {
330         _panel_size.width = ev.GetSize().GetWidth();
331         _panel_size.height = ev.GetSize().GetHeight();
332
333         calculate_sizes ();
334         refresh ();
335         update_position_label ();
336         update_position_slider ();
337 }
338
339 void
340 FilmViewer::calculate_sizes ()
341 {
342         if (!_film || !_player) {
343                 return;
344         }
345
346         Ratio const * container = _film->container ();
347
348         float const panel_ratio = _panel_size.ratio ();
349         float const film_ratio = container ? container->ratio () : 1.78;
350
351         if (panel_ratio < film_ratio) {
352                 /* panel is less widscreen than the film; clamp width */
353                 _out_size.width = _panel_size.width;
354                 _out_size.height = lrintf (_out_size.width / film_ratio);
355         } else {
356                 /* panel is more widescreen than the film; clamp height */
357                 _out_size.height = _panel_size.height;
358                 _out_size.width = lrintf (_out_size.height * film_ratio);
359         }
360
361         /* Catch silly values */
362         _out_size.width = max (64, _out_size.width);
363         _out_size.height = max (64, _out_size.height);
364
365         _player->set_video_container_size (_out_size);
366 }
367
368 void
369 FilmViewer::play_clicked ()
370 {
371         check_play_state ();
372 }
373
374 void
375 FilmViewer::check_play_state ()
376 {
377         if (!_film || _film->video_frame_rate() == 0) {
378                 return;
379         }
380
381         if (_play_button->GetValue()) {
382                 _timer.Start (1000 / _film->video_frame_rate());
383         } else {
384                 _timer.Stop ();
385         }
386 }
387
388 void
389 FilmViewer::update_position_slider ()
390 {
391         if (!_film) {
392                 _slider->SetValue (0);
393                 return;
394         }
395
396         DCPTime const len = _film->length ();
397
398         if (len.get ()) {
399                 int const new_slider_position = 4096 * _position.get() / len.get();
400                 if (new_slider_position != _slider->GetValue()) {
401                         _slider->SetValue (new_slider_position);
402                 }
403         }
404 }
405
406 void
407 FilmViewer::update_position_label ()
408 {
409         if (!_film) {
410                 _frame_number->SetLabel ("0");
411                 _timecode->SetLabel ("0:0:0.0");
412                 return;
413         }
414
415         double const fps = _film->video_frame_rate ();
416         /* Count frame number from 1 ... not sure if this is the best idea */
417         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_position.seconds() * fps) + 1));
418         _timecode->SetLabel (time_to_timecode (_position, fps));
419 }
420
421 void
422 FilmViewer::active_jobs_changed (optional<string> j)
423 {
424         /* examine content is the only job which stops the viewer working */
425         bool const a = !j || *j != "examine_content";
426         _slider->Enable (a);
427         _play_button->Enable (a);
428 }
429
430 void
431 FilmViewer::back_clicked ()
432 {
433         DCPTime p = _position - DCPTime::from_frames (1, _film->video_frame_rate ());
434         if (p < DCPTime ()) {
435                 p = DCPTime ();
436         }
437
438         get (p, true);
439         update_position_label ();
440         update_position_slider ();
441 }
442
443 void
444 FilmViewer::forward_clicked ()
445 {
446         DCPTime p = _position + DCPTime::from_frames (1, _film->video_frame_rate ());
447         if (p >= _film->length ()) {
448                 p = _position;
449         }
450
451         get (p, true);
452         update_position_label ();
453         update_position_slider ();
454 }
455
456 void
457 FilmViewer::player_changed (bool frequent)
458 {
459         if (frequent) {
460                 return;
461         }
462
463         if (_coalesce_player_changes) {
464                 _pending_player_change = true;
465                 return;
466         }
467
468         calculate_sizes ();
469         refresh ();
470         update_position_label ();
471         update_position_slider ();
472 }
473
474 void
475 FilmViewer::setup_sensitivity ()
476 {
477         bool const c = _film && !_film->content().empty ();
478
479         _slider->Enable (c);
480         _back_button->Enable (c);
481         _forward_button->Enable (c);
482         _play_button->Enable (c);
483         _outline_content->Enable (c);
484         _frame_number->Enable (c);
485         _timecode->Enable (c);
486
487         _left_eye->Enable (c && _film->three_d ());
488         _right_eye->Enable (c && _film->three_d ());
489 }
490
491 void
492 FilmViewer::film_changed (Film::Property p)
493 {
494         if (p == Film::CONTENT || p == Film::THREE_D) {
495                 setup_sensitivity ();
496         }
497 }
498
499 /** Re-get the current frame */
500 void
501 FilmViewer::refresh ()
502 {
503         get (_position, _last_get_accurate);
504 }
505
506 void
507 FilmViewer::set_position (DCPTime p)
508 {
509         _position = p;
510         get (_position, true);
511         update_position_label ();
512         update_position_slider ();
513 }
514
515 void
516 FilmViewer::set_coalesce_player_changes (bool c)
517 {
518         _coalesce_player_changes = c;
519
520         if (c) {
521                 _pending_player_change = false;
522         } else {
523                 if (_pending_player_change) {
524                         player_changed (false);
525                 }
526         }
527 }