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;
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 , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
84 , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
85 , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
86 , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
87 , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
88 , _coalesce_player_changes (false)
89 , _pending_player_change (false)
90 , _last_seek_accurate (true)
91 , _audio (DCPOMATIC_RTAUDIO_API)
93 , _audio_block_size (1024)
95 , _latency_history_count (0)
99 _panel->SetDoubleBuffered (true);
102 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
104 _v_sizer = new wxBoxSizer (wxVERTICAL);
107 _v_sizer->Add (_panel, 1, wxEXPAND);
109 wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
110 if (outline_content) {
111 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
112 view_options->Add (_outline_content, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
114 view_options->Add (_left_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
115 view_options->Add (_right_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
116 if (jump_to_selected) {
117 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
118 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
120 _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
122 wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
124 wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
125 time_sizer->Add (_frame_number, 0, wxEXPAND);
126 time_sizer->Add (_timecode, 0, wxEXPAND);
128 h_sizer->Add (_back_button, 0, wxALL, 2);
129 h_sizer->Add (time_sizer, 0, wxEXPAND);
130 h_sizer->Add (_forward_button, 0, wxALL, 2);
131 h_sizer->Add (_play_button, 0, wxEXPAND);
132 h_sizer->Add (_slider, 1, wxEXPAND);
134 _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
136 _frame_number->SetMinSize (wxSize (84, -1));
137 _back_button->SetMinSize (wxSize (32, -1));
138 _forward_button->SetMinSize (wxSize (32, -1));
140 _panel->Bind (wxEVT_PAINT, boost::bind (&FilmViewer::paint_panel, this));
141 _panel->Bind (wxEVT_SIZE, boost::bind (&FilmViewer::panel_sized, this, _1));
142 if (_outline_content) {
143 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::refresh_panel, this));
145 _left_eye->Bind (wxEVT_RADIOBUTTON, boost::bind (&FilmViewer::refresh, this));
146 _right_eye->Bind (wxEVT_RADIOBUTTON, boost::bind (&FilmViewer::refresh, this));
147 _slider->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&FilmViewer::slider_moved, this, false));
148 _slider->Bind (wxEVT_SCROLL_PAGEUP, boost::bind (&FilmViewer::slider_moved, this, false));
149 _slider->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind (&FilmViewer::slider_moved, this, false));
150 _slider->Bind (wxEVT_SCROLL_CHANGED, boost::bind (&FilmViewer::slider_moved, this, true));
151 _play_button->Bind (wxEVT_TOGGLEBUTTON, boost::bind (&FilmViewer::play_clicked, this));
152 _timer.Bind (wxEVT_TIMER, boost::bind (&FilmViewer::timer, this));
153 _back_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::back_clicked, this, _1));
154 _forward_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::forward_clicked, this, _1));
155 _frame_number->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::frame_number_clicked, this));
156 _timecode->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::timecode_clicked, this));
157 if (_jump_to_selected) {
158 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::jump_to_selected_clicked, this));
159 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
162 set_film (shared_ptr<Film> ());
164 JobManager::instance()->ActiveJobsChanged.connect (
165 bind (&FilmViewer::active_jobs_changed, this, _2)
168 setup_sensitivity ();
170 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
171 config_changed (Config::SOUND_OUTPUT);
174 FilmViewer::~FilmViewer ()
180 FilmViewer::set_film (shared_ptr<Film> film)
190 update_position_slider ();
191 update_position_label ();
198 _player.reset (new Player (_film, _film->playlist ()));
199 _player->set_fast ();
200 } catch (bad_alloc) {
201 error_dialog (this, _("There is not enough free memory to do that."));
206 /* Always burn in subtitles, even if content is set not to, otherwise we won't see them
209 _player->set_always_burn_subtitles (true);
210 _player->set_play_referenced ();
212 _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
213 _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
215 /* Keep about 1 second's worth of history samples */
216 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
223 setup_sensitivity ();
227 FilmViewer::recreate_butler ()
229 bool const was_running = stop ();
236 AudioMapping map = AudioMapping (_film->audio_channels(), _audio_channels);
238 if (_audio_channels != 2 || _film->audio_channels() < 3) {
239 for (int i = 0; i < min (_film->audio_channels(), _audio_channels); ++i) {
243 /* Special case: stereo output, at least 3 channel input, map L+R to L/R and
244 C to both, all 3dB down.
246 map.set (0, 0, 1 / sqrt(2)); // L -> L
247 map.set (1, 1, 1 / sqrt(2)); // R -> R
248 map.set (2, 0, 1 / sqrt(2)); // C -> L
249 map.set (2, 1, 1 / sqrt(2)); // C -> R
252 _butler.reset (new Butler (_player, _film->log(), map, _audio_channels));
253 if (!Config::instance()->sound()) {
254 _butler->disable_audio ();
263 FilmViewer::refresh_panel ()
272 DCPOMATIC_ASSERT (_butler);
274 pair<shared_ptr<PlayerVideo>, DCPTime> video;
276 video = _butler->get_video ();
279 ((_left_eye->GetValue() && video.first->eyes() == EYES_RIGHT) || (_right_eye->GetValue() && video.first->eyes() == EYES_LEFT))
290 if ((time() - video.second) > one_video_frame()) {
291 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
292 part if this frame is J2K).
294 _video_position = video.second;
299 /* In an ideal world, what we would do here is:
301 * 1. convert to XYZ exactly as we do in the DCP creation path.
302 * 2. convert back to RGB for the preview display, compensating
303 * for the monitor etc. etc.
305 * but this is inefficient if the source is RGB. Since we don't
306 * (currently) care too much about the precise accuracy of the preview's
307 * colour mapping (and we care more about its speed) we try to short-
308 * circuit this "ideal" situation in some cases.
310 * The content's specified colour conversion indicates the colourspace
311 * which the content is in (according to the user).
313 * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
314 * image and convert it (from whatever the user has said it is) to RGB.
317 _frame = video.first->image (
318 bind (&Log::dcp_log, _film->log().get(), _1, _2),
319 bind (&PlayerVideo::always_rgb, _1),
323 ImageChanged (video.first);
325 _video_position = video.second;
326 _inter_position = video.first->inter_position ();
327 _inter_size = video.first->inter_size ();
335 if (!_film || !_playing) {
340 update_position_label ();
341 update_position_slider ();
342 DCPTime const next = _video_position + one_video_frame();
344 if (next >= _film->length()) {
348 _timer.Start (max ((next.seconds() - time().seconds()) * 1000, 1.0), wxTIMER_ONE_SHOT);
356 FilmViewer::paint_panel ()
358 wxPaintDC dc (_panel);
360 if (!_frame || !_film || !_out_size.width || !_out_size.height) {
365 wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
366 wxBitmap frame_bitmap (frame);
367 dc.DrawBitmap (frame_bitmap, 0, 0);
369 if (_out_size.width < _panel_size.width) {
370 wxPen p (GetBackgroundColour ());
371 wxBrush b (GetBackgroundColour ());
374 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
377 if (_out_size.height < _panel_size.height) {
378 wxPen p (GetBackgroundColour ());
379 wxBrush b (GetBackgroundColour ());
382 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
385 if (_outline_content && _outline_content->GetValue ()) {
386 wxPen p (wxColour (255, 0, 0), 2);
388 dc.SetBrush (*wxTRANSPARENT_BRUSH);
389 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
394 FilmViewer::slider_moved (bool update_slider)
401 DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
402 /* Ensure that we hit the end of the film at the end of the slider */
403 if (t >= _film->length ()) {
404 t = _film->length() - one_video_frame();
407 update_position_label ();
409 update_position_slider ();
414 FilmViewer::panel_sized (wxSizeEvent& ev)
416 _panel_size.width = ev.GetSize().GetWidth();
417 _panel_size.height = ev.GetSize().GetHeight();
421 update_position_label ();
422 update_position_slider ();
426 FilmViewer::calculate_sizes ()
432 Ratio const * container = _film->container ();
434 float const panel_ratio = _panel_size.ratio ();
435 float const film_ratio = container ? container->ratio () : 1.78;
437 if (panel_ratio < film_ratio) {
438 /* panel is less widscreen than the film; clamp width */
439 _out_size.width = _panel_size.width;
440 _out_size.height = lrintf (_out_size.width / film_ratio);
442 /* panel is more widescreen than the film; clamp height */
443 _out_size.height = _panel_size.height;
444 _out_size.width = lrintf (_out_size.height * film_ratio);
447 /* Catch silly values */
448 _out_size.width = max (64, _out_size.width);
449 _out_size.height = max (64, _out_size.height);
451 _player->set_video_container_size (_out_size);
455 FilmViewer::play_clicked ()
461 FilmViewer::check_play_state ()
463 if (!_film || _film->video_frame_rate() == 0) {
467 if (_play_button->GetValue()) {
477 if (_audio.isStreamOpen()) {
478 _audio.setStreamTime (_video_position.seconds());
479 _audio.startStream ();
490 if (_audio.isStreamRunning()) {
491 /* stop stream and discard any remainig queued samples */
492 _audio.abortStream ();
500 _play_button->SetValue (false);
505 FilmViewer::update_position_slider ()
508 _slider->SetValue (0);
512 DCPTime const len = _film->length ();
515 int const new_slider_position = 4096 * _video_position.get() / len.get();
516 if (new_slider_position != _slider->GetValue()) {
517 _slider->SetValue (new_slider_position);
523 FilmViewer::update_position_label ()
526 _frame_number->SetLabel ("0");
527 _timecode->SetLabel ("0:0:0.0");
531 double const fps = _film->video_frame_rate ();
532 /* Count frame number from 1 ... not sure if this is the best idea */
533 _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_video_position.seconds() * fps) + 1));
534 _timecode->SetLabel (time_to_timecode (_video_position, fps));
538 FilmViewer::active_jobs_changed (optional<string> j)
540 /* examine content is the only job which stops the viewer working */
541 bool const a = !j || *j != "examine_content";
543 _play_button->Enable (a);
547 FilmViewer::nudge_amount (wxMouseEvent& ev)
549 DCPTime amount = one_video_frame ();
551 if (ev.ShiftDown() && !ev.ControlDown()) {
552 amount = DCPTime::from_seconds (1);
553 } else if (!ev.ShiftDown() && ev.ControlDown()) {
554 amount = DCPTime::from_seconds (10);
555 } else if (ev.ShiftDown() && ev.ControlDown()) {
556 amount = DCPTime::from_seconds (60);
563 FilmViewer::go_to (DCPTime t)
565 if (t < DCPTime ()) {
569 if (t >= _film->length ()) {
570 t = _film->length ();
574 update_position_label ();
575 update_position_slider ();
579 FilmViewer::back_clicked (wxMouseEvent& ev)
581 go_to (_video_position - nudge_amount (ev));
586 FilmViewer::forward_clicked (wxMouseEvent& ev)
588 go_to (_video_position + nudge_amount (ev));
593 FilmViewer::player_changed (bool frequent)
599 if (_coalesce_player_changes) {
600 _pending_player_change = true;
606 update_position_label ();
607 update_position_slider ();
611 FilmViewer::setup_sensitivity ()
613 bool const c = _film && !_film->content().empty ();
616 _back_button->Enable (c);
617 _forward_button->Enable (c);
618 _play_button->Enable (c);
619 if (_outline_content) {
620 _outline_content->Enable (c);
622 _frame_number->Enable (c);
623 _timecode->Enable (c);
624 if (_jump_to_selected) {
625 _jump_to_selected->Enable (c);
628 _left_eye->Enable (c && _film->three_d ());
629 _right_eye->Enable (c && _film->three_d ());
633 FilmViewer::film_changed (Film::Property p)
635 if (p == Film::CONTENT || p == Film::THREE_D) {
636 setup_sensitivity ();
640 /** Re-get the current frame */
642 FilmViewer::refresh ()
644 seek (_video_position, _last_seek_accurate);
648 FilmViewer::set_position (DCPTime p)
652 update_position_label ();
653 update_position_slider ();
657 FilmViewer::set_coalesce_player_changes (bool c)
659 _coalesce_player_changes = c;
662 _pending_player_change = false;
664 if (_pending_player_change) {
665 player_changed (false);
671 FilmViewer::timecode_clicked ()
673 PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
674 if (dialog->ShowModal() == wxID_OK) {
675 go_to (dialog->get ());
681 FilmViewer::frame_number_clicked ()
683 PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
684 if (dialog->ShowModal() == wxID_OK) {
685 go_to (dialog->get ());
691 FilmViewer::jump_to_selected_clicked ()
693 Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
697 FilmViewer::seek (DCPTime t, bool accurate)
703 bool const was_running = stop ();
705 _butler->seek (t, accurate);
706 _last_seek_accurate = accurate;
715 FilmViewer::config_changed (Config::Property p)
717 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
721 if (_audio.isStreamOpen ()) {
722 _audio.closeStream ();
725 if (Config::instance()->sound()) {
727 if (Config::instance()->sound_output()) {
728 while (st < _audio.getDeviceCount()) {
729 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
734 if (st == _audio.getDeviceCount()) {
735 st = _audio.getDefaultOutputDevice();
738 st = _audio.getDefaultOutputDevice();
741 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
745 RtAudio::StreamParameters sp;
747 sp.nChannels = _audio_channels;
750 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
751 #ifdef DCPOMATIC_USE_RTERROR
752 } catch (RtError& e) {
754 } catch (RtAudioError& e) {
758 wxString::Format (_("Could not set up audio output (%s). There will be no audio during the preview."), e.what())
769 FilmViewer::time () const
771 if (_audio.isStreamRunning ()) {
772 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
773 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
776 return _video_position;
780 FilmViewer::audio_callback (void* out_p, unsigned int frames)
782 _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
784 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
786 _latency_history.push_back (_audio.getStreamLatency ());
787 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
788 _latency_history.pop_front ();
796 FilmViewer::average_latency () const
798 boost::mutex::scoped_lock lm (_latency_history_mutex);
799 if (_latency_history.empty()) {
804 BOOST_FOREACH (Frame i, _latency_history) {
808 return total / _latency_history.size();
812 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
814 _player->set_dcp_decode_reduction (reduction);
818 FilmViewer::one_video_frame () const
820 return DCPTime::from_frames (1, _film->video_frame_rate());