Work around strange build error on Ubuntu 18.04
[dcpomatic.git] / src / wx / video_waveform_plot.cc
1 /*
2     Copyright (C) 2015-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 "film_viewer.h"
23 #include "video_waveform_plot.h"
24 #include "wx_util.h"
25 #include "lib/dcp_video.h"
26 #include "lib/film.h"
27 #include "lib/image.h"
28 #include "lib/player_video.h"
29 #include <dcp/locale_convert.h>
30 #include <dcp/openjpeg_image.h>
31 #include <dcp/warnings.h>
32 LIBDCP_DISABLE_WARNINGS
33 #include <wx/graphics.h>
34 #include <wx/rawbmp.h>
35 LIBDCP_ENABLE_WARNINGS
36 #include <boost/bind/bind.hpp>
37
38
39 using std::make_shared;
40 using std::max;
41 using std::min;
42 using std::shared_ptr;
43 using std::string;
44 using std::weak_ptr;
45 #if BOOST_VERSION >= 106100
46 using namespace boost::placeholders;
47 #endif
48 using dcp::locale_convert;
49
50
51 int const VideoWaveformPlot::_vertical_margin = 8;
52 int const VideoWaveformPlot::_pixel_values = 4096;
53 int const VideoWaveformPlot::_x_axis_width = 52;
54
55
56 VideoWaveformPlot::VideoWaveformPlot (wxWindow* parent, weak_ptr<const Film> film, weak_ptr<FilmViewer> viewer)
57         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
58         , _film (film)
59 {
60 #ifndef __WXOSX__
61         SetDoubleBuffered (true);
62 #endif
63
64         auto fv = viewer.lock ();
65         DCPOMATIC_ASSERT (fv);
66
67         _viewer_connection = fv->ImageChanged.connect (boost::bind(&VideoWaveformPlot::set_image, this, _1));
68
69         Bind (wxEVT_PAINT, boost::bind(&VideoWaveformPlot::paint, this));
70         Bind (wxEVT_SIZE,  boost::bind(&VideoWaveformPlot::sized, this, _1));
71         Bind (wxEVT_MOTION, boost::bind(&VideoWaveformPlot::mouse_moved, this, _1));
72
73         SetMinSize (wxSize (640, 512));
74         SetBackgroundColour (wxColour (0, 0, 0));
75 }
76
77
78 void
79 VideoWaveformPlot::paint ()
80 {
81         wxPaintDC dc (this);
82
83         if (_dirty) {
84                 create_waveform ();
85                 _dirty = false;
86         }
87
88         if (!_waveform) {
89                 return;
90         }
91
92         auto gc = wxGraphicsContext::Create (dc);
93         if (!gc) {
94                 return;
95         }
96
97         int const height = _waveform->size().height;
98
99         gc->SetPen (wxPen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
100
101         gc->SetFont (gc->CreateFont (*wxSMALL_FONT, wxColour (255, 255, 255)));
102         double label_width;
103         double label_height;
104         double label_descent;
105         double label_leading;
106         gc->GetTextExtent (wxT ("1024"), &label_width, &label_height, &label_descent, &label_leading);
107
108         double extra[3];
109         double w;
110         gc->GetTextExtent (wxT ("0"), &w, &label_height, &label_descent, &label_leading);
111         extra[0] = label_width - w;
112         gc->GetTextExtent (wxT ("64"), &w, &label_height, &label_descent, &label_leading);
113         extra[1] = label_width - w;
114         gc->GetTextExtent (wxT ("512"), &w, &label_height, &label_descent, &label_leading);
115         extra[2] = label_width - w;
116
117         int label_gaps = 2;
118         while (height / label_gaps > 64) {
119                 label_gaps *= 2;
120         }
121
122         for (int i = 0; i < label_gaps + 1; ++i) {
123                 auto p = gc->CreatePath ();
124                 int const y = _vertical_margin + height - (i * height / label_gaps) - 1;
125                 p.MoveToPoint (label_width + 8, y);
126                 p.AddLineToPoint (_x_axis_width - 4, y);
127                 gc->StrokePath (p);
128                 int x = 4;
129                 int const n = i * _pixel_values / label_gaps;
130                 if (n < 10) {
131                         x += extra[0];
132                 } else if (n < 100) {
133                         x += extra[1];
134                 } else if (n < 1000) {
135                         x += extra[2];
136                 }
137                 gc->DrawText (std_to_wx(locale_convert<string>(n)), x, y - (label_height / 2));
138         }
139
140         wxImage waveform (_waveform->size().width, height, _waveform->data()[0], true);
141         wxBitmap bitmap (waveform);
142         gc->DrawBitmap (bitmap, _x_axis_width, _vertical_margin, _waveform->size().width, height);
143
144         delete gc;
145 }
146
147
148 void
149 VideoWaveformPlot::create_waveform ()
150 {
151         _waveform.reset ();
152
153         if (!_image) {
154                 return;
155         }
156
157         auto const image_size = _image->size();
158         int const waveform_height = GetSize().GetHeight() - _vertical_margin * 2;
159         _waveform = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size (image_size.width, waveform_height), Image::Alignment::PADDED);
160
161         for (int x = 0; x < image_size.width; ++x) {
162
163                 /* Work out one vertical `slice' of waveform pixels.  Each value in
164                    strip is the number of samples in image with the corresponding group of
165                    values.
166                 */
167                 int strip[waveform_height];
168                 memset (strip, 0, waveform_height * sizeof(int));
169
170                 int* ip = _image->data (_component) + x;
171                 for (int y = 0; y < image_size.height; ++y) {
172                         strip[*ip * waveform_height / _pixel_values]++;
173                         ip += image_size.width;
174                 }
175
176                 /* Copy slice into the waveform */
177                 uint8_t* wp = _waveform->data()[0] + x * 3;
178                 for (int y = waveform_height - 1; y >= 0; --y) {
179                         wp[0] = wp[1] = wp[2] = min (255, (strip[y] * 255 / waveform_height) * _contrast);
180                         wp += _waveform->stride()[0];
181                 }
182         }
183
184         _waveform = _waveform->scale (
185                 dcp::Size (GetSize().GetWidth() - _x_axis_width, waveform_height),
186                 dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::COMPACT, false
187                 );
188 }
189
190
191 void
192 VideoWaveformPlot::set_image (shared_ptr<PlayerVideo> image)
193 {
194         if (!_enabled) {
195                 return;
196         }
197
198         /* We must copy the PlayerVideo here as we will call ::image() on it, potentially
199            with a different pixel_format than was used when ::prepare() was called.
200         */
201         _image = DCPVideo::convert_to_xyz (image->shallow_copy(), [](dcp::NoteType, string) {});
202         _dirty = true;
203         Refresh ();
204 }
205
206
207 void
208 VideoWaveformPlot::sized (wxSizeEvent &)
209 {
210         _dirty = true;
211 }
212
213
214 void
215 VideoWaveformPlot::set_enabled (bool e)
216 {
217         _enabled = e;
218 }
219
220
221 void
222 VideoWaveformPlot::set_component (int c)
223 {
224         _component = c;
225         _dirty = true;
226         Refresh ();
227 }
228
229
230 /** Set `contrast', i.e. a fudge multiplication factor to make low-level signals easier to see,
231  *  between 0 and 256.
232  */
233 void
234 VideoWaveformPlot::set_contrast (int b)
235 {
236         _contrast = b;
237         _dirty = true;
238         Refresh ();
239 }
240
241
242 void
243 VideoWaveformPlot::mouse_moved (wxMouseEvent& ev)
244 {
245         if (!_image) {
246                 return;
247         }
248
249         if (_dirty) {
250                 create_waveform ();
251                 _dirty = false;
252         }
253
254         auto film = _film.lock ();
255         if (!film) {
256                 return;
257         }
258
259         auto const full = film->frame_size ();
260
261         auto const xs = static_cast<double> (full.width) / _waveform->size().width;
262         int const x1 = max (0, min (full.width - 1, int (floor (ev.GetPosition().x - _x_axis_width - 0.5) * xs)));
263         int const x2 = max (0, min (full.width - 1, int (floor (ev.GetPosition().x - _x_axis_width + 0.5) * xs)));
264
265         auto const ys = static_cast<double> (_pixel_values) / _waveform->size().height;
266         int const fy = _waveform->size().height - (ev.GetPosition().y - _vertical_margin);
267         int const y1 = max (0, min (_pixel_values - 1, int (floor (fy - 0.5) * ys)));
268         int const y2 = max (0, min (_pixel_values - 1, int (floor (fy + 0.5) * ys)));
269
270         MouseMoved (x1, x2, y1, y2);
271 }