Merge branch '1.0' into 1.0-3D-take2
[dcpomatic.git] / src / lib / writer.h
1 /*
2     Copyright (C) 2012 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 #include <list>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/thread.hpp>
23 #include <boost/thread/condition.hpp>
24 #include "exceptions.h"
25 #include "types.h"
26
27 class Film;
28 class EncodedData;
29 class AudioBuffers;
30 class Job;
31
32 namespace libdcp {
33         class MonoPictureAsset;
34         class MonoPictureAssetWriter;
35         class SoundAsset;
36         class SoundAssetWriter;
37 }
38
39 struct QueueItem
40 {
41 public:
42         enum Type {
43                 /** a normal frame with some JPEG200 data */
44                 FULL,
45                 /** a frame whose data already exists in the MXF,
46                     and we fake-write it; i.e. we update the writer's
47                     state but we use the data that is already on disk.
48                 */
49                 FAKE,
50                 /** this is a repeat of the last frame to be written */
51                 REPEAT
52         } type;
53
54         /** encoded data for FULL */
55         boost::shared_ptr<const EncodedData> encoded;
56         /** size of data for FAKE */
57         int size;
58         /** frame index */
59         int frame;
60         Eyes eyes;
61 };
62
63 bool operator< (QueueItem const & a, QueueItem const & b);
64 bool operator== (QueueItem const & a, QueueItem const & b);
65
66 class Writer : public ExceptionStore, public boost::noncopyable
67 {
68 public:
69         Writer (boost::shared_ptr<const Film>, boost::shared_ptr<Job>);
70
71         bool can_fake_write (int) const;
72         
73         void write (boost::shared_ptr<const EncodedData>, int, Eyes);
74         void fake_write (int, Eyes);
75         void write (boost::shared_ptr<const AudioBuffers>);
76         void repeat (int f, Eyes);
77         void finish ();
78
79 private:
80
81         void thread ();
82         void check_existing_picture_mxf ();
83         bool check_existing_picture_mxf_frame (FILE *, int, Eyes);
84         bool have_sequenced_image_at_queue_head () const;
85
86         /** our Film */
87         boost::shared_ptr<const Film> _film;
88         boost::shared_ptr<Job> _job;
89         /** the first frame index that does not already exist in our MXF */
90         int _first_nonexistant_frame;
91
92         /** our thread, or 0 */
93         boost::thread* _thread;
94         /** true if our thread should finish */
95         bool _finish;
96         /** queue of things to write to disk */
97         std::list<QueueItem> _queue;
98         /** number of FULL frames whose JPEG200 data is currently held in RAM */
99         int _queued_full_in_memory;
100         /** mutex for thread state */
101         mutable boost::mutex _mutex;
102         /** condition to manage thread wakeups */
103         boost::condition _condition;
104         /** the data of the last written frame, or 0 if there isn't one */
105         boost::shared_ptr<const EncodedData> _last_written;
106         /** the index of the last written frame */
107         int _last_written_frame;
108         Eyes _last_written_eyes;
109         /** maximum number of frames to hold in memory, for when we are managing
110             ordering
111         */
112         static const int _maximum_frames_in_memory;
113
114         /** number of FULL written frames */
115         int _full_written;
116         /** number of FAKE written frames */
117         int _fake_written;
118         /** number of REPEAT written frames */
119         int _repeat_written;
120         /** number of frames pushed to disk and then recovered
121             due to the limit of frames to be held in memory.
122         */
123         int _pushed_to_disk;
124         
125         boost::shared_ptr<libdcp::MonoPictureAsset> _picture_asset;
126         boost::shared_ptr<libdcp::MonoPictureAssetWriter> _picture_asset_writer;
127         boost::shared_ptr<libdcp::SoundAsset> _sound_asset;
128         boost::shared_ptr<libdcp::SoundAssetWriter> _sound_asset_writer;
129 };