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"
49 #include <libavutil/pixfmt.h>
51 #include <dcp/exceptions.h>
52 #include <wx/tglbtn.h>
65 using boost::shared_ptr;
66 using boost::dynamic_pointer_cast;
67 using boost::weak_ptr;
68 using boost::optional;
73 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
75 return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
78 FilmViewer::FilmViewer (wxWindow* p)
79 : _panel (new wxPanel (p))
80 , _coalesce_player_changes (false)
81 , _audio (DCPOMATIC_RTAUDIO_API)
83 , _audio_block_size (1024)
85 , _latency_history_count (0)
87 , _closed_captions_dialog (new ClosedCaptionsDialog(p))
88 , _outline_content (false)
91 #ifdef DCPOMATIC_VARIANT_SWAROOP
92 , _in_watermark (false)
93 , _background_image (false)
97 _panel->SetDoubleBuffered (true);
100 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
101 _panel->SetBackgroundColour (*wxBLACK);
103 _panel->Bind (wxEVT_PAINT, boost::bind (&FilmViewer::paint_panel, this));
104 _panel->Bind (wxEVT_SIZE, boost::bind (&FilmViewer::panel_sized, this, _1));
105 _timer.Bind (wxEVT_TIMER, boost::bind (&FilmViewer::timer, this));
107 set_film (shared_ptr<Film> ());
109 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
110 config_changed (Config::SOUND_OUTPUT);
113 FilmViewer::~FilmViewer ()
119 FilmViewer::set_film (shared_ptr<Film> film)
126 _video_position = DCPTime ();
127 _player_video.first.reset ();
128 _player_video.second = DCPTime ();
131 _closed_captions_dialog->clear ();
142 _player.reset (new Player (_film, _film->playlist ()));
143 _player->set_fast ();
144 if (_dcp_decode_reduction) {
145 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
147 } catch (bad_alloc &) {
148 error_dialog (_panel, _("There is not enough free memory to do that."));
153 _player->set_always_burn_open_subtitles ();
154 _player->set_play_referenced ();
156 _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
157 _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
159 /* Keep about 1 second's worth of history samples */
160 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
169 FilmViewer::recreate_butler ()
171 bool const was_running = stop ();
178 AudioMapping map = AudioMapping (_film->audio_channels(), _audio_channels);
180 if (_audio_channels != 2 || _film->audio_channels() < 3) {
181 for (int i = 0; i < min (_film->audio_channels(), _audio_channels); ++i) {
185 /* Special case: stereo output, at least 3 channel input.
186 Map so that Lt = L(-3dB) + Ls(-3dB) + C(-6dB) + Lfe(-10dB)
187 Rt = R(-3dB) + Rs(-3dB) + C(-6dB) + Lfe(-10dB)
189 map.set (dcp::LEFT, 0, 1 / sqrt(2)); // L -> Lt
190 map.set (dcp::RIGHT, 1, 1 / sqrt(2)); // R -> Rt
191 map.set (dcp::CENTRE, 0, 1 / 2.0); // C -> Lt
192 map.set (dcp::CENTRE, 1, 1 / 2.0); // C -> Rt
193 map.set (dcp::LFE, 0, 1 / sqrt(10)); // Lfe -> Lt
194 map.set (dcp::LFE, 1, 1 / sqrt(10)); // Lfe -> Rt
195 map.set (dcp::LS, 0, 1 / sqrt(2)); // Ls -> Lt
196 map.set (dcp::RS, 1, 1 / sqrt(2)); // Rs -> Rt
199 _butler.reset (new Butler(_player, map, _audio_channels, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
200 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
201 _butler->disable_audio ();
204 _closed_captions_dialog->set_butler (_butler);
212 FilmViewer::refresh_panel ()
221 DCPOMATIC_ASSERT (_butler);
225 _player_video = _butler->get_video (&e);
226 if (!_player_video.first && e == Butler::AGAIN) {
227 signal_manager->when_idle (boost::bind(&FilmViewer::get, this));
231 _player_video.first &&
233 _eyes != _player_video.first->eyes() &&
234 _player_video.first->eyes() != EYES_BOTH
239 display_player_video ();
243 FilmViewer::display_player_video ()
245 if (!_player_video.first) {
251 if (_playing && (time() - _player_video.second) > one_video_frame()) {
252 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
253 part if this frame is J2K).
255 _video_position = _player_video.second;
260 /* In an ideal world, what we would do here is:
262 * 1. convert to XYZ exactly as we do in the DCP creation path.
263 * 2. convert back to RGB for the preview display, compensating
264 * for the monitor etc. etc.
266 * but this is inefficient if the source is RGB. Since we don't
267 * (currently) care too much about the precise accuracy of the preview's
268 * colour mapping (and we care more about its speed) we try to short-
269 * circuit this "ideal" situation in some cases.
271 * The content's specified colour conversion indicates the colourspace
272 * which the content is in (according to the user).
274 * PlayerVideo::image (bound to PlayerVideo::force) will take the source
275 * image and convert it (from whatever the user has said it is) to RGB.
278 _frame = _player_video.first->image (bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true);
280 ImageChanged (_player_video.first);
282 _video_position = _player_video.second;
283 _inter_position = _player_video.first->inter_position ();
284 _inter_size = _player_video.first->inter_size ();
288 _closed_captions_dialog->update (time());
294 if (!_film || !_playing) {
300 DCPTime const next = _video_position + one_video_frame();
302 if (next >= _film->length()) {
308 LOG_DEBUG_PLAYER("%1 -> %2; delay %3", next.seconds(), time().seconds(), max((next.seconds() - time().seconds()) * 1000, 1.0));
309 _timer.Start (max ((next.seconds() - time().seconds()) * 1000, 1.0), wxTIMER_ONE_SHOT);
317 #ifdef DCPOMATIC_VARIANT_SWAROOP
318 FilmViewer::maybe_draw_background_image (wxPaintDC& dc)
320 optional<boost::filesystem::path> bg = Config::instance()->player_background_image();
322 wxImage image (std_to_wx(bg->string()));
323 wxBitmap bitmap (image);
324 dc.DrawBitmap (bitmap, max(0, (_panel_size.width - image.GetSize().GetWidth()) / 2), max(0, (_panel_size.height - image.GetSize().GetHeight()) / 2));
331 FilmViewer::maybe_draw_background_image (wxPaintDC &)
338 FilmViewer::paint_panel ()
340 wxPaintDC dc (_panel);
342 #ifdef DCPOMATIC_VARIANT_SWAROOP
343 if (_background_image) {
345 maybe_draw_background_image (dc);
350 if (!_out_size.width || !_out_size.height || !_film || !_frame || _out_size != _frame->size()) {
355 wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
356 wxBitmap frame_bitmap (frame);
357 dc.DrawBitmap (frame_bitmap, 0, max(0, (_panel_size.height - _out_size.height) / 2));
359 #ifdef DCPOMATIC_VARIANT_SWAROOP
360 DCPTime const period = DCPTime::from_seconds(Config::instance()->player_watermark_period() * 60);
361 int64_t n = _video_position.get() / period.get();
362 DCPTime from(n * period.get());
363 DCPTime to = from + DCPTime::from_seconds(Config::instance()->player_watermark_duration() / 1000.0);
364 if (from <= _video_position && _video_position <= to) {
365 if (!_in_watermark) {
366 _in_watermark = true;
367 _watermark_x = rand() % _panel_size.width;
368 _watermark_y = rand() % _panel_size.height;
370 dc.SetTextForeground(*wxWHITE);
371 string wm = Config::instance()->player_watermark_theatre();
372 boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
373 wm += "\n" + boost::posix_time::to_iso_extended_string(t);
374 dc.DrawText(std_to_wx(wm), _watermark_x, _watermark_y);
376 _in_watermark = false;
380 if (_out_size.width < _panel_size.width) {
381 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
382 wxPen p (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
383 wxBrush b (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
386 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
389 if (_out_size.height < _panel_size.height) {
390 wxPen p (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
391 wxBrush b (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
394 int const gap = (_panel_size.height - _out_size.height) / 2;
395 dc.DrawRectangle (0, 0, _panel_size.width, gap);
396 dc.DrawRectangle (0, gap + _out_size.height + 1, _panel_size.width, gap);
399 if (_outline_content) {
400 wxPen p (wxColour (255, 0, 0), 2);
402 dc.SetBrush (*wxTRANSPARENT_BRUSH);
403 dc.DrawRectangle (_inter_position.x, _inter_position.y + (_panel_size.height - _out_size.height) / 2, _inter_size.width, _inter_size.height);
408 FilmViewer::set_outline_content (bool o)
410 _outline_content = o;
415 FilmViewer::set_eyes (Eyes e)
422 FilmViewer::panel_sized (wxSizeEvent& ev)
424 _panel_size.width = ev.GetSize().GetWidth();
425 _panel_size.height = ev.GetSize().GetHeight();
428 if (!quick_refresh()) {
435 FilmViewer::calculate_sizes ()
437 if (!_film || !_player) {
441 Ratio const * container = _film->container ();
443 float const panel_ratio = _panel_size.ratio ();
444 float const film_ratio = container ? container->ratio () : 1.78;
446 if (panel_ratio < film_ratio) {
447 /* panel is less widscreen than the film; clamp width */
448 _out_size.width = _panel_size.width;
449 _out_size.height = lrintf (_out_size.width / film_ratio);
451 /* panel is more widescreen than the film; clamp height */
452 _out_size.height = _panel_size.height;
453 _out_size.width = lrintf (_out_size.height * film_ratio);
456 /* Catch silly values */
457 _out_size.width = max (64, _out_size.width);
458 _out_size.height = max (64, _out_size.height);
460 _player->set_video_container_size (_out_size);
470 optional<bool> v = PlaybackPermitted ();
472 /* Computer says no */
476 if (_audio.isStreamOpen()) {
477 _audio.setStreamTime (_video_position.seconds());
478 _audio.startStream ();
484 Started (position());
490 if (_audio.isStreamRunning()) {
491 /* stop stream and discard any remaining queued samples */
492 _audio.abortStream ();
500 Stopped (position());
505 FilmViewer::player_change (ChangeType type, int property, bool frequent)
507 if (type != CHANGE_TYPE_DONE || frequent) {
511 if (_coalesce_player_changes) {
512 _pending_player_changes.push_back (property);
517 bool refreshed = false;
519 property == VideoContentProperty::CROP ||
520 property == VideoContentProperty::SCALE ||
521 property == VideoContentProperty::FADE_IN ||
522 property == VideoContentProperty::FADE_OUT ||
523 property == VideoContentProperty::COLOUR_CONVERSION ||
524 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
525 property == PlayerProperty::FILM_CONTAINER
527 refreshed = quick_refresh ();
537 FilmViewer::film_change (ChangeType type, Film::Property p)
539 if (type == CHANGE_TYPE_DONE && p == Film::AUDIO_CHANNELS) {
544 /** Re-get the current frame slowly by seeking */
546 FilmViewer::slow_refresh ()
548 seek (_video_position, true);
551 /** Try to re-get the current frame quickly by resetting the metadata
552 * in the PlayerVideo that we used last time.
553 * @return true if this was possible, false if not.
556 FilmViewer::quick_refresh ()
558 if (!_player_video.first) {
562 if (!_player_video.first->reset_metadata (_film, _player->video_container_size(), _film->frame_size())) {
566 display_player_video ();
571 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
573 optional<DCPTime> dt = _player->content_time_to_dcp (content, t);
575 seek (*dt, accurate);
580 FilmViewer::set_coalesce_player_changes (bool c)
582 _coalesce_player_changes = c;
585 BOOST_FOREACH (int i, _pending_player_changes) {
586 player_change (CHANGE_TYPE_DONE, i, false);
588 _pending_player_changes.clear ();
593 FilmViewer::seek (DCPTime t, bool accurate)
599 if (t < DCPTime ()) {
603 if (t >= _film->length ()) {
604 t = _film->length ();
607 bool const was_running = stop ();
609 _closed_captions_dialog->clear ();
610 _butler->seek (t, accurate);
621 FilmViewer::config_changed (Config::Property p)
623 #ifdef DCPOMATIC_VARIANT_SWAROOP
624 if (p == Config::PLAYER_BACKGROUND_IMAGE) {
630 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
634 if (_audio.isStreamOpen ()) {
635 _audio.closeStream ();
638 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
640 if (Config::instance()->sound_output()) {
641 while (st < _audio.getDeviceCount()) {
642 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
647 if (st == _audio.getDeviceCount()) {
648 st = _audio.getDefaultOutputDevice();
651 st = _audio.getDefaultOutputDevice();
654 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
656 RtAudio::StreamParameters sp;
658 sp.nChannels = _audio_channels;
661 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
662 #ifdef DCPOMATIC_USE_RTERROR
663 } catch (RtError& e) {
665 } catch (RtAudioError& e) {
669 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
681 FilmViewer::uncorrected_time () const
683 if (_audio.isStreamRunning ()) {
684 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
687 return _video_position;
691 FilmViewer::time () const
693 if (_audio.isStreamRunning ()) {
694 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
695 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
698 return _video_position;
702 FilmViewer::audio_callback (void* out_p, unsigned int frames)
705 optional<DCPTime> t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
706 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
707 /* There was an underrun or this audio is on time; carry on */
710 /* The audio we just got was (very) late; drop it and get some more. */
713 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
715 _latency_history.push_back (_audio.getStreamLatency ());
716 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
717 _latency_history.pop_front ();
725 FilmViewer::average_latency () const
727 boost::mutex::scoped_lock lm (_latency_history_mutex);
728 if (_latency_history.empty()) {
733 BOOST_FOREACH (Frame i, _latency_history) {
737 return total / _latency_history.size();
741 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
743 _dcp_decode_reduction = reduction;
745 _player->set_dcp_decode_reduction (reduction);
750 FilmViewer::dcp_decode_reduction () const
752 return _dcp_decode_reduction;
756 FilmViewer::one_video_frame () const
758 return DCPTime::from_frames (1, _film->video_frame_rate());
761 /** Open a dialog box showing our film's closed captions */
763 FilmViewer::show_closed_captions ()
765 _closed_captions_dialog->Show();
769 FilmViewer::seek_by (DCPTime by, bool accurate)
771 seek (_video_position + by, accurate);
775 FilmViewer::set_pad_black (bool p)