2 Copyright (C) 2012-2019 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"
46 #include "lib/compose.hpp"
47 #include "lib/dcpomatic_log.h"
48 #include "lib/text_content.h"
50 #include <libavutil/pixfmt.h>
52 #include <dcp/exceptions.h>
53 #include <wx/tglbtn.h>
66 using boost::shared_ptr;
67 using boost::dynamic_pointer_cast;
68 using boost::weak_ptr;
69 using boost::optional;
74 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
76 return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
79 FilmViewer::FilmViewer (wxWindow* p)
80 : _panel (new wxPanel (p))
81 , _coalesce_player_changes (false)
82 , _audio (DCPOMATIC_RTAUDIO_API)
84 , _audio_block_size (1024)
86 , _latency_history_count (0)
88 , _closed_captions_dialog (new ClosedCaptionsDialog(p, this))
89 , _outline_content (false)
92 #ifdef DCPOMATIC_VARIANT_SWAROOP
93 , _in_watermark (false)
94 , _background_image (false)
98 _panel->SetDoubleBuffered (true);
101 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
102 _panel->SetBackgroundColour (*wxBLACK);
104 _panel->Bind (wxEVT_PAINT, boost::bind (&FilmViewer::paint_panel, this));
105 _panel->Bind (wxEVT_SIZE, boost::bind (&FilmViewer::panel_sized, this, _1));
106 _timer.Bind (wxEVT_TIMER, boost::bind (&FilmViewer::timer, this));
108 set_film (shared_ptr<Film> ());
110 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
111 config_changed (Config::SOUND_OUTPUT);
114 FilmViewer::~FilmViewer ()
120 FilmViewer::set_film (shared_ptr<Film> film)
127 _video_position = DCPTime ();
128 _player_video.first.reset ();
129 _player_video.second = DCPTime ();
132 _closed_captions_dialog->clear ();
143 _player.reset (new Player (_film, _film->playlist ()));
144 _player->set_fast ();
145 if (_dcp_decode_reduction) {
146 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
148 } catch (bad_alloc &) {
149 error_dialog (_panel, _("There is not enough free memory to do that."));
154 _player->set_always_burn_open_subtitles ();
155 _player->set_play_referenced ();
157 _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
158 _film->ContentChange.connect (boost::bind(&FilmViewer::content_change, this, _1, _3));
159 _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
161 /* Keep about 1 second's worth of history samples */
162 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
164 _closed_captions_dialog->update_tracks (_film);
173 FilmViewer::recreate_butler ()
175 bool const was_running = stop ();
182 AudioMapping map = AudioMapping (_film->audio_channels(), _audio_channels);
184 if (_audio_channels != 2 || _film->audio_channels() < 3) {
185 for (int i = 0; i < min (_film->audio_channels(), _audio_channels); ++i) {
189 /* Special case: stereo output, at least 3 channel input.
190 Map so that Lt = L(-3dB) + Ls(-3dB) + C(-6dB) + Lfe(-10dB)
191 Rt = R(-3dB) + Rs(-3dB) + C(-6dB) + Lfe(-10dB)
193 if (_film->audio_channels() > 0) {
194 map.set (dcp::LEFT, 0, 1 / sqrt(2)); // L -> Lt
196 if (_film->audio_channels() > 1) {
197 map.set (dcp::RIGHT, 1, 1 / sqrt(2)); // R -> Rt
199 if (_film->audio_channels() > 2) {
200 map.set (dcp::CENTRE, 0, 1 / 2.0); // C -> Lt
201 map.set (dcp::CENTRE, 1, 1 / 2.0); // C -> Rt
203 if (_film->audio_channels() > 3) {
204 map.set (dcp::LFE, 0, 1 / sqrt(10)); // Lfe -> Lt
205 map.set (dcp::LFE, 1, 1 / sqrt(10)); // Lfe -> Rt
207 if (_film->audio_channels() > 4) {
208 map.set (dcp::LS, 0, 1 / sqrt(2)); // Ls -> Lt
210 if (_film->audio_channels() > 5) {
211 map.set (dcp::RS, 1, 1 / sqrt(2)); // Rs -> Rt
215 _butler.reset (new Butler(_player, map, _audio_channels, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
216 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
217 _butler->disable_audio ();
220 _closed_captions_dialog->set_butler (_butler);
228 FilmViewer::refresh_panel ()
237 DCPOMATIC_ASSERT (_butler);
241 _player_video = _butler->get_video (&e);
242 if (!_player_video.first && e == Butler::AGAIN) {
243 signal_manager->when_idle (boost::bind(&FilmViewer::get, this));
247 _player_video.first &&
249 _eyes != _player_video.first->eyes() &&
250 _player_video.first->eyes() != EYES_BOTH
255 } catch (DecodeError& e) {
256 error_dialog (_panel, e.what());
259 display_player_video ();
263 FilmViewer::display_player_video ()
265 if (!_player_video.first) {
271 if (_playing && (time() - _player_video.second) > one_video_frame()) {
272 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
273 part if this frame is J2K).
275 _video_position = _player_video.second;
280 /* In an ideal world, what we would do here is:
282 * 1. convert to XYZ exactly as we do in the DCP creation path.
283 * 2. convert back to RGB for the preview display, compensating
284 * for the monitor etc. etc.
286 * but this is inefficient if the source is RGB. Since we don't
287 * (currently) care too much about the precise accuracy of the preview's
288 * colour mapping (and we care more about its speed) we try to short-
289 * circuit this "ideal" situation in some cases.
291 * The content's specified colour conversion indicates the colourspace
292 * which the content is in (according to the user).
294 * PlayerVideo::image (bound to PlayerVideo::force) will take the source
295 * image and convert it (from whatever the user has said it is) to RGB.
298 _frame = _player_video.first->image (bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true);
300 ImageChanged (_player_video.first);
302 _video_position = _player_video.second;
303 _inter_position = _player_video.first->inter_position ();
304 _inter_size = _player_video.first->inter_size ();
308 _closed_captions_dialog->update (time());
314 if (!_film || !_playing) {
320 DCPTime const next = _video_position + one_video_frame();
322 if (next >= _film->length()) {
328 LOG_DEBUG_PLAYER("%1 -> %2; delay %3", next.seconds(), time().seconds(), max((next.seconds() - time().seconds()) * 1000, 1.0));
329 _timer.Start (max ((next.seconds() - time().seconds()) * 1000, 1.0), wxTIMER_ONE_SHOT);
337 #ifdef DCPOMATIC_VARIANT_SWAROOP
338 FilmViewer::maybe_draw_background_image (wxPaintDC& dc)
340 optional<boost::filesystem::path> bg = Config::instance()->player_background_image();
342 wxImage image (std_to_wx(bg->string()));
343 wxBitmap bitmap (image);
344 dc.DrawBitmap (bitmap, max(0, (_panel_size.width - image.GetSize().GetWidth()) / 2), max(0, (_panel_size.height - image.GetSize().GetHeight()) / 2));
351 FilmViewer::maybe_draw_background_image (wxPaintDC &)
358 FilmViewer::paint_panel ()
360 wxPaintDC dc (_panel);
362 #ifdef DCPOMATIC_VARIANT_SWAROOP
363 if (_background_image) {
365 maybe_draw_background_image (dc);
370 if (!_out_size.width || !_out_size.height || !_film || !_frame || _out_size != _frame->size()) {
374 wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
375 wxBitmap frame_bitmap (frame);
376 dc.DrawBitmap (frame_bitmap, 0, max(0, (_panel_size.height - _out_size.height) / 2));
378 #ifdef DCPOMATIC_VARIANT_SWAROOP
379 DCPTime const period = DCPTime::from_seconds(Config::instance()->player_watermark_period() * 60);
380 int64_t n = _video_position.get() / period.get();
381 DCPTime from(n * period.get());
382 DCPTime to = from + DCPTime::from_seconds(Config::instance()->player_watermark_duration() / 1000.0);
383 if (from <= _video_position && _video_position <= to) {
384 if (!_in_watermark) {
385 _in_watermark = true;
386 _watermark_x = rand() % _panel_size.width;
387 _watermark_y = rand() % _panel_size.height;
389 dc.SetTextForeground(*wxWHITE);
390 string wm = Config::instance()->player_watermark_theatre();
391 boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
392 wm += "\n" + boost::posix_time::to_iso_extended_string(t);
393 dc.DrawText(std_to_wx(wm), _watermark_x, _watermark_y);
395 _in_watermark = false;
400 if (_out_size.width < _panel_size.width) {
401 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
402 wxPen p (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
403 wxBrush b (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
406 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
409 if (_out_size.height < _panel_size.height) {
410 wxPen p (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
411 wxBrush b (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
414 int const gap = (_panel_size.height - _out_size.height) / 2;
415 dc.DrawRectangle (0, 0, _panel_size.width, gap);
416 dc.DrawRectangle (0, gap + _out_size.height + 1, _panel_size.width, gap + 1);
419 if (_outline_content) {
420 wxPen p (wxColour (255, 0, 0), 2);
422 dc.SetBrush (*wxTRANSPARENT_BRUSH);
423 dc.DrawRectangle (_inter_position.x, _inter_position.y + (_panel_size.height - _out_size.height) / 2, _inter_size.width, _inter_size.height);
428 FilmViewer::set_outline_content (bool o)
430 _outline_content = o;
435 FilmViewer::set_eyes (Eyes e)
442 FilmViewer::panel_sized (wxSizeEvent& ev)
444 _panel_size.width = ev.GetSize().GetWidth();
445 _panel_size.height = ev.GetSize().GetHeight();
448 if (!quick_refresh()) {
455 FilmViewer::calculate_sizes ()
457 if (!_film || !_player) {
461 Ratio const * container = _film->container ();
463 float const panel_ratio = _panel_size.ratio ();
464 float const film_ratio = container ? container->ratio () : 1.78;
466 if (panel_ratio < film_ratio) {
467 /* panel is less widscreen than the film; clamp width */
468 _out_size.width = _panel_size.width;
469 _out_size.height = lrintf (_out_size.width / film_ratio);
471 /* panel is more widescreen than the film; clamp height */
472 _out_size.height = _panel_size.height;
473 _out_size.width = lrintf (_out_size.height * film_ratio);
476 /* Catch silly values */
477 _out_size.width = max (64, _out_size.width);
478 _out_size.height = max (64, _out_size.height);
480 _player->set_video_container_size (_out_size);
490 optional<bool> v = PlaybackPermitted ();
492 /* Computer says no */
496 if (_audio.isStreamOpen()) {
497 _audio.setStreamTime (_video_position.seconds());
498 _audio.startStream ();
504 Started (position());
510 if (_audio.isStreamRunning()) {
511 /* stop stream and discard any remaining queued samples */
512 _audio.abortStream ();
520 Stopped (position());
525 FilmViewer::player_change (ChangeType type, int property, bool frequent)
527 if (type != CHANGE_TYPE_DONE || frequent) {
531 if (_coalesce_player_changes) {
532 _pending_player_changes.push_back (property);
537 bool refreshed = false;
539 property == VideoContentProperty::CROP ||
540 property == VideoContentProperty::SCALE ||
541 property == VideoContentProperty::FADE_IN ||
542 property == VideoContentProperty::FADE_OUT ||
543 property == VideoContentProperty::COLOUR_CONVERSION ||
544 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
545 property == PlayerProperty::FILM_CONTAINER
547 refreshed = quick_refresh ();
557 FilmViewer::film_change (ChangeType type, Film::Property p)
559 if (type == CHANGE_TYPE_DONE && p == Film::AUDIO_CHANNELS) {
561 } else if (p == Film::CONTENT) {
562 _closed_captions_dialog->update_tracks (_film);
566 /** Re-get the current frame slowly by seeking */
568 FilmViewer::slow_refresh ()
570 seek (_video_position, true);
573 /** Try to re-get the current frame quickly by resetting the metadata
574 * in the PlayerVideo that we used last time.
575 * @return true if this was possible, false if not.
578 FilmViewer::quick_refresh ()
580 if (!_player_video.first) {
584 if (!_player_video.first->reset_metadata (_film, _player->video_container_size(), _film->frame_size())) {
588 display_player_video ();
593 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
595 optional<DCPTime> dt = _player->content_time_to_dcp (content, t);
597 seek (*dt, accurate);
602 FilmViewer::set_coalesce_player_changes (bool c)
604 _coalesce_player_changes = c;
607 BOOST_FOREACH (int i, _pending_player_changes) {
608 player_change (CHANGE_TYPE_DONE, i, false);
610 _pending_player_changes.clear ();
615 FilmViewer::seek (DCPTime t, bool accurate)
621 if (t < DCPTime ()) {
625 if (t >= _film->length ()) {
626 t = _film->length ();
629 bool const was_running = stop ();
631 _closed_captions_dialog->clear ();
632 _butler->seek (t, accurate);
643 FilmViewer::config_changed (Config::Property p)
645 #ifdef DCPOMATIC_VARIANT_SWAROOP
646 if (p == Config::PLAYER_BACKGROUND_IMAGE) {
652 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
656 if (_audio.isStreamOpen ()) {
657 _audio.closeStream ();
660 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
662 if (Config::instance()->sound_output()) {
663 while (st < _audio.getDeviceCount()) {
664 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
669 if (st == _audio.getDeviceCount()) {
670 st = _audio.getDefaultOutputDevice();
673 st = _audio.getDefaultOutputDevice();
676 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
678 RtAudio::StreamParameters sp;
680 sp.nChannels = _audio_channels;
683 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
684 #ifdef DCPOMATIC_USE_RTERROR
685 } catch (RtError& e) {
687 } catch (RtAudioError& e) {
691 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
703 FilmViewer::uncorrected_time () const
705 if (_audio.isStreamRunning ()) {
706 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
709 return _video_position;
713 FilmViewer::time () const
715 if (_audio.isStreamRunning ()) {
716 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
717 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
720 return _video_position;
724 FilmViewer::audio_callback (void* out_p, unsigned int frames)
727 optional<DCPTime> t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
728 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
729 /* There was an underrun or this audio is on time; carry on */
732 /* The audio we just got was (very) late; drop it and get some more. */
735 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
737 _latency_history.push_back (_audio.getStreamLatency ());
738 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
739 _latency_history.pop_front ();
747 FilmViewer::average_latency () const
749 boost::mutex::scoped_lock lm (_latency_history_mutex);
750 if (_latency_history.empty()) {
755 BOOST_FOREACH (Frame i, _latency_history) {
759 return total / _latency_history.size();
763 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
765 _dcp_decode_reduction = reduction;
767 _player->set_dcp_decode_reduction (reduction);
772 FilmViewer::dcp_decode_reduction () const
774 return _dcp_decode_reduction;
778 FilmViewer::one_video_frame () const
780 return DCPTime::from_frames (1, _film->video_frame_rate());
783 /** Open a dialog box showing our film's closed captions */
785 FilmViewer::show_closed_captions ()
787 _closed_captions_dialog->Show();
791 FilmViewer::seek_by (DCPTime by, bool accurate)
793 seek (_video_position + by, accurate);
797 FilmViewer::set_pad_black (bool p)
803 FilmViewer::content_change (ChangeType type, int property)
805 if (type != CHANGE_TYPE_DONE) {
809 if (property == TextContentProperty::USE || property == TextContentProperty::TYPE || property == TextContentProperty::DCP_TRACK) {
810 _closed_captions_dialog->update_tracks (_film);