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"
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;
70 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
72 return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
75 FilmViewer::FilmViewer (wxWindow* p, bool outline_content, bool jump_to_selected)
77 , _panel (new wxPanel (this))
78 , _outline_content (0)
79 , _left_eye (new wxRadioButton (this, wxID_ANY, _("Left eye"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP))
80 , _right_eye (new wxRadioButton (this, wxID_ANY, _("Right eye")))
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)
100 _panel->SetDoubleBuffered (true);
103 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
105 _v_sizer = new wxBoxSizer (wxVERTICAL);
108 _v_sizer->Add (_panel, 1, wxEXPAND);
110 wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
111 if (outline_content) {
112 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
113 view_options->Add (_outline_content, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
115 view_options->Add (_left_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
116 view_options->Add (_right_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
117 if (jump_to_selected) {
118 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
119 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
121 _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
123 wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
125 wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
126 time_sizer->Add (_frame_number, 0, wxEXPAND);
127 time_sizer->Add (_timecode, 0, wxEXPAND);
129 h_sizer->Add (_rewind_button, 0, wxALL, 2);
130 h_sizer->Add (_back_button, 0, wxALL, 2);
131 h_sizer->Add (time_sizer, 0, wxEXPAND);
132 h_sizer->Add (_forward_button, 0, wxALL, 2);
133 h_sizer->Add (_play_button, 0, wxEXPAND);
134 h_sizer->Add (_slider, 1, wxEXPAND);
136 _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
138 _frame_number->SetMinSize (wxSize (84, -1));
139 _rewind_button->SetMinSize (wxSize (32, -1));
140 _back_button->SetMinSize (wxSize (32, -1));
141 _forward_button->SetMinSize (wxSize (32, -1));
143 _panel->Bind (wxEVT_PAINT, boost::bind (&FilmViewer::paint_panel, this));
144 _panel->Bind (wxEVT_SIZE, boost::bind (&FilmViewer::panel_sized, this, _1));
145 if (_outline_content) {
146 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::refresh_panel, this));
148 _left_eye->Bind (wxEVT_RADIOBUTTON, boost::bind (&FilmViewer::slow_refresh, this));
149 _right_eye->Bind (wxEVT_RADIOBUTTON, boost::bind (&FilmViewer::slow_refresh, this));
150 _slider->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&FilmViewer::slider_moved, this, false));
151 _slider->Bind (wxEVT_SCROLL_PAGEUP, boost::bind (&FilmViewer::slider_moved, this, true));
152 _slider->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind (&FilmViewer::slider_moved, this, true));
153 _slider->Bind (wxEVT_SCROLL_THUMBRELEASE, boost::bind (&FilmViewer::slider_released, this));
154 _play_button->Bind (wxEVT_TOGGLEBUTTON, boost::bind (&FilmViewer::play_clicked, this));
155 _timer.Bind (wxEVT_TIMER, boost::bind (&FilmViewer::timer, this));
156 _rewind_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::rewind_clicked, this, _1));
157 _back_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::back_clicked, this, _1));
158 _forward_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::forward_clicked, this, _1));
159 _frame_number->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::frame_number_clicked, this));
160 _timecode->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::timecode_clicked, this));
161 if (_jump_to_selected) {
162 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::jump_to_selected_clicked, this));
163 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
166 set_film (shared_ptr<Film> ());
168 JobManager::instance()->ActiveJobsChanged.connect (
169 bind (&FilmViewer::active_jobs_changed, this, _2)
172 setup_sensitivity ();
174 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
175 config_changed (Config::SOUND_OUTPUT);
178 FilmViewer::~FilmViewer ()
184 FilmViewer::set_film (shared_ptr<Film> film)
194 update_position_slider ();
195 update_position_label ();
206 _player.reset (new Player (_film, _film->playlist ()));
207 _player->set_fast ();
208 if (_dcp_decode_reduction) {
209 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
211 } catch (bad_alloc) {
212 error_dialog (this, _("There is not enough free memory to do that."));
217 /* Always burn in subtitles, even if content is set not to, otherwise we won't see them
220 _player->set_always_burn_subtitles (true);
221 _player->set_play_referenced ();
223 _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
224 _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1, _2));
226 /* Keep about 1 second's worth of history samples */
227 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
234 setup_sensitivity ();
238 FilmViewer::recreate_butler ()
240 bool const was_running = stop ();
247 AudioMapping map = AudioMapping (_film->audio_channels(), _audio_channels);
249 if (_audio_channels != 2 || _film->audio_channels() < 3) {
250 for (int i = 0; i < min (_film->audio_channels(), _audio_channels); ++i) {
254 /* Special case: stereo output, at least 3 channel input, map L+R to L/R and
255 C to both, all 3dB down.
257 map.set (0, 0, 1 / sqrt(2)); // L -> L
258 map.set (1, 1, 1 / sqrt(2)); // R -> R
259 map.set (2, 0, 1 / sqrt(2)); // C -> L
260 map.set (2, 1, 1 / sqrt(2)); // C -> R
263 _butler.reset (new Butler (_player, _film->log(), map, _audio_channels));
264 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
265 _butler->disable_audio ();
274 FilmViewer::refresh_panel ()
283 DCPOMATIC_ASSERT (_butler);
286 _player_video = _butler->get_video ();
289 ((_left_eye->GetValue() && _player_video.first->eyes() == EYES_RIGHT) || (_right_eye->GetValue() && _player_video.first->eyes() == EYES_LEFT))
294 display_player_video ();
298 FilmViewer::display_player_video ()
300 if (!_player_video.first) {
306 if (_playing && (time() - _player_video.second) > one_video_frame()) {
307 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
308 part if this frame is J2K).
310 _video_position = _player_video.second;
315 /* In an ideal world, what we would do here is:
317 * 1. convert to XYZ exactly as we do in the DCP creation path.
318 * 2. convert back to RGB for the preview display, compensating
319 * for the monitor etc. etc.
321 * but this is inefficient if the source is RGB. Since we don't
322 * (currently) care too much about the precise accuracy of the preview's
323 * colour mapping (and we care more about its speed) we try to short-
324 * circuit this "ideal" situation in some cases.
326 * The content's specified colour conversion indicates the colourspace
327 * which the content is in (according to the user).
329 * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
330 * image and convert it (from whatever the user has said it is) to RGB.
333 _frame = _player_video.first->image (
334 bind (&Log::dcp_log, _film->log().get(), _1, _2),
335 bind (&PlayerVideo::always_rgb, _1),
339 ImageChanged (_player_video.first);
341 _video_position = _player_video.second;
342 _inter_position = _player_video.first->inter_position ();
343 _inter_size = _player_video.first->inter_size ();
351 if (!_film || !_playing) {
356 update_position_label ();
357 update_position_slider ();
358 DCPTime const next = _video_position + one_video_frame();
360 if (next >= _film->length()) {
364 _timer.Start (max ((next.seconds() - time().seconds()) * 1000, 1.0), wxTIMER_ONE_SHOT);
372 FilmViewer::paint_panel ()
374 wxPaintDC dc (_panel);
376 if (!_frame || !_film || !_out_size.width || !_out_size.height) {
381 wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
382 wxBitmap frame_bitmap (frame);
383 dc.DrawBitmap (frame_bitmap, 0, 0);
385 if (_out_size.width < _panel_size.width) {
386 wxPen p (GetBackgroundColour ());
387 wxBrush b (GetBackgroundColour ());
390 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
393 if (_out_size.height < _panel_size.height) {
394 wxPen p (GetBackgroundColour ());
395 wxBrush b (GetBackgroundColour ());
398 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
401 if (_outline_content && _outline_content->GetValue ()) {
402 wxPen p (wxColour (255, 0, 0), 2);
404 dc.SetBrush (*wxTRANSPARENT_BRUSH);
405 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
409 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
411 FilmViewer::slider_moved (bool page)
417 if (!page && !_slider_being_moved) {
418 /* This is the first event of a drag; stop playback for the duration of the drag */
419 _was_running_before_slider = stop ();
420 _slider_being_moved = true;
423 DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
424 /* Ensure that we hit the end of the film at the end of the slider */
425 if (t >= _film->length ()) {
426 t = _film->length() - one_video_frame();
429 update_position_label ();
433 FilmViewer::slider_released ()
435 if (_was_running_before_slider) {
436 /* Restart after a drag */
439 _slider_being_moved = false;
443 FilmViewer::panel_sized (wxSizeEvent& ev)
445 _panel_size.width = ev.GetSize().GetWidth();
446 _panel_size.height = ev.GetSize().GetHeight();
449 if (!quick_refresh()) {
452 update_position_label ();
453 update_position_slider ();
457 FilmViewer::calculate_sizes ()
463 Ratio const * container = _film->container ();
465 float const panel_ratio = _panel_size.ratio ();
466 float const film_ratio = container ? container->ratio () : 1.78;
468 if (panel_ratio < film_ratio) {
469 /* panel is less widscreen than the film; clamp width */
470 _out_size.width = _panel_size.width;
471 _out_size.height = lrintf (_out_size.width / film_ratio);
473 /* panel is more widescreen than the film; clamp height */
474 _out_size.height = _panel_size.height;
475 _out_size.width = lrintf (_out_size.height * film_ratio);
478 /* Catch silly values */
479 _out_size.width = max (64, _out_size.width);
480 _out_size.height = max (64, _out_size.height);
482 _player->set_video_container_size (_out_size);
486 FilmViewer::play_clicked ()
492 FilmViewer::check_play_state ()
494 if (!_film || _film->video_frame_rate() == 0) {
498 if (_play_button->GetValue()) {
508 if (_audio.isStreamOpen()) {
509 _audio.setStreamTime (_video_position.seconds());
510 _audio.startStream ();
516 _play_button->SetValue (true);
522 if (_audio.isStreamRunning()) {
523 /* stop stream and discard any remaining queued samples */
524 _audio.abortStream ();
532 _play_button->SetValue (false);
537 FilmViewer::update_position_slider ()
540 _slider->SetValue (0);
544 DCPTime const len = _film->length ();
547 int const new_slider_position = 4096 * _video_position.get() / len.get();
548 if (new_slider_position != _slider->GetValue()) {
549 _slider->SetValue (new_slider_position);
555 FilmViewer::update_position_label ()
558 _frame_number->SetLabel ("0");
559 _timecode->SetLabel ("0:0:0.0");
563 double const fps = _film->video_frame_rate ();
564 /* Count frame number from 1 ... not sure if this is the best idea */
565 _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_video_position.seconds() * fps) + 1));
566 _timecode->SetLabel (time_to_timecode (_video_position, fps));
570 FilmViewer::active_jobs_changed (optional<string> j)
572 /* examine content is the only job which stops the viewer working */
573 bool const a = !j || *j != "examine_content";
575 _play_button->Enable (a);
579 FilmViewer::nudge_amount (wxMouseEvent& ev)
581 DCPTime amount = one_video_frame ();
583 if (ev.ShiftDown() && !ev.ControlDown()) {
584 amount = DCPTime::from_seconds (1);
585 } else if (!ev.ShiftDown() && ev.ControlDown()) {
586 amount = DCPTime::from_seconds (10);
587 } else if (ev.ShiftDown() && ev.ControlDown()) {
588 amount = DCPTime::from_seconds (60);
595 FilmViewer::go_to (DCPTime t)
597 if (t < DCPTime ()) {
601 if (t >= _film->length ()) {
602 t = _film->length ();
606 update_position_label ();
607 update_position_slider ();
611 FilmViewer::rewind_clicked (wxMouseEvent& ev)
618 FilmViewer::back_clicked (wxMouseEvent& ev)
620 go_to (_video_position - nudge_amount (ev));
625 FilmViewer::forward_clicked (wxMouseEvent& ev)
627 go_to (_video_position + nudge_amount (ev));
632 FilmViewer::player_changed (int property, bool frequent)
638 if (_coalesce_player_changes) {
639 _pending_player_changes.push_back (property);
644 bool refreshed = false;
646 property == VideoContentProperty::CROP ||
647 property == VideoContentProperty::SCALE ||
648 property == VideoContentProperty::FADE_IN ||
649 property == VideoContentProperty::FADE_OUT ||
650 property == VideoContentProperty::COLOUR_CONVERSION ||
651 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
652 property == PlayerProperty::FILM_CONTAINER
654 refreshed = quick_refresh ();
660 update_position_label ();
661 update_position_slider ();
665 FilmViewer::setup_sensitivity ()
667 bool const c = _film && !_film->content().empty ();
670 _rewind_button->Enable (c);
671 _back_button->Enable (c);
672 _forward_button->Enable (c);
673 _play_button->Enable (c);
674 if (_outline_content) {
675 _outline_content->Enable (c);
677 _frame_number->Enable (c);
678 _timecode->Enable (c);
679 if (_jump_to_selected) {
680 _jump_to_selected->Enable (c);
683 _left_eye->Enable (c && _film->three_d ());
684 _right_eye->Enable (c && _film->three_d ());
688 FilmViewer::film_changed (Film::Property p)
690 if (p == Film::CONTENT || p == Film::THREE_D) {
691 setup_sensitivity ();
692 } else if (p == Film::AUDIO_CHANNELS) {
697 /** Re-get the current frame slowly by seeking */
699 FilmViewer::slow_refresh ()
701 seek (_video_position, true);
704 /** Try to re-get the current frame quickly by resetting the metadata
705 * in the PlayerVideo that we used last time.
706 * @return true if this was possible, false if not.
709 FilmViewer::quick_refresh ()
711 if (!_player_video.first) {
715 if (!_player_video.first->reset_metadata (_player->video_container_size(), _film->frame_size())) {
719 display_player_video ();
724 FilmViewer::set_position (DCPTime p)
728 update_position_label ();
729 update_position_slider ();
733 FilmViewer::set_coalesce_player_changes (bool c)
735 _coalesce_player_changes = c;
738 BOOST_FOREACH (int i, _pending_player_changes) {
739 player_changed (i, false);
741 _pending_player_changes.clear ();
746 FilmViewer::timecode_clicked ()
748 PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
749 if (dialog->ShowModal() == wxID_OK) {
750 go_to (dialog->get ());
756 FilmViewer::frame_number_clicked ()
758 PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
759 if (dialog->ShowModal() == wxID_OK) {
760 go_to (dialog->get ());
766 FilmViewer::jump_to_selected_clicked ()
768 Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
772 FilmViewer::seek (DCPTime t, bool accurate)
778 bool const was_running = stop ();
780 _butler->seek (t, accurate);
789 FilmViewer::config_changed (Config::Property p)
791 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
795 if (_audio.isStreamOpen ()) {
796 _audio.closeStream ();
799 if (Config::instance()->sound()) {
801 if (Config::instance()->sound_output()) {
802 while (st < _audio.getDeviceCount()) {
803 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
808 if (st == _audio.getDeviceCount()) {
809 st = _audio.getDefaultOutputDevice();
812 st = _audio.getDefaultOutputDevice();
815 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
817 RtAudio::StreamParameters sp;
819 sp.nChannels = _audio_channels;
822 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
823 #ifdef DCPOMATIC_USE_RTERROR
824 } catch (RtError& e) {
826 } catch (RtAudioError& e) {
830 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
842 FilmViewer::time () const
844 if (_audio.isStreamRunning ()) {
845 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
846 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
849 return _video_position;
853 FilmViewer::audio_callback (void* out_p, unsigned int frames)
855 _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
857 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
859 _latency_history.push_back (_audio.getStreamLatency ());
860 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
861 _latency_history.pop_front ();
869 FilmViewer::average_latency () const
871 boost::mutex::scoped_lock lm (_latency_history_mutex);
872 if (_latency_history.empty()) {
877 BOOST_FOREACH (Frame i, _latency_history) {
881 return total / _latency_history.size();
885 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
887 _dcp_decode_reduction = reduction;
889 _player->set_dcp_decode_reduction (reduction);
894 FilmViewer::dcp_decode_reduction () const
896 return _dcp_decode_reduction;
900 FilmViewer::one_video_frame () const
902 return DCPTime::from_frames (1, _film->video_frame_rate());