af7ec670134bd9bdda2f32298ce9f5aa3a5c6cc0
[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 "stream.h"
41 #include "dci_metadata.h"
42
43 class Format;
44 class Job;
45 class Filter;
46 class Log;
47 class ExamineContentJob;
48 class ExternalAudioStream;
49
50 /** @class Film
51  *  @brief A representation of a video, maybe with sound.
52  *
53  *  A representation of a piece of video (maybe with sound), including naming,
54  *  the source content file, and how it should be presented in a DCP.
55  */
56 class Film : public boost::enable_shared_from_this<Film>
57 {
58 public:
59         Film (std::string d, bool must_exist = true);
60         Film (Film const &);
61         ~Film ();
62
63         std::string j2k_dir () const;
64
65         void examine_content ();
66         void send_dcp_to_tms ();
67
68         void make_dcp (bool);
69
70         /** @return Logger.
71          *  It is safe to call this from any thread.
72          */
73         Log* log () const {
74                 return _log;
75         }
76
77         int encoded_frames () const;
78         
79         std::string file (std::string f) const;
80         std::string dir (std::string d) const;
81
82         std::string content_path () const;
83         ContentType content_type () const;
84         
85         int target_audio_sample_rate () const;
86         
87         void write_metadata () const;
88         void read_metadata ();
89
90         libdcp::Size cropped_size (libdcp::Size) const;
91         boost::optional<int> dcp_length () const;
92         std::string dci_name () const;
93         std::string dcp_name () const;
94
95         /** @return true if our state has changed since we last saved it */
96         bool dirty () const {
97                 return _dirty;
98         }
99
100         int audio_channels () const;
101
102         void set_dci_date_today ();
103
104         /** Identifiers for the parts of our state;
105             used for signalling changes.
106         */
107         enum Property {
108                 NONE,
109                 NAME,
110                 USE_DCI_NAME,
111                 CONTENT,
112                 TRUST_CONTENT_HEADER,
113                 DCP_CONTENT_TYPE,
114                 FORMAT,
115                 CROP,
116                 FILTERS,
117                 SCALER,
118                 DCP_TRIM_START,
119                 DCP_TRIM_END,
120                 DCP_AB,
121                 CONTENT_AUDIO_STREAM,
122                 EXTERNAL_AUDIO,
123                 USE_CONTENT_AUDIO,
124                 AUDIO_GAIN,
125                 AUDIO_DELAY,
126                 STILL_DURATION,
127                 SUBTITLE_STREAM,
128                 WITH_SUBTITLES,
129                 SUBTITLE_OFFSET,
130                 SUBTITLE_SCALE,
131                 COLOUR_LUT,
132                 J2K_BANDWIDTH,
133                 DCI_METADATA,
134                 SIZE,
135                 LENGTH,
136                 CONTENT_AUDIO_STREAMS,
137                 SUBTITLE_STREAMS,
138                 FRAMES_PER_SECOND,
139         };
140
141
142         /* GET */
143
144         std::string directory () const {
145                 boost::mutex::scoped_lock lm (_directory_mutex);
146                 return _directory;
147         }
148
149         std::string name () const {
150                 boost::mutex::scoped_lock lm (_state_mutex);
151                 return _name;
152         }
153
154         bool use_dci_name () const {
155                 boost::mutex::scoped_lock lm (_state_mutex);
156                 return _use_dci_name;
157         }
158
159         std::string content () const {
160                 boost::mutex::scoped_lock lm (_state_mutex);
161                 return _content;
162         }
163
164         bool trust_content_header () const {
165                 boost::mutex::scoped_lock lm (_state_mutex);
166                 return _trust_content_header;
167         }
168
169         DCPContentType const * dcp_content_type () const {
170                 boost::mutex::scoped_lock lm (_state_mutex);
171                 return _dcp_content_type;
172         }
173
174         Format const * format () const {
175                 boost::mutex::scoped_lock lm (_state_mutex);
176                 return _format;
177         }
178
179         Crop crop () const {
180                 boost::mutex::scoped_lock lm (_state_mutex);
181                 return _crop;
182         }
183
184         std::vector<Filter const *> filters () const {
185                 boost::mutex::scoped_lock lm (_state_mutex);
186                 return _filters;
187         }
188
189         Scaler const * scaler () const {
190                 boost::mutex::scoped_lock lm (_state_mutex);
191                 return _scaler;
192         }
193
194         SourceFrame dcp_trim_start () const {
195                 boost::mutex::scoped_lock lm (_state_mutex);
196                 return _dcp_trim_start;
197         }
198
199         SourceFrame dcp_trim_end () const {
200                 boost::mutex::scoped_lock lm (_state_mutex);
201                 return _dcp_trim_end;
202         }
203
204         bool dcp_ab () const {
205                 boost::mutex::scoped_lock lm (_state_mutex);
206                 return _dcp_ab;
207         }
208
209         boost::shared_ptr<AudioStream> content_audio_stream () const {
210                 boost::mutex::scoped_lock lm (_state_mutex);
211                 return _content_audio_stream;
212         }
213
214         std::vector<std::string> external_audio () const {
215                 boost::mutex::scoped_lock lm (_state_mutex);
216                 return _external_audio;
217         }
218
219         bool use_content_audio () const {
220                 boost::mutex::scoped_lock lm (_state_mutex);
221                 return _use_content_audio;
222         }
223         
224         float audio_gain () const {
225                 boost::mutex::scoped_lock lm (_state_mutex);
226                 return _audio_gain;
227         }
228
229         int audio_delay () const {
230                 boost::mutex::scoped_lock lm (_state_mutex);
231                 return _audio_delay;
232         }
233
234         int still_duration () const {
235                 boost::mutex::scoped_lock lm (_state_mutex);
236                 return _still_duration;
237         }
238
239         boost::shared_ptr<SubtitleStream> subtitle_stream () const {
240                 boost::mutex::scoped_lock lm (_state_mutex);
241                 return _subtitle_stream;
242         }
243
244         bool with_subtitles () const {
245                 boost::mutex::scoped_lock lm (_state_mutex);
246                 return _with_subtitles;
247         }
248
249         int subtitle_offset () const {
250                 boost::mutex::scoped_lock lm (_state_mutex);
251                 return _subtitle_offset;
252         }
253
254         float subtitle_scale () const {
255                 boost::mutex::scoped_lock lm (_state_mutex);
256                 return _subtitle_scale;
257         }
258
259         int colour_lut () const {
260                 boost::mutex::scoped_lock lm (_state_mutex);
261                 return _colour_lut;
262         }
263
264         int j2k_bandwidth () const {
265                 boost::mutex::scoped_lock lm (_state_mutex);
266                 return _j2k_bandwidth;
267         }
268
269         DCIMetadata dci_metadata () const {
270                 boost::mutex::scoped_lock lm (_state_mutex);
271                 return _dci_metadata;
272         }
273         
274         libdcp::Size size () const {
275                 boost::mutex::scoped_lock lm (_state_mutex);
276                 return _size;
277         }
278
279         boost::optional<SourceFrame> length () const {
280                 boost::mutex::scoped_lock lm (_state_mutex);
281                 return _length;
282         }
283         
284         std::string content_digest () const {
285                 boost::mutex::scoped_lock lm (_state_mutex);
286                 return _content_digest;
287         }
288         
289         std::vector<boost::shared_ptr<AudioStream> > content_audio_streams () const {
290                 boost::mutex::scoped_lock lm (_state_mutex);
291                 return _content_audio_streams;
292         }
293
294         std::vector<boost::shared_ptr<SubtitleStream> > subtitle_streams () const {
295                 boost::mutex::scoped_lock lm (_state_mutex);
296                 return _subtitle_streams;
297         }
298         
299         float frames_per_second () const {
300                 boost::mutex::scoped_lock lm (_state_mutex);
301                 if (content_type() == STILL) {
302                         return 24;
303                 }
304                 
305                 return _frames_per_second;
306         }
307
308         boost::shared_ptr<AudioStream> audio_stream () const;
309
310         
311         /* SET */
312
313         void set_directory (std::string);
314         void set_name (std::string);
315         void set_use_dci_name (bool);
316         void set_content (std::string);
317         void set_trust_content_header (bool);
318         void set_dcp_content_type (DCPContentType const *);
319         void set_format (Format const *);
320         void set_crop (Crop);
321         void set_left_crop (int);
322         void set_right_crop (int);
323         void set_top_crop (int);
324         void set_bottom_crop (int);
325         void set_filters (std::vector<Filter const *>);
326         void set_scaler (Scaler const *);
327         void set_dcp_trim_start (int);
328         void set_dcp_trim_end (int);
329         void set_dcp_ab (bool);
330         void set_content_audio_stream (boost::shared_ptr<AudioStream>);
331         void set_external_audio (std::vector<std::string>);
332         void set_use_content_audio (bool);
333         void set_audio_gain (float);
334         void set_audio_delay (int);
335         void set_still_duration (int);
336         void set_subtitle_stream (boost::shared_ptr<SubtitleStream>);
337         void set_with_subtitles (bool);
338         void set_subtitle_offset (int);
339         void set_subtitle_scale (float);
340         void set_colour_lut (int);
341         void set_j2k_bandwidth (int);
342         void set_dci_metadata (DCIMetadata);
343         void set_size (libdcp::Size);
344         void set_length (SourceFrame);
345         void unset_length ();
346         void set_content_digest (std::string);
347         void set_content_audio_streams (std::vector<boost::shared_ptr<AudioStream> >);
348         void set_subtitle_streams (std::vector<boost::shared_ptr<SubtitleStream> >);
349         void set_frames_per_second (float);
350
351         /** Emitted when some property has changed */
352         mutable boost::signals2::signal<void (Property)> Changed;
353
354         /** Current version number of the state file */
355         static int const state_version;
356
357 private:
358         
359         /** Log to write to */
360         Log* _log;
361
362         /** Any running ExamineContentJob, or 0 */
363         boost::shared_ptr<ExamineContentJob> _examine_content_job;
364
365         /** The date that we should use in a DCI name */
366         boost::gregorian::date _dci_date;
367
368         void signal_changed (Property);
369         void examine_content_finished ();
370
371         /** Complete path to directory containing the film metadata;
372          *  must not be relative.
373          */
374         std::string _directory;
375         /** Mutex for _directory */
376         mutable boost::mutex _directory_mutex;
377         
378         /** Name for DVD-o-matic */
379         std::string _name;
380         /** True if a auto-generated DCI-compliant name should be used for our DCP */
381         bool _use_dci_name;
382         /** File or directory containing content; may be relative to our directory
383          *  or an absolute path.
384          */
385         std::string _content;
386         /** If this is true, we will believe the length specified by the content
387          *  file's header; if false, we will run through the whole content file
388          *  the first time we see it in order to obtain the length.
389          */
390         bool _trust_content_header;
391         /** The type of content that this Film represents (feature, trailer etc.) */
392         DCPContentType const * _dcp_content_type;
393         /** The format to present this Film in (flat, scope, etc.) */
394         Format const * _format;
395         /** The crop to apply to the source */
396         Crop _crop;
397         /** Video filters that should be used when generating DCPs */
398         std::vector<Filter const *> _filters;
399         /** Scaler algorithm to use */
400         Scaler const * _scaler;
401         /** Frames to trim off the start of the DCP */
402         int _dcp_trim_start;
403         /** Frames to trim off the end of the DCP */
404         int _dcp_trim_end;
405         /** true to create an A/B comparison DCP, where the left half of the image
406             is the video without any filters or post-processing, and the right half
407             has the specified filters and post-processing.
408         */
409         bool _dcp_ab;
410         /** The audio stream to use from our content */
411         boost::shared_ptr<AudioStream> _content_audio_stream;
412         /** List of filenames of external audio files, in channel order
413             (L, R, C, Lfe, Ls, Rs)
414         */
415         std::vector<std::string> _external_audio;
416         /** true to use audio from our content file; false to use external audio */
417         bool _use_content_audio;
418         /** Gain to apply to audio in dB */
419         float _audio_gain;
420         /** Delay to apply to audio (positive moves audio later) in milliseconds */
421         int _audio_delay;
422         /** Duration to make still-sourced films (in seconds) */
423         int _still_duration;
424         boost::shared_ptr<SubtitleStream> _subtitle_stream;
425         /** True if subtitles should be shown for this film */
426         bool _with_subtitles;
427         /** y offset for placing subtitles, in source pixels; +ve is further down
428             the frame, -ve is further up.
429         */
430         int _subtitle_offset;
431         /** scale factor to apply to subtitles */
432         float _subtitle_scale;
433         /** index of colour LUT to use when converting RGB to XYZ.
434          *  0: sRGB
435          *  1: Rec 709
436          */
437         int _colour_lut;
438         /** bandwidth for J2K files in bits per second */
439         int _j2k_bandwidth;
440
441         /** DCI naming stuff */
442         DCIMetadata _dci_metadata;
443
444         /* Data which are cached to speed things up */
445
446         /** libdcp::Size, in pixels, of the source (ignoring cropping) */
447         libdcp::Size _size;
448         /** The length of the source, in video frames (as far as we know) */
449         boost::optional<SourceFrame> _length;
450         /** MD5 digest of our content file */
451         std::string _content_digest;
452         /** The audio streams in our content */
453         std::vector<boost::shared_ptr<AudioStream> > _content_audio_streams;
454         /** A stream to represent possible external audio (will always exist) */
455         boost::shared_ptr<AudioStream> _external_audio_stream;
456         /** the subtitle streams that we can use */
457         std::vector<boost::shared_ptr<SubtitleStream> > _subtitle_streams;
458         /** Frames per second of the source */
459         float _frames_per_second;
460
461         /** true if our state has changed since we last saved it */
462         mutable bool _dirty;
463
464         /** Mutex for all state except _directory */
465         mutable boost::mutex _state_mutex;
466
467         friend class paths_test;
468 };
469
470 #endif