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