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