2 Copyright (C) 2012 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 <libdcp/picture_asset.h>
22 #include <libdcp/sound_asset.h>
23 #include <libdcp/picture_frame.h>
24 #include <libdcp/reel.h>
26 #include "compose.hpp"
30 #include "dcp_video_frame.h"
38 using boost::shared_ptr;
40 int const Writer::_maximum_frames_in_memory = 8;
42 Writer::Writer (shared_ptr<Film> f)
44 , _first_nonexistant_frame (0)
47 , _queued_full_in_memory (0)
48 , _last_written_frame (-1)
54 /* Remove any old DCP */
55 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
57 check_existing_picture_mxf ();
59 /* Create our picture asset in a subdirectory, named according to those
60 film's parameters which affect the video output. We will hard-link
61 it into the DCP later.
64 _picture_asset.reset (
65 new libdcp::MonoPictureAsset (
66 _film->video_mxf_dir (),
67 _film->video_mxf_filename (),
68 DCPFrameRate (_film->frames_per_second()).frames_per_second,
69 _film->format()->dcp_size()
73 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
75 if (_film->audio_channels() > 0) {
77 new libdcp::SoundAsset (
78 _film->dir (_film->dcp_name()),
80 DCPFrameRate (_film->frames_per_second()).frames_per_second,
81 _film->audio_channels(),
82 dcp_audio_sample_rate (_film->audio_stream()->sample_rate())
86 _sound_asset_writer = _sound_asset->start_write ();
89 _thread = new boost::thread (boost::bind (&Writer::thread, this));
93 Writer::write (shared_ptr<const EncodedData> encoded, int frame)
95 boost::mutex::scoped_lock lock (_mutex);
98 qi.type = QueueItem::FULL;
101 _queue.push_back (qi);
102 ++_queued_full_in_memory;
104 _condition.notify_all ();
108 Writer::fake_write (int frame)
110 boost::mutex::scoped_lock lock (_mutex);
112 ifstream ifi (_film->info_path (frame).c_str());
113 libdcp::FrameInfo info (ifi);
116 qi.type = QueueItem::FAKE;
119 _queue.push_back (qi);
121 _condition.notify_all ();
124 /** This method is not thread safe */
126 Writer::write (shared_ptr<const AudioBuffers> audio)
128 _sound_asset_writer->write (audio->data(), audio->frames());
136 boost::mutex::scoped_lock lock (_mutex);
143 _queued_full_in_memory > _maximum_frames_in_memory ||
144 (!_queue.empty() && _queue.front().frame == (_last_written_frame + 1))) {
149 TIMING ("writer sleeps with a queue of %1", _queue.size());
150 _condition.wait (lock);
151 TIMING ("writer wakes with a queue of %1", _queue.size());
154 if (_finish && _queue.empty()) {
158 /* Write any frames that we can write; i.e. those that are in sequence */
159 while (!_queue.empty() && _queue.front().frame == (_last_written_frame + 1)) {
160 QueueItem qi = _queue.front ();
162 if (qi.type == QueueItem::FULL && qi.encoded) {
163 --_queued_full_in_memory;
167 case QueueItem::FULL:
170 case QueueItem::FAKE:
173 case QueueItem::REPEAT:
180 case QueueItem::FULL:
182 _film->log()->log (String::compose ("Writer FULL-writes %1 to MXF", qi.frame));
184 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, false)));
186 libdcp::FrameInfo const fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
187 qi.encoded->write_info (_film, qi.frame, fin);
188 _last_written = qi.encoded;
191 case QueueItem::FAKE:
192 _film->log()->log (String::compose ("Writer FAKE-writes %1 to MXF", qi.frame));
193 _picture_asset_writer->fake_write (qi.size);
194 _last_written.reset ();
196 case QueueItem::REPEAT:
198 _film->log()->log (String::compose ("Writer REPEAT-writes %1 to MXF", qi.frame));
199 libdcp::FrameInfo const fin = _picture_asset_writer->write (_last_written->data(), _last_written->size());
200 _last_written->write_info (_film, qi.frame, fin);
206 ++_last_written_frame;
209 while (_queued_full_in_memory > _maximum_frames_in_memory) {
210 /* Too many frames in memory which can't yet be written to the stream.
211 Write some FULL frames to disk.
215 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
216 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
220 assert (i != _queue.rend());
226 _film->log()->log (String::compose ("Writer full (awaiting %1); pushes %2 to disk", _last_written_frame + 1, qi.frame));
227 qi.encoded->write (_film, qi.frame);
230 --_queued_full_in_memory;
243 boost::mutex::scoped_lock lock (_mutex);
245 _condition.notify_all ();
252 _picture_asset_writer->finalize ();
254 if (_sound_asset_writer) {
255 _sound_asset_writer->finalize ();
258 int const frames = _last_written_frame + 1;
259 int const duration = frames - _film->trim_start() - _film->trim_end();
261 _film->set_dcp_intrinsic_duration (frames);
263 _picture_asset->set_entry_point (_film->trim_start ());
264 _picture_asset->set_duration (duration);
266 /* Hard-link the video MXF into the DCP */
268 boost::filesystem::path from;
269 from /= _film->video_mxf_dir();
270 from /= _film->video_mxf_filename();
272 boost::filesystem::path to;
273 to /= _film->dir (_film->dcp_name());
276 boost::filesystem::create_hard_link (from, to);
278 /* And update the asset */
280 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
281 _picture_asset->set_file_name ("video.mxf");
284 _sound_asset->set_entry_point (_film->trim_start ());
285 _sound_asset->set_duration (duration);
288 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
289 DCPFrameRate dfr (_film->frames_per_second ());
291 shared_ptr<libdcp::CPL> cpl (
292 new libdcp::CPL (_film->dir (_film->dcp_name()), _film->dcp_name(), _film->dcp_content_type()->libdcp_kind (), frames, dfr.frames_per_second)
297 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
300 shared_ptr<libdcp::SubtitleAsset> ()
306 _film->log()->log (String::compose ("Wrote %1 FULL, %2 FAKE, %3 REPEAT; %4 pushed to disk", _full_written, _fake_written, _repeat_written, _pushed_to_disk));
309 /** Tell the writer that frame `f' should be a repeat of the frame before it */
311 Writer::repeat (int f)
313 boost::mutex::scoped_lock lock (_mutex);
316 qi.type = QueueItem::REPEAT;
319 _queue.push_back (qi);
321 _condition.notify_all ();
326 Writer::check_existing_picture_mxf ()
328 /* Try to open the existing MXF */
329 boost::filesystem::path p;
330 p /= _film->video_mxf_dir ();
331 p /= _film->video_mxf_filename ();
332 FILE* mxf = fopen (p.string().c_str(), "rb");
339 /* Read the frame info as written */
340 ifstream ifi (_film->info_path (_first_nonexistant_frame).c_str());
341 libdcp::FrameInfo info (ifi);
343 /* Read the data from the MXF and hash it */
344 fseek (mxf, info.offset, SEEK_SET);
345 EncodedData data (info.size);
346 fread (data.data(), 1, data.size(), mxf);
347 string const existing_hash = md5_digest (data.data(), data.size());
349 if (existing_hash != info.hash) {
350 _film->log()->log (String::compose ("Existing frame %1 failed hash check", _first_nonexistant_frame));
354 _film->log()->log (String::compose ("Have existing frame %1", _first_nonexistant_frame));
355 ++_first_nonexistant_frame;
361 /** @return true if the fake write succeeded, otherwise false */
363 Writer::can_fake_write (int frame) const
365 return (frame != 0 && frame < _first_nonexistant_frame);
370 operator< (QueueItem const & a, QueueItem const & b)
372 return a.frame < b.frame;
376 operator== (QueueItem const & a, QueueItem const & b)
378 return a.frame == b.frame;