extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
+#include <libavutil/pixdesc.h>
}
DCPOMATIC_DISABLE_WARNINGS
#include <libxml++/libxml++.h>
using boost::dynamic_pointer_cast;
using dcp::raw_convert;
-FFmpegImageProxy::FFmpegImageProxy (boost::filesystem::path path)
+FFmpegImageProxy::FFmpegImageProxy (boost::filesystem::path path, VideoRange video_range)
: _data (path)
+ , _video_range (video_range)
, _pos (0)
, _path (path)
{
}
-FFmpegImageProxy::FFmpegImageProxy (dcp::ArrayData data)
+FFmpegImageProxy::FFmpegImageProxy (dcp::ArrayData data, VideoRange video_range)
: _data (data)
+ , _video_range (video_range)
, _pos (0)
{
}
-FFmpegImageProxy::FFmpegImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> socket)
- : _pos (0)
+FFmpegImageProxy::FFmpegImageProxy (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket)
+ : _video_range (string_to_video_range(node->string_child("VideoRange")))
+ , _pos (0)
{
uint32_t const size = socket->read_uint32 ();
_data = dcp::ArrayData (size);
throw DecodeError (N_("could not decode video"));
}
- _image.reset (new Image (frame));
+ AVPixelFormat const pix_fmt = static_cast<AVPixelFormat>(frame->format);
+
+ _image.reset (new Image(frame));
+ if (_video_range == VIDEO_RANGE_VIDEO && av_pix_fmt_desc_get(pix_fmt)->flags & AV_PIX_FMT_FLAG_RGB) {
+ /* Asking for the video range to be converted by libswscale (in Image) will not work for
+ * RGB sources since that method only processes video range in YUV and greyscale. So we have
+ * to do it ourselves here.
+ */
+ _image->video_range_to_full_range();
+ }
av_packet_unref (&packet);
av_frame_free (&frame);
FFmpegImageProxy::add_metadata (xmlpp::Node* node) const
{
node->add_child("Type")->add_child_text (N_("FFmpeg"));
+ node->add_child("VideoRange")->add_child_text(video_range_to_string(_video_range));
}
void
*/
#include "image_proxy.h"
+#include "types.h"
#include <dcp/array_data.h>
#include <boost/thread/mutex.hpp>
#include <boost/filesystem.hpp>
class FFmpegImageProxy : public ImageProxy
{
public:
- explicit FFmpegImageProxy (boost::filesystem::path);
- explicit FFmpegImageProxy (dcp::ArrayData);
+ explicit FFmpegImageProxy (boost::filesystem::path, VideoRange video_range);
+ explicit FFmpegImageProxy (dcp::ArrayData, VideoRange video_range);
FFmpegImageProxy (boost::shared_ptr<cxml::Node> xml, boost::shared_ptr<Socket> socket);
Result image (
private:
dcp::ArrayData _data;
+ VideoRange _video_range;
mutable int64_t _pos;
/** Path of a file that this image came from, if applicable; stored so that
failed-decode errors can give more detail.
return dcp::ArrayData (state.data, state.size);
}
+
+
+void
+Image::video_range_to_full_range ()
+{
+ switch (_pixel_format) {
+ case AV_PIX_FMT_RGB24:
+ {
+ float const factor = 256.0 / 219.0;
+ uint8_t* p = data()[0];
+ int const lines = sample_size(0).height;
+ 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;
+ }
+ p += stride()[0];
+ }
+ break;
+ }
+ case AV_PIX_FMT_GBRP12LE:
+ {
+ float const factor = 4096.0 / 3504.0;
+ for (int c = 0; c < 3; ++c) {
+ uint16_t* p = reinterpret_cast<uint16_t*>(data()[c]);
+ int const lines = sample_size(c).height;
+ for (int y = 0; y < lines; ++y) {
+ 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;
+ }
+ }
+ }
+ break;
+ }
+ default:
+ throw PixelFormatError ("video_range_to_full_range()", _pixel_format);
+ }
+}
+
void alpha_blend (boost::shared_ptr<const Image> image, Position<int> pos);
void copy (boost::shared_ptr<const Image> image, Position<int> pos);
void fade (float);
+ void video_range_to_full_range ();
void read_from_socket (boost::shared_ptr<Socket>);
void write_to_socket (boost::shared_ptr<Socket>) const;
*/
_image.reset (new J2KImageProxy (path, _image_content->video->size(), pf));
} else {
- _image.reset (new FFmpegImageProxy (path));
+ _image.reset (new FFmpegImageProxy(path, _image_content->video->range()));
}
}
}
delete[] buffer;
} else {
- FFmpegImageProxy proxy(content->path(0));
+ FFmpegImageProxy proxy(content->path(0), content->video->range());
_video_size = proxy.image().image->size();
}
return a.name == b.name && a.index == b.index;
}
+
+string
+video_range_to_string (VideoRange r)
+{
+ switch (r) {
+ case VIDEO_RANGE_FULL:
+ return "full";
+ case VIDEO_RANGE_VIDEO:
+ return "video";
+ default:
+ DCPOMATIC_ASSERT (false);
+ }
+}
+
+
+VideoRange
+string_to_video_range (string s)
+{
+ if (s == "full") {
+ return VIDEO_RANGE_FULL;
+ } else if (s == "video") {
+ return VIDEO_RANGE_VIDEO;
+ }
+
+ DCPOMATIC_ASSERT (false);
+ return VIDEO_RANGE_FULL;
+}
+
CHANGE_TYPE_CANCELLED
};
+
enum VideoRange
{
VIDEO_RANGE_FULL, ///< full, or "JPEG" (0-255 for 8-bit)
VIDEO_RANGE_VIDEO ///< video, or "MPEG" (16-235 for 8-bit)
};
+extern std::string video_range_to_string (VideoRange r);
+extern VideoRange string_to_video_range (std::string s);
+
+
/** Type of captions.
*
* The generally accepted definitions seem to be:
emit_subtitle_image (ContentTimePeriod period, dcp::SubtitleImage sub, dcp::Size size, shared_ptr<TextDecoder> decoder)
{
/* XXX: this is rather inefficient; decoding the image just to get its size */
- FFmpegImageProxy proxy (sub.png_image());
+ FFmpegImageProxy proxy (sub.png_image(), VIDEO_RANGE_FULL);
shared_ptr<Image> image = proxy.image().image;
/* set up rect with height and width */
dcpomatic::Rect<double> rect(0, 0, image->size().width / double(size.width), image->size().height / double(size.height));
BOOST_AUTO_TEST_CASE (ffmpeg_image_proxy_same_test)
{
{
- shared_ptr<FFmpegImageProxy> proxy1(new FFmpegImageProxy(data_file0));
- shared_ptr<FFmpegImageProxy> proxy2(new FFmpegImageProxy(data_file0));
+ shared_ptr<FFmpegImageProxy> proxy1(new FFmpegImageProxy(data_file0, VIDEO_RANGE_FULL));
+ shared_ptr<FFmpegImageProxy> proxy2(new FFmpegImageProxy(data_file0, VIDEO_RANGE_FULL));
BOOST_CHECK (proxy1->same(proxy2));
}
{
- shared_ptr<FFmpegImageProxy> proxy1(new FFmpegImageProxy(data_file0));
- shared_ptr<FFmpegImageProxy> proxy2(new FFmpegImageProxy(data_file1));
+ shared_ptr<FFmpegImageProxy> proxy1(new FFmpegImageProxy(data_file0, VIDEO_RANGE_FULL));
+ shared_ptr<FFmpegImageProxy> proxy2(new FFmpegImageProxy(data_file1, VIDEO_RANGE_FULL));
BOOST_CHECK (!proxy1->same(proxy2));
}
}
void
alpha_blend_test_one (AVPixelFormat format, string suffix)
{
- shared_ptr<FFmpegImageProxy> proxy (new FFmpegImageProxy (TestPaths::private_data() / "prophet_frame.tiff"));
+ shared_ptr<FFmpegImageProxy> proxy (new FFmpegImageProxy (TestPaths::private_data() / "prophet_frame.tiff", VIDEO_RANGE_FULL));
shared_ptr<Image> raw = proxy->image().image;
shared_ptr<Image> background = raw->convert_pixel_format (dcp::YUV_TO_RGB_REC709, format, true, false);
/** Test Image::crop_scale_window with YUV420P and some windowing */
BOOST_AUTO_TEST_CASE (crop_scale_window_test)
{
- shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/flat_red.png"));
+ shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/flat_red.png", VIDEO_RANGE_FULL));
shared_ptr<Image> raw = proxy->image().image;
shared_ptr<Image> out = raw->crop_scale_window(Crop(), dcp::Size(1998, 836), dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, VIDEO_RANGE_FULL, AV_PIX_FMT_YUV420P, true, false);
shared_ptr<Image> save = out->scale(dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false);
BOOST_AUTO_TEST_CASE (crop_scale_window_test3)
{
- shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png"));
+ shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png", VIDEO_RANGE_FULL));
shared_ptr<Image> xyz = proxy->image().image->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, true, false);
shared_ptr<Image> cropped = xyz->crop_scale_window(Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, VIDEO_RANGE_FULL, AV_PIX_FMT_RGB24, false, false);
write_image(cropped, "build/test/crop_scale_window_test3.png");
BOOST_AUTO_TEST_CASE (crop_scale_window_test4)
{
- shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png"));
+ shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png", VIDEO_RANGE_FULL));
shared_ptr<Image> xyz = proxy->image().image->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, true, false);
shared_ptr<Image> cropped = xyz->crop_scale_window(Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, VIDEO_RANGE_FULL, AV_PIX_FMT_XYZ12LE, false, false);
write_image(cropped, "build/test/crop_scale_window_test4.png");
BOOST_AUTO_TEST_CASE (crop_scale_window_test5)
{
- shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png"));
+ shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png", VIDEO_RANGE_FULL));
shared_ptr<Image> xyz = proxy->image().image->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_XYZ12LE, true, false);
shared_ptr<Image> cropped = xyz->crop_scale_window(Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, VIDEO_RANGE_FULL, AV_PIX_FMT_RGB24, false, false);
write_image(cropped, "build/test/crop_scale_window_test5.png");
BOOST_AUTO_TEST_CASE (crop_scale_window_test6)
{
- shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png"));
+ shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png", VIDEO_RANGE_FULL));
shared_ptr<Image> xyz = proxy->image().image->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_XYZ12LE, true, false);
shared_ptr<Image> cropped = xyz->crop_scale_window(Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, VIDEO_RANGE_FULL, AV_PIX_FMT_XYZ12LE, false, false);
write_image(cropped, "build/test/crop_scale_window_test6.png");
BOOST_AUTO_TEST_CASE (as_png_test)
{
- shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/3d_test/000001.png"));
+ shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/3d_test/000001.png", VIDEO_RANGE_FULL));
shared_ptr<Image> image_rgb = proxy->image().image;
shared_ptr<Image> image_bgr = image_rgb->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_BGRA, true, false);
image_rgb->as_png().write ("build/test/as_png_rgb.png");
static void
fade_test_format_red (AVPixelFormat f, float amount, string name)
{
- shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/flat_red.png"));
+ shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/flat_red.png", VIDEO_RANGE_FULL));
shared_ptr<Image> red = proxy->image().image->convert_pixel_format(dcp::YUV_TO_RGB_REC709, f, true, false);
red->fade (amount);
string const filename = "fade_test_red_" + name + ".png";
double
rms_error (boost::filesystem::path ref, boost::filesystem::path check)
{
- FFmpegImageProxy ref_proxy (ref);
+ FFmpegImageProxy ref_proxy (ref, VIDEO_RANGE_FULL);
shared_ptr<Image> ref_image = ref_proxy.image().image;
- FFmpegImageProxy check_proxy (check);
+ FFmpegImageProxy check_proxy (check, VIDEO_RANGE_FULL);
shared_ptr<Image> check_image = check_proxy.image().image;
BOOST_REQUIRE_EQUAL (ref_image->pixel_format(), check_image->pixel_format());
--- /dev/null
+/*
+ Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
+
+ This file is part of DCP-o-matic.
+
+ DCP-o-matic is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ DCP-o-matic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+/** @file test/video_level_test.cc
+ * @brief Test that video level ranges are handled correctly.
+ * @ingroup specific
+ */
+
+
+#include "lib/ffmpeg_image_proxy.h"
+#include "lib/image.h"
+#include "test.h"
+#include <boost/test/unit_test.hpp>
+
+
+using boost::shared_ptr;
+
+
+static
+shared_ptr<Image>
+grey_image (dcp::Size size, uint8_t pixel)
+{
+ shared_ptr<Image> grey(new Image(AV_PIX_FMT_RGB24, size, true));
+ for (int y = 0; y < size.height; ++y) {
+ uint8_t* p = grey->data()[0] + y * grey->stride()[0];
+ for (int x = 0; x < size.width; ++x) {
+ *p++ = pixel;
+ *p++ = pixel;
+ *p++ = pixel;
+ }
+ }
+
+ return grey;
+}
+
+
+BOOST_AUTO_TEST_CASE (ffmpeg_image_full_range_not_changed)
+{
+ dcp::Size size(640, 480);
+ uint8_t const grey_pixel = 128;
+ boost::filesystem::path const file = "build/test/ffmpeg_image_full_range_not_changed.png";
+
+ write_image (grey_image(size, grey_pixel), file);
+
+ FFmpegImageProxy proxy (file, VIDEO_RANGE_FULL);
+ ImageProxy::Result result = proxy.image ();
+ BOOST_REQUIRE (!result.error);
+
+ for (int y = 0; y < size.height; ++y) {
+ uint8_t* p = result.image->data()[0] + y * result.image->stride()[0];
+ for (int x = 0; x < size.width; ++x) {
+ BOOST_REQUIRE (*p++ == grey_pixel);
+ }
+ }
+}
+
+
+BOOST_AUTO_TEST_CASE (ffmpeg_image_video_range_expanded)
+{
+ dcp::Size size(640, 480);
+ uint8_t const grey_pixel = 128;
+ uint8_t const expanded_grey_pixel = static_cast<uint8_t>((grey_pixel - 16) * 256.0 / 219);
+ boost::filesystem::path const file = "build/test/ffmpeg_image_video_range_expanded.png";
+
+ write_image (grey_image(size, grey_pixel), file);
+
+ FFmpegImageProxy proxy (file, VIDEO_RANGE_VIDEO);
+ ImageProxy::Result result = proxy.image ();
+ BOOST_REQUIRE (!result.error);
+
+ for (int y = 0; y < size.height; ++y) {
+ uint8_t* p = result.image->data()[0] + y * result.image->stride()[0];
+ for (int x = 0; x < size.width; ++x) {
+ BOOST_REQUIRE_EQUAL (*p++, expanded_grey_pixel);
+ }
+ }
+}
+
+
util_test.cc
vf_test.cc
video_content_scale_test.cc
+ video_level_test.cc
video_mxf_content_test.cc
vf_kdm_test.cc
zipper_test.cc