Fix some typos in comments.
[dcpomatic.git] / src / lib / image.cc
index 1adcabc065a2f1d3293c504a7c5df001cb05e0fc..3aba3ebd535715a820d437cf0859f4b67a162cb9 100644 (file)
 
 
 #include "compose.hpp"
+#include "dcpomatic_assert.h"
 #include "dcpomatic_socket.h"
+#include "enum_indexed_vector.h"
 #include "exceptions.h"
 #include "image.h"
+#include "maths_util.h"
+#include "memory_util.h"
 #include "rect.h"
 #include "timer.h"
-#include "util.h"
-#include "warnings.h"
 #include <dcp/rgb_xyz.h>
 #include <dcp/transfer_function.h>
-DCPOMATIC_DISABLE_WARNINGS
+#include <dcp/warnings.h>
+LIBDCP_DISABLE_WARNINGS
 extern "C" {
 #include <libavutil/frame.h>
 #include <libavutil/pixdesc.h>
 #include <libavutil/pixfmt.h>
 #include <libswscale/swscale.h>
 }
-DCPOMATIC_ENABLE_WARNINGS
-#include <png.h>
+LIBDCP_ENABLE_WARNINGS
 #if HAVE_VALGRIND_MEMCHECK_H
 #include <valgrind/memcheck.h>
 #endif
@@ -235,10 +237,10 @@ Image::crop_scale_window (
        }
 
        DCPOMATIC_ASSERT (yuv_to_rgb < dcp::YUVToRGB::COUNT);
-       int const lut[static_cast<int>(dcp::YUVToRGB::COUNT)] = {
-               SWS_CS_ITU601,
-               SWS_CS_ITU709
-       };
+       EnumIndexedVector<int, dcp::YUVToRGB> lut;
+       lut[dcp::YUVToRGB::REC601] = SWS_CS_ITU601;
+       lut[dcp::YUVToRGB::REC709] = SWS_CS_ITU709;
+       lut[dcp::YUVToRGB::REC2020] = SWS_CS_BT2020;
 
        /* The 3rd parameter here is:
           0 -> source range MPEG (i.e. "video", 16-235)
@@ -253,8 +255,8 @@ Image::crop_scale_window (
        */
        sws_setColorspaceDetails (
                scale_context,
-               sws_getCoefficients (lut[static_cast<int>(yuv_to_rgb)]), video_range == VideoRange::VIDEO ? 0 : 1,
-               sws_getCoefficients (lut[static_cast<int>(yuv_to_rgb)]), out_video_range == VideoRange::VIDEO ? 0 : 1,
+               sws_getCoefficients(lut[yuv_to_rgb]), video_range == VideoRange::VIDEO ? 0 : 1,
+               sws_getCoefficients(lut[yuv_to_rgb]), out_video_range == VideoRange::VIDEO ? 0 : 1,
                0, 1 << 16, 1 << 16
                );
 
@@ -291,13 +293,17 @@ Image::crop_scale_window (
 
        sws_freeContext (scale_context);
 
-       if (corrected_crop != Crop() && cropped_size == inter_size) {
-               /* We are cropping without any scaling or pixel format conversion, so FFmpeg may have left some
-                  data behind in our image.  Clear it out.  It may get to the point where we should just stop
-                  trying to be clever with cropping.
-               */
-               out->make_part_black (corner.x + cropped_size.width, out_size.width - cropped_size.width);
-       }
+       /* There are some cases where there will be unwanted image data left in the image at this point:
+        *
+        * 1. When we are cropping without any scaling or pixel format conversion.
+        * 2. When we are scaling to certain sizes and placing the result into a larger
+        *    black frame.
+        *
+        * Clear out the sides of the image to take care of those cases.
+        */
+       auto const pad = (out_size.width - inter_size.width) / 2;
+       out->make_part_black(0, pad);
+       out->make_part_black(corner.x + inter_size.width, pad);
 
        if (
                video_range == VideoRange::VIDEO &&
@@ -322,7 +328,7 @@ Image::convert_pixel_format (dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format,
 /** @param out_size Size to scale to.
  *  @param yuv_to_rgb YUVToRGB transform transform to use, if required.
  *  @param out_format Output pixel format.
- *  @param out_aligment Output alignment.
+ *  @param out_alignment Output alignment.
  *  @param fast Try to be fast at the possible expense of quality; at present this means using
  *  fast bilinear rather than bicubic scaling.
  */
@@ -342,10 +348,10 @@ Image::scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_fo
                );
 
        DCPOMATIC_ASSERT (yuv_to_rgb < dcp::YUVToRGB::COUNT);
-       int const lut[static_cast<int>(dcp::YUVToRGB::COUNT)] = {
-               SWS_CS_ITU601,
-               SWS_CS_ITU709
-       };
+       EnumIndexedVector<int, dcp::YUVToRGB> lut;
+       lut[dcp::YUVToRGB::REC601] = SWS_CS_ITU601;
+       lut[dcp::YUVToRGB::REC709] = SWS_CS_ITU709;
+       lut[dcp::YUVToRGB::REC2020] = SWS_CS_BT2020;
 
        /* The 3rd parameter here is:
           0 -> source range MPEG (i.e. "video", 16-235)
@@ -360,8 +366,8 @@ Image::scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_fo
        */
        sws_setColorspaceDetails (
                scale_context,
-               sws_getCoefficients (lut[static_cast<int>(yuv_to_rgb)]), 0,
-               sws_getCoefficients (lut[static_cast<int>(yuv_to_rgb)]), 0,
+               sws_getCoefficients(lut[yuv_to_rgb]), 0,
+               sws_getCoefficients(lut[yuv_to_rgb]), 0,
                0, 1 << 16, 1 << 16
                );
 
@@ -473,6 +479,21 @@ Image::make_part_black (int const start, int const width)
                }
                break;
        }
+       case AV_PIX_FMT_YUV444P10LE:
+       {
+               y_part();
+               for (int i = 1; i < 3; ++i) {
+                       auto p = reinterpret_cast<int16_t*>(data()[i]);
+                       int const h = sample_size(i).height;
+                       for (int y = 0; y < h; ++y) {
+                               for (int x = start; x < (start + width); ++x) {
+                                       p[x] = ten_bit_uv;
+                               }
+                               p += stride()[i] / 2;
+                       }
+               }
+               break;
+       }
        default:
                throw PixelFormatError ("make_part_black()", _pixel_format);
        }
@@ -714,8 +735,8 @@ Image::alpha_blend (shared_ptr<const Image> other, Position<int> position)
                auto conv = dcp::ColourConversion::srgb_to_xyz();
                double fast_matrix[9];
                dcp::combined_rgb_to_xyz (conv, fast_matrix);
-               double const * lut_in = conv.in()->lut (8, false);
-               double const * lut_out = conv.out()->lut (16, true);
+               auto lut_in = conv.in()->lut(0, 1, 8, false);
+               auto lut_out = conv.out()->lut(0, 1, 16, true);
                int const this_bpp = 6;
                for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
                        uint16_t* tp = reinterpret_cast<uint16_t*> (data()[0] + ty * stride()[0] + start_tx * this_bpp);
@@ -729,14 +750,14 @@ Image::alpha_blend (shared_ptr<const Image> other, Position<int> position)
                                double const b = lut_in[op[blue]];
 
                                /* RGB to XYZ, including Bradford transform and DCI companding */
-                               double const x = max (0.0, min (65535.0, r * fast_matrix[0] + g * fast_matrix[1] + b * fast_matrix[2]));
-                               double const y = max (0.0, min (65535.0, r * fast_matrix[3] + g * fast_matrix[4] + b * fast_matrix[5]));
-                               double const z = max (0.0, min (65535.0, r * fast_matrix[6] + g * fast_matrix[7] + b * fast_matrix[8]));
+                               double const x = max(0.0, min(1.0, r * fast_matrix[0] + g * fast_matrix[1] + b * fast_matrix[2]));
+                               double const y = max(0.0, min(1.0, r * fast_matrix[3] + g * fast_matrix[4] + b * fast_matrix[5]));
+                               double const z = max(0.0, min(1.0, r * fast_matrix[6] + g * fast_matrix[7] + b * fast_matrix[8]));
 
                                /* Out gamma LUT and blend */
-                               tp[0] = lrint(lut_out[lrint(x)] * 65535) * alpha + tp[0] * (1 - alpha);
-                               tp[1] = lrint(lut_out[lrint(y)] * 65535) * alpha + tp[1] * (1 - alpha);
-                               tp[2] = lrint(lut_out[lrint(z)] * 65535) * alpha + tp[2] * (1 - alpha);
+                               tp[0] = lrint(lut_out[lrint(x * 65535)] * 65535) * alpha + tp[0] * (1 - alpha);
+                               tp[1] = lrint(lut_out[lrint(y * 65535)] * 65535) * alpha + tp[1] * (1 - alpha);
+                               tp[2] = lrint(lut_out[lrint(z * 65535)] * 65535) * alpha + tp[2] * (1 - alpha);
 
                                tp += this_bpp / 2;
                                op += other_bpp;
@@ -972,9 +993,14 @@ Image::allocate ()
        _stride = (int *) wrapped_av_malloc (4 * sizeof (int));
        _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
 
+       auto stride_round_up = [](int stride, int t) {
+               int const a = stride + (t - 1);
+               return a - (a % t);
+       };
+
        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, _alignment == Alignment::PADDED ? ALIGNMENT : 1);
+               _stride[i] = stride_round_up (_line_size[i], _alignment == Alignment::PADDED ? ALIGNMENT : 1);
 
                /* The assembler function ff_rgb24ToY_avx (in libswscale/x86/input.asm)
                   uses a 16-byte fetch to read three bytes (R/G/B) of image data.
@@ -1364,109 +1390,6 @@ Image::memory_used () const
 }
 
 
-class Memory
-{
-public:
-       Memory ()
-               : data(0)
-               , size(0)
-       {}
-
-       ~Memory ()
-       {
-               free (data);
-       }
-
-       uint8_t* data;
-       size_t size;
-};
-
-
-static void
-png_write_data (png_structp png_ptr, png_bytep data, png_size_t length)
-{
-       auto mem = reinterpret_cast<Memory*>(png_get_io_ptr(png_ptr));
-       size_t size = mem->size + length;
-
-       if (mem->data) {
-               mem->data = reinterpret_cast<uint8_t*>(realloc(mem->data, size));
-       } else {
-               mem->data = reinterpret_cast<uint8_t*>(malloc(size));
-       }
-
-       if (!mem->data) {
-               throw EncodeError (N_("could not allocate memory for PNG"));
-       }
-
-       memcpy (mem->data + mem->size, data, length);
-       mem->size += length;
-}
-
-
-static void
-png_flush (png_structp)
-{
-
-}
-
-
-static void
-png_error_fn (png_structp png_ptr, char const * message)
-{
-       reinterpret_cast<Image*>(png_get_error_ptr(png_ptr))->png_error (message);
-}
-
-
-void
-Image::png_error (char const * message)
-{
-       throw EncodeError (String::compose ("Error during PNG write: %1", message));
-}
-
-
-dcp::ArrayData
-Image::as_png () const
-{
-       DCPOMATIC_ASSERT (bytes_per_pixel(0) == 4);
-       DCPOMATIC_ASSERT (planes() == 1);
-       if (pixel_format() != AV_PIX_FMT_RGBA) {
-               return convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGBA, Image::Alignment::PADDED, false)->as_png();
-       }
-
-       /* error handling? */
-       png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, reinterpret_cast<void*>(const_cast<Image*>(this)), png_error_fn, 0);
-       if (!png_ptr) {
-               throw EncodeError (N_("could not create PNG write struct"));
-       }
-
-       Memory state;
-
-       png_set_write_fn (png_ptr, &state, png_write_data, png_flush);
-
-       png_infop info_ptr = png_create_info_struct(png_ptr);
-       if (!info_ptr) {
-               png_destroy_write_struct (&png_ptr, &info_ptr);
-               throw EncodeError (N_("could not create PNG info struct"));
-       }
-
-       png_set_IHDR (png_ptr, info_ptr, size().width, size().height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
-
-       png_byte ** row_pointers = reinterpret_cast<png_byte **>(png_malloc(png_ptr, size().height * sizeof(png_byte *)));
-       for (int i = 0; i < size().height; ++i) {
-               row_pointers[i] = (png_byte *) (data()[0] + i * stride()[0]);
-       }
-
-       png_write_info (png_ptr, info_ptr);
-       png_write_image (png_ptr, row_pointers);
-       png_write_end (png_ptr, info_ptr);
-
-       png_destroy_write_struct (&png_ptr, &info_ptr);
-       png_free (png_ptr, row_pointers);
-
-       return dcp::ArrayData (state.data, state.size);
-}
-
-
 void
 Image::video_range_to_full_range ()
 {