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