diff options
| author | Carl Hetherington <cth@carlh.net> | 2015-08-24 16:43:38 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2015-08-25 15:22:38 +0100 |
| commit | a978f3ac575f1af017002c861480d5203cf0a34e (patch) | |
| tree | 5cd7acdcf7b7f493f3d0047ba96adaccf8509a7e /src/wx | |
| parent | 17d4990d0fca0f38268441a73c73d9e340b4bc85 (diff) | |
Add video waveform viewer.
Diffstat (limited to 'src/wx')
| -rw-r--r-- | src/wx/film_viewer.cc | 14 | ||||
| -rw-r--r-- | src/wx/film_viewer.h | 4 | ||||
| -rw-r--r-- | src/wx/video_waveform_dialog.cc | 94 | ||||
| -rw-r--r-- | src/wx/video_waveform_dialog.h | 39 | ||||
| -rw-r--r-- | src/wx/video_waveform_plot.cc | 220 | ||||
| -rw-r--r-- | src/wx/video_waveform_plot.h | 58 | ||||
| -rw-r--r-- | src/wx/wscript | 2 |
7 files changed, 428 insertions, 3 deletions
diff --git a/src/wx/film_viewer.cc b/src/wx/film_viewer.cc index 1badc0282..9b648a8b9 100644 --- a/src/wx/film_viewer.cc +++ b/src/wx/film_viewer.cc @@ -155,7 +155,7 @@ FilmViewer::set_film (shared_ptr<Film> film) _player_connection = _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1)); calculate_sizes (); - get (_position, _last_get_accurate); + refresh (); setup_sensitivity (); } @@ -184,6 +184,7 @@ FilmViewer::get (DCPTime p, bool accurate) if (!pvf.empty ()) { try { _frame = pvf.front()->image (PIX_FMT_RGB24, boost::bind (&Log::dcp_log, _film->log().get(), _1, _2)); + ImageChanged (pvf.front ()); dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601; if (pvf.front()->colour_conversion()) { @@ -295,7 +296,7 @@ FilmViewer::panel_sized (wxSizeEvent& ev) _panel_size.height = ev.GetSize().GetHeight(); calculate_sizes (); - get (_position, _last_get_accurate); + refresh (); update_position_label (); update_position_slider (); } @@ -436,7 +437,7 @@ FilmViewer::player_changed (bool frequent) } calculate_sizes (); - get (_position, _last_get_accurate); + refresh (); update_position_label (); update_position_slider (); } @@ -462,3 +463,10 @@ FilmViewer::film_changed (Film::Property p) setup_sensitivity (); } } + +/** Re-get the current frame */ +void +FilmViewer::refresh () +{ + get (_position, _last_get_accurate); +} diff --git a/src/wx/film_viewer.h b/src/wx/film_viewer.h index f6ab1a567..eb9d256d3 100644 --- a/src/wx/film_viewer.h +++ b/src/wx/film_viewer.h @@ -44,6 +44,10 @@ public: return _position; } + void refresh (); + + boost::signals2::signal<void (boost::weak_ptr<PlayerVideo>)> ImageChanged; + private: void paint_panel (); void panel_sized (wxSizeEvent &); diff --git a/src/wx/video_waveform_dialog.cc b/src/wx/video_waveform_dialog.cc new file mode 100644 index 000000000..976c4e2ae --- /dev/null +++ b/src/wx/video_waveform_dialog.cc @@ -0,0 +1,94 @@ +/* + Copyright (C) 2015 Carl Hetherington <cth@carlh.net> + + This program 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. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include "video_waveform_dialog.h" +#include "video_waveform_plot.h" +#include "film_viewer.h" +#include "wx_util.h" +#include <boost/bind.hpp> + +using std::cout; +using boost::bind; + +VideoWaveformDialog::VideoWaveformDialog (wxWindow* parent, FilmViewer* viewer) + : wxDialog (parent, wxID_ANY, _("Video Waveform"), wxDefaultPosition, wxSize (640, 512), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE) + , _viewer (viewer) +{ + wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL); + + wxBoxSizer* controls = new wxBoxSizer (wxHORIZONTAL); + + _component = new wxChoice (this, wxID_ANY); + _component->Append (wxT ("X")); + _component->Append (wxT ("Y")); + _component->Append (wxT ("Z")); + add_label_to_sizer (controls, this, _("Component"), true); + controls->Add (_component, 1, wxALL, DCPOMATIC_SIZER_X_GAP); + + add_label_to_sizer (controls, this, _("Contrast"), true); + _contrast = new wxSlider (this, wxID_ANY, 0, 0, 256); + controls->Add (_contrast, 1, wxALL, DCPOMATIC_SIZER_X_GAP); + + overall_sizer->Add (controls, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_X_GAP); + + _plot = new VideoWaveformPlot (this, _viewer); + overall_sizer->Add (_plot, 1, wxALL | wxEXPAND, 12); + +#ifdef DCPOMATIC_LINUX + wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE); + if (buttons) { + overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder()); + } +#endif + + SetSizer (overall_sizer); + overall_sizer->Layout (); + overall_sizer->SetSizeHints (this); + + Bind (wxEVT_SHOW, bind (&VideoWaveformDialog::shown, this, _1)); + _component->Bind (wxEVT_COMMAND_CHOICE_SELECTED, bind (&VideoWaveformDialog::component_changed, this)); + _contrast->Bind (wxEVT_SCROLL_THUMBTRACK, bind (&VideoWaveformDialog::contrast_changed, this)); + + _component->SetSelection (0); + _contrast->SetValue (32); + + component_changed (); + contrast_changed (); +} + +void +VideoWaveformDialog::shown (wxShowEvent& ev) +{ + _plot->set_enabled (ev.IsShown ()); + if (ev.IsShown ()) { + _viewer->refresh (); + } +} + +void +VideoWaveformDialog::component_changed () +{ + _plot->set_component (_component->GetCurrentSelection ()); +} + +void +VideoWaveformDialog::contrast_changed () +{ + _plot->set_contrast (_contrast->GetValue ()); +} diff --git a/src/wx/video_waveform_dialog.h b/src/wx/video_waveform_dialog.h new file mode 100644 index 000000000..475855d0f --- /dev/null +++ b/src/wx/video_waveform_dialog.h @@ -0,0 +1,39 @@ +/* + Copyright (C) 2015 Carl Hetherington <cth@carlh.net> + + This program 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. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include <wx/wx.h> + +class VideoWaveformPlot; +class FilmViewer; + +class VideoWaveformDialog : public wxDialog +{ +public: + VideoWaveformDialog (wxWindow* parent, FilmViewer* viewer); + +private: + void shown (wxShowEvent &); + void component_changed (); + void contrast_changed (); + + FilmViewer* _viewer; + VideoWaveformPlot* _plot; + wxChoice* _component; + wxSlider* _contrast; +}; diff --git a/src/wx/video_waveform_plot.cc b/src/wx/video_waveform_plot.cc new file mode 100644 index 000000000..8c13af7a5 --- /dev/null +++ b/src/wx/video_waveform_plot.cc @@ -0,0 +1,220 @@ +/* + Copyright (C) 2015 Carl Hetherington <cth@carlh.net> + + This program 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. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include "video_waveform_plot.h" +#include "film_viewer.h" +#include "wx_util.h" +#include "lib/image.h" +#include "lib/raw_convert.h" +#include "lib/dcp_video.h" +#include <dcp/openjpeg_image.h> +#include <wx/rawbmp.h> +#include <wx/graphics.h> +#include <boost/bind.hpp> + +using std::cout; +using std::min; +using std::string; +using boost::weak_ptr; +using boost::shared_ptr; + +int const VideoWaveformPlot::_vertical_margin = 8; + +VideoWaveformPlot::VideoWaveformPlot (wxWindow* parent, FilmViewer* viewer) + : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE) + , _dirty (true) + , _enabled (false) + , _component (0) + , _contrast (0) +{ +#ifndef __WXOSX__ + SetDoubleBuffered (true); +#endif + + _viewer_connection = viewer->ImageChanged.connect (boost::bind (&VideoWaveformPlot::set_image, this, _1)); + + Bind (wxEVT_PAINT, boost::bind (&VideoWaveformPlot::paint, this)); + Bind (wxEVT_SIZE, boost::bind (&VideoWaveformPlot::sized, 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; + } + + wxGraphicsContext* gc = wxGraphicsContext::Create (dc); + if (!gc) { + return; + } + + int const axis_x = 48; + 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 (wxT ("1024"), &label_width, &label_height, &label_descent, &label_leading); + + double extra[3]; + double w; + gc->GetTextExtent (wxT ("0"), &w, &label_height, &label_descent, &label_leading); + extra[0] = label_width - w; + gc->GetTextExtent (wxT ("64"), &w, &label_height, &label_descent, &label_leading); + extra[1] = label_width - w; + gc->GetTextExtent (wxT ("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) { + wxGraphicsPath p = gc->CreatePath (); + int const y = _vertical_margin + height - (i * height / label_gaps) - 1; + p.MoveToPoint (label_width + 8, y); + p.AddLineToPoint (axis_x, y); + gc->StrokePath (p); + int x = 4; + int const n = i * 4096 / 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 (raw_convert<string> (n)), x, y - (label_height / 2)); + } + + wxImage waveform (_waveform->size().width, height, _waveform->data()[0], true); + wxBitmap bitmap (waveform); + gc->DrawBitmap (bitmap, axis_x + 4, _vertical_margin, _waveform->size().width, height); + + delete gc; +} + +void +VideoWaveformPlot::create_waveform () +{ + _waveform.reset (); + + if (!_image) { + return; + } + + dcp::Size const size = _image->size(); + _waveform.reset (new Image (PIX_FMT_RGB24, dcp::Size (size.width, size.height), true)); + + for (int x = 0; x < 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[size.height]; + for (int i = 0; i < size.height; ++i) { + strip[i] = 0; + } + + int* ip = _image->data (_component) + x; + for (int y = 0; y < size.height; ++y) { + strip[*ip * size.height / 4096]++; + ip += size.width; + } + + /* Copy slice into the waveform */ + uint8_t* wp = _waveform->data()[0] + x * 3; + for (int y = size.height - 1; y >= 0; --y) { + wp[0] = wp[1] = wp[2] = min (255, (strip[y] * 255 / size.height) * _contrast); + wp += _waveform->stride()[0]; + } + } + + _waveform = _waveform->scale ( + dcp::Size (GetSize().GetWidth() - 32, GetSize().GetHeight() - _vertical_margin * 2), + dcp::YUV_TO_RGB_REC709, PIX_FMT_RGB24, false + ); +} + +static void +note () +{ + +} + +void +VideoWaveformPlot::set_image (weak_ptr<PlayerVideo> image) +{ + if (!_enabled) { + return; + } + + shared_ptr<PlayerVideo> pv = image.lock (); + _image = DCPVideo::convert_to_xyz (pv, boost::bind (¬e)); + _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 (); +} diff --git a/src/wx/video_waveform_plot.h b/src/wx/video_waveform_plot.h new file mode 100644 index 000000000..440be32e2 --- /dev/null +++ b/src/wx/video_waveform_plot.h @@ -0,0 +1,58 @@ +/* + Copyright (C) 2015 Carl Hetherington <cth@carlh.net> + + This program 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. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include <wx/wx.h> +#include <boost/shared_ptr.hpp> +#include <boost/weak_ptr.hpp> +#include <boost/signals2.hpp> + +namespace dcp { + class OpenJPEGImage; +} + +class PlayerVideo; +class Image; +class FilmViewer; + +class VideoWaveformPlot : public wxPanel +{ +public: + VideoWaveformPlot (wxWindow* parent, FilmViewer* viewer); + + void set_enabled (bool e); + void set_component (int c); + void set_contrast (int b); + +private: + void paint (); + void sized (wxSizeEvent &); + void create_waveform (); + void set_image (boost::weak_ptr<PlayerVideo>); + + boost::shared_ptr<dcp::OpenJPEGImage> _image; + boost::shared_ptr<const Image> _waveform; + bool _dirty; + bool _enabled; + int _component; + int _contrast; + + static int const _vertical_margin; + + boost::signals2::connection _viewer_connection; +}; diff --git a/src/wx/wscript b/src/wx/wscript index 94df94ca3..34a7608df 100644 --- a/src/wx/wscript +++ b/src/wx/wscript @@ -79,6 +79,8 @@ sources = """ timing_panel.cc update_dialog.cc video_panel.cc + video_waveform_dialog.cc + video_waveform_plot.cc wx_util.cc wx_signal_manager.cc """ |
