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