Disable warnings around all wx includes.
[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 "lib/warnings.h"
30 #include <dcp/util.h>
31 DCPOMATIC_DISABLE_WARNINGS
32 #include <wx/wx.h>
33 DCPOMATIC_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
76         dcp::Size out_size;
77         if (!_image) {
78                 dc.Clear ();
79         } else {
80                 DCPOMATIC_ASSERT (_image->alignment() == Image::Alignment::COMPACT);
81                 out_size = _image->size();
82                 wxImage frame (out_size.width, out_size.height, _image->data()[0], true);
83                 wxBitmap frame_bitmap (frame);
84                 dc.DrawBitmap (frame_bitmap, 0, max(0, (panel_size.GetHeight() - out_size.height) / 2));
85         }
86
87         auto pad = pad_colour();
88
89         if (out_size.width < panel_size.GetWidth()) {
90                 wxPen   p (pad);
91                 wxBrush b (pad);
92                 dc.SetPen (p);
93                 dc.SetBrush (b);
94                 dc.DrawRectangle (out_size.width, 0, panel_size.GetWidth() - out_size.width, panel_size.GetHeight());
95         }
96
97         if (out_size.height < panel_size.GetHeight()) {
98                 wxPen   p (pad);
99                 wxBrush b (pad);
100                 dc.SetPen (p);
101                 dc.SetBrush (b);
102                 int const gap = (panel_size.GetHeight() - out_size.height) / 2;
103                 dc.DrawRectangle (0, 0, panel_size.GetWidth(), gap);
104                 dc.DrawRectangle (0, gap + out_size.height + 1, panel_size.GetWidth(), gap + 1);
105         }
106
107         if (_viewer->outline_content()) {
108                 wxPen p (outline_content_colour(), 2);
109                 dc.SetPen (p);
110                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
111                 dc.DrawRectangle (_inter_position.x, _inter_position.y + (panel_size.GetHeight() - out_size.height) / 2, _inter_size.width, _inter_size.height);
112         }
113
114         auto subs = _viewer->outline_subtitles();
115         if (subs) {
116                 wxPen p (outline_subtitles_colour(), 2);
117                 dc.SetPen (p);
118                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
119                 dc.DrawRectangle (subs->x * out_size.width, subs->y * out_size.height, subs->width * out_size.width, subs->height * out_size.height);
120         }
121
122         _state_timer.unset();
123 }
124
125
126 void
127 SimpleVideoView::refresh_panel ()
128 {
129         _state_timer.set ("refresh-panel");
130         _panel->Refresh ();
131         _panel->Update ();
132         _state_timer.unset ();
133 }
134
135
136 void
137 SimpleVideoView::timer ()
138 {
139         if (!_viewer->playing()) {
140                 return;
141         }
142
143         display_next_frame (false);
144         auto const next = position() + _viewer->one_video_frame();
145
146         if (next >= length()) {
147                 _viewer->finished ();
148                 return;
149         }
150
151         LOG_DEBUG_VIDEO_VIEW("%1 -> %2; delay %3", next.seconds(), _viewer->time().seconds(), max((next.seconds() - _viewer->time().seconds()) * 1000, 1.0));
152         _timer.Start (max(1, time_until_next_frame().get_value_or(0)), wxTIMER_ONE_SHOT);
153
154         if (_viewer->butler()) {
155                 _viewer->butler()->rethrow ();
156         }
157 }
158
159
160 void
161 SimpleVideoView::start ()
162 {
163         VideoView::start ();
164         timer ();
165 }
166
167
168 /** Try to get a frame from the butler and display it.
169  *  @param non_blocking true to return false quickly if no video is available quickly (i.e. we are waiting for the butler).
170  *  false to ask the butler to block until it has video (unless it is suspended).
171  *  @return true on success, false if we did nothing because it would have taken too long.
172  */
173 VideoView::NextFrameResult
174 SimpleVideoView::display_next_frame (bool non_blocking)
175 {
176         auto const r = get_next_frame (non_blocking);
177         if (r != SUCCESS) {
178                 return r;
179         }
180
181         update ();
182
183         try {
184                 _viewer->butler()->rethrow ();
185         } catch (DecodeError& e) {
186                 error_dialog (get(), e.what());
187         }
188
189         return SUCCESS;
190 }
191
192
193 void
194 SimpleVideoView::update ()
195 {
196         if (!player_video().first) {
197                 _image.reset ();
198                 refresh_panel ();
199                 return;
200         }
201
202         if (_viewer->playing() && (_viewer->time() - player_video().second) > one_video_frame()) {
203                 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
204                    part if this frame is J2K).
205                 */
206                 add_dropped ();
207                 return;
208         }
209
210         /* In an ideal world, what we would do here is:
211          *
212          * 1. convert to XYZ exactly as we do in the DCP creation path.
213          * 2. convert back to RGB for the preview display, compensating
214          *    for the monitor etc. etc.
215          *
216          * but this is inefficient if the source is RGB.  Since we don't
217          * (currently) care too much about the precise accuracy of the preview's
218          * colour mapping (and we care more about its speed) we try to short-
219          * circuit this "ideal" situation in some cases.
220          *
221          * The content's specified colour conversion indicates the colourspace
222          * which the content is in (according to the user).
223          *
224          * PlayerVideo::image (bound to PlayerVideo::force) will take the source
225          * image and convert it (from whatever the user has said it is) to RGB.
226          */
227
228         _state_timer.set ("get image");
229
230         _image = player_video().first->image(boost::bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true);
231
232         _state_timer.set ("ImageChanged");
233         _viewer->image_changed (player_video().first);
234         _state_timer.unset ();
235
236         _inter_position = player_video().first->inter_position ();
237         _inter_size = player_video().first->inter_size ();
238
239         refresh_panel ();
240 }