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