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)
77 , _panel (new wxPanel (this))
78 , _outline_content (new wxCheckBox (this, wxID_ANY, _("Outline content")))
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 (new wxCheckBox (this, wxID_ANY, _("Jump to selected content")))
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)
97 _panel->SetDoubleBuffered (true);
100 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
102 _v_sizer = new wxBoxSizer (wxVERTICAL);
105 _v_sizer->Add (_panel, 1, wxEXPAND);
107 wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
108 view_options->Add (_outline_content, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
109 view_options->Add (_left_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
110 view_options->Add (_right_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
111 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
112 _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
114 wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
116 wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
117 time_sizer->Add (_frame_number, 0, wxEXPAND);
118 time_sizer->Add (_timecode, 0, wxEXPAND);
120 h_sizer->Add (_back_button, 0, wxALL, 2);
121 h_sizer->Add (time_sizer, 0, wxEXPAND);
122 h_sizer->Add (_forward_button, 0, wxALL, 2);
123 h_sizer->Add (_play_button, 0, wxEXPAND);
124 h_sizer->Add (_slider, 1, wxEXPAND);
126 _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
128 _frame_number->SetMinSize (wxSize (84, -1));
129 _back_button->SetMinSize (wxSize (32, -1));
130 _forward_button->SetMinSize (wxSize (32, -1));
132 _panel->Bind (wxEVT_PAINT, boost::bind (&FilmViewer::paint_panel, this));
133 _panel->Bind (wxEVT_SIZE, boost::bind (&FilmViewer::panel_sized, this, _1));
134 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::refresh_panel, this));
135 _left_eye->Bind (wxEVT_RADIOBUTTON, boost::bind (&FilmViewer::refresh, this));
136 _right_eye->Bind (wxEVT_RADIOBUTTON, boost::bind (&FilmViewer::refresh, this));
137 _slider->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&FilmViewer::slider_moved, this));
138 _slider->Bind (wxEVT_SCROLL_PAGEUP, boost::bind (&FilmViewer::slider_moved, this));
139 _slider->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind (&FilmViewer::slider_moved, this));
140 _play_button->Bind (wxEVT_TOGGLEBUTTON, boost::bind (&FilmViewer::play_clicked, this));
141 _timer.Bind (wxEVT_TIMER, boost::bind (&FilmViewer::timer, this));
142 _back_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::back_clicked, this, _1));
143 _forward_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::forward_clicked, this, _1));
144 _frame_number->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::frame_number_clicked, this));
145 _timecode->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::timecode_clicked, this));
146 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::jump_to_selected_clicked, this));
148 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
150 set_film (shared_ptr<Film> ());
152 JobManager::instance()->ActiveJobsChanged.connect (
153 bind (&FilmViewer::active_jobs_changed, this, _2)
156 setup_sensitivity ();
158 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
159 config_changed (Config::SOUND_OUTPUT);
162 FilmViewer::~FilmViewer ()
168 FilmViewer::set_film (shared_ptr<Film> film)
178 update_position_slider ();
179 update_position_label ();
186 _player.reset (new Player (_film, _film->playlist ()));
187 _player->set_fast ();
188 } catch (bad_alloc) {
189 error_dialog (this, _("There is not enough free memory to do that."));
194 /* Always burn in subtitles, even if content is set not to, otherwise we won't see them
197 _player->set_always_burn_subtitles (true);
198 _player->set_play_referenced ();
200 _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
201 _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
208 setup_sensitivity ();
212 FilmViewer::recreate_butler ()
214 bool const was_running = stop ();
221 AudioMapping map = AudioMapping (_film->audio_channels(), _audio_channels);
223 if (_audio_channels != 2 || _film->audio_channels() < 3) {
224 for (int i = 0; i < min (_film->audio_channels(), _audio_channels); ++i) {
228 /* Special case: stereo output, at least 3 channel input, map L+R to L/R and
229 C to both, all 3dB down.
231 map.set (0, 0, 1 / sqrt(2)); // L -> L
232 map.set (1, 1, 1 / sqrt(2)); // R -> R
233 map.set (2, 0, 1 / sqrt(2)); // C -> L
234 map.set (2, 1, 1 / sqrt(2)); // C -> R
237 _butler.reset (new Butler (_film, _player, map, _audio_channels));
245 FilmViewer::refresh_panel ()
254 pair<shared_ptr<PlayerVideo>, DCPTime> video;
256 video = _butler->get_video ();
257 } while (_film->three_d() && ((_left_eye->GetValue() && video.first->eyes() == EYES_RIGHT) || (_right_eye->GetValue() && video.first->eyes() == EYES_LEFT)));
263 /* In an ideal world, what we would do here is:
265 * 1. convert to XYZ exactly as we do in the DCP creation path.
266 * 2. convert back to RGB for the preview display, compensating
267 * for the monitor etc. etc.
269 * but this is inefficient if the source is RGB. Since we don't
270 * (currently) care too much about the precise accuracy of the preview's
271 * colour mapping (and we care more about its speed) we try to short-
272 * circuit this "ideal" situation in some cases.
274 * The content's specified colour conversion indicates the colourspace
275 * which the content is in (according to the user).
277 * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
278 * image and convert it (from whatever the user has said it is) to RGB.
281 _frame = video.first->image (
282 bind (&Log::dcp_log, _film->log().get(), _1, _2),
283 bind (&PlayerVideo::always_rgb, _1),
287 ImageChanged (video.first);
289 _video_position = video.second;
290 _inter_position = video.first->inter_position ();
291 _inter_size = video.first->inter_size ();
303 if (_audio.isStreamRunning ()) {
304 DCPTime const now = time().ceil (_film->video_frame_rate ());
306 update_position_label ();
307 update_position_slider ();
308 DCPTime const next = now + DCPTime::from_frames (1, _film->video_frame_rate ());
309 _timer.Start (max ((next.seconds() - time().seconds()) * 1000, 0.0), wxTIMER_ONE_SHOT);
318 FilmViewer::paint_panel ()
320 wxPaintDC dc (_panel);
322 if (!_frame || !_film || !_out_size.width || !_out_size.height) {
327 wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
328 wxBitmap frame_bitmap (frame);
329 dc.DrawBitmap (frame_bitmap, 0, 0);
331 if (_out_size.width < _panel_size.width) {
332 wxPen p (GetBackgroundColour ());
333 wxBrush b (GetBackgroundColour ());
336 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
339 if (_out_size.height < _panel_size.height) {
340 wxPen p (GetBackgroundColour ());
341 wxBrush b (GetBackgroundColour ());
344 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
347 if (_outline_content->GetValue ()) {
348 wxPen p (wxColour (255, 0, 0), 2);
350 dc.SetBrush (*wxTRANSPARENT_BRUSH);
351 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
356 FilmViewer::slider_moved ()
362 DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
363 /* Ensure that we hit the end of the film at the end of the slider */
364 if (t >= _film->length ()) {
365 t = _film->length() - DCPTime::from_frames (1, _film->video_frame_rate ());
368 update_position_label ();
372 FilmViewer::panel_sized (wxSizeEvent& ev)
374 _panel_size.width = ev.GetSize().GetWidth();
375 _panel_size.height = ev.GetSize().GetHeight();
379 update_position_label ();
380 update_position_slider ();
384 FilmViewer::calculate_sizes ()
390 Ratio const * container = _film->container ();
392 float const panel_ratio = _panel_size.ratio ();
393 float const film_ratio = container ? container->ratio () : 1.78;
395 if (panel_ratio < film_ratio) {
396 /* panel is less widscreen than the film; clamp width */
397 _out_size.width = _panel_size.width;
398 _out_size.height = lrintf (_out_size.width / film_ratio);
400 /* panel is more widescreen than the film; clamp height */
401 _out_size.height = _panel_size.height;
402 _out_size.width = lrintf (_out_size.height * film_ratio);
405 /* Catch silly values */
406 _out_size.width = max (64, _out_size.width);
407 _out_size.height = max (64, _out_size.height);
409 _player->set_video_container_size (_out_size);
413 FilmViewer::play_clicked ()
419 FilmViewer::check_play_state ()
421 if (!_film || _film->video_frame_rate() == 0) {
425 if (_play_button->GetValue()) {
435 if (_audio.isStreamOpen()) {
436 _audio.setStreamTime (_video_position.seconds());
437 _audio.startStream ();
441 _timer.Start (0, wxTIMER_ONE_SHOT);
447 if (_audio.isStreamRunning()) {
448 /* stop stream and discard any remainig queued samples */
449 _audio.abortStream ();
461 FilmViewer::update_position_slider ()
464 _slider->SetValue (0);
468 DCPTime const len = _film->length ();
471 int const new_slider_position = 4096 * _video_position.get() / len.get();
472 if (new_slider_position != _slider->GetValue()) {
473 _slider->SetValue (new_slider_position);
479 FilmViewer::update_position_label ()
482 _frame_number->SetLabel ("0");
483 _timecode->SetLabel ("0:0:0.0");
487 double const fps = _film->video_frame_rate ();
488 /* Count frame number from 1 ... not sure if this is the best idea */
489 _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_video_position.seconds() * fps) + 1));
490 _timecode->SetLabel (time_to_timecode (_video_position, fps));
494 FilmViewer::active_jobs_changed (optional<string> j)
496 /* examine content is the only job which stops the viewer working */
497 bool const a = !j || *j != "examine_content";
499 _play_button->Enable (a);
503 FilmViewer::nudge_amount (wxMouseEvent& ev)
505 DCPTime amount = DCPTime::from_frames (1, _film->video_frame_rate ());
507 if (ev.ShiftDown() && !ev.ControlDown()) {
508 amount = DCPTime::from_seconds (1);
509 } else if (!ev.ShiftDown() && ev.ControlDown()) {
510 amount = DCPTime::from_seconds (10);
511 } else if (ev.ShiftDown() && ev.ControlDown()) {
512 amount = DCPTime::from_seconds (60);
519 FilmViewer::go_to (DCPTime t)
521 if (t < DCPTime ()) {
525 if (t >= _film->length ()) {
526 t = _film->length ();
530 update_position_label ();
531 update_position_slider ();
535 FilmViewer::back_clicked (wxMouseEvent& ev)
537 go_to (_video_position - nudge_amount (ev));
542 FilmViewer::forward_clicked (wxMouseEvent& ev)
544 go_to (_video_position + nudge_amount (ev));
549 FilmViewer::player_changed (bool frequent)
555 if (_coalesce_player_changes) {
556 _pending_player_change = true;
562 update_position_label ();
563 update_position_slider ();
567 FilmViewer::setup_sensitivity ()
569 bool const c = _film && !_film->content().empty ();
572 _back_button->Enable (c);
573 _forward_button->Enable (c);
574 _play_button->Enable (c);
575 _outline_content->Enable (c);
576 _frame_number->Enable (c);
577 _timecode->Enable (c);
579 _left_eye->Enable (c && _film->three_d ());
580 _right_eye->Enable (c && _film->three_d ());
584 FilmViewer::film_changed (Film::Property p)
586 if (p == Film::CONTENT || p == Film::THREE_D) {
587 setup_sensitivity ();
591 /** Re-get the current frame */
593 FilmViewer::refresh ()
595 seek (_video_position, _last_seek_accurate);
599 FilmViewer::set_position (DCPTime p)
603 update_position_label ();
604 update_position_slider ();
608 FilmViewer::set_coalesce_player_changes (bool c)
610 _coalesce_player_changes = c;
613 _pending_player_change = false;
615 if (_pending_player_change) {
616 player_changed (false);
622 FilmViewer::timecode_clicked ()
624 PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
625 if (dialog->ShowModal() == wxID_OK) {
626 go_to (dialog->get ());
632 FilmViewer::frame_number_clicked ()
634 PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
635 if (dialog->ShowModal() == wxID_OK) {
636 go_to (dialog->get ());
642 FilmViewer::jump_to_selected_clicked ()
644 Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
648 FilmViewer::seek (DCPTime t, bool accurate)
654 _butler->seek (t, accurate);
655 _last_seek_accurate = accurate;
660 FilmViewer::config_changed (Config::Property p)
662 if (p != Config::SOUND_OUTPUT) {
666 if (_audio.isStreamOpen ()) {
667 _audio.closeStream ();
671 if (Config::instance()->sound_output()) {
672 while (st < _audio.getDeviceCount()) {
673 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
678 if (st == _audio.getDeviceCount()) {
679 st = _audio.getDefaultOutputDevice();
682 st = _audio.getDefaultOutputDevice();
685 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
688 RtAudio::StreamParameters sp;
690 sp.nChannels = _audio_channels;
693 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
694 #ifdef DCPOMATIC_USE_RTERROR
695 } catch (RtError& e) {
697 } catch (RtAudioError& e) {
699 error_dialog (this, wxString::Format (_("Could not set up audio output (%s). There will be no audio during the preview."), e.what()));
704 FilmViewer::time () const
706 if (_audio.isStreamRunning ()) {
707 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ());
710 return _video_position;
714 FilmViewer::audio_callback (void* out_p, unsigned int frames)
716 _butler->get_audio (reinterpret_cast<float*> (out_p), frames);