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