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 "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 #include "dcp_content_type.h"
44 #include "trimmer.h"
45 #define BOOST_TEST_DYN_LINK
46 #define BOOST_TEST_MODULE dcpomatic_test
47 #include <boost/test/unit_test.hpp>
48
49 using std::string;
50 using std::list;
51 using std::stringstream;
52 using std::vector;
53 using boost::shared_ptr;
54 using boost::thread;
55 using boost::dynamic_pointer_cast;
56
57 void
58 setup_test_config ()
59 {
60         Config::instance()->set_num_local_encoding_threads (1);
61         Config::instance()->set_servers (vector<ServerDescription*> ());
62         Config::instance()->set_server_port (61920);
63         Config::instance()->set_default_dci_metadata (DCIMetadata ());
64 }
65
66 boost::filesystem::path
67 test_film_dir (string name)
68 {
69         boost::filesystem::path p;
70         p /= "build";
71         p /= "test";
72         p /= name;
73         return p;
74 }
75
76 shared_ptr<Film>
77 new_test_film (string name)
78 {
79         boost::filesystem::path p = test_film_dir (name);
80         if (boost::filesystem::exists (p)) {
81                 boost::filesystem::remove_all (p);
82         }
83         
84         return shared_ptr<Film> (new Film (p.string(), false));
85 }
86
87
88 /* Check that Image::make_black works, and doesn't use values which crash
89    sws_scale().
90 */
91 BOOST_AUTO_TEST_CASE (make_black_test)
92 {
93         /* This needs to happen in the first test */
94         dcpomatic_setup ();
95
96         libdcp::Size in_size (512, 512);
97         libdcp::Size out_size (1024, 1024);
98
99         list<AVPixelFormat> pix_fmts;
100         pix_fmts.push_back (AV_PIX_FMT_RGB24);
101         pix_fmts.push_back (AV_PIX_FMT_YUV420P);
102         pix_fmts.push_back (AV_PIX_FMT_YUV422P10LE);
103         pix_fmts.push_back (AV_PIX_FMT_YUV444P9LE);
104         pix_fmts.push_back (AV_PIX_FMT_YUV444P9BE);
105         pix_fmts.push_back (AV_PIX_FMT_YUV444P10LE);
106         pix_fmts.push_back (AV_PIX_FMT_YUV444P10BE);
107         pix_fmts.push_back (AV_PIX_FMT_UYVY422);
108
109         int N = 0;
110         for (list<AVPixelFormat>::const_iterator i = pix_fmts.begin(); i != pix_fmts.end(); ++i) {
111                 boost::shared_ptr<Image> foo (new SimpleImage (*i, in_size, true));
112                 foo->make_black ();
113                 boost::shared_ptr<Image> bar = foo->scale_and_convert_to_rgb (out_size, 0, Scaler::from_id ("bicubic"), true);
114                 
115                 uint8_t* p = bar->data()[0];
116                 for (int y = 0; y < bar->size().height; ++y) {
117                         uint8_t* q = p;
118                         for (int x = 0; x < bar->line_size()[0]; ++x) {
119                                 BOOST_CHECK_EQUAL (*q++, 0);
120                         }
121                         p += bar->stride()[0];
122                 }
123
124                 ++N;
125         }
126 }
127
128 shared_ptr<AudioBuffers> trimmer_test_last;
129
130 void
131 trimmer_test_helper (shared_ptr<AudioBuffers> audio)
132 {
133         trimmer_test_last = audio;
134 }
135
136 /** Test the audio handling of the Trimmer */
137 BOOST_AUTO_TEST_CASE (trimmer_test)
138 {
139         Trimmer trimmer (shared_ptr<Log> (), 25, 75, 200, 48000, 25, 25);
140
141         trimmer.Audio.connect (bind (&trimmer_test_helper, _1));
142
143         /* 21 video frames-worth of audio frames; should be completely stripped */
144         trimmer_test_last.reset ();
145         shared_ptr<AudioBuffers> audio (new AudioBuffers (6, 21 * 1920));
146         trimmer.process_audio (audio);
147         BOOST_CHECK (trimmer_test_last == 0);
148
149         /* 42 more video frames-worth, 4 should be stripped from the start */
150         audio.reset (new AudioBuffers (6, 42 * 1920));
151         trimmer.process_audio (audio);
152         BOOST_CHECK (trimmer_test_last);
153         BOOST_CHECK_EQUAL (trimmer_test_last->frames(), 38 * 1920);
154
155         /* 42 more video frames-worth, should be kept as-is */
156         trimmer_test_last.reset ();
157         audio.reset (new AudioBuffers (6, 42 * 1920));
158         trimmer.process_audio (audio);
159         BOOST_CHECK (trimmer_test_last);
160         BOOST_CHECK_EQUAL (trimmer_test_last->frames(), 42 * 1920);
161
162         /* 25 more video frames-worth, 5 should be trimmed from the end */
163         trimmer_test_last.reset ();
164         audio.reset (new AudioBuffers (6, 25 * 1920));
165         trimmer.process_audio (audio);
166         BOOST_CHECK (trimmer_test_last);
167         BOOST_CHECK_EQUAL (trimmer_test_last->frames(), 20 * 1920);
168
169         /* Now some more; all should be trimmed */
170         trimmer_test_last.reset ();
171         audio.reset (new AudioBuffers (6, 100 * 1920));
172         trimmer.process_audio (audio);
173         BOOST_CHECK (trimmer_test_last == 0);
174 }
175
176
177 BOOST_AUTO_TEST_CASE (film_metadata_test)
178 {
179         setup_test_config ();
180
181         string const test_film = "build/test/film_metadata_test";
182         
183         if (boost::filesystem::exists (test_film)) {
184                 boost::filesystem::remove_all (test_film);
185         }
186
187         BOOST_CHECK_THROW (new Film (test_film, true), OpenFileError);
188         
189         shared_ptr<Film> f (new Film (test_film, false));
190         f->_dci_date = boost::gregorian::from_undelimited_string ("20130211");
191         BOOST_CHECK (f->format() == 0);
192         BOOST_CHECK (f->dcp_content_type() == 0);
193         BOOST_CHECK (f->filters ().empty());
194
195         f->set_name ("fred");
196 //      BOOST_CHECK_THROW (f->set_content ("jim"), OpenFileError);
197         f->set_dcp_content_type (DCPContentType::from_pretty_name ("Short"));
198         f->set_format (Format::from_nickname ("Flat"));
199         f->set_left_crop (1);
200         f->set_right_crop (2);
201         f->set_top_crop (3);
202         f->set_bottom_crop (4);
203         vector<Filter const *> f_filters;
204         f_filters.push_back (Filter::from_id ("pphb"));
205         f_filters.push_back (Filter::from_id ("unsharp"));
206         f->set_filters (f_filters);
207         f->set_trim_start (42);
208         f->set_trim_end (99);
209         f->set_ab (true);
210         f->write_metadata ();
211
212         stringstream s;
213         s << "diff -u test/metadata.ref " << test_film << "/metadata";
214         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
215
216         shared_ptr<Film> g (new Film (test_film, true));
217
218         BOOST_CHECK_EQUAL (g->name(), "fred");
219         BOOST_CHECK_EQUAL (g->dcp_content_type(), DCPContentType::from_pretty_name ("Short"));
220         BOOST_CHECK_EQUAL (g->format(), Format::from_nickname ("Flat"));
221         BOOST_CHECK_EQUAL (g->crop().left, 1);
222         BOOST_CHECK_EQUAL (g->crop().right, 2);
223         BOOST_CHECK_EQUAL (g->crop().top, 3);
224         BOOST_CHECK_EQUAL (g->crop().bottom, 4);
225         vector<Filter const *> g_filters = g->filters ();
226         BOOST_CHECK_EQUAL (g_filters.size(), 2);
227         BOOST_CHECK_EQUAL (g_filters.front(), Filter::from_id ("pphb"));
228         BOOST_CHECK_EQUAL (g_filters.back(), Filter::from_id ("unsharp"));
229         BOOST_CHECK_EQUAL (g->trim_start(), 42);
230         BOOST_CHECK_EQUAL (g->trim_end(), 99);
231         BOOST_CHECK_EQUAL (g->ab(), true);
232         
233         g->write_metadata ();
234         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
235 }
236
237 BOOST_AUTO_TEST_CASE (format_test)
238 {
239         Format::setup_formats ();
240         
241         Format const * f = Format::from_nickname ("Flat");
242         BOOST_CHECK (f);
243 //      BOOST_CHECK_EQUAL (f->ratio_as_integer(shared_ptr<const Film> ()), 185);
244         
245         f = Format::from_nickname ("Scope");
246         BOOST_CHECK (f);
247 //      BOOST_CHECK_EQUAL (f->ratio_as_integer(shared_ptr<const Film> ()), 239);
248 }
249
250 BOOST_AUTO_TEST_CASE (util_test)
251 {
252         string t = "Hello this is a string \"with quotes\" and indeed without them";
253         vector<string> b = split_at_spaces_considering_quotes (t);
254         vector<string>::iterator i = b.begin ();
255         BOOST_CHECK_EQUAL (*i++, "Hello");
256         BOOST_CHECK_EQUAL (*i++, "this");
257         BOOST_CHECK_EQUAL (*i++, "is");
258         BOOST_CHECK_EQUAL (*i++, "a");
259         BOOST_CHECK_EQUAL (*i++, "string");
260         BOOST_CHECK_EQUAL (*i++, "with quotes");
261         BOOST_CHECK_EQUAL (*i++, "and");
262         BOOST_CHECK_EQUAL (*i++, "indeed");
263         BOOST_CHECK_EQUAL (*i++, "without");
264         BOOST_CHECK_EQUAL (*i++, "them");
265 }
266
267 class NullLog : public Log
268 {
269 public:
270         void do_log (string) {}
271 };
272
273 void
274 do_positive_delay_line_test (int delay_length, int data_length)
275 {
276         shared_ptr<NullLog> log (new NullLog);
277         
278         DelayLine d (log, 6, delay_length);
279         shared_ptr<AudioBuffers> data (new AudioBuffers (6, data_length));
280
281         int in = 0;
282         int out = 0;
283         int returned = 0;
284         int zeros = 0;
285         
286         for (int i = 0; i < 64; ++i) {
287                 for (int j = 0; j < data_length; ++j) {
288                         for (int c = 0; c < 6; ++c ) {
289                                 data->data(c)[j] = in;
290                                 ++in;
291                         }
292                 }
293
294                 /* This only works because the delay line modifies the parameter */
295                 d.process_audio (data);
296                 returned += data->frames ();
297
298                 for (int j = 0; j < data->frames(); ++j) {
299                         if (zeros < delay_length) {
300                                 for (int c = 0; c < 6; ++c) {
301                                         BOOST_CHECK_EQUAL (data->data(c)[j], 0);
302                                 }
303                                 ++zeros;
304                         } else {
305                                 for (int c = 0; c < 6; ++c) {
306                                         BOOST_CHECK_EQUAL (data->data(c)[j], out);
307                                         ++out;
308                                 }
309                         }
310                 }
311         }
312
313         BOOST_CHECK_EQUAL (returned, 64 * data_length);
314 }
315
316 void
317 do_negative_delay_line_test (int delay_length, int data_length)
318 {
319         shared_ptr<NullLog> log (new NullLog);
320
321         DelayLine d (log, 6, delay_length);
322         shared_ptr<AudioBuffers> data (new AudioBuffers (6, data_length));
323
324         int in = 0;
325         int out = -delay_length * 6;
326         int returned = 0;
327         
328         for (int i = 0; i < 256; ++i) {
329                 data->set_frames (data_length);
330                 for (int j = 0; j < data_length; ++j) {
331                         for (int c = 0; c < 6; ++c) {
332                                 data->data(c)[j] = in;
333                                 ++in;
334                         }
335                 }
336
337                 /* This only works because the delay line modifies the parameter */
338                 d.process_audio (data);
339                 returned += data->frames ();
340
341                 for (int j = 0; j < data->frames(); ++j) {
342                         for (int c = 0; c < 6; ++c) {
343                                 BOOST_CHECK_EQUAL (data->data(c)[j], out);
344                                 ++out;
345                         }
346                 }
347         }
348
349         returned += -delay_length;
350         BOOST_CHECK_EQUAL (returned, 256 * data_length);
351 }
352
353 BOOST_AUTO_TEST_CASE (delay_line_test)
354 {
355         do_positive_delay_line_test (64, 128);
356         do_positive_delay_line_test (128, 64);
357         do_positive_delay_line_test (3, 512);
358         do_positive_delay_line_test (512, 3);
359
360         do_positive_delay_line_test (0, 64);
361
362         do_negative_delay_line_test (-64, 128);
363         do_negative_delay_line_test (-128, 64);
364         do_negative_delay_line_test (-3, 512);
365         do_negative_delay_line_test (-512, 3);
366 }
367
368 BOOST_AUTO_TEST_CASE (md5_digest_test)
369 {
370         string const t = md5_digest ("test/md5.test");
371         BOOST_CHECK_EQUAL (t, "15058685ba99decdc4398c7634796eb0");
372
373         BOOST_CHECK_THROW (md5_digest ("foobar"), OpenFileError);
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         dcpomatic_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                 dcpomatic_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                 dcpomatic_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         dcpomatic_sleep (1);
743         BOOST_CHECK_EQUAL (a->running (), true);
744         a->set_finished_ok ();
745         dcpomatic_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