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