Initial butler work.
[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/butler.h"
43 #include "lib/log.h"
44 #include "lib/config.h"
45 extern "C" {
46 #include <libavutil/pixfmt.h>
47 }
48 #include <dcp/exceptions.h>
49 #include <wx/tglbtn.h>
50 #include <iostream>
51 #include <iomanip>
52
53 using std::string;
54 using std::pair;
55 using std::min;
56 using std::max;
57 using std::cout;
58 using std::list;
59 using std::bad_alloc;
60 using std::make_pair;
61 using std::exception;
62 using boost::shared_ptr;
63 using boost::dynamic_pointer_cast;
64 using boost::weak_ptr;
65 using boost::optional;
66 using dcp::Size;
67
68 FilmViewer::FilmViewer (wxWindow* p)
69         : wxPanel (p)
70         , _panel (new wxPanel (this))
71         , _outline_content (new wxCheckBox (this, wxID_ANY, _("Outline content")))
72         , _left_eye (new wxRadioButton (this, wxID_ANY, _("Left eye"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP))
73         , _right_eye (new wxRadioButton (this, wxID_ANY, _("Right eye")))
74         , _jump_to_selected (new wxCheckBox (this, wxID_ANY, _("Jump to selected content")))
75         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
76         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
77         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
78         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
79         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
80         , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
81         , _coalesce_player_changes (false)
82         , _pending_player_change (false)
83         , _last_seek_accurate (true)
84 {
85 #ifndef __WXOSX__
86         _panel->SetDoubleBuffered (true);
87 #endif
88
89         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
90
91         _v_sizer = new wxBoxSizer (wxVERTICAL);
92         SetSizer (_v_sizer);
93
94         _v_sizer->Add (_panel, 1, wxEXPAND);
95
96         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
97         view_options->Add (_outline_content, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
98         view_options->Add (_left_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
99         view_options->Add (_right_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
100         view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
101         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
102
103         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
104
105         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
106         time_sizer->Add (_frame_number, 0, wxEXPAND);
107         time_sizer->Add (_timecode, 0, wxEXPAND);
108
109         h_sizer->Add (_back_button, 0, wxALL, 2);
110         h_sizer->Add (time_sizer, 0, wxEXPAND);
111         h_sizer->Add (_forward_button, 0, wxALL, 2);
112         h_sizer->Add (_play_button, 0, wxEXPAND);
113         h_sizer->Add (_slider, 1, wxEXPAND);
114
115         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
116
117         _frame_number->SetMinSize (wxSize (84, -1));
118         _back_button->SetMinSize (wxSize (32, -1));
119         _forward_button->SetMinSize (wxSize (32, -1));
120
121         _panel->Bind            (wxEVT_PAINT,             boost::bind (&FilmViewer::paint_panel,     this));
122         _panel->Bind            (wxEVT_SIZE,              boost::bind (&FilmViewer::panel_sized,     this, _1));
123         _outline_content->Bind  (wxEVT_CHECKBOX,          boost::bind (&FilmViewer::refresh_panel,   this));
124         _left_eye->Bind         (wxEVT_RADIOBUTTON,       boost::bind (&FilmViewer::refresh,         this));
125         _right_eye->Bind        (wxEVT_RADIOBUTTON,       boost::bind (&FilmViewer::refresh,         this));
126         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK, boost::bind (&FilmViewer::slider_moved,    this));
127         _slider->Bind           (wxEVT_SCROLL_PAGEUP,     boost::bind (&FilmViewer::slider_moved,    this));
128         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,   boost::bind (&FilmViewer::slider_moved,    this));
129         _play_button->Bind      (wxEVT_TOGGLEBUTTON,      boost::bind (&FilmViewer::play_clicked,    this));
130         _timer.Bind             (wxEVT_TIMER,             boost::bind (&FilmViewer::timer,           this));
131         _back_button->Bind      (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::back_clicked,    this, _1));
132         _forward_button->Bind   (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::forward_clicked, this, _1));
133         _frame_number->Bind     (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::frame_number_clicked, this));
134         _timecode->Bind         (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::timecode_clicked, this));
135         _jump_to_selected->Bind (wxEVT_CHECKBOX,          boost::bind (&FilmViewer::jump_to_selected_clicked, this));
136
137         _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
138
139         set_film (shared_ptr<Film> ());
140
141         JobManager::instance()->ActiveJobsChanged.connect (
142                 bind (&FilmViewer::active_jobs_changed, this, _2)
143                 );
144
145         setup_sensitivity ();
146 }
147
148 void
149 FilmViewer::set_film (shared_ptr<Film> film)
150 {
151         if (_film == film) {
152                 return;
153         }
154
155         _film = film;
156
157         _frame.reset ();
158
159         update_position_slider ();
160         update_position_label ();
161
162         if (!_film) {
163                 return;
164         }
165
166         try {
167                 _player.reset (new Player (_film, _film->playlist ()));
168                 _player->set_fast ();
169         } catch (bad_alloc) {
170                 error_dialog (this, _("There is not enough free memory to do that."));
171                 _film.reset ();
172                 return;
173         }
174
175         /* Always burn in subtitles, even if content is set not to, otherwise we won't see them
176            in the preview.
177         */
178         _player->set_always_burn_subtitles (true);
179         _player->set_ignore_audio ();
180         _player->set_play_referenced ();
181
182         _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
183         _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
184
185         recreate_butler ();
186
187         calculate_sizes ();
188         refresh ();
189
190         setup_sensitivity ();
191 }
192
193 void
194 FilmViewer::recreate_butler ()
195 {
196         _butler.reset ();
197
198         if (!_film) {
199                 return;
200         }
201
202         _butler.reset (new Butler (_film, _player));
203 }
204
205 void
206 FilmViewer::refresh_panel ()
207 {
208         _panel->Refresh ();
209         _panel->Update ();
210 }
211
212 void
213 FilmViewer::get ()
214 {
215         pair<shared_ptr<PlayerVideo>, DCPTime> video;
216         do {
217                 video = _butler->get_video ();
218         } while (_film->three_d() && ((_left_eye->GetValue() && video.first->eyes() == EYES_RIGHT) || (_right_eye->GetValue() && video.first->eyes() == EYES_LEFT)));
219
220         if (!video.first) {
221                 return;
222         }
223
224         /* In an ideal world, what we would do here is:
225          *
226          * 1. convert to XYZ exactly as we do in the DCP creation path.
227          * 2. convert back to RGB for the preview display, compensating
228          *    for the monitor etc. etc.
229          *
230          * but this is inefficient if the source is RGB.  Since we don't
231          * (currently) care too much about the precise accuracy of the preview's
232          * colour mapping (and we care more about its speed) we try to short-
233          * circuit this "ideal" situation in some cases.
234          *
235          * The content's specified colour conversion indicates the colourspace
236          * which the content is in (according to the user).
237          *
238          * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
239          * image and convert it (from whatever the user has said it is) to RGB.
240          */
241
242         _frame = video.first->image (
243                 bind (&Log::dcp_log, _film->log().get(), _1, _2),
244                 bind (&PlayerVideo::always_rgb, _1),
245                 false, true
246                 );
247
248         ImageChanged (video.first);
249
250         _position = video.second;
251         _inter_position = video.first->inter_position ();
252         _inter_size = video.first->inter_size ();
253
254         refresh_panel ();
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 ();
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         seek (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) {
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 DCPTime
431 FilmViewer::nudge_amount (wxMouseEvent& ev)
432 {
433         DCPTime amount = DCPTime::from_frames (1, _film->video_frame_rate ());
434
435         if (ev.ShiftDown() && !ev.ControlDown()) {
436                 amount = DCPTime::from_seconds (1);
437         } else if (!ev.ShiftDown() && ev.ControlDown()) {
438                 amount = DCPTime::from_seconds (10);
439         } else if (ev.ShiftDown() && ev.ControlDown()) {
440                 amount = DCPTime::from_seconds (60);
441         }
442
443         return amount;
444 }
445
446 void
447 FilmViewer::go_to (DCPTime t)
448 {
449         if (t < DCPTime ()) {
450                 t = DCPTime ();
451         }
452
453         if (t >= _film->length ()) {
454                 t = _film->length ();
455         }
456
457         seek (t, true);
458         update_position_label ();
459         update_position_slider ();
460 }
461
462 void
463 FilmViewer::back_clicked (wxMouseEvent& ev)
464 {
465         go_to (_position - nudge_amount (ev));
466         ev.Skip ();
467 }
468
469 void
470 FilmViewer::forward_clicked (wxMouseEvent& ev)
471 {
472         go_to (_position + nudge_amount (ev));
473         ev.Skip ();
474 }
475
476 void
477 FilmViewer::player_changed (bool frequent)
478 {
479         if (frequent) {
480                 return;
481         }
482
483         if (_coalesce_player_changes) {
484                 _pending_player_change = true;
485                 return;
486         }
487
488         calculate_sizes ();
489         refresh ();
490         update_position_label ();
491         update_position_slider ();
492 }
493
494 void
495 FilmViewer::setup_sensitivity ()
496 {
497         bool const c = _film && !_film->content().empty ();
498
499         _slider->Enable (c);
500         _back_button->Enable (c);
501         _forward_button->Enable (c);
502         _play_button->Enable (c);
503         _outline_content->Enable (c);
504         _frame_number->Enable (c);
505         _timecode->Enable (c);
506
507         _left_eye->Enable (c && _film->three_d ());
508         _right_eye->Enable (c && _film->three_d ());
509 }
510
511 void
512 FilmViewer::film_changed (Film::Property p)
513 {
514         if (p == Film::CONTENT || p == Film::THREE_D) {
515                 setup_sensitivity ();
516         }
517 }
518
519 /** Re-get the current frame */
520 void
521 FilmViewer::refresh ()
522 {
523         seek (_position, _last_seek_accurate);
524 }
525
526 void
527 FilmViewer::set_position (DCPTime p)
528 {
529         _position = p;
530         seek (p, true);
531         update_position_label ();
532         update_position_slider ();
533 }
534
535 void
536 FilmViewer::set_coalesce_player_changes (bool c)
537 {
538         _coalesce_player_changes = c;
539
540         if (c) {
541                 _pending_player_change = false;
542         } else {
543                 if (_pending_player_change) {
544                         player_changed (false);
545                 }
546         }
547 }
548
549 void
550 FilmViewer::timecode_clicked ()
551 {
552         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
553         if (dialog->ShowModal() == wxID_OK) {
554                 go_to (dialog->get ());
555         }
556         dialog->Destroy ();
557 }
558
559 void
560 FilmViewer::frame_number_clicked ()
561 {
562         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
563         if (dialog->ShowModal() == wxID_OK) {
564                 go_to (dialog->get ());
565         }
566         dialog->Destroy ();
567 }
568
569 void
570 FilmViewer::jump_to_selected_clicked ()
571 {
572         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
573 }
574
575 void
576 FilmViewer::seek (DCPTime t, bool accurate)
577 {
578         if (!_butler) {
579                 return;
580         }
581
582         _butler->seek (t, accurate);
583         _last_seek_accurate = accurate;
584         get ();
585 }