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