Seemingly basically working butler for video.
[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         cout << "get!\n";
216
217         pair<shared_ptr<PlayerVideo>, DCPTime> video;
218         do {
219                 video = _butler->get_video ();
220         } while (_film->three_d() && ((_left_eye->GetValue() && video.first->eyes() == EYES_RIGHT) || (_right_eye->GetValue() && video.first->eyes() == EYES_LEFT)));
221
222         if (!video.first) {
223                 return;
224         }
225
226         /* In an ideal world, what we would do here is:
227          *
228          * 1. convert to XYZ exactly as we do in the DCP creation path.
229          * 2. convert back to RGB for the preview display, compensating
230          *    for the monitor etc. etc.
231          *
232          * but this is inefficient if the source is RGB.  Since we don't
233          * (currently) care too much about the precise accuracy of the preview's
234          * colour mapping (and we care more about its speed) we try to short-
235          * circuit this "ideal" situation in some cases.
236          *
237          * The content's specified colour conversion indicates the colourspace
238          * which the content is in (according to the user).
239          *
240          * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
241          * image and convert it (from whatever the user has said it is) to RGB.
242          */
243
244         _frame = video.first->image (
245                 bind (&Log::dcp_log, _film->log().get(), _1, _2),
246                 bind (&PlayerVideo::always_rgb, _1),
247                 false, true
248                 );
249
250         ImageChanged (video.first);
251
252         _position = video.second;
253         _inter_position = video.first->inter_position ();
254         _inter_size = video.first->inter_size ();
255
256         refresh_panel ();
257 }
258
259 void
260 FilmViewer::timer ()
261 {
262         DCPTime const frame = DCPTime::from_frames (1, _film->video_frame_rate ());
263
264         if ((_position + frame) >= _film->length ()) {
265                 _play_button->SetValue (false);
266                 check_play_state ();
267         } else {
268                 get ();
269         }
270
271         update_position_label ();
272         update_position_slider ();
273 }
274
275 void
276 FilmViewer::paint_panel ()
277 {
278         wxPaintDC dc (_panel);
279
280         if (!_frame || !_film || !_out_size.width || !_out_size.height) {
281                 dc.Clear ();
282                 return;
283         }
284
285         wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
286         wxBitmap frame_bitmap (frame);
287         dc.DrawBitmap (frame_bitmap, 0, 0);
288
289         if (_out_size.width < _panel_size.width) {
290                 wxPen p (GetBackgroundColour ());
291                 wxBrush b (GetBackgroundColour ());
292                 dc.SetPen (p);
293                 dc.SetBrush (b);
294                 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
295         }
296
297         if (_out_size.height < _panel_size.height) {
298                 wxPen p (GetBackgroundColour ());
299                 wxBrush b (GetBackgroundColour ());
300                 dc.SetPen (p);
301                 dc.SetBrush (b);
302                 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
303         }
304
305         if (_outline_content->GetValue ()) {
306                 wxPen p (wxColour (255, 0, 0), 2);
307                 dc.SetPen (p);
308                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
309                 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
310         }
311 }
312
313 void
314 FilmViewer::slider_moved ()
315 {
316         if (!_film) {
317                 return;
318         }
319
320         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
321         /* Ensure that we hit the end of the film at the end of the slider */
322         if (t >= _film->length ()) {
323                 t = _film->length() - DCPTime::from_frames (1, _film->video_frame_rate ());
324         }
325         seek (t, false);
326         update_position_label ();
327 }
328
329 void
330 FilmViewer::panel_sized (wxSizeEvent& ev)
331 {
332         _panel_size.width = ev.GetSize().GetWidth();
333         _panel_size.height = ev.GetSize().GetHeight();
334
335         calculate_sizes ();
336         refresh ();
337         update_position_label ();
338         update_position_slider ();
339 }
340
341 void
342 FilmViewer::calculate_sizes ()
343 {
344         if (!_film) {
345                 return;
346         }
347
348         Ratio const * container = _film->container ();
349
350         float const panel_ratio = _panel_size.ratio ();
351         float const film_ratio = container ? container->ratio () : 1.78;
352
353         if (panel_ratio < film_ratio) {
354                 /* panel is less widscreen than the film; clamp width */
355                 _out_size.width = _panel_size.width;
356                 _out_size.height = lrintf (_out_size.width / film_ratio);
357         } else {
358                 /* panel is more widescreen than the film; clamp height */
359                 _out_size.height = _panel_size.height;
360                 _out_size.width = lrintf (_out_size.height * film_ratio);
361         }
362
363         /* Catch silly values */
364         _out_size.width = max (64, _out_size.width);
365         _out_size.height = max (64, _out_size.height);
366
367         _player->set_video_container_size (_out_size);
368 }
369
370 void
371 FilmViewer::play_clicked ()
372 {
373         check_play_state ();
374 }
375
376 void
377 FilmViewer::check_play_state ()
378 {
379         if (!_film || _film->video_frame_rate() == 0) {
380                 return;
381         }
382
383         if (_play_button->GetValue()) {
384                 _timer.Start (1000 / _film->video_frame_rate());
385         } else {
386                 _timer.Stop ();
387         }
388 }
389
390 void
391 FilmViewer::update_position_slider ()
392 {
393         if (!_film) {
394                 _slider->SetValue (0);
395                 return;
396         }
397
398         DCPTime const len = _film->length ();
399
400         if (len.get ()) {
401                 int const new_slider_position = 4096 * _position.get() / len.get();
402                 if (new_slider_position != _slider->GetValue()) {
403                         _slider->SetValue (new_slider_position);
404                 }
405         }
406 }
407
408 void
409 FilmViewer::update_position_label ()
410 {
411         if (!_film) {
412                 _frame_number->SetLabel ("0");
413                 _timecode->SetLabel ("0:0:0.0");
414                 return;
415         }
416
417         double const fps = _film->video_frame_rate ();
418         /* Count frame number from 1 ... not sure if this is the best idea */
419         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_position.seconds() * fps) + 1));
420         _timecode->SetLabel (time_to_timecode (_position, fps));
421 }
422
423 void
424 FilmViewer::active_jobs_changed (optional<string> j)
425 {
426         /* examine content is the only job which stops the viewer working */
427         bool const a = !j || *j != "examine_content";
428         _slider->Enable (a);
429         _play_button->Enable (a);
430 }
431
432 DCPTime
433 FilmViewer::nudge_amount (wxMouseEvent& ev)
434 {
435         DCPTime amount = DCPTime::from_frames (1, _film->video_frame_rate ());
436
437         if (ev.ShiftDown() && !ev.ControlDown()) {
438                 amount = DCPTime::from_seconds (1);
439         } else if (!ev.ShiftDown() && ev.ControlDown()) {
440                 amount = DCPTime::from_seconds (10);
441         } else if (ev.ShiftDown() && ev.ControlDown()) {
442                 amount = DCPTime::from_seconds (60);
443         }
444
445         return amount;
446 }
447
448 void
449 FilmViewer::go_to (DCPTime t)
450 {
451         if (t < DCPTime ()) {
452                 t = DCPTime ();
453         }
454
455         if (t >= _film->length ()) {
456                 t = _film->length ();
457         }
458
459         seek (t, true);
460         update_position_label ();
461         update_position_slider ();
462 }
463
464 void
465 FilmViewer::back_clicked (wxMouseEvent& ev)
466 {
467         go_to (_position - nudge_amount (ev));
468         ev.Skip ();
469 }
470
471 void
472 FilmViewer::forward_clicked (wxMouseEvent& ev)
473 {
474         go_to (_position + nudge_amount (ev));
475         ev.Skip ();
476 }
477
478 void
479 FilmViewer::player_changed (bool frequent)
480 {
481         if (frequent) {
482                 return;
483         }
484
485         if (_coalesce_player_changes) {
486                 _pending_player_change = true;
487                 return;
488         }
489
490         calculate_sizes ();
491         refresh ();
492         update_position_label ();
493         update_position_slider ();
494 }
495
496 void
497 FilmViewer::setup_sensitivity ()
498 {
499         bool const c = _film && !_film->content().empty ();
500
501         _slider->Enable (c);
502         _back_button->Enable (c);
503         _forward_button->Enable (c);
504         _play_button->Enable (c);
505         _outline_content->Enable (c);
506         _frame_number->Enable (c);
507         _timecode->Enable (c);
508
509         _left_eye->Enable (c && _film->three_d ());
510         _right_eye->Enable (c && _film->three_d ());
511 }
512
513 void
514 FilmViewer::film_changed (Film::Property p)
515 {
516         if (p == Film::CONTENT || p == Film::THREE_D) {
517                 setup_sensitivity ();
518         }
519 }
520
521 /** Re-get the current frame */
522 void
523 FilmViewer::refresh ()
524 {
525         seek (_position, _last_seek_accurate);
526 }
527
528 void
529 FilmViewer::set_position (DCPTime p)
530 {
531         _position = p;
532         seek (p, true);
533         update_position_label ();
534         update_position_slider ();
535 }
536
537 void
538 FilmViewer::set_coalesce_player_changes (bool c)
539 {
540         _coalesce_player_changes = c;
541
542         if (c) {
543                 _pending_player_change = false;
544         } else {
545                 if (_pending_player_change) {
546                         player_changed (false);
547                 }
548         }
549 }
550
551 void
552 FilmViewer::timecode_clicked ()
553 {
554         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
555         if (dialog->ShowModal() == wxID_OK) {
556                 go_to (dialog->get ());
557         }
558         dialog->Destroy ();
559 }
560
561 void
562 FilmViewer::frame_number_clicked ()
563 {
564         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
565         if (dialog->ShowModal() == wxID_OK) {
566                 go_to (dialog->get ());
567         }
568         dialog->Destroy ();
569 }
570
571 void
572 FilmViewer::jump_to_selected_clicked ()
573 {
574         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
575 }
576
577 void
578 FilmViewer::seek (DCPTime t, bool accurate)
579 {
580         if (!_butler) {
581                 return;
582         }
583
584         _butler->seek (t, accurate);
585         _last_seek_accurate = accurate;
586         get ();
587 }