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