2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include "compose.hpp"
25 #include "dcp_video.h"
26 #include "dcp_content_type.h"
27 #include "audio_mapping.h"
31 #include "audio_buffers.h"
32 #include "md5_digester.h"
37 #include <dcp/mono_picture_asset.h>
38 #include <dcp/stereo_picture_asset.h>
39 #include <dcp/sound_asset.h>
40 #include <dcp/sound_asset_writer.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>
48 #include <dcp/certificate_chain.h>
49 #include <dcp/interop_subtitle_asset.h>
50 #include <dcp/smpte_subtitle_asset.h>
51 #include <boost/foreach.hpp>
58 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
59 #define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_GENERAL);
60 #define LOG_DEBUG_ENCODE(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_DEBUG_ENCODE);
61 #define LOG_TIMING(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_TIMING);
62 #define LOG_WARNING_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_WARNING);
63 #define LOG_WARNING(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_WARNING);
64 #define LOG_ERROR(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_ERROR);
66 /* OS X strikes again */
74 using boost::shared_ptr;
75 using boost::weak_ptr;
76 using boost::dynamic_pointer_cast;
78 int const Writer::_info_size = 48;
80 Writer::Writer (shared_ptr<const Film> film, weak_ptr<Job> j)
85 , _queued_full_in_memory (0)
86 , _last_written_frame (-1)
87 , _last_written_eyes (EYES_RIGHT)
88 , _maximum_frames_in_memory (0)
94 /* Remove any old DCP */
95 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
97 shared_ptr<Job> job = _job.lock ();
98 DCPOMATIC_ASSERT (job);
100 BOOST_FOREACH (DCPTimePeriod p, _film->reels ()) {
104 /* Create our picture asset in a subdirectory, named according to those
105 film's parameters which affect the video output. We will hard-link
106 it into the DCP later.
109 if (_film->three_d ()) {
110 reel.picture_asset.reset (new dcp::StereoPictureAsset (dcp::Fraction (_film->video_frame_rate (), 1)));
112 reel.picture_asset.reset (new dcp::MonoPictureAsset (dcp::Fraction (_film->video_frame_rate (), 1)));
115 reel.picture_asset->set_size (_film->frame_size ());
117 if (_film->encrypted ()) {
118 reel.picture_asset->set_key (_film->key ());
121 reel.picture_asset->set_file (
122 _film->internal_video_asset_dir() / _film->internal_video_asset_filename(p)
125 job->sub (_("Checking existing image data"));
126 check_existing_picture_asset (reel);
128 reel.picture_asset_writer = reel.picture_asset->start_write (
129 _film->internal_video_asset_dir() / _film->internal_video_asset_filename(p),
130 _film->interop() ? dcp::INTEROP : dcp::SMPTE,
131 reel.first_nonexistant_frame > 0
134 if (_film->audio_channels ()) {
135 reel.sound_asset.reset (
136 new dcp::SoundAsset (dcp::Fraction (_film->video_frame_rate(), 1), _film->audio_frame_rate (), _film->audio_channels ())
139 if (_film->encrypted ()) {
140 reel.sound_asset->set_key (_film->key ());
143 /* Write the sound asset into the film directory so that we leave the creation
144 of the DCP directory until the last minute.
146 reel.sound_asset_writer = reel.sound_asset->start_write (
147 _film->directory() / audio_asset_filename (reel.sound_asset),
148 _film->interop() ? dcp::INTEROP : dcp::SMPTE
152 _reels.push_back (reel);
155 /* We can keep track of the current audio and subtitle reels easily because audio
156 and subs arrive to the Writer in sequence. This is not so for video.
158 _audio_reel = _reels.begin ();
159 _subtitle_reel = _reels.begin ();
161 /* Check that the signer is OK if we need one */
162 if (_film->is_signed() && !Config::instance()->signer_chain()->valid ()) {
163 throw InvalidSignerError ();
166 job->sub (_("Encoding image data"));
172 _thread = new boost::thread (boost::bind (&Writer::thread, this));
177 terminate_thread (false);
181 Writer::write (Data encoded, int frame, Eyes eyes)
183 boost::mutex::scoped_lock lock (_state_mutex);
185 while (_queued_full_in_memory > _maximum_frames_in_memory) {
186 /* The queue is too big; wait until that is sorted out */
187 _full_condition.wait (lock);
191 qi.type = QueueItem::FULL;
192 qi.encoded = encoded;
195 if (_film->three_d() && eyes == EYES_BOTH) {
196 /* 2D material in a 3D DCP; fake the 3D */
198 _queue.push_back (qi);
199 ++_queued_full_in_memory;
200 qi.eyes = EYES_RIGHT;
201 _queue.push_back (qi);
202 ++_queued_full_in_memory;
205 _queue.push_back (qi);
206 ++_queued_full_in_memory;
209 /* Now there's something to do: wake anything wait()ing on _empty_condition */
210 _empty_condition.notify_all ();
214 Writer::repeat (int frame, Eyes eyes)
216 boost::mutex::scoped_lock lock (_state_mutex);
218 while (_queued_full_in_memory > _maximum_frames_in_memory) {
219 /* The queue is too big; wait until that is sorted out */
220 _full_condition.wait (lock);
224 qi.type = QueueItem::REPEAT;
226 if (_film->three_d() && eyes == EYES_BOTH) {
228 _queue.push_back (qi);
229 qi.eyes = EYES_RIGHT;
230 _queue.push_back (qi);
233 _queue.push_back (qi);
236 /* Now there's something to do: wake anything wait()ing on _empty_condition */
237 _empty_condition.notify_all ();
241 Writer::fake_write (int frame, Eyes eyes)
243 boost::mutex::scoped_lock lock (_state_mutex);
245 while (_queued_full_in_memory > _maximum_frames_in_memory) {
246 /* The queue is too big; wait until that is sorted out */
247 _full_condition.wait (lock);
250 Reel const & reel = video_reel (frame);
252 FILE* file = fopen_boost (_film->info_file(reel.period), "rb");
254 throw ReadFileError (_film->info_file(reel.period));
256 dcp::FrameInfo info = read_frame_info (file, frame, eyes);
260 qi.type = QueueItem::FAKE;
263 if (_film->three_d() && eyes == EYES_BOTH) {
265 _queue.push_back (qi);
266 qi.eyes = EYES_RIGHT;
267 _queue.push_back (qi);
270 _queue.push_back (qi);
273 /* Now there's something to do: wake anything wait()ing on _empty_condition */
274 _empty_condition.notify_all ();
277 /** This method is not thread safe */
279 Writer::write (shared_ptr<const AudioBuffers> audio)
281 if (!_audio_reel->sound_asset_writer) {
285 _audio_reel->sound_asset_writer->write (audio->data(), audio->frames());
287 /* SoundAsset's `frames' are video frames, not audio frames */
288 if (_audio_reel->sound_asset_writer->frames_written() >= _audio_reel->period.duration().frames_round (_film->video_frame_rate())) {
293 /** This must be called from Writer::thread() with an appropriate lock held */
295 Writer::have_sequenced_image_at_queue_head ()
297 if (_queue.empty ()) {
303 /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
305 if (_queue.front().eyes == EYES_BOTH) {
307 return _queue.front().frame == (_last_written_frame + 1);
312 if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
316 if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
324 Writer::write_frame_info (int frame, Eyes eyes, dcp::FrameInfo info) const
326 Reel const & reel = video_reel (frame);
328 boost::filesystem::path info_file = _film->info_file (reel.period);
329 if (boost::filesystem::exists (info_file)) {
330 file = fopen_boost (info_file, "r+b");
332 file = fopen_boost (info_file, "wb");
335 throw OpenFileError (info_file);
337 dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
338 fwrite (&info.offset, sizeof (info.offset), 1, file);
339 fwrite (&info.size, sizeof (info.size), 1, file);
340 fwrite (info.hash.c_str(), 1, info.hash.size(), file);
350 boost::mutex::scoped_lock lock (_state_mutex);
354 if (_finish || _queued_full_in_memory > _maximum_frames_in_memory || have_sequenced_image_at_queue_head ()) {
355 /* We've got something to do: go and do it */
359 /* Nothing to do: wait until something happens which may indicate that we do */
360 LOG_TIMING (N_("writer-sleep queue=%1"), _queue.size());
361 _empty_condition.wait (lock);
362 LOG_TIMING (N_("writer-wake queue=%1"), _queue.size());
365 if (_finish && _queue.empty()) {
369 /* We stop here if we have been asked to finish, and if either the queue
370 is empty or we do not have a sequenced image at its head (if this is the
371 case we will never terminate as no new frames will be sent once
374 if (_finish && (!have_sequenced_image_at_queue_head() || _queue.empty())) {
375 /* (Hopefully temporarily) log anything that was not written */
376 if (!_queue.empty() && !have_sequenced_image_at_queue_head()) {
377 LOG_WARNING (N_("Finishing writer with a left-over queue of %1:"), _queue.size());
378 for (list<QueueItem>::const_iterator i = _queue.begin(); i != _queue.end(); ++i) {
379 if (i->type == QueueItem::FULL) {
380 LOG_WARNING (N_("- type FULL, frame %1, eyes %2"), i->frame, i->eyes);
382 LOG_WARNING (N_("- type FAKE, size %1, frame %2, eyes %3"), i->size, i->frame, i->eyes);
385 LOG_WARNING (N_("Last written frame %1, last written eyes %2"), _last_written_frame, _last_written_eyes);
389 /* Write any frames that we can write; i.e. those that are in sequence. */
390 while (have_sequenced_image_at_queue_head ()) {
391 QueueItem qi = _queue.front ();
393 if (qi.type == QueueItem::FULL && qi.encoded) {
394 --_queued_full_in_memory;
399 Reel const & reel = video_reel (qi.frame);
402 case QueueItem::FULL:
404 LOG_DEBUG_ENCODE (N_("Writer FULL-writes %1 (%2)"), qi.frame, qi.eyes);
406 qi.encoded = Data (_film->j2c_path (qi.frame, qi.eyes, false));
409 dcp::FrameInfo fin = reel.picture_asset_writer->write (qi.encoded->data().get (), qi.encoded->size());
410 write_frame_info (qi.frame, qi.eyes, fin);
411 _last_written[qi.eyes] = qi.encoded;
415 case QueueItem::FAKE:
416 LOG_DEBUG_ENCODE (N_("Writer FAKE-writes %1"), qi.frame);
417 reel.picture_asset_writer->fake_write (qi.size);
418 _last_written[qi.eyes].reset ();
421 case QueueItem::REPEAT:
422 LOG_DEBUG_ENCODE (N_("Writer REPEAT-writes %1"), qi.frame);
423 dcp::FrameInfo fin = reel.picture_asset_writer->write (
424 _last_written[qi.eyes]->data().get(),
425 _last_written[qi.eyes]->size()
427 write_frame_info (qi.frame, qi.eyes, fin);
434 _last_written_frame = qi.frame;
435 _last_written_eyes = qi.eyes;
437 shared_ptr<Job> job = _job.lock ();
438 DCPOMATIC_ASSERT (job);
439 int64_t total = _film->length().frames_round (_film->video_frame_rate ());
440 if (_film->three_d ()) {
441 /* _full_written and so on are incremented for each eye, so we need to double the total
442 frames to get the correct progress.
447 job->set_progress (float (_full_written + _fake_written + _repeat_written) / total);
451 while (_queued_full_in_memory > _maximum_frames_in_memory) {
452 /* Too many frames in memory which can't yet be written to the stream.
453 Write some FULL frames to disk.
456 /* Find one from the back of the queue */
458 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
459 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
463 DCPOMATIC_ASSERT (i != _queue.rend());
467 /* i is valid here, even though we don't hold a lock on the mutex,
468 since list iterators are unaffected by insertion and only this
469 thread could erase the last item in the list.
473 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
474 _last_written_frame + 1,
475 _last_written_eyes, i->frame
478 i->encoded->write_via_temp (_film->j2c_path (i->frame, i->eyes, true), _film->j2c_path (i->frame, i->eyes, false));
482 --_queued_full_in_memory;
485 /* The queue has probably just gone down a bit; notify anything wait()ing on _full_condition */
486 _full_condition.notify_all ();
495 Writer::terminate_thread (bool can_throw)
497 boost::mutex::scoped_lock lock (_state_mutex);
503 _empty_condition.notify_all ();
504 _full_condition.notify_all ();
507 DCPOMATIC_ASSERT (_thread->joinable ());
524 terminate_thread (true);
526 BOOST_FOREACH (Reel& i, _reels) {
528 if (!i.picture_asset_writer->finalize ()) {
529 /* Nothing was written to the picture asset */
530 i.picture_asset.reset ();
533 if (i.sound_asset_writer && !i.sound_asset_writer->finalize ()) {
534 /* Nothing was written to the sound asset */
535 i.sound_asset.reset ();
538 /* Hard-link any video asset file into the DCP */
539 if (i.picture_asset) {
540 boost::filesystem::path video_from = i.picture_asset->file ();
541 boost::filesystem::path video_to;
542 video_to /= _film->dir (_film->dcp_name());
543 video_to /= video_asset_filename (i.picture_asset);
545 boost::system::error_code ec;
546 boost::filesystem::create_hard_link (video_from, video_to, ec);
548 LOG_WARNING_NC ("Hard-link failed; copying instead");
549 boost::filesystem::copy_file (video_from, video_to, ec);
551 LOG_ERROR ("Failed to copy video file from %1 to %2 (%3)", video_from.string(), video_to.string(), ec.message ());
552 throw FileError (ec.message(), video_from);
556 i.picture_asset->set_file (video_to);
559 /* Move the audio asset into the DCP */
561 boost::filesystem::path audio_to;
562 audio_to /= _film->dir (_film->dcp_name ());
563 audio_to /= audio_asset_filename (i.sound_asset);
565 boost::system::error_code ec;
566 boost::filesystem::rename (_film->file (audio_asset_filename (i.sound_asset)), audio_to, ec);
569 String::compose (_("could not move audio asset into the DCP (%1)"), ec.value ()), audio_asset_filename (i.sound_asset)
573 i.sound_asset->set_file (audio_to);
577 dcp::DCP dcp (_film->dir (_film->dcp_name()));
579 shared_ptr<dcp::CPL> cpl (
582 _film->dcp_content_type()->libdcp_kind ()
588 BOOST_FOREACH (Reel& i, _reels) {
589 shared_ptr<dcp::Reel> reel (new dcp::Reel ());
591 shared_ptr<dcp::ReelPictureAsset> reel_picture_asset;
593 if (i.picture_asset) {
594 /* We have made a picture asset of our own. Put it into the reel */
595 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (i.picture_asset);
597 reel_picture_asset.reset (new dcp::ReelMonoPictureAsset (mono, 0));
600 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (i.picture_asset);
602 reel_picture_asset.reset (new dcp::ReelStereoPictureAsset (stereo, 0));
605 /* We don't have a picture asset of our own; maybe we need to reference one */
606 /* XXX: this is all a hack */
607 BOOST_FOREACH (shared_ptr<dcp::ReelAsset> j, _reel_assets) {
608 shared_ptr<dcp::ReelPictureAsset> k = dynamic_pointer_cast<dcp::ReelPictureAsset> (j);
610 reel_picture_asset = k;
615 reel->add (reel_picture_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)));
621 /* We don't have a sound asset of our own; maybe we need to reference one */
622 /* XXX: this is all a hack */
623 BOOST_FOREACH (shared_ptr<dcp::ReelAsset> j, _reel_assets) {
624 if (dynamic_pointer_cast<dcp::ReelSoundAsset> (j)) {
630 if (i.subtitle_asset) {
631 boost::filesystem::path liberation;
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";
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));
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"));
650 /* All our assets should be the same length; use the picture asset length here
651 as a reference to set the subtitle one.
653 dynamic_pointer_cast<dcp::SMPTESubtitleAsset>(i.subtitle_asset)->set_intrinsic_duration (
654 reel_picture_asset->intrinsic_duration ()
657 i.subtitle_asset->write (
658 _film->dir (_film->dcp_name ()) / ("sub_" + i.subtitle_asset->id() + ".mxf")
662 reel->add (shared_ptr<dcp::ReelSubtitleAsset> (
663 new dcp::ReelSubtitleAsset (
665 dcp::Fraction (_film->video_frame_rate(), 1),
666 reel_picture_asset->intrinsic_duration (),
671 /* We don't have a subtitle asset of our own; maybe we need to reference one */
672 /* XXX: this is all a hack */
673 BOOST_FOREACH (shared_ptr<dcp::ReelAsset> j, _reel_assets) {
674 if (dynamic_pointer_cast<dcp::ReelSubtitleAsset> (j)) {
682 shared_ptr<Job> job = _job.lock ();
683 DCPOMATIC_ASSERT (job);
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));
691 job->sub (_("Computing audio digest"));
692 i.sound_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
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);
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);
705 meta.set_issue_date_now ();
707 cpl->set_metadata (meta);
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 ();
718 dcp.write_xml (_film->interop () ? dcp::INTEROP : dcp::SMPTE, meta, signer);
721 N_("Wrote %1 FULL, %2 FAKE, %3 REPEAT, %4 pushed to disk"), _full_written, _fake_written, _repeat_written, _pushed_to_disk
726 Writer::check_existing_picture_asset (Reel& reel)
728 /* Try to open the existing asset */
729 FILE* asset_file = fopen_boost (reel.picture_asset->file(), "rb");
731 LOG_GENERAL ("Could not open existing asset at %1 (errno=%2)", reel.picture_asset->file().string(), errno);
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;
738 FILE* info_file = fopen_boost (_film->info_file(reel.period), "rb");
740 LOG_GENERAL_NC ("Could not open film info file");
745 if (_film->three_d ()) {
746 /* Start looking at the last left frame */
747 reel.first_nonexistant_frame = n / 2;
749 reel.first_nonexistant_frame = n;
755 /* Read the data from the info file; for 3D we just check the left
756 frames until we find a good one.
758 dcp::FrameInfo info = read_frame_info (info_file, reel.first_nonexistant_frame, _film->three_d () ? EYES_LEFT : EYES_BOTH);
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);
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);
779 --reel.first_nonexistant_frame;
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.
787 ++reel.first_nonexistant_frame;
794 /** @param frame Frame index.
795 * @return true if we can fake-write this frame.
798 Writer::can_fake_write (int frame) const
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.
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.
809 return (frame != 0 && frame < video_reel(frame).first_nonexistant_frame);
813 Writer::write (PlayerSubtitles subs)
815 if (subs.text.empty ()) {
819 if (_subtitle_reel->period.to < subs.from) {
823 if (!_subtitle_reel->subtitle_asset) {
824 string lang = _film->subtitle_language ();
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;
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;
846 for (list<dcp::SubtitleString>::const_iterator i = subs.text.begin(); i != subs.text.end(); ++i) {
847 _subtitle_reel->subtitle_asset->add (*i);
852 Writer::write (list<shared_ptr<Font> > fonts)
854 /* Just keep a list of fonts and we'll deal with them in ::finish */
855 copy (fonts.begin (), fonts.end (), back_inserter (_fonts));
859 operator< (QueueItem const & a, QueueItem const & b)
861 if (a.frame != b.frame) {
862 return a.frame < b.frame;
865 return static_cast<int> (a.eyes) < static_cast<int> (b.eyes);
869 operator== (QueueItem const & a, QueueItem const & b)
871 return a.frame == b.frame && a.eyes == b.eyes;
875 Writer::set_encoder_threads (int threads)
877 _maximum_frames_in_memory = lrint (threads * 1.1);
881 Writer::frame_info_position (int frame, Eyes eyes) const
885 return frame * _info_size;
887 return frame * _info_size * 2;
889 return frame * _info_size * 2 + _info_size;
891 DCPOMATIC_ASSERT (false);
894 DCPOMATIC_ASSERT (false);
898 Writer::read_frame_info (FILE* file, int frame, Eyes eyes) const
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);
905 char hash_buffer[33];
906 fread (hash_buffer, 1, 32, file);
907 hash_buffer[32] = '\0';
908 info.hash = hash_buffer;
914 Writer::write (shared_ptr<dcp::ReelAsset> asset)
916 _reel_assets.push_back (asset);
920 Writer::video_reel (int frame) const
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)) {
928 DCPOMATIC_ASSERT (i != _reels.end ());