Try again to sort out image alignment a bit.
authorCarl Hetherington <cth@carlh.net>
Sun, 16 Dec 2012 23:43:22 +0000 (23:43 +0000)
committerCarl Hetherington <cth@carlh.net>
Sun, 16 Dec 2012 23:43:22 +0000 (23:43 +0000)
src/lib/dcp_video_frame.cc
src/lib/image.cc
src/lib/image.h
src/lib/imagemagick_decoder.cc
src/lib/matcher.cc
src/lib/server.cc
src/lib/subtitle.cc
src/lib/util.cc
src/lib/video_decoder.cc
src/wx/film_viewer.cc
test/test.cc

index 996aff33f7016d7c86f7e2a75ac537185543faab..7099f73defc9a8c7a1b70e53efc37e8ae2387d8d 100644 (file)
@@ -154,10 +154,10 @@ shared_ptr<EncodedData>
 DCPVideoFrame::encode_locally ()
 {
        if (!_post_process.empty ()) {
-               _input = _input->post_process (_post_process);
+               _input = _input->post_process (_post_process, true);
        }
        
-       shared_ptr<Image> prepared = _input->scale_and_convert_to_rgb (_out_size, _padding, _scaler);
+       shared_ptr<Image> prepared = _input->scale_and_convert_to_rgb (_out_size, _padding, _scaler, true);
 
        if (_subtitle) {
                Rect tx = subtitle_transformed_area (
@@ -166,7 +166,7 @@ DCPVideoFrame::encode_locally ()
                        _subtitle->area(), _subtitle_offset, _subtitle_scale
                        );
 
-               shared_ptr<Image> im = _subtitle->image()->scale (tx.size(), _scaler);
+               shared_ptr<Image> im = _subtitle->image()->scale (tx.size(), _scaler, true);
                prepared->alpha_blend (im, tx.position());
        }
 
index 2e4c18323d22a77430c8a8645783caef286c95c2..fb72d1aee90315c59fbc4010785d4b926f37ce6f 100644 (file)
@@ -89,11 +89,11 @@ Image::components () const
 }
 
 shared_ptr<Image>
-Image::scale (Size out_size, Scaler const * scaler) const
+Image::scale (Size out_size, Scaler const * scaler, bool aligned) const
 {
        assert (scaler);
 
-       shared_ptr<Image> scaled (new AlignedImage (pixel_format(), out_size));
+       shared_ptr<Image> scaled (new SimpleImage (pixel_format(), out_size, aligned));
 
        struct SwsContext* scale_context = sws_getContext (
                size().width, size().height, pixel_format(),
@@ -118,14 +118,14 @@ Image::scale (Size out_size, Scaler const * scaler) const
  *  @param scaler Scaler to use.
  */
 shared_ptr<Image>
-Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scaler) const
+Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scaler, bool aligned) const
 {
        assert (scaler);
 
        Size content_size = out_size;
        content_size.width -= (padding * 2);
 
-       shared_ptr<Image> rgb (new CompactImage (PIX_FMT_RGB24, content_size));
+       shared_ptr<Image> rgb (new SimpleImage (PIX_FMT_RGB24, content_size, aligned));
 
        struct SwsContext* scale_context = sws_getContext (
                size().width, size().height, pixel_format(),
@@ -146,7 +146,7 @@ Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scal
           scheme of things.
        */
        if (padding > 0) {
-               shared_ptr<Image> padded_rgb (new AlignedImage (PIX_FMT_RGB24, out_size));
+               shared_ptr<Image> padded_rgb (new SimpleImage (PIX_FMT_RGB24, out_size, aligned));
                padded_rgb->make_black ();
 
                /* XXX: we are cheating a bit here; we know the frame is RGB so we can
@@ -173,9 +173,9 @@ Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scal
  *  @return Post-processed image.
  */
 shared_ptr<Image>
-Image::post_process (string pp) const
+Image::post_process (string pp, bool aligned) const
 {
-       shared_ptr<Image> out (new AlignedImage (pixel_format(), size ()));
+       shared_ptr<Image> out (new SimpleImage (pixel_format(), size (), aligned));
 
        int pp_format = 0;
        switch (pixel_format()) {
@@ -293,9 +293,10 @@ Image::write_to_socket (shared_ptr<Socket> socket) const
  *  @param p Pixel format.
  *  @param s Size in pixels.
  */
-SimpleImage::SimpleImage (AVPixelFormat p, Size s, function<int (int, int const *)> stride_computer)
+SimpleImage::SimpleImage (AVPixelFormat p, Size s, bool aligned)
        : Image (p)
        , _size (s)
+       , _aligned (aligned)
 {
        _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
        _data[0] = _data[1] = _data[2] = _data[3] = 0;
@@ -329,7 +330,7 @@ SimpleImage::SimpleImage (AVPixelFormat p, Size s, function<int (int, int const
        }
 
        for (int i = 0; i < components(); ++i) {
-               _stride[i] = stride_computer (i, _line_size);
+               _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
                _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
        }
 }
@@ -346,38 +347,8 @@ SimpleImage::~SimpleImage ()
        av_free (_stride);
 }
 
-uint8_t **
-SimpleImage::data () const
-{
-       return _data;
-}
-
-int *
-SimpleImage::line_size () const
-{
-       return _line_size;
-}
-
-int *
-SimpleImage::stride () const
-{
-       return _stride;
-}
-
-Size
-SimpleImage::size () const
-{
-       return _size;
-}
-
-AlignedImage::AlignedImage (AVPixelFormat f, Size s)
-       : SimpleImage (f, s, boost::bind (stride_round_up, _1, _2, 32))
-{
-
-}
-
-AlignedImage::AlignedImage (shared_ptr<const Image> im)
-       : SimpleImage (im->pixel_format(), im->size(), boost::bind (stride_round_up, _1, _2, 32))
+SimpleImage::SimpleImage (shared_ptr<const Image> im, bool aligned)
+       : Image (im->pixel_format())
 {
        assert (components() == im->components());
 
@@ -396,30 +367,28 @@ AlignedImage::AlignedImage (shared_ptr<const Image> im)
        }
 }
 
-CompactImage::CompactImage (AVPixelFormat f, Size s)
-       : SimpleImage (f, s, boost::bind (stride_round_up, _1, _2, 1))
+uint8_t **
+SimpleImage::data () const
 {
-
+       return _data;
 }
 
-CompactImage::CompactImage (shared_ptr<const Image> im)
-       : SimpleImage (im->pixel_format(), im->size(), boost::bind (stride_round_up, _1, _2, 1))
+int *
+SimpleImage::line_size () const
 {
-       assert (components() == im->components());
-
-       for (int c = 0; c < components(); ++c) {
+       return _line_size;
+}
 
-               assert (line_size()[c] == im->line_size()[c]);
+int *
+SimpleImage::stride () const
+{
+       return _stride;
+}
 
-               uint8_t* t = data()[c];
-               uint8_t* o = im->data()[c];
-               
-               for (int y = 0; y < lines(c); ++y) {
-                       memcpy (t, o, line_size()[c]);
-                       t += stride()[c];
-                       o += im->stride()[c];
-               }
-       }
+Size
+SimpleImage::size () const
+{
+       return _size;
 }
 
 FilterBufferImage::FilterBufferImage (AVPixelFormat p, AVFilterBufferRef* b)
@@ -460,7 +429,7 @@ FilterBufferImage::size () const
 }
 
 RGBPlusAlphaImage::RGBPlusAlphaImage (shared_ptr<const Image> im)
-       : SimpleImage (im->pixel_format(), im->size(), boost::bind (stride_round_up, _1, _2, 1))
+       : SimpleImage (im->pixel_format(), im->size(), false)
 {
        assert (im->pixel_format() == PIX_FMT_RGBA);
 
index 0cd38da11a9b97b0871488801541fc98d332ccdf..22758a6cf66f2f826939e670f344c1206185a2ab 100644 (file)
@@ -69,9 +69,10 @@ public:
 
        int components () const;
        int lines (int) const;
-       boost::shared_ptr<Image> scale_and_convert_to_rgb (Size, int, Scaler const *) const;
-       boost::shared_ptr<Image> scale (Size, Scaler const *) const;
-       boost::shared_ptr<Image> post_process (std::string) const;
+
+       boost::shared_ptr<Image> scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scaler, bool aligned) const;
+       boost::shared_ptr<Image> scale (Size, Scaler const *, bool aligned) const;
+       boost::shared_ptr<Image> post_process (std::string, bool aligned) const;
        void alpha_blend (boost::shared_ptr<const Image> image, Position pos);
        
        void make_black ();
@@ -111,7 +112,8 @@ private:
 class SimpleImage : public Image
 {
 public:
-       SimpleImage (AVPixelFormat, Size, boost::function<int (int, int const *)> rounder);
+       SimpleImage (AVPixelFormat, Size, bool);
+       SimpleImage (boost::shared_ptr<const Image>, bool aligned);
        ~SimpleImage ();
 
        uint8_t ** data () const;
@@ -125,26 +127,7 @@ private:
        uint8_t** _data; ///< array of pointers to components
        int* _line_size; ///< array of sizes of the data in each line, in pixels (without any alignment padding bytes)
        int* _stride; ///< array of strides for each line (including any alignment padding bytes)
-};
-
-/** @class AlignedImage
- *  @brief An image whose pixel data is padded so that rows always start on 32-byte boundaries.
- */
-class AlignedImage : public SimpleImage
-{
-public:
-       AlignedImage (AVPixelFormat, Size);
-       AlignedImage (boost::shared_ptr<const Image>);
-};
-
-/** @class CompactImage
- *  @brief An image whose pixel data is not padded, so rows may start at any pixel alignment.
- */
-class CompactImage : public SimpleImage
-{
-public:
-       CompactImage (AVPixelFormat, Size);
-       CompactImage (boost::shared_ptr<const Image>);
+       bool _aligned;
 };
 
 class RGBPlusAlphaImage : public SimpleImage
index c0db088938f65e49876bf22fc0ae5bd3b7a99cc5..33bc5ee7bb4eb492f4bdb0490d5e00f91c4977e4 100644 (file)
@@ -78,7 +78,7 @@ ImageMagickDecoder::pass ()
        Magick::Image* magick_image = new Magick::Image (_film->content_path ());
        
        Size size = native_size ();
-       shared_ptr<CompactImage> image (new CompactImage (PIX_FMT_RGB24, size));
+       shared_ptr<SimpleImage> image (new SimpleImage (PIX_FMT_RGB24, size, false));
 
        uint8_t* p = image->data()[0];
        for (int y = 0; y < size.height; ++y) {
index 7b443453922bf537e6e2694929f50c0363ba7579..2dd36c11e79a1d60328eb5d0e828bf1266d5851b 100644 (file)
@@ -81,7 +81,7 @@ Matcher::process_end ()
                
                _log->log (String::compose ("Emitting %1 frames of black video", black_video_frames));
 
-               shared_ptr<Image> black (new CompactImage (_pixel_format.get(), _size.get()));
+               shared_ptr<Image> black (new SimpleImage (_pixel_format.get(), _size.get(), false));
                black->make_black ();
                for (int i = 0; i < black_video_frames; ++i) {
                        Video (black, shared_ptr<Subtitle>());
index 38f9834ff3be354a74361fc60cd6b787fe119748..bea75cff8a41e80294b73cbf7d0c6783236be832 100644 (file)
@@ -113,13 +113,13 @@ Server::process (shared_ptr<Socket> socket)
        PixelFormat pixel_format = (PixelFormat) pixel_format_int;
        Scaler const * scaler = Scaler::from_id (scaler_id);
        
-       shared_ptr<Image> image (new AlignedImage (pixel_format, in_size));
+       shared_ptr<Image> image (new SimpleImage (pixel_format, in_size, true));
 
        image->read_from_socket (socket);
 
        shared_ptr<Subtitle> sub;
        if (subtitle_size.width && subtitle_size.height) {
-               shared_ptr<Image> subtitle_image (new AlignedImage (PIX_FMT_RGBA, subtitle_size));
+               shared_ptr<Image> subtitle_image (new SimpleImage (PIX_FMT_RGBA, subtitle_size, true));
                subtitle_image->read_from_socket (socket);
                sub.reset (new Subtitle (subtitle_position, subtitle_image));
        }
index 282a2cde1a68b9e494af6fcc005d3f88b8f7b4d2..4b7f81947f66480401b8cde3b6387bf31a117b80 100644 (file)
@@ -54,7 +54,7 @@ TimedSubtitle::TimedSubtitle (AVSubtitle const & sub, double c)
                throw DecodeError ("non-bitmap subtitles not yet supported");
        }
        
-       shared_ptr<Image> image (new AlignedImage (PIX_FMT_RGBA, Size (rect->w, rect->h)));
+       shared_ptr<Image> image (new SimpleImage (PIX_FMT_RGBA, Size (rect->w, rect->h), true));
 
        /* Start of the first line in the subtitle */
        uint8_t* sub_p = rect->pict.data[0];
index b69581eba56a1e8b282f75cff6ad02371d737aac..66eaea39e21258328693570609926c475117cffb 100644 (file)
@@ -576,7 +576,8 @@ Rect::intersection (Rect const & other) const
 }
 
 /** Round a number up to the nearest multiple of another number.
- *  @param a Number to round.
+ *  @param c Index.
+ *  @param s Array of numbers to round, indexed by c.
  *  @param t Multiple to round to.
  *  @return Rounded number.
  */
index cb55b4d188a0af99baea677721b417269e628186..c3be2a1743eb45b10bc2b65ceda60c5326c70d55 100644 (file)
@@ -56,7 +56,7 @@ void
 VideoDecoder::repeat_last_video ()
 {
        if (!_last_image) {
-               _last_image.reset (new CompactImage (pixel_format(), native_size()));
+               _last_image.reset (new SimpleImage (pixel_format(), native_size(), false));
                _last_image->make_black ();
        }
 
index 15a2c4034bdb3dda8e3bea2bc374b704a1997e57..78b104d2024e47c10c1aa357ecf0538e5a36d076 100644 (file)
@@ -235,7 +235,8 @@ FilmViewer::raw_to_display ()
                return;
        }
 
-       _display_frame = _raw_frame->scale_and_convert_to_rgb (Size (_out_width, _out_height), 0, _film->scaler());
+       /* Get a compacted image as we have to feed it to wxWidgets */
+       _display_frame = _raw_frame->scale_and_convert_to_rgb (Size (_out_width, _out_height), 0, _film->scaler(), false);
 
        if (_raw_sub) {
                Rect tx = subtitle_transformed_area (
@@ -244,7 +245,7 @@ FilmViewer::raw_to_display ()
                        _raw_sub->area(), _film->subtitle_offset(), _film->subtitle_scale()
                        );
                
-               _display_sub.reset (new RGBPlusAlphaImage (_raw_sub->image()->scale (tx.size(), _film->scaler ())));
+               _display_sub.reset (new RGBPlusAlphaImage (_raw_sub->image()->scale (tx.size(), _film->scaler(), false)));
                _display_sub_position = tx.position();
        } else {
                _display_sub.reset ();
index 2bbf4f08a91d034705cdc89e31a058d7c8aac06d..595492f7c1830ec9e85a87b83288ec148160b824 100644 (file)
@@ -327,7 +327,7 @@ do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription* descriptio
 
 BOOST_AUTO_TEST_CASE (client_server_test)
 {
-       shared_ptr<Image> image (new CompactImage (PIX_FMT_RGB24, Size (1998, 1080)));
+       shared_ptr<Image> image (new SimpleImage (PIX_FMT_RGB24, Size (1998, 1080), false));
        uint8_t* p = image->data()[0];
        
        for (int y = 0; y < 1080; ++y) {
@@ -338,7 +338,7 @@ BOOST_AUTO_TEST_CASE (client_server_test)
                }
        }
 
-       shared_ptr<Image> sub_image (new CompactImage (PIX_FMT_RGBA, Size (100, 200)));
+       shared_ptr<Image> sub_image (new SimpleImage (PIX_FMT_RGBA, Size (100, 200), false));
        p = sub_image->data()[0];
        for (int y = 0; y < 200; ++y) {
                for (int x = 0; x < 100; ++x) {