Obey requests to change the video range of RGB content.
[dcpomatic.git] / test / test.cc
1 /*
2     Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  test/test.cc
22  *  @brief Overall test stuff and useful methods for tests.
23  */
24
25 #include "lib/config.h"
26 #include "lib/util.h"
27 #include "lib/signal_manager.h"
28 #include "lib/film.h"
29 #include "lib/job_manager.h"
30 #include "lib/job.h"
31 #include "lib/cross.h"
32 #include "lib/encode_server_finder.h"
33 #include "lib/ffmpeg_image_proxy.h"
34 #include "lib/image.h"
35 #include "lib/ratio.h"
36 #include "lib/dcp_content_type.h"
37 #include "lib/log_entry.h"
38 #include "lib/compose.hpp"
39 #include "lib/file_log.h"
40 #include "lib/dcpomatic_log.h"
41 #include "test.h"
42 #include <dcp/dcp.h>
43 #include <dcp/cpl.h>
44 #include <dcp/reel.h>
45 #include <dcp/reel_picture_asset.h>
46 #include <dcp/mono_picture_frame.h>
47 #include <dcp/mono_picture_asset.h>
48 #include <dcp/openjpeg_image.h>
49 #include <asdcp/AS_DCP.h>
50 #include <png.h>
51 #include <sndfile.h>
52 #include <libxml++/libxml++.h>
53 extern "C" {
54 #include <libavformat/avformat.h>
55 }
56 #define BOOST_TEST_DYN_LINK
57 #define BOOST_TEST_MODULE dcpomatic_test
58 #include <boost/test/unit_test.hpp>
59 #include <boost/algorithm/string.hpp>
60 #include <list>
61 #include <vector>
62 #include <iostream>
63
64 using std::string;
65 using std::vector;
66 using std::min;
67 using std::cout;
68 using std::cerr;
69 using std::list;
70 using std::abs;
71 using boost::shared_ptr;
72 using boost::scoped_array;
73 using boost::dynamic_pointer_cast;
74 #if BOOST_VERSION >= 106100
75 using namespace boost::placeholders;
76 #endif
77
78
79 boost::filesystem::path
80 TestPaths::TestPaths::private_data ()
81 {
82         char* env = getenv("DCPOMATIC_TEST_PRIVATE");
83         if (env) {
84                 return boost::filesystem::path(env);
85         }
86
87         return boost::filesystem::canonical(boost::filesystem::path ("..") / boost::filesystem::path ("dcpomatic-test-private"));
88 }
89
90
91 boost::filesystem::path TestPaths::xsd ()
92 {
93         return boost::filesystem::canonical(boost::filesystem::path("..") / boost::filesystem::path("libdcp") / boost::filesystem::path("xsd"));
94 }
95
96
97 void
98 setup_test_config ()
99 {
100         Config::instance()->set_master_encoding_threads (boost::thread::hardware_concurrency());
101         Config::instance()->set_server_encoding_threads (1);
102         Config::instance()->set_server_port_base (61921);
103         Config::instance()->set_default_isdcf_metadata (ISDCFMetadata ());
104         Config::instance()->set_default_container (Ratio::from_id ("185"));
105         Config::instance()->set_default_dcp_content_type (static_cast<DCPContentType*> (0));
106         Config::instance()->set_default_audio_delay (0);
107         Config::instance()->set_default_j2k_bandwidth (100000000);
108         Config::instance()->set_default_interop (false);
109         Config::instance()->set_default_still_length (10);
110         Config::instance()->set_log_types (
111                 LogEntry::TYPE_GENERAL | LogEntry::TYPE_WARNING | LogEntry::TYPE_ERROR | LogEntry::TYPE_DEBUG_THREED | LogEntry::TYPE_DEBUG_ENCODE | LogEntry::TYPE_DEBUG_PLAYER
112                 );
113         Config::instance()->set_automatic_audio_analysis (false);
114 }
115
116 class TestSignalManager : public SignalManager
117 {
118 public:
119         /* No wakes in tests: we call ui_idle ourselves */
120         void wake_ui ()
121         {
122
123         }
124 };
125
126 struct TestConfig
127 {
128         TestConfig ()
129         {
130                 State::override_path = "build/test/state";
131                 boost::filesystem::remove_all (*State::override_path);
132
133                 dcpomatic_setup ();
134                 setup_test_config ();
135
136                 EncodeServerFinder::instance()->stop ();
137
138                 signal_manager = new TestSignalManager ();
139
140                 dcpomatic_log.reset (new FileLog("build/test/log"));
141         }
142
143         ~TestConfig ()
144         {
145                 JobManager::drop ();
146         }
147 };
148
149 BOOST_GLOBAL_FIXTURE (TestConfig);
150
151 boost::filesystem::path
152 test_film_dir (string name)
153 {
154         boost::filesystem::path p;
155         p /= "build";
156         p /= "test";
157         p /= name;
158         return p;
159 }
160
161 shared_ptr<Film>
162 new_test_film (string name)
163 {
164         boost::filesystem::path p = test_film_dir (name);
165         if (boost::filesystem::exists (p)) {
166                 boost::filesystem::remove_all (p);
167         }
168
169         shared_ptr<Film> film = shared_ptr<Film> (new Film (p));
170         film->write_metadata ();
171         return film;
172 }
173
174 shared_ptr<Film>
175 new_test_film2 (string name)
176 {
177         boost::filesystem::path p = test_film_dir (name);
178         if (boost::filesystem::exists (p)) {
179                 boost::filesystem::remove_all (p);
180         }
181
182         shared_ptr<Film> film = shared_ptr<Film> (new Film (p));
183         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
184         film->set_container (Ratio::from_id ("185"));
185         film->write_metadata ();
186         return film;
187 }
188
189 void
190 check_wav_file (boost::filesystem::path ref, boost::filesystem::path check)
191 {
192         SF_INFO ref_info;
193         ref_info.format = 0;
194         SNDFILE* ref_file = sf_open (ref.string().c_str(), SFM_READ, &ref_info);
195         BOOST_CHECK (ref_file);
196
197         SF_INFO check_info;
198         check_info.format = 0;
199         SNDFILE* check_file = sf_open (check.string().c_str(), SFM_READ, &check_info);
200         BOOST_CHECK (check_file);
201
202         BOOST_CHECK_EQUAL (ref_info.frames, check_info.frames);
203         BOOST_CHECK_EQUAL (ref_info.samplerate, check_info.samplerate);
204         BOOST_CHECK_EQUAL (ref_info.channels, check_info.channels);
205         BOOST_CHECK_EQUAL (ref_info.format, check_info.format);
206
207         /* buffer_size is in frames */
208         sf_count_t const buffer_size = 65536 * ref_info.channels;
209         scoped_array<int32_t> ref_buffer (new int32_t[buffer_size]);
210         scoped_array<int32_t> check_buffer (new int32_t[buffer_size]);
211
212         sf_count_t N = ref_info.frames;
213         while (N) {
214                 sf_count_t this_time = min (buffer_size, N);
215                 sf_count_t r = sf_readf_int (ref_file, ref_buffer.get(), this_time);
216                 BOOST_CHECK_EQUAL (r, this_time);
217                 r = sf_readf_int (check_file, check_buffer.get(), this_time);
218                 BOOST_CHECK_EQUAL (r, this_time);
219
220                 for (sf_count_t i = 0; i < this_time; ++i) {
221                         BOOST_REQUIRE_MESSAGE (
222                                 abs (ref_buffer[i] - check_buffer[i]) <= 65536,
223                                 ref << " differs from " << check << " at " << (ref_info.frames - N + i) << " of " << ref_info.frames
224                                 << "(" << ref_buffer[i] << " vs " << check_buffer[i] << ")"
225                                 );
226                 }
227
228                 N -= this_time;
229         }
230 }
231
232 void
233 check_mxf_audio_file (boost::filesystem::path ref, boost::filesystem::path check)
234 {
235         ASDCP::PCM::MXFReader ref_reader;
236         BOOST_REQUIRE (!ASDCP_FAILURE (ref_reader.OpenRead (ref.string().c_str())));
237
238         ASDCP::PCM::AudioDescriptor ref_desc;
239         BOOST_REQUIRE (!ASDCP_FAILURE (ref_reader.FillAudioDescriptor (ref_desc)));
240
241         ASDCP::PCM::MXFReader check_reader;
242         BOOST_REQUIRE (!ASDCP_FAILURE (check_reader.OpenRead (check.string().c_str())));
243
244         ASDCP::PCM::AudioDescriptor check_desc;
245         BOOST_REQUIRE (!ASDCP_FAILURE (check_reader.FillAudioDescriptor (check_desc)));
246
247         BOOST_REQUIRE_EQUAL (ref_desc.ContainerDuration, check_desc.ContainerDuration);
248
249         ASDCP::PCM::FrameBuffer ref_buffer (Kumu::Megabyte);
250         ASDCP::PCM::FrameBuffer check_buffer (Kumu::Megabyte);
251         for (size_t i = 0; i < ref_desc.ContainerDuration; ++i) {
252                 ref_reader.ReadFrame (i, ref_buffer, 0);
253                 check_reader.ReadFrame (i, check_buffer, 0);
254                 BOOST_REQUIRE (memcmp(ref_buffer.RoData(), check_buffer.RoData(), ref_buffer.Size()) == 0);
255         }
256 }
257
258
259 /** @return true if the files are the same, otherwise false */
260 bool
261 mxf_atmos_files_same (boost::filesystem::path ref, boost::filesystem::path check, bool verbose)
262 {
263         ASDCP::ATMOS::MXFReader ref_reader;
264         BOOST_REQUIRE (!ASDCP_FAILURE(ref_reader.OpenRead(ref.string().c_str())));
265
266         ASDCP::ATMOS::AtmosDescriptor ref_desc;
267         BOOST_REQUIRE (!ASDCP_FAILURE(ref_reader.FillAtmosDescriptor(ref_desc)));
268
269         ASDCP::ATMOS::MXFReader check_reader;
270         BOOST_REQUIRE (!ASDCP_FAILURE(check_reader.OpenRead(check.string().c_str())));
271
272         ASDCP::ATMOS::AtmosDescriptor check_desc;
273         BOOST_REQUIRE (!ASDCP_FAILURE(check_reader.FillAtmosDescriptor(check_desc)));
274
275         if (ref_desc.EditRate.Numerator != check_desc.EditRate.Numerator) {
276                 if (verbose) {
277                         std::cout << "EditRate.Numerator differs.\n";
278                 }
279                 return false;
280         }
281         if (ref_desc.EditRate.Denominator != check_desc.EditRate.Denominator) {
282                 if (verbose) {
283                         std::cout << "EditRate.Denominator differs.\n";
284                 }
285                 return false;
286         }
287         if (ref_desc.ContainerDuration != check_desc.ContainerDuration) {
288                 if (verbose) {
289                         std::cout << "EditRate.ContainerDuration differs.\n";
290                 }
291                 return false;
292         }
293         if (ref_desc.FirstFrame != check_desc.FirstFrame) {
294                 if (verbose) {
295                         std::cout << "EditRate.FirstFrame differs.\n";
296                 }
297                 return false;
298         }
299         if (ref_desc.MaxChannelCount != check_desc.MaxChannelCount) {
300                 if (verbose) {
301                         std::cout << "EditRate.MaxChannelCount differs.\n";
302                 }
303                 return false;
304         }
305         if (ref_desc.MaxObjectCount != check_desc.MaxObjectCount) {
306                 if (verbose) {
307                         std::cout << "EditRate.MaxObjectCount differs.\n";
308                 }
309                 return false;
310         }
311         if (ref_desc.AtmosVersion != check_desc.AtmosVersion) {
312                 if (verbose) {
313                         std::cout << "EditRate.AtmosVersion differs.\n";
314                 }
315                 return false;
316         }
317
318         ASDCP::DCData::FrameBuffer ref_buffer (Kumu::Megabyte);
319         ASDCP::DCData::FrameBuffer check_buffer (Kumu::Megabyte);
320         for (size_t i = 0; i < ref_desc.ContainerDuration; ++i) {
321                 ref_reader.ReadFrame (i, ref_buffer, 0);
322                 check_reader.ReadFrame (i, check_buffer, 0);
323                 if (memcmp(ref_buffer.RoData(), check_buffer.RoData(), ref_buffer.Size())) {
324                         if (verbose) {
325                                 std::cout << "data differs.\n";
326                         }
327                         return false;
328                 }
329         }
330
331         return true;
332 }
333
334
335 static
336 double
337 rms_error (boost::filesystem::path ref, boost::filesystem::path check)
338 {
339         FFmpegImageProxy ref_proxy (ref, VIDEO_RANGE_FULL);
340         shared_ptr<Image> ref_image = ref_proxy.image().image;
341         FFmpegImageProxy check_proxy (check, VIDEO_RANGE_FULL);
342         shared_ptr<Image> check_image = check_proxy.image().image;
343
344         BOOST_REQUIRE_EQUAL (ref_image->pixel_format(), check_image->pixel_format());
345         AVPixelFormat const format = ref_image->pixel_format();
346
347         BOOST_REQUIRE (ref_image->size() == check_image->size());
348         int const width = ref_image->size().width;
349         int const height = ref_image->size().height;
350
351         double sum_square = 0;
352         switch (format) {
353                 case AV_PIX_FMT_RGBA:
354                 {
355                         for (int y = 0; y < height; ++y) {
356                                 uint8_t* p = ref_image->data()[0] + y * ref_image->stride()[0];
357                                 uint8_t* q = check_image->data()[0] + y * check_image->stride()[0];
358                                 for (int x = 0; x < width; ++x) {
359                                         for (int c = 0; c < 4; ++c) {
360                                                 sum_square += pow((*p++ - *q++), 2);
361                                         }
362                                 }
363                         }
364                         break;
365                 }
366                 case AV_PIX_FMT_RGB24:
367                 {
368                         for (int y = 0; y < height; ++y) {
369                                 uint8_t* p = ref_image->data()[0] + y * ref_image->stride()[0];
370                                 uint8_t* q = check_image->data()[0] + y * check_image->stride()[0];
371                                 for (int x = 0; x < width; ++x) {
372                                         for (int c = 0; c < 3; ++c) {
373                                                 sum_square += pow((*p++ - *q++), 2);
374                                         }
375                                 }
376                         }
377                         break;
378                 }
379                 case AV_PIX_FMT_RGB48BE:
380                 {
381                         for (int y = 0; y < height; ++y) {
382                                 uint16_t* p = reinterpret_cast<uint16_t*>(ref_image->data()[0] + y * ref_image->stride()[0]);
383                                 uint16_t* q = reinterpret_cast<uint16_t*>(check_image->data()[0] + y * check_image->stride()[0]);
384                                 for (int x = 0; x < width; ++x) {
385                                         for (int c = 0; c < 3; ++c) {
386                                                 sum_square += pow((*p++ - *q++), 2);
387                                         }
388                                 }
389                         }
390                         break;
391                 }
392                 default:
393                         BOOST_REQUIRE_MESSAGE (false, "unrecognised pixel format " << format);
394         }
395
396         return sqrt(sum_square / (height * width));
397 }
398
399
400 BOOST_AUTO_TEST_CASE (rms_error_test)
401 {
402         BOOST_CHECK_CLOSE (rms_error("test/data/check_image0.png", "test/data/check_image0.png"), 0, 0.001);
403         BOOST_CHECK_CLOSE (rms_error("test/data/check_image0.png", "test/data/check_image1.png"), 2.2778, 0.001);
404         BOOST_CHECK_CLOSE (rms_error("test/data/check_image0.png", "test/data/check_image2.png"), 59.8896, 0.001);
405         BOOST_CHECK_CLOSE (rms_error("test/data/check_image0.png", "test/data/check_image3.png"), 0.89164, 0.001);
406 }
407
408
409 void
410 check_image (boost::filesystem::path ref, boost::filesystem::path check, double threshold)
411 {
412         double const e = rms_error (ref, check);
413         BOOST_CHECK_MESSAGE (e < threshold, ref << " differs from " << check << " " << e);
414 }
415
416
417 void
418 check_file (boost::filesystem::path ref, boost::filesystem::path check)
419 {
420         uintmax_t N = boost::filesystem::file_size (ref);
421         BOOST_CHECK_EQUAL (N, boost::filesystem::file_size (check));
422         FILE* ref_file = fopen_boost (ref, "rb");
423         BOOST_CHECK (ref_file);
424         FILE* check_file = fopen_boost (check, "rb");
425         BOOST_CHECK (check_file);
426
427         int const buffer_size = 65536;
428         uint8_t* ref_buffer = new uint8_t[buffer_size];
429         uint8_t* check_buffer = new uint8_t[buffer_size];
430
431         string error = "File " + check.string() + " differs from reference " + ref.string();
432
433         while (N) {
434                 uintmax_t this_time = min (uintmax_t (buffer_size), N);
435                 size_t r = fread (ref_buffer, 1, this_time, ref_file);
436                 BOOST_CHECK_EQUAL (r, this_time);
437                 r = fread (check_buffer, 1, this_time, check_file);
438                 BOOST_CHECK_EQUAL (r, this_time);
439
440                 BOOST_CHECK_MESSAGE (memcmp (ref_buffer, check_buffer, this_time) == 0, error);
441                 if (memcmp (ref_buffer, check_buffer, this_time)) {
442                         break;
443                 }
444
445                 N -= this_time;
446         }
447
448         delete[] ref_buffer;
449         delete[] check_buffer;
450
451         fclose (ref_file);
452         fclose (check_file);
453 }
454
455 static void
456 note (dcp::NoteType t, string n)
457 {
458         if (t == dcp::DCP_ERROR) {
459                 cerr << n << "\n";
460         }
461 }
462
463
464 void
465 check_dcp (boost::filesystem::path ref, shared_ptr<const Film> film)
466 {
467         check_dcp (ref, film->dir(film->dcp_name()));
468 }
469
470
471 void
472 check_dcp (boost::filesystem::path ref, boost::filesystem::path check)
473 {
474         dcp::DCP ref_dcp (ref);
475         ref_dcp.read ();
476         dcp::DCP check_dcp (check);
477         check_dcp.read ();
478
479         dcp::EqualityOptions options;
480         options.max_mean_pixel_error = 5;
481         options.max_std_dev_pixel_error = 5;
482         options.max_audio_sample_error = 255;
483         options.cpl_annotation_texts_can_differ = true;
484         options.reel_annotation_texts_can_differ = true;
485         options.reel_hashes_can_differ = true;
486         options.issue_dates_can_differ = true;
487
488         BOOST_CHECK (ref_dcp.equals (check_dcp, options, boost::bind (note, _1, _2)));
489 }
490
491 void
492 check_xml (xmlpp::Element* ref, xmlpp::Element* test, list<string> ignore)
493 {
494         BOOST_CHECK_EQUAL (ref->get_name (), test->get_name ());
495         BOOST_CHECK_EQUAL (ref->get_namespace_prefix (), test->get_namespace_prefix ());
496
497         if (find (ignore.begin(), ignore.end(), ref->get_name()) != ignore.end ()) {
498                 return;
499         }
500
501         xmlpp::Element::NodeList ref_children = ref->get_children ();
502         xmlpp::Element::NodeList test_children = test->get_children ();
503         BOOST_REQUIRE_MESSAGE (
504                 ref_children.size() == test_children.size(),
505                 ref->get_name() << " has " << ref_children.size() << " or " << test_children.size() << " children"
506                 );
507
508         xmlpp::Element::NodeList::iterator k = ref_children.begin ();
509         xmlpp::Element::NodeList::iterator l = test_children.begin ();
510         while (k != ref_children.end ()) {
511
512                 /* XXX: should be doing xmlpp::EntityReference, xmlpp::XIncludeEnd, xmlpp::XIncludeStart */
513
514                 xmlpp::Element* ref_el = dynamic_cast<xmlpp::Element*> (*k);
515                 xmlpp::Element* test_el = dynamic_cast<xmlpp::Element*> (*l);
516                 BOOST_CHECK ((ref_el && test_el) || (!ref_el && !test_el));
517                 if (ref_el && test_el) {
518                         check_xml (ref_el, test_el, ignore);
519                 }
520
521                 xmlpp::ContentNode* ref_cn = dynamic_cast<xmlpp::ContentNode*> (*k);
522                 xmlpp::ContentNode* test_cn = dynamic_cast<xmlpp::ContentNode*> (*l);
523                 BOOST_CHECK ((ref_cn && test_cn) || (!ref_cn && !test_cn));
524                 if (ref_cn && test_cn) {
525                         BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content ());
526                 }
527
528                 ++k;
529                 ++l;
530         }
531
532         xmlpp::Element::AttributeList ref_attributes = ref->get_attributes ();
533         xmlpp::Element::AttributeList test_attributes = test->get_attributes ();
534         BOOST_CHECK_EQUAL (ref_attributes.size(), test_attributes.size ());
535
536         xmlpp::Element::AttributeList::const_iterator m = ref_attributes.begin();
537         xmlpp::Element::AttributeList::const_iterator n = test_attributes.begin();
538         while (m != ref_attributes.end ()) {
539                 BOOST_CHECK_EQUAL ((*m)->get_name(), (*n)->get_name());
540                 BOOST_CHECK_EQUAL ((*m)->get_value(), (*n)->get_value());
541
542                 ++m;
543                 ++n;
544         }
545 }
546
547 void
548 check_xml (boost::filesystem::path ref, boost::filesystem::path test, list<string> ignore)
549 {
550         xmlpp::DomParser* ref_parser = new xmlpp::DomParser (ref.string ());
551         xmlpp::Element* ref_root = ref_parser->get_document()->get_root_node ();
552         xmlpp::DomParser* test_parser = new xmlpp::DomParser (test.string ());
553         xmlpp::Element* test_root = test_parser->get_document()->get_root_node ();
554
555         check_xml (ref_root, test_root, ignore);
556 }
557
558 bool
559 wait_for_jobs ()
560 {
561         JobManager* jm = JobManager::instance ();
562         while (jm->work_to_do ()) {
563                 while (signal_manager->ui_idle ()) {}
564                 dcpomatic_sleep_seconds (1);
565         }
566
567         if (jm->errors ()) {
568                 int N = 0;
569                 for (list<shared_ptr<Job> >::iterator i = jm->_jobs.begin(); i != jm->_jobs.end(); ++i) {
570                         if ((*i)->finished_in_error ()) {
571                                 ++N;
572                         }
573                 }
574                 cerr << N << " errors.\n";
575
576                 for (list<shared_ptr<Job> >::iterator i = jm->_jobs.begin(); i != jm->_jobs.end(); ++i) {
577                         if ((*i)->finished_in_error ()) {
578                                 cerr << (*i)->name() << ":\n"
579                                      << "\tsummary: " << (*i)->error_summary () << "\n"
580                                      << "\tdetails: " << (*i)->error_details () << "\n";
581                         }
582                 }
583         }
584
585         while (signal_manager->ui_idle ()) {}
586
587         if (jm->errors ()) {
588                 JobManager::drop ();
589                 return true;
590         }
591
592         return false;
593 }
594
595
596 class Memory
597 {
598 public:
599         Memory ()
600                 : data(0)
601                 , size(0)
602         {}
603
604         ~Memory ()
605         {
606                 free (data);
607         }
608
609         uint8_t* data;
610         size_t size;
611 };
612
613
614 static void
615 png_write_data (png_structp png_ptr, png_bytep data, png_size_t length)
616 {
617         Memory* mem = reinterpret_cast<Memory*>(png_get_io_ptr(png_ptr));
618         size_t size = mem->size + length;
619
620         if (mem->data) {
621                 mem->data = reinterpret_cast<uint8_t*>(realloc(mem->data, size));
622         } else {
623                 mem->data = reinterpret_cast<uint8_t*>(malloc(size));
624         }
625
626         BOOST_REQUIRE (mem->data);
627
628         memcpy (mem->data + mem->size, data, length);
629         mem->size += length;
630 }
631
632
633 static void
634 png_flush (png_structp)
635 {
636
637 }
638
639
640 static void
641 png_error_fn (png_structp png_ptr, char const * message)
642 {
643         reinterpret_cast<Image*>(png_get_error_ptr(png_ptr))->png_error (message);
644 }
645
646
647 void
648 write_image (shared_ptr<const Image> image, boost::filesystem::path file)
649 {
650         int png_color_type = 0;
651         int bits_per_pixel = 0;
652         switch (image->pixel_format()) {
653         case AV_PIX_FMT_RGB24:
654                 png_color_type = PNG_COLOR_TYPE_RGB;
655                 bits_per_pixel = 8;
656                 break;
657         case AV_PIX_FMT_XYZ12LE:
658                 png_color_type = PNG_COLOR_TYPE_RGB;
659                 bits_per_pixel = 16;
660                 break;
661         default:
662                 BOOST_REQUIRE_MESSAGE (false, "unexpected pixel format " << image->pixel_format());
663         }
664
665         /* error handling? */
666         png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, reinterpret_cast<void*>(const_cast<Image*>(image.get())), png_error_fn, 0);
667         BOOST_REQUIRE (png_ptr);
668
669         Memory state;
670
671         png_set_write_fn (png_ptr, &state, png_write_data, png_flush);
672
673         png_infop info_ptr = png_create_info_struct(png_ptr);
674         BOOST_REQUIRE (info_ptr);
675
676         png_set_IHDR (png_ptr, info_ptr, image->size().width, image->size().height, bits_per_pixel, png_color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
677
678         png_byte ** row_pointers = reinterpret_cast<png_byte **>(png_malloc(png_ptr, image->size().height * sizeof(png_byte *)));
679         for (int i = 0; i < image->size().height; ++i) {
680                 row_pointers[i] = (png_byte *) (image->data()[0] + i * image->stride()[0]);
681         }
682
683         png_write_info (png_ptr, info_ptr);
684         png_write_image (png_ptr, row_pointers);
685         png_write_end (png_ptr, info_ptr);
686
687         png_destroy_write_struct (&png_ptr, &info_ptr);
688         png_free (png_ptr, row_pointers);
689
690         dcp::ArrayData(state.data, state.size).write(file);
691 }
692
693
694 void
695 check_ffmpeg (boost::filesystem::path ref, boost::filesystem::path check, int audio_tolerance)
696 {
697         int const r = system (String::compose("ffcmp -t %1 %2 %3", audio_tolerance, ref.string(), check.string()).c_str());
698         BOOST_REQUIRE_EQUAL (WEXITSTATUS(r), 0);
699 }
700
701 void
702 check_one_frame (boost::filesystem::path dcp_dir, int64_t index, boost::filesystem::path ref)
703 {
704         dcp::DCP dcp (dcp_dir);
705         dcp.read ();
706         shared_ptr<dcp::MonoPictureAsset> asset = dynamic_pointer_cast<dcp::MonoPictureAsset> (dcp.cpls().front()->reels().front()->main_picture()->asset());
707         BOOST_REQUIRE (asset);
708         shared_ptr<const dcp::MonoPictureFrame> frame = asset->start_read()->get_frame(index);
709         shared_ptr<const dcp::MonoPictureFrame> ref_frame (new dcp::MonoPictureFrame (ref));
710
711         shared_ptr<dcp::OpenJPEGImage> image = frame->xyz_image ();
712         shared_ptr<dcp::OpenJPEGImage> ref_image = ref_frame->xyz_image ();
713
714         BOOST_REQUIRE (image->size() == ref_image->size());
715
716         int off = 0;
717         for (int y = 0; y < ref_image->size().height; ++y) {
718                 for (int x = 0; x < ref_image->size().width; ++x) {
719                         BOOST_REQUIRE_EQUAL (ref_image->data(0)[off], image->data(0)[off]);
720                         BOOST_REQUIRE_EQUAL (ref_image->data(1)[off], image->data(1)[off]);
721                         BOOST_REQUIRE_EQUAL (ref_image->data(2)[off], image->data(2)[off]);
722                         ++off;
723                 }
724         }
725 }
726
727 boost::filesystem::path
728 dcp_file (shared_ptr<const Film> film, string prefix)
729 {
730         boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (film->dir(film->dcp_name()));
731         while (i != boost::filesystem::directory_iterator() && !boost::algorithm::starts_with (i->path().leaf().string(), prefix)) {
732                 ++i;
733         }
734
735         BOOST_REQUIRE (i != boost::filesystem::directory_iterator());
736         return i->path();
737 }
738
739 boost::filesystem::path
740 subtitle_file (shared_ptr<Film> film)
741 {
742         for (
743                 boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (film->directory().get() / film->dcp_name (false));
744                 i != boost::filesystem::directory_iterator ();
745                 ++i) {
746
747                 if (boost::filesystem::is_directory (i->path ())) {
748                         for (
749                                 boost::filesystem::directory_iterator j = boost::filesystem::directory_iterator (i->path ());
750                                 j != boost::filesystem::directory_iterator ();
751                                 ++j) {
752
753                                 if (boost::algorithm::starts_with (j->path().leaf().string(), "sub_")) {
754                                         return j->path();
755                                 }
756                         }
757                 }
758         }
759
760         BOOST_REQUIRE (false);
761         /* Remove warning */
762         return boost::filesystem::path("/");
763 }
764
765 void
766 make_random_file (boost::filesystem::path path, size_t size)
767 {
768         FILE* t = fopen_boost(path, "wb");
769         BOOST_REQUIRE (t);
770         for (size_t i = 0; i < size; ++i) {
771                 uint8_t r = rand() & 0xff;
772                 fwrite (&r, 1, 1, t);
773         }
774         fclose (t);
775 }
776
777
778 LogSwitcher::LogSwitcher (shared_ptr<Log> log)
779         : _old (dcpomatic_log)
780 {
781         dcpomatic_log = log;
782 }
783
784
785 LogSwitcher::~LogSwitcher ()
786 {
787         dcpomatic_log = _old;
788 }
789