f5f9444092d2b634d892af93e452a15d46d21c73
[dcpomatic.git] / src / lib / film.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 /** @file  src/film.h
21  *  @brief A representation of a piece of video (with sound), including naming,
22  *  the source content file, and how it should be presented in a DCP.
23  */
24
25 #ifndef DVDOMATIC_FILM_H
26 #define DVDOMATIC_FILM_H
27
28 #include <string>
29 #include <vector>
30 #include <inttypes.h>
31 #include <boost/thread/mutex.hpp>
32 #include <boost/thread.hpp>
33 #include <boost/signals2.hpp>
34 #include <boost/enable_shared_from_this.hpp>
35 extern "C" {
36 #include <libavcodec/avcodec.h>
37 }
38 #include "dcp_content_type.h"
39 #include "util.h"
40 #include "dci_metadata.h"
41
42 class Format;
43 class Job;
44 class Filter;
45 class Log;
46 class ExamineContentJob;
47 class AnalyseAudioJob;
48 class ExternalAudioStream;
49 class Content;
50 class Playlist;
51
52 /** @class Film
53  *  @brief A representation of a video, maybe with sound.
54  *
55  *  A representation of a piece of video (maybe with sound), including naming,
56  *  the source content file, and how it should be presented in a DCP.
57  */
58 class Film : public boost::enable_shared_from_this<Film>
59 {
60 public:
61         Film (std::string d, bool must_exist = true);
62         Film (Film const &);
63         ~Film ();
64
65         std::string info_dir () const;
66         std::string j2c_path (int f, bool t) const;
67         std::string info_path (int f) const;
68         std::string video_mxf_dir () const;
69         std::string video_mxf_filename () const;
70         std::string audio_analysis_path () const;
71
72         void examine_content (boost::shared_ptr<Content>);
73         void analyse_audio ();
74         void send_dcp_to_tms ();
75
76         void make_dcp ();
77
78         /** @return Logger.
79          *  It is safe to call this from any thread.
80          */
81         boost::shared_ptr<Log> log () const {
82                 return _log;
83         }
84
85         int encoded_frames () const;
86         
87         std::string file (std::string f) const;
88         std::string dir (std::string d) const;
89
90         int target_audio_sample_rate () const;
91         
92         void write_metadata () const;
93         void read_metadata ();
94
95         libdcp::Size cropped_size (libdcp::Size) const;
96         std::string dci_name (bool if_created_now) const;
97         std::string dcp_name (bool if_created_now = false) const;
98
99         /** @return true if our state has changed since we last saved it */
100         bool dirty () const {
101                 return _dirty;
102         }
103
104         void set_dci_date_today ();
105
106         bool have_dcp () const;
107
108         boost::shared_ptr<Playlist> playlist () const;
109
110         /** Identifiers for the parts of our state;
111             used for signalling changes.
112         */
113         enum Property {
114                 NONE,
115                 NAME,
116                 USE_DCI_NAME,
117                 TRUST_CONTENT_HEADERS,
118                 CONTENT,
119                 DCP_CONTENT_TYPE,
120                 FORMAT,
121                 CROP,
122                 FILTERS,
123                 SCALER,
124                 TRIM_START,
125                 TRIM_END,
126                 AB,
127                 AUDIO_GAIN,
128                 AUDIO_DELAY,
129                 STILL_DURATION,
130                 SUBTITLE_STREAM,
131                 WITH_SUBTITLES,
132                 SUBTITLE_OFFSET,
133                 SUBTITLE_SCALE,
134                 COLOUR_LUT,
135                 J2K_BANDWIDTH,
136                 DCI_METADATA,
137                 DCP_FRAME_RATE
138         };
139
140
141         /* GET */
142
143         std::string directory () const {
144                 boost::mutex::scoped_lock lm (_directory_mutex);
145                 return _directory;
146         }
147
148         std::string name () const {
149                 boost::mutex::scoped_lock lm (_state_mutex);
150                 return _name;
151         }
152
153         bool use_dci_name () const {
154                 boost::mutex::scoped_lock lm (_state_mutex);
155                 return _use_dci_name;
156         }
157
158         bool trust_content_headers () const {
159                 boost::mutex::scoped_lock lm (_state_mutex);
160                 return _trust_content_headers;
161         }
162
163         std::list<boost::shared_ptr<Content> > content () const {
164                 boost::mutex::scoped_lock lm (_state_mutex);
165                 return _content;
166         }
167
168         DCPContentType const * dcp_content_type () const {
169                 boost::mutex::scoped_lock lm (_state_mutex);
170                 return _dcp_content_type;
171         }
172
173         Format const * format () const {
174                 boost::mutex::scoped_lock lm (_state_mutex);
175                 return _format;
176         }
177
178         Crop crop () const {
179                 boost::mutex::scoped_lock lm (_state_mutex);
180                 return _crop;
181         }
182
183         std::vector<Filter const *> filters () const {
184                 boost::mutex::scoped_lock lm (_state_mutex);
185                 return _filters;
186         }
187
188         Scaler const * scaler () const {
189                 boost::mutex::scoped_lock lm (_state_mutex);
190                 return _scaler;
191         }
192
193         int trim_start () const {
194                 boost::mutex::scoped_lock lm (_state_mutex);
195                 return _trim_start;
196         }
197
198         int trim_end () const {
199                 boost::mutex::scoped_lock lm (_state_mutex);
200                 return _trim_end;
201         }
202
203         bool ab () const {
204                 boost::mutex::scoped_lock lm (_state_mutex);
205                 return _ab;
206         }
207
208         float audio_gain () const {
209                 boost::mutex::scoped_lock lm (_state_mutex);
210                 return _audio_gain;
211         }
212
213         int audio_delay () const {
214                 boost::mutex::scoped_lock lm (_state_mutex);
215                 return _audio_delay;
216         }
217
218         bool with_subtitles () const {
219                 boost::mutex::scoped_lock lm (_state_mutex);
220                 return _with_subtitles;
221         }
222
223         int subtitle_offset () const {
224                 boost::mutex::scoped_lock lm (_state_mutex);
225                 return _subtitle_offset;
226         }
227
228         float subtitle_scale () const {
229                 boost::mutex::scoped_lock lm (_state_mutex);
230                 return _subtitle_scale;
231         }
232
233         int colour_lut () const {
234                 boost::mutex::scoped_lock lm (_state_mutex);
235                 return _colour_lut;
236         }
237
238         int j2k_bandwidth () const {
239                 boost::mutex::scoped_lock lm (_state_mutex);
240                 return _j2k_bandwidth;
241         }
242
243         DCIMetadata dci_metadata () const {
244                 boost::mutex::scoped_lock lm (_state_mutex);
245                 return _dci_metadata;
246         }
247
248         int dcp_frame_rate () const {
249                 boost::mutex::scoped_lock lm (_state_mutex);
250                 return _dcp_frame_rate;
251         }
252
253         /* SET */
254
255         void set_directory (std::string);
256         void set_name (std::string);
257         void set_use_dci_name (bool);
258         void set_trust_content_headers (bool);
259         void add_content (boost::shared_ptr<Content>);
260         void set_dcp_content_type (DCPContentType const *);
261         void set_format (Format const *);
262         void set_crop (Crop);
263         void set_left_crop (int);
264         void set_right_crop (int);
265         void set_top_crop (int);
266         void set_bottom_crop (int);
267         void set_filters (std::vector<Filter const *>);
268         void set_scaler (Scaler const *);
269         void set_trim_start (int);
270         void set_trim_end (int);
271         void set_ab (bool);
272         void set_audio_gain (float);
273         void set_audio_delay (int);
274         void set_still_duration (int);
275         void set_with_subtitles (bool);
276         void set_subtitle_offset (int);
277         void set_subtitle_scale (float);
278         void set_colour_lut (int);
279         void set_j2k_bandwidth (int);
280         void set_dci_metadata (DCIMetadata);
281         void set_dcp_frame_rate (int);
282
283         /** Emitted when some property has changed */
284         mutable boost::signals2::signal<void (Property)> Changed;
285
286         boost::signals2::signal<void ()> AudioAnalysisSucceeded;
287
288         /** Current version number of the state file */
289         static int const state_version;
290
291 private:
292         
293         /** Log to write to */
294         boost::shared_ptr<Log> _log;
295
296         /** Any running AnalyseAudioJob, or 0 */
297         boost::shared_ptr<AnalyseAudioJob> _analyse_audio_job;
298
299         void signal_changed (Property);
300         void examine_content_finished ();
301         void analyse_audio_finished ();
302         std::string video_state_identifier () const;
303
304         /** Complete path to directory containing the film metadata;
305          *  must not be relative.
306          */
307         std::string _directory;
308         /** Mutex for _directory */
309         mutable boost::mutex _directory_mutex;
310         
311         /** Name for DVD-o-matic */
312         std::string _name;
313         /** True if a auto-generated DCI-compliant name should be used for our DCP */
314         bool _use_dci_name;
315         typedef std::list<boost::shared_ptr<Content> > ContentList;
316         ContentList _content;
317         bool _trust_content_headers;
318         /** The type of content that this Film represents (feature, trailer etc.) */
319         DCPContentType const * _dcp_content_type;
320         /** The format to present this Film in (flat, scope, etc.) */
321         Format const * _format;
322         /** The crop to apply to the source */
323         Crop _crop;
324         /** Video filters that should be used when generating DCPs */
325         std::vector<Filter const *> _filters;
326         /** Scaler algorithm to use */
327         Scaler const * _scaler;
328         /** Frames to trim off the start of the DCP */
329         int _trim_start;
330         /** Frames to trim off the end of the DCP */
331         int _trim_end;
332         /** true to create an A/B comparison DCP, where the left half of the image
333             is the video without any filters or post-processing, and the right half
334             has the specified filters and post-processing.
335         */
336         bool _ab;
337         /** Gain to apply to audio in dB */
338         float _audio_gain;
339         /** Delay to apply to audio (positive moves audio later) in milliseconds */
340         int _audio_delay;
341         /** True if subtitles should be shown for this film */
342         bool _with_subtitles;
343         /** y offset for placing subtitles, in source pixels; +ve is further down
344             the frame, -ve is further up.
345         */
346         int _subtitle_offset;
347         /** scale factor to apply to subtitles */
348         float _subtitle_scale;
349         /** index of colour LUT to use when converting RGB to XYZ.
350          *  0: sRGB
351          *  1: Rec 709
352          */
353         int _colour_lut;
354         /** bandwidth for J2K files in bits per second */
355         int _j2k_bandwidth;
356
357         /** DCI naming stuff */
358         DCIMetadata _dci_metadata;
359         /** The date that we should use in a DCI name */
360         boost::gregorian::date _dci_date;
361         /** Frames per second to run our DCP at */
362         int _dcp_frame_rate;
363
364         /** true if our state has changed since we last saved it */
365         mutable bool _dirty;
366
367         /** Mutex for all state except _directory */
368         mutable boost::mutex _state_mutex;
369
370         friend class paths_test;
371         friend class film_metadata_test;
372 };
373
374 #endif