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