Add video waveform viewer.
[dcpomatic.git] / src / wx / video_waveform_plot.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "video_waveform_plot.h"
21 #include "film_viewer.h"
22 #include "wx_util.h"
23 #include "lib/image.h"
24 #include "lib/raw_convert.h"
25 #include "lib/dcp_video.h"
26 #include <dcp/openjpeg_image.h>
27 #include <wx/rawbmp.h>
28 #include <wx/graphics.h>
29 #include <boost/bind.hpp>
30
31 using std::cout;
32 using std::min;
33 using std::string;
34 using boost::weak_ptr;
35 using boost::shared_ptr;
36
37 int const VideoWaveformPlot::_vertical_margin = 8;
38
39 VideoWaveformPlot::VideoWaveformPlot (wxWindow* parent, FilmViewer* viewer)
40         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
41         , _dirty (true)
42         , _enabled (false)
43         , _component (0)
44         , _contrast (0)
45 {
46 #ifndef __WXOSX__
47         SetDoubleBuffered (true);
48 #endif
49
50         _viewer_connection = viewer->ImageChanged.connect (boost::bind (&VideoWaveformPlot::set_image, this, _1));
51
52         Bind (wxEVT_PAINT, boost::bind (&VideoWaveformPlot::paint, this));
53         Bind (wxEVT_SIZE,  boost::bind (&VideoWaveformPlot::sized, this, _1));
54
55         SetMinSize (wxSize (640, 512));
56         SetBackgroundColour (wxColour (0, 0, 0));
57 }
58
59 void
60 VideoWaveformPlot::paint ()
61 {
62         wxPaintDC dc (this);
63
64         if (_dirty) {
65                 create_waveform ();
66                 _dirty = false;
67         }
68
69         if (!_waveform) {
70                 return;
71         }
72
73         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
74         if (!gc) {
75                 return;
76         }
77
78         int const axis_x = 48;
79         int const height = _waveform->size().height;
80
81         gc->SetPen (wxPen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
82
83         gc->SetFont (gc->CreateFont (*wxSMALL_FONT, wxColour (255, 255, 255)));
84         double label_width;
85         double label_height;
86         double label_descent;
87         double label_leading;
88         gc->GetTextExtent (wxT ("1024"), &label_width, &label_height, &label_descent, &label_leading);
89
90         double extra[3];
91         double w;
92         gc->GetTextExtent (wxT ("0"), &w, &label_height, &label_descent, &label_leading);
93         extra[0] = label_width - w;
94         gc->GetTextExtent (wxT ("64"), &w, &label_height, &label_descent, &label_leading);
95         extra[1] = label_width - w;
96         gc->GetTextExtent (wxT ("512"), &w, &label_height, &label_descent, &label_leading);
97         extra[2] = label_width - w;
98
99         int label_gaps = 2;
100         while (height / label_gaps > 64) {
101                 label_gaps *= 2;
102         }
103
104         for (int i = 0; i < label_gaps + 1; ++i) {
105                 wxGraphicsPath p = gc->CreatePath ();
106                 int const y = _vertical_margin + height - (i * height / label_gaps) - 1;
107                 p.MoveToPoint (label_width + 8, y);
108                 p.AddLineToPoint (axis_x, y);
109                 gc->StrokePath (p);
110                 int x = 4;
111                 int const n = i * 4096 / label_gaps;
112                 if (n < 10) {
113                         x += extra[0];
114                 } else if (n < 100) {
115                         x += extra[1];
116                 } else if (n < 1000) {
117                         x += extra[2];
118                 }
119                 gc->DrawText (std_to_wx (raw_convert<string> (n)), x, y - (label_height / 2));
120         }
121
122         wxImage waveform (_waveform->size().width, height, _waveform->data()[0], true);
123         wxBitmap bitmap (waveform);
124         gc->DrawBitmap (bitmap, axis_x + 4, _vertical_margin, _waveform->size().width, height);
125
126         delete gc;
127 }
128
129 void
130 VideoWaveformPlot::create_waveform ()
131 {
132         _waveform.reset ();
133
134         if (!_image) {
135                 return;
136         }
137
138         dcp::Size const size = _image->size();
139         _waveform.reset (new Image (PIX_FMT_RGB24, dcp::Size (size.width, size.height), true));
140
141         for (int x = 0; x < size.width; ++x) {
142
143                 /* Work out one vertical `slice' of waveform pixels.  Each value in
144                    strip is the number of samples in image with the corresponding group of
145                    values.
146                 */
147                 int strip[size.height];
148                 for (int i = 0; i < size.height; ++i) {
149                         strip[i] = 0;
150                 }
151
152                 int* ip = _image->data (_component) + x;
153                 for (int y = 0; y < size.height; ++y) {
154                         strip[*ip * size.height / 4096]++;
155                         ip += size.width;
156                 }
157
158                 /* Copy slice into the waveform */
159                 uint8_t* wp = _waveform->data()[0] + x * 3;
160                 for (int y = size.height - 1; y >= 0; --y) {
161                         wp[0] = wp[1] = wp[2] = min (255, (strip[y] * 255 / size.height) * _contrast);
162                         wp += _waveform->stride()[0];
163                 }
164         }
165
166         _waveform = _waveform->scale (
167                 dcp::Size (GetSize().GetWidth() - 32, GetSize().GetHeight() - _vertical_margin * 2),
168                 dcp::YUV_TO_RGB_REC709, PIX_FMT_RGB24, false
169                 );
170 }
171
172 static void
173 note ()
174 {
175
176 }
177
178 void
179 VideoWaveformPlot::set_image (weak_ptr<PlayerVideo> image)
180 {
181         if (!_enabled) {
182                 return;
183         }
184
185         shared_ptr<PlayerVideo> pv = image.lock ();
186         _image = DCPVideo::convert_to_xyz (pv, boost::bind (&note));
187         _dirty = true;
188         Refresh ();
189 }
190
191 void
192 VideoWaveformPlot::sized (wxSizeEvent &)
193 {
194         _dirty = true;
195 }
196
197 void
198 VideoWaveformPlot::set_enabled (bool e)
199 {
200         _enabled = e;
201 }
202
203 void
204 VideoWaveformPlot::set_component (int c)
205 {
206         _component = c;
207         _dirty = true;
208         Refresh ();
209 }
210
211 /** Set `contrast', i.e. a fudge multiplication factor to make low-level signals easier to see,
212  *  between 0 and 256.
213  */
214 void
215 VideoWaveformPlot::set_contrast (int b)
216 {
217         _contrast = b;
218         _dirty = true;
219         Refresh ();
220 }