2 Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 /** @file test/video_level_test.cc
23 * @brief Test that video level ranges are handled correctly.
28 #include "lib/content_factory.h"
29 #include "lib/content_video.h"
30 #include "lib/dcp_content.h"
31 #include "lib/decoder_factory.h"
33 #include "lib/ffmpeg_content.h"
34 #include "lib/ffmpeg_decoder.h"
35 #include "lib/ffmpeg_image_proxy.h"
36 #include "lib/image.h"
37 #include "lib/image_content.h"
38 #include "lib/image_decoder.h"
39 #include "lib/ffmpeg_encoder.h"
40 #include "lib/job_manager.h"
41 #include "lib/transcode_job.h"
42 #include "lib/video_decoder.h"
46 #include <dcp/mono_picture_asset.h>
47 #include <dcp/mono_picture_frame.h>
48 #include <dcp/openjpeg_image.h>
50 #include <dcp/reel_picture_asset.h>
51 #include <boost/test/unit_test.hpp>
59 using std::dynamic_pointer_cast;
60 using std::make_shared;
61 using boost::optional;
62 #if BOOST_VERSION >= 106100
63 using namespace boost::placeholders;
65 using std::shared_ptr;
70 grey_image (dcp::Size size, uint8_t pixel)
72 auto grey = make_shared<Image>(AV_PIX_FMT_RGB24, size, true);
73 for (int y = 0; y < size.height; ++y) {
74 uint8_t* p = grey->data()[0] + y * grey->stride()[0];
75 for (int x = 0; x < size.width; ++x) {
86 BOOST_AUTO_TEST_CASE (ffmpeg_image_full_range_not_changed)
88 dcp::Size size(640, 480);
89 uint8_t const grey_pixel = 128;
90 boost::filesystem::path const file = "build/test/ffmpeg_image_full_range_not_changed.png";
92 write_image (grey_image(size, grey_pixel), file);
94 FFmpegImageProxy proxy (file, VideoRange::FULL);
95 ImageProxy::Result result = proxy.image ();
96 BOOST_REQUIRE (!result.error);
98 for (int y = 0; y < size.height; ++y) {
99 uint8_t* p = result.image->data()[0] + y * result.image->stride()[0];
100 for (int x = 0; x < size.width; ++x) {
101 BOOST_REQUIRE (*p++ == grey_pixel);
107 BOOST_AUTO_TEST_CASE (ffmpeg_image_video_range_expanded)
109 dcp::Size size(640, 480);
110 uint8_t const grey_pixel = 128;
111 uint8_t const expanded_grey_pixel = static_cast<uint8_t>((grey_pixel - 16) * 256.0 / 219);
112 boost::filesystem::path const file = "build/test/ffmpeg_image_video_range_expanded.png";
114 write_image (grey_image(size, grey_pixel), file);
116 FFmpegImageProxy proxy (file, VideoRange::VIDEO);
117 ImageProxy::Result result = proxy.image ();
118 BOOST_REQUIRE (!result.error);
120 for (int y = 0; y < size.height; ++y) {
121 uint8_t* p = result.image->data()[0] + y * result.image->stride()[0];
122 for (int x = 0; x < size.width; ++x) {
123 BOOST_REQUIRE_EQUAL (*p++, expanded_grey_pixel);
131 pixel_range (shared_ptr<const Image> image)
133 pair<int, int> range(INT_MAX, 0);
134 switch (image->pixel_format()) {
135 case AV_PIX_FMT_RGB24:
137 dcp::Size const size = image->sample_size(0);
138 for (int y = 0; y < size.height; ++y) {
139 uint8_t* p = image->data()[0] + y * image->stride()[0];
140 for (int x = 0; x < size.width * 3; ++x) {
141 range.first = min(range.first, static_cast<int>(*p));
142 range.second = max(range.second, static_cast<int>(*p));
148 case AV_PIX_FMT_YUV444P:
150 for (int c = 0; c < 3; ++c) {
151 dcp::Size const size = image->sample_size(c);
152 for (int y = 0; y < size.height; ++y) {
153 uint8_t* p = image->data()[c] + y * image->stride()[c];
154 for (int x = 0; x < size.width; ++x) {
155 range.first = min(range.first, static_cast<int>(*p));
156 range.second = max(range.second, static_cast<int>(*p));
163 case AV_PIX_FMT_YUV422P10LE:
164 case AV_PIX_FMT_YUV444P10LE:
165 case AV_PIX_FMT_YUV444P12LE:
167 for (int c = 0; c < 3; ++c) {
168 dcp::Size const size = image->sample_size(c);
169 for (int y = 0; y < size.height; ++y) {
170 uint16_t* p = reinterpret_cast<uint16_t*>(image->data()[c]) + y * image->stride()[c] / 2;
171 for (int x = 0; x < size.width; ++x) {
172 range.first = min(range.first, static_cast<int>(*p));
173 range.second = max(range.second, static_cast<int>(*p));
181 BOOST_REQUIRE_MESSAGE (false, "No support for pixel format " << image->pixel_format());
190 pixel_range (shared_ptr<const Film> film, shared_ptr<const Content> content)
192 auto decoder = decoder_factory(film, content, false, false, shared_ptr<Decoder>());
193 optional<ContentVideo> content_video;
194 decoder->video->Data.connect ([&content_video](ContentVideo cv) {
197 while (!content_video) {
198 BOOST_REQUIRE (!decoder->pass());
201 return pixel_range (content_video->image->image().image);
207 pixel_range (boost::filesystem::path dcp_path)
209 dcp::DCP dcp (dcp_path);
212 auto picture = dynamic_pointer_cast<dcp::MonoPictureAsset>(dcp.cpls().front()->reels().front()->main_picture()->asset());
213 BOOST_REQUIRE (picture);
214 auto frame = picture->start_read()->get_frame(0)->xyz_image();
216 int const width = frame->size().width;
217 int const height = frame->size().height;
219 pair<int, int> range(INT_MAX, 0);
220 for (int c = 0; c < 3; ++c) {
221 for (int y = 0; y < height; ++y) {
222 int* p = frame->data(c) + y * width;
223 for (int x = 0; x < width; ++x) {
224 range.first = min(range.first, *p);
225 range.second = max(range.second, *p);
235 /* Functions to make a Film with different sorts of content.
237 * In these names V = video range (limited)
238 * F = full range (not limited)
245 movie_V (string name)
247 auto film = new_test_film2 (name);
248 auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mp4").front());
249 BOOST_REQUIRE (content);
250 film->examine_and_add_content (content);
251 BOOST_REQUIRE (!wait_for_jobs());
253 auto range = pixel_range (film, content);
254 BOOST_CHECK_EQUAL (range.first, 15);
255 BOOST_CHECK_EQUAL (range.second, 243);
263 movie_VoF (string name)
265 auto film = new_test_film2 (name);
266 auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mp4").front());
267 BOOST_REQUIRE (content);
268 film->examine_and_add_content (content);
269 BOOST_REQUIRE (!wait_for_jobs());
270 content->video->set_range (VideoRange::FULL);
272 auto range = pixel_range (film, content);
273 BOOST_CHECK_EQUAL (range.first, 15);
274 BOOST_CHECK_EQUAL (range.second, 243);
282 movie_F (string name)
284 auto film = new_test_film2 (name);
285 auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mov").front());
286 BOOST_REQUIRE (content);
287 film->examine_and_add_content (content);
288 BOOST_REQUIRE (!wait_for_jobs());
290 auto range = pixel_range (film, content);
291 BOOST_CHECK_EQUAL (range.first, 0);
292 BOOST_CHECK_EQUAL (range.second, 1023);
300 movie_FoV (string name)
302 auto film = new_test_film2 (name);
303 auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mov").front());
304 BOOST_REQUIRE (content);
305 film->examine_and_add_content (content);
306 BOOST_REQUIRE (!wait_for_jobs());
307 content->video->set_range (VideoRange::VIDEO);
309 auto range = pixel_range (film, content);
310 BOOST_CHECK_EQUAL (range.first, 0);
311 BOOST_CHECK_EQUAL (range.second, 1023);
319 image_F (string name)
321 auto film = new_test_film2 (name);
322 auto content = dynamic_pointer_cast<ImageContent>(content_factory("test/data/rgb_grey_testcard.png").front());
323 BOOST_REQUIRE (content);
324 film->examine_and_add_content (content);
325 BOOST_REQUIRE (!wait_for_jobs());
327 auto range = pixel_range (film, content);
328 BOOST_CHECK_EQUAL (range.first, 0);
329 BOOST_CHECK_EQUAL (range.second, 255);
337 image_FoV (string name)
339 auto film = new_test_film2 (name);
340 auto content = dynamic_pointer_cast<ImageContent>(content_factory("test/data/rgb_grey_testcard.png").front());
341 BOOST_REQUIRE (content);
342 film->examine_and_add_content (content);
343 BOOST_REQUIRE (!wait_for_jobs());
344 content->video->set_range (VideoRange::VIDEO);
346 auto range = pixel_range (film, content);
347 /* We are taking some full-range content and saying it should be read as video range, after which its
348 * pixels will still be full range.
350 BOOST_CHECK_EQUAL (range.first, 0);
351 BOOST_CHECK_EQUAL (range.second, 255);
361 boost::filesystem::path const dcp = "test/data/RgbGreyTestcar_TST-1_F_MOS_2K_20201115_SMPTE_OV";
362 auto film = new_test_film2 (name);
363 auto content = make_shared<DCPContent>(dcp);
364 film->examine_and_add_content (content);
365 BOOST_REQUIRE (!wait_for_jobs());
367 auto range = pixel_range (dcp);
368 BOOST_CHECK_EQUAL (range.first, 0);
369 BOOST_CHECK_EQUAL (range.second, 4081);
376 /* Functions to get the pixel range in different sorts of output */
379 /** Get the pixel range in a DCP made from film */
382 dcp_range (shared_ptr<Film> film)
384 make_and_verify_dcp (film);
385 return pixel_range (film->dir(film->dcp_name()));
389 /** Get the pixel range in a video-range movie exported from film */
392 V_movie_range (shared_ptr<Film> film)
394 auto job = make_shared<TranscodeJob>(film);
396 make_shared<FFmpegEncoder>(film, job, film->file("export.mov"), ExportFormat::PRORES, true, false, false, 23)
398 JobManager::instance()->add (job);
399 BOOST_REQUIRE (!wait_for_jobs());
401 /* This is a bit of a hack; add the exported file into the project so we can decode it */
402 auto content = make_shared<FFmpegContent>(film->file("export.mov"));
403 film->examine_and_add_content (content);
404 BOOST_REQUIRE (!wait_for_jobs());
406 return pixel_range (film, content);
413 BOOST_AUTO_TEST_CASE (movie_V_to_dcp)
415 auto range = dcp_range (movie_V("movie_V_to_dcp"));
416 /* Video range has been correctly expanded to full for the DCP */
417 check_int_close (range, {0, 4083}, 2);
421 BOOST_AUTO_TEST_CASE (movie_VoF_to_dcp)
423 auto range = dcp_range (movie_VoF("movie_VoF_to_dcp"));
424 /* We said that video range data was really full range, so here we are in the DCP
425 * with video-range data.
427 check_int_close (range, {350, 3832}, 2);
431 BOOST_AUTO_TEST_CASE (movie_F_to_dcp)
433 auto range = dcp_range (movie_F("movie_F_to_dcp"));
434 /* The nearly-full-range of the input has been preserved */
435 check_int_close (range, {0, 4083}, 2);
439 BOOST_AUTO_TEST_CASE (video_FoV_to_dcp)
441 auto range = dcp_range (movie_FoV("video_FoV_to_dcp"));
442 /* The nearly-full-range of the input has become even more full, and clipped */
443 check_int_close (range, {0, 4095}, 2);
447 BOOST_AUTO_TEST_CASE (image_F_to_dcp)
449 auto range = dcp_range (image_F("image_F_to_dcp"));
450 check_int_close (range, {0, 4083}, 3);
454 BOOST_AUTO_TEST_CASE (image_FoV_to_dcp)
456 auto range = dcp_range (image_FoV("image_FoV_to_dcp"));
457 /* The nearly-full-range of the input has become even more full, and clipped.
458 * XXX: I'm not sure why this doesn't quite hit 4095.
460 check_int_close (range, {0, 4095}, 16);
464 BOOST_AUTO_TEST_CASE (movie_V_to_V_movie)
466 auto range = V_movie_range (movie_V("movie_V_to_V_movie"));
467 BOOST_CHECK_EQUAL (range.first, 60);
468 BOOST_CHECK_EQUAL (range.second, 998);
472 BOOST_AUTO_TEST_CASE (movie_VoF_to_V_movie)
474 auto range = V_movie_range (movie_VoF("movie_VoF_to_V_movie"));
475 BOOST_CHECK_EQUAL (range.first, 116);
476 BOOST_CHECK_EQUAL (range.second, 939);
480 BOOST_AUTO_TEST_CASE (movie_F_to_V_movie)
482 auto range = V_movie_range (movie_F("movie_F_to_V_movie"));
483 BOOST_CHECK_EQUAL (range.first, 4);
484 BOOST_CHECK_EQUAL (range.second, 1019);
488 BOOST_AUTO_TEST_CASE (movie_FoV_to_V_movie)
490 auto range = V_movie_range (movie_FoV("movie_FoV_to_V_movie"));
491 BOOST_CHECK_EQUAL (range.first, 4);
492 BOOST_CHECK_EQUAL (range.second, 1019);
496 BOOST_AUTO_TEST_CASE (image_F_to_V_movie)
498 auto range = V_movie_range (image_F("image_F_to_V_movie"));
499 BOOST_CHECK_EQUAL (range.first, 64);
500 BOOST_CHECK_EQUAL (range.second, 960);
504 BOOST_AUTO_TEST_CASE (image_FoV_to_V_movie)
506 auto range = V_movie_range (image_FoV("image_FoV_to_V_movie"));
507 BOOST_CHECK_EQUAL (range.first, 102);
508 BOOST_CHECK_EQUAL (range.second, 923);
512 BOOST_AUTO_TEST_CASE (dcp_F_to_V_movie)
514 auto range = V_movie_range (dcp_F("dcp_F_to_V_movie"));
515 BOOST_CHECK_EQUAL (range.first, 64);
516 BOOST_CHECK_EQUAL (range.second, 944);