Try to fix rounding problems with ratios due to old unused integer ops.
[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 "delay_line.h"
32 #include "image.h"
33 #include "log.h"
34 #include "dcp_video_frame.h"
35 #include "config.h"
36 #include "server.h"
37 #include "cross.h"
38 #include "job.h"
39 #include "subtitle.h"
40 #include "scaler.h"
41 #include "ffmpeg_decoder.h"
42 #include "sndfile_decoder.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 BOOST_AUTO_TEST_CASE (film_metadata_test)
127 {
128         setup_test_config ();
129
130         string const test_film = "build/test/film_metadata_test";
131         
132         if (boost::filesystem::exists (test_film)) {
133                 boost::filesystem::remove_all (test_film);
134         }
135
136         BOOST_CHECK_THROW (new Film (test_film, true), OpenFileError);
137         
138         shared_ptr<Film> f (new Film (test_film, false));
139         f->_dci_date = boost::gregorian::from_undelimited_string ("20130211");
140         BOOST_CHECK (f->format() == 0);
141         BOOST_CHECK (f->dcp_content_type() == 0);
142         BOOST_CHECK (f->filters ().empty());
143
144         f->set_name ("fred");
145         BOOST_CHECK_THROW (f->set_content ("jim"), OpenFileError);
146         f->set_dcp_content_type (DCPContentType::from_pretty_name ("Short"));
147         f->set_format (Format::from_nickname ("Flat"));
148         f->set_left_crop (1);
149         f->set_right_crop (2);
150         f->set_top_crop (3);
151         f->set_bottom_crop (4);
152         vector<Filter const *> f_filters;
153         f_filters.push_back (Filter::from_id ("pphb"));
154         f_filters.push_back (Filter::from_id ("unsharp"));
155         f->set_filters (f_filters);
156         f->set_trim_start (42);
157         f->set_trim_end (99);
158         f->set_dcp_ab (true);
159         f->write_metadata ();
160
161         stringstream s;
162         s << "diff -u test/metadata.ref " << test_film << "/metadata";
163         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
164
165         shared_ptr<Film> g (new Film (test_film, true));
166
167         BOOST_CHECK_EQUAL (g->name(), "fred");
168         BOOST_CHECK_EQUAL (g->dcp_content_type(), DCPContentType::from_pretty_name ("Short"));
169         BOOST_CHECK_EQUAL (g->format(), Format::from_nickname ("Flat"));
170         BOOST_CHECK_EQUAL (g->crop().left, 1);
171         BOOST_CHECK_EQUAL (g->crop().right, 2);
172         BOOST_CHECK_EQUAL (g->crop().top, 3);
173         BOOST_CHECK_EQUAL (g->crop().bottom, 4);
174         vector<Filter const *> g_filters = g->filters ();
175         BOOST_CHECK_EQUAL (g_filters.size(), 2);
176         BOOST_CHECK_EQUAL (g_filters.front(), Filter::from_id ("pphb"));
177         BOOST_CHECK_EQUAL (g_filters.back(), Filter::from_id ("unsharp"));
178         BOOST_CHECK_EQUAL (g->trim_start(), 42);
179         BOOST_CHECK_EQUAL (g->trim_end(), 99);
180         BOOST_CHECK_EQUAL (g->dcp_ab(), true);
181         
182         g->write_metadata ();
183         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
184 }
185
186 BOOST_AUTO_TEST_CASE (stream_test)
187 {
188         FFmpegAudioStream a ("ffmpeg 4 44100 1 hello there world", boost::optional<int> (1));
189         BOOST_CHECK_EQUAL (a.id(), 4);
190         BOOST_CHECK_EQUAL (a.sample_rate(), 44100);
191         BOOST_CHECK_EQUAL (a.channel_layout(), 1);
192         BOOST_CHECK_EQUAL (a.name(), "hello there world");
193         BOOST_CHECK_EQUAL (a.to_string(), "ffmpeg 4 44100 1 hello there world");
194
195         SndfileStream e ("external 44100 1", boost::optional<int> (1));
196         BOOST_CHECK_EQUAL (e.sample_rate(), 44100);
197         BOOST_CHECK_EQUAL (e.channel_layout(), 1);
198         BOOST_CHECK_EQUAL (e.to_string(), "external 44100 1");
199
200         SubtitleStream s ("5 a b c", boost::optional<int> (1));
201         BOOST_CHECK_EQUAL (s.id(), 5);
202         BOOST_CHECK_EQUAL (s.name(), "a b c");
203
204         shared_ptr<AudioStream> ff = audio_stream_factory ("ffmpeg 4 44100 1 hello there world", boost::optional<int> (1));
205         shared_ptr<FFmpegAudioStream> cff = dynamic_pointer_cast<FFmpegAudioStream> (ff);
206         BOOST_CHECK (cff);
207         BOOST_CHECK_EQUAL (cff->id(), 4);
208         BOOST_CHECK_EQUAL (cff->sample_rate(), 44100);
209         BOOST_CHECK_EQUAL (cff->channel_layout(), 1);
210         BOOST_CHECK_EQUAL (cff->name(), "hello there world");
211         BOOST_CHECK_EQUAL (cff->to_string(), "ffmpeg 4 44100 1 hello there world");
212
213         shared_ptr<AudioStream> fe = audio_stream_factory ("external 44100 1", boost::optional<int> (1));
214         BOOST_CHECK_EQUAL (fe->sample_rate(), 44100);
215         BOOST_CHECK_EQUAL (fe->channel_layout(), 1);
216         BOOST_CHECK_EQUAL (fe->to_string(), "external 44100 1");
217 }
218
219 BOOST_AUTO_TEST_CASE (format_test)
220 {
221         Format::setup_formats ();
222         
223         Format const * f = Format::from_nickname ("Flat");
224         BOOST_CHECK (f);
225         BOOST_CHECK_EQUAL (f->dcp_size().width, 1998);
226         BOOST_CHECK_EQUAL (f->dcp_size().height, 1080);
227         
228         f = Format::from_nickname ("Scope");
229         BOOST_CHECK (f);
230         BOOST_CHECK_EQUAL (f->dcp_size().width, 2048);
231         BOOST_CHECK_EQUAL (f->dcp_size().height, 858);
232 }
233
234 /* Test VariableFormat-based scaling of content */
235 BOOST_AUTO_TEST_CASE (scaling_test)
236 {
237         shared_ptr<Film> film (new Film (test_film_dir ("scaling_test").string(), false));
238
239         /* 4:3 ratio */
240         film->set_size (libdcp::Size (320, 240));
241
242         /* This format should preserve aspect ratio of the source */
243         Format const * format = Format::from_id ("var-185");
244
245         /* We should have enough padding that the result is 4:3,
246            which would be 1440 pixels.
247         */
248         BOOST_CHECK_EQUAL (format->dcp_padding (film), (1998 - 1440) / 2);
249         
250         /* This crops it to 1.291666667 */
251         film->set_left_crop (5);
252         film->set_right_crop (5);
253
254         /* We should now have enough padding that the result is 1.29166667,
255            which would be 1395 pixels.
256         */
257         BOOST_CHECK_EQUAL (format->dcp_padding (film), rint ((1998 - 1395) / 2.0));
258 }
259
260 BOOST_AUTO_TEST_CASE (util_test)
261 {
262         string t = "Hello this is a string \"with quotes\" and indeed without them";
263         vector<string> b = split_at_spaces_considering_quotes (t);
264         vector<string>::iterator i = b.begin ();
265         BOOST_CHECK_EQUAL (*i++, "Hello");
266         BOOST_CHECK_EQUAL (*i++, "this");
267         BOOST_CHECK_EQUAL (*i++, "is");
268         BOOST_CHECK_EQUAL (*i++, "a");
269         BOOST_CHECK_EQUAL (*i++, "string");
270         BOOST_CHECK_EQUAL (*i++, "with quotes");
271         BOOST_CHECK_EQUAL (*i++, "and");
272         BOOST_CHECK_EQUAL (*i++, "indeed");
273         BOOST_CHECK_EQUAL (*i++, "without");
274         BOOST_CHECK_EQUAL (*i++, "them");
275 }
276
277 class NullLog : public Log
278 {
279 public:
280         void do_log (string) {}
281 };
282
283 void
284 do_positive_delay_line_test (int delay_length, int data_length)
285 {
286         shared_ptr<NullLog> log (new NullLog);
287         
288         DelayLine d (log, 6, delay_length);
289         shared_ptr<AudioBuffers> data (new AudioBuffers (6, data_length));
290
291         int in = 0;
292         int out = 0;
293         int returned = 0;
294         int zeros = 0;
295         
296         for (int i = 0; i < 64; ++i) {
297                 for (int j = 0; j < data_length; ++j) {
298                         for (int c = 0; c < 6; ++c ) {
299                                 data->data(c)[j] = in;
300                                 ++in;
301                         }
302                 }
303
304                 /* This only works because the delay line modifies the parameter */
305                 d.process_audio (data);
306                 returned += data->frames ();
307
308                 for (int j = 0; j < data->frames(); ++j) {
309                         if (zeros < delay_length) {
310                                 for (int c = 0; c < 6; ++c) {
311                                         BOOST_CHECK_EQUAL (data->data(c)[j], 0);
312                                 }
313                                 ++zeros;
314                         } else {
315                                 for (int c = 0; c < 6; ++c) {
316                                         BOOST_CHECK_EQUAL (data->data(c)[j], out);
317                                         ++out;
318                                 }
319                         }
320                 }
321         }
322
323         BOOST_CHECK_EQUAL (returned, 64 * data_length);
324 }
325
326 void
327 do_negative_delay_line_test (int delay_length, int data_length)
328 {
329         shared_ptr<NullLog> log (new NullLog);
330
331         DelayLine d (log, 6, delay_length);
332         shared_ptr<AudioBuffers> data (new AudioBuffers (6, data_length));
333
334         int in = 0;
335         int out = -delay_length * 6;
336         int returned = 0;
337         
338         for (int i = 0; i < 256; ++i) {
339                 data->set_frames (data_length);
340                 for (int j = 0; j < data_length; ++j) {
341                         for (int c = 0; c < 6; ++c) {
342                                 data->data(c)[j] = in;
343                                 ++in;
344                         }
345                 }
346
347                 /* This only works because the delay line modifies the parameter */
348                 d.process_audio (data);
349                 returned += data->frames ();
350
351                 for (int j = 0; j < data->frames(); ++j) {
352                         for (int c = 0; c < 6; ++c) {
353                                 BOOST_CHECK_EQUAL (data->data(c)[j], out);
354                                 ++out;
355                         }
356                 }
357         }
358
359         returned += -delay_length;
360         BOOST_CHECK_EQUAL (returned, 256 * data_length);
361 }
362
363 BOOST_AUTO_TEST_CASE (delay_line_test)
364 {
365         do_positive_delay_line_test (64, 128);
366         do_positive_delay_line_test (128, 64);
367         do_positive_delay_line_test (3, 512);
368         do_positive_delay_line_test (512, 3);
369
370         do_positive_delay_line_test (0, 64);
371
372         do_negative_delay_line_test (-64, 128);
373         do_negative_delay_line_test (-128, 64);
374         do_negative_delay_line_test (-3, 512);
375         do_negative_delay_line_test (-512, 3);
376 }
377
378 BOOST_AUTO_TEST_CASE (md5_digest_test)
379 {
380         string const t = md5_digest ("test/md5.test");
381         BOOST_CHECK_EQUAL (t, "15058685ba99decdc4398c7634796eb0");
382
383         BOOST_CHECK_THROW (md5_digest ("foobar"), OpenFileError);
384 }
385
386 BOOST_AUTO_TEST_CASE (paths_test)
387 {
388         shared_ptr<Film> f = new_test_film ("paths_test");
389         f->set_directory ("build/test/a/b/c/d/e");
390
391         f->_content = "/foo/bar/baz";
392         BOOST_CHECK_EQUAL (f->content_path(), "/foo/bar/baz");
393         f->_content = "foo/bar/baz";
394         BOOST_CHECK_EQUAL (f->content_path(), "build/test/a/b/c/d/e/foo/bar/baz");
395 }
396
397 void
398 do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription* description, shared_ptr<EncodedData> locally_encoded)
399 {
400         shared_ptr<EncodedData> remotely_encoded;
401         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
402         BOOST_CHECK (remotely_encoded);
403         
404         BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size());
405         BOOST_CHECK (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()) == 0);
406 }
407
408 BOOST_AUTO_TEST_CASE (client_server_test)
409 {
410         shared_ptr<Image> image (new SimpleImage (PIX_FMT_RGB24, libdcp::Size (1998, 1080), true));
411         uint8_t* p = image->data()[0];
412         
413         for (int y = 0; y < 1080; ++y) {
414                 uint8_t* q = p;
415                 for (int x = 0; x < 1998; ++x) {
416                         *q++ = x % 256;
417                         *q++ = y % 256;
418                         *q++ = (x + y) % 256;
419                 }
420                 p += image->stride()[0];
421         }
422
423         shared_ptr<Image> sub_image (new SimpleImage (PIX_FMT_RGBA, libdcp::Size (100, 200), true));
424         p = sub_image->data()[0];
425         for (int y = 0; y < 200; ++y) {
426                 uint8_t* q = p;
427                 for (int x = 0; x < 100; ++x) {
428                         *q++ = y % 256;
429                         *q++ = x % 256;
430                         *q++ = (x + y) % 256;
431                         *q++ = 1;
432                 }
433                 p += sub_image->stride()[0];
434         }
435
436         shared_ptr<Subtitle> subtitle (new Subtitle (Position (50, 60), sub_image));
437
438         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test.log"));
439
440         shared_ptr<DCPVideoFrame> frame (
441                 new DCPVideoFrame (
442                         image,
443                         subtitle,
444                         libdcp::Size (1998, 1080),
445                         0,
446                         0,
447                         1,
448                         Scaler::from_id ("bicubic"),
449                         0,
450                         24,
451                         "",
452                         0,
453                         200000000,
454                         log
455                         )
456                 );
457
458         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
459         BOOST_ASSERT (locally_encoded);
460         
461         Server* server = new Server (log);
462
463         new thread (boost::bind (&Server::run, server, 2));
464
465         /* Let the server get itself ready */
466         dvdomatic_sleep (1);
467
468         ServerDescription description ("localhost", 2);
469
470         list<thread*> threads;
471         for (int i = 0; i < 8; ++i) {
472                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, &description, locally_encoded)));
473         }
474
475         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
476                 (*i)->join ();
477         }
478
479         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
480                 delete *i;
481         }
482 }
483
484 BOOST_AUTO_TEST_CASE (make_dcp_test)
485 {
486         shared_ptr<Film> film = new_test_film ("make_dcp_test");
487         film->set_name ("test_film2");
488         film->set_content ("../../../test/test.mp4");
489         film->set_format (Format::from_nickname ("Flat"));
490         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
491         film->make_dcp ();
492         film->write_metadata ();
493
494         while (JobManager::instance()->work_to_do ()) {
495                 dvdomatic_sleep (1);
496         }
497         
498         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
499 }
500
501 /** Test Film::have_dcp().  Requires the output from make_dcp_test above */
502 BOOST_AUTO_TEST_CASE (have_dcp_test)
503 {
504         boost::filesystem::path p = test_film_dir ("make_dcp_test");
505         Film f (p.string ());
506         BOOST_CHECK (f.have_dcp());
507
508         p /= f.dcp_name();
509         p /= "video.mxf";
510         boost::filesystem::remove (p);
511         BOOST_CHECK (!f.have_dcp ());
512 }
513
514 BOOST_AUTO_TEST_CASE (make_dcp_with_range_test)
515 {
516         shared_ptr<Film> film = new_test_film ("make_dcp_with_range_test");
517         film->set_name ("test_film3");
518         film->set_content ("../../../test/test.mp4");
519         film->examine_content ();
520         film->set_format (Format::from_nickname ("Flat"));
521         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
522         film->set_trim_end (42);
523         film->make_dcp ();
524
525         while (JobManager::instance()->work_to_do() && !JobManager::instance()->errors()) {
526                 dvdomatic_sleep (1);
527         }
528
529         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
530 }
531
532 /* Test best_dcp_frame_rate and FrameRateConversion */
533 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test)
534 {
535         /* Run some tests with a limited range of allowed rates */
536         
537         std::list<int> afr;
538         afr.push_back (24);
539         afr.push_back (25);
540         afr.push_back (30);
541         Config::instance()->set_allowed_dcp_frame_rates (afr);
542
543         int best = best_dcp_frame_rate (60);
544         FrameRateConversion frc = FrameRateConversion (60, best);
545         BOOST_CHECK_EQUAL (best, 30);
546         BOOST_CHECK_EQUAL (frc.skip, true);
547         BOOST_CHECK_EQUAL (frc.repeat, false);
548         BOOST_CHECK_EQUAL (frc.change_speed, false);
549         
550         best = best_dcp_frame_rate (50);
551         frc = FrameRateConversion (50, best);
552         BOOST_CHECK_EQUAL (best, 25);
553         BOOST_CHECK_EQUAL (frc.skip, true);
554         BOOST_CHECK_EQUAL (frc.repeat, false);
555         BOOST_CHECK_EQUAL (frc.change_speed, false);
556
557         best = best_dcp_frame_rate (48);
558         frc = FrameRateConversion (48, best);
559         BOOST_CHECK_EQUAL (best, 24);
560         BOOST_CHECK_EQUAL (frc.skip, true);
561         BOOST_CHECK_EQUAL (frc.repeat, false);
562         BOOST_CHECK_EQUAL (frc.change_speed, false);
563         
564         best = best_dcp_frame_rate (30);
565         frc = FrameRateConversion (30, best);
566         BOOST_CHECK_EQUAL (best, 30);
567         BOOST_CHECK_EQUAL (frc.skip, false);
568         BOOST_CHECK_EQUAL (frc.repeat, false);
569         BOOST_CHECK_EQUAL (frc.change_speed, false);
570
571         best = best_dcp_frame_rate (29.97);
572         frc = FrameRateConversion (29.97, best);
573         BOOST_CHECK_EQUAL (best, 30);
574         BOOST_CHECK_EQUAL (frc.skip, false);
575         BOOST_CHECK_EQUAL (frc.repeat, false);
576         BOOST_CHECK_EQUAL (frc.change_speed, true);
577         
578         best = best_dcp_frame_rate (25);
579         frc = FrameRateConversion (25, best);
580         BOOST_CHECK_EQUAL (best, 25);
581         BOOST_CHECK_EQUAL (frc.skip, false);
582         BOOST_CHECK_EQUAL (frc.repeat, false);
583         BOOST_CHECK_EQUAL (frc.change_speed, false);
584
585         best = best_dcp_frame_rate (24);
586         frc = FrameRateConversion (24, best);
587         BOOST_CHECK_EQUAL (best, 24);
588         BOOST_CHECK_EQUAL (frc.skip, false);
589         BOOST_CHECK_EQUAL (frc.repeat, false);
590         BOOST_CHECK_EQUAL (frc.change_speed, false);
591
592         best = best_dcp_frame_rate (14.5);
593         frc = FrameRateConversion (14.5, best);
594         BOOST_CHECK_EQUAL (best, 30);
595         BOOST_CHECK_EQUAL (frc.skip, false);
596         BOOST_CHECK_EQUAL (frc.repeat, true);
597         BOOST_CHECK_EQUAL (frc.change_speed, true);
598
599         best = best_dcp_frame_rate (12.6);
600         frc = FrameRateConversion (12.6, best);
601         BOOST_CHECK_EQUAL (best, 25);
602         BOOST_CHECK_EQUAL (frc.skip, false);
603         BOOST_CHECK_EQUAL (frc.repeat, true);
604         BOOST_CHECK_EQUAL (frc.change_speed, true);
605
606         best = best_dcp_frame_rate (12.4);
607         frc = FrameRateConversion (12.4, best);
608         BOOST_CHECK_EQUAL (best, 25);
609         BOOST_CHECK_EQUAL (frc.skip, false);
610         BOOST_CHECK_EQUAL (frc.repeat, true);
611         BOOST_CHECK_EQUAL (frc.change_speed, true);
612
613         best = best_dcp_frame_rate (12);
614         frc = FrameRateConversion (12, best);
615         BOOST_CHECK_EQUAL (best, 24);
616         BOOST_CHECK_EQUAL (frc.skip, false);
617         BOOST_CHECK_EQUAL (frc.repeat, true);
618         BOOST_CHECK_EQUAL (frc.change_speed, false);
619
620         /* Now add some more rates and see if it will use them
621            in preference to skip/repeat.
622         */
623
624         afr.push_back (48);
625         afr.push_back (50);
626         afr.push_back (60);
627         Config::instance()->set_allowed_dcp_frame_rates (afr);
628
629         best = best_dcp_frame_rate (60);
630         frc = FrameRateConversion (60, best);
631         BOOST_CHECK_EQUAL (best, 60);
632         BOOST_CHECK_EQUAL (frc.skip, false);
633         BOOST_CHECK_EQUAL (frc.repeat, false);
634         BOOST_CHECK_EQUAL (frc.change_speed, false);
635         
636         best = best_dcp_frame_rate (50);
637         frc = FrameRateConversion (50, best);
638         BOOST_CHECK_EQUAL (best, 50);
639         BOOST_CHECK_EQUAL (frc.skip, false);
640         BOOST_CHECK_EQUAL (frc.repeat, false);
641         BOOST_CHECK_EQUAL (frc.change_speed, false);
642
643         best = best_dcp_frame_rate (48);
644         frc = FrameRateConversion (48, best);
645         BOOST_CHECK_EQUAL (best, 48);
646         BOOST_CHECK_EQUAL (frc.skip, false);
647         BOOST_CHECK_EQUAL (frc.repeat, false);
648         BOOST_CHECK_EQUAL (frc.change_speed, false);
649
650         /* Check some out-there conversions (not the best) */
651         
652         frc = FrameRateConversion (14.99, 24);
653         BOOST_CHECK_EQUAL (frc.skip, false);
654         BOOST_CHECK_EQUAL (frc.repeat, true);
655         BOOST_CHECK_EQUAL (frc.change_speed, true);
656
657         /* Check some conversions with limited DCP targets */
658
659         afr.clear ();
660         afr.push_back (24);
661         Config::instance()->set_allowed_dcp_frame_rates (afr);
662
663         best = best_dcp_frame_rate (25);
664         frc = FrameRateConversion (25, best);
665         BOOST_CHECK_EQUAL (best, 24);
666         BOOST_CHECK_EQUAL (frc.skip, false);
667         BOOST_CHECK_EQUAL (frc.repeat, false);
668         BOOST_CHECK_EQUAL (frc.change_speed, true);
669 }
670
671 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
672 {
673         std::list<int> afr;
674         afr.push_back (24);
675         afr.push_back (25);
676         afr.push_back (30);
677         Config::instance()->set_allowed_dcp_frame_rates (afr);
678
679         shared_ptr<Film> f = new_test_film ("audio_sampling_rate_test");
680         f->set_source_frame_rate (24);
681         f->set_dcp_frame_rate (24);
682
683         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
684         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 48000);
685
686         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
687         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 48000);
688
689         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 80000, 0)));
690         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 96000);
691
692         f->set_source_frame_rate (23.976);
693         f->set_dcp_frame_rate (best_dcp_frame_rate (23.976));
694         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
695         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 47952);
696
697         f->set_source_frame_rate (29.97);
698         f->set_dcp_frame_rate (best_dcp_frame_rate (29.97));
699         BOOST_CHECK_EQUAL (f->dcp_frame_rate (), 30);
700         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
701         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 47952);
702
703         f->set_source_frame_rate (25);
704         f->set_dcp_frame_rate (24);
705         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
706         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 50000);
707
708         f->set_source_frame_rate (25);
709         f->set_dcp_frame_rate (24);
710         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
711         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 50000);
712
713         /* Check some out-there conversions (not the best) */
714         
715         f->set_source_frame_rate (14.99);
716         f->set_dcp_frame_rate (25);
717         f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 16000, 0)));
718         /* The FrameRateConversion within target_audio_sample_rate should choose to double-up
719            the 14.99 fps video to 30 and then run it slow at 25.
720         */
721         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), rint (48000 * 2 * 14.99 / 25));
722 }
723
724 class TestJob : public Job
725 {
726 public:
727         TestJob (shared_ptr<Film> f)
728                 : Job (f)
729         {
730
731         }
732
733         void set_finished_ok () {
734                 set_state (FINISHED_OK);
735         }
736
737         void set_finished_error () {
738                 set_state (FINISHED_ERROR);
739         }
740
741         void run ()
742         {
743                 while (1) {
744                         if (finished ()) {
745                                 return;
746                         }
747                 }
748         }
749
750         string name () const {
751                 return "";
752         }
753 };
754
755 BOOST_AUTO_TEST_CASE (job_manager_test)
756 {
757         shared_ptr<Film> f;
758
759         /* Single job */
760         shared_ptr<TestJob> a (new TestJob (f));
761
762         JobManager::instance()->add (a);
763         dvdomatic_sleep (1);
764         BOOST_CHECK_EQUAL (a->running (), true);
765         a->set_finished_ok ();
766         dvdomatic_sleep (2);
767         BOOST_CHECK_EQUAL (a->finished_ok(), true);
768 }
769
770 BOOST_AUTO_TEST_CASE (compact_image_test)
771 {
772         SimpleImage* s = new SimpleImage (PIX_FMT_RGB24, libdcp::Size (50, 50), false);
773         BOOST_CHECK_EQUAL (s->components(), 1);
774         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
775         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
776         BOOST_CHECK (s->data()[0]);
777         BOOST_CHECK (!s->data()[1]);
778         BOOST_CHECK (!s->data()[2]);
779         BOOST_CHECK (!s->data()[3]);
780
781         /* copy constructor */
782         SimpleImage* t = new SimpleImage (*s);
783         BOOST_CHECK_EQUAL (t->components(), 1);
784         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
785         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
786         BOOST_CHECK (t->data()[0]);
787         BOOST_CHECK (!t->data()[1]);
788         BOOST_CHECK (!t->data()[2]);
789         BOOST_CHECK (!t->data()[3]);
790         BOOST_CHECK (t->data() != s->data());
791         BOOST_CHECK (t->data()[0] != s->data()[0]);
792         BOOST_CHECK (t->line_size() != s->line_size());
793         BOOST_CHECK (t->line_size()[0] == s->line_size()[0]);
794         BOOST_CHECK (t->stride() != s->stride());
795         BOOST_CHECK (t->stride()[0] == s->stride()[0]);
796
797         /* assignment operator */
798         SimpleImage* u = new SimpleImage (PIX_FMT_YUV422P, libdcp::Size (150, 150), true);
799         *u = *s;
800         BOOST_CHECK_EQUAL (u->components(), 1);
801         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
802         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
803         BOOST_CHECK (u->data()[0]);
804         BOOST_CHECK (!u->data()[1]);
805         BOOST_CHECK (!u->data()[2]);
806         BOOST_CHECK (!u->data()[3]);
807         BOOST_CHECK (u->data() != s->data());
808         BOOST_CHECK (u->data()[0] != s->data()[0]);
809         BOOST_CHECK (u->line_size() != s->line_size());
810         BOOST_CHECK (u->line_size()[0] == s->line_size()[0]);
811         BOOST_CHECK (u->stride() != s->stride());
812         BOOST_CHECK (u->stride()[0] == s->stride()[0]);
813
814         delete s;
815         delete t;
816         delete u;
817 }
818
819 BOOST_AUTO_TEST_CASE (aligned_image_test)
820 {
821         SimpleImage* s = new SimpleImage (PIX_FMT_RGB24, libdcp::Size (50, 50), true);
822         BOOST_CHECK_EQUAL (s->components(), 1);
823         /* 160 is 150 aligned to the nearest 32 bytes */
824         BOOST_CHECK_EQUAL (s->stride()[0], 160);
825         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
826         BOOST_CHECK (s->data()[0]);
827         BOOST_CHECK (!s->data()[1]);
828         BOOST_CHECK (!s->data()[2]);
829         BOOST_CHECK (!s->data()[3]);
830
831         /* copy constructor */
832         SimpleImage* t = new SimpleImage (*s);
833         BOOST_CHECK_EQUAL (t->components(), 1);
834         BOOST_CHECK_EQUAL (t->stride()[0], 160);
835         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
836         BOOST_CHECK (t->data()[0]);
837         BOOST_CHECK (!t->data()[1]);
838         BOOST_CHECK (!t->data()[2]);
839         BOOST_CHECK (!t->data()[3]);
840         BOOST_CHECK (t->data() != s->data());
841         BOOST_CHECK (t->data()[0] != s->data()[0]);
842         BOOST_CHECK (t->line_size() != s->line_size());
843         BOOST_CHECK (t->line_size()[0] == s->line_size()[0]);
844         BOOST_CHECK (t->stride() != s->stride());
845         BOOST_CHECK (t->stride()[0] == s->stride()[0]);
846
847         /* assignment operator */
848         SimpleImage* u = new SimpleImage (PIX_FMT_YUV422P, libdcp::Size (150, 150), false);
849         *u = *s;
850         BOOST_CHECK_EQUAL (u->components(), 1);
851         BOOST_CHECK_EQUAL (u->stride()[0], 160);
852         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
853         BOOST_CHECK (u->data()[0]);
854         BOOST_CHECK (!u->data()[1]);
855         BOOST_CHECK (!u->data()[2]);
856         BOOST_CHECK (!u->data()[3]);
857         BOOST_CHECK (u->data() != s->data());
858         BOOST_CHECK (u->data()[0] != s->data()[0]);
859         BOOST_CHECK (u->line_size() != s->line_size());
860         BOOST_CHECK (u->line_size()[0] == s->line_size()[0]);
861         BOOST_CHECK (u->stride() != s->stride());
862         BOOST_CHECK (u->stride()[0] == s->stride()[0]);
863
864         delete s;
865         delete t;
866         delete u;
867 }