Use libdcp's compress_j2k; move Data into libdcp.
[dcpomatic.git] / src / lib / writer.h
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/lib/writer.h
21  *  @brief Writer class.
22  */
23
24 #include "types.h"
25 #include "player_subtitles.h"
26 #include "exception_store.h"
27 #include <boost/shared_ptr.hpp>
28 #include <boost/weak_ptr.hpp>
29 #include <boost/thread.hpp>
30 #include <boost/thread/condition.hpp>
31 #include <list>
32
33 namespace dcp {
34         class Data;
35 }
36
37 class Film;
38 class AudioBuffers;
39 class Job;
40 class Font;
41 class ReferencedReelAsset;
42 class ReelWriter;
43
44 struct QueueItem
45 {
46 public:
47         QueueItem ()
48                 : size (0)
49         {}
50
51         enum Type {
52                 /** a normal frame with some JPEG200 data */
53                 FULL,
54                 /** a frame whose data already exists in the MXF,
55                     and we fake-write it; i.e. we update the writer's
56                     state but we use the data that is already on disk.
57                 */
58                 FAKE,
59                 REPEAT,
60         } type;
61
62         /** encoded data for FULL */
63         boost::optional<dcp::Data> encoded;
64         /** size of data for FAKE */
65         int size;
66         /** reel index */
67         size_t reel;
68         /** frame index within the reel */
69         int frame;
70         /** eyes for FULL, FAKE and REPEAT */
71         Eyes eyes;
72 };
73
74 bool operator< (QueueItem const & a, QueueItem const & b);
75 bool operator== (QueueItem const & a, QueueItem const & b);
76
77 /** @class Writer
78  *  @brief Class to manage writing JPEG2000 and audio data to assets on disk.
79  *
80  *  This class creates sound and picture assets, then takes Data
81  *  or AudioBuffers objects (containing image or sound data respectively)
82  *  and writes them to the assets.
83  *
84  *  ::write() for Data (picture) can be called out of order, and the Writer
85  *  will sort it out.  write() for AudioBuffers must be called in order.
86  */
87
88 class Writer : public ExceptionStore, public boost::noncopyable
89 {
90 public:
91         Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
92         ~Writer ();
93
94         void start ();
95
96         bool can_fake_write (Frame) const;
97
98         void write (dcp::Data, Frame, Eyes);
99         void fake_write (Frame, Eyes);
100         bool can_repeat (Frame) const;
101         void repeat (Frame, Eyes);
102         void write (boost::shared_ptr<const AudioBuffers>);
103         void write (PlayerSubtitles subs);
104         void write (std::list<boost::shared_ptr<Font> > fonts);
105         void write (ReferencedReelAsset asset);
106         void finish ();
107
108         void set_encoder_threads (int threads);
109
110 private:
111         void thread ();
112         void terminate_thread (bool);
113         bool have_sequenced_image_at_queue_head ();
114         size_t video_reel (int frame) const;
115
116         /** our Film */
117         boost::shared_ptr<const Film> _film;
118         boost::weak_ptr<Job> _job;
119         std::vector<ReelWriter> _reels;
120         std::vector<ReelWriter>::iterator _audio_reel;
121         std::vector<ReelWriter>::iterator _subtitle_reel;
122
123         /** our thread, or 0 */
124         boost::thread* _thread;
125         /** true if our thread should finish */
126         bool _finish;
127         /** queue of things to write to disk */
128         std::list<QueueItem> _queue;
129         /** number of FULL frames whose JPEG200 data is currently held in RAM */
130         int _queued_full_in_memory;
131         /** mutex for thread state */
132         mutable boost::mutex _state_mutex;
133         /** condition to manage thread wakeups when we have nothing to do  */
134         boost::condition _empty_condition;
135         /** condition to manage thread wakeups when we have too much to do */
136         boost::condition _full_condition;
137         /** maximum number of frames to hold in memory, for when we are managing
138          *  ordering
139          */
140         int _maximum_frames_in_memory;
141
142         /** number of FULL written frames */
143         int _full_written;
144         /** number of FAKE written frames */
145         int _fake_written;
146         int _repeat_written;
147         /** number of frames pushed to disk and then recovered
148             due to the limit of frames to be held in memory.
149         */
150         int _pushed_to_disk;
151
152         std::list<ReferencedReelAsset> _reel_assets;
153
154         std::list<boost::shared_ptr<Font> > _fonts;
155 };