Add pixel formats tests.
[dcpomatic.git] / test / test.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <fstream>
21 #include <iostream>
22 #include <boost/filesystem.hpp>
23 #include <boost/algorithm/string/predicate.hpp>
24 #include <boost/date_time.hpp>
25 #include "format.h"
26 #include "film.h"
27 #include "filter.h"
28 #include "job_manager.h"
29 #include "util.h"
30 #include "exceptions.h"
31 #include "image.h"
32 #include "log.h"
33 #include "dcp_video_frame.h"
34 #include "config.h"
35 #include "server.h"
36 #include "cross.h"
37 #include "job.h"
38 #include "subtitle.h"
39 #include "scaler.h"
40 #include "ffmpeg_decoder.h"
41 #include "sndfile_decoder.h"
42 #include "trimmer.h"
43 #define BOOST_TEST_DYN_LINK
44 #define BOOST_TEST_MODULE dvdomatic_test
45 #include <boost/test/unit_test.hpp>
46
47 using std::string;
48 using std::list;
49 using std::stringstream;
50 using std::vector;
51 using boost::shared_ptr;
52 using boost::thread;
53 using boost::dynamic_pointer_cast;
54
55 void
56 setup_test_config ()
57 {
58         Config::instance()->set_num_local_encoding_threads (1);
59         Config::instance()->set_servers (vector<ServerDescription*> ());
60         Config::instance()->set_server_port (61920);
61         Config::instance()->set_default_dci_metadata (DCIMetadata ());
62 }
63
64 boost::filesystem::path
65 test_film_dir (string name)
66 {
67         boost::filesystem::path p;
68         p /= "build";
69         p /= "test";
70         p /= name;
71         return p;
72 }
73
74 shared_ptr<Film>
75 new_test_film (string name)
76 {
77         boost::filesystem::path p = test_film_dir (name);
78         if (boost::filesystem::exists (p)) {
79                 boost::filesystem::remove_all (p);
80         }
81         
82         return shared_ptr<Film> (new Film (p.string(), false));
83 }
84
85
86 /* Check that Image::make_black works, and doesn't use values which crash
87    sws_scale().
88 */
89 BOOST_AUTO_TEST_CASE (make_black_test)
90 {
91         /* This needs to happen in the first test */
92         dvdomatic_setup ();
93
94         libdcp::Size in_size (512, 512);
95         libdcp::Size out_size (1024, 1024);
96
97         list<AVPixelFormat> pix_fmts;
98         pix_fmts.push_back (AV_PIX_FMT_RGB24);
99         pix_fmts.push_back (AV_PIX_FMT_YUV420P);
100         pix_fmts.push_back (AV_PIX_FMT_YUV422P10LE);
101         pix_fmts.push_back (AV_PIX_FMT_YUV444P9LE);
102         pix_fmts.push_back (AV_PIX_FMT_YUV444P9BE);
103         pix_fmts.push_back (AV_PIX_FMT_YUV444P10LE);
104         pix_fmts.push_back (AV_PIX_FMT_YUV444P10BE);
105         pix_fmts.push_back (AV_PIX_FMT_UYVY422);
106
107         int N = 0;
108         for (list<AVPixelFormat>::const_iterator i = pix_fmts.begin(); i != pix_fmts.end(); ++i) {
109                 boost::shared_ptr<Image> foo (new SimpleImage (*i, in_size, true));
110                 foo->make_black ();
111                 boost::shared_ptr<Image> bar = foo->scale_and_convert_to_rgb (out_size, 0, Scaler::from_id ("bicubic"), true);
112                 
113                 uint8_t* p = bar->data()[0];
114                 for (int y = 0; y < bar->size().height; ++y) {
115                         uint8_t* q = p;
116                         for (int x = 0; x < bar->line_size()[0]; ++x) {
117                                 BOOST_CHECK_EQUAL (*q++, 0);
118                         }
119                         p += bar->stride()[0];
120                 }
121
122                 ++N;
123         }
124 }
125
126
127 struct Case
128 {
129         Case (AVPixelFormat f, int c, int l0, int l1, int l2, float b0, float b1, float b2)
130                 : format(f)
131                 , components(c)
132         {
133                 lines[0] = l0;
134                 lines[1] = l1;
135                 lines[2] = l2;
136                 bpp[0] = b0;
137                 bpp[1] = b1;
138                 bpp[2] = b2;
139         }
140         
141         AVPixelFormat format;
142         int components;
143         int lines[3];
144         float bpp[3];
145 };
146
147 BOOST_AUTO_TEST_CASE (pixel_formats_test)
148 {
149         list<Case> cases;
150         cases.push_back(Case(AV_PIX_FMT_RGB24,       1, 480, 480, 480, 3, 0,   0  ));
151         cases.push_back(Case(AV_PIX_FMT_RGBA,        1, 480, 480, 480, 4, 0,   0  ));
152         cases.push_back(Case(AV_PIX_FMT_YUV420P,     3, 480, 240, 240, 1, 0.5, 0.5));
153         cases.push_back(Case(AV_PIX_FMT_YUV422P,     3, 480, 480, 480, 1, 0.5, 0.5));
154         cases.push_back(Case(AV_PIX_FMT_YUV422P10LE, 3, 480, 480, 480, 2, 1,   1  ));
155         cases.push_back(Case(AV_PIX_FMT_YUV422P16LE, 3, 480, 480, 480, 2, 1,   1  ));
156         cases.push_back(Case(AV_PIX_FMT_UYVY422,     1, 480, 480, 480, 2, 2,   2  ));
157         cases.push_back(Case(AV_PIX_FMT_YUV444P,     3, 480, 480, 480, 3, 3,   3  ));
158         cases.push_back(Case(AV_PIX_FMT_YUV444P9BE,  3, 480, 480, 480, 6, 6,   6  ));
159         cases.push_back(Case(AV_PIX_FMT_YUV444P9LE,  3, 480, 480, 480, 6, 6,   6  ));
160         cases.push_back(Case(AV_PIX_FMT_YUV444P10BE, 3, 480, 480, 480, 6, 6,   6  ));
161         cases.push_back(Case(AV_PIX_FMT_YUV444P10LE, 3, 480, 480, 480, 6, 6,   6  ));
162
163         for (list<Case>::iterator i = cases.begin(); i != cases.end(); ++i) {
164                 AVFrame* f = av_frame_alloc ();
165                 f->width = 640;
166                 f->height = 480;
167                 f->format = static_cast<int> (i->format);
168                 FrameImage t (f);
169                 BOOST_CHECK_EQUAL(t.components(), i->components);
170                 BOOST_CHECK_EQUAL(t.lines(0), i->lines[0]);
171                 BOOST_CHECK_EQUAL(t.lines(1), i->lines[1]);
172                 BOOST_CHECK_EQUAL(t.lines(2), i->lines[2]);
173                 BOOST_CHECK_EQUAL(t.bytes_per_pixel(0), i->bpp[0]);
174                 BOOST_CHECK_EQUAL(t.bytes_per_pixel(1), i->bpp[1]);
175                 BOOST_CHECK_EQUAL(t.bytes_per_pixel(2), i->bpp[2]);
176         }
177 }
178
179 shared_ptr<const Image> trimmer_test_last_video;
180 shared_ptr<const AudioBuffers> trimmer_test_last_audio;
181
182 void
183 trimmer_test_video_helper (shared_ptr<const Image> image, bool, shared_ptr<Subtitle>)
184 {
185         trimmer_test_last_video = image;
186 }
187
188 void
189 trimmer_test_audio_helper (shared_ptr<const AudioBuffers> audio)
190 {
191         trimmer_test_last_audio = audio;
192 }
193
194 BOOST_AUTO_TEST_CASE (trimmer_passthrough_test)
195 {
196         Trimmer trimmer (shared_ptr<Log> (), 0, 0, 200, 48000, 25, 25);
197         trimmer.Video.connect (bind (&trimmer_test_video_helper, _1, _2, _3));
198         trimmer.Audio.connect (bind (&trimmer_test_audio_helper, _1));
199
200         shared_ptr<SimpleImage> video (new SimpleImage (PIX_FMT_RGB24, libdcp::Size (1998, 1080), true));
201         shared_ptr<AudioBuffers> audio (new AudioBuffers (6, 42 * 1920));
202
203         trimmer.process_video (video, false, shared_ptr<Subtitle> ());
204         trimmer.process_audio (audio);
205
206         BOOST_CHECK_EQUAL (video.get(), trimmer_test_last_video.get());
207         BOOST_CHECK_EQUAL (audio.get(), trimmer_test_last_audio.get());
208         BOOST_CHECK_EQUAL (audio->frames(), trimmer_test_last_audio->frames());
209 }
210
211
212 /** Test the audio handling of the Trimmer */
213 BOOST_AUTO_TEST_CASE (trimmer_audio_test)
214 {
215         Trimmer trimmer (shared_ptr<Log> (), 25, 75, 200, 48000, 25, 25);
216
217         trimmer.Audio.connect (bind (&trimmer_test_audio_helper, _1));
218
219         /* 21 video frames-worth of audio frames; should be completely stripped */
220         trimmer_test_last_audio.reset ();
221         shared_ptr<AudioBuffers> audio (new AudioBuffers (6, 21 * 1920));
222         trimmer.process_audio (audio);
223         BOOST_CHECK (trimmer_test_last_audio == 0);
224
225         /* 42 more video frames-worth, 4 should be stripped from the start */
226         audio.reset (new AudioBuffers (6, 42 * 1920));
227         trimmer.process_audio (audio);
228         BOOST_CHECK (trimmer_test_last_audio);
229         BOOST_CHECK_EQUAL (trimmer_test_last_audio->frames(), 38 * 1920);
230
231         /* 42 more video frames-worth, should be kept as-is */
232         trimmer_test_last_audio.reset ();
233         audio.reset (new AudioBuffers (6, 42 * 1920));
234         trimmer.process_audio (audio);
235         BOOST_CHECK (trimmer_test_last_audio);
236         BOOST_CHECK_EQUAL (trimmer_test_last_audio->frames(), 42 * 1920);
237
238         /* 25 more video frames-worth, 5 should be trimmed from the end */
239         trimmer_test_last_audio.reset ();
240         audio.reset (new AudioBuffers (6, 25 * 1920));
241         trimmer.process_audio (audio);
242         BOOST_CHECK (trimmer_test_last_audio);
243         BOOST_CHECK_EQUAL (trimmer_test_last_audio->frames(), 20 * 1920);
244
245         /* Now some more; all should be trimmed */
246         trimmer_test_last_audio.reset ();
247         audio.reset (new AudioBuffers (6, 100 * 1920));
248         trimmer.process_audio (audio);
249         BOOST_CHECK (trimmer_test_last_audio == 0);
250 }
251
252
253 BOOST_AUTO_TEST_CASE (film_metadata_test)
254 {
255         setup_test_config ();
256
257         string const test_film = "build/test/film_metadata_test";
258         
259         if (boost::filesystem::exists (test_film)) {
260                 boost::filesystem::remove_all (test_film);
261         }
262
263         BOOST_CHECK_THROW (new Film (test_film, true), OpenFileError);
264         
265         shared_ptr<Film> f (new Film (test_film, false));
266         f->_dci_date = boost::gregorian::from_undelimited_string ("20130211");
267         BOOST_CHECK (f->format() == 0);
268         BOOST_CHECK (f->dcp_content_type() == 0);
269         BOOST_CHECK (f->filters ().empty());
270
271         f->set_name ("fred");
272         BOOST_CHECK_THROW (f->set_content ("jim"), OpenFileError);
273         f->set_dcp_content_type (DCPContentType::from_pretty_name ("Short"));
274         f->set_format (Format::from_nickname ("Flat"));
275         f->set_left_crop (1);
276         f->set_right_crop (2);
277         f->set_top_crop (3);
278         f->set_bottom_crop (4);
279         vector<Filter const *> f_filters;
280         f_filters.push_back (Filter::from_id ("pphb"));
281         f_filters.push_back (Filter::from_id ("unsharp"));
282         f->set_filters (f_filters);
283         f->set_trim_start (42);
284         f->set_trim_end (99);
285         f->set_dcp_ab (true);
286         f->write_metadata ();
287
288         stringstream s;
289         s << "diff -u test/metadata.ref " << test_film << "/metadata";
290         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
291
292         shared_ptr<Film> g (new Film (test_film, true));
293
294         BOOST_CHECK_EQUAL (g->name(), "fred");
295         BOOST_CHECK_EQUAL (g->dcp_content_type(), DCPContentType::from_pretty_name ("Short"));
296         BOOST_CHECK_EQUAL (g->format(), Format::from_nickname ("Flat"));
297         BOOST_CHECK_EQUAL (g->crop().left, 1);
298         BOOST_CHECK_EQUAL (g->crop().right, 2);
299         BOOST_CHECK_EQUAL (g->crop().top, 3);
300         BOOST_CHECK_EQUAL (g->crop().bottom, 4);
301         vector<Filter const *> g_filters = g->filters ();
302         BOOST_CHECK_EQUAL (g_filters.size(), 2);
303         BOOST_CHECK_EQUAL (g_filters.front(), Filter::from_id ("pphb"));
304         BOOST_CHECK_EQUAL (g_filters.back(), Filter::from_id ("unsharp"));
305         BOOST_CHECK_EQUAL (g->trim_start(), 42);
306         BOOST_CHECK_EQUAL (g->trim_end(), 99);
307         BOOST_CHECK_EQUAL (g->dcp_ab(), true);
308         
309         g->write_metadata ();
310         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
311 }
312
313 BOOST_AUTO_TEST_CASE (stream_test)
314 {
315         FFmpegAudioStream a ("ffmpeg 4 44100 1 hello there world", boost::optional<int> (1));
316         BOOST_CHECK_EQUAL (a.id(), 4);
317         BOOST_CHECK_EQUAL (a.sample_rate(), 44100);
318         BOOST_CHECK_EQUAL (a.channel_layout(), 1);
319         BOOST_CHECK_EQUAL (a.name(), "hello there world");
320         BOOST_CHECK_EQUAL (a.to_string(), "ffmpeg 4 44100 1 hello there world");
321
322         SndfileStream e ("external 44100 1", boost::optional<int> (1));
323         BOOST_CHECK_EQUAL (e.sample_rate(), 44100);
324         BOOST_CHECK_EQUAL (e.channel_layout(), 1);
325         BOOST_CHECK_EQUAL (e.to_string(), "external 44100 1");
326
327         SubtitleStream s ("5 a b c", boost::optional<int> (1));
328         BOOST_CHECK_EQUAL (s.id(), 5);
329         BOOST_CHECK_EQUAL (s.name(), "a b c");
330
331         shared_ptr<AudioStream> ff = audio_stream_factory ("ffmpeg 4 44100 1 hello there world", boost::optional<int> (1));
332         shared_ptr<FFmpegAudioStream> cff = dynamic_pointer_cast<FFmpegAudioStream> (ff);
333         BOOST_CHECK (cff);
334         BOOST_CHECK_EQUAL (cff->id(), 4);
335         BOOST_CHECK_EQUAL (cff->sample_rate(), 44100);
336         BOOST_CHECK_EQUAL (cff->channel_layout(), 1);
337         BOOST_CHECK_EQUAL (cff->name(), "hello there world");
338         BOOST_CHECK_EQUAL (cff->to_string(), "ffmpeg 4 44100 1 hello there world");
339
340         shared_ptr<AudioStream> fe = audio_stream_factory ("external 44100 1", boost::optional<int> (1));
341         BOOST_CHECK_EQUAL (fe->sample_rate(), 44100);
342         BOOST_CHECK_EQUAL (fe->channel_layout(), 1);
343         BOOST_CHECK_EQUAL (fe->to_string(), "external 44100 1");
344 }
345
346 BOOST_AUTO_TEST_CASE (format_test)
347 {
348         Format::setup_formats ();
349         
350         Format const * f = Format::from_nickname ("Flat");
351         BOOST_CHECK (f);
352         BOOST_CHECK_EQUAL (f->dcp_size().width, 1998);
353         BOOST_CHECK_EQUAL (f->dcp_size().height, 1080);
354         
355         f = Format::from_nickname ("Scope");
356         BOOST_CHECK (f);
357         BOOST_CHECK_EQUAL (f->dcp_size().width, 2048);
358         BOOST_CHECK_EQUAL (f->dcp_size().height, 858);
359 }
360
361 /* Test VariableFormat-based scaling of content */
362 BOOST_AUTO_TEST_CASE (scaling_test)
363 {
364         shared_ptr<Film> film (new Film (test_film_dir ("scaling_test").string(), false));
365
366         /* 4:3 ratio */
367         film->set_size (libdcp::Size (320, 240));
368
369         /* This format should preserve aspect ratio of the source */
370         Format const * format = Format::from_id ("var-185");
371
372         /* We should have enough padding that the result is 4:3,
373            which would be 1440 pixels.
374         */
375         BOOST_CHECK_EQUAL (format->dcp_padding (film), (1998 - 1440) / 2);
376         
377         /* This crops it to 1.291666667 */
378         film->set_left_crop (5);
379         film->set_right_crop (5);
380
381         /* We should now have enough padding that the result is 1.29166667,
382            which would be 1395 pixels.
383         */
384         BOOST_CHECK_EQUAL (format->dcp_padding (film), rint ((1998 - 1395) / 2.0));
385 }
386
387 BOOST_AUTO_TEST_CASE (util_test)
388 {
389         string t = "Hello this is a string \"with quotes\" and indeed without them";
390         vector<string> b = split_at_spaces_considering_quotes (t);
391         vector<string>::iterator i = b.begin ();
392         BOOST_CHECK_EQUAL (*i++, "Hello");
393         BOOST_CHECK_EQUAL (*i++, "this");
394         BOOST_CHECK_EQUAL (*i++, "is");
395         BOOST_CHECK_EQUAL (*i++, "a");
396         BOOST_CHECK_EQUAL (*i++, "string");
397         BOOST_CHECK_EQUAL (*i++, "with quotes");
398         BOOST_CHECK_EQUAL (*i++, "and");
399         BOOST_CHECK_EQUAL (*i++, "indeed");
400         BOOST_CHECK_EQUAL (*i++, "without");
401         BOOST_CHECK_EQUAL (*i++, "them");
402 }
403
404 class NullLog : public Log
405 {
406 public:
407         void do_log (string) {}
408 };
409
410 BOOST_AUTO_TEST_CASE (md5_digest_test)
411 {
412         string const t = md5_digest ("test/md5.test");
413         BOOST_CHECK_EQUAL (t, "15058685ba99decdc4398c7634796eb0");
414
415         BOOST_CHECK_THROW (md5_digest ("foobar"), OpenFileError);
416 }
417
418 BOOST_AUTO_TEST_CASE (paths_test)
419 {
420         shared_ptr<Film> f = new_test_film ("paths_test");
421         f->set_directory ("build/test/a/b/c/d/e");
422
423         f->_content = "/foo/bar/baz";
424         BOOST_CHECK_EQUAL (f->content_path(), "/foo/bar/baz");
425         f->_content = "foo/bar/baz";
426         BOOST_CHECK_EQUAL (f->content_path(), "build/test/a/b/c/d/e/foo/bar/baz");
427 }
428
429 void
430 do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription* description, shared_ptr<EncodedData> locally_encoded)
431 {
432         shared_ptr<EncodedData> remotely_encoded;
433         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
434         BOOST_CHECK (remotely_encoded);
435         
436         BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size());
437         BOOST_CHECK (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()) == 0);
438 }
439
440 BOOST_AUTO_TEST_CASE (client_server_test)
441 {
442         shared_ptr<Image> image (new SimpleImage (PIX_FMT_RGB24, libdcp::Size (1998, 1080), true));
443         uint8_t* p = image->data()[0];
444         
445         for (int y = 0; y < 1080; ++y) {
446                 uint8_t* q = p;
447                 for (int x = 0; x < 1998; ++x) {
448                         *q++ = x % 256;
449                         *q++ = y % 256;
450                         *q++ = (x + y) % 256;
451                 }
452                 p += image->stride()[0];
453         }
454
455         shared_ptr<Image> sub_image (new SimpleImage (PIX_FMT_RGBA, libdcp::Size (100, 200), true));
456         p = sub_image->data()[0];
457         for (int y = 0; y < 200; ++y) {
458                 uint8_t* q = p;
459                 for (int x = 0; x < 100; ++x) {
460                         *q++ = y % 256;
461                         *q++ = x % 256;
462                         *q++ = (x + y) % 256;
463                         *q++ = 1;
464                 }
465                 p += sub_image->stride()[0];
466         }
467
468         shared_ptr<Subtitle> subtitle (new Subtitle (Position (50, 60), sub_image));
469
470         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test.log"));
471
472         shared_ptr<DCPVideoFrame> frame (
473                 new DCPVideoFrame (
474                         image,
475                         subtitle,
476                         libdcp::Size (1998, 1080),
477                         0,
478                         0,
479                         1,
480                         Scaler::from_id ("bicubic"),
481                         0,
482                         24,
483                         "",
484                         0,
485                         200000000,
486                         log
487                         )
488                 );
489
490         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
491         BOOST_ASSERT (locally_encoded);
492         
493         Server* server = new Server (log);
494
495         new thread (boost::bind (&Server::run, server, 2));
496
497         /* Let the server get itself ready */
498         dvdomatic_sleep (1);
499
500         ServerDescription description ("localhost", 2);
501
502         list<thread*> threads;
503         for (int i = 0; i < 8; ++i) {
504                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, &description, locally_encoded)));
505         }
506
507         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
508                 (*i)->join ();
509         }
510
511         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
512                 delete *i;
513         }
514 }
515
516 BOOST_AUTO_TEST_CASE (make_dcp_test)
517 {
518         shared_ptr<Film> film = new_test_film ("make_dcp_test");
519         film->set_name ("test_film2");
520         film->set_content ("../../../test/test.mp4");
521         film->set_format (Format::from_nickname ("Flat"));
522         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
523         film->make_dcp ();
524         film->write_metadata ();
525
526         while (JobManager::instance()->work_to_do ()) {
527                 dvdomatic_sleep (1);
528         }
529         
530         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
531 }
532
533 /** Test Film::have_dcp().  Requires the output from make_dcp_test above */
534 BOOST_AUTO_TEST_CASE (have_dcp_test)
535 {
536         boost::filesystem::path p = test_film_dir ("make_dcp_test");
537         Film f (p.string ());
538         BOOST_CHECK (f.have_dcp());
539
540         p /= f.dcp_name();
541         p /= f.dcp_video_mxf_filename();
542         boost::filesystem::remove (p);
543         BOOST_CHECK (!f.have_dcp ());
544 }
545
546 BOOST_AUTO_TEST_CASE (make_dcp_with_range_test)
547 {
548         shared_ptr<Film> film = new_test_film ("make_dcp_with_range_test");
549         film->set_name ("test_film3");
550         film->set_content ("../../../test/test.mp4");
551         film->examine_content ();
552         film->set_format (Format::from_nickname ("Flat"));
553         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
554         film->set_trim_end (42);
555         film->make_dcp ();
556
557         while (JobManager::instance()->work_to_do() && !JobManager::instance()->errors()) {
558                 dvdomatic_sleep (1);
559         }
560
561         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
562 }
563
564 /* Test best_dcp_frame_rate and FrameRateConversion */
565 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test)
566 {
567         /* Run some tests with a limited range of allowed rates */
568         
569         std::list<int> afr;
570         afr.push_back (24);
571         afr.push_back (25);
572         afr.push_back (30);
573         Config::instance()->set_allowed_dcp_frame_rates (afr);
574
575         int best = best_dcp_frame_rate (60);
576         FrameRateConversion frc = FrameRateConversion (60, best);
577         BOOST_CHECK_EQUAL (best, 30);
578         BOOST_CHECK_EQUAL (frc.skip, true);
579         BOOST_CHECK_EQUAL (frc.repeat, false);
580         BOOST_CHECK_EQUAL (frc.change_speed, false);
581         
582         best = best_dcp_frame_rate (50);
583         frc = FrameRateConversion (50, best);
584         BOOST_CHECK_EQUAL (best, 25);
585         BOOST_CHECK_EQUAL (frc.skip, true);
586         BOOST_CHECK_EQUAL (frc.repeat, false);
587         BOOST_CHECK_EQUAL (frc.change_speed, false);
588
589         best = best_dcp_frame_rate (48);
590         frc = FrameRateConversion (48, best);
591         BOOST_CHECK_EQUAL (best, 24);
592         BOOST_CHECK_EQUAL (frc.skip, true);
593         BOOST_CHECK_EQUAL (frc.repeat, false);
594         BOOST_CHECK_EQUAL (frc.change_speed, false);
595         
596         best = best_dcp_frame_rate (30);
597         frc = FrameRateConversion (30, best);
598         BOOST_CHECK_EQUAL (best, 30);
599         BOOST_CHECK_EQUAL (frc.skip, false);
600         BOOST_CHECK_EQUAL (frc.repeat, false);
601         BOOST_CHECK_EQUAL (frc.change_speed, false);
602
603         best = best_dcp_frame_rate (29.97);
604         frc = FrameRateConversion (29.97, best);
605         BOOST_CHECK_EQUAL (best, 30);
606         BOOST_CHECK_EQUAL (frc.skip, false);
607         BOOST_CHECK_EQUAL (frc.repeat, false);
608         BOOST_CHECK_EQUAL (frc.change_speed, true);
609         
610         best = best_dcp_frame_rate (25);
611         frc = FrameRateConversion (25, best);
612         BOOST_CHECK_EQUAL (best, 25);
613         BOOST_CHECK_EQUAL (frc.skip, false);
614         BOOST_CHECK_EQUAL (frc.repeat, false);
615         BOOST_CHECK_EQUAL (frc.change_speed, false);
616
617         best = best_dcp_frame_rate (24);
618         frc = FrameRateConversion (24, best);
619         BOOST_CHECK_EQUAL (best, 24);
620         BOOST_CHECK_EQUAL (frc.skip, false);
621         BOOST_CHECK_EQUAL (frc.repeat, false);
622         BOOST_CHECK_EQUAL (frc.change_speed, false);
623
624         best = best_dcp_frame_rate (14.5);
625         frc = FrameRateConversion (14.5, best);
626         BOOST_CHECK_EQUAL (best, 30);
627         BOOST_CHECK_EQUAL (frc.skip, false);
628         BOOST_CHECK_EQUAL (frc.repeat, true);
629         BOOST_CHECK_EQUAL (frc.change_speed, true);
630
631         best = best_dcp_frame_rate (12.6);
632         frc = FrameRateConversion (12.6, best);
633         BOOST_CHECK_EQUAL (best, 25);
634         BOOST_CHECK_EQUAL (frc.skip, false);
635         BOOST_CHECK_EQUAL (frc.repeat, true);
636         BOOST_CHECK_EQUAL (frc.change_speed, true);
637
638         best = best_dcp_frame_rate (12.4);
639         frc = FrameRateConversion (12.4, best);
640         BOOST_CHECK_EQUAL (best, 25);
641         BOOST_CHECK_EQUAL (frc.skip, false);
642         BOOST_CHECK_EQUAL (frc.repeat, true);
643         BOOST_CHECK_EQUAL (frc.change_speed, true);
644
645         best = best_dcp_frame_rate (12);
646         frc = FrameRateConversion (12, best);
647         BOOST_CHECK_EQUAL (best, 24);
648         BOOST_CHECK_EQUAL (frc.skip, false);
649         BOOST_CHECK_EQUAL (frc.repeat, true);
650         BOOST_CHECK_EQUAL (frc.change_speed, false);
651
652         /* Now add some more rates and see if it will use them
653            in preference to skip/repeat.
654         */
655
656         afr.push_back (48);
657         afr.push_back (50);
658         afr.push_back (60);
659         Config::instance()->set_allowed_dcp_frame_rates (afr);
660
661         best = best_dcp_frame_rate (60);
662         frc = FrameRateConversion (60, best);
663         BOOST_CHECK_EQUAL (best, 60);
664         BOOST_CHECK_EQUAL (frc.skip, false);
665         BOOST_CHECK_EQUAL (frc.repeat, false);
666         BOOST_CHECK_EQUAL (frc.change_speed, false);
667         
668         best = best_dcp_frame_rate (50);
669         frc = FrameRateConversion (50, best);
670         BOOST_CHECK_EQUAL (best, 50);
671         BOOST_CHECK_EQUAL (frc.skip, false);
672         BOOST_CHECK_EQUAL (frc.repeat, false);
673         BOOST_CHECK_EQUAL (frc.change_speed, false);
674
675         best = best_dcp_frame_rate (48);
676         frc = FrameRateConversion (48, best);
677         BOOST_CHECK_EQUAL (best, 48);
678         BOOST_CHECK_EQUAL (frc.skip, false);
679         BOOST_CHECK_EQUAL (frc.repeat, false);
680         BOOST_CHECK_EQUAL (frc.change_speed, false);
681
682         /* Check some out-there conversions (not the best) */
683         
684         frc = FrameRateConversion (14.99, 24);
685         BOOST_CHECK_EQUAL (frc.skip, false);
686         BOOST_CHECK_EQUAL (frc.repeat, true);
687         BOOST_CHECK_EQUAL (frc.change_speed, true);
688
689         /* Check some conversions with limited DCP targets */
690
691         afr.clear ();
692         afr.push_back (24);
693         Config::instance()->set_allowed_dcp_frame_rates (afr);
694
695         best = best_dcp_frame_rate (25);
696         frc = FrameRateConversion (25, best);
697         BOOST_CHECK_EQUAL (best, 24);
698         BOOST_CHECK_EQUAL (frc.skip, false);
699         BOOST_CHECK_EQUAL (frc.repeat, false);
700         BOOST_CHECK_EQUAL (frc.change_speed, true);
701 }
702
703 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
704 {
705         std::list<int> afr;
706         afr.push_back (24);
707         afr.push_back (25);
708         afr.push_back (30);
709         Config::instance()->set_allowed_dcp_frame_rates (afr);
710
711         shared_ptr<Film> f = new_test_film ("audio_sampling_rate_test");
712         f->set_source_frame_rate (24);
713         f->set_dcp_frame_rate (24);
714
715         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
716         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 48000);
717
718         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
719         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 48000);
720
721         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 80000, 0)));
722         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 96000);
723
724         f->set_source_frame_rate (23.976);
725         f->set_dcp_frame_rate (best_dcp_frame_rate (23.976));
726         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
727         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 47952);
728
729         f->set_source_frame_rate (29.97);
730         f->set_dcp_frame_rate (best_dcp_frame_rate (29.97));
731         BOOST_CHECK_EQUAL (f->dcp_frame_rate (), 30);
732         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
733         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 47952);
734
735         f->set_source_frame_rate (25);
736         f->set_dcp_frame_rate (24);
737         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
738         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 50000);
739
740         f->set_source_frame_rate (25);
741         f->set_dcp_frame_rate (24);
742         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
743         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 50000);
744
745         /* Check some out-there conversions (not the best) */
746         
747         f->set_source_frame_rate (14.99);
748         f->set_dcp_frame_rate (25);
749         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 16000, 0)));
750         /* The FrameRateConversion within target_audio_sample_rate should choose to double-up
751            the 14.99 fps video to 30 and then run it slow at 25.
752         */
753         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), rint (48000 * 2 * 14.99 / 25));
754 }
755
756 class TestJob : public Job
757 {
758 public:
759         TestJob (shared_ptr<Film> f)
760                 : Job (f)
761         {
762
763         }
764
765         void set_finished_ok () {
766                 set_state (FINISHED_OK);
767         }
768
769         void set_finished_error () {
770                 set_state (FINISHED_ERROR);
771         }
772
773         void run ()
774         {
775                 while (1) {
776                         if (finished ()) {
777                                 return;
778                         }
779                 }
780         }
781
782         string name () const {
783                 return "";
784         }
785 };
786
787 BOOST_AUTO_TEST_CASE (job_manager_test)
788 {
789         shared_ptr<Film> f;
790
791         /* Single job */
792         shared_ptr<TestJob> a (new TestJob (f));
793
794         JobManager::instance()->add (a);
795         dvdomatic_sleep (1);
796         BOOST_CHECK_EQUAL (a->running (), true);
797         a->set_finished_ok ();
798         dvdomatic_sleep (2);
799         BOOST_CHECK_EQUAL (a->finished_ok(), true);
800 }
801
802 BOOST_AUTO_TEST_CASE (compact_image_test)
803 {
804         SimpleImage* s = new SimpleImage (PIX_FMT_RGB24, libdcp::Size (50, 50), false);
805         BOOST_CHECK_EQUAL (s->components(), 1);
806         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
807         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
808         BOOST_CHECK (s->data()[0]);
809         BOOST_CHECK (!s->data()[1]);
810         BOOST_CHECK (!s->data()[2]);
811         BOOST_CHECK (!s->data()[3]);
812
813         /* copy constructor */
814         SimpleImage* t = new SimpleImage (*s);
815         BOOST_CHECK_EQUAL (t->components(), 1);
816         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
817         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
818         BOOST_CHECK (t->data()[0]);
819         BOOST_CHECK (!t->data()[1]);
820         BOOST_CHECK (!t->data()[2]);
821         BOOST_CHECK (!t->data()[3]);
822         BOOST_CHECK (t->data() != s->data());
823         BOOST_CHECK (t->data()[0] != s->data()[0]);
824         BOOST_CHECK (t->line_size() != s->line_size());
825         BOOST_CHECK (t->line_size()[0] == s->line_size()[0]);
826         BOOST_CHECK (t->stride() != s->stride());
827         BOOST_CHECK (t->stride()[0] == s->stride()[0]);
828
829         /* assignment operator */
830         SimpleImage* u = new SimpleImage (PIX_FMT_YUV422P, libdcp::Size (150, 150), true);
831         *u = *s;
832         BOOST_CHECK_EQUAL (u->components(), 1);
833         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
834         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
835         BOOST_CHECK (u->data()[0]);
836         BOOST_CHECK (!u->data()[1]);
837         BOOST_CHECK (!u->data()[2]);
838         BOOST_CHECK (!u->data()[3]);
839         BOOST_CHECK (u->data() != s->data());
840         BOOST_CHECK (u->data()[0] != s->data()[0]);
841         BOOST_CHECK (u->line_size() != s->line_size());
842         BOOST_CHECK (u->line_size()[0] == s->line_size()[0]);
843         BOOST_CHECK (u->stride() != s->stride());
844         BOOST_CHECK (u->stride()[0] == s->stride()[0]);
845
846         delete s;
847         delete t;
848         delete u;
849 }
850
851 BOOST_AUTO_TEST_CASE (aligned_image_test)
852 {
853         SimpleImage* s = new SimpleImage (PIX_FMT_RGB24, libdcp::Size (50, 50), true);
854         BOOST_CHECK_EQUAL (s->components(), 1);
855         /* 160 is 150 aligned to the nearest 32 bytes */
856         BOOST_CHECK_EQUAL (s->stride()[0], 160);
857         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
858         BOOST_CHECK (s->data()[0]);
859         BOOST_CHECK (!s->data()[1]);
860         BOOST_CHECK (!s->data()[2]);
861         BOOST_CHECK (!s->data()[3]);
862
863         /* copy constructor */
864         SimpleImage* t = new SimpleImage (*s);
865         BOOST_CHECK_EQUAL (t->components(), 1);
866         BOOST_CHECK_EQUAL (t->stride()[0], 160);
867         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
868         BOOST_CHECK (t->data()[0]);
869         BOOST_CHECK (!t->data()[1]);
870         BOOST_CHECK (!t->data()[2]);
871         BOOST_CHECK (!t->data()[3]);
872         BOOST_CHECK (t->data() != s->data());
873         BOOST_CHECK (t->data()[0] != s->data()[0]);
874         BOOST_CHECK (t->line_size() != s->line_size());
875         BOOST_CHECK (t->line_size()[0] == s->line_size()[0]);
876         BOOST_CHECK (t->stride() != s->stride());
877         BOOST_CHECK (t->stride()[0] == s->stride()[0]);
878
879         /* assignment operator */
880         SimpleImage* u = new SimpleImage (PIX_FMT_YUV422P, libdcp::Size (150, 150), false);
881         *u = *s;
882         BOOST_CHECK_EQUAL (u->components(), 1);
883         BOOST_CHECK_EQUAL (u->stride()[0], 160);
884         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
885         BOOST_CHECK (u->data()[0]);
886         BOOST_CHECK (!u->data()[1]);
887         BOOST_CHECK (!u->data()[2]);
888         BOOST_CHECK (!u->data()[3]);
889         BOOST_CHECK (u->data() != s->data());
890         BOOST_CHECK (u->data()[0] != s->data()[0]);
891         BOOST_CHECK (u->line_size() != s->line_size());
892         BOOST_CHECK (u->line_size()[0] == s->line_size()[0]);
893         BOOST_CHECK (u->stride() != s->stride());
894         BOOST_CHECK (u->stride()[0] == s->stride()[0]);
895
896         delete s;
897         delete t;
898         delete u;
899 }
900