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