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 ()
217 pair<shared_ptr<PlayerVideo>, DCPTime> video;
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)));
226 /* In an ideal world, what we would do here is:
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.
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.
237 * The content's specified colour conversion indicates the colourspace
238 * which the content is in (according to the user).
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.
244 _frame = video.first->image (
245 bind (&Log::dcp_log, _film->log().get(), _1, _2),
246 bind (&PlayerVideo::always_rgb, _1),
250 ImageChanged (video.first);
252 _position = video.second;
253 _inter_position = video.first->inter_position ();
254 _inter_size = video.first->inter_size ();
262 DCPTime const frame = DCPTime::from_frames (1, _film->video_frame_rate ());
264 if ((_position + frame) >= _film->length ()) {
265 _play_button->SetValue (false);
271 update_position_label ();
272 update_position_slider ();
276 FilmViewer::paint_panel ()
278 wxPaintDC dc (_panel);
280 if (!_frame || !_film || !_out_size.width || !_out_size.height) {
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);
289 if (_out_size.width < _panel_size.width) {
290 wxPen p (GetBackgroundColour ());
291 wxBrush b (GetBackgroundColour ());
294 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
297 if (_out_size.height < _panel_size.height) {
298 wxPen p (GetBackgroundColour ());
299 wxBrush b (GetBackgroundColour ());
302 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
305 if (_outline_content->GetValue ()) {
306 wxPen p (wxColour (255, 0, 0), 2);
308 dc.SetBrush (*wxTRANSPARENT_BRUSH);
309 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
314 FilmViewer::slider_moved ()
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 ());
326 update_position_label ();
330 FilmViewer::panel_sized (wxSizeEvent& ev)
332 _panel_size.width = ev.GetSize().GetWidth();
333 _panel_size.height = ev.GetSize().GetHeight();
337 update_position_label ();
338 update_position_slider ();
342 FilmViewer::calculate_sizes ()
348 Ratio const * container = _film->container ();
350 float const panel_ratio = _panel_size.ratio ();
351 float const film_ratio = container ? container->ratio () : 1.78;
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);
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);
363 /* Catch silly values */
364 _out_size.width = max (64, _out_size.width);
365 _out_size.height = max (64, _out_size.height);
367 _player->set_video_container_size (_out_size);
371 FilmViewer::play_clicked ()
377 FilmViewer::check_play_state ()
379 if (!_film || _film->video_frame_rate() == 0) {
383 if (_play_button->GetValue()) {
384 _timer.Start (1000 / _film->video_frame_rate());
391 FilmViewer::update_position_slider ()
394 _slider->SetValue (0);
398 DCPTime const len = _film->length ();
401 int const new_slider_position = 4096 * _position.get() / len.get();
402 if (new_slider_position != _slider->GetValue()) {
403 _slider->SetValue (new_slider_position);
409 FilmViewer::update_position_label ()
412 _frame_number->SetLabel ("0");
413 _timecode->SetLabel ("0:0:0.0");
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));
424 FilmViewer::active_jobs_changed (optional<string> j)
426 /* examine content is the only job which stops the viewer working */
427 bool const a = !j || *j != "examine_content";
429 _play_button->Enable (a);
433 FilmViewer::nudge_amount (wxMouseEvent& ev)
435 DCPTime amount = DCPTime::from_frames (1, _film->video_frame_rate ());
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);
449 FilmViewer::go_to (DCPTime t)
451 if (t < DCPTime ()) {
455 if (t >= _film->length ()) {
456 t = _film->length ();
460 update_position_label ();
461 update_position_slider ();
465 FilmViewer::back_clicked (wxMouseEvent& ev)
467 go_to (_position - nudge_amount (ev));
472 FilmViewer::forward_clicked (wxMouseEvent& ev)
474 go_to (_position + nudge_amount (ev));
479 FilmViewer::player_changed (bool frequent)
485 if (_coalesce_player_changes) {
486 _pending_player_change = true;
492 update_position_label ();
493 update_position_slider ();
497 FilmViewer::setup_sensitivity ()
499 bool const c = _film && !_film->content().empty ();
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);
509 _left_eye->Enable (c && _film->three_d ());
510 _right_eye->Enable (c && _film->three_d ());
514 FilmViewer::film_changed (Film::Property p)
516 if (p == Film::CONTENT || p == Film::THREE_D) {
517 setup_sensitivity ();
521 /** Re-get the current frame */
523 FilmViewer::refresh ()
525 seek (_position, _last_seek_accurate);
529 FilmViewer::set_position (DCPTime p)
533 update_position_label ();
534 update_position_slider ();
538 FilmViewer::set_coalesce_player_changes (bool c)
540 _coalesce_player_changes = c;
543 _pending_player_change = false;
545 if (_pending_player_change) {
546 player_changed (false);
552 FilmViewer::timecode_clicked ()
554 PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
555 if (dialog->ShowModal() == wxID_OK) {
556 go_to (dialog->get ());
562 FilmViewer::frame_number_clicked ()
564 PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
565 if (dialog->ShowModal() == wxID_OK) {
566 go_to (dialog->get ());
572 FilmViewer::jump_to_selected_clicked ()
574 Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
578 FilmViewer::seek (DCPTime t, bool accurate)
584 _butler->seek (t, accurate);
585 _last_seek_accurate = accurate;