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