Add video waveform viewer.
authorCarl Hetherington <cth@carlh.net>
Mon, 24 Aug 2015 15:43:38 +0000 (16:43 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 25 Aug 2015 14:22:38 +0000 (15:22 +0100)
15 files changed:
src/lib/dcp_video.cc
src/lib/dcp_video.h
src/lib/image.cc
src/lib/image.h
src/tools/dcpomatic.cc
src/wx/film_viewer.cc
src/wx/film_viewer.h
src/wx/video_waveform_dialog.cc [new file with mode: 0644]
src/wx/video_waveform_dialog.h [new file with mode: 0644]
src/wx/video_waveform_plot.cc [new file with mode: 0644]
src/wx/video_waveform_plot.h [new file with mode: 0644]
src/wx/wscript
test/client_server_test.cc
test/image_test.cc
test/pixel_formats_test.cc

index f4c6222e2cfc2ff74d003ffc0b0ed6221bd9ad3a..e02bcffca614ee10167975d268c1544c6ccc6c0a 100644 (file)
@@ -91,27 +91,35 @@ DCPVideo::DCPVideo (shared_ptr<const PlayerVideo> frame, shared_ptr<const cxml::
        _resolution = Resolution (node->optional_number_child<int>("Resolution").get_value_or (RESOLUTION_2K));
 }
 
-/** J2K-encode this frame on the local host.
- *  @return Encoded data.
- */
-Data
-DCPVideo::encode_locally (dcp::NoteHandler note)
+shared_ptr<dcp::OpenJPEGImage>
+DCPVideo::convert_to_xyz (shared_ptr<const PlayerVideo> frame, dcp::NoteHandler note)
 {
        shared_ptr<dcp::OpenJPEGImage> xyz;
 
-       shared_ptr<Image> image = _frame->image (AV_PIX_FMT_RGB48LE, note);
-       if (_frame->colour_conversion()) {
+       shared_ptr<Image> image = frame->image (AV_PIX_FMT_RGB48LE, note);
+       if (frame->colour_conversion()) {
                xyz = dcp::rgb_to_xyz (
                        image->data()[0],
                        image->size(),
                        image->stride()[0],
-                       _frame->colour_conversion().get(),
+                       frame->colour_conversion().get(),
                        note
                        );
        } else {
                xyz = dcp::xyz_to_xyz (image->data()[0], image->size(), image->stride()[0]);
        }
 
+       return xyz;
+}
+
+/** J2K-encode this frame on the local host.
+ *  @return Encoded data.
+ */
+Data
+DCPVideo::encode_locally (dcp::NoteHandler note)
+{
+       shared_ptr<dcp::OpenJPEGImage> xyz = convert_to_xyz (_frame, note);
+
        /* Set the max image and component sizes based on frame_rate */
        int max_cs_len = ((float) _j2k_bandwidth) / 8 / _frames_per_second;
        if (_frame->eyes() == EYES_LEFT || _frame->eyes() == EYES_RIGHT) {
index 8fd668b00dc7402a016a171b5967b54dcded8fa8..4c771419244d3dc00bf39ce82134787bb149c5a1 100644 (file)
@@ -60,6 +60,8 @@ public:
 
        bool same (boost::shared_ptr<const DCPVideo> other) const;
 
+       static boost::shared_ptr<dcp::OpenJPEGImage> convert_to_xyz (boost::shared_ptr<const PlayerVideo> frame, dcp::NoteHandler note);
+
 private:
 
        void add_metadata (xmlpp::Element *) const;
index 55f49daf853def88d5676d0b29646b2c104842cb..0c7a0ef0da26bfad6ea20f96ec2a5316af678a9a 100644 (file)
@@ -61,15 +61,26 @@ Image::line_factor (int n) const
 }
 
 /** @param n Component index.
- *  @return Number of lines in the image for the given component.
+ *  @return Number of samples (i.e. pixels, unless sub-sampled) in each direction for this component.
  */
-int
-Image::lines (int n) const
+dcp::Size
+Image::sample_size (int n) const
 {
-       return rint (ceil (static_cast<double>(size().height) / line_factor (n)));
+       int horizontal_factor = 1;
+       if (n > 0) {
+               AVPixFmtDescriptor const * d = av_pix_fmt_desc_get (_pixel_format);
+               if (!d) {
+                       throw PixelFormatError ("sample_size()", _pixel_format);
+               }
+               horizontal_factor = pow (2.0f, d->log2_chroma_w);
+       }
+
+       return dcp::Size (
+               rint (ceil (static_cast<double>(size().width) / horizontal_factor)),
+               rint (ceil (static_cast<double>(size().height) / line_factor (n)))
+               );
 }
 
-/** @return Number of components */
 int
 Image::components () const
 {
@@ -78,6 +89,18 @@ Image::components () const
                throw PixelFormatError ("components()", _pixel_format);
        }
 
+       return d->nb_components;
+}
+
+/** @return Number of planes */
+int
+Image::planes () const
+{
+       AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
+       if (!d) {
+               throw PixelFormatError ("planes()", _pixel_format);
+       }
+
        if ((d->flags & PIX_FMT_PLANAR) == 0) {
                return 1;
        }
@@ -136,8 +159,8 @@ Image::crop_scale_window (
        }
 
        /* Prepare input data pointers with crop */
-       uint8_t* scale_in_data[components()];
-       for (int c = 0; c < components(); ++c) {
+       uint8_t* scale_in_data[planes()];
+       for (int c = 0; c < planes(); ++c) {
                /* To work out the crop in bytes, start by multiplying
                   the crop by the (average) bytes per pixel.  Then
                   round down so that we don't crop a subsampled pixel until
@@ -150,8 +173,8 @@ Image::crop_scale_window (
        /* Corner of the image within out_size */
        Position<int> const corner ((out_size.width - inter_size.width) / 2, (out_size.height - inter_size.height) / 2);
 
-       uint8_t* scale_out_data[out->components()];
-       for (int c = 0; c < out->components(); ++c) {
+       uint8_t* scale_out_data[out->planes()];
+       for (int c = 0; c < out->planes(); ++c) {
                scale_out_data[c] = out->data()[c] + int (rint (out->bytes_per_pixel(c) * corner.x)) + out->stride()[c] * corner.y;
        }
 
@@ -212,10 +235,11 @@ Image::scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_fo
 void
 Image::yuv_16_black (uint16_t v, bool alpha)
 {
-       memset (data()[0], 0, lines(0) * stride()[0]);
+       memset (data()[0], 0, sample_size(0).height * stride()[0]);
        for (int i = 1; i < 3; ++i) {
                int16_t* p = reinterpret_cast<int16_t*> (data()[i]);
-               for (int y = 0; y < lines(i); ++y) {
+               int const lines = sample_size(i).height;
+               for (int y = 0; y < lines; ++y) {
                        /* We divide by 2 here because we are writing 2 bytes at a time */
                        for (int x = 0; x < line_size()[i] / 2; ++x) {
                                p[x] = v;
@@ -225,7 +249,7 @@ Image::yuv_16_black (uint16_t v, bool alpha)
        }
 
        if (alpha) {
-               memset (data()[3], 0, lines(3) * stride()[3]);
+               memset (data()[3], 0, sample_size(3).height * stride()[3]);
        }
 }
 
@@ -252,17 +276,17 @@ Image::make_black ()
        case PIX_FMT_YUV422P:
        case PIX_FMT_YUV444P:
        case PIX_FMT_YUV411P:
-               memset (data()[0], 0, lines(0) * stride()[0]);
-               memset (data()[1], eight_bit_uv, lines(1) * stride()[1]);
-               memset (data()[2], eight_bit_uv, lines(2) * stride()[2]);
+               memset (data()[0], 0, sample_size(0).height * stride()[0]);
+               memset (data()[1], eight_bit_uv, sample_size(1).height * stride()[1]);
+               memset (data()[2], eight_bit_uv, sample_size(2).height * stride()[2]);
                break;
 
        case PIX_FMT_YUVJ420P:
        case PIX_FMT_YUVJ422P:
        case PIX_FMT_YUVJ444P:
-               memset (data()[0], 0, lines(0) * stride()[0]);
-               memset (data()[1], eight_bit_uv + 1, lines(1) * stride()[1]);
-               memset (data()[2], eight_bit_uv + 1, lines(2) * stride()[2]);
+               memset (data()[0], 0, sample_size(0).height * stride()[0]);
+               memset (data()[1], eight_bit_uv + 1, sample_size(1).height * stride()[1]);
+               memset (data()[2], eight_bit_uv + 1, sample_size(2).height * stride()[2]);
                break;
 
        case PIX_FMT_YUV422P9LE:
@@ -334,12 +358,12 @@ Image::make_black ()
        case PIX_FMT_RGB555LE:
        case PIX_FMT_RGB48LE:
        case PIX_FMT_RGB48BE:
-               memset (data()[0], 0, lines(0) * stride()[0]);
+               memset (data()[0], 0, sample_size(0).height * stride()[0]);
                break;
 
        case PIX_FMT_UYVY422:
        {
-               int const Y = lines(0);
+               int const Y = sample_size(0).height;
                int const X = line_size()[0];
                uint8_t* p = data()[0];
                for (int y = 0; y < Y; ++y) {
@@ -365,7 +389,7 @@ Image::make_transparent ()
                throw PixelFormatError ("make_transparent()", _pixel_format);
        }
 
-       memset (data()[0], 0, lines(0) * stride()[0]);
+       memset (data()[0], 0, sample_size(0).height * stride()[0]);
 }
 
 void
@@ -471,9 +495,10 @@ Image::copy (shared_ptr<const Image> other, Position<int> position)
 void
 Image::read_from_socket (shared_ptr<Socket> socket)
 {
-       for (int i = 0; i < components(); ++i) {
+       for (int i = 0; i < planes(); ++i) {
                uint8_t* p = data()[i];
-               for (int y = 0; y < lines(i); ++y) {
+               int const lines = sample_size(i).height;
+               for (int y = 0; y < lines; ++y) {
                        socket->read (p, line_size()[i]);
                        p += stride()[i];
                }
@@ -483,9 +508,10 @@ Image::read_from_socket (shared_ptr<Socket> socket)
 void
 Image::write_to_socket (shared_ptr<Socket> socket) const
 {
-       for (int i = 0; i < components(); ++i) {
+       for (int i = 0; i < planes(); ++i) {
                uint8_t* p = data()[i];
-               for (int y = 0; y < lines(i); ++y) {
+               int const lines = sample_size(i).height;
+               for (int y = 0; y < lines; ++y) {
                        socket->write (p, line_size()[i]);
                        p += stride()[i];
                }
@@ -500,7 +526,7 @@ Image::bytes_per_pixel (int c) const
                throw PixelFormatError ("bytes_per_pixel()", _pixel_format);
        }
 
-       if (c >= components()) {
+       if (c >= planes()) {
                return 0;
        }
 
@@ -551,7 +577,7 @@ Image::allocate ()
        _stride = (int *) wrapped_av_malloc (4 * sizeof (int));
        _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
 
-       for (int i = 0; i < components(); ++i) {
+       for (int i = 0; i < planes(); ++i) {
                _line_size[i] = ceil (_size.width * bytes_per_pixel(i));
                _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
 
@@ -569,7 +595,7 @@ Image::allocate ()
                   so I'll just over-allocate by 32 bytes and have done with it.  Empirical
                   testing suggests that it works.
                */
-               _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * lines (i) + 32);
+               _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * sample_size(i).height + 32);
        }
 }
 
@@ -580,10 +606,11 @@ Image::Image (Image const & other)
 {
        allocate ();
 
-       for (int i = 0; i < components(); ++i) {
+       for (int i = 0; i < planes(); ++i) {
                uint8_t* p = _data[i];
                uint8_t* q = other._data[i];
-               for (int j = 0; j < lines(i); ++j) {
+               int const lines = sample_size(i).height;
+               for (int j = 0; j < lines; ++j) {
                        memcpy (p, q, _line_size[i]);
                        p += stride()[i];
                        q += other.stride()[i];
@@ -598,10 +625,11 @@ Image::Image (AVFrame* frame)
 {
        allocate ();
 
-       for (int i = 0; i < components(); ++i) {
+       for (int i = 0; i < planes(); ++i) {
                uint8_t* p = _data[i];
                uint8_t* q = frame->data[i];
-               for (int j = 0; j < lines(i); ++j) {
+               int const lines = sample_size(i).height;
+               for (int j = 0; j < lines; ++j) {
                        memcpy (p, q, _line_size[i]);
                        p += stride()[i];
                        /* AVFrame's linesize is what we call `stride' */
@@ -617,11 +645,12 @@ Image::Image (shared_ptr<const Image> other, bool aligned)
 {
        allocate ();
 
-       for (int i = 0; i < components(); ++i) {
+       for (int i = 0; i < planes(); ++i) {
                DCPOMATIC_ASSERT (line_size()[i] == other->line_size()[i]);
                uint8_t* p = _data[i];
                uint8_t* q = other->data()[i];
-               for (int j = 0; j < lines(i); ++j) {
+               int const lines = sample_size(i).height;
+               for (int j = 0; j < lines; ++j) {
                        memcpy (p, q, line_size()[i]);
                        p += stride()[i];
                        q += other->stride()[i];
@@ -659,7 +688,7 @@ Image::swap (Image & other)
 /** Destroy a Image */
 Image::~Image ()
 {
-       for (int i = 0; i < components(); ++i) {
+       for (int i = 0; i < planes(); ++i) {
                av_free (_data[i]);
        }
 
@@ -674,7 +703,7 @@ Image::data () const
        return _data;
 }
 
-int *
+int const *
 Image::line_size () const
 {
        return _line_size;
@@ -726,18 +755,19 @@ merge (list<PositionImage> images)
 bool
 operator== (Image const & a, Image const & b)
 {
-       if (a.components() != b.components() || a.pixel_format() != b.pixel_format() || a.aligned() != b.aligned()) {
+       if (a.planes() != b.planes() || a.pixel_format() != b.pixel_format() || a.aligned() != b.aligned()) {
                return false;
        }
 
-       for (int c = 0; c < a.components(); ++c) {
-               if (a.lines(c) != b.lines(c) || a.line_size()[c] != b.line_size()[c] || a.stride()[c] != b.stride()[c]) {
+       for (int c = 0; c < a.planes(); ++c) {
+               if (a.sample_size(c).height != b.sample_size(c).height || a.line_size()[c] != b.line_size()[c] || a.stride()[c] != b.stride()[c]) {
                        return false;
                }
 
                uint8_t* p = a.data()[c];
                uint8_t* q = b.data()[c];
-               for (int y = 0; y < a.lines(c); ++y) {
+               int const lines = a.sample_size(c).height;
+               for (int y = 0; y < lines; ++y) {
                        if (memcmp (p, q, a.line_size()[c]) != 0) {
                                return false;
                        }
@@ -773,7 +803,8 @@ Image::fade (float f)
                /* 8-bit */
                for (int c = 0; c < 3; ++c) {
                        uint8_t* p = data()[c];
-                       for (int y = 0; y < lines(c); ++y) {
+                       int const lines = sample_size(c).height;
+                       for (int y = 0; y < lines; ++y) {
                                uint8_t* q = p;
                                for (int x = 0; x < line_size()[c]; ++x) {
                                        *q = int (float (*q) * f);
@@ -802,7 +833,8 @@ Image::fade (float f)
                        int const stride_pixels = stride()[c] / 2;
                        int const line_size_pixels = line_size()[c] / 2;
                        uint16_t* p = reinterpret_cast<uint16_t*> (data()[c]);
-                       for (int y = 0; y < lines(c); ++y) {
+                       int const lines = sample_size(c).height;
+                       for (int y = 0; y < lines; ++y) {
                                uint16_t* q = p;
                                for (int x = 0; x < line_size_pixels; ++x) {
                                        *q = int (float (*q) * f);
@@ -832,7 +864,8 @@ Image::fade (float f)
                        int const stride_pixels = stride()[c] / 2;
                        int const line_size_pixels = line_size()[c] / 2;
                        uint16_t* p = reinterpret_cast<uint16_t*> (data()[c]);
-                       for (int y = 0; y < lines(c); ++y) {
+                       int const lines = sample_size(c).height;
+                       for (int y = 0; y < lines; ++y) {
                                uint16_t* q = p;
                                for (int x = 0; x < line_size_pixels; ++x) {
                                        *q = swap_16 (int (float (swap_16 (*q)) * f));
@@ -845,7 +878,7 @@ Image::fade (float f)
 
        case PIX_FMT_UYVY422:
        {
-               int const Y = lines(0);
+               int const Y = sample_size(0).height;
                int const X = line_size()[0];
                uint8_t* p = data()[0];
                for (int y = 0; y < Y; ++y) {
index 492f6212e28db97116250323dae56d59c9e15554..fabcb5675f676bafa0cfcb5b7d83062297e11de3 100644 (file)
@@ -49,14 +49,15 @@ public:
        ~Image ();
 
        uint8_t * const * data () const;
-       int * line_size () const;
+       int const * line_size () const;
        int const * stride () const;
        dcp::Size size () const;
        bool aligned () const;
 
+       int planes () const;
        int components () const;
        int line_factor (int) const;
-       int lines (int) const;
+       dcp::Size sample_size (int) const;
 
        boost::shared_ptr<Image> scale (dcp::Size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat, bool aligned) const;
        boost::shared_ptr<Image> crop_scale_window (Crop c, dcp::Size, dcp::Size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat, bool aligned) const;
index d2747944ffaa22adb09791ef367ee16db5250c7a..f16087a484e028916bf6bdaa9b63034b7b0b65dc 100644 (file)
  *  @brief The main DCP-o-matic GUI.
  */
 
+#include "wx/film_viewer.h"
+#include "wx/film_editor.h"
+#include "wx/job_manager_view.h"
+#include "wx/config_dialog.h"
+#include "wx/wx_util.h"
+#include "wx/new_film_dialog.h"
+#include "wx/wx_signal_manager.h"
+#include "wx/about_dialog.h"
+#include "wx/kdm_dialog.h"
+#include "wx/servers_list_dialog.h"
+#include "wx/hints_dialog.h"
+#include "wx/update_dialog.h"
+#include "wx/content_panel.h"
+#include "wx/report_problem_dialog.h"
+#include "wx/video_waveform_dialog.h"
 #include "lib/film.h"
 #include "lib/config.h"
 #include "lib/util.h"
 #include "lib/cross.h"
 #include "lib/content_factory.h"
 #include "lib/compose.hpp"
-#include "wx/film_viewer.h"
-#include "wx/film_editor.h"
-#include "wx/job_manager_view.h"
-#include "wx/config_dialog.h"
-#include "wx/wx_util.h"
-#include "wx/new_film_dialog.h"
-#include "wx/wx_signal_manager.h"
-#include "wx/about_dialog.h"
-#include "wx/kdm_dialog.h"
-#include "wx/servers_list_dialog.h"
-#include "wx/hints_dialog.h"
-#include "wx/update_dialog.h"
-#include "wx/content_panel.h"
-#include "wx/report_problem_dialog.h"
 #include <dcp/exceptions.h>
 #include <wx/generic/aboutdlgg.h>
 #include <wx/stdpaths.h>
@@ -138,6 +139,7 @@ enum {
        ID_jobs_make_kdms,
        ID_jobs_send_dcp_to_tms,
        ID_jobs_show_dcp,
+       ID_tools_video_waveform,
        ID_tools_hints,
        ID_tools_encoding_servers,
        ID_tools_check_for_updates,
@@ -152,6 +154,7 @@ class DOMFrame : public wxFrame
 public:
        DOMFrame (wxString const & title)
                : wxFrame (NULL, -1, title)
+               , _video_waveform_dialog (0)
                , _hints_dialog (0)
                , _servers_list_dialog (0)
                , _config_dialog (0)
@@ -199,6 +202,7 @@ public:
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::jobs_make_kdms, this),          ID_jobs_make_kdms);
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::jobs_send_dcp_to_tms, this),    ID_jobs_send_dcp_to_tms);
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::jobs_show_dcp, this),           ID_jobs_show_dcp);
+               Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_video_waveform, this),    ID_tools_video_waveform);
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_hints, this),             ID_tools_hints);
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_encoding_servers, this),  ID_tools_encoding_servers);
                Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
@@ -514,6 +518,15 @@ private:
 #endif
        }
 
+       void tools_video_waveform ()
+       {
+               if (!_video_waveform_dialog) {
+                       _video_waveform_dialog = new VideoWaveformDialog (this, _film_viewer);
+               }
+
+               _video_waveform_dialog->Show ();
+       }
+
        void tools_hints ()
        {
                if (!_hints_dialog) {
@@ -687,6 +700,7 @@ private:
                add_item (jobs_menu, _("S&how DCP"), ID_jobs_show_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
 
                wxMenu* tools = new wxMenu;
+               add_item (tools, _("Video waveform..."), ID_tools_video_waveform, NEEDS_FILM);
                add_item (tools, _("Hints..."), ID_tools_hints, 0);
                add_item (tools, _("Encoding servers..."), ID_tools_encoding_servers, 0);
                add_item (tools, _("Check for updates"), ID_tools_check_for_updates, 0);
@@ -745,6 +759,7 @@ private:
 
        FilmEditor* _film_editor;
        FilmViewer* _film_viewer;
+       VideoWaveformDialog* _video_waveform_dialog;
        HintsDialog* _hints_dialog;
        ServersListDialog* _servers_list_dialog;
        wxPreferencesEditor* _config_dialog;
index 1badc0282fc2258edb1c77f22867147e96fefc74..9b648a8b970ed28c3fb031a72e70d9b18d8ef237 100644 (file)
@@ -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);
+}
index f6ab1a567efed8e56be5c685aac38f61c88adc7b..eb9d256d3782857b2c83e4d5d06a964a2c105ce7 100644 (file)
@@ -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 (file)
index 0000000..976c4e2
--- /dev/null
@@ -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 (file)
index 0000000..475855d
--- /dev/null
@@ -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 (file)
index 0000000..8c13af7
--- /dev/null
@@ -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 (&note));
+       _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 (file)
index 0000000..440be32
--- /dev/null
@@ -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;
+};
index 94df94ca3104bd36f12fbf7eeac39737cf3768e1..34a7608dfdbfa159371a15fb4565dbe6315cb342 100644 (file)
@@ -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
           """
index e326e00fb5a6da8c93467f40427751759b71a2c5..db198e79c3ebd9d26ab310a74dbf8e8bbb4c01ac 100644 (file)
@@ -139,7 +139,7 @@ BOOST_AUTO_TEST_CASE (client_server_test_yuv)
        shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
        uint8_t* p = image->data()[0];
 
-       for (int i = 0; i < image->components(); ++i) {
+       for (int i = 0; i < image->planes(); ++i) {
                uint8_t* p = image->data()[i];
                for (int j = 0; j < image->line_size()[i]; ++j) {
                        *p++ = j % 256;
index cb1aa35d7e7dd9c0f71628ed5c576970467e0b29..65a6c5ca756a9468d79c469a905f24431aca5f1b 100644 (file)
@@ -35,7 +35,7 @@ using boost::shared_ptr;
 BOOST_AUTO_TEST_CASE (aligned_image_test)
 {
        Image* s = new Image (PIX_FMT_RGB24, dcp::Size (50, 50), true);
-       BOOST_CHECK_EQUAL (s->components(), 1);
+       BOOST_CHECK_EQUAL (s->planes(), 1);
        /* 160 is 150 aligned to the nearest 32 bytes */
        BOOST_CHECK_EQUAL (s->stride()[0], 160);
        BOOST_CHECK_EQUAL (s->line_size()[0], 150);
@@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE (aligned_image_test)
 
        /* copy constructor */
        Image* t = new Image (*s);
-       BOOST_CHECK_EQUAL (t->components(), 1);
+       BOOST_CHECK_EQUAL (t->planes(), 1);
        BOOST_CHECK_EQUAL (t->stride()[0], 160);
        BOOST_CHECK_EQUAL (t->line_size()[0], 150);
        BOOST_CHECK (t->data()[0]);
@@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE (aligned_image_test)
        /* assignment operator */
        Image* u = new Image (PIX_FMT_YUV422P, dcp::Size (150, 150), false);
        *u = *s;
-       BOOST_CHECK_EQUAL (u->components(), 1);
+       BOOST_CHECK_EQUAL (u->planes(), 1);
        BOOST_CHECK_EQUAL (u->stride()[0], 160);
        BOOST_CHECK_EQUAL (u->line_size()[0], 150);
        BOOST_CHECK (u->data()[0]);
@@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE (aligned_image_test)
 BOOST_AUTO_TEST_CASE (compact_image_test)
 {
        Image* s = new Image (PIX_FMT_RGB24, dcp::Size (50, 50), false);
-       BOOST_CHECK_EQUAL (s->components(), 1);
+       BOOST_CHECK_EQUAL (s->planes(), 1);
        BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
        BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
        BOOST_CHECK (s->data()[0]);
@@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE (compact_image_test)
 
        /* copy constructor */
        Image* t = new Image (*s);
-       BOOST_CHECK_EQUAL (t->components(), 1);
+       BOOST_CHECK_EQUAL (t->planes(), 1);
        BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
        BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
        BOOST_CHECK (t->data()[0]);
@@ -112,7 +112,7 @@ BOOST_AUTO_TEST_CASE (compact_image_test)
        /* assignment operator */
        Image* u = new Image (PIX_FMT_YUV422P, dcp::Size (150, 150), true);
        *u = *s;
-       BOOST_CHECK_EQUAL (u->components(), 1);
+       BOOST_CHECK_EQUAL (u->planes(), 1);
        BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
        BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
        BOOST_CHECK (u->data()[0]);
index f99bd717cdbe0d70c0c4ea4e3ad6a5d204a7c6cd..a104e620b516180f284ae5abdf8a80ccc5eaab16 100644 (file)
@@ -18,7 +18,7 @@
 */
 
 /** @file  src/pixel_formats_test.cc
- *  @brief Make sure that Image::lines() and Image::bytes_per_pixel() return the right
+ *  @brief Make sure that Image::sample_size() and Image::bytes_per_pixel() return the right
  *  things for various pixel formats.
  *
  *  @see test/image_test.cc
@@ -42,7 +42,7 @@ struct Case
 {
        Case (AVPixelFormat f, int c, int l0, int l1, int l2, float b0, float b1, float b2)
                : format(f)
-               , components(c)
+               , planes(c)
        {
                lines[0] = l0;
                lines[1] = l1;
@@ -53,7 +53,7 @@ struct Case
        }
 
        AVPixelFormat format;
-       int components;
+       int planes;
        int lines[3];
        float bpp[3];
 };
@@ -82,10 +82,10 @@ BOOST_AUTO_TEST_CASE (pixel_formats_test)
                f->format = static_cast<int> (i->format);
                av_frame_get_buffer (f, true);
                Image t (f);
-               BOOST_CHECK_EQUAL(t.components(), i->components);
-               BOOST_CHECK_EQUAL(t.lines(0), i->lines[0]);
-               BOOST_CHECK_EQUAL(t.lines(1), i->lines[1]);
-               BOOST_CHECK_EQUAL(t.lines(2), i->lines[2]);
+               BOOST_CHECK_EQUAL(t.planes(), i->planes);
+               BOOST_CHECK_EQUAL(t.sample_size(0).height, i->lines[0]);
+               BOOST_CHECK_EQUAL(t.sample_size(1).height, i->lines[1]);
+               BOOST_CHECK_EQUAL(t.sample_size(2).height, i->lines[2]);
                BOOST_CHECK_EQUAL(t.bytes_per_pixel(0), i->bpp[0]);
                BOOST_CHECK_EQUAL(t.bytes_per_pixel(1), i->bpp[1]);
                BOOST_CHECK_EQUAL(t.bytes_per_pixel(2), i->bpp[2]);