Make sure we use limited ("video") range data when exporting.
[dcpomatic.git] / src / wx / simple_video_view.cc
1 /*
2     Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "simple_video_view.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "closed_captions_dialog.h"
25 #include "lib/image.h"
26 #include "lib/dcpomatic_log.h"
27 #include "lib/butler.h"
28 #include <dcp/util.h>
29 #include <wx/wx.h>
30 #include <boost/bind.hpp>
31
32 using std::max;
33 using std::string;
34 using boost::optional;
35 using boost::shared_ptr;
36 #if BOOST_VERSION >= 106100
37 using namespace boost::placeholders;
38 #endif
39 using namespace dcpomatic;
40
41
42 SimpleVideoView::SimpleVideoView (FilmViewer* viewer, wxWindow* parent)
43         : VideoView (viewer)
44 {
45         _panel = new wxPanel (parent);
46
47 #ifndef __WXOSX__
48         _panel->SetDoubleBuffered (true);
49 #endif
50
51         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
52         _panel->SetBackgroundColour (*wxBLACK);
53
54         _panel->Bind (wxEVT_PAINT, boost::bind (&SimpleVideoView::paint, this));
55         _panel->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
56
57         _timer.Bind (wxEVT_TIMER, boost::bind(&SimpleVideoView::timer, this));
58 }
59
60 void
61 SimpleVideoView::paint ()
62 {
63         _state_timer.set("paint-panel");
64         wxPaintDC dc (_panel);
65
66         dcp::Size const out_size = _viewer->out_size ();
67         wxSize const panel_size = _panel->GetSize ();
68
69 #ifdef DCPOMATIC_VARIANT_SWAROOP
70         if (_viewer->background_image()) {
71                 dc.Clear ();
72                 optional<boost::filesystem::path> bg = Config::instance()->player_background_image();
73                 if (bg) {
74                         wxImage image (std_to_wx(bg->string()));
75                         wxBitmap bitmap (image);
76                         dc.DrawBitmap (bitmap, max(0, (panel_size.GetWidth() - image.GetSize().GetWidth()) / 2), max(0, (panel_size.GetHeight() - image.GetSize().GetHeight()) / 2));
77                 }
78                 return;
79         }
80 #endif
81
82         if (!out_size.width || !out_size.height || !_image || out_size != _image->size()) {
83                 dc.Clear ();
84         } else {
85
86                 wxImage frame (out_size.width, out_size.height, _image->data()[0], true);
87                 wxBitmap frame_bitmap (frame);
88                 dc.DrawBitmap (frame_bitmap, 0, max(0, (panel_size.GetHeight() - out_size.height) / 2));
89
90 #ifdef DCPOMATIC_VARIANT_SWAROOP
91                 DCPTime const period = DCPTime::from_seconds(Config::instance()->player_watermark_period() * 60);
92                 int64_t n = position().get() / period.get();
93                 DCPTime from(n * period.get());
94                 DCPTime to = from + DCPTime::from_seconds(Config::instance()->player_watermark_duration() / 1000.0);
95                 if (from <= position() && position() <= to) {
96                         if (!_in_watermark) {
97                                 _in_watermark = true;
98                                 _watermark_x = rand() % panel_size.GetWidth();
99                                 _watermark_y = rand() % panel_size.GetHeight();
100                         }
101                         dc.SetTextForeground(*wxWHITE);
102                         string wm = Config::instance()->player_watermark_theatre();
103                         boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
104                         wm += "\n" + boost::posix_time::to_iso_extended_string(t);
105                         dc.DrawText(std_to_wx(wm), _watermark_x, _watermark_y);
106                 } else {
107                         _in_watermark = false;
108                 }
109 #endif
110         }
111
112         if (out_size.width < panel_size.GetWidth()) {
113                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
114                 wxPen   p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
115                 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
116                 dc.SetPen (p);
117                 dc.SetBrush (b);
118                 dc.DrawRectangle (out_size.width, 0, panel_size.GetWidth() - out_size.width, panel_size.GetHeight());
119         }
120
121         if (out_size.height < panel_size.GetHeight()) {
122                 wxPen   p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
123                 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
124                 dc.SetPen (p);
125                 dc.SetBrush (b);
126                 int const gap = (panel_size.GetHeight() - out_size.height) / 2;
127                 dc.DrawRectangle (0, 0, panel_size.GetWidth(), gap);
128                 dc.DrawRectangle (0, gap + out_size.height + 1, panel_size.GetWidth(), gap + 1);
129         }
130
131         if (_viewer->outline_content()) {
132                 wxPen p (wxColour (255, 0, 0), 2);
133                 dc.SetPen (p);
134                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
135                 dc.DrawRectangle (_inter_position.x, _inter_position.y + (panel_size.GetHeight() - out_size.height) / 2, _inter_size.width, _inter_size.height);
136         }
137
138         optional<dcpomatic::Rect<double> > subs = _viewer->outline_subtitles();
139         if (subs) {
140                 wxPen p (wxColour(0, 255, 0), 2);
141                 dc.SetPen (p);
142                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
143                 dc.DrawRectangle (subs->x * out_size.width, subs->y * out_size.height, subs->width * out_size.width, subs->height * out_size.height);
144         }
145
146         _state_timer.unset();
147 }
148
149 void
150 SimpleVideoView::refresh_panel ()
151 {
152         _state_timer.set ("refresh-panel");
153         _panel->Refresh ();
154         _panel->Update ();
155         _state_timer.unset ();
156 }
157
158 void
159 SimpleVideoView::timer ()
160 {
161         if (!_viewer->playing()) {
162                 return;
163         }
164
165         display_next_frame (false);
166         DCPTime const next = position() + _viewer->one_video_frame();
167
168         if (next >= length()) {
169                 _viewer->finished ();
170                 return;
171         }
172
173         LOG_DEBUG_VIDEO_VIEW("%1 -> %2; delay %3", next.seconds(), _viewer->time().seconds(), max((next.seconds() - _viewer->time().seconds()) * 1000, 1.0));
174         _timer.Start (max(1, time_until_next_frame().get_value_or(0)), wxTIMER_ONE_SHOT);
175
176         if (_viewer->butler()) {
177                 _viewer->butler()->rethrow ();
178         }
179 }
180
181 void
182 SimpleVideoView::start ()
183 {
184         VideoView::start ();
185         timer ();
186 }
187
188 /** Try to get a frame from the butler and display it.
189  *  @param non_blocking true to return false quickly if no video is available quickly (i.e. we are waiting for the butler).
190  *  false to ask the butler to block until it has video (unless it is suspended).
191  *  @return true on success, false if we did nothing because it would have taken too long.
192  */
193 bool
194 SimpleVideoView::display_next_frame (bool non_blocking)
195 {
196         bool r = get_next_frame (non_blocking);
197         if (!r) {
198                 if (non_blocking) {
199                         /* No video available; return saying we failed */
200                         return false;
201                 } else {
202                         /* Player was suspended; come back later */
203                         signal_manager->when_idle (boost::bind(&SimpleVideoView::display_next_frame, this, false));
204                         return false;
205                 }
206         }
207
208         update ();
209
210         try {
211                 _viewer->butler()->rethrow ();
212         } catch (DecodeError& e) {
213                 error_dialog (get(), e.what());
214         }
215
216         return true;
217 }
218
219 void
220 SimpleVideoView::update ()
221 {
222         if (!player_video().first) {
223                 set_image (shared_ptr<Image>());
224                 refresh_panel ();
225                 return;
226         }
227
228         if (_viewer->playing() && (_viewer->time() - player_video().second) > one_video_frame()) {
229                 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
230                    part if this frame is J2K).
231                 */
232                 add_dropped ();
233                 return;
234         }
235
236         /* In an ideal world, what we would do here is:
237          *
238          * 1. convert to XYZ exactly as we do in the DCP creation path.
239          * 2. convert back to RGB for the preview display, compensating
240          *    for the monitor etc. etc.
241          *
242          * but this is inefficient if the source is RGB.  Since we don't
243          * (currently) care too much about the precise accuracy of the preview's
244          * colour mapping (and we care more about its speed) we try to short-
245          * circuit this "ideal" situation in some cases.
246          *
247          * The content's specified colour conversion indicates the colourspace
248          * which the content is in (according to the user).
249          *
250          * PlayerVideo::image (bound to PlayerVideo::force) will take the source
251          * image and convert it (from whatever the user has said it is) to RGB.
252          */
253
254         _state_timer.set ("get image");
255
256         set_image (
257                 player_video().first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VIDEO_RANGE_FULL, false, true)
258                 );
259
260         _state_timer.set ("ImageChanged");
261         _viewer->image_changed (player_video().first);
262         _state_timer.unset ();
263
264         _inter_position = player_video().first->inter_position ();
265         _inter_size = player_video().first->inter_size ();
266
267         refresh_panel ();
268 }