2 Copyright (C) 2012-2017 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"
29 #include "closed_captions_dialog.h"
31 #include "lib/ratio.h"
33 #include "lib/job_manager.h"
34 #include "lib/image.h"
35 #include "lib/exceptions.h"
36 #include "lib/examine_content_job.h"
37 #include "lib/filter.h"
38 #include "lib/player.h"
39 #include "lib/player_video.h"
40 #include "lib/video_content.h"
41 #include "lib/video_decoder.h"
42 #include "lib/timer.h"
43 #include "lib/butler.h"
45 #include "lib/config.h"
47 #include <libavutil/pixfmt.h>
49 #include <dcp/exceptions.h>
50 #include <wx/tglbtn.h>
63 using boost::shared_ptr;
64 using boost::dynamic_pointer_cast;
65 using boost::weak_ptr;
66 using boost::optional;
71 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
73 return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
76 FilmViewer::FilmViewer (wxWindow* p, bool outline_content, bool jump_to_selected)
78 , _panel (new wxPanel (this))
79 , _outline_content (0)
81 , _jump_to_selected (0)
82 , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
83 , _rewind_button (new wxButton (this, wxID_ANY, wxT("|<")))
84 , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
85 , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
86 , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
87 , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
88 , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
89 , _coalesce_player_changes (false)
90 , _slider_being_moved (false)
91 , _was_running_before_slider (false)
92 , _audio (DCPOMATIC_RTAUDIO_API)
94 , _audio_block_size (1024)
96 , _latency_history_count (0)
98 , _closed_captions_dialog (new ClosedCaptionsDialog(GetParent()))
101 _panel->SetDoubleBuffered (true);
104 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
106 _v_sizer = new wxBoxSizer (wxVERTICAL);
109 _v_sizer->Add (_panel, 1, wxEXPAND);
111 wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
112 if (outline_content) {
113 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
114 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
117 _eye = new wxChoice (this, wxID_ANY);
118 _eye->Append (_("Left"));
119 _eye->Append (_("Right"));
120 _eye->SetSelection (0);
121 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
123 if (jump_to_selected) {
124 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
125 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
128 _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
130 wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
132 wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
133 time_sizer->Add (_frame_number, 0, wxEXPAND);
134 time_sizer->Add (_timecode, 0, wxEXPAND);
136 h_sizer->Add (_rewind_button, 0, wxALL, 2);
137 h_sizer->Add (_back_button, 0, wxALL, 2);
138 h_sizer->Add (time_sizer, 0, wxEXPAND);
139 h_sizer->Add (_forward_button, 0, wxALL, 2);
140 h_sizer->Add (_play_button, 0, wxEXPAND);
141 h_sizer->Add (_slider, 1, wxEXPAND);
143 _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
145 _frame_number->SetMinSize (wxSize (84, -1));
146 _rewind_button->SetMinSize (wxSize (32, -1));
147 _back_button->SetMinSize (wxSize (32, -1));
148 _forward_button->SetMinSize (wxSize (32, -1));
150 _panel->Bind (wxEVT_PAINT, boost::bind (&FilmViewer::paint_panel, this));
151 _panel->Bind (wxEVT_SIZE, boost::bind (&FilmViewer::panel_sized, this, _1));
152 if (_outline_content) {
153 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::refresh_panel, this));
155 _eye->Bind (wxEVT_CHOICE, boost::bind (&FilmViewer::slow_refresh, this));
156 _slider->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&FilmViewer::slider_moved, this, false));
157 _slider->Bind (wxEVT_SCROLL_PAGEUP, boost::bind (&FilmViewer::slider_moved, this, true));
158 _slider->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind (&FilmViewer::slider_moved, this, true));
159 _slider->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind (&FilmViewer::slider_released, this));
160 _play_button->Bind (wxEVT_TOGGLEBUTTON, boost::bind (&FilmViewer::play_clicked, this));
161 _timer.Bind (wxEVT_TIMER, boost::bind (&FilmViewer::timer, this));
162 _rewind_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::rewind_clicked, this, _1));
163 _back_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::back_clicked, this, _1));
164 _forward_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::forward_clicked, this, _1));
165 _frame_number->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::frame_number_clicked, this));
166 _timecode->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::timecode_clicked, this));
167 if (_jump_to_selected) {
168 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::jump_to_selected_clicked, this));
169 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
172 set_film (shared_ptr<Film> ());
174 JobManager::instance()->ActiveJobsChanged.connect (
175 bind (&FilmViewer::active_jobs_changed, this, _2)
178 setup_sensitivity ();
180 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
181 config_changed (Config::SOUND_OUTPUT);
184 FilmViewer::~FilmViewer ()
190 FilmViewer::set_film (shared_ptr<Film> film)
199 _closed_captions_dialog->clear ();
201 update_position_slider ();
202 update_position_label ();
213 _player.reset (new Player (_film, _film->playlist ()));
214 _player->set_fast ();
215 if (_dcp_decode_reduction) {
216 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
218 } catch (bad_alloc) {
219 error_dialog (this, _("There is not enough free memory to do that."));
224 _player->set_always_burn_open_subtitles ();
225 _player->set_play_referenced ();
227 _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
228 _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
230 /* Keep about 1 second's worth of history samples */
231 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
238 setup_sensitivity ();
242 FilmViewer::recreate_butler ()
244 bool const was_running = stop ();
251 AudioMapping map = AudioMapping (_film->audio_channels(), _audio_channels);
253 if (_audio_channels != 2 || _film->audio_channels() < 3) {
254 for (int i = 0; i < min (_film->audio_channels(), _audio_channels); ++i) {
258 /* Special case: stereo output, at least 3 channel input.
259 Map so that Lt = L(-3dB) + Ls(-3dB) + C(-6dB) + Lfe(-10dB)
260 Rt = R(-3dB) + Rs(-3dB) + C(-6dB) + Lfe(-10dB)
262 map.set (dcp::LEFT, 0, 1 / sqrt(2)); // L -> Lt
263 map.set (dcp::RIGHT, 1, 1 / sqrt(2)); // R -> Rt
264 map.set (dcp::CENTRE, 0, 1 / 2.0); // C -> Lt
265 map.set (dcp::CENTRE, 1, 1 / 2.0); // C -> Rt
266 map.set (dcp::LFE, 0, 1 / sqrt(10)); // Lfe -> Lt
267 map.set (dcp::LFE, 1, 1 / sqrt(10)); // Lfe -> Rt
268 map.set (dcp::LS, 0, 1 / sqrt(2)); // Ls -> Lt
269 map.set (dcp::RS, 1, 1 / sqrt(2)); // Rs -> Rt
272 _butler.reset (new Butler (_player, _film->log(), map, _audio_channels));
273 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
274 _butler->disable_audio ();
277 _closed_captions_dialog->set_butler (_butler);
285 FilmViewer::refresh_panel ()
294 DCPOMATIC_ASSERT (_butler);
297 _player_video = _butler->get_video ();
300 ((_eye->GetSelection() == 0 && _player_video.first->eyes() == EYES_RIGHT) || (_eye->GetSelection() == 1 && _player_video.first->eyes() == EYES_LEFT))
305 display_player_video ();
309 FilmViewer::display_player_video ()
311 if (!_player_video.first) {
317 if (_playing && (time() - _player_video.second) > one_video_frame()) {
318 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
319 part if this frame is J2K).
321 _video_position = _player_video.second;
326 /* In an ideal world, what we would do here is:
328 * 1. convert to XYZ exactly as we do in the DCP creation path.
329 * 2. convert back to RGB for the preview display, compensating
330 * for the monitor etc. etc.
332 * but this is inefficient if the source is RGB. Since we don't
333 * (currently) care too much about the precise accuracy of the preview's
334 * colour mapping (and we care more about its speed) we try to short-
335 * circuit this "ideal" situation in some cases.
337 * The content's specified colour conversion indicates the colourspace
338 * which the content is in (according to the user).
340 * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
341 * image and convert it (from whatever the user has said it is) to RGB.
344 _frame = _player_video.first->image (
345 bind (&Log::dcp_log, _film->log().get(), _1, _2),
346 bind (&PlayerVideo::always_rgb, _1),
350 ImageChanged (_player_video.first);
352 _video_position = _player_video.second;
353 _inter_position = _player_video.first->inter_position ();
354 _inter_size = _player_video.first->inter_size ();
358 _closed_captions_dialog->update (time());
364 if (!_film || !_playing) {
369 update_position_label ();
370 update_position_slider ();
371 DCPTime const next = _video_position + one_video_frame();
373 if (next >= _film->length()) {
377 _timer.Start (max ((next.seconds() - time().seconds()) * 1000, 1.0), wxTIMER_ONE_SHOT);
385 FilmViewer::paint_panel ()
387 wxPaintDC dc (_panel);
389 if (!_frame || !_film || !_out_size.width || !_out_size.height || _out_size != _frame->size()) {
394 wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
395 wxBitmap frame_bitmap (frame);
396 dc.DrawBitmap (frame_bitmap, 0, 0);
398 if (_out_size.width < _panel_size.width) {
399 wxPen p (GetBackgroundColour ());
400 wxBrush b (GetBackgroundColour ());
403 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
406 if (_out_size.height < _panel_size.height) {
407 wxPen p (GetBackgroundColour ());
408 wxBrush b (GetBackgroundColour ());
411 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
414 if (_outline_content && _outline_content->GetValue ()) {
415 wxPen p (wxColour (255, 0, 0), 2);
417 dc.SetBrush (*wxTRANSPARENT_BRUSH);
418 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
422 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
424 FilmViewer::slider_moved (bool page)
430 if (!page && !_slider_being_moved) {
431 /* This is the first event of a drag; stop playback for the duration of the drag */
432 _was_running_before_slider = stop ();
433 _slider_being_moved = true;
436 DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
437 t = t.round (_film->video_frame_rate());
438 /* Ensure that we hit the end of the film at the end of the slider */
439 if (t >= _film->length ()) {
440 t = _film->length() - one_video_frame();
443 update_position_label ();
447 FilmViewer::slider_released ()
449 if (_was_running_before_slider) {
450 /* Restart after a drag */
453 _slider_being_moved = false;
457 FilmViewer::panel_sized (wxSizeEvent& ev)
459 _panel_size.width = ev.GetSize().GetWidth();
460 _panel_size.height = ev.GetSize().GetHeight();
463 if (!quick_refresh()) {
466 update_position_label ();
467 update_position_slider ();
471 FilmViewer::calculate_sizes ()
473 if (!_film || !_player) {
477 Ratio const * container = _film->container ();
479 float const panel_ratio = _panel_size.ratio ();
480 float const film_ratio = container ? container->ratio () : 1.78;
482 if (panel_ratio < film_ratio) {
483 /* panel is less widscreen than the film; clamp width */
484 _out_size.width = _panel_size.width;
485 _out_size.height = lrintf (_out_size.width / film_ratio);
487 /* panel is more widescreen than the film; clamp height */
488 _out_size.height = _panel_size.height;
489 _out_size.width = lrintf (_out_size.height * film_ratio);
492 /* Catch silly values */
493 _out_size.width = max (64, _out_size.width);
494 _out_size.height = max (64, _out_size.height);
496 _player->set_video_container_size (_out_size);
500 FilmViewer::play_clicked ()
506 FilmViewer::check_play_state ()
508 if (!_film || _film->video_frame_rate() == 0) {
512 if (_play_button->GetValue()) {
526 if (_audio.isStreamOpen()) {
527 _audio.setStreamTime (_video_position.seconds());
528 _audio.startStream ();
534 _play_button->SetValue (true);
540 if (_audio.isStreamRunning()) {
541 /* stop stream and discard any remaining queued samples */
542 _audio.abortStream ();
550 _play_button->SetValue (false);
555 FilmViewer::update_position_slider ()
558 _slider->SetValue (0);
562 DCPTime const len = _film->length ();
565 int const new_slider_position = 4096 * _video_position.get() / len.get();
566 if (new_slider_position != _slider->GetValue()) {
567 _slider->SetValue (new_slider_position);
573 FilmViewer::update_position_label ()
576 _frame_number->SetLabel ("0");
577 _timecode->SetLabel ("0:0:0.0");
581 double const fps = _film->video_frame_rate ();
582 /* Count frame number from 1 ... not sure if this is the best idea */
583 _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_video_position.seconds() * fps) + 1));
584 _timecode->SetLabel (time_to_timecode (_video_position, fps));
588 FilmViewer::active_jobs_changed (optional<string> j)
590 /* examine content is the only job which stops the viewer working */
591 bool const a = !j || *j != "examine_content";
593 _play_button->Enable (a);
597 FilmViewer::nudge_amount (wxKeyboardState& ev)
599 DCPTime amount = one_video_frame ();
601 if (ev.ShiftDown() && !ev.ControlDown()) {
602 amount = DCPTime::from_seconds (1);
603 } else if (!ev.ShiftDown() && ev.ControlDown()) {
604 amount = DCPTime::from_seconds (10);
605 } else if (ev.ShiftDown() && ev.ControlDown()) {
606 amount = DCPTime::from_seconds (60);
613 FilmViewer::go_to (DCPTime t)
615 if (t < DCPTime ()) {
619 if (t >= _film->length ()) {
620 t = _film->length ();
624 update_position_label ();
625 update_position_slider ();
629 FilmViewer::rewind_clicked (wxMouseEvent& ev)
636 FilmViewer::back_frame ()
642 go_to (_video_position - one_video_frame());
646 FilmViewer::forward_frame ()
652 go_to (_video_position + one_video_frame());
656 FilmViewer::back_clicked (wxKeyboardState& ev)
658 go_to (_video_position - nudge_amount (ev));
662 FilmViewer::forward_clicked (wxKeyboardState& ev)
664 go_to (_video_position + nudge_amount (ev));
668 FilmViewer::player_change (ChangeType type, int property, bool frequent)
670 if (type != CHANGE_TYPE_DONE || frequent) {
674 if (_coalesce_player_changes) {
675 _pending_player_changes.push_back (property);
680 bool refreshed = false;
682 property == VideoContentProperty::CROP ||
683 property == VideoContentProperty::SCALE ||
684 property == VideoContentProperty::FADE_IN ||
685 property == VideoContentProperty::FADE_OUT ||
686 property == VideoContentProperty::COLOUR_CONVERSION ||
687 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
688 property == PlayerProperty::FILM_CONTAINER
690 refreshed = quick_refresh ();
696 update_position_label ();
697 update_position_slider ();
701 FilmViewer::setup_sensitivity ()
703 bool const c = _film && !_film->content().empty ();
706 _rewind_button->Enable (c);
707 _back_button->Enable (c);
708 _forward_button->Enable (c);
709 _play_button->Enable (c);
710 if (_outline_content) {
711 _outline_content->Enable (c);
713 _frame_number->Enable (c);
714 _timecode->Enable (c);
715 if (_jump_to_selected) {
716 _jump_to_selected->Enable (c);
719 _eye->Enable (c && _film->three_d ());
723 FilmViewer::film_changed (Film::Property p)
725 if (p == Film::CONTENT || p == Film::THREE_D) {
726 setup_sensitivity ();
727 } else if (p == Film::AUDIO_CHANNELS) {
732 /** Re-get the current frame slowly by seeking */
734 FilmViewer::slow_refresh ()
736 seek (_video_position, true);
739 /** Try to re-get the current frame quickly by resetting the metadata
740 * in the PlayerVideo that we used last time.
741 * @return true if this was possible, false if not.
744 FilmViewer::quick_refresh ()
746 if (!_player_video.first) {
750 if (!_player_video.first->reset_metadata (_player->video_container_size(), _film->frame_size())) {
754 display_player_video ();
759 FilmViewer::set_position (DCPTime p)
763 update_position_label ();
764 update_position_slider ();
768 FilmViewer::set_position (shared_ptr<Content> content, ContentTime t)
770 optional<DCPTime> dt = _player->content_time_to_dcp (content, t);
777 FilmViewer::set_coalesce_player_changes (bool c)
779 _coalesce_player_changes = c;
782 BOOST_FOREACH (int i, _pending_player_changes) {
783 player_change (CHANGE_TYPE_DONE, i, false);
785 _pending_player_changes.clear ();
790 FilmViewer::timecode_clicked ()
792 PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
793 if (dialog->ShowModal() == wxID_OK) {
794 go_to (dialog->get ());
800 FilmViewer::frame_number_clicked ()
802 PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
803 if (dialog->ShowModal() == wxID_OK) {
804 go_to (dialog->get ());
810 FilmViewer::jump_to_selected_clicked ()
812 Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
816 FilmViewer::seek (DCPTime t, bool accurate)
822 bool const was_running = stop ();
824 _closed_captions_dialog->clear ();
825 _butler->seek (t, accurate);
834 FilmViewer::config_changed (Config::Property p)
836 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
840 if (_audio.isStreamOpen ()) {
841 _audio.closeStream ();
844 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
846 if (Config::instance()->sound_output()) {
847 while (st < _audio.getDeviceCount()) {
848 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
853 if (st == _audio.getDeviceCount()) {
854 st = _audio.getDefaultOutputDevice();
857 st = _audio.getDefaultOutputDevice();
860 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
862 RtAudio::StreamParameters sp;
864 sp.nChannels = _audio_channels;
867 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
868 #ifdef DCPOMATIC_USE_RTERROR
869 } catch (RtError& e) {
871 } catch (RtAudioError& e) {
875 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
887 FilmViewer::uncorrected_time () const
889 if (_audio.isStreamRunning ()) {
890 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
893 return _video_position;
897 FilmViewer::time () const
899 if (_audio.isStreamRunning ()) {
900 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
901 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
904 return _video_position;
908 FilmViewer::audio_callback (void* out_p, unsigned int frames)
911 optional<DCPTime> t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
912 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
913 /* There was an underrun or this audio is on time; carry on */
916 /* The audio we just got was (very) late; drop it and get some more. */
919 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
921 _latency_history.push_back (_audio.getStreamLatency ());
922 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
923 _latency_history.pop_front ();
931 FilmViewer::average_latency () const
933 boost::mutex::scoped_lock lm (_latency_history_mutex);
934 if (_latency_history.empty()) {
939 BOOST_FOREACH (Frame i, _latency_history) {
943 return total / _latency_history.size();
947 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
949 _dcp_decode_reduction = reduction;
951 _player->set_dcp_decode_reduction (reduction);
956 FilmViewer::dcp_decode_reduction () const
958 return _dcp_decode_reduction;
962 FilmViewer::one_video_frame () const
964 return DCPTime::from_frames (1, _film->video_frame_rate());
968 FilmViewer::show_closed_captions ()
970 _closed_captions_dialog->Show();