C++11 tidying.
[dcpomatic.git] / src / lib / image.cc
index 52e8878add58203b7b474e9468c3b5fb1fa81879..8e6c5717b9064025a1adb03a619731a13835b347 100644 (file)
 #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
 extern "C" {
 #include <libavutil/frame.h>
 #include <libavutil/pixdesc.h>
 #include <libavutil/pixfmt.h>
 #include <libswscale/swscale.h>
 }
+DCPOMATIC_ENABLE_WARNINGS
 #include <png.h>
 #if HAVE_VALGRIND_MEMCHECK_H
 #include <valgrind/memcheck.h>
@@ -64,6 +67,15 @@ using dcp::Size;
 /** The memory alignment, in bytes, used for each row of an image if aligment is requested */
 #define ALIGNMENT 64
 
+/* U/V black value for 8-bit colour */
+static uint8_t const eight_bit_uv =    (1 << 7) - 1;
+/* U/V black value for 9-bit colour */
+static uint16_t const nine_bit_uv =    (1 << 8) - 1;
+/* U/V black value for 10-bit colour */
+static uint16_t const ten_bit_uv =     (1 << 9) - 1;
+/* U/V black value for 16-bit colour */
+static uint16_t const sixteen_bit_uv = (1 << 15) - 1;
+
 
 int
 Image::vertical_factor (int n) const
@@ -102,8 +114,8 @@ dcp::Size
 Image::sample_size (int n) const
 {
        return dcp::Size (
-               lrint (ceil(static_cast<double>(size().width) / horizontal_factor (n))),
-               lrint (ceil(static_cast<double>(size().height) / vertical_factor (n)))
+               lrint (ceil(static_cast<double>(size().width) / horizontal_factor(n))),
+               lrint (ceil(static_cast<double>(size().height) / vertical_factor(n)))
                );
 }
 
@@ -285,6 +297,15 @@ Image::crop_scale_window (
                out->make_part_black (corner.x + cropped_size.width, out_size.width - cropped_size.width);
        }
 
+       if (
+               video_range == VideoRange::VIDEO &&
+               out_video_range == VideoRange::FULL &&
+               av_pix_fmt_desc_get(_pixel_format)->flags & AV_PIX_FMT_FLAG_RGB
+          ) {
+               /* libswscale will not convert video range for RGB sources, so we have to do it ourselves */
+               out->video_range_to_full_range ();
+       }
+
        return out;
 }
 
@@ -381,8 +402,19 @@ Image::swap_16 (uint16_t v)
 }
 
 void
-Image::make_part_black (int x, int w)
+Image::make_part_black (int const start, int const width)
 {
+       auto y_part = [&]() {
+               int const bpp = bytes_per_pixel(0);
+               int const h = sample_size(0).height;
+               int const s = stride()[0];
+               auto p = data()[0];
+               for (int y = 0; y < h; ++y) {
+                       memset (p + start * bpp, 0, width * bpp);
+                       p += s;
+               }
+       };
+
        switch (_pixel_format) {
        case AV_PIX_FMT_RGB24:
        case AV_PIX_FMT_ARGB:
@@ -399,12 +431,41 @@ Image::make_part_black (int x, int w)
                int const s = stride()[0];
                uint8_t* p = data()[0];
                for (int y = 0; y < h; y++) {
-                       memset (p + x * bpp, 0, w * bpp);
+                       memset (p + start * bpp, 0, width * bpp);
                        p += s;
                }
                break;
        }
-
+       case AV_PIX_FMT_YUV420P:
+       {
+               y_part ();
+               for (int i = 1; i < 3; ++i) {
+                       auto p = data()[i];
+                       int const h = sample_size(i).height;
+                       for (int y = 0; y < h; ++y) {
+                               for (int x = start / 2; x < (start + width) / 2; ++x) {
+                                       p[x] = eight_bit_uv;
+                               }
+                               p += stride()[i];
+                       }
+               }
+               break;
+       }
+       case AV_PIX_FMT_YUV422P10LE:
+       {
+               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 / 2; x < (start + width) / 2; ++x) {
+                                       p[x] = ten_bit_uv;
+                               }
+                               p += stride()[i] / 2;
+                       }
+               }
+               break;
+       }
        default:
                throw PixelFormatError ("make_part_black()", _pixel_format);
        }
@@ -413,15 +474,6 @@ Image::make_part_black (int x, int w)
 void
 Image::make_black ()
 {
-       /* U/V black value for 8-bit colour */
-       static uint8_t const eight_bit_uv =     (1 << 7) - 1;
-       /* U/V black value for 9-bit colour */
-       static uint16_t const nine_bit_uv =     (1 << 8) - 1;
-       /* U/V black value for 10-bit colour */
-       static uint16_t const ten_bit_uv =      (1 << 9) - 1;
-       /* U/V black value for 16-bit colour */
-       static uint16_t const sixteen_bit_uv =  (1 << 15) - 1;
-
        switch (_pixel_format) {
        case AV_PIX_FMT_YUV420P:
        case AV_PIX_FMT_YUV422P:
@@ -975,7 +1027,7 @@ Image::Image (Image const & other)
        }
 }
 
-Image::Image (AVFrame* frame)
+Image::Image (AVFrame const * frame)
        : _size (frame->width, frame->height)
        , _pixel_format (static_cast<AVPixelFormat>(frame->format))
        , _aligned (true)
@@ -1390,13 +1442,29 @@ Image::video_range_to_full_range ()
                for (int y = 0; y < lines; ++y) {
                        uint8_t* q = p;
                        for (int x = 0; x < line_size()[0]; ++x) {
-                               *q = int((*q - 16) * factor);
+                               *q = clamp(lrintf((*q - 16) * factor), 0L, 255L);
                                ++q;
                        }
                        p += stride()[0];
                }
                break;
        }
+       case AV_PIX_FMT_RGB48LE:
+       {
+               float const factor = 65536.0 / 56064.0;
+               uint16_t* p = reinterpret_cast<uint16_t*>(data()[0]);
+               int const lines = sample_size(0).height;
+               for (int y = 0; y < lines; ++y) {
+                       uint16_t* q = p;
+                       int const line_size_pixels = line_size()[0] / 2;
+                       for (int x = 0; x < line_size_pixels; ++x) {
+                               *q = clamp(lrintf((*q - 4096) * factor), 0L, 65535L);
+                               ++q;
+                       }
+                       p += stride()[0] / 2;
+               }
+               break;
+       }
        case AV_PIX_FMT_GBRP12LE:
        {
                float const factor = 4096.0 / 3504.0;
@@ -1407,7 +1475,7 @@ Image::video_range_to_full_range ()
                                uint16_t* q = p;
                                int const line_size_pixels = line_size()[c] / 2;
                                for (int x = 0; x < line_size_pixels; ++x) {
-                                       *q = int((*q - 256) * factor);
+                                       *q = clamp(lrintf((*q - 256) * factor), 0L, 4095L);
                                        ++q;
                                }
                        }