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