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