11b5bf6229b8713dffde89a777cd5181335136f6 from master; default colour conversions...
[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 "encoded_data.h"
34 #include "version.h"
35 #include "font.h"
36 #include "util.h"
37 #include <dcp/mono_picture_mxf.h>
38 #include <dcp/stereo_picture_mxf.h>
39 #include <dcp/sound_mxf.h>
40 #include <dcp/sound_mxf_writer.h>
41 #include <dcp/reel.h>
42 #include <dcp/reel_mono_picture_asset.h>
43 #include <dcp/reel_stereo_picture_asset.h>
44 #include <dcp/reel_sound_asset.h>
45 #include <dcp/reel_subtitle_asset.h>
46 #include <dcp/dcp.h>
47 #include <dcp/cpl.h>
48 #include <dcp/signer.h>
49 #include <dcp/interop_subtitle_content.h>
50 #include <dcp/font.h>
51 #include <boost/foreach.hpp>
52 #include <fstream>
53 #include <cerrno>
54
55 #include "i18n.h"
56
57 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
58 #define LOG_TIMING(...) _film->log()->microsecond_log (String::compose (__VA_ARGS__), Log::TYPE_TIMING);
59 #define LOG_WARNING_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_WARNING);
60 #define LOG_WARNING(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_WARNING);
61 #define LOG_ERROR(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_ERROR);
62 #define LOG_DEBUG(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_DEBUG);
63 #define LOG_DEBUG_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_DEBUG);
64
65 /* OS X strikes again */
66 #undef set_key
67
68 using std::make_pair;
69 using std::pair;
70 using std::string;
71 using std::list;
72 using std::cout;
73 using boost::shared_ptr;
74 using boost::weak_ptr;
75 using boost::dynamic_pointer_cast;
76
77 Writer::Writer (shared_ptr<const Film> f, weak_ptr<Job> j)
78         : _film (f)
79         , _job (j)
80         , _first_nonexistant_frame (0)
81         , _thread (0)
82         , _finish (false)
83         , _queued_full_in_memory (0)
84         , _last_written_frame (-1)
85         , _last_written_eyes (EYES_RIGHT)
86         , _full_written (0)
87         , _fake_written (0)
88         , _pushed_to_disk (0)
89 {
90         /* Remove any old DCP */
91         boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
92
93         shared_ptr<Job> job = _job.lock ();
94         DCPOMATIC_ASSERT (job);
95
96         /* Create our picture asset in a subdirectory, named according to those
97            film's parameters which affect the video output.  We will hard-link
98            it into the DCP later.
99         */
100
101         if (_film->three_d ()) {
102                 _picture_mxf.reset (new dcp::StereoPictureMXF (dcp::Fraction (_film->video_frame_rate (), 1)));
103         } else {
104                 _picture_mxf.reset (new dcp::MonoPictureMXF (dcp::Fraction (_film->video_frame_rate (), 1)));
105         }
106
107         _picture_mxf->set_size (_film->frame_size ());
108
109         if (_film->encrypted ()) {
110                 _picture_mxf->set_key (_film->key ());
111         }
112         
113         _picture_mxf_writer = _picture_mxf->start_write (
114                 _film->internal_video_mxf_dir() / _film->internal_video_mxf_filename(),
115                 _film->interop() ? dcp::INTEROP : dcp::SMPTE,
116                 _first_nonexistant_frame > 0
117                 );
118
119         if (_film->audio_channels ()) {
120                 _sound_mxf.reset (new dcp::SoundMXF (dcp::Fraction (_film->video_frame_rate(), 1), _film->audio_frame_rate (), _film->audio_channels ()));
121
122                 if (_film->encrypted ()) {
123                         _sound_mxf->set_key (_film->key ());
124                 }
125         
126                 /* Write the sound MXF into the film directory so that we leave the creation
127                    of the DCP directory until the last minute.
128                 */
129                 _sound_mxf_writer = _sound_mxf->start_write (_film->directory() / audio_mxf_filename (_sound_mxf), _film->interop() ? dcp::INTEROP : dcp::SMPTE);
130         }
131
132         /* Check that the signer is OK if we need one */
133         if (_film->is_signed() && !Config::instance()->signer()->valid ()) {
134                 throw InvalidSignerError ();
135         }
136
137         job->sub (_("Checking existing image data"));
138         check_existing_picture_mxf ();
139         
140         _thread = new boost::thread (boost::bind (&Writer::thread, this));
141
142         job->sub (_("Encoding image data"));
143 }
144
145 Writer::~Writer ()
146 {
147         terminate_thread (false);
148 }
149
150 void
151 Writer::write (shared_ptr<const EncodedData> encoded, int frame, Eyes eyes)
152 {
153         boost::mutex::scoped_lock lock (_mutex);
154
155         while (_queued_full_in_memory > maximum_frames_in_memory ()) {
156                 /* The queue is too big; wait until that is sorted out */
157                 _full_condition.wait (lock);
158         }
159
160         QueueItem qi;
161         qi.type = QueueItem::FULL;
162         qi.encoded = encoded;
163         qi.frame = frame;
164
165         if (_film->three_d() && eyes == EYES_BOTH) {
166                 /* 2D material in a 3D DCP; fake the 3D */
167                 qi.eyes = EYES_LEFT;
168                 _queue.push_back (qi);
169                 ++_queued_full_in_memory;
170                 qi.eyes = EYES_RIGHT;
171                 _queue.push_back (qi);
172                 ++_queued_full_in_memory;
173         } else {
174                 qi.eyes = eyes;
175                 _queue.push_back (qi);
176                 ++_queued_full_in_memory;
177         }
178
179         /* Now there's something to do: wake anything wait()ing on _empty_condition */
180         _empty_condition.notify_all ();
181 }
182
183 void
184 Writer::fake_write (int frame, Eyes eyes)
185 {
186         boost::mutex::scoped_lock lock (_mutex);
187
188         while (_queued_full_in_memory > maximum_frames_in_memory ()) {
189                 /* The queue is too big; wait until that is sorted out */
190                 _full_condition.wait (lock);
191         }
192         
193         FILE* file = fopen_boost (_film->info_file (), "rb");
194         if (!file) {
195                 throw ReadFileError (_film->info_file ());
196         }
197         dcp::FrameInfo info = read_frame_info (file, frame, eyes);
198         fclose (file);
199         
200         QueueItem qi;
201         qi.type = QueueItem::FAKE;
202         qi.size = info.size;
203         qi.frame = frame;
204         if (_film->three_d() && eyes == EYES_BOTH) {
205                 qi.eyes = EYES_LEFT;
206                 _queue.push_back (qi);
207                 qi.eyes = EYES_RIGHT;
208                 _queue.push_back (qi);
209         } else {
210                 qi.eyes = eyes;
211                 _queue.push_back (qi);
212         }
213
214         /* Now there's something to do: wake anything wait()ing on _empty_condition */
215         _empty_condition.notify_all ();
216 }
217
218 /** This method is not thread safe */
219 void
220 Writer::write (shared_ptr<const AudioBuffers> audio)
221 {
222         if (_sound_mxf_writer) {
223                 _sound_mxf_writer->write (audio->data(), audio->frames());
224         }
225 }
226
227 /** This must be called from Writer::thread() with an appropriate lock held */
228 bool
229 Writer::have_sequenced_image_at_queue_head ()
230 {
231         if (_queue.empty ()) {
232                 return false;
233         }
234
235         _queue.sort ();
236
237         /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
238
239         if (_queue.front().eyes == EYES_BOTH) {
240                 /* 2D */
241                 return _queue.front().frame == (_last_written_frame + 1);
242         }
243
244         /* 3D */
245
246         if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
247                 return true;
248         }
249
250         if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
251                 return true;
252         }
253
254         return false;
255 }
256
257 void
258 Writer::thread ()
259 try
260 {
261         while (true)
262         {
263                 boost::mutex::scoped_lock lock (_mutex);
264
265                 /* This is for debugging only */
266                 bool done_something = false;
267
268                 while (true) {
269                         
270                         if (_finish || _queued_full_in_memory > maximum_frames_in_memory () || have_sequenced_image_at_queue_head ()) {
271                                 /* We've got something to do: go and do it */
272                                 break;
273                         }
274
275                         /* Nothing to do: wait until something happens which may indicate that we do */
276                         LOG_TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
277                         _empty_condition.wait (lock);
278                         LOG_TIMING (N_("writer wakes with a queue of %1"), _queue.size());
279                 }
280
281                 if (_finish && _queue.empty()) {
282                         return;
283                 }
284
285                 /* We stop here if we have been asked to finish, and if either the queue
286                    is empty or we do not have a sequenced image at its head (if this is the
287                    case we will never terminate as no new frames will be sent once
288                    _finish is true).
289                 */
290                 if (_finish && (!have_sequenced_image_at_queue_head() || _queue.empty())) {
291                         done_something = true;
292                         /* (Hopefully temporarily) log anything that was not written */
293                         if (!_queue.empty() && !have_sequenced_image_at_queue_head()) {
294                                 LOG_WARNING (N_("Finishing writer with a left-over queue of %1:"), _queue.size());
295                                 for (list<QueueItem>::const_iterator i = _queue.begin(); i != _queue.end(); ++i) {
296                                         if (i->type == QueueItem::FULL) {
297                                                 LOG_WARNING (N_("- type FULL, frame %1, eyes %2"), i->frame, i->eyes);
298                                         } else {
299                                                 LOG_WARNING (N_("- type FAKE, size %1, frame %2, eyes %3"), i->size, i->frame, i->eyes);
300                                         }                                               
301                                 }
302                                 LOG_WARNING (N_("Last written frame %1, last written eyes %2"), _last_written_frame, _last_written_eyes);
303                         }
304                         return;
305                 }
306                 /* Write any frames that we can write; i.e. those that are in sequence. */
307                 while (have_sequenced_image_at_queue_head ()) {
308                         done_something = true;
309                         QueueItem qi = _queue.front ();
310                         _queue.pop_front ();
311                         if (qi.type == QueueItem::FULL && qi.encoded) {
312                                 --_queued_full_in_memory;
313                         }
314
315                         lock.unlock ();
316                         switch (qi.type) {
317                         case QueueItem::FULL:
318                         {
319                                 LOG_GENERAL (N_("Writer FULL-writes %1 to MXF"), qi.frame);
320                                 if (!qi.encoded) {
321                                         qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, qi.eyes, false)));
322                                 }
323
324                                 dcp::FrameInfo fin = _picture_mxf_writer->write (qi.encoded->data(), qi.encoded->size());
325                                 qi.encoded->write_info (_film, qi.frame, qi.eyes, fin);
326                                 _last_written[qi.eyes] = qi.encoded;
327                                 ++_full_written;
328                                 break;
329                         }
330                         case QueueItem::FAKE:
331                                 LOG_GENERAL (N_("Writer FAKE-writes %1 to MXF"), qi.frame);
332                                 _picture_mxf_writer->fake_write (qi.size);
333                                 _last_written[qi.eyes].reset ();
334                                 ++_fake_written;
335                                 break;
336                         }
337                         lock.lock ();
338
339                         _last_written_frame = qi.frame;
340                         _last_written_eyes = qi.eyes;
341                         
342                         shared_ptr<Job> job = _job.lock ();
343                         DCPOMATIC_ASSERT (job);
344                         int64_t total = _film->length().frames (_film->video_frame_rate ());
345                         if (_film->three_d ()) {
346                                 /* _full_written and so on are incremented for each eye, so we need to double the total
347                                    frames to get the correct progress.
348                                 */
349                                 total *= 2;
350                         }
351                         if (total) {
352                                 job->set_progress (float (_full_written + _fake_written) / total);
353                         }
354                 }
355
356                 while (_queued_full_in_memory > maximum_frames_in_memory ()) {
357                         done_something = true;
358                         /* Too many frames in memory which can't yet be written to the stream.
359                            Write some FULL frames to disk.
360                         */
361
362                         /* Find one from the back of the queue */
363                         _queue.sort ();
364                         list<QueueItem>::reverse_iterator i = _queue.rbegin ();
365                         while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
366                                 ++i;
367                         }
368
369                         DCPOMATIC_ASSERT (i != _queue.rend());
370                         ++_pushed_to_disk;
371                         lock.unlock ();
372
373                         /* i is valid here, even though we don't hold a lock on the mutex,
374                            since list iterators are unaffected by insertion and only this
375                            thread could erase the last item in the list.
376                         */
377
378                         LOG_GENERAL (
379                                 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
380                                 _last_written_frame + 1,
381                                 _last_written_eyes, i->frame
382                                 );
383                         
384                         i->encoded->write (_film, i->frame, i->eyes);
385                         
386                         lock.lock ();
387                         i->encoded.reset ();
388                         --_queued_full_in_memory;
389                 }
390
391                 if (!done_something) {
392                         LOG_DEBUG_NC ("Writer loop ran without doing anything");
393                         LOG_DEBUG ("_queued_full_in_memory=%1", _queued_full_in_memory);
394                         LOG_DEBUG ("_queue_size=%1", _queue.size ());
395                         LOG_DEBUG ("_finish=%1", _finish);
396                         LOG_DEBUG ("_last_written_frame=%1", _last_written_frame);
397                 }
398
399                 /* The queue has probably just gone down a bit; notify anything wait()ing on _full_condition */
400                 _full_condition.notify_all ();
401         }
402 }
403 catch (...)
404 {
405         store_current ();
406 }
407
408 void
409 Writer::terminate_thread (bool can_throw)
410 {
411         boost::mutex::scoped_lock lock (_mutex);
412         if (_thread == 0) {
413                 return;
414         }
415         
416         _finish = true;
417         _empty_condition.notify_all ();
418         _full_condition.notify_all ();
419         lock.unlock ();
420
421         _thread->join ();
422         if (can_throw) {
423                 rethrow ();
424         }
425         
426         delete _thread;
427         _thread = 0;
428 }       
429
430 void
431 Writer::finish ()
432 {
433         if (!_thread) {
434                 return;
435         }
436         
437         terminate_thread (true);
438
439         _picture_mxf_writer->finalize ();
440         if (_sound_mxf_writer) {
441                 _sound_mxf_writer->finalize ();
442         }
443         
444         /* Hard-link the video MXF into the DCP */
445         boost::filesystem::path video_from = _picture_mxf->file ();
446         
447         boost::filesystem::path video_to;
448         video_to /= _film->dir (_film->dcp_name());
449         video_to /= video_mxf_filename (_picture_mxf);
450
451         boost::system::error_code ec;
452         boost::filesystem::create_hard_link (video_from, video_to, ec);
453         if (ec) {
454                 LOG_WARNING_NC ("Hard-link failed; copying instead");
455                 boost::filesystem::copy_file (video_from, video_to, ec);
456                 if (ec) {
457                         LOG_ERROR ("Failed to copy video file from %1 to %2 (%3)", video_from.string(), video_to.string(), ec.message ());
458                         throw FileError (ec.message(), video_from);
459                 }
460         }
461
462         _picture_mxf->set_file (video_to);
463
464         /* Move the audio MXF into the DCP */
465
466         if (_sound_mxf) {
467                 boost::filesystem::path audio_to;
468                 audio_to /= _film->dir (_film->dcp_name ());
469                 audio_to /= audio_mxf_filename (_sound_mxf);
470                 
471                 boost::filesystem::rename (_film->file (audio_mxf_filename (_sound_mxf)), audio_to, ec);
472                 if (ec) {
473                         throw FileError (
474                                 String::compose (_("could not move audio MXF into the DCP (%1)"), ec.value ()), audio_mxf_filename (_sound_mxf)
475                                 );
476                 }
477
478                 _sound_mxf->set_file (audio_to);
479         }
480
481         dcp::DCP dcp (_film->dir (_film->dcp_name()));
482
483         shared_ptr<dcp::CPL> cpl (
484                 new dcp::CPL (
485                         _film->dcp_name(),
486                         _film->dcp_content_type()->libdcp_kind ()
487                         )
488                 );
489         
490         dcp.add (cpl);
491
492         shared_ptr<dcp::Reel> reel (new dcp::Reel ());
493
494         shared_ptr<dcp::MonoPictureMXF> mono = dynamic_pointer_cast<dcp::MonoPictureMXF> (_picture_mxf);
495         if (mono) {
496                 reel->add (shared_ptr<dcp::ReelPictureAsset> (new dcp::ReelMonoPictureAsset (mono, 0)));
497                 dcp.add (mono);
498         }
499
500         shared_ptr<dcp::StereoPictureMXF> stereo = dynamic_pointer_cast<dcp::StereoPictureMXF> (_picture_mxf);
501         if (stereo) {
502                 reel->add (shared_ptr<dcp::ReelPictureAsset> (new dcp::ReelStereoPictureAsset (stereo, 0)));
503                 dcp.add (stereo);
504         }
505
506         if (_sound_mxf) {
507                 reel->add (shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (_sound_mxf, 0)));
508                 dcp.add (_sound_mxf);
509         }
510
511         if (_subtitle_content) {
512                 boost::filesystem::path const liberation = shared_path () / "LiberationSans-Regular.ttf";
513
514                 /* Add all the fonts to the subtitle content and as assets to the DCP */
515                 BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
516                         boost::filesystem::path const from = i->file.get_value_or (liberation);
517                         _subtitle_content->add_font (i->id, from.leaf().string ());
518
519                         boost::filesystem::path to = _film->dir (_film->dcp_name ()) / _subtitle_content->id ();
520                         boost::filesystem::create_directories (to, ec);
521                         if (ec) {
522                                 throw FileError (_("Could not create directory"), to);
523                         }
524
525                         to /= from.leaf();
526
527                         boost::system::error_code ec;
528                         boost::filesystem::copy_file (from, to, ec);
529                         if (ec) {
530                                 throw FileError ("Could not copy font to DCP", from);
531                         }
532
533                         dcp.add (shared_ptr<dcp::Font> (new dcp::Font (to)));
534                 }
535
536                 _subtitle_content->write_xml (_film->dir (_film->dcp_name ()) / _subtitle_content->id () / subtitle_content_filename (_subtitle_content));
537                 reel->add (shared_ptr<dcp::ReelSubtitleAsset> (
538                                    new dcp::ReelSubtitleAsset (
539                                            _subtitle_content,
540                                            dcp::Fraction (_film->video_frame_rate(), 1),
541                                            _picture_mxf->intrinsic_duration (),
542                                            0
543                                            )
544                                    ));
545                 
546                 dcp.add (_subtitle_content);
547         }
548         
549         cpl->add (reel);
550
551         shared_ptr<Job> job = _job.lock ();
552         DCPOMATIC_ASSERT (job);
553
554         job->sub (_("Computing image digest"));
555         _picture_mxf->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
556
557         if (_sound_mxf) {
558                 job->sub (_("Computing audio digest"));
559                 _sound_mxf->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
560         }
561
562         dcp::XMLMetadata meta;
563         meta.issuer = Config::instance()->dcp_issuer ();
564         meta.creator = String::compose ("DCP-o-matic %1 %2", dcpomatic_version, dcpomatic_git_commit);
565         meta.set_issue_date_now ();
566
567         shared_ptr<const dcp::Signer> signer;
568         if (_film->is_signed ()) {
569                 signer = Config::instance()->signer ();
570                 /* We did check earlier, but check again here to be on the safe side */
571                 if (!signer->valid ()) {
572                         throw InvalidSignerError ();
573                 }
574         }
575
576         dcp.write_xml (_film->interop () ? dcp::INTEROP : dcp::SMPTE, meta, signer);
577
578         LOG_GENERAL (
579                 N_("Wrote %1 FULL, %2 FAKE, %3 pushed to disk"), _full_written, _fake_written, _pushed_to_disk
580                 );
581 }
582
583 bool
584 Writer::check_existing_picture_mxf_frame (FILE* mxf, int f, Eyes eyes)
585 {
586         /* Read the frame info as written */
587         FILE* file = fopen_boost (_film->info_file (), "rb");
588         if (!file) {
589                 LOG_GENERAL ("Existing frame %1 has no info file", f);
590                 return false;
591         }
592         
593         dcp::FrameInfo info = read_frame_info (file, f, eyes);
594         fclose (file);
595         if (info.size == 0) {
596                 LOG_GENERAL ("Existing frame %1 has no info file", f);
597                 return false;
598         }
599         
600         /* Read the data from the MXF and hash it */
601         dcpomatic_fseek (mxf, info.offset, SEEK_SET);
602         EncodedData data (info.size);
603         size_t const read = fread (data.data(), 1, data.size(), mxf);
604         if (read != static_cast<size_t> (data.size ())) {
605                 LOG_GENERAL ("Existing frame %1 is incomplete", f);
606                 return false;
607         }
608
609         MD5Digester digester;
610         digester.add (data.data(), data.size());
611         if (digester.get() != info.hash) {
612                 LOG_GENERAL ("Existing frame %1 failed hash check", f);
613                 return false;
614         }
615
616         return true;
617 }
618
619 void
620 Writer::check_existing_picture_mxf ()
621 {
622         /* Try to open the existing MXF */
623         FILE* mxf = fopen_boost (_picture_mxf->file(), "rb");
624         if (!mxf) {
625                 LOG_GENERAL ("Could not open existing MXF at %1 (errno=%2)", _picture_mxf->file().string(), errno);
626                 return;
627         }
628
629         while (true) {
630
631                 shared_ptr<Job> job = _job.lock ();
632                 DCPOMATIC_ASSERT (job);
633
634                 job->set_progress_unknown ();
635
636                 if (_film->three_d ()) {
637                         if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_LEFT)) {
638                                 break;
639                         }
640                         if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_RIGHT)) {
641                                 break;
642                         }
643                 } else {
644                         if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_BOTH)) {
645                                 break;
646                         }
647                 }
648
649                 LOG_GENERAL ("Have existing frame %1", _first_nonexistant_frame);
650                 ++_first_nonexistant_frame;
651         }
652
653         fclose (mxf);
654 }
655
656 /** @param frame Frame index.
657  *  @return true if we can fake-write this frame.
658  */
659 bool
660 Writer::can_fake_write (int frame) const
661 {
662         /* We have to do a proper write of the first frame so that we can set up the JPEG2000
663            parameters in the MXF writer.
664         */
665         return (frame != 0 && frame < _first_nonexistant_frame);
666 }
667
668 void
669 Writer::write (PlayerSubtitles subs)
670 {
671         if (subs.text.empty ()) {
672                 return;
673         }
674
675         if (!_subtitle_content) {
676                 string lang = _film->subtitle_language ();
677                 if (lang.empty ()) {
678                         lang = "Unknown";
679                 }
680                 _subtitle_content.reset (new dcp::InteropSubtitleContent (_film->name(), lang));
681         }
682         
683         for (list<dcp::SubtitleString>::const_iterator i = subs.text.begin(); i != subs.text.end(); ++i) {
684                 _subtitle_content->add (*i);
685         }
686 }
687
688 void
689 Writer::write (list<shared_ptr<Font> > fonts)
690 {
691         /* Just keep a list of fonts and we'll deal with them in ::finish */
692         copy (fonts.begin (), fonts.end (), back_inserter (_fonts));
693 }
694
695 bool
696 operator< (QueueItem const & a, QueueItem const & b)
697 {
698         if (a.frame != b.frame) {
699                 return a.frame < b.frame;
700         }
701
702         return static_cast<int> (a.eyes) < static_cast<int> (b.eyes);
703 }
704
705 bool
706 operator== (QueueItem const & a, QueueItem const & b)
707 {
708         return a.frame == b.frame && a.eyes == b.eyes;
709 }
710
711 int
712 Writer::maximum_frames_in_memory () const
713 {
714         return Config::instance()->num_local_encoding_threads() + 4;
715 }