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