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