aab7d5fc78da6e1d5c4211093c34c04b2781d2ea
[dcpomatic.git] / src / lib / writer.h
1 /*
2     Copyright (C) 2012-2021 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
22 /** @file  src/lib/writer.h
23  *  @brief Writer class.
24  */
25
26
27 #include "atmos_metadata.h"
28 #include "types.h"
29 #include "player_text.h"
30 #include "exception_store.h"
31 #include "dcp_text_track.h"
32 #include "weak_film.h"
33 #include <dcp/atmos_frame.h>
34 #include <boost/thread.hpp>
35 #include <boost/thread/condition.hpp>
36 #include <list>
37
38
39 namespace dcp {
40         class Data;
41 }
42
43 namespace dcpomatic {
44         class FontData;
45 }
46
47 class Film;
48 class AudioBuffers;
49 class Job;
50 class ReferencedReelAsset;
51 class ReelWriter;
52
53
54 struct QueueItem
55 {
56 public:
57         QueueItem () {}
58
59         enum class Type {
60                 /** a normal frame with some JPEG200 data */
61                 FULL,
62                 /** a frame whose data already exists in the MXF,
63                     and we fake-write it; i.e. we update the writer's
64                     state but we use the data that is already on disk.
65                 */
66                 FAKE,
67                 REPEAT,
68         } type;
69
70         /** encoded data for FULL */
71         std::shared_ptr<const dcp::Data> encoded;
72         /** size of data for FAKE */
73         int size = 0;
74         /** reel index */
75         size_t reel = 0;
76         /** frame index within the reel */
77         int frame = 0;
78         /** eyes for FULL, FAKE and REPEAT */
79         Eyes eyes = Eyes::BOTH;
80 };
81
82
83 bool operator< (QueueItem const & a, QueueItem const & b);
84 bool operator== (QueueItem const & a, QueueItem const & b);
85
86
87 /** @class Writer
88  *  @brief Class to manage writing JPEG2000 and audio data to assets on disk.
89  *
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.
93  *
94  *  write() for Data (picture) can be called out of order, and the Writer
95  *  will sort it out.  write() for AudioBuffers must be called in order.
96  */
97
98 class Writer : public ExceptionStore, public WeakConstFilm
99 {
100 public:
101         Writer (std::weak_ptr<const Film>, std::weak_ptr<Job>, bool text_only = false);
102         ~Writer ();
103
104         Writer (Writer const &) = delete;
105         Writer& operator= (Writer const &) = delete;
106
107         void start ();
108
109         bool can_fake_write (Frame) const;
110
111         void write (std::shared_ptr<const dcp::Data>, Frame, Eyes);
112         void fake_write (Frame, Eyes);
113         bool can_repeat (Frame) const;
114         void repeat (Frame, Eyes);
115         void write (std::shared_ptr<const AudioBuffers>, dcpomatic::DCPTime time);
116         void write (PlayerText text, TextType type, boost::optional<DCPTextTrack>, dcpomatic::DCPTimePeriod period);
117         void write (std::vector<dcpomatic::FontData> fonts);
118         void write (ReferencedReelAsset asset);
119         void write (std::shared_ptr<const dcp::AtmosFrame> atmos, dcpomatic::DCPTime time, AtmosMetadata metadata);
120         void finish (boost::filesystem::path output_dcp);
121
122         void set_encoder_threads (int threads);
123
124 private:
125         void thread ();
126         void terminate_thread (bool);
127         bool have_sequenced_image_at_queue_head ();
128         size_t video_reel (int frame) const;
129         void set_digest_progress (Job* job, float progress);
130         void write_cover_sheet (boost::filesystem::path output_dcp);
131         void calculate_referenced_digests (boost::function<void (float)> set_progress);
132         void write_hanging_text (ReelWriter& reel);
133
134         std::weak_ptr<Job> _job;
135         std::vector<ReelWriter> _reels;
136         std::vector<ReelWriter>::iterator _audio_reel;
137         std::vector<ReelWriter>::iterator _subtitle_reel;
138         std::map<DCPTextTrack, std::vector<ReelWriter>::iterator> _caption_reels;
139         std::vector<ReelWriter>::iterator _atmos_reel;
140
141         /** our thread */
142         boost::thread _thread;
143         /** true if our thread should finish */
144         bool _finish = false;
145         /** queue of things to write to disk */
146         std::list<QueueItem> _queue;
147         /** number of FULL frames whose JPEG200 data is currently held in RAM */
148         int _queued_full_in_memory = 0;
149         /** mutex for thread state */
150         mutable boost::mutex _state_mutex;
151         /** condition to manage thread wakeups when we have nothing to do  */
152         boost::condition _empty_condition;
153         /** condition to manage thread wakeups when we have too much to do */
154         boost::condition _full_condition;
155         /** maximum number of frames to hold in memory, for when we are managing
156          *  ordering
157          */
158         int _maximum_frames_in_memory;
159         unsigned int _maximum_queue_size;
160
161         class LastWritten
162         {
163         public:
164                 LastWritten()
165                         : _frame(-1)
166                         , _eyes(Eyes::RIGHT)
167                 {}
168
169                 /** @return true if qi is the next item after this one */
170                 bool next (QueueItem qi) const;
171                 void update (QueueItem qi);
172
173                 int frame () const {
174                         return _frame;
175                 }
176
177         private:
178                 int _frame;
179                 Eyes _eyes;
180         };
181
182         /** The last frame written to each reel */
183         std::vector<LastWritten> _last_written;
184
185         /** number of FULL written frames */
186         int _full_written = 0;
187         /** number of FAKE written frames */
188         int _fake_written = 0;
189         int _repeat_written = 0;
190         /** number of frames pushed to disk and then recovered
191             due to the limit of frames to be held in memory.
192         */
193         int _pushed_to_disk = 0;
194
195         bool _text_only;
196
197         boost::mutex _digest_progresses_mutex;
198         std::map<boost::thread::id, float> _digest_progresses;
199
200         std::list<ReferencedReelAsset> _reel_assets;
201
202         std::vector<dcpomatic::FontData> _fonts;
203
204         /** true if any reel has any subtitles */
205         bool _have_subtitles = false;
206         /** all closed caption tracks that we have on any reel */
207         std::set<DCPTextTrack> _have_closed_captions;
208
209         struct HangingText {
210                 PlayerText text;
211                 TextType type;
212                 boost::optional<DCPTextTrack> track;
213                 dcpomatic::DCPTimePeriod period;
214         };
215
216         std::vector<HangingText> _hanging_texts;
217 };