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/player.h"
42 #include "lib/player_video.h"
43 #include "lib/transcode_job.h"
44 #include "lib/video_decoder.h"
48 #include <dcp/mono_picture_asset.h>
49 #include <dcp/mono_picture_frame.h>
50 #include <dcp/openjpeg_image.h>
52 #include <dcp/reel_picture_asset.h>
53 #include <boost/test/unit_test.hpp>
60 using std::dynamic_pointer_cast;
61 using std::make_shared;
62 using boost::optional;
63 #if BOOST_VERSION >= 106100
64 using namespace boost::placeholders;
66 using std::shared_ptr;
71 grey_image (dcp::Size size, uint8_t pixel)
73 auto grey = make_shared<Image>(AV_PIX_FMT_RGB24, size, Image::Alignment::PADDED);
74 for (int y = 0; y < size.height; ++y) {
75 uint8_t* p = grey->data()[0] + y * grey->stride()[0];
76 for (int x = 0; x < size.width; ++x) {
87 BOOST_AUTO_TEST_CASE (ffmpeg_image_full_range_not_changed)
89 dcp::Size size(640, 480);
90 uint8_t const grey_pixel = 128;
91 boost::filesystem::path const file = "build/test/ffmpeg_image_full_range_not_changed.png";
93 write_image (grey_image(size, grey_pixel), file);
95 FFmpegImageProxy proxy (file);
96 ImageProxy::Result result = proxy.image (Image::Alignment::COMPACT);
97 BOOST_REQUIRE (!result.error);
99 for (int y = 0; y < size.height; ++y) {
100 uint8_t* p = result.image->data()[0] + y * result.image->stride()[0];
101 for (int x = 0; x < size.width; ++x) {
102 BOOST_REQUIRE (*p++ == grey_pixel);
108 BOOST_AUTO_TEST_CASE (ffmpeg_image_video_range_expanded)
110 dcp::Size size(1998, 1080);
111 uint8_t const grey_pixel = 128;
112 uint8_t const expanded_grey_pixel = static_cast<uint8_t>(lrintf((grey_pixel - 16) * 256.0 / 219));
113 boost::filesystem::path const file = "build/test/ffmpeg_image_video_range_expanded.png";
115 write_image(grey_image(size, grey_pixel), file);
117 auto content = content_factory(file).front();
118 auto film = new_test_film2 ("ffmpeg_image_video_range_expanded", { content });
119 content->video->set_range (VideoRange::VIDEO);
120 auto player = make_shared<Player>(film, film->playlist());
122 shared_ptr<PlayerVideo> player_video;
123 player->Video.connect([&player_video](shared_ptr<PlayerVideo> pv, dcpomatic::DCPTime) {
126 while (!player_video) {
127 BOOST_REQUIRE (!player->pass());
130 auto image = player_video->image ([](AVPixelFormat f) { return f; }, VideoRange::FULL, false);
132 for (int y = 0; y < size.height; ++y) {
133 uint8_t* p = image->data()[0] + y * image->stride()[0];
134 for (int x = 0; x < size.width; ++x) {
135 BOOST_REQUIRE_EQUAL (*p++, expanded_grey_pixel);
143 pixel_range (shared_ptr<const Image> image)
145 pair<int, int> range(INT_MAX, 0);
146 switch (image->pixel_format()) {
147 case AV_PIX_FMT_RGB24:
149 dcp::Size const size = image->sample_size(0);
150 for (int y = 0; y < size.height; ++y) {
151 uint8_t* p = image->data()[0] + y * image->stride()[0];
152 for (int x = 0; x < size.width * 3; ++x) {
153 range.first = min(range.first, static_cast<int>(*p));
154 range.second = max(range.second, static_cast<int>(*p));
160 case AV_PIX_FMT_YUV444P:
162 for (int c = 0; c < 3; ++c) {
163 dcp::Size const size = image->sample_size(c);
164 for (int y = 0; y < size.height; ++y) {
165 uint8_t* p = image->data()[c] + y * image->stride()[c];
166 for (int x = 0; x < size.width; ++x) {
167 range.first = min(range.first, static_cast<int>(*p));
168 range.second = max(range.second, static_cast<int>(*p));
175 case AV_PIX_FMT_YUV422P10LE:
176 case AV_PIX_FMT_YUV444P10LE:
177 case AV_PIX_FMT_YUV444P12LE:
179 for (int c = 0; c < 3; ++c) {
180 dcp::Size const size = image->sample_size(c);
181 for (int y = 0; y < size.height; ++y) {
182 uint16_t* p = reinterpret_cast<uint16_t*>(image->data()[c]) + y * image->stride()[c] / 2;
183 for (int x = 0; x < size.width; ++x) {
184 range.first = min(range.first, static_cast<int>(*p));
185 range.second = max(range.second, static_cast<int>(*p));
193 BOOST_REQUIRE_MESSAGE (false, "No support for pixel format " << image->pixel_format());
200 /** @return pixel range of the first frame in @ref content in its raw form, i.e.
201 * straight out of the decoder with no level processing, scaling etc.
205 pixel_range (shared_ptr<const Film> film, shared_ptr<const Content> content)
207 auto decoder = decoder_factory(film, content, false, false, shared_ptr<Decoder>());
208 optional<ContentVideo> content_video;
209 decoder->video->Data.connect ([&content_video](ContentVideo cv) {
212 while (!content_video) {
213 BOOST_REQUIRE (!decoder->pass());
216 return pixel_range (content_video->image->image(Image::Alignment::COMPACT).image);
222 pixel_range (boost::filesystem::path dcp_path)
224 dcp::DCP dcp (dcp_path);
227 auto picture = dynamic_pointer_cast<dcp::MonoPictureAsset>(dcp.cpls().front()->reels().front()->main_picture()->asset());
228 BOOST_REQUIRE (picture);
229 auto frame = picture->start_read()->get_frame(0)->xyz_image();
231 int const width = frame->size().width;
232 int const height = frame->size().height;
234 pair<int, int> range(INT_MAX, 0);
235 for (int c = 0; c < 3; ++c) {
236 for (int y = 0; y < height; ++y) {
237 int* p = frame->data(c) + y * width;
238 for (int x = 0; x < width; ++x) {
239 range.first = min(range.first, *p);
240 range.second = max(range.second, *p);
250 /* Functions to make a Film with different sorts of content.
252 * In these names V = video range (limited)
253 * F = full range (not limited)
260 movie_V (string name)
262 auto film = new_test_film2 (name);
263 auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mp4").front());
264 BOOST_REQUIRE (content);
265 film->examine_and_add_content (content);
266 BOOST_REQUIRE (!wait_for_jobs());
268 auto range = pixel_range (film, content);
269 BOOST_CHECK_EQUAL (range.first, 15);
270 BOOST_CHECK_EQUAL (range.second, 243);
278 movie_VoF (string name)
280 auto film = new_test_film2 (name);
281 auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mp4").front());
282 BOOST_REQUIRE (content);
283 film->examine_and_add_content (content);
284 BOOST_REQUIRE (!wait_for_jobs());
285 content->video->set_range (VideoRange::FULL);
287 auto range = pixel_range (film, content);
288 BOOST_CHECK_EQUAL (range.first, 15);
289 BOOST_CHECK_EQUAL (range.second, 243);
297 movie_F (string name)
299 auto film = new_test_film2 (name);
300 auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mov").front());
301 BOOST_REQUIRE (content);
302 film->examine_and_add_content (content);
303 BOOST_REQUIRE (!wait_for_jobs());
305 auto range = pixel_range (film, content);
306 BOOST_CHECK_EQUAL (range.first, 0);
307 BOOST_CHECK_EQUAL (range.second, 1023);
315 movie_FoV (string name)
317 auto film = new_test_film2 (name);
318 auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mov").front());
319 BOOST_REQUIRE (content);
320 film->examine_and_add_content (content);
321 BOOST_REQUIRE (!wait_for_jobs());
322 content->video->set_range (VideoRange::VIDEO);
324 auto range = pixel_range (film, content);
325 BOOST_CHECK_EQUAL (range.first, 0);
326 BOOST_CHECK_EQUAL (range.second, 1023);
334 image_F (string name)
336 auto film = new_test_film2 (name);
337 auto content = dynamic_pointer_cast<ImageContent>(content_factory("test/data/rgb_grey_testcard.png").front());
338 BOOST_REQUIRE (content);
339 film->examine_and_add_content (content);
340 BOOST_REQUIRE (!wait_for_jobs());
342 auto range = pixel_range (film, content);
343 BOOST_CHECK_EQUAL (range.first, 0);
344 BOOST_CHECK_EQUAL (range.second, 255);
352 image_FoV (string name)
354 auto film = new_test_film2 (name);
355 auto content = dynamic_pointer_cast<ImageContent>(content_factory("test/data/rgb_grey_testcard.png").front());
356 BOOST_REQUIRE (content);
357 film->examine_and_add_content (content);
358 BOOST_REQUIRE (!wait_for_jobs());
359 content->video->set_range (VideoRange::VIDEO);
361 auto range = pixel_range (film, content);
362 /* We are taking some full-range content and saying it should be read as video range, after which its
363 * pixels will still be full range.
365 BOOST_CHECK_EQUAL (range.first, 0);
366 BOOST_CHECK_EQUAL (range.second, 255);
376 boost::filesystem::path const dcp = "test/data/RgbGreyTestcar_TST-1_F_MOS_2K_20201115_SMPTE_OV";
377 auto film = new_test_film2 (name);
378 auto content = make_shared<DCPContent>(dcp);
379 film->examine_and_add_content (content);
380 BOOST_REQUIRE (!wait_for_jobs());
382 auto range = pixel_range (dcp);
383 BOOST_CHECK_EQUAL (range.first, 0);
384 BOOST_CHECK_EQUAL (range.second, 4081);
391 /* Functions to get the pixel range in different sorts of output */
394 /** Get the pixel range in a DCP made from film */
397 dcp_range (shared_ptr<Film> film)
399 make_and_verify_dcp (film);
400 return pixel_range (film->dir(film->dcp_name()));
404 /** Get the pixel range in a video-range movie exported from film */
407 V_movie_range (shared_ptr<Film> film)
409 auto job = make_shared<TranscodeJob>(film);
411 make_shared<FFmpegEncoder>(film, job, film->file("export.mov"), ExportFormat::PRORES, true, false, false, 23)
413 JobManager::instance()->add (job);
414 BOOST_REQUIRE (!wait_for_jobs());
416 /* This is a bit of a hack; add the exported file into the project so we can decode it */
417 auto content = make_shared<FFmpegContent>(film->file("export.mov"));
418 film->examine_and_add_content (content);
419 BOOST_REQUIRE (!wait_for_jobs());
421 return pixel_range (film, content);
428 BOOST_AUTO_TEST_CASE (movie_V_to_dcp)
430 auto range = dcp_range (movie_V("movie_V_to_dcp"));
431 /* Video range has been correctly expanded to full for the DCP */
432 check_int_close (range, {0, 4083}, 2);
436 BOOST_AUTO_TEST_CASE (movie_VoF_to_dcp)
438 auto range = dcp_range (movie_VoF("movie_VoF_to_dcp"));
439 /* We said that video range data was really full range, so here we are in the DCP
440 * with video-range data.
442 check_int_close (range, {350, 3832}, 2);
446 BOOST_AUTO_TEST_CASE (movie_F_to_dcp)
448 auto range = dcp_range (movie_F("movie_F_to_dcp"));
449 /* The nearly-full-range of the input has been preserved */
450 check_int_close (range, {0, 4083}, 2);
454 BOOST_AUTO_TEST_CASE (video_FoV_to_dcp)
456 auto range = dcp_range (movie_FoV("video_FoV_to_dcp"));
457 /* The nearly-full-range of the input has become even more full, and clipped */
458 check_int_close (range, {0, 4095}, 2);
462 BOOST_AUTO_TEST_CASE (image_F_to_dcp)
464 auto range = dcp_range (image_F("image_F_to_dcp"));
465 check_int_close (range, {0, 4083}, 3);
469 BOOST_AUTO_TEST_CASE (image_FoV_to_dcp)
471 auto range = dcp_range (image_FoV("image_FoV_to_dcp"));
472 /* The nearly-full-range of the input has become even more full, and clipped.
473 * XXX: I'm not sure why this doesn't quite hit 4095.
475 check_int_close (range, {0, 4095}, 16);
479 BOOST_AUTO_TEST_CASE (movie_V_to_V_movie)
481 auto range = V_movie_range (movie_V("movie_V_to_V_movie"));
482 BOOST_CHECK_EQUAL (range.first, 60);
483 BOOST_CHECK_EQUAL (range.second, 998);
487 BOOST_AUTO_TEST_CASE (movie_VoF_to_V_movie)
489 auto range = V_movie_range (movie_VoF("movie_VoF_to_V_movie"));
490 BOOST_CHECK_EQUAL (range.first, 116);
491 BOOST_CHECK_EQUAL (range.second, 939);
495 BOOST_AUTO_TEST_CASE (movie_F_to_V_movie)
497 auto range = V_movie_range (movie_F("movie_F_to_V_movie"));
498 BOOST_CHECK_EQUAL (range.first, 4);
499 BOOST_CHECK_EQUAL (range.second, 1019);
503 BOOST_AUTO_TEST_CASE (movie_FoV_to_V_movie)
505 auto range = V_movie_range (movie_FoV("movie_FoV_to_V_movie"));
506 BOOST_CHECK_EQUAL (range.first, 4);
507 BOOST_CHECK_EQUAL (range.second, 1019);
511 BOOST_AUTO_TEST_CASE (image_F_to_V_movie)
513 auto range = V_movie_range (image_F("image_F_to_V_movie"));
514 BOOST_CHECK_EQUAL (range.first, 64);
515 BOOST_CHECK_EQUAL (range.second, 960);
519 BOOST_AUTO_TEST_CASE (image_FoV_to_V_movie)
521 auto range = V_movie_range (image_FoV("image_FoV_to_V_movie"));
522 BOOST_CHECK_EQUAL (range.first, 64);
523 BOOST_CHECK_EQUAL (range.second, 960);
527 BOOST_AUTO_TEST_CASE (dcp_F_to_V_movie)
529 auto range = V_movie_range (dcp_F("dcp_F_to_V_movie"));
530 BOOST_CHECK_EQUAL (range.first, 64);
531 BOOST_CHECK_EQUAL (range.second, 944);