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