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.
20 /** @file src/lib/writer.h
21 * @brief Writer class.
25 #include "player_subtitles.h"
27 #include "exception_store.h"
28 #include <dcp/picture_asset_writer.h>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/weak_ptr.hpp>
31 #include <boost/thread.hpp>
32 #include <boost/thread/condition.hpp>
40 class ReferencedReelAsset;
43 class MonoPictureAsset;
44 class MonoPictureAssetWriter;
45 class StereoPictureAsset;
46 class StereoPictureAssetWriter;
48 class PictureAssetWriter;
50 class SoundAssetWriter;
63 /** a normal frame with some JPEG200 data */
65 /** a frame whose data already exists in the MXF,
66 and we fake-write it; i.e. we update the writer's
67 state but we use the data that is already on disk.
74 /** encoded data for FULL */
75 boost::optional<Data> encoded;
76 /** size of data for FAKE */
80 /** eyes for FULL, FAKE and REPEAT */
84 bool operator< (QueueItem const & a, QueueItem const & b);
85 bool operator== (QueueItem const & a, QueueItem const & b);
88 * @brief Class to manage writing JPEG2000 and audio data to assets on disk.
90 * This class creates sound and picture assets, then takes Data
91 * or AudioBuffers objects (containing image or sound data respectively)
92 * and writes them to the assets.
94 * ::write() for Data can be called out of order, and the Writer
95 * will sort it out. write() for AudioBuffers must be called in order.
98 class Writer : public ExceptionStore, public boost::noncopyable
101 Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
106 bool can_fake_write (Frame) const;
108 void write (Data, Frame, Eyes);
109 void fake_write (Frame, Eyes);
110 void ref_write (Frame);
111 void repeat (Frame, Eyes);
112 void write (boost::shared_ptr<const AudioBuffers>);
113 void write (PlayerSubtitles subs);
114 void write (std::list<boost::shared_ptr<Font> > fonts);
115 void write (ReferencedReelAsset asset);
118 void set_encoder_threads (int threads);
125 : first_nonexistant_frame (0)
129 DCPTimePeriod period;
130 /** the first frame index that does not already exist in our MXF */
131 int first_nonexistant_frame;
134 boost::shared_ptr<dcp::PictureAsset> picture_asset;
135 boost::shared_ptr<dcp::PictureAssetWriter> picture_asset_writer;
136 boost::shared_ptr<dcp::SoundAsset> sound_asset;
137 boost::shared_ptr<dcp::SoundAssetWriter> sound_asset_writer;
138 boost::shared_ptr<dcp::SubtitleAsset> subtitle_asset;
142 void terminate_thread (bool);
143 void check_existing_picture_asset (Reel& reel);
144 bool have_sequenced_image_at_queue_head ();
145 void write_frame_info (int frame, Eyes eyes, dcp::FrameInfo info) const;
146 long frame_info_position (int frame, Eyes eyes) const;
147 dcp::FrameInfo read_frame_info (FILE* file, int frame, Eyes eyes) const;
148 Reel const & video_reel (int frame) const;
151 boost::shared_ptr<const Film> _film;
152 boost::weak_ptr<Job> _job;
153 std::list<Reel> _reels;
154 std::list<Reel>::iterator _audio_reel;
155 std::list<Reel>::iterator _subtitle_reel;
157 /** our thread, or 0 */
158 boost::thread* _thread;
159 /** true if our thread should finish */
161 /** queue of things to write to disk */
162 std::list<QueueItem> _queue;
163 /** number of FULL frames whose JPEG200 data is currently held in RAM */
164 int _queued_full_in_memory;
165 /** mutex for thread state */
166 mutable boost::mutex _state_mutex;
167 /** condition to manage thread wakeups when we have nothing to do */
168 boost::condition _empty_condition;
169 /** condition to manage thread wakeups when we have too much to do */
170 boost::condition _full_condition;
171 /** the data of the last written frame, if there is one */
172 boost::optional<Data> _last_written[EYES_COUNT];
173 /** the index of the last written frame */
174 int _last_written_frame;
175 Eyes _last_written_eyes;
176 /** maximum number of frames to hold in memory, for when we are managing
179 int _maximum_frames_in_memory;
181 /** number of FULL written frames */
183 /** number of FAKE written frames */
187 /** number of frames pushed to disk and then recovered
188 due to the limit of frames to be held in memory.
192 std::list<ReferencedReelAsset> _reel_assets;
194 std::list<boost::shared_ptr<Font> > _fonts;
196 static int const _info_size;