2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file src/film_viewer.cc
22 * @brief A wx widget to view a preview of a Film.
25 #include "film_viewer.h"
26 #include "playhead_to_timecode_dialog.h"
27 #include "playhead_to_frame_dialog.h"
30 #include "lib/ratio.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"
44 #include "lib/config.h"
46 #include <libavutil/pixfmt.h>
48 #include <dcp/exceptions.h>
49 #include <wx/tglbtn.h>
62 using boost::shared_ptr;
63 using boost::dynamic_pointer_cast;
64 using boost::weak_ptr;
65 using boost::optional;
68 FilmViewer::FilmViewer (wxWindow* 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)
86 _panel->SetDoubleBuffered (true);
89 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
91 _v_sizer = new wxBoxSizer (wxVERTICAL);
94 _v_sizer->Add (_panel, 1, wxEXPAND);
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);
103 wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
105 wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
106 time_sizer->Add (_frame_number, 0, wxEXPAND);
107 time_sizer->Add (_timecode, 0, wxEXPAND);
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);
115 _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
117 _frame_number->SetMinSize (wxSize (84, -1));
118 _back_button->SetMinSize (wxSize (32, -1));
119 _forward_button->SetMinSize (wxSize (32, -1));
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));
137 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
139 set_film (shared_ptr<Film> ());
141 JobManager::instance()->ActiveJobsChanged.connect (
142 bind (&FilmViewer::active_jobs_changed, this, _2)
145 setup_sensitivity ();
149 FilmViewer::set_film (shared_ptr<Film> film)
159 update_position_slider ();
160 update_position_label ();
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."));
175 /* Always burn in subtitles, even if content is set not to, otherwise we won't see them
178 _player->set_always_burn_subtitles (true);
179 _player->set_ignore_audio ();
180 _player->set_play_referenced ();
182 _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
183 _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
190 setup_sensitivity ();
194 FilmViewer::recreate_butler ()
202 _butler.reset (new Butler (_film, _player));
206 FilmViewer::refresh_panel ()
215 pair<shared_ptr<PlayerVideo>, DCPTime> video;
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)));
224 /* In an ideal world, what we would do here is:
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.
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.
235 * The content's specified colour conversion indicates the colourspace
236 * which the content is in (according to the user).
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.
242 _frame = video.first->image (
243 bind (&Log::dcp_log, _film->log().get(), _1, _2),
244 bind (&PlayerVideo::always_rgb, _1),
248 ImageChanged (video.first);
250 _position = video.second;
251 _inter_position = video.first->inter_position ();
252 _inter_size = video.first->inter_size ();
260 DCPTime const frame = DCPTime::from_frames (1, _film->video_frame_rate ());
262 if ((_position + frame) >= _film->length ()) {
263 _play_button->SetValue (false);
269 update_position_label ();
270 update_position_slider ();
274 FilmViewer::paint_panel ()
276 wxPaintDC dc (_panel);
278 if (!_frame || !_film || !_out_size.width || !_out_size.height) {
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);
287 if (_out_size.width < _panel_size.width) {
288 wxPen p (GetBackgroundColour ());
289 wxBrush b (GetBackgroundColour ());
292 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
295 if (_out_size.height < _panel_size.height) {
296 wxPen p (GetBackgroundColour ());
297 wxBrush b (GetBackgroundColour ());
300 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
303 if (_outline_content->GetValue ()) {
304 wxPen p (wxColour (255, 0, 0), 2);
306 dc.SetBrush (*wxTRANSPARENT_BRUSH);
307 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
312 FilmViewer::slider_moved ()
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 ());
324 update_position_label ();
328 FilmViewer::panel_sized (wxSizeEvent& ev)
330 _panel_size.width = ev.GetSize().GetWidth();
331 _panel_size.height = ev.GetSize().GetHeight();
335 update_position_label ();
336 update_position_slider ();
340 FilmViewer::calculate_sizes ()
346 Ratio const * container = _film->container ();
348 float const panel_ratio = _panel_size.ratio ();
349 float const film_ratio = container ? container->ratio () : 1.78;
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);
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);
361 /* Catch silly values */
362 _out_size.width = max (64, _out_size.width);
363 _out_size.height = max (64, _out_size.height);
365 _player->set_video_container_size (_out_size);
369 FilmViewer::play_clicked ()
375 FilmViewer::check_play_state ()
377 if (!_film || _film->video_frame_rate() == 0) {
381 if (_play_button->GetValue()) {
382 _timer.Start (1000 / _film->video_frame_rate());
389 FilmViewer::update_position_slider ()
392 _slider->SetValue (0);
396 DCPTime const len = _film->length ();
399 int const new_slider_position = 4096 * _position.get() / len.get();
400 if (new_slider_position != _slider->GetValue()) {
401 _slider->SetValue (new_slider_position);
407 FilmViewer::update_position_label ()
410 _frame_number->SetLabel ("0");
411 _timecode->SetLabel ("0:0:0.0");
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));
422 FilmViewer::active_jobs_changed (optional<string> j)
424 /* examine content is the only job which stops the viewer working */
425 bool const a = !j || *j != "examine_content";
427 _play_button->Enable (a);
431 FilmViewer::nudge_amount (wxMouseEvent& ev)
433 DCPTime amount = DCPTime::from_frames (1, _film->video_frame_rate ());
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);
447 FilmViewer::go_to (DCPTime t)
449 if (t < DCPTime ()) {
453 if (t >= _film->length ()) {
454 t = _film->length ();
458 update_position_label ();
459 update_position_slider ();
463 FilmViewer::back_clicked (wxMouseEvent& ev)
465 go_to (_position - nudge_amount (ev));
470 FilmViewer::forward_clicked (wxMouseEvent& ev)
472 go_to (_position + nudge_amount (ev));
477 FilmViewer::player_changed (bool frequent)
483 if (_coalesce_player_changes) {
484 _pending_player_change = true;
490 update_position_label ();
491 update_position_slider ();
495 FilmViewer::setup_sensitivity ()
497 bool const c = _film && !_film->content().empty ();
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);
507 _left_eye->Enable (c && _film->three_d ());
508 _right_eye->Enable (c && _film->three_d ());
512 FilmViewer::film_changed (Film::Property p)
514 if (p == Film::CONTENT || p == Film::THREE_D) {
515 setup_sensitivity ();
519 /** Re-get the current frame */
521 FilmViewer::refresh ()
523 seek (_position, _last_seek_accurate);
527 FilmViewer::set_position (DCPTime p)
531 update_position_label ();
532 update_position_slider ();
536 FilmViewer::set_coalesce_player_changes (bool c)
538 _coalesce_player_changes = c;
541 _pending_player_change = false;
543 if (_pending_player_change) {
544 player_changed (false);
550 FilmViewer::timecode_clicked ()
552 PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
553 if (dialog->ShowModal() == wxID_OK) {
554 go_to (dialog->get ());
560 FilmViewer::frame_number_clicked ()
562 PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
563 if (dialog->ShowModal() == wxID_OK) {
564 go_to (dialog->get ());
570 FilmViewer::jump_to_selected_clicked ()
572 Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
576 FilmViewer::seek (DCPTime t, bool accurate)
582 _butler->seek (t, accurate);
583 _last_seek_accurate = accurate;