2 Copyright (C) 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 #include "simple_video_view.h"
22 #include "film_viewer.h"
24 #include "closed_captions_dialog.h"
25 #include "lib/image.h"
26 #include "lib/dcpomatic_log.h"
27 #include "lib/butler.h"
30 #include <boost/bind.hpp>
34 using boost::optional;
35 using boost::shared_ptr;
36 #if BOOST_VERSION >= 106100
37 using namespace boost::placeholders;
39 using namespace dcpomatic;
42 SimpleVideoView::SimpleVideoView (FilmViewer* viewer, wxWindow* parent)
45 _panel = new wxPanel (parent);
48 _panel->SetDoubleBuffered (true);
51 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
52 _panel->SetBackgroundColour (*wxBLACK);
54 _panel->Bind (wxEVT_PAINT, boost::bind (&SimpleVideoView::paint, this));
55 _panel->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
57 _timer.Bind (wxEVT_TIMER, boost::bind(&SimpleVideoView::timer, this));
61 SimpleVideoView::paint ()
63 _state_timer.set("paint-panel");
64 wxPaintDC dc (_panel);
66 dcp::Size const out_size = _viewer->out_size ();
67 wxSize const panel_size = _panel->GetSize ();
69 if (!out_size.width || !out_size.height || !_image || out_size != _image->size()) {
73 wxImage frame (out_size.width, out_size.height, _image->data()[0], true);
74 wxBitmap frame_bitmap (frame);
75 dc.DrawBitmap (frame_bitmap, 0, max(0, (panel_size.GetHeight() - out_size.height) / 2));
78 if (out_size.width < panel_size.GetWidth()) {
79 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
80 wxPen p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
81 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
84 dc.DrawRectangle (out_size.width, 0, panel_size.GetWidth() - out_size.width, panel_size.GetHeight());
87 if (out_size.height < panel_size.GetHeight()) {
88 wxPen p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
89 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
92 int const gap = (panel_size.GetHeight() - out_size.height) / 2;
93 dc.DrawRectangle (0, 0, panel_size.GetWidth(), gap);
94 dc.DrawRectangle (0, gap + out_size.height + 1, panel_size.GetWidth(), gap + 1);
97 if (_viewer->outline_content()) {
98 wxPen p (wxColour (255, 0, 0), 2);
100 dc.SetBrush (*wxTRANSPARENT_BRUSH);
101 dc.DrawRectangle (_inter_position.x, _inter_position.y + (panel_size.GetHeight() - out_size.height) / 2, _inter_size.width, _inter_size.height);
104 optional<dcpomatic::Rect<double> > subs = _viewer->outline_subtitles();
106 wxPen p (wxColour(0, 255, 0), 2);
108 dc.SetBrush (*wxTRANSPARENT_BRUSH);
109 dc.DrawRectangle (subs->x * out_size.width, subs->y * out_size.height, subs->width * out_size.width, subs->height * out_size.height);
112 _state_timer.unset();
116 SimpleVideoView::refresh_panel ()
118 _state_timer.set ("refresh-panel");
121 _state_timer.unset ();
125 SimpleVideoView::timer ()
127 if (!_viewer->playing()) {
131 display_next_frame (false);
132 DCPTime const next = position() + _viewer->one_video_frame();
134 if (next >= length()) {
135 _viewer->finished ();
139 LOG_DEBUG_VIDEO_VIEW("%1 -> %2; delay %3", next.seconds(), _viewer->time().seconds(), max((next.seconds() - _viewer->time().seconds()) * 1000, 1.0));
140 _timer.Start (max(1, time_until_next_frame().get_value_or(0)), wxTIMER_ONE_SHOT);
142 if (_viewer->butler()) {
143 _viewer->butler()->rethrow ();
148 SimpleVideoView::start ()
154 /** Try to get a frame from the butler and display it.
155 * @param non_blocking true to return false quickly if no video is available quickly (i.e. we are waiting for the butler).
156 * false to ask the butler to block until it has video (unless it is suspended).
157 * @return true on success, false if we did nothing because it would have taken too long.
160 SimpleVideoView::display_next_frame (bool non_blocking)
162 bool r = get_next_frame (non_blocking);
165 /* No video available; return saying we failed */
168 /* Player was suspended; come back later */
169 signal_manager->when_idle (boost::bind(&SimpleVideoView::display_next_frame, this, false));
177 _viewer->butler()->rethrow ();
178 } catch (DecodeError& e) {
179 error_dialog (get(), e.what());
186 SimpleVideoView::update ()
188 if (!player_video().first) {
189 set_image (shared_ptr<Image>());
194 if (_viewer->playing() && (_viewer->time() - player_video().second) > one_video_frame()) {
195 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
196 part if this frame is J2K).
202 /* In an ideal world, what we would do here is:
204 * 1. convert to XYZ exactly as we do in the DCP creation path.
205 * 2. convert back to RGB for the preview display, compensating
206 * for the monitor etc. etc.
208 * but this is inefficient if the source is RGB. Since we don't
209 * (currently) care too much about the precise accuracy of the preview's
210 * colour mapping (and we care more about its speed) we try to short-
211 * circuit this "ideal" situation in some cases.
213 * The content's specified colour conversion indicates the colourspace
214 * which the content is in (according to the user).
216 * PlayerVideo::image (bound to PlayerVideo::force) will take the source
217 * image and convert it (from whatever the user has said it is) to RGB.
220 _state_timer.set ("get image");
223 player_video().first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VIDEO_RANGE_FULL, false, true)
226 _state_timer.set ("ImageChanged");
227 _viewer->image_changed (player_video().first);
228 _state_timer.unset ();
230 _inter_position = player_video().first->inter_position ();
231 _inter_size = player_video().first->inter_size ();