Change MagickImageProxy to FFmpegImageProxy and make it use FFmpeg
[dcpomatic.git] / src / lib / image.cc
index 158bce1d901aab1b7ff3de884ce8b090bb035805..0590a4e78901e3a0adceb2a2a3238cd56fb9ca36 100644 (file)
@@ -36,6 +36,9 @@ extern "C" {
 #include <libavutil/pixdesc.h>
 #include <libavutil/frame.h>
 }
+#if HAVE_VALGRIND_MEMCHECK_H
+#include <valgrind/memcheck.h>
+#endif
 #include <iostream>
 
 #include "i18n.h"
@@ -183,8 +186,8 @@ Image::crop_scale_window (
                0, 1 << 16, 1 << 16
                );
 
-       AVPixFmtDescriptor const * desc = av_pix_fmt_desc_get (_pixel_format);
-       if (!desc) {
+       AVPixFmtDescriptor const * in_desc = av_pix_fmt_desc_get (_pixel_format);
+       if (!in_desc) {
                throw PixelFormatError ("crop_scale_window()", _pixel_format);
        }
 
@@ -196,16 +199,23 @@ Image::crop_scale_window (
                   round down so that we don't crop a subsampled pixel until
                   we've cropped all of its Y-channel pixels.
                */
-               int const x = lrintf (bytes_per_pixel(c) * crop.left) & ~ ((int) desc->log2_chroma_w);
+               int const x = lrintf (bytes_per_pixel(c) * crop.left) & ~ ((int) in_desc->log2_chroma_w);
                scale_in_data[c] = data()[c] + x + stride()[c] * (crop.top / vertical_factor(c));
        }
 
        /* 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);
 
+       AVPixFmtDescriptor const * out_desc = av_pix_fmt_desc_get (out_format);
+       if (!out_desc) {
+               throw PixelFormatError ("crop_scale_window()", out_format);
+       }
+
        uint8_t* scale_out_data[out->planes()];
        for (int c = 0; c < out->planes(); ++c) {
-               scale_out_data[c] = out->data()[c] + lrintf (out->bytes_per_pixel(c) * corner.x) + out->stride()[c] * (corner.y / out->vertical_factor(c));
+               /* See the note in the crop loop above */
+               int const x = lrintf (out->bytes_per_pixel(c) * corner.x) & ~ ((int) out_desc->log2_chroma_w);
+               scale_out_data[c] = out->data()[c] + x + out->stride()[c] * (corner.y / out->vertical_factor(c));
        }
 
        sws_scale (
@@ -814,11 +824,18 @@ Image::allocate ()
                   testing suggests that it works.
                */
                _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * sample_size(i).height + _extra_pixels * bytes_per_pixel(i) + 32);
+#if HAVE_VALGRIND_MEMCHECK_H
+               /* The data between the end of the line size and the stride is undefined but processed by
+                  libswscale, causing lots of valgrind errors.  Mark it all defined to quell these errors.
+               */
+               VALGRIND_MAKE_MEM_DEFINED (_data[i], _stride[i] * sample_size(i).height + _extra_pixels * bytes_per_pixel(i) + 32);
+#endif
        }
 }
 
 Image::Image (Image const & other)
-       : _size (other._size)
+       : boost::enable_shared_from_this<Image>(other)
+       , _size (other._size)
        , _pixel_format (other._pixel_format)
        , _aligned (other._aligned)
        , _extra_pixels (other._extra_pixels)
@@ -1118,8 +1135,8 @@ Image::fade (float f)
        }
 }
 
-shared_ptr<Image>
-Image::ensure_aligned (shared_ptr<Image> image)
+shared_ptr<const Image>
+Image::ensure_aligned (shared_ptr<const Image> image)
 {
        if (image->aligned()) {
                return image;
@@ -1137,3 +1154,10 @@ Image::memory_used () const
        }
        return m;
 }
+
+dcp::Data
+Image::as_png () const
+{
+       /* XXX */
+       return dcp::Data();
+}