f866bd4a895a6789df53f5ba4d4da1bb04a3ac4e
[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
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         if (_viewer->crop_guess()) {
123                 wxPen p (crop_guess_colour(), 2);
124                 dc.SetPen (p);
125                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
126                 auto const crop_guess = _viewer->crop_guess().get();
127                 dc.DrawRectangle (
128                         _inter_position.x + _inter_size.width * crop_guess.x,
129                         _inter_position.y + _inter_size.height * crop_guess.y,
130                         _inter_size.width * crop_guess.width,
131                         _inter_size.height * crop_guess.height
132                         );
133         }
134
135         _state_timer.unset();
136 }
137
138
139 void
140 SimpleVideoView::refresh_panel ()
141 {
142         _state_timer.set ("refresh-panel");
143         _panel->Refresh ();
144         _panel->Update ();
145         _state_timer.unset ();
146 }
147
148
149 void
150 SimpleVideoView::timer ()
151 {
152         if (!_viewer->playing()) {
153                 return;
154         }
155
156         display_next_frame (false);
157         auto const next = position() + _viewer->one_video_frame();
158
159         if (next >= length()) {
160                 _viewer->finished ();
161                 return;
162         }
163
164         LOG_DEBUG_VIDEO_VIEW("%1 -> %2; delay %3", next.seconds(), _viewer->time().seconds(), max((next.seconds() - _viewer->time().seconds()) * 1000, 1.0));
165         _timer.Start (max(1, time_until_next_frame().get_value_or(0)), wxTIMER_ONE_SHOT);
166
167         if (_viewer->butler()) {
168                 _viewer->butler()->rethrow ();
169         }
170 }
171
172
173 void
174 SimpleVideoView::start ()
175 {
176         VideoView::start ();
177         timer ();
178 }
179
180
181 /** Try to get a frame from the butler and display it.
182  *  @param non_blocking true to return false quickly if no video is available quickly (i.e. we are waiting for the butler).
183  *  false to ask the butler to block until it has video (unless it is suspended).
184  *  @return true on success, false if we did nothing because it would have taken too long.
185  */
186 VideoView::NextFrameResult
187 SimpleVideoView::display_next_frame (bool non_blocking)
188 {
189         auto const r = get_next_frame (non_blocking);
190         if (r != SUCCESS) {
191                 return r;
192         }
193
194         update ();
195
196         try {
197                 _viewer->butler()->rethrow ();
198         } catch (DecodeError& e) {
199                 error_dialog (get(), e.what());
200         }
201
202         return SUCCESS;
203 }
204
205
206 void
207 SimpleVideoView::update ()
208 {
209         if (!player_video().first) {
210                 _image.reset ();
211                 refresh_panel ();
212                 return;
213         }
214
215         if (_viewer->playing() && (_viewer->time() - player_video().second) > one_video_frame()) {
216                 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
217                    part if this frame is J2K).
218                 */
219                 add_dropped ();
220                 return;
221         }
222
223         /* In an ideal world, what we would do here is:
224          *
225          * 1. convert to XYZ exactly as we do in the DCP creation path.
226          * 2. convert back to RGB for the preview display, compensating
227          *    for the monitor etc. etc.
228          *
229          * but this is inefficient if the source is RGB.  Since we don't
230          * (currently) care too much about the precise accuracy of the preview's
231          * colour mapping (and we care more about its speed) we try to short-
232          * circuit this "ideal" situation in some cases.
233          *
234          * The content's specified colour conversion indicates the colourspace
235          * which the content is in (according to the user).
236          *
237          * PlayerVideo::image (bound to PlayerVideo::force) will take the source
238          * image and convert it (from whatever the user has said it is) to RGB.
239          */
240
241         _state_timer.set ("get image");
242
243         _image = player_video().first->image(boost::bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true);
244
245         _state_timer.set ("ImageChanged");
246         _viewer->image_changed (player_video().first);
247         _state_timer.unset ();
248
249         _inter_position = player_video().first->inter_position ();
250         _inter_size = player_video().first->inter_size ();
251
252         refresh_panel ();
253 }