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 BOOST_CHECK_EQUAL (range.first, 11);
348 BOOST_CHECK_EQUAL (range.second, 250);
358 boost::filesystem::path const dcp = "test/data/RgbGreyTestcar_TST-1_F_MOS_2K_20201115_SMPTE_OV";
359 auto film = new_test_film2 (name);
360 auto content = make_shared<DCPContent>(dcp);
361 film->examine_and_add_content (content);
362 BOOST_REQUIRE (!wait_for_jobs());
364 auto range = pixel_range (dcp);
365 BOOST_CHECK_EQUAL (range.first, 0);
366 BOOST_CHECK_EQUAL (range.second, 4081);
373 /* Functions to get the pixel range in different sorts of output */
376 /** Get the pixel range in a DCP made from film */
379 dcp_range (shared_ptr<Film> film)
381 make_and_verify_dcp (film);
382 return pixel_range (film->dir(film->dcp_name()));
386 /** Get the pixel range in a video-range movie exported from film */
389 V_movie_range (shared_ptr<Film> film)
391 auto job = make_shared<TranscodeJob>(film);
393 make_shared<FFmpegEncoder>(film, job, film->file("export.mov"), ExportFormat::PRORES, true, false, false, 23)
395 JobManager::instance()->add (job);
396 BOOST_REQUIRE (!wait_for_jobs());
398 /* This is a bit of a hack; add the exported file into the project so we can decode it */
399 auto content = make_shared<FFmpegContent>(film->file("export.mov"));
400 film->examine_and_add_content (content);
401 BOOST_REQUIRE (!wait_for_jobs());
403 return pixel_range (film, content);
410 BOOST_AUTO_TEST_CASE (movie_V_to_dcp)
412 auto range = dcp_range (movie_V("movie_V_to_dcp"));
413 /* Video range has been correctly expanded to full for the DCP */
414 check_int_close (range, {0, 4083}, 2);
418 BOOST_AUTO_TEST_CASE (movie_VoF_to_dcp)
420 auto range = dcp_range (movie_VoF("movie_VoF_to_dcp"));
421 /* We said that video range data was really full range, so here we are in the DCP
422 * with video-range data.
424 check_int_close (range, {350, 3832}, 2);
428 BOOST_AUTO_TEST_CASE (movie_F_to_dcp)
430 auto range = dcp_range (movie_F("movie_F_to_dcp"));
431 /* The nearly-full-range of the input has been preserved */
432 check_int_close (range, {0, 4083}, 2);
436 BOOST_AUTO_TEST_CASE (video_FoV_to_dcp)
438 auto range = dcp_range (movie_FoV("video_FoV_to_dcp"));
439 /* The nearly-full-range of the input has become even more full, and clipped */
440 check_int_close (range, {0, 4095}, 2);
444 BOOST_AUTO_TEST_CASE (image_F_to_dcp)
446 auto range = dcp_range (image_F("image_F_to_dcp"));
447 check_int_close (range, {0, 4083}, 3);
451 BOOST_AUTO_TEST_CASE (image_FoV_to_dcp)
453 auto range = dcp_range (image_FoV("image_FoV_to_dcp"));
454 check_int_close (range, {430, 4012}, 2);
458 BOOST_AUTO_TEST_CASE (movie_V_to_V_movie)
460 auto range = V_movie_range (movie_V("movie_V_to_V_movie"));
461 BOOST_CHECK_EQUAL (range.first, 60);
462 BOOST_CHECK_EQUAL (range.second, 998);
466 BOOST_AUTO_TEST_CASE (movie_VoF_to_V_movie)
468 auto range = V_movie_range (movie_VoF("movie_VoF_to_V_movie"));
469 BOOST_CHECK_EQUAL (range.first, 116);
470 BOOST_CHECK_EQUAL (range.second, 939);
474 BOOST_AUTO_TEST_CASE (movie_F_to_V_movie)
476 auto range = V_movie_range (movie_F("movie_F_to_V_movie"));
477 BOOST_CHECK_EQUAL (range.first, 4);
478 BOOST_CHECK_EQUAL (range.second, 1019);
482 BOOST_AUTO_TEST_CASE (movie_FoV_to_V_movie)
484 auto range = V_movie_range (movie_FoV("movie_FoV_to_V_movie"));
485 BOOST_CHECK_EQUAL (range.first, 4);
486 BOOST_CHECK_EQUAL (range.second, 1019);
490 BOOST_AUTO_TEST_CASE (image_F_to_V_movie)
492 auto range = V_movie_range (image_F("image_F_to_V_movie"));
493 BOOST_CHECK_EQUAL (range.first, 64);
494 BOOST_CHECK_EQUAL (range.second, 960);
498 BOOST_AUTO_TEST_CASE (image_FoV_to_V_movie)
500 auto range = V_movie_range (image_FoV("image_FoV_to_V_movie"));
501 BOOST_CHECK_EQUAL (range.first, 102);
502 BOOST_CHECK_EQUAL (range.second, 923);
506 BOOST_AUTO_TEST_CASE (dcp_F_to_V_movie)
508 auto range = V_movie_range (dcp_F("dcp_F_to_V_movie"));
509 BOOST_CHECK_EQUAL (range.first, 64);
510 BOOST_CHECK_EQUAL (range.second, 944);