X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fimage.cc;h=8e6c5717b9064025a1adb03a619731a13835b347;hb=fc1441eeaa3c0805c37809685ea7a3f5ca173666;hp=3a23d1abb49c69aa077644edee893f7b621c3029;hpb=edbccd8d04a33f9e8d03677d8ebc671f40b0f822;p=dcpomatic.git diff --git a/src/lib/image.cc b/src/lib/image.cc index 3a23d1abb..8e6c5717b 100644 --- a/src/lib/image.cc +++ b/src/lib/image.cc @@ -31,14 +31,17 @@ #include "rect.h" #include "timer.h" #include "util.h" +#include "warnings.h" #include #include +DCPOMATIC_DISABLE_WARNINGS extern "C" { #include #include #include #include } +DCPOMATIC_ENABLE_WARNINGS #include #if HAVE_VALGRIND_MEMCHECK_H #include @@ -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(size().width) / horizontal_factor (n))), - lrint (ceil(static_cast(size().height) / vertical_factor (n))) + lrint (ceil(static_cast(size().width) / horizontal_factor(n))), + lrint (ceil(static_cast(size().height) / vertical_factor(n))) ); } @@ -239,8 +251,8 @@ Image::crop_scale_window ( */ sws_setColorspaceDetails ( scale_context, - sws_getCoefficients (lut[static_cast(yuv_to_rgb)]), video_range == VIDEO_RANGE_VIDEO ? 0 : 1, - sws_getCoefficients (lut[static_cast(yuv_to_rgb)]), out_video_range == VIDEO_RANGE_VIDEO ? 0 : 1, + sws_getCoefficients (lut[static_cast(yuv_to_rgb)]), video_range == VideoRange::VIDEO ? 0 : 1, + sws_getCoefficients (lut[static_cast(yuv_to_rgb)]), out_video_range == VideoRange::VIDEO ? 0 : 1, 0, 1 << 16, 1 << 16 ); @@ -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(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,11 +1027,13 @@ Image::Image (Image const & other) } } -Image::Image (AVFrame* frame) +Image::Image (AVFrame const * frame) : _size (frame->width, frame->height) - , _pixel_format (static_cast (frame->format)) + , _pixel_format (static_cast(frame->format)) , _aligned (true) { + DCPOMATIC_ASSERT (_pixel_format != AV_PIX_FMT_NONE); + allocate (); for (int i = 0; i < planes(); ++i) { @@ -1388,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(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; @@ -1405,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; } }