summaryrefslogtreecommitdiff
path: root/src/wx/video_waveform_plot.cc
blob: 2827689a72b5aefb48092726cf8388d95d50262b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
    Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "film_viewer.h"
#include "video_waveform_plot.h"
#include "wx_util.h"
#include "lib/dcp_video.h"
#include "lib/film.h"
#include "lib/image.h"
#include "lib/player_video.h"
#include <dcp/locale_convert.h>
#include <dcp/openjpeg_image.h>
#include <dcp/warnings.h>
LIBDCP_DISABLE_WARNINGS
#include <wx/graphics.h>
#include <wx/rawbmp.h>
LIBDCP_ENABLE_WARNINGS
#include <boost/bind/bind.hpp>


using std::make_shared;
using std::max;
using std::min;
using std::shared_ptr;
using std::string;
using std::weak_ptr;
#if BOOST_VERSION >= 106100
using namespace boost::placeholders;
#endif
using dcp::locale_convert;


int const VideoWaveformPlot::_vertical_margin = 8;
int const VideoWaveformPlot::_pixel_values = 4096;
int const VideoWaveformPlot::_x_axis_width = 52;


VideoWaveformPlot::VideoWaveformPlot(wxWindow* parent, weak_ptr<const Film> film, FilmViewer& viewer)
	: wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
	, _film (film)
	, _viewer(viewer)
{
#ifndef __WXOSX__
	SetDoubleBuffered (true);
#endif

	_viewer_connection = _viewer.ImageChanged.connect(boost::bind(&VideoWaveformPlot::set_image, this));

	Bind (wxEVT_PAINT, boost::bind(&VideoWaveformPlot::paint, this));
	Bind (wxEVT_SIZE,  boost::bind(&VideoWaveformPlot::sized, this, _1));
	Bind (wxEVT_MOTION, boost::bind(&VideoWaveformPlot::mouse_moved, this, _1));

	SetMinSize (wxSize (640, 512));
	SetBackgroundColour (wxColour (0, 0, 0));
}


void
VideoWaveformPlot::paint ()
{
	wxPaintDC dc (this);

	if (_dirty) {
		create_waveform ();
		_dirty = false;
	}

	if (!_waveform) {
		return;
	}

	auto gc = wxGraphicsContext::Create (dc);
	if (!gc) {
		return;
	}

	int const height = _waveform->size().height;

	gc->SetPen (wxPen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));

	gc->SetFont (gc->CreateFont (*wxSMALL_FONT, wxColour (255, 255, 255)));
	double label_width;
	double label_height;
	double label_descent;
	double label_leading;
	gc->GetTextExtent(char_to_wx("1024"), &label_width, &label_height, &label_descent, &label_leading);

	double extra[3];
	double w;
	gc->GetTextExtent(char_to_wx("0"), &w, &label_height, &label_descent, &label_leading);
	extra[0] = label_width - w;
	gc->GetTextExtent(char_to_wx("64"), &w, &label_height, &label_descent, &label_leading);
	extra[1] = label_width - w;
	gc->GetTextExtent(char_to_wx("512"), &w, &label_height, &label_descent, &label_leading);
	extra[2] = label_width - w;

	int label_gaps = 2;
	while (height / label_gaps > 64) {
		label_gaps *= 2;
	}

	for (int i = 0; i < label_gaps + 1; ++i) {
		auto p = gc->CreatePath ();
		int const y = _vertical_margin + height - (i * height / label_gaps) - 1;
		p.MoveToPoint (label_width + 8, y);
		p.AddLineToPoint (_x_axis_width - 4, y);
		gc->StrokePath (p);
		int x = 4;
		int const n = i * _pixel_values / label_gaps;
		if (n < 10) {
			x += extra[0];
		} else if (n < 100) {
			x += extra[1];
		} else if (n < 1000) {
			x += extra[2];
		}
		gc->DrawText (std_to_wx(locale_convert<string>(n)), x, y - (label_height / 2));
	}

	wxImage waveform (_waveform->size().width, height, _waveform->data()[0], true);
	wxBitmap bitmap (waveform);
	gc->DrawBitmap (bitmap, _x_axis_width, _vertical_margin, _waveform->size().width, height);

	delete gc;
}


void
VideoWaveformPlot::create_waveform ()
{
	_waveform.reset ();

	if (!_image) {
		return;
	}

	auto const image_size = _image->size();
	int const waveform_height = GetSize().GetHeight() - _vertical_margin * 2;
	_waveform = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size (image_size.width, waveform_height), Image::Alignment::PADDED);

	for (int x = 0; x < image_size.width; ++x) {

		/* Work out one vertical `slice' of waveform pixels.  Each value in
		   strip is the number of samples in image with the corresponding group of
		   values.
		*/
		int strip[waveform_height];
		memset (strip, 0, waveform_height * sizeof(int));

		int* ip = _image->data (_component) + x;
		for (int y = 0; y < image_size.height; ++y) {
			strip[*ip * waveform_height / _pixel_values]++;
			ip += image_size.width;
		}

		/* Copy slice into the waveform */
		uint8_t* wp = _waveform->data()[0] + x * 3;
		for (int y = waveform_height - 1; y >= 0; --y) {
			wp[0] = wp[1] = wp[2] = min (255, (strip[y] * 255 / waveform_height) * _contrast);
			wp += _waveform->stride()[0];
		}
	}

	_waveform = _waveform->scale (
		dcp::Size (GetSize().GetWidth() - _x_axis_width, waveform_height),
		dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::COMPACT, false
		);
}


void
VideoWaveformPlot::set_image()
{
	if (!_enabled) {
		return;
	}

	/* We must copy the PlayerVideo here as we will call ::image() on it, potentially
	   with a different pixel_format than was used when ::prepare() was called.
	*/
	_image = DCPVideo::convert_to_xyz(_viewer.last_image()->shallow_copy());
	_dirty = true;
	Refresh ();
}


void
VideoWaveformPlot::sized (wxSizeEvent &)
{
	_dirty = true;
}


void
VideoWaveformPlot::set_enabled (bool e)
{
	_enabled = e;
}


void
VideoWaveformPlot::set_component (int c)
{
	_component = c;
	_dirty = true;
	Refresh ();
}


/** Set `contrast', i.e. a fudge multiplication factor to make low-level signals easier to see,
 *  between 0 and 256.
 */
void
VideoWaveformPlot::set_contrast (int b)
{
	_contrast = b;
	_dirty = true;
	Refresh ();
}


void
VideoWaveformPlot::mouse_moved (wxMouseEvent& ev)
{
	if (!_image) {
		return;
	}

	if (_dirty) {
		create_waveform ();
		_dirty = false;
	}

	auto film = _film.lock ();
	if (!film) {
		return;
	}

	auto const full = film->frame_size ();

	auto const xs = static_cast<double> (full.width) / _waveform->size().width;
	int const x1 = max (0, min (full.width - 1, int (floor (ev.GetPosition().x - _x_axis_width - 0.5) * xs)));
	int const x2 = max (0, min (full.width - 1, int (floor (ev.GetPosition().x - _x_axis_width + 0.5) * xs)));

	auto const ys = static_cast<double> (_pixel_values) / _waveform->size().height;
	int const fy = _waveform->size().height - (ev.GetPosition().y - _vertical_margin);
	int const y1 = max (0, min (_pixel_values - 1, int (floor (fy - 0.5) * ys)));
	int const y2 = max (0, min (_pixel_values - 1, int (floor (fy + 0.5) * ys)));

	MouseMoved (x1, x2, y1, y2);
}