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