Use decoder_factory() and a lambda to clean things up a bit.
[dcpomatic.git] / test / video_level_test.cc
index 4ae722e7cd99f85d6148c6c87601d6eeb124d244..c7e255394ac90d5855e739e2e83c1a7e32c25da0 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -28,6 +28,7 @@
 #include "lib/content_factory.h"
 #include "lib/content_video.h"
 #include "lib/dcp_content.h"
+#include "lib/decoder_factory.h"
 #include "lib/film.h"
 #include "lib/ffmpeg_content.h"
 #include "lib/ffmpeg_decoder.h"
@@ -55,16 +56,20 @@ using std::make_pair;
 using std::max;
 using std::pair;
 using std::string;
-using boost::dynamic_pointer_cast;
+using std::dynamic_pointer_cast;
+using std::make_shared;
 using boost::optional;
-using boost::shared_ptr;
+#if BOOST_VERSION >= 106100
+using namespace boost::placeholders;
+#endif
+using std::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));
+       auto grey = make_shared<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) {
@@ -86,7 +91,7 @@ BOOST_AUTO_TEST_CASE (ffmpeg_image_full_range_not_changed)
 
        write_image (grey_image(size, grey_pixel), file);
 
-       FFmpegImageProxy proxy (file, VIDEO_RANGE_FULL);
+       FFmpegImageProxy proxy (file, VideoRange::FULL);
        ImageProxy::Result result = proxy.image ();
        BOOST_REQUIRE (!result.error);
 
@@ -108,7 +113,7 @@ BOOST_AUTO_TEST_CASE (ffmpeg_image_video_range_expanded)
 
        write_image (grey_image(size, grey_pixel), file);
 
-       FFmpegImageProxy proxy (file, VIDEO_RANGE_VIDEO);
+       FFmpegImageProxy proxy (file, VideoRange::VIDEO);
        ImageProxy::Result result = proxy.image ();
        BOOST_REQUIRE (!result.error);
 
@@ -121,17 +126,6 @@ BOOST_AUTO_TEST_CASE (ffmpeg_image_video_range_expanded)
 }
 
 
-static optional<ContentVideo> content_video;
-
-
-static
-void
-video_handler (ContentVideo cv)
-{
-       content_video = cv;
-}
-
-
 static
 pair<int, int>
 pixel_range (shared_ptr<const Image> image)
@@ -193,26 +187,13 @@ pixel_range (shared_ptr<const Image> image)
 
 static
 pair<int, int>
-pixel_range (shared_ptr<Film> film, shared_ptr<const FFmpegContent> content)
+pixel_range (shared_ptr<const Film> film, shared_ptr<const Content> content)
 {
-       shared_ptr<FFmpegDecoder> decoder(new FFmpegDecoder(film, content, false));
-       decoder->video->Data.connect (bind(&video_handler, _1));
-       content_video = boost::none;
-       while (!content_video) {
-               BOOST_REQUIRE (!decoder->pass());
-       }
-
-       return pixel_range (content_video->image->image().image);
-}
-
-
-static
-pair<int, int>
-pixel_range (shared_ptr<Film> film, shared_ptr<const ImageContent> content)
-{
-       shared_ptr<ImageDecoder> decoder(new ImageDecoder(film, content));
-       decoder->video->Data.connect (bind(&video_handler, _1));
-       content_video = boost::none;
+       auto decoder = decoder_factory(film, content, false, false, shared_ptr<Decoder>());
+       optional<ContentVideo> content_video;
+       decoder->video->Data.connect ([&content_video](ContentVideo cv) {
+               content_video = cv;
+       });
        while (!content_video) {
                BOOST_REQUIRE (!decoder->pass());
        }
@@ -228,9 +209,9 @@ pixel_range (boost::filesystem::path dcp_path)
        dcp::DCP dcp (dcp_path);
        dcp.read ();
 
-       shared_ptr<dcp::MonoPictureAsset> picture = dynamic_pointer_cast<dcp::MonoPictureAsset>(dcp.cpls().front()->reels().front()->main_picture()->asset());
+       auto picture = dynamic_pointer_cast<dcp::MonoPictureAsset>(dcp.cpls().front()->reels().front()->main_picture()->asset());
        BOOST_REQUIRE (picture);
-       shared_ptr<dcp::OpenJPEGImage> frame = picture->start_read()->get_frame(0)->xyz_image();
+       auto frame = picture->start_read()->get_frame(0)->xyz_image();
 
        int const width = frame->size().width;
        int const height = frame->size().height;
@@ -263,13 +244,13 @@ static
 shared_ptr<Film>
 movie_V (string name)
 {
-       shared_ptr<Film> film = new_test_film2 (name);
-       shared_ptr<FFmpegContent> content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mp4").front());
+       auto film = new_test_film2 (name);
+       auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mp4").front());
        BOOST_REQUIRE (content);
        film->examine_and_add_content (content);
        BOOST_REQUIRE (!wait_for_jobs());
 
-       pair<int, int> range = pixel_range (film, content);
+       auto range = pixel_range (film, content);
        BOOST_CHECK_EQUAL (range.first, 15);
        BOOST_CHECK_EQUAL (range.second, 243);
 
@@ -281,14 +262,14 @@ static
 shared_ptr<Film>
 movie_VoF (string name)
 {
-       shared_ptr<Film> film = new_test_film2 (name);
-       shared_ptr<FFmpegContent> content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mp4").front());
+       auto film = new_test_film2 (name);
+       auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mp4").front());
        BOOST_REQUIRE (content);
        film->examine_and_add_content (content);
        BOOST_REQUIRE (!wait_for_jobs());
-       content->video->set_range (VIDEO_RANGE_FULL);
+       content->video->set_range (VideoRange::FULL);
 
-       pair<int, int> range = pixel_range (film, content);
+       auto range = pixel_range (film, content);
        BOOST_CHECK_EQUAL (range.first, 15);
        BOOST_CHECK_EQUAL (range.second, 243);
 
@@ -300,13 +281,13 @@ static
 shared_ptr<Film>
 movie_F (string name)
 {
-       shared_ptr<Film> film = new_test_film2 (name);
-       shared_ptr<FFmpegContent> content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mov").front());
+       auto film = new_test_film2 (name);
+       auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mov").front());
        BOOST_REQUIRE (content);
        film->examine_and_add_content (content);
        BOOST_REQUIRE (!wait_for_jobs());
 
-       pair<int, int> range = pixel_range (film, content);
+       auto range = pixel_range (film, content);
        BOOST_CHECK_EQUAL (range.first, 0);
        BOOST_CHECK_EQUAL (range.second, 1023);
 
@@ -318,14 +299,14 @@ static
 shared_ptr<Film>
 movie_FoV (string name)
 {
-       shared_ptr<Film> film = new_test_film2 (name);
-       shared_ptr<FFmpegContent> content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mov").front());
+       auto film = new_test_film2 (name);
+       auto content = dynamic_pointer_cast<FFmpegContent>(content_factory("test/data/rgb_grey_testcard.mov").front());
        BOOST_REQUIRE (content);
        film->examine_and_add_content (content);
        BOOST_REQUIRE (!wait_for_jobs());
-       content->video->set_range (VIDEO_RANGE_VIDEO);
+       content->video->set_range (VideoRange::VIDEO);
 
-       pair<int, int> range = pixel_range (film, content);
+       auto range = pixel_range (film, content);
        BOOST_CHECK_EQUAL (range.first, 0);
        BOOST_CHECK_EQUAL (range.second, 1023);
 
@@ -337,13 +318,13 @@ static
 shared_ptr<Film>
 image_F (string name)
 {
-       shared_ptr<Film> film = new_test_film2 (name);
-       shared_ptr<ImageContent> content = dynamic_pointer_cast<ImageContent>(content_factory("test/data/rgb_grey_testcard.png").front());
+       auto film = new_test_film2 (name);
+       auto content = dynamic_pointer_cast<ImageContent>(content_factory("test/data/rgb_grey_testcard.png").front());
        BOOST_REQUIRE (content);
        film->examine_and_add_content (content);
        BOOST_REQUIRE (!wait_for_jobs());
 
-       pair<int, int> range = pixel_range (film, content);
+       auto range = pixel_range (film, content);
        BOOST_CHECK_EQUAL (range.first, 0);
        BOOST_CHECK_EQUAL (range.second, 255);
 
@@ -355,14 +336,14 @@ static
 shared_ptr<Film>
 image_FoV (string name)
 {
-       shared_ptr<Film> film = new_test_film2 (name);
-       shared_ptr<ImageContent> content = dynamic_pointer_cast<ImageContent>(content_factory("test/data/rgb_grey_testcard.png").front());
+       auto film = new_test_film2 (name);
+       auto content = dynamic_pointer_cast<ImageContent>(content_factory("test/data/rgb_grey_testcard.png").front());
        BOOST_REQUIRE (content);
        film->examine_and_add_content (content);
        BOOST_REQUIRE (!wait_for_jobs());
-       content->video->set_range (VIDEO_RANGE_VIDEO);
+       content->video->set_range (VideoRange::VIDEO);
 
-       pair<int, int> range = pixel_range (film, content);
+       auto range = pixel_range (film, content);
        BOOST_CHECK_EQUAL (range.first, 11);
        BOOST_CHECK_EQUAL (range.second, 250);
 
@@ -375,12 +356,12 @@ shared_ptr<Film>
 dcp_F (string name)
 {
        boost::filesystem::path const dcp = "test/data/RgbGreyTestcar_TST-1_F_MOS_2K_20201115_SMPTE_OV";
-       shared_ptr<Film> film = new_test_film2 (name);
-       shared_ptr<DCPContent> content(new DCPContent(dcp));
-       film->examine_and_add_content (shared_ptr<DCPContent>(new DCPContent(dcp)));
+       auto film = new_test_film2 (name);
+       auto content = make_shared<DCPContent>(dcp);
+       film->examine_and_add_content (content);
        BOOST_REQUIRE (!wait_for_jobs());
 
-       pair<int, int> range = pixel_range (dcp);
+       auto range = pixel_range (dcp);
        BOOST_CHECK_EQUAL (range.first, 0);
        BOOST_CHECK_EQUAL (range.second, 4081);
 
@@ -397,8 +378,7 @@ static
 pair<int, int>
 dcp_range (shared_ptr<Film> film)
 {
-       film->make_dcp ();
-       BOOST_REQUIRE (!wait_for_jobs());
+       make_and_verify_dcp (film);
        return pixel_range (film->dir(film->dcp_name()));
 }
 
@@ -408,17 +388,15 @@ static
 pair<int, int>
 V_movie_range (shared_ptr<Film> film)
 {
-       shared_ptr<TranscodeJob> job (new TranscodeJob(film));
+       auto job = make_shared<TranscodeJob>(film);
        job->set_encoder (
-               shared_ptr<FFmpegEncoder>(
-                       new FFmpegEncoder (film, job, film->file("export.mov"), EXPORT_FORMAT_PRORES, true, false, false, 23)
-                       )
+               make_shared<FFmpegEncoder>(film, job, film->file("export.mov"), ExportFormat::PRORES, true, false, false, 23)
                );
        JobManager::instance()->add (job);
        BOOST_REQUIRE (!wait_for_jobs());
 
        /* This is a bit of a hack; add the exported file into the project so we can decode it */
-       shared_ptr<FFmpegContent> content(new FFmpegContent(film->file("export.mov")));
+       auto content = make_shared<FFmpegContent>(film->file("export.mov"));
        film->examine_and_add_content (content);
        BOOST_REQUIRE (!wait_for_jobs());
 
@@ -431,61 +409,55 @@ V_movie_range (shared_ptr<Film> film)
 
 BOOST_AUTO_TEST_CASE (movie_V_to_dcp)
 {
-       pair<int, int> range = dcp_range (movie_V("movie_V_to_dcp"));
+       auto range = dcp_range (movie_V("movie_V_to_dcp"));
        /* Video range has been correctly expanded to full for the DCP */
-       BOOST_CHECK_EQUAL (range.first, 0);
-       BOOST_CHECK_EQUAL (range.second, 4082);
+       check_int_close (range, {0, 4083}, 2);
 }
 
 
 BOOST_AUTO_TEST_CASE (movie_VoF_to_dcp)
 {
-       pair<int, int> range = dcp_range (movie_VoF("movie_VoF_to_dcp"));
+       auto range = dcp_range (movie_VoF("movie_VoF_to_dcp"));
        /* We said that video range data was really full range, so here we are in the DCP
         * with video-range data.
         */
-       BOOST_CHECK_EQUAL (range.first, 350);
-       BOOST_CHECK_EQUAL (range.second, 3832);
+       check_int_close (range, {350, 3832}, 2);
 }
 
 
 BOOST_AUTO_TEST_CASE (movie_F_to_dcp)
 {
-       pair<int, int> range = dcp_range (movie_F("movie_F_to_dcp"));
+       auto range = dcp_range (movie_F("movie_F_to_dcp"));
        /* The nearly-full-range of the input has been preserved */
-       BOOST_CHECK_EQUAL (range.first, 0);
-       BOOST_CHECK_EQUAL (range.second, 4082);
+       check_int_close (range, {0, 4083}, 2);
 }
 
 
 BOOST_AUTO_TEST_CASE (video_FoV_to_dcp)
 {
-       pair<int, int> range = dcp_range (movie_FoV("video_FoV_to_dcp"));
+       auto range = dcp_range (movie_FoV("video_FoV_to_dcp"));
        /* The nearly-full-range of the input has become even more full, and clipped */
-       BOOST_CHECK_EQUAL (range.first, 0);
-       BOOST_CHECK_EQUAL (range.second, 4095);
+       check_int_close (range, {0, 4095}, 2);
 }
 
 
 BOOST_AUTO_TEST_CASE (image_F_to_dcp)
 {
-       pair<int, int> range = dcp_range (image_F("image_F_to_dcp"));
-       BOOST_CHECK_EQUAL (range.first, 0);
-       BOOST_CHECK_EQUAL (range.second, 4081);
+       auto range = dcp_range (image_F("image_F_to_dcp"));
+       check_int_close (range, {0, 4083}, 3);
 }
 
 
 BOOST_AUTO_TEST_CASE (image_FoV_to_dcp)
 {
-       pair<int, int> range = dcp_range (image_FoV("image_FoV_to_dcp"));
-       BOOST_CHECK_EQUAL (range.first, 431);
-       BOOST_CHECK_EQUAL (range.second, 4012);
+       auto range = dcp_range (image_FoV("image_FoV_to_dcp"));
+       check_int_close (range, {430, 4012}, 2);
 }
 
 
 BOOST_AUTO_TEST_CASE (movie_V_to_V_movie)
 {
-       pair<int, int> range = V_movie_range (movie_V("movie_V_to_V_movie"));
+       auto range = V_movie_range (movie_V("movie_V_to_V_movie"));
        BOOST_CHECK_EQUAL (range.first, 60);
        BOOST_CHECK_EQUAL (range.second, 998);
 }
@@ -493,7 +465,7 @@ BOOST_AUTO_TEST_CASE (movie_V_to_V_movie)
 
 BOOST_AUTO_TEST_CASE (movie_VoF_to_V_movie)
 {
-       pair<int, int> range = V_movie_range (movie_VoF("movie_VoF_to_V_movie"));
+       auto range = V_movie_range (movie_VoF("movie_VoF_to_V_movie"));
        BOOST_CHECK_EQUAL (range.first, 116);
        BOOST_CHECK_EQUAL (range.second, 939);
 }
@@ -501,7 +473,7 @@ BOOST_AUTO_TEST_CASE (movie_VoF_to_V_movie)
 
 BOOST_AUTO_TEST_CASE (movie_F_to_V_movie)
 {
-       pair<int, int> range = V_movie_range (movie_F("movie_F_to_V_movie"));
+       auto range = V_movie_range (movie_F("movie_F_to_V_movie"));
        BOOST_CHECK_EQUAL (range.first, 4);
        BOOST_CHECK_EQUAL (range.second, 1019);
 }
@@ -509,7 +481,7 @@ BOOST_AUTO_TEST_CASE (movie_F_to_V_movie)
 
 BOOST_AUTO_TEST_CASE (movie_FoV_to_V_movie)
 {
-       pair<int, int> range = V_movie_range (movie_FoV("movie_FoV_to_V_movie"));
+       auto range = V_movie_range (movie_FoV("movie_FoV_to_V_movie"));
        BOOST_CHECK_EQUAL (range.first, 4);
        BOOST_CHECK_EQUAL (range.second, 1019);
 }
@@ -517,7 +489,7 @@ BOOST_AUTO_TEST_CASE (movie_FoV_to_V_movie)
 
 BOOST_AUTO_TEST_CASE (image_F_to_V_movie)
 {
-       pair<int, int> range = V_movie_range (image_F("image_F_to_V_movie"));
+       auto range = V_movie_range (image_F("image_F_to_V_movie"));
        BOOST_CHECK_EQUAL (range.first, 64);
        BOOST_CHECK_EQUAL (range.second, 960);
 }
@@ -525,7 +497,7 @@ BOOST_AUTO_TEST_CASE (image_F_to_V_movie)
 
 BOOST_AUTO_TEST_CASE (image_FoV_to_V_movie)
 {
-       pair<int, int> range = V_movie_range (image_FoV("image_FoV_to_V_movie"));
+       auto range = V_movie_range (image_FoV("image_FoV_to_V_movie"));
        BOOST_CHECK_EQUAL (range.first, 102);
        BOOST_CHECK_EQUAL (range.second, 923);
 }
@@ -533,7 +505,7 @@ BOOST_AUTO_TEST_CASE (image_FoV_to_V_movie)
 
 BOOST_AUTO_TEST_CASE (dcp_F_to_V_movie)
 {
-       pair<int, int> range = V_movie_range (dcp_F("dcp_F_to_V_movie"));
+       auto range = V_movie_range (dcp_F("dcp_F_to_V_movie"));
        BOOST_CHECK_EQUAL (range.first, 64);
        BOOST_CHECK_EQUAL (range.second, 944);
 }