Improve OpenFileError so that it doesn't say "opening for read"
[dcpomatic.git] / src / lib / reel_writer.cc
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "reel_writer.h"
22 #include "film.h"
23 #include "cross.h"
24 #include "job.h"
25 #include "log.h"
26 #include "dcpomatic_log.h"
27 #include "digester.h"
28 #include "font.h"
29 #include "compose.hpp"
30 #include "audio_buffers.h"
31 #include "image.h"
32 #include <dcp/mono_picture_asset.h>
33 #include <dcp/stereo_picture_asset.h>
34 #include <dcp/sound_asset.h>
35 #include <dcp/sound_asset_writer.h>
36 #include <dcp/reel.h>
37 #include <dcp/reel_mono_picture_asset.h>
38 #include <dcp/reel_stereo_picture_asset.h>
39 #include <dcp/reel_sound_asset.h>
40 #include <dcp/reel_subtitle_asset.h>
41 #include <dcp/reel_closed_caption_asset.h>
42 #include <dcp/reel_markers_asset.h>
43 #include <dcp/dcp.h>
44 #include <dcp/cpl.h>
45 #include <dcp/certificate_chain.h>
46 #include <dcp/interop_subtitle_asset.h>
47 #include <dcp/smpte_subtitle_asset.h>
48 #include <dcp/raw_convert.h>
49 #include <dcp/subtitle_image.h>
50 #include <boost/foreach.hpp>
51
52 #include "i18n.h"
53
54 using std::list;
55 using std::string;
56 using std::cout;
57 using std::map;
58 using boost::shared_ptr;
59 using boost::optional;
60 using boost::dynamic_pointer_cast;
61 using dcp::Data;
62 using dcp::raw_convert;
63 using namespace dcpomatic;
64
65 int const ReelWriter::_info_size = 48;
66
67 /** @param job Related job, or 0 */
68 ReelWriter::ReelWriter (
69         shared_ptr<const Film> film, DCPTimePeriod period, shared_ptr<Job> job, int reel_index, int reel_count, optional<string> content_summary
70         )
71         : _film (film)
72         , _period (period)
73         , _last_written_video_frame (-1)
74         , _last_written_eyes (EYES_RIGHT)
75         , _reel_index (reel_index)
76         , _reel_count (reel_count)
77         , _content_summary (content_summary)
78 {
79         /* Create our picture asset in a subdirectory, named according to those
80            film's parameters which affect the video output.  We will hard-link
81            it into the DCP later.
82         */
83
84         dcp::Standard const standard = _film->interop() ? dcp::INTEROP : dcp::SMPTE;
85
86         if (_film->three_d ()) {
87                 _picture_asset.reset (new dcp::StereoPictureAsset (dcp::Fraction (_film->video_frame_rate(), 1), standard));
88         } else {
89                 _picture_asset.reset (new dcp::MonoPictureAsset (dcp::Fraction (_film->video_frame_rate(), 1), standard));
90         }
91
92         _picture_asset->set_size (_film->frame_size ());
93
94         if (_film->encrypted ()) {
95                 _picture_asset->set_key (_film->key ());
96                 _picture_asset->set_context_id (_film->context_id ());
97         }
98
99         _picture_asset->set_file (
100                 _film->internal_video_asset_dir() / _film->internal_video_asset_filename(_period)
101                 );
102
103         if (job) {
104                 job->sub (_("Checking existing image data"));
105         }
106         _first_nonexistant_frame = check_existing_picture_asset ();
107
108         _picture_asset_writer = _picture_asset->start_write (
109                 _film->internal_video_asset_dir() / _film->internal_video_asset_filename(_period),
110                 _first_nonexistant_frame > 0
111                 );
112
113         if (_film->audio_channels ()) {
114                 _sound_asset.reset (
115                         new dcp::SoundAsset (dcp::Fraction (_film->video_frame_rate(), 1), _film->audio_frame_rate (), _film->audio_channels (), standard)
116                         );
117
118                 if (_film->encrypted ()) {
119                         _sound_asset->set_key (_film->key ());
120                 }
121
122                 DCPOMATIC_ASSERT (_film->directory());
123
124                 /* Write the sound asset into the film directory so that we leave the creation
125                    of the DCP directory until the last minute.
126                 */
127                 _sound_asset_writer = _sound_asset->start_write (
128                         _film->directory().get() / audio_asset_filename (_sound_asset, _reel_index, _reel_count, _content_summary)
129                         );
130         }
131 }
132
133 /** @param frame reel-relative frame */
134 void
135 ReelWriter::write_frame_info (Frame frame, Eyes eyes, dcp::FrameInfo info) const
136 {
137         FILE* file = 0;
138         boost::filesystem::path info_file = _film->info_file (_period);
139
140         bool const read = boost::filesystem::exists (info_file);
141
142         if (read) {
143                 file = fopen_boost (info_file, "r+b");
144         } else {
145                 file = fopen_boost (info_file, "wb");
146         }
147
148         if (!file) {
149                 throw OpenFileError (info_file, errno, read ? OpenFileError::READ_WRITE : OpenFileError::WRITE);
150         }
151         dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
152         checked_fwrite (&info.offset, sizeof (info.offset), file, info_file);
153         checked_fwrite (&info.size, sizeof (info.size), file, info_file);
154         checked_fwrite (info.hash.c_str(), info.hash.size(), file, info_file);
155         fclose (file);
156 }
157
158 dcp::FrameInfo
159 ReelWriter::read_frame_info (FILE* file, Frame frame, Eyes eyes) const
160 {
161         dcp::FrameInfo info;
162         dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
163         checked_fread (&info.offset, sizeof(info.offset), file, _film->info_file(_period));
164         checked_fread (&info.size, sizeof(info.size), file, _film->info_file(_period));
165
166         char hash_buffer[33];
167         checked_fread (hash_buffer, 32, file, _film->info_file(_period));
168         hash_buffer[32] = '\0';
169         info.hash = hash_buffer;
170
171         return info;
172 }
173
174 long
175 ReelWriter::frame_info_position (Frame frame, Eyes eyes) const
176 {
177         switch (eyes) {
178         case EYES_BOTH:
179                 return frame * _info_size;
180         case EYES_LEFT:
181                 return frame * _info_size * 2;
182         case EYES_RIGHT:
183                 return frame * _info_size * 2 + _info_size;
184         default:
185                 DCPOMATIC_ASSERT (false);
186         }
187
188         DCPOMATIC_ASSERT (false);
189 }
190
191 Frame
192 ReelWriter::check_existing_picture_asset ()
193 {
194         DCPOMATIC_ASSERT (_picture_asset->file());
195         boost::filesystem::path asset = _picture_asset->file().get();
196
197         /* If there is an existing asset, break any hard links to it as we are about to change its contents
198            (if only by changing the IDs); see #1126.
199         */
200
201         if (boost::filesystem::exists(asset) && boost::filesystem::hard_link_count(asset) > 1) {
202                 boost::filesystem::copy_file (asset, asset.string() + ".tmp");
203                 boost::filesystem::remove (asset);
204                 boost::filesystem::rename (asset.string() + ".tmp", asset);
205         }
206
207         /* Try to open the existing asset */
208         FILE* asset_file = fopen_boost (asset, "rb");
209         if (!asset_file) {
210                 LOG_GENERAL ("Could not open existing asset at %1 (errno=%2)", asset.string(), errno);
211                 return 0;
212         } else {
213                 LOG_GENERAL ("Opened existing asset at %1", asset.string());
214         }
215
216         /* Offset of the last dcp::FrameInfo in the info file */
217         int const n = (boost::filesystem::file_size (_film->info_file(_period)) / _info_size) - 1;
218         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);
219
220         FILE* info_file = fopen_boost (_film->info_file(_period), "rb");
221         if (!info_file) {
222                 LOG_GENERAL_NC ("Could not open film info file");
223                 fclose (asset_file);
224                 return 0;
225         }
226
227         Frame first_nonexistant_frame;
228         if (_film->three_d ()) {
229                 /* Start looking at the last left frame */
230                 first_nonexistant_frame = n / 2;
231         } else {
232                 first_nonexistant_frame = n;
233         }
234
235         while (!existing_picture_frame_ok(asset_file, info_file, first_nonexistant_frame) && first_nonexistant_frame > 0) {
236                 --first_nonexistant_frame;
237         }
238
239         if (!_film->three_d() && first_nonexistant_frame > 0) {
240                 /* If we are doing 3D we might have found a good L frame with no R, so only
241                    do this if we're in 2D and we've just found a good B(oth) frame.
242                 */
243                 ++first_nonexistant_frame;
244         }
245
246         LOG_GENERAL ("Proceeding with first nonexistant frame %1", first_nonexistant_frame);
247
248         fclose (asset_file);
249         fclose (info_file);
250
251         return first_nonexistant_frame;
252 }
253
254 void
255 ReelWriter::write (optional<Data> encoded, Frame frame, Eyes eyes)
256 {
257         dcp::FrameInfo fin = _picture_asset_writer->write (encoded->data().get (), encoded->size());
258         write_frame_info (frame, eyes, fin);
259         _last_written[eyes] = encoded;
260         _last_written_video_frame = frame;
261         _last_written_eyes = eyes;
262 }
263
264 void
265 ReelWriter::fake_write (Frame frame, Eyes eyes, int size)
266 {
267         _picture_asset_writer->fake_write (size);
268         _last_written_video_frame = frame;
269         _last_written_eyes = eyes;
270 }
271
272 void
273 ReelWriter::repeat_write (Frame frame, Eyes eyes)
274 {
275         dcp::FrameInfo fin = _picture_asset_writer->write (
276                 _last_written[eyes]->data().get(),
277                 _last_written[eyes]->size()
278                 );
279         write_frame_info (frame, eyes, fin);
280         _last_written_video_frame = frame;
281         _last_written_eyes = eyes;
282 }
283
284 void
285 ReelWriter::finish ()
286 {
287         if (!_picture_asset_writer->finalize ()) {
288                 /* Nothing was written to the picture asset */
289                 LOG_GENERAL ("Nothing was written to reel %1 of %2", _reel_index, _reel_count);
290                 _picture_asset.reset ();
291         }
292
293         if (_sound_asset_writer && !_sound_asset_writer->finalize ()) {
294                 /* Nothing was written to the sound asset */
295                 _sound_asset.reset ();
296         }
297
298         /* Hard-link any video asset file into the DCP */
299         if (_picture_asset) {
300                 DCPOMATIC_ASSERT (_picture_asset->file());
301                 boost::filesystem::path video_from = _picture_asset->file().get();
302                 boost::filesystem::path video_to;
303                 video_to /= _film->dir (_film->dcp_name());
304                 video_to /= video_asset_filename (_picture_asset, _reel_index, _reel_count, _content_summary);
305
306                 boost::system::error_code ec;
307                 boost::filesystem::create_hard_link (video_from, video_to, ec);
308                 if (ec) {
309                         LOG_WARNING_NC ("Hard-link failed; copying instead");
310                         boost::filesystem::copy_file (video_from, video_to, ec);
311                         if (ec) {
312                                 LOG_ERROR ("Failed to copy video file from %1 to %2 (%3)", video_from.string(), video_to.string(), ec.message ());
313                                 throw FileError (ec.message(), video_from);
314                         }
315                 }
316
317                 _picture_asset->set_file (video_to);
318         }
319
320         /* Move the audio asset into the DCP */
321         if (_sound_asset) {
322                 boost::filesystem::path audio_to;
323                 audio_to /= _film->dir (_film->dcp_name ());
324                 string const aaf = audio_asset_filename (_sound_asset, _reel_index, _reel_count, _content_summary);
325                 audio_to /= aaf;
326
327                 boost::system::error_code ec;
328                 boost::filesystem::rename (_film->file (aaf), audio_to, ec);
329                 if (ec) {
330                         throw FileError (
331                                 String::compose (_("could not move audio asset into the DCP (%1)"), ec.value ()), aaf
332                                 );
333                 }
334
335                 _sound_asset->set_file (audio_to);
336         }
337 }
338
339 template <class T>
340 shared_ptr<T>
341 maybe_add_text (
342         shared_ptr<dcp::SubtitleAsset> asset,
343         int64_t picture_duration,
344         shared_ptr<dcp::Reel> reel,
345         list<ReferencedReelAsset> const & refs,
346         list<shared_ptr<Font> > const & fonts,
347         shared_ptr<const Film> film,
348         DCPTimePeriod period
349         )
350 {
351         Frame const period_duration = period.duration().frames_round(film->video_frame_rate());
352
353         shared_ptr<T> reel_asset;
354
355         if (asset) {
356                 boost::filesystem::path liberation_normal;
357                 try {
358                         liberation_normal = shared_path() / "LiberationSans-Regular.ttf";
359                         if (!boost::filesystem::exists (liberation_normal)) {
360                                 /* Hack for unit tests */
361                                 liberation_normal = shared_path() / "fonts" / "LiberationSans-Regular.ttf";
362                         }
363                 } catch (boost::filesystem::filesystem_error& e) {
364
365                 }
366
367                 if (!boost::filesystem::exists(liberation_normal)) {
368                         liberation_normal = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf";
369                 }
370
371                 /* Add the font to the subtitle content */
372                 BOOST_FOREACH (shared_ptr<Font> j, fonts) {
373                         asset->add_font (j->id(), j->file().get_value_or(liberation_normal));
374                 }
375
376                 if (dynamic_pointer_cast<dcp::InteropSubtitleAsset> (asset)) {
377                         boost::filesystem::path directory = film->dir (film->dcp_name ()) / asset->id ();
378                         boost::filesystem::create_directories (directory);
379                         asset->write (directory / ("sub_" + asset->id() + ".xml"));
380                 } else {
381                         /* All our assets should be the same length; use the picture asset length here
382                            as a reference to set the subtitle one.  We'll use the duration rather than
383                            the intrinsic duration; we don't care if the picture asset has been trimmed, we're
384                            just interested in its presentation length.
385                         */
386                         dynamic_pointer_cast<dcp::SMPTESubtitleAsset>(asset)->set_intrinsic_duration (picture_duration);
387
388                         asset->write (
389                                 film->dir(film->dcp_name()) / ("sub_" + asset->id() + ".mxf")
390                                 );
391                 }
392
393                 reel_asset.reset (
394                         new T (
395                                 asset,
396                                 dcp::Fraction (film->video_frame_rate(), 1),
397                                 picture_duration,
398                                 0
399                                 )
400                         );
401         } else {
402                 /* We don't have a subtitle asset of our own; hopefully we have one to reference */
403                 BOOST_FOREACH (ReferencedReelAsset j, refs) {
404                         shared_ptr<T> k = dynamic_pointer_cast<T> (j.asset);
405                         if (k && j.period == period) {
406                                 reel_asset = k;
407                                 /* If we have a hash for this asset in the CPL, assume that it is correct */
408                                 if (k->hash()) {
409                                         k->asset_ref()->set_hash (k->hash().get());
410                                 }
411                         }
412                 }
413         }
414
415         if (reel_asset) {
416                 if (reel_asset->actual_duration() != period_duration) {
417                         throw ProgrammingError (
418                                 __FILE__, __LINE__,
419                                 String::compose ("%1 vs %2", reel_asset->actual_duration(), period_duration)
420                                 );
421                 }
422                 reel->add (reel_asset);
423         }
424
425         return reel_asset;
426 }
427
428 shared_ptr<dcp::Reel>
429 ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr<Font> > const & fonts)
430 {
431         shared_ptr<dcp::Reel> reel (new dcp::Reel ());
432
433         shared_ptr<dcp::ReelPictureAsset> reel_picture_asset;
434
435         if (_picture_asset) {
436                 /* We have made a picture asset of our own.  Put it into the reel */
437                 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (_picture_asset);
438                 if (mono) {
439                         reel_picture_asset.reset (new dcp::ReelMonoPictureAsset (mono, 0));
440                 }
441
442                 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (_picture_asset);
443                 if (stereo) {
444                         reel_picture_asset.reset (new dcp::ReelStereoPictureAsset (stereo, 0));
445                 }
446         } else {
447                 LOG_GENERAL ("no picture asset of our own; look through %1", refs.size());
448                 /* We don't have a picture asset of our own; hopefully we have one to reference */
449                 BOOST_FOREACH (ReferencedReelAsset j, refs) {
450                         shared_ptr<dcp::ReelPictureAsset> k = dynamic_pointer_cast<dcp::ReelPictureAsset> (j.asset);
451                         if (k) {
452                                 LOG_GENERAL ("candidate picture asset period is %1-%2", j.period.from.get(), j.period.to.get());
453                         }
454                         if (k && j.period == _period) {
455                                 reel_picture_asset = k;
456                         }
457                 }
458         }
459
460         LOG_GENERAL ("create_reel for %1-%2; %3 of %4", _period.from.get(), _period.to.get(), _reel_index, _reel_count);
461
462         Frame const period_duration = _period.duration().frames_round(_film->video_frame_rate());
463
464         DCPOMATIC_ASSERT (reel_picture_asset);
465         if (reel_picture_asset->duration() != period_duration) {
466                 throw ProgrammingError (
467                         __FILE__, __LINE__,
468                         String::compose ("%1 vs %2", reel_picture_asset->actual_duration(), period_duration)
469                         );
470         }
471         reel->add (reel_picture_asset);
472
473         /* If we have a hash for this asset in the CPL, assume that it is correct */
474         if (reel_picture_asset->hash()) {
475                 reel_picture_asset->asset_ref()->set_hash (reel_picture_asset->hash().get());
476         }
477
478         shared_ptr<dcp::ReelSoundAsset> reel_sound_asset;
479
480         if (_sound_asset) {
481                 /* We have made a sound asset of our own.  Put it into the reel */
482                 reel_sound_asset.reset (new dcp::ReelSoundAsset (_sound_asset, 0));
483         } else {
484                 /* We don't have a sound asset of our own; hopefully we have one to reference */
485                 BOOST_FOREACH (ReferencedReelAsset j, refs) {
486                         shared_ptr<dcp::ReelSoundAsset> k = dynamic_pointer_cast<dcp::ReelSoundAsset> (j.asset);
487                         if (k && j.period == _period) {
488                                 reel_sound_asset = k;
489                                 /* If we have a hash for this asset in the CPL, assume that it is correct */
490                                 if (k->hash()) {
491                                         k->asset_ref()->set_hash (k->hash().get());
492                                 }
493                         }
494                 }
495         }
496
497         DCPOMATIC_ASSERT (reel_sound_asset);
498         if (reel_sound_asset->actual_duration() != period_duration) {
499                 LOG_ERROR (
500                         "Reel sound asset has length %1 but reel period is %2",
501                         reel_sound_asset->actual_duration(),
502                         period_duration
503                         );
504                 if (reel_sound_asset->actual_duration() != period_duration) {
505                         throw ProgrammingError (
506                                 __FILE__, __LINE__,
507                                 String::compose ("%1 vs %2", reel_sound_asset->actual_duration(), period_duration)
508                                 );
509                 }
510
511         }
512         reel->add (reel_sound_asset);
513
514         maybe_add_text<dcp::ReelSubtitleAsset> (_subtitle_asset, reel_picture_asset->actual_duration(), reel, refs, fonts, _film, _period);
515         for (map<DCPTextTrack, shared_ptr<dcp::SubtitleAsset> >::const_iterator i = _closed_caption_assets.begin(); i != _closed_caption_assets.end(); ++i) {
516                 shared_ptr<dcp::ReelClosedCaptionAsset> a = maybe_add_text<dcp::ReelClosedCaptionAsset> (
517                         i->second, reel_picture_asset->actual_duration(), reel, refs, fonts, _film, _period
518                         );
519                 a->set_annotation_text (i->first.name);
520                 a->set_language (i->first.language);
521         }
522
523         map<dcp::Marker, DCPTime> markers = _film->markers ();
524         map<dcp::Marker, DCPTime> reel_markers;
525         for (map<dcp::Marker, DCPTime>::const_iterator i = markers.begin(); i != markers.end(); ++i) {
526                 if (_period.contains(i->second)) {
527                         reel_markers[i->first] = i->second;
528                 }
529         }
530
531         if (!reel_markers.empty ()) {
532                 shared_ptr<dcp::ReelMarkersAsset> ma (new dcp::ReelMarkersAsset(dcp::Fraction(_film->video_frame_rate(), 1), 0));
533                 for (map<dcp::Marker, DCPTime>::const_iterator i = reel_markers.begin(); i != reel_markers.end(); ++i) {
534                         int h, m, s, f;
535                         DCPTime relative = i->second - _period.from;
536                         relative.split (_film->video_frame_rate(), h, m, s, f);
537                         ma->set (i->first, dcp::Time(h, m, s, f, _film->video_frame_rate()));
538                 }
539                 reel->add (ma);
540         }
541
542         return reel;
543 }
544
545 void
546 ReelWriter::calculate_digests (boost::function<void (float)> set_progress)
547 {
548         if (_picture_asset) {
549                 _picture_asset->hash (set_progress);
550         }
551
552         if (_sound_asset) {
553                 _sound_asset->hash (set_progress);
554         }
555 }
556
557 Frame
558 ReelWriter::start () const
559 {
560         return _period.from.frames_floor (_film->video_frame_rate());
561 }
562
563
564 void
565 ReelWriter::write (shared_ptr<const AudioBuffers> audio)
566 {
567         if (!_sound_asset_writer) {
568                 return;
569         }
570
571         DCPOMATIC_ASSERT (audio);
572         _sound_asset_writer->write (audio->data(), audio->frames());
573 }
574
575 void
576 ReelWriter::write (PlayerText subs, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
577 {
578         shared_ptr<dcp::SubtitleAsset> asset;
579
580         switch (type) {
581         case TEXT_OPEN_SUBTITLE:
582                 asset = _subtitle_asset;
583                 break;
584         case TEXT_CLOSED_CAPTION:
585                 DCPOMATIC_ASSERT (track);
586                 asset = _closed_caption_assets[*track];
587                 break;
588         default:
589                 DCPOMATIC_ASSERT (false);
590         }
591
592         if (!asset) {
593                 string lang = _film->subtitle_language ();
594                 if (_film->interop ()) {
595                         shared_ptr<dcp::InteropSubtitleAsset> s (new dcp::InteropSubtitleAsset ());
596                         s->set_movie_title (_film->name ());
597                         if (type == TEXT_OPEN_SUBTITLE) {
598                                 s->set_language (lang.empty() ? "Unknown" : lang);
599                         } else {
600                                 s->set_language (track->language);
601                         }
602                         s->set_reel_number (raw_convert<string> (_reel_index + 1));
603                         asset = s;
604                 } else {
605                         shared_ptr<dcp::SMPTESubtitleAsset> s (new dcp::SMPTESubtitleAsset ());
606                         s->set_content_title_text (_film->name ());
607                         if (type == TEXT_OPEN_SUBTITLE && !lang.empty()) {
608                                 s->set_language (lang);
609                         } else {
610                                 s->set_language (track->language);
611                         }
612                         s->set_edit_rate (dcp::Fraction (_film->video_frame_rate (), 1));
613                         s->set_reel_number (_reel_index + 1);
614                         s->set_time_code_rate (_film->video_frame_rate ());
615                         s->set_start_time (dcp::Time ());
616                         if (_film->encrypted ()) {
617                                 s->set_key (_film->key ());
618                         }
619                         asset = s;
620                 }
621         }
622
623         switch (type) {
624         case TEXT_OPEN_SUBTITLE:
625                 _subtitle_asset = asset;
626                 break;
627         case TEXT_CLOSED_CAPTION:
628                 DCPOMATIC_ASSERT (track);
629                 _closed_caption_assets[*track] = asset;
630                 break;
631         default:
632                 DCPOMATIC_ASSERT (false);
633         }
634
635         BOOST_FOREACH (StringText i, subs.string) {
636                 /* XXX: couldn't / shouldn't we use period here rather than getting time from the subtitle? */
637                 i.set_in  (i.in()  - dcp::Time (_period.from.seconds(), i.in().tcr));
638                 i.set_out (i.out() - dcp::Time (_period.from.seconds(), i.out().tcr));
639                 asset->add (shared_ptr<dcp::Subtitle>(new dcp::SubtitleString(i)));
640         }
641
642         BOOST_FOREACH (BitmapText i, subs.bitmap) {
643                 asset->add (
644                         shared_ptr<dcp::Subtitle>(
645                                 new dcp::SubtitleImage(
646                                         i.image->as_png(),
647                                         dcp::Time(period.from.seconds() - _period.from.seconds(), _film->video_frame_rate()),
648                                         dcp::Time(period.to.seconds() - _period.from.seconds(), _film->video_frame_rate()),
649                                         i.rectangle.x, dcp::HALIGN_LEFT, i.rectangle.y, dcp::VALIGN_TOP,
650                                         dcp::Time(), dcp::Time()
651                                         )
652                                 )
653                         );
654         }
655 }
656
657 bool
658 ReelWriter::existing_picture_frame_ok (FILE* asset_file, FILE* info_file, Frame frame) const
659 {
660         LOG_GENERAL ("Checking existing picture frame %1", frame);
661
662         /* Read the data from the info file; for 3D we just check the left
663            frames until we find a good one.
664         */
665         dcp::FrameInfo const info = read_frame_info (info_file, frame, _film->three_d () ? EYES_LEFT : EYES_BOTH);
666
667         bool ok = true;
668
669         /* Read the data from the asset and hash it */
670         dcpomatic_fseek (asset_file, info.offset, SEEK_SET);
671         Data data (info.size);
672         size_t const read = fread (data.data().get(), 1, data.size(), asset_file);
673         LOG_GENERAL ("Read %1 bytes of asset data; wanted %2", read, info.size);
674         if (read != static_cast<size_t> (data.size ())) {
675                 LOG_GENERAL ("Existing frame %1 is incomplete", frame);
676                 ok = false;
677         } else {
678                 Digester digester;
679                 digester.add (data.data().get(), data.size());
680                 LOG_GENERAL ("Hash %1 vs %2", digester.get(), info.hash);
681                 if (digester.get() != info.hash) {
682                         LOG_GENERAL ("Existing frame %1 failed hash check", frame);
683                         ok = false;
684                 }
685         }
686
687         return ok;
688 }