2 Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "film_viewer.h"
23 #include "video_waveform_plot.h"
25 #include "lib/dcp_video.h"
27 #include "lib/image.h"
28 #include "lib/player_video.h"
29 #include "lib/warnings.h"
30 #include <dcp/locale_convert.h>
31 #include <dcp/openjpeg_image.h>
32 DCPOMATIC_DISABLE_WARNINGS
33 #include <wx/graphics.h>
34 #include <wx/rawbmp.h>
35 DCPOMATIC_ENABLE_WARNINGS
36 #include <boost/bind/bind.hpp>
41 using std::make_shared;
44 using std::shared_ptr;
47 #if BOOST_VERSION >= 106100
48 using namespace boost::placeholders;
50 using dcp::locale_convert;
53 int const VideoWaveformPlot::_vertical_margin = 8;
54 int const VideoWaveformPlot::_pixel_values = 4096;
55 int const VideoWaveformPlot::_x_axis_width = 52;
58 VideoWaveformPlot::VideoWaveformPlot (wxWindow* parent, weak_ptr<const Film> film, weak_ptr<FilmViewer> viewer)
59 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
63 SetDoubleBuffered (true);
66 auto fv = viewer.lock ();
67 DCPOMATIC_ASSERT (fv);
69 _viewer_connection = fv->ImageChanged.connect (boost::bind(&VideoWaveformPlot::set_image, this, _1));
71 Bind (wxEVT_PAINT, boost::bind(&VideoWaveformPlot::paint, this));
72 Bind (wxEVT_SIZE, boost::bind(&VideoWaveformPlot::sized, this, _1));
73 Bind (wxEVT_MOTION, boost::bind(&VideoWaveformPlot::mouse_moved, this, _1));
75 SetMinSize (wxSize (640, 512));
76 SetBackgroundColour (wxColour (0, 0, 0));
81 VideoWaveformPlot::paint ()
94 auto gc = wxGraphicsContext::Create (dc);
99 int const height = _waveform->size().height;
101 gc->SetPen (wxPen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
103 gc->SetFont (gc->CreateFont (*wxSMALL_FONT, wxColour (255, 255, 255)));
106 double label_descent;
107 double label_leading;
108 gc->GetTextExtent (wxT ("1024"), &label_width, &label_height, &label_descent, &label_leading);
112 gc->GetTextExtent (wxT ("0"), &w, &label_height, &label_descent, &label_leading);
113 extra[0] = label_width - w;
114 gc->GetTextExtent (wxT ("64"), &w, &label_height, &label_descent, &label_leading);
115 extra[1] = label_width - w;
116 gc->GetTextExtent (wxT ("512"), &w, &label_height, &label_descent, &label_leading);
117 extra[2] = label_width - w;
120 while (height / label_gaps > 64) {
124 for (int i = 0; i < label_gaps + 1; ++i) {
125 auto p = gc->CreatePath ();
126 int const y = _vertical_margin + height - (i * height / label_gaps) - 1;
127 p.MoveToPoint (label_width + 8, y);
128 p.AddLineToPoint (_x_axis_width - 4, y);
131 int const n = i * _pixel_values / label_gaps;
134 } else if (n < 100) {
136 } else if (n < 1000) {
139 gc->DrawText (std_to_wx(locale_convert<string>(n)), x, y - (label_height / 2));
142 wxImage waveform (_waveform->size().width, height, _waveform->data()[0], true);
143 wxBitmap bitmap (waveform);
144 gc->DrawBitmap (bitmap, _x_axis_width, _vertical_margin, _waveform->size().width, height);
151 VideoWaveformPlot::create_waveform ()
159 auto const image_size = _image->size();
160 int const waveform_height = GetSize().GetHeight() - _vertical_margin * 2;
161 _waveform = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size (image_size.width, waveform_height), Image::Alignment::PADDED);
163 for (int x = 0; x < image_size.width; ++x) {
165 /* Work out one vertical `slice' of waveform pixels. Each value in
166 strip is the number of samples in image with the corresponding group of
169 int strip[waveform_height];
170 memset (strip, 0, waveform_height * sizeof(int));
172 int* ip = _image->data (_component) + x;
173 for (int y = 0; y < image_size.height; ++y) {
174 strip[*ip * waveform_height / _pixel_values]++;
175 ip += image_size.width;
178 /* Copy slice into the waveform */
179 uint8_t* wp = _waveform->data()[0] + x * 3;
180 for (int y = waveform_height - 1; y >= 0; --y) {
181 wp[0] = wp[1] = wp[2] = min (255, (strip[y] * 255 / waveform_height) * _contrast);
182 wp += _waveform->stride()[0];
186 _waveform = _waveform->scale (
187 dcp::Size (GetSize().GetWidth() - _x_axis_width, waveform_height),
188 dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::COMPACT, false
194 VideoWaveformPlot::set_image (shared_ptr<PlayerVideo> image)
200 /* We must copy the PlayerVideo here as we will call ::image() on it, potentially
201 with a different pixel_format than was used when ::prepare() was called.
203 _image = DCPVideo::convert_to_xyz (image->shallow_copy(), [](dcp::NoteType, string) {});
210 VideoWaveformPlot::sized (wxSizeEvent &)
217 VideoWaveformPlot::set_enabled (bool e)
224 VideoWaveformPlot::set_component (int c)
232 /** Set `contrast', i.e. a fudge multiplication factor to make low-level signals easier to see,
236 VideoWaveformPlot::set_contrast (int b)
245 VideoWaveformPlot::mouse_moved (wxMouseEvent& ev)
256 auto film = _film.lock ();
261 auto const full = film->frame_size ();
263 auto const xs = static_cast<double> (full.width) / _waveform->size().width;
264 int const x1 = max (0, min (full.width - 1, int (floor (ev.GetPosition().x - _x_axis_width - 0.5) * xs)));
265 int const x2 = max (0, min (full.width - 1, int (floor (ev.GetPosition().x - _x_axis_width + 0.5) * xs)));
267 auto const ys = static_cast<double> (_pixel_values) / _waveform->size().height;
268 int const fy = _waveform->size().height - (ev.GetPosition().y - _vertical_margin);
269 int const y1 = max (0, min (_pixel_values - 1, int (floor (fy - 0.5) * ys)));
270 int const y2 = max (0, min (_pixel_values - 1, int (floor (fy + 0.5) * ys)));
272 MouseMoved (x1, x2, y1, y2);