Rename split-by-video content slightly; fix referencing to multi-reel DCPs.
[dcpomatic.git] / src / lib / writer.cc
1 /*
2     Copyright (C) 2012-2015 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 "writer.h"
21 #include "compose.hpp"
22 #include "film.h"
23 #include "ratio.h"
24 #include "log.h"
25 #include "dcp_video.h"
26 #include "dcp_content_type.h"
27 #include "audio_mapping.h"
28 #include "config.h"
29 #include "job.h"
30 #include "cross.h"
31 #include "audio_buffers.h"
32 #include "md5_digester.h"
33 #include "data.h"
34 #include "version.h"
35 #include "font.h"
36 #include "util.h"
37 #include "referenced_reel_asset.h"
38 #include <dcp/mono_picture_asset.h>
39 #include <dcp/stereo_picture_asset.h>
40 #include <dcp/sound_asset.h>
41 #include <dcp/sound_asset_writer.h>
42 #include <dcp/reel.h>
43 #include <dcp/reel_mono_picture_asset.h>
44 #include <dcp/reel_stereo_picture_asset.h>
45 #include <dcp/reel_sound_asset.h>
46 #include <dcp/reel_subtitle_asset.h>
47 #include <dcp/dcp.h>
48 #include <dcp/cpl.h>
49 #include <dcp/certificate_chain.h>
50 #include <dcp/interop_subtitle_asset.h>
51 #include <dcp/smpte_subtitle_asset.h>
52 #include <boost/foreach.hpp>
53 #include <fstream>
54 #include <cerrno>
55 #include <iostream>
56
57 #include "i18n.h"
58
59 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
60 #define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_GENERAL);
61 #define LOG_DEBUG_ENCODE(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_DEBUG_ENCODE);
62 #define LOG_TIMING(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_TIMING);
63 #define LOG_WARNING_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_WARNING);
64 #define LOG_WARNING(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_WARNING);
65 #define LOG_ERROR(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_ERROR);
66
67 /* OS X strikes again */
68 #undef set_key
69
70 using std::make_pair;
71 using std::pair;
72 using std::string;
73 using std::list;
74 using std::cout;
75 using boost::shared_ptr;
76 using boost::weak_ptr;
77 using boost::dynamic_pointer_cast;
78
79 int const Writer::_info_size = 48;
80
81 Writer::Writer (shared_ptr<const Film> film, weak_ptr<Job> j)
82         : _film (film)
83         , _job (j)
84         , _thread (0)
85         , _finish (false)
86         , _queued_full_in_memory (0)
87         , _last_written_frame (-1)
88         , _last_written_eyes (EYES_RIGHT)
89         , _maximum_frames_in_memory (0)
90         , _full_written (0)
91         , _fake_written (0)
92         , _repeat_written (0)
93         , _pushed_to_disk (0)
94 {
95         /* Remove any old DCP */
96         boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
97
98         shared_ptr<Job> job = _job.lock ();
99         DCPOMATIC_ASSERT (job);
100
101         BOOST_FOREACH (DCPTimePeriod p, _film->reels ()) {
102                 Reel reel;
103                 reel.period = p;
104
105                 /* Create our picture asset in a subdirectory, named according to those
106                    film's parameters which affect the video output.  We will hard-link
107                    it into the DCP later.
108                 */
109
110                 if (_film->three_d ()) {
111                         reel.picture_asset.reset (new dcp::StereoPictureAsset (dcp::Fraction (_film->video_frame_rate (), 1)));
112                 } else {
113                         reel.picture_asset.reset (new dcp::MonoPictureAsset (dcp::Fraction (_film->video_frame_rate (), 1)));
114                 }
115
116                 reel.picture_asset->set_size (_film->frame_size ());
117
118                 if (_film->encrypted ()) {
119                         reel.picture_asset->set_key (_film->key ());
120                 }
121
122                 reel.picture_asset->set_file (
123                         _film->internal_video_asset_dir() / _film->internal_video_asset_filename(p)
124                         );
125
126                 job->sub (_("Checking existing image data"));
127                 check_existing_picture_asset (reel);
128
129                 reel.picture_asset_writer = reel.picture_asset->start_write (
130                         _film->internal_video_asset_dir() / _film->internal_video_asset_filename(p),
131                         _film->interop() ? dcp::INTEROP : dcp::SMPTE,
132                         reel.first_nonexistant_frame > 0
133                         );
134
135                 if (_film->audio_channels ()) {
136                         reel.sound_asset.reset (
137                                 new dcp::SoundAsset (dcp::Fraction (_film->video_frame_rate(), 1), _film->audio_frame_rate (), _film->audio_channels ())
138                         );
139
140                         if (_film->encrypted ()) {
141                                 reel.sound_asset->set_key (_film->key ());
142                         }
143
144                         /* Write the sound asset into the film directory so that we leave the creation
145                            of the DCP directory until the last minute.
146                         */
147                         reel.sound_asset_writer = reel.sound_asset->start_write (
148                                 _film->directory() / audio_asset_filename (reel.sound_asset),
149                                 _film->interop() ? dcp::INTEROP : dcp::SMPTE
150                                 );
151                 }
152
153                 _reels.push_back (reel);
154         }
155
156         /* We can keep track of the current audio and subtitle reels easily because audio
157            and subs arrive to the Writer in sequence.  This is not so for video.
158         */
159         _audio_reel = _reels.begin ();
160         _subtitle_reel = _reels.begin ();
161
162         /* Check that the signer is OK if we need one */
163         if (_film->is_signed() && !Config::instance()->signer_chain()->valid ()) {
164                 throw InvalidSignerError ();
165         }
166
167         job->sub (_("Encoding image data"));
168 }
169
170 void
171 Writer::start ()
172 {
173         _thread = new boost::thread (boost::bind (&Writer::thread, this));
174 }
175
176 Writer::~Writer ()
177 {
178         terminate_thread (false);
179 }
180
181 void
182 Writer::write (Data encoded, int frame, Eyes eyes)
183 {
184         boost::mutex::scoped_lock lock (_state_mutex);
185
186         while (_queued_full_in_memory > _maximum_frames_in_memory) {
187                 /* The queue is too big; wait until that is sorted out */
188                 _full_condition.wait (lock);
189         }
190
191         QueueItem qi;
192         qi.type = QueueItem::FULL;
193         qi.encoded = encoded;
194         qi.frame = frame;
195
196         if (_film->three_d() && eyes == EYES_BOTH) {
197                 /* 2D material in a 3D DCP; fake the 3D */
198                 qi.eyes = EYES_LEFT;
199                 _queue.push_back (qi);
200                 ++_queued_full_in_memory;
201                 qi.eyes = EYES_RIGHT;
202                 _queue.push_back (qi);
203                 ++_queued_full_in_memory;
204         } else {
205                 qi.eyes = eyes;
206                 _queue.push_back (qi);
207                 ++_queued_full_in_memory;
208         }
209
210         /* Now there's something to do: wake anything wait()ing on _empty_condition */
211         _empty_condition.notify_all ();
212 }
213
214 void
215 Writer::repeat (int frame, Eyes eyes)
216 {
217         boost::mutex::scoped_lock lock (_state_mutex);
218
219         while (_queued_full_in_memory > _maximum_frames_in_memory) {
220                 /* The queue is too big; wait until that is sorted out */
221                 _full_condition.wait (lock);
222         }
223
224         QueueItem qi;
225         qi.type = QueueItem::REPEAT;
226         qi.frame = frame;
227         if (_film->three_d() && eyes == EYES_BOTH) {
228                 qi.eyes = EYES_LEFT;
229                 _queue.push_back (qi);
230                 qi.eyes = EYES_RIGHT;
231                 _queue.push_back (qi);
232         } else {
233                 qi.eyes = eyes;
234                 _queue.push_back (qi);
235         }
236
237         /* Now there's something to do: wake anything wait()ing on _empty_condition */
238         _empty_condition.notify_all ();
239 }
240
241 void
242 Writer::fake_write (int frame, Eyes eyes)
243 {
244         boost::mutex::scoped_lock lock (_state_mutex);
245
246         while (_queued_full_in_memory > _maximum_frames_in_memory) {
247                 /* The queue is too big; wait until that is sorted out */
248                 _full_condition.wait (lock);
249         }
250
251         Reel const & reel = video_reel (frame);
252
253         FILE* file = fopen_boost (_film->info_file(reel.period), "rb");
254         if (!file) {
255                 throw ReadFileError (_film->info_file(reel.period));
256         }
257         dcp::FrameInfo info = read_frame_info (file, frame, eyes);
258         fclose (file);
259
260         QueueItem qi;
261         qi.type = QueueItem::FAKE;
262         qi.size = info.size;
263         qi.frame = frame;
264         if (_film->three_d() && eyes == EYES_BOTH) {
265                 qi.eyes = EYES_LEFT;
266                 _queue.push_back (qi);
267                 qi.eyes = EYES_RIGHT;
268                 _queue.push_back (qi);
269         } else {
270                 qi.eyes = eyes;
271                 _queue.push_back (qi);
272         }
273
274         /* Now there's something to do: wake anything wait()ing on _empty_condition */
275         _empty_condition.notify_all ();
276 }
277
278 /** This method is not thread safe */
279 void
280 Writer::write (shared_ptr<const AudioBuffers> audio)
281 {
282         if (!_audio_reel->sound_asset_writer) {
283                 return;
284         }
285
286         _audio_reel->sound_asset_writer->write (audio->data(), audio->frames());
287
288         /* SoundAsset's `frames' are video frames, not audio frames */
289         if (_audio_reel->sound_asset_writer->frames_written() >= _audio_reel->period.duration().frames_round (_film->video_frame_rate())) {
290                 ++_audio_reel;
291         }
292 }
293
294 /** This must be called from Writer::thread() with an appropriate lock held */
295 bool
296 Writer::have_sequenced_image_at_queue_head ()
297 {
298         if (_queue.empty ()) {
299                 return false;
300         }
301
302         _queue.sort ();
303
304         /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
305
306         if (_queue.front().eyes == EYES_BOTH) {
307                 /* 2D */
308                 return _queue.front().frame == (_last_written_frame + 1);
309         }
310
311         /* 3D */
312
313         if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
314                 return true;
315         }
316
317         if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
318                 return true;
319         }
320
321         return false;
322 }
323
324 void
325 Writer::write_frame_info (int frame, Eyes eyes, dcp::FrameInfo info) const
326 {
327         Reel const & reel = video_reel (frame);
328         FILE* file = 0;
329         boost::filesystem::path info_file = _film->info_file (reel.period);
330         if (boost::filesystem::exists (info_file)) {
331                 file = fopen_boost (info_file, "r+b");
332         } else {
333                 file = fopen_boost (info_file, "wb");
334         }
335         if (!file) {
336                 throw OpenFileError (info_file);
337         }
338         dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
339         fwrite (&info.offset, sizeof (info.offset), 1, file);
340         fwrite (&info.size, sizeof (info.size), 1, file);
341         fwrite (info.hash.c_str(), 1, info.hash.size(), file);
342         fclose (file);
343 }
344
345 void
346 Writer::thread ()
347 try
348 {
349         while (true)
350         {
351                 boost::mutex::scoped_lock lock (_state_mutex);
352
353                 while (true) {
354
355                         if (_finish || _queued_full_in_memory > _maximum_frames_in_memory || have_sequenced_image_at_queue_head ()) {
356                                 /* We've got something to do: go and do it */
357                                 break;
358                         }
359
360                         /* Nothing to do: wait until something happens which may indicate that we do */
361                         LOG_TIMING (N_("writer-sleep queue=%1"), _queue.size());
362                         _empty_condition.wait (lock);
363                         LOG_TIMING (N_("writer-wake queue=%1"), _queue.size());
364                 }
365
366                 if (_finish && _queue.empty()) {
367                         return;
368                 }
369
370                 /* We stop here if we have been asked to finish, and if either the queue
371                    is empty or we do not have a sequenced image at its head (if this is the
372                    case we will never terminate as no new frames will be sent once
373                    _finish is true).
374                 */
375                 if (_finish && (!have_sequenced_image_at_queue_head() || _queue.empty())) {
376                         /* (Hopefully temporarily) log anything that was not written */
377                         if (!_queue.empty() && !have_sequenced_image_at_queue_head()) {
378                                 LOG_WARNING (N_("Finishing writer with a left-over queue of %1:"), _queue.size());
379                                 for (list<QueueItem>::const_iterator i = _queue.begin(); i != _queue.end(); ++i) {
380                                         if (i->type == QueueItem::FULL) {
381                                                 LOG_WARNING (N_("- type FULL, frame %1, eyes %2"), i->frame, i->eyes);
382                                         } else {
383                                                 LOG_WARNING (N_("- type FAKE, size %1, frame %2, eyes %3"), i->size, i->frame, i->eyes);
384                                         }
385                                 }
386                                 LOG_WARNING (N_("Last written frame %1, last written eyes %2"), _last_written_frame, _last_written_eyes);
387                         }
388                         return;
389                 }
390                 /* Write any frames that we can write; i.e. those that are in sequence. */
391                 while (have_sequenced_image_at_queue_head ()) {
392                         QueueItem qi = _queue.front ();
393                         _queue.pop_front ();
394                         if (qi.type == QueueItem::FULL && qi.encoded) {
395                                 --_queued_full_in_memory;
396                         }
397
398                         lock.unlock ();
399
400                         Reel const & reel = video_reel (qi.frame);
401
402                         switch (qi.type) {
403                         case QueueItem::FULL:
404                         {
405                                 LOG_DEBUG_ENCODE (N_("Writer FULL-writes %1 (%2)"), qi.frame, qi.eyes);
406                                 if (!qi.encoded) {
407                                         qi.encoded = Data (_film->j2c_path (qi.frame, qi.eyes, false));
408                                 }
409
410                                 dcp::FrameInfo fin = reel.picture_asset_writer->write (qi.encoded->data().get (), qi.encoded->size());
411                                 write_frame_info (qi.frame, qi.eyes, fin);
412                                 _last_written[qi.eyes] = qi.encoded;
413                                 ++_full_written;
414                                 break;
415                         }
416                         case QueueItem::FAKE:
417                                 LOG_DEBUG_ENCODE (N_("Writer FAKE-writes %1"), qi.frame);
418                                 reel.picture_asset_writer->fake_write (qi.size);
419                                 _last_written[qi.eyes].reset ();
420                                 ++_fake_written;
421                                 break;
422                         case QueueItem::REPEAT:
423                                 LOG_DEBUG_ENCODE (N_("Writer REPEAT-writes %1"), qi.frame);
424                                 dcp::FrameInfo fin = reel.picture_asset_writer->write (
425                                         _last_written[qi.eyes]->data().get(),
426                                         _last_written[qi.eyes]->size()
427                                         );
428                                 write_frame_info (qi.frame, qi.eyes, fin);
429                                 ++_repeat_written;
430                                 break;
431                         }
432
433                         lock.lock ();
434
435                         _last_written_frame = qi.frame;
436                         _last_written_eyes = qi.eyes;
437
438                         shared_ptr<Job> job = _job.lock ();
439                         DCPOMATIC_ASSERT (job);
440                         int64_t total = _film->length().frames_round (_film->video_frame_rate ());
441                         if (_film->three_d ()) {
442                                 /* _full_written and so on are incremented for each eye, so we need to double the total
443                                    frames to get the correct progress.
444                                 */
445                                 total *= 2;
446                         }
447                         if (total) {
448                                 job->set_progress (float (_full_written + _fake_written + _repeat_written) / total);
449                         }
450                 }
451
452                 while (_queued_full_in_memory > _maximum_frames_in_memory) {
453                         /* Too many frames in memory which can't yet be written to the stream.
454                            Write some FULL frames to disk.
455                         */
456
457                         /* Find one from the back of the queue */
458                         _queue.sort ();
459                         list<QueueItem>::reverse_iterator i = _queue.rbegin ();
460                         while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
461                                 ++i;
462                         }
463
464                         DCPOMATIC_ASSERT (i != _queue.rend());
465                         ++_pushed_to_disk;
466                         lock.unlock ();
467
468                         /* i is valid here, even though we don't hold a lock on the mutex,
469                            since list iterators are unaffected by insertion and only this
470                            thread could erase the last item in the list.
471                         */
472
473                         LOG_GENERAL (
474                                 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
475                                 _last_written_frame + 1,
476                                 _last_written_eyes, i->frame
477                                 );
478
479                         i->encoded->write_via_temp (_film->j2c_path (i->frame, i->eyes, true), _film->j2c_path (i->frame, i->eyes, false));
480
481                         lock.lock ();
482                         i->encoded.reset ();
483                         --_queued_full_in_memory;
484                 }
485
486                 /* The queue has probably just gone down a bit; notify anything wait()ing on _full_condition */
487                 _full_condition.notify_all ();
488         }
489 }
490 catch (...)
491 {
492         store_current ();
493 }
494
495 void
496 Writer::terminate_thread (bool can_throw)
497 {
498         boost::mutex::scoped_lock lock (_state_mutex);
499         if (_thread == 0) {
500                 return;
501         }
502
503         _finish = true;
504         _empty_condition.notify_all ();
505         _full_condition.notify_all ();
506         lock.unlock ();
507
508         DCPOMATIC_ASSERT (_thread->joinable ());
509         _thread->join ();
510         if (can_throw) {
511                 rethrow ();
512         }
513
514         delete _thread;
515         _thread = 0;
516 }
517
518 void
519 Writer::finish ()
520 {
521         if (!_thread) {
522                 return;
523         }
524
525         terminate_thread (true);
526
527         BOOST_FOREACH (Reel& i, _reels) {
528
529                 if (!i.picture_asset_writer->finalize ()) {
530                 /* Nothing was written to the picture asset */
531                         i.picture_asset.reset ();
532                 }
533
534                 if (i.sound_asset_writer && !i.sound_asset_writer->finalize ()) {
535                         /* Nothing was written to the sound asset */
536                         i.sound_asset.reset ();
537                 }
538
539                 /* Hard-link any video asset file into the DCP */
540                 if (i.picture_asset) {
541                         boost::filesystem::path video_from = i.picture_asset->file ();
542                         boost::filesystem::path video_to;
543                         video_to /= _film->dir (_film->dcp_name());
544                         video_to /= video_asset_filename (i.picture_asset);
545
546                         boost::system::error_code ec;
547                         boost::filesystem::create_hard_link (video_from, video_to, ec);
548                         if (ec) {
549                                 LOG_WARNING_NC ("Hard-link failed; copying instead");
550                                 boost::filesystem::copy_file (video_from, video_to, ec);
551                                 if (ec) {
552                                         LOG_ERROR ("Failed to copy video file from %1 to %2 (%3)", video_from.string(), video_to.string(), ec.message ());
553                                         throw FileError (ec.message(), video_from);
554                                 }
555                         }
556
557                         i.picture_asset->set_file (video_to);
558                 }
559
560                 /* Move the audio asset into the DCP */
561                 if (i.sound_asset) {
562                         boost::filesystem::path audio_to;
563                         audio_to /= _film->dir (_film->dcp_name ());
564                         audio_to /= audio_asset_filename (i.sound_asset);
565
566                         boost::system::error_code ec;
567                         boost::filesystem::rename (_film->file (audio_asset_filename (i.sound_asset)), audio_to, ec);
568                         if (ec) {
569                                 throw FileError (
570                                         String::compose (_("could not move audio asset into the DCP (%1)"), ec.value ()), audio_asset_filename (i.sound_asset)
571                                         );
572                         }
573
574                         i.sound_asset->set_file (audio_to);
575                 }
576         }
577
578         dcp::DCP dcp (_film->dir (_film->dcp_name()));
579
580         shared_ptr<dcp::CPL> cpl (
581                 new dcp::CPL (
582                         _film->dcp_name(),
583                         _film->dcp_content_type()->libdcp_kind ()
584                         )
585                 );
586
587         dcp.add (cpl);
588
589         BOOST_FOREACH (Reel& i, _reels) {
590                 shared_ptr<dcp::Reel> reel (new dcp::Reel ());
591
592                 shared_ptr<dcp::ReelPictureAsset> reel_picture_asset;
593
594                 if (i.picture_asset) {
595                         /* We have made a picture asset of our own.  Put it into the reel */
596                         shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (i.picture_asset);
597                         if (mono) {
598                                 reel_picture_asset.reset (new dcp::ReelMonoPictureAsset (mono, 0));
599                         }
600
601                         shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (i.picture_asset);
602                         if (stereo) {
603                                 reel_picture_asset.reset (new dcp::ReelStereoPictureAsset (stereo, 0));
604                         }
605                 } else {
606                         /* We don't have a picture asset of our own; hopefully we have one to reference */
607                         BOOST_FOREACH (ReferencedReelAsset j, _reel_assets) {
608                                 shared_ptr<dcp::ReelPictureAsset> k = dynamic_pointer_cast<dcp::ReelPictureAsset> (j.asset);
609                                 if (k && j.period == i.period) {
610                                         reel_picture_asset = k;
611                                 }
612                         }
613                 }
614
615                 reel->add (reel_picture_asset);
616
617                 if (i.sound_asset) {
618                         /* We have made a sound asset of our own.  Put it into the reel */
619                         reel->add (shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (i.sound_asset, 0)));
620                 } else {
621                         /* We don't have a sound asset of our own; hopefully we have one to reference */
622                         BOOST_FOREACH (ReferencedReelAsset j, _reel_assets) {
623                                 shared_ptr<dcp::ReelSoundAsset> k = dynamic_pointer_cast<dcp::ReelSoundAsset> (j.asset);
624                                 if (k && j.period == i.period) {
625                                         reel->add (k);
626                                 }
627                         }
628                 }
629
630                 if (i.subtitle_asset) {
631                         boost::filesystem::path liberation;
632                         try {
633                                 liberation = shared_path () / "LiberationSans-Regular.ttf";
634                         } catch (boost::filesystem::filesystem_error& e) {
635                                 /* Hack: try the debian/ubuntu location if getting the shared path failed */
636                                 liberation = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf";
637                         }
638
639                         /* Add all the fonts to the subtitle content */
640                         BOOST_FOREACH (shared_ptr<Font> j, _fonts) {
641                                 i.subtitle_asset->add_font (j->id(), j->file().get_value_or (liberation));
642                         }
643
644                         if (dynamic_pointer_cast<dcp::InteropSubtitleAsset> (i.subtitle_asset)) {
645                                 boost::filesystem::path directory = _film->dir (_film->dcp_name ()) / i.subtitle_asset->id ();
646                                 boost::filesystem::create_directories (directory);
647                                 i.subtitle_asset->write (directory / ("sub_" + i.subtitle_asset->id() + ".xml"));
648                         } else {
649
650                                 /* All our assets should be the same length; use the picture asset length here
651                                    as a reference to set the subtitle one.
652                                 */
653                                 dynamic_pointer_cast<dcp::SMPTESubtitleAsset>(i.subtitle_asset)->set_intrinsic_duration (
654                                         reel_picture_asset->intrinsic_duration ()
655                                         );
656
657                                 i.subtitle_asset->write (
658                                         _film->dir (_film->dcp_name ()) / ("sub_" + i.subtitle_asset->id() + ".mxf")
659                                         );
660                         }
661
662                         reel->add (shared_ptr<dcp::ReelSubtitleAsset> (
663                                            new dcp::ReelSubtitleAsset (
664                                                    i.subtitle_asset,
665                                                    dcp::Fraction (_film->video_frame_rate(), 1),
666                                                    reel_picture_asset->intrinsic_duration (),
667                                                    0
668                                                    )
669                                            ));
670                 } else {
671                         /* We don't have a subtitle asset of our own; hopefully we have one to reference */
672                         BOOST_FOREACH (ReferencedReelAsset j, _reel_assets) {
673                                 shared_ptr<dcp::ReelSubtitleAsset> k = dynamic_pointer_cast<dcp::ReelSubtitleAsset> (j.asset);
674                                 if (k && j.period == i.period) {
675                                         reel->add (k);
676                                 }
677                         }
678                 }
679
680                 cpl->add (reel);
681
682                 shared_ptr<Job> job = _job.lock ();
683                 DCPOMATIC_ASSERT (job);
684
685                 job->sub (_("Computing image digest"));
686                 if (i.picture_asset) {
687                         i.picture_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
688                 }
689
690                 if (i.sound_asset) {
691                         job->sub (_("Computing audio digest"));
692                         i.sound_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
693                 }
694         }
695
696         dcp::XMLMetadata meta;
697         meta.creator = Config::instance()->dcp_creator ();
698         if (meta.creator.empty ()) {
699                 meta.creator = String::compose ("DCP-o-matic %1 %2", dcpomatic_version, dcpomatic_git_commit);
700         }
701         meta.issuer = Config::instance()->dcp_issuer ();
702         if (meta.issuer.empty ()) {
703                 meta.issuer = String::compose ("DCP-o-matic %1 %2", dcpomatic_version, dcpomatic_git_commit);
704         }
705         meta.set_issue_date_now ();
706
707         cpl->set_metadata (meta);
708
709         shared_ptr<const dcp::CertificateChain> signer;
710         if (_film->is_signed ()) {
711                 signer = Config::instance()->signer_chain ();
712                 /* We did check earlier, but check again here to be on the safe side */
713                 if (!signer->valid ()) {
714                         throw InvalidSignerError ();
715                 }
716         }
717
718         dcp.write_xml (_film->interop () ? dcp::INTEROP : dcp::SMPTE, meta, signer);
719
720         LOG_GENERAL (
721                 N_("Wrote %1 FULL, %2 FAKE, %3 REPEAT, %4 pushed to disk"), _full_written, _fake_written, _repeat_written, _pushed_to_disk
722                 );
723 }
724
725 void
726 Writer::check_existing_picture_asset (Reel& reel)
727 {
728         /* Try to open the existing asset */
729         FILE* asset_file = fopen_boost (reel.picture_asset->file(), "rb");
730         if (!asset_file) {
731                 LOG_GENERAL ("Could not open existing asset at %1 (errno=%2)", reel.picture_asset->file().string(), errno);
732                 return;
733         }
734
735         /* Offset of the last dcp::FrameInfo in the info file */
736         int const n = (boost::filesystem::file_size (_film->info_file(reel.period)) / _info_size) - 1;
737
738         FILE* info_file = fopen_boost (_film->info_file(reel.period), "rb");
739         if (!info_file) {
740                 LOG_GENERAL_NC ("Could not open film info file");
741                 fclose (asset_file);
742                 return;
743         }
744
745         if (_film->three_d ()) {
746                 /* Start looking at the last left frame */
747                 reel.first_nonexistant_frame = n / 2;
748         } else {
749                 reel.first_nonexistant_frame = n;
750         }
751
752         bool ok = false;
753
754         while (!ok) {
755                 /* Read the data from the info file; for 3D we just check the left
756                    frames until we find a good one.
757                 */
758                 dcp::FrameInfo info = read_frame_info (info_file, reel.first_nonexistant_frame, _film->three_d () ? EYES_LEFT : EYES_BOTH);
759
760                 ok = true;
761
762                 /* Read the data from the asset and hash it */
763                 dcpomatic_fseek (asset_file, info.offset, SEEK_SET);
764                 Data data (info.size);
765                 size_t const read = fread (data.data().get(), 1, data.size(), asset_file);
766                 if (read != static_cast<size_t> (data.size ())) {
767                         LOG_GENERAL ("Existing frame %1 is incomplete", reel.first_nonexistant_frame);
768                         ok = false;
769                 } else {
770                         MD5Digester digester;
771                         digester.add (data.data().get(), data.size());
772                         if (digester.get() != info.hash) {
773                                 LOG_GENERAL ("Existing frame %1 failed hash check", reel.first_nonexistant_frame);
774                                 ok = false;
775                         }
776                 }
777
778                 if (!ok) {
779                         --reel.first_nonexistant_frame;
780                 }
781         }
782
783         if (!_film->three_d ()) {
784                 /* If we are doing 3D we might have found a good L frame with no R, so only
785                    do this if we're in 2D and we've just found a good B(oth) frame.
786                 */
787                 ++reel.first_nonexistant_frame;
788         }
789
790         fclose (asset_file);
791         fclose (info_file);
792 }
793
794 /** @param frame Frame index.
795  *  @return true if we can fake-write this frame.
796  */
797 bool
798 Writer::can_fake_write (int frame) const
799 {
800         /* We have to do a proper write of the first frame so that we can set up the JPEG2000
801            parameters in the asset writer.
802         */
803
804         /* XXX: need to correct frame to be relative to the reel, perhaps?
805            and clarify whether first_nonexistant_frame is relative to the start
806            of the DCP or of the reel.
807         */
808
809         return (frame != 0 && frame < video_reel(frame).first_nonexistant_frame);
810 }
811
812 void
813 Writer::write (PlayerSubtitles subs)
814 {
815         if (subs.text.empty ()) {
816                 return;
817         }
818
819         if (_subtitle_reel->period.to < subs.from) {
820                 ++_subtitle_reel;
821         }
822
823         if (!_subtitle_reel->subtitle_asset) {
824                 string lang = _film->subtitle_language ();
825                 if (lang.empty ()) {
826                         lang = "Unknown";
827                 }
828                 if (_film->interop ()) {
829                         shared_ptr<dcp::InteropSubtitleAsset> s (new dcp::InteropSubtitleAsset ());
830                         s->set_movie_title (_film->name ());
831                         s->set_language (lang);
832                         s->set_reel_number ("1");
833                         _subtitle_reel->subtitle_asset = s;
834                 } else {
835                         shared_ptr<dcp::SMPTESubtitleAsset> s (new dcp::SMPTESubtitleAsset ());
836                         s->set_content_title_text (_film->name ());
837                         s->set_language (lang);
838                         s->set_edit_rate (dcp::Fraction (_film->video_frame_rate (), 1));
839                         s->set_reel_number (1);
840                         s->set_time_code_rate (_film->video_frame_rate ());
841                         s->set_start_time (dcp::Time ());
842                         _subtitle_reel->subtitle_asset = s;
843                 }
844         }
845
846         for (list<dcp::SubtitleString>::const_iterator i = subs.text.begin(); i != subs.text.end(); ++i) {
847                 _subtitle_reel->subtitle_asset->add (*i);
848         }
849 }
850
851 void
852 Writer::write (list<shared_ptr<Font> > fonts)
853 {
854         /* Just keep a list of fonts and we'll deal with them in ::finish */
855         copy (fonts.begin (), fonts.end (), back_inserter (_fonts));
856 }
857
858 bool
859 operator< (QueueItem const & a, QueueItem const & b)
860 {
861         if (a.frame != b.frame) {
862                 return a.frame < b.frame;
863         }
864
865         return static_cast<int> (a.eyes) < static_cast<int> (b.eyes);
866 }
867
868 bool
869 operator== (QueueItem const & a, QueueItem const & b)
870 {
871         return a.frame == b.frame && a.eyes == b.eyes;
872 }
873
874 void
875 Writer::set_encoder_threads (int threads)
876 {
877         _maximum_frames_in_memory = lrint (threads * 1.1);
878 }
879
880 long
881 Writer::frame_info_position (int frame, Eyes eyes) const
882 {
883         switch (eyes) {
884         case EYES_BOTH:
885                 return frame * _info_size;
886         case EYES_LEFT:
887                 return frame * _info_size * 2;
888         case EYES_RIGHT:
889                 return frame * _info_size * 2 + _info_size;
890         default:
891                 DCPOMATIC_ASSERT (false);
892         }
893
894         DCPOMATIC_ASSERT (false);
895 }
896
897 dcp::FrameInfo
898 Writer::read_frame_info (FILE* file, int frame, Eyes eyes) const
899 {
900         dcp::FrameInfo info;
901         dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
902         fread (&info.offset, sizeof (info.offset), 1, file);
903         fread (&info.size, sizeof (info.size), 1, file);
904
905         char hash_buffer[33];
906         fread (hash_buffer, 1, 32, file);
907         hash_buffer[32] = '\0';
908         info.hash = hash_buffer;
909
910         return info;
911 }
912
913 void
914 Writer::write (ReferencedReelAsset asset)
915 {
916         _reel_assets.push_back (asset);
917 }
918
919 Writer::Reel const &
920 Writer::video_reel (int frame) const
921 {
922         DCPTime t = DCPTime::from_frames (frame, _film->video_frame_rate ());
923         list<Reel>::const_iterator i = _reels.begin ();
924         while (i != _reels.end() && !i->period.contains (t)) {
925                 ++i;
926         }
927
928         DCPOMATIC_ASSERT (i != _reels.end ());
929         return *i;
930 }