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