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