Some const correctness.
[dcpomatic.git] / test / test.cc
index 15c34ca78dc23206f0efaa9f83e512701db202de..496c915198818753036d2864656cd26aa75693e8 100644 (file)
@@ -28,7 +28,6 @@
 #include "job_manager.h"
 #include "util.h"
 #include "exceptions.h"
-#include "delay_line.h"
 #include "image.h"
 #include "log.h"
 #include "dcp_video_frame.h"
@@ -39,7 +38,8 @@
 #include "subtitle.h"
 #include "scaler.h"
 #include "ffmpeg_decoder.h"
-#include "external_audio_decoder.h"
+#include "sndfile_decoder.h"
+#include "trimmer.h"
 #define BOOST_TEST_DYN_LINK
 #define BOOST_TEST_MODULE dvdomatic_test
 #include <boost/test/unit_test.hpp>
@@ -102,6 +102,7 @@ BOOST_AUTO_TEST_CASE (make_black_test)
        pix_fmts.push_back (AV_PIX_FMT_YUV444P9BE);
        pix_fmts.push_back (AV_PIX_FMT_YUV444P10LE);
        pix_fmts.push_back (AV_PIX_FMT_YUV444P10BE);
+       pix_fmts.push_back (AV_PIX_FMT_UYVY422);
 
        int N = 0;
        for (list<AVPixelFormat>::const_iterator i = pix_fmts.begin(); i != pix_fmts.end(); ++i) {
@@ -122,6 +123,80 @@ BOOST_AUTO_TEST_CASE (make_black_test)
        }
 }
 
+shared_ptr<const Image> trimmer_test_last_video;
+shared_ptr<const AudioBuffers> trimmer_test_last_audio;
+
+void
+trimmer_test_video_helper (shared_ptr<const Image> image, bool, shared_ptr<Subtitle>)
+{
+       trimmer_test_last_video = image;
+}
+
+void
+trimmer_test_audio_helper (shared_ptr<const AudioBuffers> audio)
+{
+       trimmer_test_last_audio = audio;
+}
+
+BOOST_AUTO_TEST_CASE (trimmer_passthrough_test)
+{
+       Trimmer trimmer (shared_ptr<Log> (), 0, 0, 200, 48000, 25, 25);
+       trimmer.Video.connect (bind (&trimmer_test_video_helper, _1, _2, _3));
+       trimmer.Audio.connect (bind (&trimmer_test_audio_helper, _1));
+
+       shared_ptr<SimpleImage> video (new SimpleImage (PIX_FMT_RGB24, libdcp::Size (1998, 1080), true));
+       shared_ptr<AudioBuffers> audio (new AudioBuffers (6, 42 * 1920));
+
+       trimmer.process_video (video, false, shared_ptr<Subtitle> ());
+       trimmer.process_audio (audio);
+
+       BOOST_CHECK_EQUAL (video.get(), trimmer_test_last_video.get());
+       BOOST_CHECK_EQUAL (audio.get(), trimmer_test_last_audio.get());
+       BOOST_CHECK_EQUAL (audio->frames(), trimmer_test_last_audio->frames());
+}
+
+
+/** Test the audio handling of the Trimmer */
+BOOST_AUTO_TEST_CASE (trimmer_audio_test)
+{
+       Trimmer trimmer (shared_ptr<Log> (), 25, 75, 200, 48000, 25, 25);
+
+       trimmer.Audio.connect (bind (&trimmer_test_audio_helper, _1));
+
+       /* 21 video frames-worth of audio frames; should be completely stripped */
+       trimmer_test_last_audio.reset ();
+       shared_ptr<AudioBuffers> audio (new AudioBuffers (6, 21 * 1920));
+       trimmer.process_audio (audio);
+       BOOST_CHECK (trimmer_test_last_audio == 0);
+
+       /* 42 more video frames-worth, 4 should be stripped from the start */
+       audio.reset (new AudioBuffers (6, 42 * 1920));
+       trimmer.process_audio (audio);
+       BOOST_CHECK (trimmer_test_last_audio);
+       BOOST_CHECK_EQUAL (trimmer_test_last_audio->frames(), 38 * 1920);
+
+       /* 42 more video frames-worth, should be kept as-is */
+       trimmer_test_last_audio.reset ();
+       audio.reset (new AudioBuffers (6, 42 * 1920));
+       trimmer.process_audio (audio);
+       BOOST_CHECK (trimmer_test_last_audio);
+       BOOST_CHECK_EQUAL (trimmer_test_last_audio->frames(), 42 * 1920);
+
+       /* 25 more video frames-worth, 5 should be trimmed from the end */
+       trimmer_test_last_audio.reset ();
+       audio.reset (new AudioBuffers (6, 25 * 1920));
+       trimmer.process_audio (audio);
+       BOOST_CHECK (trimmer_test_last_audio);
+       BOOST_CHECK_EQUAL (trimmer_test_last_audio->frames(), 20 * 1920);
+
+       /* Now some more; all should be trimmed */
+       trimmer_test_last_audio.reset ();
+       audio.reset (new AudioBuffers (6, 100 * 1920));
+       trimmer.process_audio (audio);
+       BOOST_CHECK (trimmer_test_last_audio == 0);
+}
+
+
 BOOST_AUTO_TEST_CASE (film_metadata_test)
 {
        setup_test_config ();
@@ -191,7 +266,7 @@ BOOST_AUTO_TEST_CASE (stream_test)
        BOOST_CHECK_EQUAL (a.name(), "hello there world");
        BOOST_CHECK_EQUAL (a.to_string(), "ffmpeg 4 44100 1 hello there world");
 
-       ExternalAudioStream e ("external 44100 1", boost::optional<int> (1));
+       SndfileStream e ("external 44100 1", boost::optional<int> (1));
        BOOST_CHECK_EQUAL (e.sample_rate(), 44100);
        BOOST_CHECK_EQUAL (e.channel_layout(), 1);
        BOOST_CHECK_EQUAL (e.to_string(), "external 44100 1");
@@ -221,11 +296,39 @@ BOOST_AUTO_TEST_CASE (format_test)
        
        Format const * f = Format::from_nickname ("Flat");
        BOOST_CHECK (f);
-       BOOST_CHECK_EQUAL (f->ratio_as_integer(shared_ptr<const Film> ()), 185);
+       BOOST_CHECK_EQUAL (f->dcp_size().width, 1998);
+       BOOST_CHECK_EQUAL (f->dcp_size().height, 1080);
        
        f = Format::from_nickname ("Scope");
        BOOST_CHECK (f);
-       BOOST_CHECK_EQUAL (f->ratio_as_integer(shared_ptr<const Film> ()), 239);
+       BOOST_CHECK_EQUAL (f->dcp_size().width, 2048);
+       BOOST_CHECK_EQUAL (f->dcp_size().height, 858);
+}
+
+/* Test VariableFormat-based scaling of content */
+BOOST_AUTO_TEST_CASE (scaling_test)
+{
+       shared_ptr<Film> film (new Film (test_film_dir ("scaling_test").string(), false));
+
+       /* 4:3 ratio */
+       film->set_size (libdcp::Size (320, 240));
+
+       /* This format should preserve aspect ratio of the source */
+       Format const * format = Format::from_id ("var-185");
+
+       /* We should have enough padding that the result is 4:3,
+          which would be 1440 pixels.
+       */
+       BOOST_CHECK_EQUAL (format->dcp_padding (film), (1998 - 1440) / 2);
+       
+       /* This crops it to 1.291666667 */
+       film->set_left_crop (5);
+       film->set_right_crop (5);
+
+       /* We should now have enough padding that the result is 1.29166667,
+          which would be 1395 pixels.
+       */
+       BOOST_CHECK_EQUAL (format->dcp_padding (film), rint ((1998 - 1395) / 2.0));
 }
 
 BOOST_AUTO_TEST_CASE (util_test)
@@ -251,101 +354,6 @@ public:
        void do_log (string) {}
 };
 
-void
-do_positive_delay_line_test (int delay_length, int data_length)
-{
-       NullLog log;
-       
-       DelayLine d (&log, 6, delay_length);
-       shared_ptr<AudioBuffers> data (new AudioBuffers (6, data_length));
-
-       int in = 0;
-       int out = 0;
-       int returned = 0;
-       int zeros = 0;
-       
-       for (int i = 0; i < 64; ++i) {
-               for (int j = 0; j < data_length; ++j) {
-                       for (int c = 0; c < 6; ++c ) {
-                               data->data(c)[j] = in;
-                               ++in;
-                       }
-               }
-
-               /* This only works because the delay line modifies the parameter */
-               d.process_audio (data);
-               returned += data->frames ();
-
-               for (int j = 0; j < data->frames(); ++j) {
-                       if (zeros < delay_length) {
-                               for (int c = 0; c < 6; ++c) {
-                                       BOOST_CHECK_EQUAL (data->data(c)[j], 0);
-                               }
-                               ++zeros;
-                       } else {
-                               for (int c = 0; c < 6; ++c) {
-                                       BOOST_CHECK_EQUAL (data->data(c)[j], out);
-                                       ++out;
-                               }
-                       }
-               }
-       }
-
-       BOOST_CHECK_EQUAL (returned, 64 * data_length);
-}
-
-void
-do_negative_delay_line_test (int delay_length, int data_length)
-{
-       NullLog log;
-
-       DelayLine d (&log, 6, delay_length);
-       shared_ptr<AudioBuffers> data (new AudioBuffers (6, data_length));
-
-       int in = 0;
-       int out = -delay_length * 6;
-       int returned = 0;
-       
-       for (int i = 0; i < 256; ++i) {
-               data->set_frames (data_length);
-               for (int j = 0; j < data_length; ++j) {
-                       for (int c = 0; c < 6; ++c) {
-                               data->data(c)[j] = in;
-                               ++in;
-                       }
-               }
-
-               /* This only works because the delay line modifies the parameter */
-               d.process_audio (data);
-               returned += data->frames ();
-
-               for (int j = 0; j < data->frames(); ++j) {
-                       for (int c = 0; c < 6; ++c) {
-                               BOOST_CHECK_EQUAL (data->data(c)[j], out);
-                               ++out;
-                       }
-               }
-       }
-
-       returned += -delay_length;
-       BOOST_CHECK_EQUAL (returned, 256 * data_length);
-}
-
-BOOST_AUTO_TEST_CASE (delay_line_test)
-{
-       do_positive_delay_line_test (64, 128);
-       do_positive_delay_line_test (128, 64);
-       do_positive_delay_line_test (3, 512);
-       do_positive_delay_line_test (512, 3);
-
-       do_positive_delay_line_test (0, 64);
-
-       do_negative_delay_line_test (-64, 128);
-       do_negative_delay_line_test (-128, 64);
-       do_negative_delay_line_test (-3, 512);
-       do_negative_delay_line_test (-512, 3);
-}
-
 BOOST_AUTO_TEST_CASE (md5_digest_test)
 {
        string const t = md5_digest ("test/md5.test");
@@ -406,7 +414,7 @@ BOOST_AUTO_TEST_CASE (client_server_test)
 
        shared_ptr<Subtitle> subtitle (new Subtitle (Position (50, 60), sub_image));
 
-       FileLog log ("build/test/client_server_test.log");
+       shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test.log"));
 
        shared_ptr<DCPVideoFrame> frame (
                new DCPVideoFrame (
@@ -422,14 +430,14 @@ BOOST_AUTO_TEST_CASE (client_server_test)
                        "",
                        0,
                        200000000,
-                       &log
+                       log
                        )
                );
 
        shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
        BOOST_ASSERT (locally_encoded);
        
-       Server* server = new Server (&log);
+       Server* server = new Server (log);
 
        new thread (boost::bind (&Server::run, server, 2));
 
@@ -477,7 +485,7 @@ BOOST_AUTO_TEST_CASE (have_dcp_test)
        BOOST_CHECK (f.have_dcp());
 
        p /= f.dcp_name();
-       p /= "video.mxf";
+       p /= f.dcp_video_mxf_filename();
        boost::filesystem::remove (p);
        BOOST_CHECK (!f.have_dcp ());
 }
@@ -836,3 +844,4 @@ BOOST_AUTO_TEST_CASE (aligned_image_test)
        delete t;
        delete u;
 }
+