Bump ffmpeg to master somewhere post 3.0.
[dcpomatic.git] / src / lib / image.cc
index 134172038fc04892615cd02af5593de2b4b626e8..7ccb9906eda8663ec6b3eb92a8d56f12802fe7aa 100644 (file)
@@ -124,8 +124,29 @@ Image::crop_scale_window (
        DCPOMATIC_ASSERT (out_size.width >= inter_size.width);
        DCPOMATIC_ASSERT (out_size.height >= inter_size.height);
 
-       /* Here's an image of out_size */
-       shared_ptr<Image> out (new Image (out_format, out_size, out_aligned));
+       /* Here's an image of out_size.  Below we may write to it starting at an offset so we get some padding.
+          Hence we want to write in the following pattern:
+
+          block start   write start                                  line end
+          |..(padding)..|<------line-size------------->|..(padding)..|
+          |..(padding)..|<------line-size------------->|..(padding)..|
+          |..(padding)..|<------line-size------------->|..(padding)..|
+
+          where line-size is of the smaller (inter_size) image and the full padded line length is that of
+          out_size.  To get things to work we have to tell FFmpeg that the stride is that of out_size.
+          However some parts of FFmpeg (notably rgb48Toxyz12 in swscale.c) process data for the full
+          specified *stride*.  This does not matter until we get to the last line:
+
+          block start   write start                                  line end
+          |..(padding)..|<------line-size------------->|XXXwrittenXXX|
+          |XXXwrittenXXX|<------line-size------------->|XXXwrittenXXX|
+          |XXXwrittenXXX|<------line-size------------->|XXXwrittenXXXXXXwrittenXXX
+                                                                      ^^^^ out of bounds
+
+          To get around this, we ask Image to overallocate its buffers by the overrun.
+       */
+
+       shared_ptr<Image> out (new Image (out_format, out_size, out_aligned, (out_size.width - inter_size.width) / 2));
        out->make_black ();
 
        /* Size of the image after any crop */
@@ -360,6 +381,7 @@ Image::make_black ()
        case AV_PIX_FMT_RGB555LE:
        case AV_PIX_FMT_RGB48LE:
        case AV_PIX_FMT_RGB48BE:
+       case AV_PIX_FMT_XYZ12LE:
                memset (data()[0], 0, sample_size(0).height * stride()[0]);
                break;
 
@@ -463,10 +485,35 @@ Image::alpha_blend (shared_ptr<const Image> other, Position<int> position)
                        uint8_t* op = other->data()[0] + oy * other->stride()[0];
                        for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
                                float const alpha = float (op[3]) / 255;
-                               /* Blend high bytes */
-                               tp[1] = op[0] * alpha + tp[1] * (1 - alpha);
+                               /* Blend high bytes; the RGBA in op appears to be BGRA */
+                               tp[1] = op[2] * alpha + tp[1] * (1 - alpha);
                                tp[3] = op[1] * alpha + tp[3] * (1 - alpha);
-                               tp[5] = op[2] * alpha + tp[5] * (1 - alpha);
+                               tp[5] = op[0] * alpha + tp[5] * (1 - alpha);
+
+                               tp += this_bpp;
+                               op += other_bpp;
+                       }
+               }
+               break;
+       }
+       case AV_PIX_FMT_XYZ12LE:
+       {
+               int const this_bpp = 6;
+               for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
+                       uint8_t* tp = data()[0] + ty * stride()[0] + start_tx * this_bpp;
+                       uint8_t* op = other->data()[0] + oy * other->stride()[0];
+                       for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
+                               float const alpha = float (op[3]) / 255;
+
+                               /* Convert sRGB to XYZ; op is BGRA */
+                               int const x = 0.4124564 + op[2] + 0.3575761 * op[1] + 0.1804375 * op[0];
+                               int const y = 0.2126729 + op[2] + 0.7151522 * op[1] + 0.0721750 * op[0];
+                               int const z = 0.0193339 + op[2] + 0.1191920 * op[1] + 0.9503041 * op[0];
+
+                               /* Blend high bytes */
+                               tp[1] = min (x, 255) * alpha + tp[1] * (1 - alpha);
+                               tp[3] = min (y, 255) * alpha + tp[3] * (1 - alpha);
+                               tp[5] = min (z, 255) * alpha + tp[5] * (1 - alpha);
 
                                tp += this_bpp;
                                op += other_bpp;
@@ -534,15 +581,15 @@ Image::bytes_per_pixel (int c) const
 
        float bpp[4] = { 0, 0, 0, 0 };
 
-       bpp[0] = floor ((d->comp[0].depth_minus1 + 1 + 7) / 8);
+       bpp[0] = floor ((d->comp[0].depth + 7) / 8);
        if (d->nb_components > 1) {
-               bpp[1] = floor ((d->comp[1].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
+               bpp[1] = floor ((d->comp[1].depth + 7) / 8) / pow (2.0f, d->log2_chroma_w);
        }
        if (d->nb_components > 2) {
-               bpp[2] = floor ((d->comp[2].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
+               bpp[2] = floor ((d->comp[2].depth + 7) / 8) / pow (2.0f, d->log2_chroma_w);
        }
        if (d->nb_components > 3) {
-               bpp[3] = floor ((d->comp[3].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
+               bpp[3] = floor ((d->comp[3].depth + 7) / 8) / pow (2.0f, d->log2_chroma_w);
        }
 
        if ((d->flags & AV_PIX_FMT_FLAG_PLANAR) == 0) {
@@ -558,11 +605,13 @@ Image::bytes_per_pixel (int c) const
  *
  *  @param p Pixel format.
  *  @param s Size in pixels.
+ *  @param extra_pixels Amount of extra "run-off" memory to allocate at the end of each plane in pixels.
  */
-Image::Image (AVPixelFormat p, dcp::Size s, bool aligned)
+Image::Image (AVPixelFormat p, dcp::Size s, bool aligned, int extra_pixels)
        : _size (s)
        , _pixel_format (p)
        , _aligned (aligned)
+       , _extra_pixels (extra_pixels)
 {
        allocate ();
 }
@@ -597,7 +646,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] * sample_size(i).height + 32);
+               _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * sample_size(i).height + _extra_pixels * bytes_per_pixel(i) + 32);
        }
 }
 
@@ -605,6 +654,7 @@ Image::Image (Image const & other)
        : _size (other._size)
        , _pixel_format (other._pixel_format)
        , _aligned (other._aligned)
+       , _extra_pixels (other._extra_pixels)
 {
        allocate ();
 
@@ -624,6 +674,7 @@ Image::Image (AVFrame* frame)
        : _size (frame->width, frame->height)
        , _pixel_format (static_cast<AVPixelFormat> (frame->format))
        , _aligned (true)
+       , _extra_pixels (0)
 {
        allocate ();
 
@@ -644,6 +695,7 @@ Image::Image (shared_ptr<const Image> other, bool aligned)
        : _size (other->_size)
        , _pixel_format (other->_pixel_format)
        , _aligned (aligned)
+       , _extra_pixels (other->_extra_pixels)
 {
        allocate ();
 
@@ -830,6 +882,7 @@ Image::fade (float f)
        case AV_PIX_FMT_YUVA422P10LE:
        case AV_PIX_FMT_YUVA444P10LE:
        case AV_PIX_FMT_RGB48LE:
+       case AV_PIX_FMT_XYZ12LE:
                /* 16-bit little-endian */
                for (int c = 0; c < 3; ++c) {
                        int const stride_pixels = stride()[c] / 2;