2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "reel_writer.h"
28 #include "compose.hpp"
29 #include "audio_buffers.h"
30 #include <dcp/mono_picture_asset.h>
31 #include <dcp/stereo_picture_asset.h>
32 #include <dcp/sound_asset.h>
33 #include <dcp/sound_asset_writer.h>
35 #include <dcp/reel_mono_picture_asset.h>
36 #include <dcp/reel_stereo_picture_asset.h>
37 #include <dcp/reel_sound_asset.h>
38 #include <dcp/reel_subtitle_asset.h>
41 #include <dcp/certificate_chain.h>
42 #include <dcp/interop_subtitle_asset.h>
43 #include <dcp/smpte_subtitle_asset.h>
44 #include <boost/foreach.hpp>
45 #include <boost/make_shared.hpp>
49 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
50 #define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_GENERAL);
51 #define LOG_WARNING_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_WARNING);
52 #define LOG_ERROR(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_ERROR);
57 using boost::shared_ptr;
58 using boost::make_shared;
59 using boost::optional;
60 using boost::dynamic_pointer_cast;
63 int const ReelWriter::_info_size = 48;
65 ReelWriter::ReelWriter (shared_ptr<const Film> film, DCPTimePeriod period, shared_ptr<Job> job)
68 , _first_nonexistant_frame (0)
69 , _last_written_video_frame (-1)
70 , _last_written_eyes (EYES_RIGHT)
71 , _total_written_audio_frames (0)
73 /* Create our picture asset in a subdirectory, named according to those
74 film's parameters which affect the video output. We will hard-link
75 it into the DCP later.
78 if (_film->three_d ()) {
79 _picture_asset.reset (new dcp::StereoPictureAsset (dcp::Fraction (_film->video_frame_rate (), 1)));
81 _picture_asset.reset (new dcp::MonoPictureAsset (dcp::Fraction (_film->video_frame_rate (), 1)));
84 _picture_asset->set_size (_film->frame_size ());
86 if (_film->encrypted ()) {
87 _picture_asset->set_key (_film->key ());
90 _picture_asset->set_file (
91 _film->internal_video_asset_dir() / _film->internal_video_asset_filename(_period)
94 job->sub (_("Checking existing image data"));
95 check_existing_picture_asset ();
97 _picture_asset_writer = _picture_asset->start_write (
98 _film->internal_video_asset_dir() / _film->internal_video_asset_filename(_period),
99 _film->interop() ? dcp::INTEROP : dcp::SMPTE,
100 _first_nonexistant_frame > 0
103 if (_film->audio_channels ()) {
105 new dcp::SoundAsset (dcp::Fraction (_film->video_frame_rate(), 1), _film->audio_frame_rate (), _film->audio_channels ())
108 if (_film->encrypted ()) {
109 _sound_asset->set_key (_film->key ());
112 /* Write the sound asset into the film directory so that we leave the creation
113 of the DCP directory until the last minute.
115 _sound_asset_writer = _sound_asset->start_write (
116 _film->directory() / audio_asset_filename (_sound_asset),
117 _film->interop() ? dcp::INTEROP : dcp::SMPTE
122 /** @param frame reel-relative frame */
124 ReelWriter::write_frame_info (Frame frame, Eyes eyes, dcp::FrameInfo info) const
127 boost::filesystem::path info_file = _film->info_file (_period);
128 if (boost::filesystem::exists (info_file)) {
129 file = fopen_boost (info_file, "r+b");
131 file = fopen_boost (info_file, "wb");
134 throw OpenFileError (info_file);
136 dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
137 fwrite (&info.offset, sizeof (info.offset), 1, file);
138 fwrite (&info.size, sizeof (info.size), 1, file);
139 fwrite (info.hash.c_str(), 1, info.hash.size(), file);
144 ReelWriter::read_frame_info (FILE* file, Frame frame, Eyes eyes) const
147 dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
148 fread (&info.offset, sizeof (info.offset), 1, file);
149 fread (&info.size, sizeof (info.size), 1, file);
151 char hash_buffer[33];
152 fread (hash_buffer, 1, 32, file);
153 hash_buffer[32] = '\0';
154 info.hash = hash_buffer;
160 ReelWriter::frame_info_position (Frame frame, Eyes eyes) const
164 return frame * _info_size;
166 return frame * _info_size * 2;
168 return frame * _info_size * 2 + _info_size;
170 DCPOMATIC_ASSERT (false);
173 DCPOMATIC_ASSERT (false);
177 ReelWriter::check_existing_picture_asset ()
179 /* Try to open the existing asset */
180 FILE* asset_file = fopen_boost (_picture_asset->file(), "rb");
182 LOG_GENERAL ("Could not open existing asset at %1 (errno=%2)", _picture_asset->file().string(), errno);
185 LOG_GENERAL ("Opened existing asset at %1", _picture_asset->file().string());
188 /* Offset of the last dcp::FrameInfo in the info file */
189 int const n = (boost::filesystem::file_size (_film->info_file(_period)) / _info_size) - 1;
190 LOG_GENERAL ("The last FI is %1; info file is %2, info size %3", n, boost::filesystem::file_size (_film->info_file(_period)), _info_size);
192 FILE* info_file = fopen_boost (_film->info_file(_period), "rb");
194 LOG_GENERAL_NC ("Could not open film info file");
199 if (_film->three_d ()) {
200 /* Start looking at the last left frame */
201 _first_nonexistant_frame = n / 2;
203 _first_nonexistant_frame = n;
206 while (!existing_picture_frame_ok(asset_file, info_file) && _first_nonexistant_frame > 0) {
207 --_first_nonexistant_frame;
210 if (!_film->three_d() && _first_nonexistant_frame > 0) {
211 /* If we are doing 3D we might have found a good L frame with no R, so only
212 do this if we're in 2D and we've just found a good B(oth) frame.
214 ++_first_nonexistant_frame;
217 LOG_GENERAL ("Proceeding with first nonexistant frame %1", _first_nonexistant_frame);
224 ReelWriter::write (optional<Data> encoded, Frame frame, Eyes eyes)
226 dcp::FrameInfo fin = _picture_asset_writer->write (encoded->data().get (), encoded->size());
227 write_frame_info (frame, eyes, fin);
228 _last_written[eyes] = encoded;
229 _last_written_video_frame = frame;
230 _last_written_eyes = eyes;
234 ReelWriter::fake_write (Frame frame, Eyes eyes, int size)
236 _picture_asset_writer->fake_write (size);
237 _last_written_video_frame = frame;
238 _last_written_eyes = eyes;
242 ReelWriter::repeat_write (Frame frame, Eyes eyes)
244 dcp::FrameInfo fin = _picture_asset_writer->write (
245 _last_written[eyes]->data().get(),
246 _last_written[eyes]->size()
248 write_frame_info (frame, eyes, fin);
249 _last_written_video_frame = frame;
250 _last_written_eyes = eyes;
254 ReelWriter::finish ()
256 if (!_picture_asset_writer->finalize ()) {
257 /* Nothing was written to the picture asset */
258 _picture_asset.reset ();
261 if (_sound_asset_writer && !_sound_asset_writer->finalize ()) {
262 /* Nothing was written to the sound asset */
263 _sound_asset.reset ();
266 /* Hard-link any video asset file into the DCP */
267 if (_picture_asset) {
268 boost::filesystem::path video_from = _picture_asset->file ();
269 boost::filesystem::path video_to;
270 video_to /= _film->dir (_film->dcp_name());
271 video_to /= video_asset_filename (_picture_asset);
273 boost::system::error_code ec;
274 boost::filesystem::create_hard_link (video_from, video_to, ec);
276 LOG_WARNING_NC ("Hard-link failed; copying instead");
277 boost::filesystem::copy_file (video_from, video_to, ec);
279 LOG_ERROR ("Failed to copy video file from %1 to %2 (%3)", video_from.string(), video_to.string(), ec.message ());
280 throw FileError (ec.message(), video_from);
284 _picture_asset->set_file (video_to);
287 /* Move the audio asset into the DCP */
289 boost::filesystem::path audio_to;
290 audio_to /= _film->dir (_film->dcp_name ());
291 audio_to /= audio_asset_filename (_sound_asset);
293 boost::system::error_code ec;
294 boost::filesystem::rename (_film->file (audio_asset_filename (_sound_asset)), audio_to, ec);
297 String::compose (_("could not move audio asset into the DCP (%1)"), ec.value ()), audio_asset_filename (_sound_asset)
301 _sound_asset->set_file (audio_to);
305 shared_ptr<dcp::Reel>
306 ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr<Font> > const & fonts)
308 shared_ptr<dcp::Reel> reel = make_shared<dcp::Reel> ();
310 shared_ptr<dcp::ReelPictureAsset> reel_picture_asset;
312 if (_picture_asset) {
313 /* We have made a picture asset of our own. Put it into the reel */
314 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (_picture_asset);
316 reel_picture_asset.reset (new dcp::ReelMonoPictureAsset (mono, 0));
319 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (_picture_asset);
321 reel_picture_asset.reset (new dcp::ReelStereoPictureAsset (stereo, 0));
324 /* We don't have a picture asset of our own; hopefully we have one to reference */
325 BOOST_FOREACH (ReferencedReelAsset j, refs) {
326 shared_ptr<dcp::ReelPictureAsset> k = dynamic_pointer_cast<dcp::ReelPictureAsset> (j.asset);
327 if (k && j.period == _period) {
328 reel_picture_asset = k;
333 reel->add (reel_picture_asset);
334 /* If we have a hash for this asset in the CPL, assume that it is correct */
335 if (reel_picture_asset->hash()) {
336 reel_picture_asset->asset_ref()->set_hash (reel_picture_asset->hash().get());
340 /* We have made a sound asset of our own. Put it into the reel */
341 reel->add (make_shared<dcp::ReelSoundAsset> (_sound_asset, 0));
343 /* We don't have a sound asset of our own; hopefully we have one to reference */
344 BOOST_FOREACH (ReferencedReelAsset j, refs) {
345 shared_ptr<dcp::ReelSoundAsset> k = dynamic_pointer_cast<dcp::ReelSoundAsset> (j.asset);
346 if (k && j.period == _period) {
348 /* If we have a hash for this asset in the CPL, assume that it is correct */
350 k->asset_ref()->set_hash (k->hash().get());
356 if (_subtitle_asset) {
358 boost::filesystem::path liberation_normal;
360 liberation_normal = shared_path() / "LiberationSans-Regular.ttf";
361 if (!boost::filesystem::exists (liberation_normal)) {
362 /* Hack for unit tests */
363 liberation_normal = shared_path() / "fonts" / "LiberationSans-Regular.ttf";
365 } catch (boost::filesystem::filesystem_error& e) {
369 if (!boost::filesystem::exists(liberation_normal)) {
370 liberation_normal = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf";
373 /* Add all the fonts to the subtitle content */
374 BOOST_FOREACH (shared_ptr<Font> j, fonts) {
375 _subtitle_asset->add_font (j->id(), j->file(FontFiles::NORMAL).get_value_or(liberation_normal));
378 if (dynamic_pointer_cast<dcp::InteropSubtitleAsset> (_subtitle_asset)) {
379 boost::filesystem::path directory = _film->dir (_film->dcp_name ()) / _subtitle_asset->id ();
380 boost::filesystem::create_directories (directory);
381 _subtitle_asset->write (directory / ("sub_" + _subtitle_asset->id() + ".xml"));
384 /* All our assets should be the same length; use the picture asset length here
385 as a reference to set the subtitle one.
387 dynamic_pointer_cast<dcp::SMPTESubtitleAsset>(_subtitle_asset)->set_intrinsic_duration (
388 reel_picture_asset->intrinsic_duration ()
391 _subtitle_asset->write (
392 _film->dir (_film->dcp_name ()) / ("sub_" + _subtitle_asset->id() + ".mxf")
396 reel->add (shared_ptr<dcp::ReelSubtitleAsset> (
397 new dcp::ReelSubtitleAsset (
399 dcp::Fraction (_film->video_frame_rate(), 1),
400 reel_picture_asset->intrinsic_duration (),
405 /* We don't have a subtitle asset of our own; hopefully we have one to reference */
406 BOOST_FOREACH (ReferencedReelAsset j, refs) {
407 shared_ptr<dcp::ReelSubtitleAsset> k = dynamic_pointer_cast<dcp::ReelSubtitleAsset> (j.asset);
408 if (k && j.period == _period) {
410 /* If we have a hash for this asset in the CPL, assume that it is correct */
412 k->asset_ref()->set_hash (k->hash().get());
422 ReelWriter::calculate_digests (shared_ptr<Job> job)
424 job->sub (_("Computing image digest"));
425 if (_picture_asset) {
426 _picture_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
430 job->sub (_("Computing audio digest"));
431 _sound_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
436 ReelWriter::start () const
438 return _period.from.frames_floor (_film->video_frame_rate());
443 ReelWriter::write (shared_ptr<const AudioBuffers> audio)
445 if (!_sound_asset_writer) {
450 _sound_asset_writer->write (audio->data(), audio->frames());
453 ++_total_written_audio_frames;
457 ReelWriter::write (PlayerSubtitles subs)
459 if (!_subtitle_asset) {
460 string lang = _film->subtitle_language ();
464 if (_film->interop ()) {
465 shared_ptr<dcp::InteropSubtitleAsset> s = make_shared<dcp::InteropSubtitleAsset> ();
466 s->set_movie_title (_film->name ());
467 s->set_language (lang);
468 s->set_reel_number ("1");
471 shared_ptr<dcp::SMPTESubtitleAsset> s = make_shared<dcp::SMPTESubtitleAsset> ();
472 s->set_content_title_text (_film->name ());
473 s->set_language (lang);
474 s->set_edit_rate (dcp::Fraction (_film->video_frame_rate (), 1));
475 s->set_reel_number (1);
476 s->set_time_code_rate (_film->video_frame_rate ());
477 s->set_start_time (dcp::Time ());
482 BOOST_FOREACH (dcp::SubtitleString i, subs.text) {
483 i.set_in (i.in() - dcp::Time (_period.from.seconds(), i.in().tcr));
484 i.set_out (i.out() - dcp::Time (_period.from.seconds(), i.out().tcr));
485 _subtitle_asset->add (i);
490 ReelWriter::existing_picture_frame_ok (FILE* asset_file, FILE* info_file) const
492 LOG_GENERAL ("Checking existing picture frame %1", _first_nonexistant_frame);
494 /* Read the data from the info file; for 3D we just check the left
495 frames until we find a good one.
497 dcp::FrameInfo const info = read_frame_info (info_file, _first_nonexistant_frame, _film->three_d () ? EYES_LEFT : EYES_BOTH);
501 /* Read the data from the asset and hash it */
502 dcpomatic_fseek (asset_file, info.offset, SEEK_SET);
503 Data data (info.size);
504 size_t const read = fread (data.data().get(), 1, data.size(), asset_file);
505 LOG_GENERAL ("Read %1 bytes of asset data; wanted %2", read, info.size);
506 if (read != static_cast<size_t> (data.size ())) {
507 LOG_GENERAL ("Existing frame %1 is incomplete", _first_nonexistant_frame);
511 digester.add (data.data().get(), data.size());
512 LOG_GENERAL ("Hash %1 vs %2", digester.get(), info.hash);
513 if (digester.get() != info.hash) {
514 LOG_GENERAL ("Existing frame %1 failed hash check", _first_nonexistant_frame);