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.
20 #include <libdcp/picture_asset.h>
22 #include "compose.hpp"
26 #include "dcp_video_frame.h"
30 using boost::shared_ptr;
32 unsigned int const Writer::_maximum_frames_in_memory = 8;
34 Writer::Writer (shared_ptr<const Film> f)
38 , _last_written_frame (-1)
40 _picture_asset.reset (
41 new libdcp::MonoPictureAsset (
42 _film->dir (_film->dcp_name()),
43 String::compose ("video_%1.mxf", 0),
44 DCPFrameRate (_film->frames_per_second()).frames_per_second,
45 _film->format()->dcp_size()
49 _picture_asset_writer = _picture_asset->start_write ();
51 _thread = new boost::thread (boost::bind (&Writer::thread, this));
55 Writer::write (shared_ptr<EncodedData> encoded, int frame)
57 boost::mutex::scoped_lock lock (_mutex);
58 _queue.push_back (make_pair (encoded, frame));
59 _condition.notify_all ();
64 bool operator() (pair<shared_ptr<EncodedData>, int> const & a, pair<shared_ptr<EncodedData>, int> const & b) {
65 return a.second < b.second;
74 boost::mutex::scoped_lock lock (_mutex);
78 _queue.size() > _maximum_frames_in_memory ||
79 (!_queue.empty() && _queue.front().second == (_last_written_frame + 1))) {
84 TIMING ("writer sleeps with a queue of %1; %2 pending", _queue.size(), _pending.size());
85 _condition.wait (lock);
86 TIMING ("writer wakes with a queue of %1", _queue.size());
88 _queue.sort (QueueSorter ());
91 if (_finish && _queue.empty() && _pending.empty()) {
95 /* Write any frames that we can write; i.e. those that are in sequence */
96 while (!_queue.empty() && _queue.front().second == (_last_written_frame + 1)) {
97 pair<boost::shared_ptr<EncodedData>, int> encoded = _queue.front ();
101 _film->log()->log (String::compose ("Writer writes %1 to MXF", encoded.second));
103 _picture_asset_writer->write (encoded.first->data(), encoded.first->size());
104 _last_written = encoded.first;
106 _picture_asset_writer->write (_last_written->data(), _last_written->size());
110 ++_last_written_frame;
113 while (_queue.size() > _maximum_frames_in_memory) {
114 /* Too many frames in memory which can't yet be written to the stream.
118 pair<boost::shared_ptr<EncodedData>, int> encoded = _queue.back ();
120 if (!encoded.first) {
121 /* This is a `repeat-last' frame, so no need to write it to disk */
126 _film->log()->log (String::compose ("Writer full (awaiting %1); pushes %2 to disk", _last_written_frame + 1, encoded.second));
127 encoded.first->write (_film, encoded.second);
130 _pending.push_back (encoded.second);
133 while (_queue.size() < _maximum_frames_in_memory && !_pending.empty()) {
134 /* We have some space in memory. Fetch some frames back off disk. */
137 int const fetch = _pending.front ();
140 _film->log()->log (String::compose ("Writer pulls %1 back from disk", fetch));
141 shared_ptr<EncodedData> encoded;
142 if (boost::filesystem::exists (_film->frame_out_path (fetch, false))) {
143 /* It's an actual frame (not a repeat-last); load it in */
144 encoded.reset (new EncodedData (_film->frame_out_path (fetch, false)));
148 _queue.push_back (make_pair (encoded, fetch));
149 _pending.remove (fetch);
162 boost::mutex::scoped_lock lock (_mutex);
164 _condition.notify_all ();
171 _picture_asset_writer->finalize ();
174 /** Tell the writer that frame `f' should be a repeat of the frame before it */
176 Writer::repeat (int f)
178 boost::mutex::scoped_lock lock (_mutex);
179 _queue.push_back (make_pair (shared_ptr<EncodedData> (), f));