X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fimage.cc;h=5866ee5b418a82b01eec841e832a9fd0bb8db86c;hb=f2b10879dcafccdb28f88d53dc1fc22094b96c1d;hp=a4e04bb626d5a146e0cd0405aa94eec850feb5c7;hpb=7c05a521d19b2613cfdf698ddc9f88c462ceaf18;p=dcpomatic.git diff --git a/src/lib/image.cc b/src/lib/image.cc index a4e04bb62..5866ee5b4 100644 --- a/src/lib/image.cc +++ b/src/lib/image.cc @@ -25,24 +25,25 @@ #include "compose.hpp" +#include "dcpomatic_assert.h" #include "dcpomatic_socket.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 #include -DCPOMATIC_DISABLE_WARNINGS +#include +LIBDCP_DISABLE_WARNINGS extern "C" { #include #include #include #include } -DCPOMATIC_ENABLE_WARNINGS -#include +LIBDCP_ENABLE_WARNINGS #if HAVE_VALGRIND_MEMCHECK_H #include #endif @@ -107,6 +108,7 @@ Image::horizontal_factor (int n) const return lrintf(powf(2.0f, d->log2_chroma_w)); } + /** @param n Component index. * @return Number of samples (i.e. pixels, unless sub-sampled) in each direction for this component. */ @@ -119,19 +121,20 @@ Image::sample_size (int n) const ); } + /** @return Number of planes */ int Image::planes () const { + if (_pixel_format == AV_PIX_FMT_PAL8) { + return 2; + } + auto d = av_pix_fmt_desc_get(_pixel_format); if (!d) { throw PixelFormatError ("planes()", _pixel_format); } - if (_pixel_format == AV_PIX_FMT_PAL8) { - return 2; - } - if ((d->flags & AV_PIX_FMT_FLAG_PLANAR) == 0) { return 1; } @@ -289,13 +292,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 && @@ -309,12 +316,14 @@ Image::crop_scale_window ( return out; } + shared_ptr Image::convert_pixel_format (dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, Alignment out_alignment, bool fast) const { return scale(size(), yuv_to_rgb, out_format, out_alignment, fast); } + /** @param out_size Size to scale to. * @param yuv_to_rgb YUVToRGB transform transform to use, if required. * @param out_format Output pixel format. @@ -373,6 +382,7 @@ Image::scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_fo return scaled; } + /** Blacken a YUV image whose bits per pixel is rounded up to 16 */ void Image::yuv_16_black (uint16_t v, bool alpha) @@ -395,12 +405,14 @@ Image::yuv_16_black (uint16_t v, bool alpha) } } + uint16_t Image::swap_16 (uint16_t v) { return ((v >> 8) & 0xff) | ((v & 0xff) << 8); } + void Image::make_part_black (int const start, int const width) { @@ -471,6 +483,7 @@ Image::make_part_black (int const start, int const width) } } + void Image::make_black () { @@ -586,6 +599,7 @@ Image::make_black () } } + void Image::make_transparent () { @@ -596,6 +610,7 @@ Image::make_transparent () memset (data()[0], 0, sample_size(0).height * stride()[0]); } + void Image::alpha_blend (shared_ptr other, Position position) { @@ -704,8 +719,8 @@ Image::alpha_blend (shared_ptr other, Position 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 (data()[0] + ty * stride()[0] + start_tx * this_bpp); @@ -719,14 +734,14 @@ Image::alpha_blend (shared_ptr other, Position 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; @@ -842,6 +857,7 @@ Image::alpha_blend (shared_ptr other, Position position) } } + void Image::copy (shared_ptr other, Position position) { @@ -857,6 +873,7 @@ Image::copy (shared_ptr other, Position position) } } + void Image::read_from_socket (shared_ptr socket) { @@ -870,6 +887,7 @@ Image::read_from_socket (shared_ptr socket) } } + void Image::write_to_socket (shared_ptr socket) const { @@ -883,6 +901,7 @@ Image::write_to_socket (shared_ptr socket) const } } + float Image::bytes_per_pixel (int c) const { @@ -929,6 +948,7 @@ Image::bytes_per_pixel (int c) const return bpp[c]; } + /** Construct a Image of a given size and format, allocating memory * as required. * @@ -957,9 +977,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. @@ -1008,6 +1033,7 @@ Image::allocate () } } + Image::Image (Image const & other) : std::enable_shared_from_this(other) , _size (other._size) @@ -1028,6 +1054,7 @@ Image::Image (Image const & other) } } + Image::Image (AVFrame const * frame, Alignment alignment) : _size (frame->width, frame->height) , _pixel_format (static_cast(frame->format)) @@ -1050,6 +1077,7 @@ Image::Image (AVFrame const * frame, Alignment alignment) } } + Image::Image (shared_ptr other, Alignment alignment) : _size (other->_size) , _pixel_format (other->_pixel_format) @@ -1070,6 +1098,7 @@ Image::Image (shared_ptr other, Alignment alignment) } } + Image& Image::operator= (Image const & other) { @@ -1082,6 +1111,7 @@ Image::operator= (Image const & other) return *this; } + void Image::swap (Image & other) { @@ -1097,6 +1127,7 @@ Image::swap (Image & other) std::swap (_alignment, other._alignment); } + Image::~Image () { for (int i = 0; i < planes(); ++i) { @@ -1108,30 +1139,35 @@ Image::~Image () av_free (_stride); } + uint8_t * const * Image::data () const { return _data; } + int const * Image::line_size () const { return _line_size; } + int const * Image::stride () const { return _stride; } + dcp::Size Image::size () const { return _size; } + Image::Alignment Image::alignment () const { @@ -1194,6 +1230,7 @@ operator== (Image const & a, Image const & b) return true; } + /** Fade the image. * @param f Amount to fade by; 0 is black, 1 is no fade. */ @@ -1336,103 +1373,6 @@ Image::memory_used () const return m; } -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(png_get_io_ptr(png_ptr)); - size_t size = mem->size + length; - - if (mem->data) { - mem->data = reinterpret_cast(realloc(mem->data, size)); - } else { - mem->data = reinterpret_cast(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(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(const_cast(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_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 ()