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