Only add fonts to assets when they are required.
[dcpomatic.git] / src / lib / reel_writer.h
1 /*
2     Copyright (C) 2012-2020 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 #include "atmos_metadata.h"
23 #include "dcp_text_track.h"
24 #include "dcpomatic_time.h"
25 #include "enum_indexed_vector.h"
26 #include "font_id_map.h"
27 #include "player_text.h"
28 #include "referenced_reel_asset.h"
29 #include "render_text.h"
30 #include "weak_film.h"
31 #include <dcp/atmos_asset_writer.h>
32 #include <dcp/file.h>
33 #include <dcp/picture_asset_writer.h>
34
35
36 class AudioBuffers;
37 class Film;
38 class InfoFileHandle;
39 class Job;
40 struct write_frame_info_test;
41
42 namespace dcp {
43         class AtmosAsset;
44         class MonoPictureAsset;
45         class MonoPictureAssetWriter;
46         class PictureAsset;
47         class PictureAssetWriter;
48         class Reel;
49         class ReelAsset;
50         class ReelPictureAsset;
51         class SoundAsset;
52         class SoundAssetWriter;
53         class StereoPictureAsset;
54         class StereoPictureAssetWriter;
55         class SubtitleAsset;
56 }
57
58
59 class ReelWriter : public WeakConstFilm
60 {
61 public:
62         ReelWriter (
63                 std::weak_ptr<const Film> film,
64                 dcpomatic::DCPTimePeriod period,
65                 std::shared_ptr<Job> job,
66                 int reel_index,
67                 int reel_count,
68                 bool text_only
69                 );
70
71         void write (std::shared_ptr<const dcp::Data> encoded, Frame frame, Eyes eyes);
72         void fake_write (int size);
73         void repeat_write (Frame frame, Eyes eyes);
74         void write (std::shared_ptr<const AudioBuffers> audio);
75         void write(PlayerText text, TextType type, boost::optional<DCPTextTrack> track, dcpomatic::DCPTimePeriod period, FontIdMap const& fonts, std::shared_ptr<dcpomatic::Font> chosen_interop_font);
76         void write (std::shared_ptr<const dcp::AtmosFrame> atmos, AtmosMetadata metadata);
77
78         void finish (boost::filesystem::path output_dcp);
79         std::shared_ptr<dcp::Reel> create_reel (
80                 std::list<ReferencedReelAsset> const & refs,
81                 boost::filesystem::path output_dcp,
82                 bool ensure_subtitles,
83                 std::set<DCPTextTrack> ensure_closed_captions
84                 );
85         void calculate_digests (std::function<void (float)> set_progress);
86
87         Frame start () const;
88
89         dcpomatic::DCPTimePeriod period () const {
90                 return _period;
91         }
92
93         int first_nonexistent_frame () const {
94                 return _first_nonexistent_frame;
95         }
96
97         dcp::FrameInfo read_frame_info (std::shared_ptr<InfoFileHandle> info, Frame frame, Eyes eyes) const;
98
99 private:
100
101         friend struct ::write_frame_info_test;
102
103         void write_frame_info (Frame frame, Eyes eyes, dcp::FrameInfo info) const;
104         long frame_info_position (Frame frame, Eyes eyes) const;
105         Frame check_existing_picture_asset (boost::filesystem::path asset);
106         bool existing_picture_frame_ok (dcp::File& asset_file, std::shared_ptr<InfoFileHandle> info_file, Frame frame) const;
107         std::shared_ptr<dcp::SubtitleAsset> empty_text_asset (TextType type, boost::optional<DCPTextTrack> track, bool with_dummy) const;
108
109         std::shared_ptr<dcp::ReelPictureAsset> create_reel_picture (std::shared_ptr<dcp::Reel> reel, std::list<ReferencedReelAsset> const & refs) const;
110         void create_reel_sound (std::shared_ptr<dcp::Reel> reel, std::list<ReferencedReelAsset> const & refs) const;
111         void create_reel_text (
112                 std::shared_ptr<dcp::Reel> reel,
113                 std::list<ReferencedReelAsset> const & refs,
114                 int64_t duration,
115                 boost::filesystem::path output_dcp,
116                 bool ensure_subtitles,
117                 std::set<DCPTextTrack> ensure_closed_captions
118                 ) const;
119         void create_reel_markers (std::shared_ptr<dcp::Reel> reel) const;
120         float convert_vertical_position(StringText const& subtitle, dcp::SubtitleStandard to) const;
121
122         dcpomatic::DCPTimePeriod _period;
123         /** the first picture frame index that does not already exist in our MXF */
124         int _first_nonexistent_frame;
125         /** the data of the last written frame, if there is one */
126         EnumIndexedVector<std::shared_ptr<const dcp::Data>, Eyes> _last_written;
127         /** index of this reel within the DCP (starting from 0) */
128         int _reel_index;
129         /** number of reels in the DCP */
130         int _reel_count;
131         boost::optional<std::string> _content_summary;
132         std::weak_ptr<Job> _job;
133         bool _text_only;
134
135         dcp::ArrayData _default_font;
136
137         std::shared_ptr<dcp::PictureAsset> _picture_asset;
138         /** picture asset writer, or 0 if we are not writing any picture because we already have one */
139         std::shared_ptr<dcp::PictureAssetWriter> _picture_asset_writer;
140         std::shared_ptr<dcp::SoundAsset> _sound_asset;
141         std::shared_ptr<dcp::SoundAssetWriter> _sound_asset_writer;
142         std::shared_ptr<dcp::SubtitleAsset> _subtitle_asset;
143         std::map<DCPTextTrack, std::shared_ptr<dcp::SubtitleAsset>> _closed_caption_assets;
144         std::shared_ptr<dcp::AtmosAsset> _atmos_asset;
145         std::shared_ptr<dcp::AtmosAssetWriter> _atmos_asset_writer;
146
147         mutable FontMetrics _font_metrics;
148
149         static int const _info_size;
150 };