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