2 Copyright (C) 2019-2021 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/>.
22 #include "closed_captions_dialog.h"
23 #include "film_viewer.h"
24 #include "simple_video_view.h"
26 #include "lib/butler.h"
27 #include "lib/dcpomatic_log.h"
28 #include "lib/image.h"
31 #include <boost/bind/bind.hpp>
35 using std::shared_ptr;
37 using boost::optional;
38 #if BOOST_VERSION >= 106100
39 using namespace boost::placeholders;
41 using namespace dcpomatic;
44 SimpleVideoView::SimpleVideoView (FilmViewer* viewer, wxWindow* parent)
47 _panel = new wxPanel (parent);
50 _panel->SetDoubleBuffered (true);
53 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
54 _panel->SetBackgroundColour (*wxBLACK);
56 _panel->Bind (wxEVT_PAINT, boost::bind (&SimpleVideoView::paint, this));
57 _panel->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
59 _timer.Bind (wxEVT_TIMER, boost::bind(&SimpleVideoView::timer, this));
64 SimpleVideoView::paint ()
66 _state_timer.set("paint-panel");
67 wxPaintDC dc (_panel);
69 auto const panel_size = _panel->GetSize ();
75 DCPOMATIC_ASSERT (_image->alignment() == Image::Alignment::COMPACT);
76 out_size = _image->size();
77 wxImage frame (out_size.width, out_size.height, _image->data()[0], true);
78 wxBitmap frame_bitmap (frame);
79 dc.DrawBitmap (frame_bitmap, 0, max(0, (panel_size.GetHeight() - out_size.height) / 2));
82 auto pad = pad_colour();
84 if (out_size.width < panel_size.GetWidth()) {
89 dc.DrawRectangle (out_size.width, 0, panel_size.GetWidth() - out_size.width, panel_size.GetHeight());
92 if (out_size.height < panel_size.GetHeight()) {
97 int const gap = (panel_size.GetHeight() - out_size.height) / 2;
98 dc.DrawRectangle (0, 0, panel_size.GetWidth(), gap);
99 dc.DrawRectangle (0, gap + out_size.height + 1, panel_size.GetWidth(), gap + 1);
102 if (_viewer->outline_content()) {
103 wxPen p (outline_content_colour(), 2);
105 dc.SetBrush (*wxTRANSPARENT_BRUSH);
106 dc.DrawRectangle (_inter_position.x, _inter_position.y + (panel_size.GetHeight() - out_size.height) / 2, _inter_size.width, _inter_size.height);
109 auto subs = _viewer->outline_subtitles();
111 wxPen p (outline_subtitles_colour(), 2);
113 dc.SetBrush (*wxTRANSPARENT_BRUSH);
114 dc.DrawRectangle (subs->x * out_size.width, subs->y * out_size.height, subs->width * out_size.width, subs->height * out_size.height);
117 _state_timer.unset();
122 SimpleVideoView::refresh_panel ()
124 _state_timer.set ("refresh-panel");
127 _state_timer.unset ();
132 SimpleVideoView::timer ()
134 if (!_viewer->playing()) {
138 display_next_frame (false);
139 auto const next = position() + _viewer->one_video_frame();
141 if (next >= length()) {
142 _viewer->finished ();
146 LOG_DEBUG_VIDEO_VIEW("%1 -> %2; delay %3", next.seconds(), _viewer->time().seconds(), max((next.seconds() - _viewer->time().seconds()) * 1000, 1.0));
147 _timer.Start (max(1, time_until_next_frame().get_value_or(0)), wxTIMER_ONE_SHOT);
149 if (_viewer->butler()) {
150 _viewer->butler()->rethrow ();
156 SimpleVideoView::start ()
163 /** Try to get a frame from the butler and display it.
164 * @param non_blocking true to return false quickly if no video is available quickly (i.e. we are waiting for the butler).
165 * false to ask the butler to block until it has video (unless it is suspended).
166 * @return true on success, false if we did nothing because it would have taken too long.
168 VideoView::NextFrameResult
169 SimpleVideoView::display_next_frame (bool non_blocking)
171 auto const r = get_next_frame (non_blocking);
179 _viewer->butler()->rethrow ();
180 } catch (DecodeError& e) {
181 error_dialog (get(), e.what());
189 SimpleVideoView::update ()
191 if (!player_video().first) {
197 if (_viewer->playing() && (_viewer->time() - player_video().second) > one_video_frame()) {
198 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
199 part if this frame is J2K).
205 /* In an ideal world, what we would do here is:
207 * 1. convert to XYZ exactly as we do in the DCP creation path.
208 * 2. convert back to RGB for the preview display, compensating
209 * for the monitor etc. etc.
211 * but this is inefficient if the source is RGB. Since we don't
212 * (currently) care too much about the precise accuracy of the preview's
213 * colour mapping (and we care more about its speed) we try to short-
214 * circuit this "ideal" situation in some cases.
216 * The content's specified colour conversion indicates the colourspace
217 * which the content is in (according to the user).
219 * PlayerVideo::image (bound to PlayerVideo::force) will take the source
220 * image and convert it (from whatever the user has said it is) to RGB.
223 _state_timer.set ("get image");
225 _image = player_video().first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, true);
227 _state_timer.set ("ImageChanged");
228 _viewer->image_changed (player_video().first);
229 _state_timer.unset ();
231 _inter_position = player_video().first->inter_position ();
232 _inter_size = player_video().first->inter_size ();