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