Various fix-ups.
[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 #define BOOST_TEST_DYN_LINK
45 #define BOOST_TEST_MODULE dvdomatic_test
46 #include <boost/test/unit_test.hpp>
47
48 using std::string;
49 using std::list;
50 using std::stringstream;
51 using std::vector;
52 using boost::shared_ptr;
53 using boost::thread;
54 using boost::dynamic_pointer_cast;
55
56 void
57 setup_test_config ()
58 {
59         Config::instance()->set_num_local_encoding_threads (1);
60         Config::instance()->set_servers (vector<ServerDescription*> ());
61         Config::instance()->set_server_port (61920);
62         Config::instance()->set_default_dci_metadata (DCIMetadata ());
63 }
64
65 boost::filesystem::path
66 test_film_dir (string name)
67 {
68         boost::filesystem::path p;
69         p /= "build";
70         p /= "test";
71         p /= name;
72         return p;
73 }
74
75 shared_ptr<Film>
76 new_test_film (string name)
77 {
78         boost::filesystem::path p = test_film_dir (name);
79         if (boost::filesystem::exists (p)) {
80                 boost::filesystem::remove_all (p);
81         }
82         
83         return shared_ptr<Film> (new Film (p.string(), false));
84 }
85
86
87 /* Check that Image::make_black works, and doesn't use values which crash
88    sws_scale().
89 */
90 BOOST_AUTO_TEST_CASE (make_black_test)
91 {
92         /* This needs to happen in the first test */
93         dvdomatic_setup ();
94
95         libdcp::Size in_size (512, 512);
96         libdcp::Size out_size (1024, 1024);
97
98         list<AVPixelFormat> pix_fmts;
99         pix_fmts.push_back (AV_PIX_FMT_RGB24);
100         pix_fmts.push_back (AV_PIX_FMT_YUV420P);
101         pix_fmts.push_back (AV_PIX_FMT_YUV422P10LE);
102         pix_fmts.push_back (AV_PIX_FMT_YUV444P9LE);
103         pix_fmts.push_back (AV_PIX_FMT_YUV444P9BE);
104         pix_fmts.push_back (AV_PIX_FMT_YUV444P10LE);
105         pix_fmts.push_back (AV_PIX_FMT_YUV444P10BE);
106         pix_fmts.push_back (AV_PIX_FMT_UYVY422);
107
108         int N = 0;
109         for (list<AVPixelFormat>::const_iterator i = pix_fmts.begin(); i != pix_fmts.end(); ++i) {
110                 boost::shared_ptr<Image> foo (new SimpleImage (*i, in_size, true));
111                 foo->make_black ();
112                 boost::shared_ptr<Image> bar = foo->scale_and_convert_to_rgb (out_size, 0, Scaler::from_id ("bicubic"), true);
113                 
114                 uint8_t* p = bar->data()[0];
115                 for (int y = 0; y < bar->size().height; ++y) {
116                         uint8_t* q = p;
117                         for (int x = 0; x < bar->line_size()[0]; ++x) {
118                                 BOOST_CHECK_EQUAL (*q++, 0);
119                         }
120                         p += bar->stride()[0];
121                 }
122
123                 ++N;
124         }
125 }
126
127 BOOST_AUTO_TEST_CASE (film_metadata_test)
128 {
129         setup_test_config ();
130
131         string const test_film = "build/test/film_metadata_test";
132         
133         if (boost::filesystem::exists (test_film)) {
134                 boost::filesystem::remove_all (test_film);
135         }
136
137         BOOST_CHECK_THROW (new Film (test_film, true), OpenFileError);
138         
139         shared_ptr<Film> f (new Film (test_film, false));
140         f->_dci_date = boost::gregorian::from_undelimited_string ("20130211");
141         BOOST_CHECK (f->format() == 0);
142         BOOST_CHECK (f->dcp_content_type() == 0);
143         BOOST_CHECK (f->filters ().empty());
144
145         f->set_name ("fred");
146 //      BOOST_CHECK_THROW (f->set_content ("jim"), OpenFileError);
147         f->set_dcp_content_type (DCPContentType::from_pretty_name ("Short"));
148         f->set_format (Format::from_nickname ("Flat"));
149         f->set_left_crop (1);
150         f->set_right_crop (2);
151         f->set_top_crop (3);
152         f->set_bottom_crop (4);
153         vector<Filter const *> f_filters;
154         f_filters.push_back (Filter::from_id ("pphb"));
155         f_filters.push_back (Filter::from_id ("unsharp"));
156         f->set_filters (f_filters);
157         f->set_trim_start (42);
158         f->set_trim_end (99);
159         f->set_ab (true);
160         f->write_metadata ();
161
162         stringstream s;
163         s << "diff -u test/metadata.ref " << test_film << "/metadata";
164         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
165
166         shared_ptr<Film> g (new Film (test_film, true));
167
168         BOOST_CHECK_EQUAL (g->name(), "fred");
169         BOOST_CHECK_EQUAL (g->dcp_content_type(), DCPContentType::from_pretty_name ("Short"));
170         BOOST_CHECK_EQUAL (g->format(), Format::from_nickname ("Flat"));
171         BOOST_CHECK_EQUAL (g->crop().left, 1);
172         BOOST_CHECK_EQUAL (g->crop().right, 2);
173         BOOST_CHECK_EQUAL (g->crop().top, 3);
174         BOOST_CHECK_EQUAL (g->crop().bottom, 4);
175         vector<Filter const *> g_filters = g->filters ();
176         BOOST_CHECK_EQUAL (g_filters.size(), 2);
177         BOOST_CHECK_EQUAL (g_filters.front(), Filter::from_id ("pphb"));
178         BOOST_CHECK_EQUAL (g_filters.back(), Filter::from_id ("unsharp"));
179         BOOST_CHECK_EQUAL (g->trim_start(), 42);
180         BOOST_CHECK_EQUAL (g->trim_end(), 99);
181         BOOST_CHECK_EQUAL (g->ab(), true);
182         
183         g->write_metadata ();
184         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
185 }
186
187 BOOST_AUTO_TEST_CASE (format_test)
188 {
189         Format::setup_formats ();
190         
191         Format const * f = Format::from_nickname ("Flat");
192         BOOST_CHECK (f);
193 //      BOOST_CHECK_EQUAL (f->ratio_as_integer(shared_ptr<const Film> ()), 185);
194         
195         f = Format::from_nickname ("Scope");
196         BOOST_CHECK (f);
197 //      BOOST_CHECK_EQUAL (f->ratio_as_integer(shared_ptr<const Film> ()), 239);
198 }
199
200 BOOST_AUTO_TEST_CASE (util_test)
201 {
202         string t = "Hello this is a string \"with quotes\" and indeed without them";
203         vector<string> b = split_at_spaces_considering_quotes (t);
204         vector<string>::iterator i = b.begin ();
205         BOOST_CHECK_EQUAL (*i++, "Hello");
206         BOOST_CHECK_EQUAL (*i++, "this");
207         BOOST_CHECK_EQUAL (*i++, "is");
208         BOOST_CHECK_EQUAL (*i++, "a");
209         BOOST_CHECK_EQUAL (*i++, "string");
210         BOOST_CHECK_EQUAL (*i++, "with quotes");
211         BOOST_CHECK_EQUAL (*i++, "and");
212         BOOST_CHECK_EQUAL (*i++, "indeed");
213         BOOST_CHECK_EQUAL (*i++, "without");
214         BOOST_CHECK_EQUAL (*i++, "them");
215 }
216
217 class NullLog : public Log
218 {
219 public:
220         void do_log (string) {}
221 };
222
223 void
224 do_positive_delay_line_test (int delay_length, int data_length)
225 {
226         shared_ptr<NullLog> log (new NullLog);
227         
228         DelayLine d (log, 6, delay_length);
229         shared_ptr<AudioBuffers> data (new AudioBuffers (6, data_length));
230
231         int in = 0;
232         int out = 0;
233         int returned = 0;
234         int zeros = 0;
235         
236         for (int i = 0; i < 64; ++i) {
237                 for (int j = 0; j < data_length; ++j) {
238                         for (int c = 0; c < 6; ++c ) {
239                                 data->data(c)[j] = in;
240                                 ++in;
241                         }
242                 }
243
244                 /* This only works because the delay line modifies the parameter */
245                 d.process_audio (data);
246                 returned += data->frames ();
247
248                 for (int j = 0; j < data->frames(); ++j) {
249                         if (zeros < delay_length) {
250                                 for (int c = 0; c < 6; ++c) {
251                                         BOOST_CHECK_EQUAL (data->data(c)[j], 0);
252                                 }
253                                 ++zeros;
254                         } else {
255                                 for (int c = 0; c < 6; ++c) {
256                                         BOOST_CHECK_EQUAL (data->data(c)[j], out);
257                                         ++out;
258                                 }
259                         }
260                 }
261         }
262
263         BOOST_CHECK_EQUAL (returned, 64 * data_length);
264 }
265
266 void
267 do_negative_delay_line_test (int delay_length, int data_length)
268 {
269         shared_ptr<NullLog> log (new NullLog);
270
271         DelayLine d (log, 6, delay_length);
272         shared_ptr<AudioBuffers> data (new AudioBuffers (6, data_length));
273
274         int in = 0;
275         int out = -delay_length * 6;
276         int returned = 0;
277         
278         for (int i = 0; i < 256; ++i) {
279                 data->set_frames (data_length);
280                 for (int j = 0; j < data_length; ++j) {
281                         for (int c = 0; c < 6; ++c) {
282                                 data->data(c)[j] = in;
283                                 ++in;
284                         }
285                 }
286
287                 /* This only works because the delay line modifies the parameter */
288                 d.process_audio (data);
289                 returned += data->frames ();
290
291                 for (int j = 0; j < data->frames(); ++j) {
292                         for (int c = 0; c < 6; ++c) {
293                                 BOOST_CHECK_EQUAL (data->data(c)[j], out);
294                                 ++out;
295                         }
296                 }
297         }
298
299         returned += -delay_length;
300         BOOST_CHECK_EQUAL (returned, 256 * data_length);
301 }
302
303 BOOST_AUTO_TEST_CASE (delay_line_test)
304 {
305         do_positive_delay_line_test (64, 128);
306         do_positive_delay_line_test (128, 64);
307         do_positive_delay_line_test (3, 512);
308         do_positive_delay_line_test (512, 3);
309
310         do_positive_delay_line_test (0, 64);
311
312         do_negative_delay_line_test (-64, 128);
313         do_negative_delay_line_test (-128, 64);
314         do_negative_delay_line_test (-3, 512);
315         do_negative_delay_line_test (-512, 3);
316 }
317
318 BOOST_AUTO_TEST_CASE (md5_digest_test)
319 {
320         string const t = md5_digest ("test/md5.test");
321         BOOST_CHECK_EQUAL (t, "15058685ba99decdc4398c7634796eb0");
322
323         BOOST_CHECK_THROW (md5_digest ("foobar"), OpenFileError);
324 }
325
326 void
327 do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription* description, shared_ptr<EncodedData> locally_encoded)
328 {
329         shared_ptr<EncodedData> remotely_encoded;
330         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
331         BOOST_CHECK (remotely_encoded);
332         
333         BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size());
334         BOOST_CHECK (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()) == 0);
335 }
336
337 BOOST_AUTO_TEST_CASE (client_server_test)
338 {
339         shared_ptr<Image> image (new SimpleImage (PIX_FMT_RGB24, libdcp::Size (1998, 1080), true));
340         uint8_t* p = image->data()[0];
341         
342         for (int y = 0; y < 1080; ++y) {
343                 uint8_t* q = p;
344                 for (int x = 0; x < 1998; ++x) {
345                         *q++ = x % 256;
346                         *q++ = y % 256;
347                         *q++ = (x + y) % 256;
348                 }
349                 p += image->stride()[0];
350         }
351
352         shared_ptr<Image> sub_image (new SimpleImage (PIX_FMT_RGBA, libdcp::Size (100, 200), true));
353         p = sub_image->data()[0];
354         for (int y = 0; y < 200; ++y) {
355                 uint8_t* q = p;
356                 for (int x = 0; x < 100; ++x) {
357                         *q++ = y % 256;
358                         *q++ = x % 256;
359                         *q++ = (x + y) % 256;
360                         *q++ = 1;
361                 }
362                 p += sub_image->stride()[0];
363         }
364
365         shared_ptr<Subtitle> subtitle (new Subtitle (Position (50, 60), sub_image));
366
367         shared_ptr<FileLog> log (new FileLog ("build/test/client_server_test.log"));
368
369         shared_ptr<DCPVideoFrame> frame (
370                 new DCPVideoFrame (
371                         image,
372                         subtitle,
373                         libdcp::Size (1998, 1080),
374                         0,
375                         0,
376                         1,
377                         Scaler::from_id ("bicubic"),
378                         0,
379                         24,
380                         "",
381                         0,
382                         200000000,
383                         log
384                         )
385                 );
386
387         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
388         BOOST_ASSERT (locally_encoded);
389         
390         Server* server = new Server (log);
391
392         new thread (boost::bind (&Server::run, server, 2));
393
394         /* Let the server get itself ready */
395         dvdomatic_sleep (1);
396
397         ServerDescription description ("localhost", 2);
398
399         list<thread*> threads;
400         for (int i = 0; i < 8; ++i) {
401                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, &description, locally_encoded)));
402         }
403
404         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
405                 (*i)->join ();
406         }
407
408         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
409                 delete *i;
410         }
411 }
412
413 BOOST_AUTO_TEST_CASE (make_dcp_test)
414 {
415         shared_ptr<Film> film = new_test_film ("make_dcp_test");
416         film->set_name ("test_film2");
417 //      film->set_content ("../../../test/test.mp4");
418         film->set_format (Format::from_nickname ("Flat"));
419         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
420         film->make_dcp ();
421         film->write_metadata ();
422
423         while (JobManager::instance()->work_to_do ()) {
424                 dvdomatic_sleep (1);
425         }
426         
427         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
428 }
429
430 /** Test Film::have_dcp().  Requires the output from make_dcp_test above */
431 BOOST_AUTO_TEST_CASE (have_dcp_test)
432 {
433         boost::filesystem::path p = test_film_dir ("make_dcp_test");
434         Film f (p.string ());
435         BOOST_CHECK (f.have_dcp());
436
437         p /= f.dcp_name();
438         p /= "video.mxf";
439         boost::filesystem::remove (p);
440         BOOST_CHECK (!f.have_dcp ());
441 }
442
443 BOOST_AUTO_TEST_CASE (make_dcp_with_range_test)
444 {
445         shared_ptr<Film> film = new_test_film ("make_dcp_with_range_test");
446         film->set_name ("test_film3");
447 //      film->set_content ("../../../test/test.mp4");
448 //      film->examine_content ();
449         film->set_format (Format::from_nickname ("Flat"));
450         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
451         film->set_trim_end (42);
452         film->make_dcp ();
453
454         while (JobManager::instance()->work_to_do() && !JobManager::instance()->errors()) {
455                 dvdomatic_sleep (1);
456         }
457
458         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
459 }
460
461 /* Test best_dcp_frame_rate and FrameRateConversion */
462 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test)
463 {
464         /* Run some tests with a limited range of allowed rates */
465         
466         std::list<int> afr;
467         afr.push_back (24);
468         afr.push_back (25);
469         afr.push_back (30);
470         Config::instance()->set_allowed_dcp_frame_rates (afr);
471
472         int best = best_dcp_frame_rate (60);
473         FrameRateConversion frc = FrameRateConversion (60, best);
474         BOOST_CHECK_EQUAL (best, 30);
475         BOOST_CHECK_EQUAL (frc.skip, true);
476         BOOST_CHECK_EQUAL (frc.repeat, false);
477         BOOST_CHECK_EQUAL (frc.change_speed, false);
478         
479         best = best_dcp_frame_rate (50);
480         frc = FrameRateConversion (50, best);
481         BOOST_CHECK_EQUAL (best, 25);
482         BOOST_CHECK_EQUAL (frc.skip, true);
483         BOOST_CHECK_EQUAL (frc.repeat, false);
484         BOOST_CHECK_EQUAL (frc.change_speed, false);
485
486         best = best_dcp_frame_rate (48);
487         frc = FrameRateConversion (48, best);
488         BOOST_CHECK_EQUAL (best, 24);
489         BOOST_CHECK_EQUAL (frc.skip, true);
490         BOOST_CHECK_EQUAL (frc.repeat, false);
491         BOOST_CHECK_EQUAL (frc.change_speed, false);
492         
493         best = best_dcp_frame_rate (30);
494         frc = FrameRateConversion (30, best);
495         BOOST_CHECK_EQUAL (best, 30);
496         BOOST_CHECK_EQUAL (frc.skip, false);
497         BOOST_CHECK_EQUAL (frc.repeat, false);
498         BOOST_CHECK_EQUAL (frc.change_speed, false);
499
500         best = best_dcp_frame_rate (29.97);
501         frc = FrameRateConversion (29.97, best);
502         BOOST_CHECK_EQUAL (best, 30);
503         BOOST_CHECK_EQUAL (frc.skip, false);
504         BOOST_CHECK_EQUAL (frc.repeat, false);
505         BOOST_CHECK_EQUAL (frc.change_speed, true);
506         
507         best = best_dcp_frame_rate (25);
508         frc = FrameRateConversion (25, best);
509         BOOST_CHECK_EQUAL (best, 25);
510         BOOST_CHECK_EQUAL (frc.skip, false);
511         BOOST_CHECK_EQUAL (frc.repeat, false);
512         BOOST_CHECK_EQUAL (frc.change_speed, false);
513
514         best = best_dcp_frame_rate (24);
515         frc = FrameRateConversion (24, best);
516         BOOST_CHECK_EQUAL (best, 24);
517         BOOST_CHECK_EQUAL (frc.skip, false);
518         BOOST_CHECK_EQUAL (frc.repeat, false);
519         BOOST_CHECK_EQUAL (frc.change_speed, false);
520
521         best = best_dcp_frame_rate (14.5);
522         frc = FrameRateConversion (14.5, best);
523         BOOST_CHECK_EQUAL (best, 30);
524         BOOST_CHECK_EQUAL (frc.skip, false);
525         BOOST_CHECK_EQUAL (frc.repeat, true);
526         BOOST_CHECK_EQUAL (frc.change_speed, true);
527
528         best = best_dcp_frame_rate (12.6);
529         frc = FrameRateConversion (12.6, best);
530         BOOST_CHECK_EQUAL (best, 25);
531         BOOST_CHECK_EQUAL (frc.skip, false);
532         BOOST_CHECK_EQUAL (frc.repeat, true);
533         BOOST_CHECK_EQUAL (frc.change_speed, true);
534
535         best = best_dcp_frame_rate (12.4);
536         frc = FrameRateConversion (12.4, best);
537         BOOST_CHECK_EQUAL (best, 25);
538         BOOST_CHECK_EQUAL (frc.skip, false);
539         BOOST_CHECK_EQUAL (frc.repeat, true);
540         BOOST_CHECK_EQUAL (frc.change_speed, true);
541
542         best = best_dcp_frame_rate (12);
543         frc = FrameRateConversion (12, best);
544         BOOST_CHECK_EQUAL (best, 24);
545         BOOST_CHECK_EQUAL (frc.skip, false);
546         BOOST_CHECK_EQUAL (frc.repeat, true);
547         BOOST_CHECK_EQUAL (frc.change_speed, false);
548
549         /* Now add some more rates and see if it will use them
550            in preference to skip/repeat.
551         */
552
553         afr.push_back (48);
554         afr.push_back (50);
555         afr.push_back (60);
556         Config::instance()->set_allowed_dcp_frame_rates (afr);
557
558         best = best_dcp_frame_rate (60);
559         frc = FrameRateConversion (60, best);
560         BOOST_CHECK_EQUAL (best, 60);
561         BOOST_CHECK_EQUAL (frc.skip, false);
562         BOOST_CHECK_EQUAL (frc.repeat, false);
563         BOOST_CHECK_EQUAL (frc.change_speed, false);
564         
565         best = best_dcp_frame_rate (50);
566         frc = FrameRateConversion (50, best);
567         BOOST_CHECK_EQUAL (best, 50);
568         BOOST_CHECK_EQUAL (frc.skip, false);
569         BOOST_CHECK_EQUAL (frc.repeat, false);
570         BOOST_CHECK_EQUAL (frc.change_speed, false);
571
572         best = best_dcp_frame_rate (48);
573         frc = FrameRateConversion (48, best);
574         BOOST_CHECK_EQUAL (best, 48);
575         BOOST_CHECK_EQUAL (frc.skip, false);
576         BOOST_CHECK_EQUAL (frc.repeat, false);
577         BOOST_CHECK_EQUAL (frc.change_speed, false);
578
579         /* Check some out-there conversions (not the best) */
580         
581         frc = FrameRateConversion (14.99, 24);
582         BOOST_CHECK_EQUAL (frc.skip, false);
583         BOOST_CHECK_EQUAL (frc.repeat, true);
584         BOOST_CHECK_EQUAL (frc.change_speed, true);
585
586         /* Check some conversions with limited DCP targets */
587
588         afr.clear ();
589         afr.push_back (24);
590         Config::instance()->set_allowed_dcp_frame_rates (afr);
591
592         best = best_dcp_frame_rate (25);
593         frc = FrameRateConversion (25, best);
594         BOOST_CHECK_EQUAL (best, 24);
595         BOOST_CHECK_EQUAL (frc.skip, false);
596         BOOST_CHECK_EQUAL (frc.repeat, false);
597         BOOST_CHECK_EQUAL (frc.change_speed, true);
598 }
599
600 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
601 {
602         std::list<int> afr;
603         afr.push_back (24);
604         afr.push_back (25);
605         afr.push_back (30);
606         Config::instance()->set_allowed_dcp_frame_rates (afr);
607
608         shared_ptr<Film> f = new_test_film ("audio_sampling_rate_test");
609 //      f->set_source_frame_rate (24);
610         f->set_dcp_frame_rate (24);
611
612 //      f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
613         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 48000);
614
615 //      f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
616         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 48000);
617
618 //      f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 80000, 0)));
619         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 96000);
620
621 //      f->set_source_frame_rate (23.976);
622         f->set_dcp_frame_rate (best_dcp_frame_rate (23.976));
623 //      f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
624         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 47952);
625
626 //      f->set_source_frame_rate (29.97);
627         f->set_dcp_frame_rate (best_dcp_frame_rate (29.97));
628         BOOST_CHECK_EQUAL (f->dcp_frame_rate (), 30);
629 //      f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
630         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 47952);
631
632 //      f->set_source_frame_rate (25);
633         f->set_dcp_frame_rate (24);
634 //      f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
635         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 50000);
636
637 //      f->set_source_frame_rate (25);
638         f->set_dcp_frame_rate (24);
639 //      f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
640         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 50000);
641
642         /* Check some out-there conversions (not the best) */
643         
644 //      f->set_source_frame_rate (14.99);
645         f->set_dcp_frame_rate (25);
646 //      f->set_content_audio_stream (shared_ptr<AudioStream> (new FFmpegAudioStream ("a", 42, 16000, 0)));
647         /* The FrameRateConversion within target_audio_sample_rate should choose to double-up
648            the 14.99 fps video to 30 and then run it slow at 25.
649         */
650         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), rint (48000 * 2 * 14.99 / 25));
651 }
652
653 class TestJob : public Job
654 {
655 public:
656         TestJob (shared_ptr<Film> f)
657                 : Job (f)
658         {
659
660         }
661
662         void set_finished_ok () {
663                 set_state (FINISHED_OK);
664         }
665
666         void set_finished_error () {
667                 set_state (FINISHED_ERROR);
668         }
669
670         void run ()
671         {
672                 while (1) {
673                         if (finished ()) {
674                                 return;
675                         }
676                 }
677         }
678
679         string name () const {
680                 return "";
681         }
682 };
683
684 BOOST_AUTO_TEST_CASE (job_manager_test)
685 {
686         shared_ptr<Film> f;
687
688         /* Single job */
689         shared_ptr<TestJob> a (new TestJob (f));
690
691         JobManager::instance()->add (a);
692         dvdomatic_sleep (1);
693         BOOST_CHECK_EQUAL (a->running (), true);
694         a->set_finished_ok ();
695         dvdomatic_sleep (2);
696         BOOST_CHECK_EQUAL (a->finished_ok(), true);
697 }
698
699 BOOST_AUTO_TEST_CASE (compact_image_test)
700 {
701         SimpleImage* s = new SimpleImage (PIX_FMT_RGB24, libdcp::Size (50, 50), false);
702         BOOST_CHECK_EQUAL (s->components(), 1);
703         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
704         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
705         BOOST_CHECK (s->data()[0]);
706         BOOST_CHECK (!s->data()[1]);
707         BOOST_CHECK (!s->data()[2]);
708         BOOST_CHECK (!s->data()[3]);
709
710         /* copy constructor */
711         SimpleImage* t = new SimpleImage (*s);
712         BOOST_CHECK_EQUAL (t->components(), 1);
713         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
714         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
715         BOOST_CHECK (t->data()[0]);
716         BOOST_CHECK (!t->data()[1]);
717         BOOST_CHECK (!t->data()[2]);
718         BOOST_CHECK (!t->data()[3]);
719         BOOST_CHECK (t->data() != s->data());
720         BOOST_CHECK (t->data()[0] != s->data()[0]);
721         BOOST_CHECK (t->line_size() != s->line_size());
722         BOOST_CHECK (t->line_size()[0] == s->line_size()[0]);
723         BOOST_CHECK (t->stride() != s->stride());
724         BOOST_CHECK (t->stride()[0] == s->stride()[0]);
725
726         /* assignment operator */
727         SimpleImage* u = new SimpleImage (PIX_FMT_YUV422P, libdcp::Size (150, 150), true);
728         *u = *s;
729         BOOST_CHECK_EQUAL (u->components(), 1);
730         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
731         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
732         BOOST_CHECK (u->data()[0]);
733         BOOST_CHECK (!u->data()[1]);
734         BOOST_CHECK (!u->data()[2]);
735         BOOST_CHECK (!u->data()[3]);
736         BOOST_CHECK (u->data() != s->data());
737         BOOST_CHECK (u->data()[0] != s->data()[0]);
738         BOOST_CHECK (u->line_size() != s->line_size());
739         BOOST_CHECK (u->line_size()[0] == s->line_size()[0]);
740         BOOST_CHECK (u->stride() != s->stride());
741         BOOST_CHECK (u->stride()[0] == s->stride()[0]);
742
743         delete s;
744         delete t;
745         delete u;
746 }
747
748 BOOST_AUTO_TEST_CASE (aligned_image_test)
749 {
750         SimpleImage* s = new SimpleImage (PIX_FMT_RGB24, libdcp::Size (50, 50), true);
751         BOOST_CHECK_EQUAL (s->components(), 1);
752         /* 160 is 150 aligned to the nearest 32 bytes */
753         BOOST_CHECK_EQUAL (s->stride()[0], 160);
754         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
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], 160);
764         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
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), false);
778         *u = *s;
779         BOOST_CHECK_EQUAL (u->components(), 1);
780         BOOST_CHECK_EQUAL (u->stride()[0], 160);
781         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
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 }