Do the delay line in floats with an AudioBuffer.
[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 "format.h"
25 #include "film.h"
26 #include "filter.h"
27 #include "job_manager.h"
28 #include "util.h"
29 #include "exceptions.h"
30 #include "dvd.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 #define BOOST_TEST_DYN_LINK
42 #define BOOST_TEST_MODULE dvdomatic_test
43 #include <boost/test/unit_test.hpp>
44
45 using std::string;
46 using std::list;
47 using std::stringstream;
48 using std::vector;
49 using boost::shared_ptr;
50 using boost::thread;
51
52 void
53 setup_test_config ()
54 {
55         Config::instance()->set_num_local_encoding_threads (1);
56         Config::instance()->set_colour_lut_index (0);
57         Config::instance()->set_j2k_bandwidth (200000000);
58         Config::instance()->set_servers (vector<ServerDescription*> ());
59         Config::instance()->set_server_port (61920);
60 }
61
62 shared_ptr<Film>
63 new_test_film (string name)
64 {
65         string const d = String::compose ("build/test/%1", name);
66         if (boost::filesystem::exists (d)) {
67                 boost::filesystem::remove_all (d);
68         }
69         return shared_ptr<Film> (new Film (d, false));
70 }
71
72 BOOST_AUTO_TEST_CASE (film_metadata_test)
73 {
74         dvdomatic_setup ();
75         setup_test_config ();
76
77         string const test_film = "build/test/film_metadata_test";
78         
79         if (boost::filesystem::exists (test_film)) {
80                 boost::filesystem::remove_all (test_film);
81         }
82
83         BOOST_CHECK_THROW (new Film (test_film, true), OpenFileError);
84         
85         shared_ptr<Film> f (new Film (test_film, false));
86         BOOST_CHECK (f->format() == 0);
87         BOOST_CHECK (f->dcp_content_type() == 0);
88         BOOST_CHECK (f->filters ().empty());
89
90         f->set_name ("fred");
91         BOOST_CHECK_THROW (f->set_content ("jim"), OpenFileError);
92         f->set_dcp_content_type (DCPContentType::from_pretty_name ("Short"));
93         f->set_format (Format::from_nickname ("Flat"));
94         f->set_left_crop (1);
95         f->set_right_crop (2);
96         f->set_top_crop (3);
97         f->set_bottom_crop (4);
98         vector<Filter const *> f_filters;
99         f_filters.push_back (Filter::from_id ("pphb"));
100         f_filters.push_back (Filter::from_id ("unsharp"));
101         f->set_filters (f_filters);
102         f->set_dcp_frames (42);
103         f->set_dcp_ab (true);
104         f->write_metadata ();
105
106         stringstream s;
107         s << "diff -u test/metadata.ref " << test_film << "/metadata";
108         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
109
110         shared_ptr<Film> g (new Film (test_film, true));
111
112         BOOST_CHECK_EQUAL (g->name(), "fred");
113         BOOST_CHECK_EQUAL (g->dcp_content_type(), DCPContentType::from_pretty_name ("Short"));
114         BOOST_CHECK_EQUAL (g->format(), Format::from_nickname ("Flat"));
115         BOOST_CHECK_EQUAL (g->crop().left, 1);
116         BOOST_CHECK_EQUAL (g->crop().right, 2);
117         BOOST_CHECK_EQUAL (g->crop().top, 3);
118         BOOST_CHECK_EQUAL (g->crop().bottom, 4);
119         vector<Filter const *> g_filters = g->filters ();
120         BOOST_CHECK_EQUAL (g_filters.size(), 2);
121         BOOST_CHECK_EQUAL (g_filters.front(), Filter::from_id ("pphb"));
122         BOOST_CHECK_EQUAL (g_filters.back(), Filter::from_id ("unsharp"));
123         BOOST_CHECK_EQUAL (g->dcp_frames(), 42);
124         BOOST_CHECK_EQUAL (g->dcp_ab(), true);
125         
126         g->write_metadata ();
127         BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0);
128 }
129
130 BOOST_AUTO_TEST_CASE (format_test)
131 {
132         Format::setup_formats ();
133         
134         Format const * f = Format::from_nickname ("Flat");
135         BOOST_CHECK (f);
136         BOOST_CHECK_EQUAL (f->ratio_as_integer(shared_ptr<const Film> ()), 185);
137         
138         f = Format::from_nickname ("Scope");
139         BOOST_CHECK (f);
140         BOOST_CHECK_EQUAL (f->ratio_as_integer(shared_ptr<const Film> ()), 239);
141 }
142
143 BOOST_AUTO_TEST_CASE (util_test)
144 {
145         string t = "Hello this is a string \"with quotes\" and indeed without them";
146         vector<string> b = split_at_spaces_considering_quotes (t);
147         vector<string>::iterator i = b.begin ();
148         BOOST_CHECK_EQUAL (*i++, "Hello");
149         BOOST_CHECK_EQUAL (*i++, "this");
150         BOOST_CHECK_EQUAL (*i++, "is");
151         BOOST_CHECK_EQUAL (*i++, "a");
152         BOOST_CHECK_EQUAL (*i++, "string");
153         BOOST_CHECK_EQUAL (*i++, "with quotes");
154         BOOST_CHECK_EQUAL (*i++, "and");
155         BOOST_CHECK_EQUAL (*i++, "indeed");
156         BOOST_CHECK_EQUAL (*i++, "without");
157         BOOST_CHECK_EQUAL (*i++, "them");
158 }
159
160 BOOST_AUTO_TEST_CASE (dvd_test)
161 {
162         list<DVDTitle> const t = dvd_titles ("test/dvd");
163         BOOST_CHECK_EQUAL (t.size(), 3);
164         list<DVDTitle>::const_iterator i = t.begin ();
165         
166         BOOST_CHECK_EQUAL (i->number, 1);
167         BOOST_CHECK_EQUAL (i->size, 0);
168         ++i;
169         
170         BOOST_CHECK_EQUAL (i->number, 2);
171         BOOST_CHECK_EQUAL (i->size, 14);
172         ++i;
173         
174         BOOST_CHECK_EQUAL (i->number, 3);
175         BOOST_CHECK_EQUAL (i->size, 7);
176 }
177
178 void
179 do_positive_delay_line_test (int delay_length, int data_length)
180 {
181         DelayLine d (6, delay_length);
182         shared_ptr<AudioBuffers> data (new AudioBuffers (6, data_length));
183
184         int in = 0;
185         int out = 0;
186         int returned = 0;
187         int zeros = 0;
188         
189         for (int i = 0; i < 64; ++i) {
190                 for (int j = 0; j < data_length; ++j) {
191                         for (int c = 0; c < 6; ++c ) {
192                                 data->data(c)[j] = in;
193                                 ++in;
194                         }
195                 }
196
197                 d.feed (data);
198                 returned += data->frames ();
199
200                 for (int j = 0; j < data->frames(); ++j) {
201                         if (zeros < delay_length) {
202                                 for (int c = 0; c < 6; ++c) {
203                                         BOOST_CHECK_EQUAL (data->data(c)[j], 0);
204                                 }
205                                 ++zeros;
206                         } else {
207                                 for (int c = 0; c < 6; ++c) {
208                                         BOOST_CHECK_EQUAL (data->data(c)[j], out);
209                                         ++out;
210                                 }
211                         }
212                 }
213         }
214
215         BOOST_CHECK_EQUAL (returned, 64 * data_length);
216 }
217
218 void
219 do_negative_delay_line_test (int delay_length, int data_length)
220 {
221         DelayLine d (6, delay_length);
222         shared_ptr<AudioBuffers> data (new AudioBuffers (6, data_length));
223
224         int in = 0;
225         int out = -delay_length * 6;
226         int returned = 0;
227         
228         for (int i = 0; i < 256; ++i) {
229                 data->set_frames (data_length);
230                 for (int j = 0; j < data_length; ++j) {
231                         for (int c = 0; c < 6; ++c) {
232                                 data->data(c)[j] = in;
233                                 ++in;
234                         }
235                 }
236
237                 d.feed (data);
238                 returned += data->frames ();
239
240                 for (int j = 0; j < data->frames(); ++j) {
241                         for (int c = 0; c < 6; ++c) {
242                                 BOOST_CHECK_EQUAL (data->data(c)[j], out);
243                                 ++out;
244                         }
245                 }
246         }
247
248         returned += -delay_length;
249         BOOST_CHECK_EQUAL (returned, 256 * data_length);
250 }
251
252 BOOST_AUTO_TEST_CASE (delay_line_test)
253 {
254         do_positive_delay_line_test (64, 128);
255         do_positive_delay_line_test (128, 64);
256         do_positive_delay_line_test (3, 512);
257         do_positive_delay_line_test (512, 3);
258
259         do_positive_delay_line_test (0, 64);
260
261         do_negative_delay_line_test (-64, 128);
262         do_negative_delay_line_test (-128, 64);
263         do_negative_delay_line_test (-3, 512);
264         do_negative_delay_line_test (-512, 3);
265 }
266
267 BOOST_AUTO_TEST_CASE (md5_digest_test)
268 {
269         string const t = md5_digest ("test/md5.test");
270         BOOST_CHECK_EQUAL (t, "15058685ba99decdc4398c7634796eb0");
271
272         BOOST_CHECK_THROW (md5_digest ("foobar"), OpenFileError);
273 }
274
275 BOOST_AUTO_TEST_CASE (paths_test)
276 {
277         shared_ptr<Film> f = new_test_film ("paths_test");
278         f->set_directory ("build/test/a/b/c/d/e");
279         vector<int> thumbs;
280         thumbs.push_back (42);
281         f->set_thumbs (thumbs);
282         BOOST_CHECK_EQUAL (f->thumb_file (0), "build/test/a/b/c/d/e/thumbs/00000042.png");
283
284         f->_content = "/foo/bar/baz";
285         BOOST_CHECK_EQUAL (f->content_path(), "/foo/bar/baz");
286         f->_content = "foo/bar/baz";
287         BOOST_CHECK_EQUAL (f->content_path(), "build/test/a/b/c/d/e/foo/bar/baz");
288 }
289
290 void
291 do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription* description, shared_ptr<EncodedData> locally_encoded, int N)
292 {
293         shared_ptr<EncodedData> remotely_encoded;
294         BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
295         BOOST_CHECK (remotely_encoded);
296         
297         BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size());
298         BOOST_CHECK (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()) == 0);
299 }
300
301 BOOST_AUTO_TEST_CASE (client_server_test)
302 {
303         shared_ptr<Image> image (new CompactImage (PIX_FMT_RGB24, Size (1998, 1080)));
304         uint8_t* p = image->data()[0];
305         
306         for (int y = 0; y < 1080; ++y) {
307                 for (int x = 0; x < 1998; ++x) {
308                         *p++ = x % 256;
309                         *p++ = y % 256;
310                         *p++ = (x + y) % 256;
311                 }
312         }
313
314         shared_ptr<Image> sub_image (new CompactImage (PIX_FMT_RGBA, Size (100, 200)));
315         p = sub_image->data()[0];
316         for (int y = 0; y < 200; ++y) {
317                 for (int x = 0; x < 100; ++x) {
318                         *p++ = y % 256;
319                         *p++ = x % 256;
320                         *p++ = (x + y) % 256;
321                         *p++ = 1;
322                 }
323         }
324
325         shared_ptr<Subtitle> subtitle (new Subtitle (Position (50, 60), sub_image));
326
327         FileLog log ("build/test/client_server_test.log");
328
329         shared_ptr<DCPVideoFrame> frame (
330                 new DCPVideoFrame (
331                         image,
332                         subtitle,
333                         Size (1998, 1080),
334                         0,
335                         0,
336                         1,
337                         Scaler::from_id ("bicubic"),
338                         0,
339                         24,
340                         "",
341                         0,
342                         200000000,
343                         &log
344                         )
345                 );
346
347         shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
348         BOOST_ASSERT (locally_encoded);
349         
350         Server* server = new Server (&log);
351
352         new thread (boost::bind (&Server::run, server, 2));
353
354         /* Let the server get itself ready */
355         dvdomatic_sleep (1);
356
357         ServerDescription description ("localhost", 2);
358
359         list<thread*> threads;
360         for (int i = 0; i < 8; ++i) {
361                 threads.push_back (new thread (boost::bind (do_remote_encode, frame, &description, locally_encoded, i)));
362         }
363
364         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
365                 (*i)->join ();
366         }
367
368         for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
369                 delete *i;
370         }
371 }
372
373 BOOST_AUTO_TEST_CASE (make_dcp_test)
374 {
375         shared_ptr<Film> film = new_test_film ("make_dcp_test");
376         film->set_name ("test_film2");
377         film->set_content ("../../../test/test.mp4");
378         film->set_format (Format::from_nickname ("Flat"));
379         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
380         film->make_dcp (true);
381
382         while (JobManager::instance()->work_to_do ()) {
383                 dvdomatic_sleep (1);
384         }
385         
386         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
387 }
388
389 BOOST_AUTO_TEST_CASE (make_dcp_with_range_test)
390 {
391         shared_ptr<Film> film = new_test_film ("make_dcp_with_range_test");
392         film->set_name ("test_film3");
393         film->set_content ("../../../test/test.mp4");
394         film->examine_content ();
395         film->set_format (Format::from_nickname ("Flat"));
396         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
397         film->set_dcp_frames (42);
398         film->make_dcp (true);
399
400         while (JobManager::instance()->work_to_do() && !JobManager::instance()->errors()) {
401                 dvdomatic_sleep (1);
402         }
403
404         BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
405 }
406
407 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
408 {
409         shared_ptr<Film> f = new_test_film ("audio_sampling_rate_test");
410         f->set_frames_per_second (24);
411
412         f->set_audio_sample_rate (48000);
413         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 48000);
414
415         f->set_audio_sample_rate (44100);
416         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 48000);
417
418         f->set_audio_sample_rate (80000);
419         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 96000);
420
421         f->set_frames_per_second (23.976);
422         f->set_audio_sample_rate (48000);
423         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 47952);
424
425         f->set_frames_per_second (29.97);
426         f->set_audio_sample_rate (48000);
427         BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 47952);
428 }
429
430 class TestJob : public Job
431 {
432 public:
433         TestJob (shared_ptr<Film> f, shared_ptr<Job> req)
434                 : Job (f, req)
435         {
436
437         }
438
439         void set_finished_ok () {
440                 set_state (FINISHED_OK);
441         }
442
443         void set_finished_error () {
444                 set_state (FINISHED_ERROR);
445         }
446
447         void run ()
448         {
449                 while (1) {
450                         if (finished ()) {
451                                 return;
452                         }
453                 }
454         }
455
456         string name () const {
457                 return "";
458         }
459 };
460
461 BOOST_AUTO_TEST_CASE (job_manager_test)
462 {
463         shared_ptr<Film> f;
464
465         /* Single job, no dependency */
466         shared_ptr<TestJob> a (new TestJob (f, shared_ptr<Job> ()));
467
468         JobManager::instance()->add (a);
469         dvdomatic_sleep (1);
470         BOOST_CHECK_EQUAL (a->running (), true);
471         a->set_finished_ok ();
472         dvdomatic_sleep (2);
473         BOOST_CHECK_EQUAL (a->finished_ok(), true);
474
475         /* Two jobs, dependency */
476         a.reset (new TestJob (f, shared_ptr<Job> ()));
477         shared_ptr<TestJob> b (new TestJob (f, a));
478
479         JobManager::instance()->add (a);
480         JobManager::instance()->add (b);
481         dvdomatic_sleep (2);
482         BOOST_CHECK_EQUAL (a->running(), true);
483         BOOST_CHECK_EQUAL (b->running(), false);
484         a->set_finished_ok ();
485         dvdomatic_sleep (2);
486         BOOST_CHECK_EQUAL (a->finished_ok(), true);
487         BOOST_CHECK_EQUAL (b->running(), true);
488         b->set_finished_ok ();
489         dvdomatic_sleep (2);
490         BOOST_CHECK_EQUAL (b->finished_ok(), true);
491
492         /* Two jobs, dependency, first fails */
493         a.reset (new TestJob (f, shared_ptr<Job> ()));
494         b.reset (new TestJob (f, a));
495
496         JobManager::instance()->add (a);
497         JobManager::instance()->add (b);
498         dvdomatic_sleep (2);
499         BOOST_CHECK_EQUAL (a->running(), true);
500         BOOST_CHECK_EQUAL (b->running(), false);
501         a->set_finished_error ();
502         dvdomatic_sleep (2);
503         BOOST_CHECK_EQUAL (a->finished_in_error(), true);
504         BOOST_CHECK_EQUAL (b->running(), false);
505 }
506
507 BOOST_AUTO_TEST_CASE (stream_test)
508 {
509         AudioStream a ("4 9 hello there world");
510         BOOST_CHECK_EQUAL (a.id(), 4);
511         BOOST_CHECK_EQUAL (a.channels(), 9);
512         BOOST_CHECK_EQUAL (a.name(), "hello there world");
513         BOOST_CHECK_EQUAL (a.to_string(), "4 9 hello there world");
514
515         SubtitleStream s ("5 a b c");
516         BOOST_CHECK_EQUAL (s.id(), 5);
517         BOOST_CHECK_EQUAL (s.name(), "a b c");
518 }