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