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"
43 #include "lib/config.h"
45 #include <libavutil/pixfmt.h>
47 #include <dcp/exceptions.h>
48 #include <wx/tglbtn.h>
61 using boost::shared_ptr;
62 using boost::dynamic_pointer_cast;
63 using boost::weak_ptr;
64 using boost::optional;
67 FilmViewer::FilmViewer (wxWindow* p)
69 , _panel (new wxPanel (this))
70 , _outline_content (new wxCheckBox (this, wxID_ANY, _("Outline content")))
71 , _left_eye (new wxRadioButton (this, wxID_ANY, _("Left eye"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP))
72 , _right_eye (new wxRadioButton (this, wxID_ANY, _("Right eye")))
73 , _jump_to_selected (new wxCheckBox (this, wxID_ANY, _("Jump to selected content")))
74 , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
75 , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
76 , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
77 , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
78 , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
79 , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
80 , _coalesce_player_changes (false)
81 , _pending_player_change (false)
82 , _last_seek_accurate (true)
85 _panel->SetDoubleBuffered (true);
88 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
90 _v_sizer = new wxBoxSizer (wxVERTICAL);
93 _v_sizer->Add (_panel, 1, wxEXPAND);
95 wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
96 view_options->Add (_outline_content, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
97 view_options->Add (_left_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
98 view_options->Add (_right_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
99 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
100 _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
102 wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
104 wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
105 time_sizer->Add (_frame_number, 0, wxEXPAND);
106 time_sizer->Add (_timecode, 0, wxEXPAND);
108 h_sizer->Add (_back_button, 0, wxALL, 2);
109 h_sizer->Add (time_sizer, 0, wxEXPAND);
110 h_sizer->Add (_forward_button, 0, wxALL, 2);
111 h_sizer->Add (_play_button, 0, wxEXPAND);
112 h_sizer->Add (_slider, 1, wxEXPAND);
114 _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
116 _frame_number->SetMinSize (wxSize (84, -1));
117 _back_button->SetMinSize (wxSize (32, -1));
118 _forward_button->SetMinSize (wxSize (32, -1));
120 _panel->Bind (wxEVT_PAINT, boost::bind (&FilmViewer::paint_panel, this));
121 _panel->Bind (wxEVT_SIZE, boost::bind (&FilmViewer::panel_sized, this, _1));
122 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::refresh_panel, this));
123 _left_eye->Bind (wxEVT_RADIOBUTTON, boost::bind (&FilmViewer::refresh, this));
124 _right_eye->Bind (wxEVT_RADIOBUTTON, boost::bind (&FilmViewer::refresh, this));
125 _slider->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&FilmViewer::slider_moved, this));
126 _slider->Bind (wxEVT_SCROLL_PAGEUP, boost::bind (&FilmViewer::slider_moved, this));
127 _slider->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind (&FilmViewer::slider_moved, this));
128 _play_button->Bind (wxEVT_TOGGLEBUTTON, boost::bind (&FilmViewer::play_clicked, this));
129 _timer.Bind (wxEVT_TIMER, boost::bind (&FilmViewer::timer, this));
130 _back_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::back_clicked, this, _1));
131 _forward_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::forward_clicked, this, _1));
132 _frame_number->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::frame_number_clicked, this));
133 _timecode->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::timecode_clicked, this));
134 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::jump_to_selected_clicked, this));
136 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
138 set_film (shared_ptr<Film> ());
140 JobManager::instance()->ActiveJobsChanged.connect (
141 bind (&FilmViewer::active_jobs_changed, this, _2)
144 setup_sensitivity ();
148 FilmViewer::set_film (shared_ptr<Film> film)
158 update_position_slider ();
159 update_position_label ();
166 _player.reset (new Player (_film, _film->playlist ()));
167 _player->set_fast ();
168 } catch (bad_alloc) {
169 error_dialog (this, _("There is not enough free memory to do that."));
174 /* Always burn in subtitles, even if content is set not to, otherwise we won't see them
177 _player->set_always_burn_subtitles (true);
178 _player->set_ignore_audio ();
179 _player->set_play_referenced ();
181 _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
183 _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
184 _player->Video.connect (boost::bind (&FilmViewer::video, this, _1));
189 setup_sensitivity ();
193 FilmViewer::refresh_panel ()
200 FilmViewer::video (shared_ptr<PlayerVideo> pv)
206 if (_film->three_d ()) {
207 if ((_left_eye->GetValue() && pv->eyes() == EYES_RIGHT) || (_right_eye->GetValue() && pv->eyes() == EYES_LEFT)) {
212 /* In an ideal world, what we would do here is:
214 * 1. convert to XYZ exactly as we do in the DCP creation path.
215 * 2. convert back to RGB for the preview display, compensating
216 * for the monitor etc. etc.
218 * but this is inefficient if the source is RGB. Since we don't
219 * (currently) care too much about the precise accuracy of the preview's
220 * colour mapping (and we care more about its speed) we try to short-
221 * circuit this "ideal" situation in some cases.
223 * The content's specified colour conversion indicates the colourspace
224 * which the content is in (according to the user).
226 * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
227 * image and convert it (from whatever the user has said it is) to RGB.
231 bind (&Log::dcp_log, _film->log().get(), _1, _2),
232 bind (&PlayerVideo::always_rgb, _1),
238 _position = pv->time ();
239 _inter_position = pv->inter_position ();
240 _inter_size = pv->inter_size ();
248 Image const * current = _frame.get ();
249 while (!_player->pass() && _frame.get() == current) {}
255 DCPTime const frame = DCPTime::from_frames (1, _film->video_frame_rate ());
257 if ((_position + frame) >= _film->length ()) {
258 _play_button->SetValue (false);
264 update_position_label ();
265 update_position_slider ();
269 FilmViewer::paint_panel ()
271 wxPaintDC dc (_panel);
273 if (!_frame || !_film || !_out_size.width || !_out_size.height) {
278 wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
279 wxBitmap frame_bitmap (frame);
280 dc.DrawBitmap (frame_bitmap, 0, 0);
282 if (_out_size.width < _panel_size.width) {
283 wxPen p (GetBackgroundColour ());
284 wxBrush b (GetBackgroundColour ());
287 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
290 if (_out_size.height < _panel_size.height) {
291 wxPen p (GetBackgroundColour ());
292 wxBrush b (GetBackgroundColour ());
295 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
298 if (_outline_content->GetValue ()) {
299 wxPen p (wxColour (255, 0, 0), 2);
301 dc.SetBrush (*wxTRANSPARENT_BRUSH);
302 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
307 FilmViewer::slider_moved ()
313 DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
314 /* Ensure that we hit the end of the film at the end of the slider */
315 if (t >= _film->length ()) {
316 t = _film->length() - DCPTime::from_frames (1, _film->video_frame_rate ());
319 update_position_label ();
323 FilmViewer::panel_sized (wxSizeEvent& ev)
325 _panel_size.width = ev.GetSize().GetWidth();
326 _panel_size.height = ev.GetSize().GetHeight();
330 update_position_label ();
331 update_position_slider ();
335 FilmViewer::calculate_sizes ()
337 if (!_film || !_player) {
341 Ratio const * container = _film->container ();
343 float const panel_ratio = _panel_size.ratio ();
344 float const film_ratio = container ? container->ratio () : 1.78;
346 if (panel_ratio < film_ratio) {
347 /* panel is less widscreen than the film; clamp width */
348 _out_size.width = _panel_size.width;
349 _out_size.height = lrintf (_out_size.width / film_ratio);
351 /* panel is more widescreen than the film; clamp height */
352 _out_size.height = _panel_size.height;
353 _out_size.width = lrintf (_out_size.height * film_ratio);
356 /* Catch silly values */
357 _out_size.width = max (64, _out_size.width);
358 _out_size.height = max (64, _out_size.height);
360 _player->set_video_container_size (_out_size);
364 FilmViewer::play_clicked ()
370 FilmViewer::check_play_state ()
372 if (!_film || _film->video_frame_rate() == 0) {
376 if (_play_button->GetValue()) {
377 _timer.Start (1000 / _film->video_frame_rate());
384 FilmViewer::update_position_slider ()
387 _slider->SetValue (0);
391 DCPTime const len = _film->length ();
394 int const new_slider_position = 4096 * _position.get() / len.get();
395 if (new_slider_position != _slider->GetValue()) {
396 _slider->SetValue (new_slider_position);
402 FilmViewer::update_position_label ()
405 _frame_number->SetLabel ("0");
406 _timecode->SetLabel ("0:0:0.0");
410 double const fps = _film->video_frame_rate ();
411 /* Count frame number from 1 ... not sure if this is the best idea */
412 _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_position.seconds() * fps) + 1));
413 _timecode->SetLabel (time_to_timecode (_position, fps));
417 FilmViewer::active_jobs_changed (optional<string> j)
419 /* examine content is the only job which stops the viewer working */
420 bool const a = !j || *j != "examine_content";
422 _play_button->Enable (a);
426 FilmViewer::nudge_amount (wxMouseEvent& ev)
428 DCPTime amount = DCPTime::from_frames (1, _film->video_frame_rate ());
430 if (ev.ShiftDown() && !ev.ControlDown()) {
431 amount = DCPTime::from_seconds (1);
432 } else if (!ev.ShiftDown() && ev.ControlDown()) {
433 amount = DCPTime::from_seconds (10);
434 } else if (ev.ShiftDown() && ev.ControlDown()) {
435 amount = DCPTime::from_seconds (60);
442 FilmViewer::go_to (DCPTime t)
444 if (t < DCPTime ()) {
448 if (t >= _film->length ()) {
449 t = _film->length ();
453 update_position_label ();
454 update_position_slider ();
458 FilmViewer::back_clicked (wxMouseEvent& ev)
460 go_to (_position - nudge_amount (ev));
465 FilmViewer::forward_clicked (wxMouseEvent& ev)
467 go_to (_position + nudge_amount (ev));
472 FilmViewer::player_changed (bool frequent)
478 if (_coalesce_player_changes) {
479 _pending_player_change = true;
485 update_position_label ();
486 update_position_slider ();
490 FilmViewer::setup_sensitivity ()
492 bool const c = _film && !_film->content().empty ();
495 _back_button->Enable (c);
496 _forward_button->Enable (c);
497 _play_button->Enable (c);
498 _outline_content->Enable (c);
499 _frame_number->Enable (c);
500 _timecode->Enable (c);
502 _left_eye->Enable (c && _film->three_d ());
503 _right_eye->Enable (c && _film->three_d ());
507 FilmViewer::film_changed (Film::Property p)
509 if (p == Film::CONTENT || p == Film::THREE_D) {
510 setup_sensitivity ();
514 /** Re-get the current frame */
516 FilmViewer::refresh ()
518 seek (_position, _last_seek_accurate);
522 FilmViewer::set_position (DCPTime p)
526 update_position_label ();
527 update_position_slider ();
531 FilmViewer::set_coalesce_player_changes (bool c)
533 _coalesce_player_changes = c;
536 _pending_player_change = false;
538 if (_pending_player_change) {
539 player_changed (false);
545 FilmViewer::timecode_clicked ()
547 PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
548 if (dialog->ShowModal() == wxID_OK) {
549 go_to (dialog->get ());
555 FilmViewer::frame_number_clicked ()
557 PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
558 if (dialog->ShowModal() == wxID_OK) {
559 go_to (dialog->get ());
565 FilmViewer::jump_to_selected_clicked ()
567 Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
571 FilmViewer::seek (DCPTime t, bool accurate)
573 _player->seek (t, accurate);
574 _last_seek_accurate = accurate;