X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=test%2Ftest.cc;h=2dac199fe316f62aa9115e5cfb683e0a27eb5ee3;hb=2cb2078282c8b799d7a0d0016e1518f1a4b55af1;hp=382ffd77b0c990e339990c46cc4b6e29b5588b91;hpb=4b2b565a03f26c399960416efeed300dd80e401b;p=libdcp.git diff --git a/test/test.cc b/test/test.cc index 382ffd77..2dac199f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -36,39 +36,48 @@ #include "compose.hpp" #include "cpl.h" #include "dcp.h" -#include "file.h" #include "interop_subtitle_asset.h" +#include "file.h" +#include "j2k_transcode.h" +#include "mono_picture_asset.h" #include "mono_picture_asset.h" +#include "openjpeg_image.h" +#include "picture_asset_writer.h" #include "picture_asset_writer.h" #include "reel.h" +#include "reel_asset.h" +#include "reel_interop_closed_caption_asset.h" +#include "reel_interop_subtitle_asset.h" +#include "reel_markers_asset.h" +#include "reel_mono_picture_asset.h" #include "reel_mono_picture_asset.h" +#include "reel_smpte_closed_caption_asset.h" +#include "reel_smpte_subtitle_asset.h" #include "reel_sound_asset.h" -#include "reel_closed_caption_asset.h" -#include "reel_subtitle_asset.h" +#include "smpte_subtitle_asset.h" #include "sound_asset.h" #include "sound_asset_writer.h" -#include "smpte_subtitle_asset.h" -#include "mono_picture_asset.h" -#include "openjpeg_image.h" -#include "j2k.h" -#include "picture_asset_writer.h" -#include "reel_mono_picture_asset.h" -#include "reel_asset.h" #include "test.h" #include "util.h" +#include "warnings.h" +LIBDCP_DISABLE_WARNINGS #include #include +LIBDCP_ENABLE_WARNINGS #include +LIBDCP_DISABLE_WARNINGS #include +LIBDCP_ENABLE_WARNINGS #include #include #include + using std::string; using std::min; -using std::list; using std::vector; -using boost::shared_ptr; +using std::shared_ptr; +using std::make_shared; using boost::optional; @@ -95,8 +104,9 @@ struct TestConfig } }; + void -check_xml (xmlpp::Element* ref, xmlpp::Element* test, list ignore_tags, bool ignore_whitespace) +check_xml (xmlpp::Element* ref, xmlpp::Element* test, vector ignore_tags, bool ignore_whitespace) { BOOST_CHECK_EQUAL (ref->get_name (), test->get_name ()); BOOST_CHECK_EQUAL (ref->get_namespace_prefix (), test->get_namespace_prefix ()); @@ -105,49 +115,75 @@ check_xml (xmlpp::Element* ref, xmlpp::Element* test, list ignore_tags, return; } - xmlpp::Element::NodeList ref_children = ref->get_children (); - xmlpp::Element::NodeList test_children = test->get_children (); - BOOST_REQUIRE_MESSAGE ( - ref_children.size () == test_children.size (), - "child counts of " << ref->get_name() << " differ; ref has " << ref_children.size() << ", test has " << test_children.size() - ); + auto whitespace_content = [](xmlpp::Node* node) { + auto content = dynamic_cast(node); + return content && content->get_content().find_first_not_of(" \t\r\n") == string::npos; + }; - xmlpp::Element::NodeList::iterator k = ref_children.begin (); - xmlpp::Element::NodeList::iterator l = test_children.begin (); - while (k != ref_children.end ()) { + auto ref_children = ref->get_children (); + auto test_children = test->get_children (); + + auto k = ref_children.begin (); + auto l = test_children.begin (); + while (k != ref_children.end() && l != test_children.end()) { + + if (dynamic_cast(*k)) { + ++k; + continue; + } + + if (dynamic_cast(*l)) { + ++l; + continue; + } + + if (whitespace_content(*k) && ignore_whitespace) { + ++k; + continue; + } + + if (whitespace_content(*l) && ignore_whitespace) { + ++l; + continue; + } /* XXX: should be doing xmlpp::EntityReference, xmlpp::XIncludeEnd, xmlpp::XIncludeStart */ - xmlpp::Element* ref_el = dynamic_cast (*k); - xmlpp::Element* test_el = dynamic_cast (*l); + auto ref_el = dynamic_cast (*k); + auto test_el = dynamic_cast (*l); BOOST_CHECK ((ref_el && test_el) || (!ref_el && !test_el)); if (ref_el && test_el) { check_xml (ref_el, test_el, ignore_tags, ignore_whitespace); } - xmlpp::ContentNode* ref_cn = dynamic_cast (*k); - xmlpp::ContentNode* test_cn = dynamic_cast (*l); + auto ref_cn = dynamic_cast (*k); + auto test_cn = dynamic_cast (*l); BOOST_CHECK ((ref_cn && test_cn) || (!ref_cn && !test_cn)); if (ref_cn && test_cn) { - if ( - !ignore_whitespace || - ref_cn->get_content().find_first_not_of(" \t\r\n") != string::npos || - test_cn->get_content().find_first_not_of(" \t\r\n") != string::npos) { - - BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content ()); - } + BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content()); } ++k; ++l; } - xmlpp::Element::AttributeList ref_attributes = ref->get_attributes (); - xmlpp::Element::AttributeList test_attributes = test->get_attributes (); + while (k != ref_children.end() && ignore_whitespace && whitespace_content(*k)) { + ++k; + } + + while (l != test_children.end() && ignore_whitespace && whitespace_content(*l)) { + ++l; + } + + BOOST_REQUIRE (k == ref_children.end()); + BOOST_REQUIRE (l == test_children.end()); + + auto ref_attributes = ref->get_attributes (); + auto test_attributes = test->get_attributes (); BOOST_CHECK_EQUAL (ref_attributes.size(), test_attributes.size ()); - xmlpp::Element::AttributeList::const_iterator m = ref_attributes.begin(); - xmlpp::Element::AttributeList::const_iterator n = test_attributes.begin(); + auto m = ref_attributes.begin(); + auto n = test_attributes.begin(); while (m != ref_attributes.end ()) { BOOST_CHECK_EQUAL ((*m)->get_name(), (*n)->get_name()); BOOST_CHECK_EQUAL ((*m)->get_value(), (*n)->get_value()); @@ -158,55 +194,56 @@ check_xml (xmlpp::Element* ref, xmlpp::Element* test, list ignore_tags, } void -check_xml (string ref, string test, list ignore) +check_xml (string ref, string test, vector ignore, bool ignore_whitespace) { - xmlpp::DomParser* ref_parser = new xmlpp::DomParser (); + auto ref_parser = new xmlpp::DomParser (); ref_parser->parse_memory (ref); - xmlpp::Element* ref_root = ref_parser->get_document()->get_root_node (); - xmlpp::DomParser* test_parser = new xmlpp::DomParser (); + auto ref_root = ref_parser->get_document()->get_root_node (); + auto test_parser = new xmlpp::DomParser (); test_parser->parse_memory (test); - xmlpp::Element* test_root = test_parser->get_document()->get_root_node (); + auto test_root = test_parser->get_document()->get_root_node (); - check_xml (ref_root, test_root, ignore); + check_xml (ref_root, test_root, ignore, ignore_whitespace); } void check_file (boost::filesystem::path ref, boost::filesystem::path check) { - uintmax_t N = boost::filesystem::file_size (ref); - BOOST_CHECK_EQUAL (N, boost::filesystem::file_size (check)); - FILE* ref_file = dcp::fopen_boost (ref, "rb"); - BOOST_CHECK (ref_file); - FILE* check_file = dcp::fopen_boost (check, "rb"); - BOOST_CHECK (check_file); + uintmax_t size = boost::filesystem::file_size (ref); + BOOST_CHECK_EQUAL (size, boost::filesystem::file_size(check)); + dcp::File ref_file(ref, "rb"); + BOOST_REQUIRE (ref_file); + dcp::File check_file(check, "rb"); + BOOST_REQUIRE (check_file); int const buffer_size = 65536; - uint8_t* ref_buffer = new uint8_t[buffer_size]; - uint8_t* check_buffer = new uint8_t[buffer_size]; + std::vector ref_buffer(buffer_size); + std::vector check_buffer(buffer_size); - string error; - error = "File " + check.string() + " differs from reference " + ref.string(); + uintmax_t pos = 0; - while (N) { - uintmax_t this_time = min (uintmax_t (buffer_size), N); - size_t r = fread (ref_buffer, 1, this_time, ref_file); + while (pos < size) { + uintmax_t this_time = min (uintmax_t(buffer_size), size - pos); + size_t r = ref_file.read(ref_buffer.data(), 1, this_time); BOOST_CHECK_EQUAL (r, this_time); - r = fread (check_buffer, 1, this_time, check_file); + r = check_file.read(check_buffer.data(), 1, this_time); BOOST_CHECK_EQUAL (r, this_time); - BOOST_CHECK_MESSAGE (memcmp (ref_buffer, check_buffer, this_time) == 0, error); - if (memcmp (ref_buffer, check_buffer, this_time)) { + if (memcmp(ref_buffer.data(), check_buffer.data(), this_time) != 0) { + for (int i = 0; i < buffer_size; ++i) { + if (ref_buffer[i] != check_buffer[i]) { + BOOST_CHECK_MESSAGE ( + false, + dcp::String::compose("File %1 differs from reference %2 at offset %3", check, ref, pos + i) + ); + break; + } + } break; } - N -= this_time; + pos += this_time; } - - delete[] ref_buffer; - delete[] check_buffer; - - fclose (ref_file); - fclose (check_file); } @@ -224,19 +261,29 @@ RNGFixer::~RNGFixer () shared_ptr -simple_picture (boost::filesystem::path path, string suffix) +simple_picture (boost::filesystem::path path, string suffix, int frames, optional key) { dcp::MXFMetadata mxf_meta; mxf_meta.company_name = "OpenDCP"; mxf_meta.product_name = "OpenDCP"; mxf_meta.product_version = "0.0.25"; - shared_ptr mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE)); + auto mp = make_shared(dcp::Fraction (24, 1), dcp::Standard::SMPTE); mp->set_metadata (mxf_meta); - shared_ptr picture_writer = mp->start_write (path / dcp::String::compose("video%1.mxf", suffix), false); - dcp::File j2c ("test/data/32x32_red_square.j2c"); - for (int i = 0; i < 24; ++i) { - picture_writer->write (j2c.data (), j2c.size ()); + if (key) { + mp->set_key (*key); + } + auto picture_writer = mp->start_write(path / dcp::String::compose("video%1.mxf", suffix), dcp::PictureAsset::Behaviour::MAKE_NEW); + + dcp::Size const size (1998, 1080); + auto image = make_shared(size); + for (int i = 0; i < 3; ++i) { + memset (image->data(i), 0, 2 * size.width * size.height); + } + auto j2c = dcp::compress_j2k (image, 100000000, 24, false, false); + + for (int i = 0; i < frames; ++i) { + picture_writer->write (j2c.data(), j2c.size()); } picture_writer->finalize (); @@ -244,8 +291,42 @@ simple_picture (boost::filesystem::path path, string suffix) } +shared_ptr +simple_sound(boost::filesystem::path path, string suffix, dcp::MXFMetadata mxf_meta, string language, int frames, int sample_rate, optional key, int channels) +{ + /* Set a valid language, then overwrite it, so that the language parameter can be badly formed */ + auto ms = make_shared(dcp::Fraction(24, 1), sample_rate, channels, dcp::LanguageTag("en-US"), dcp::Standard::SMPTE); + if (key) { + ms->set_key (*key); + } + ms->_language = language; + ms->set_metadata (mxf_meta); + auto sound_writer = ms->start_write(path / dcp::String::compose("audio%1.mxf", suffix), {}, dcp::SoundAsset::AtmosSync::DISABLED, dcp::SoundAsset::MCASubDescriptors::ENABLED); + + int const samples_per_frame = sample_rate / 24; + + float* silence[channels]; + for (auto i = 0; i < channels; ++i) { + silence[i] = new float[samples_per_frame]; + memset (silence[i], 0, samples_per_frame * sizeof(float)); + } + + for (auto i = 0; i < frames; ++i) { + sound_writer->write(silence, channels, samples_per_frame); + } + + sound_writer->finalize (); + + for (auto i = 0; i < channels; ++i) { + delete[] silence[i]; + } + + return ms; +} + + shared_ptr -make_simple (boost::filesystem::path path, int reels) +make_simple (boost::filesystem::path path, int reels, int frames, dcp::Standard standard, optional key) { /* Some known metadata */ dcp::MXFMetadata mxf_meta; @@ -253,10 +334,12 @@ make_simple (boost::filesystem::path path, int reels) mxf_meta.product_name = "OpenDCP"; mxf_meta.product_version = "0.0.25"; + auto constexpr sample_rate = 48000; + boost::filesystem::remove_all (path); boost::filesystem::create_directories (path); - shared_ptr d (new dcp::DCP (path)); - shared_ptr cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE)); + auto d = make_shared(path); + auto cpl = make_shared("A Test DCP", dcp::ContentKind::TRAILER, standard); cpl->set_annotation_text ("A Test DCP"); cpl->set_issuer ("OpenDCP 0.0.25"); cpl->set_creator ("OpenDCP 0.0.25"); @@ -264,41 +347,33 @@ make_simple (boost::filesystem::path path, int reels) cpl->set_content_version ( dcp::ContentVersion("urn:uuid:75ac29aa-42ac-1234-ecae-49251abefd11", "content-version-label-text") ); + cpl->set_main_sound_configuration(dcp::MainSoundConfiguration("51/L,R,C,LFE,Ls,Rs")); + cpl->set_main_sound_sample_rate(sample_rate); + cpl->set_main_picture_stored_area(dcp::Size(1998, 1080)); + cpl->set_main_picture_active_area(dcp::Size(1998, 1080)); + cpl->set_version_number(1); for (int i = 0; i < reels; ++i) { string suffix = reels == 1 ? "" : dcp::String::compose("%1", i); - shared_ptr mp = simple_picture (path, suffix); - - shared_ptr ms (new dcp::SoundAsset(dcp::Fraction(24, 1), 48000, 1, dcp::LanguageTag("en-US"), dcp::SMPTE)); - ms->set_metadata (mxf_meta); - vector active_channels; - active_channels.push_back (dcp::LEFT); - shared_ptr sound_writer = ms->start_write (path / dcp::String::compose("audio%1.mxf", suffix), active_channels); - - SF_INFO info; - info.format = 0; - SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info); - BOOST_CHECK (sndfile); - float buffer[4096*6]; - float* channels[1]; - channels[0] = buffer; - while (true) { - sf_count_t N = sf_readf_float (sndfile, buffer, 4096); - sound_writer->write (channels, N); - if (N < 4096) { - break; - } - } + auto mp = simple_picture (path, suffix, frames, key); + auto ms = simple_sound (path, suffix, mxf_meta, "en-US", frames, sample_rate, key); - sound_writer->finalize (); + auto reel = make_shared( + shared_ptr(new dcp::ReelMonoPictureAsset(mp, 0)), + shared_ptr(new dcp::ReelSoundAsset(ms, 0)) + ); - cpl->add (shared_ptr ( - new dcp::Reel ( - shared_ptr(new dcp::ReelMonoPictureAsset(mp, 0)), - shared_ptr(new dcp::ReelSoundAsset(ms, 0)) - ) - )); + auto markers = make_shared(dcp::Fraction(24, 1), frames); + if (i == 0) { + markers->set (dcp::Marker::FFOC, dcp::Time(0, 0, 0, 1, 24)); + } + if (i == reels - 1) { + markers->set (dcp::Marker::LFOC, dcp::Time(0, 0, 0, frames - 1, 24)); + } + reel->add (markers); + + cpl->add (reel); } d->add (cpl); @@ -309,47 +384,57 @@ make_simple (boost::filesystem::path path, int reels) shared_ptr simple_subtitle () { - return shared_ptr( - new dcp::SubtitleString( - optional(), - false, - false, - false, - dcp::Colour(255, 255, 255), - 42, - 1, - dcp::Time(0, 0, 4, 0, 24), - dcp::Time(0, 0, 8, 0, 24), - 0.5, - dcp::HALIGN_CENTER, - 0.8, - dcp::VALIGN_TOP, - dcp::DIRECTION_LTR, - "Hello world", - dcp::NONE, - dcp::Colour(255, 255, 255), - dcp::Time(), - dcp::Time() - ) + return std::make_shared( + optional(), + false, + false, + false, + dcp::Colour(255, 255, 255), + 42, + 1, + dcp::Time(0, 0, 4, 0, 24), + dcp::Time(0, 0, 8, 0, 24), + 0.5, + dcp::HAlign::CENTER, + 0.8, + dcp::VAlign::TOP, + 0, + dcp::Direction::LTR, + "Hello world", + dcp::Effect::NONE, + dcp::Colour(255, 255, 255), + dcp::Time(), + dcp::Time(), + 0 ); } +shared_ptr +simple_markers (int frames) +{ + auto markers = make_shared(dcp::Fraction(24, 1), frames); + markers->set (dcp::Marker::FFOC, dcp::Time(1, 24, 24)); + markers->set (dcp::Marker::LFOC, dcp::Time(frames - 1, 24, 24)); + return markers; +} + + shared_ptr make_simple_with_interop_subs (boost::filesystem::path path) { - shared_ptr dcp = make_simple (path); + auto dcp = make_simple (path, 1, 24, dcp::Standard::INTEROP); - shared_ptr subs(new dcp::InteropSubtitleAsset()); + auto subs = make_shared(); subs->add (simple_subtitle()); boost::filesystem::create_directory (path / "subs"); - dcp::Data data(4096); - data.write (path / "subs" / "font.ttf"); - subs->add_font ("afont", path / "subs" / "font.ttf"); + dcp::ArrayData data(4096); + memset(data.data(), 0, data.size()); + subs->add_font ("afont", data); subs->write (path / "subs" / "subs.xml"); - shared_ptr reel_subs(new dcp::ReelSubtitleAsset(subs, dcp::Fraction(24, 1), 240, 0)); + auto reel_subs = make_shared(subs, dcp::Fraction(24, 1), 240, 0); dcp->cpls().front()->reels().front()->add (reel_subs); return dcp; @@ -359,15 +444,16 @@ make_simple_with_interop_subs (boost::filesystem::path path) shared_ptr make_simple_with_smpte_subs (boost::filesystem::path path) { - shared_ptr dcp = make_simple (path); + auto dcp = make_simple (path, 1, 192); - shared_ptr subs(new dcp::SMPTESubtitleAsset()); + auto subs = make_shared(); + subs->set_language (dcp::LanguageTag("de-DE")); + subs->set_start_time (dcp::Time()); subs->add (simple_subtitle()); - dcp::Data data(4096); subs->write (path / "subs.mxf"); - shared_ptr reel_subs(new dcp::ReelSubtitleAsset(subs, dcp::Fraction(24, 1), 240, 0)); + auto reel_subs = make_shared(subs, dcp::Fraction(24, 1), 192, 0); dcp->cpls().front()->reels().front()->add (reel_subs); return dcp; @@ -377,14 +463,14 @@ make_simple_with_smpte_subs (boost::filesystem::path path) shared_ptr make_simple_with_interop_ccaps (boost::filesystem::path path) { - shared_ptr dcp = make_simple (path); + auto dcp = make_simple (path, 1, 24, dcp::Standard::INTEROP); - shared_ptr subs(new dcp::InteropSubtitleAsset()); + auto subs = make_shared(); subs->add (simple_subtitle()); subs->write (path / "ccap.xml"); - shared_ptr reel_caps(new dcp::ReelClosedCaptionAsset(subs, dcp::Fraction(24, 1), 240, 0)); - dcp->cpls().front()->reels().front()->add (reel_caps); + auto reel_caps = make_shared(subs, dcp::Fraction(24, 1), 240, 0); + dcp->cpls()[0]->reels()[0]->add (reel_caps); return dcp; } @@ -393,24 +479,26 @@ make_simple_with_interop_ccaps (boost::filesystem::path path) shared_ptr make_simple_with_smpte_ccaps (boost::filesystem::path path) { - shared_ptr dcp = make_simple (path); + auto dcp = make_simple (path, 1, 192); - shared_ptr subs(new dcp::SMPTESubtitleAsset()); + auto subs = make_shared(); + subs->set_language (dcp::LanguageTag("de-DE")); + subs->set_start_time (dcp::Time()); subs->add (simple_subtitle()); subs->write (path / "ccap.mxf"); - shared_ptr reel_caps(new dcp::ReelClosedCaptionAsset(subs, dcp::Fraction(24, 1), 240, 0)); - dcp->cpls().front()->reels().front()->add (reel_caps); + auto reel_caps = make_shared(subs, dcp::Fraction(24, 1), 192, 0); + dcp->cpls()[0]->reels()[0]->add(reel_caps); return dcp; } shared_ptr -black_image () +black_image (dcp::Size size) { - shared_ptr image(new dcp::OpenJPEGImage(dcp::Size(1998, 1080))); - int const pixels = 1998 * 1080; + auto image = make_shared(size); + int const pixels = size.width * size.height; for (int i = 0; i < 3; ++i) { memset (image->data(i), 0, pixels * sizeof(int)); } @@ -421,19 +509,35 @@ black_image () shared_ptr black_picture_asset (boost::filesystem::path dir, int frames) { - shared_ptr image = black_image (); - dcp::Data frame = dcp::compress_j2k (image, 100000000, 24, false, false); + auto image = black_image (); + auto frame = dcp::compress_j2k (image, 100000000, 24, false, false); BOOST_REQUIRE (frame.size() < 230000000 / (24 * 8)); - shared_ptr asset(new dcp::MonoPictureAsset(dcp::Fraction(24, 1), dcp::SMPTE)); + auto asset = make_shared(dcp::Fraction(24, 1), dcp::Standard::SMPTE); + asset->set_metadata (dcp::MXFMetadata("libdcp", "libdcp", "1.6.4devel")); boost::filesystem::create_directories (dir); - shared_ptr writer = asset->start_write (dir / "pic.mxf", true); + auto writer = asset->start_write(dir / "pic.mxf", dcp::PictureAsset::Behaviour::MAKE_NEW); for (int i = 0; i < frames; ++i) { - writer->write (frame.data().get(), frame.size()); + writer->write (frame.data(), frame.size()); } writer->finalize (); - return shared_ptr(new dcp::ReelMonoPictureAsset(asset, 0)); + return make_shared(asset, 0); +} + + +boost::filesystem::path +find_file (boost::filesystem::path dir, string filename_part) +{ + boost::optional found; + for (auto i: boost::filesystem::directory_iterator(dir)) { + if (i.path().filename().string().find(filename_part) != string::npos) { + BOOST_REQUIRE (!found); + found = i; + } + } + BOOST_REQUIRE (found); + return *found; }