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