Various work on certificate handling for screens; need XML config here, now.
[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 #include <boost/date_time/posix_time/posix_time.hpp>
36 extern "C" {
37 #include <libavcodec/avcodec.h>
38 }
39 #include "dcp_content_type.h"
40 #include "util.h"
41 #include "stream.h"
42
43 class Format;
44 class Job;
45 class Filter;
46 class Log;
47 class ExamineContentJob;
48 class ExternalAudioStream;
49 class Screen;
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 j2k_dir () const;
65
66         void examine_content ();
67         void send_dcp_to_tms ();
68
69         void make_dcp (bool);
70
71         /** @return Logger.
72          *  It is safe to call this from any thread.
73          */
74         Log* log () const {
75                 return _log;
76         }
77
78         int encoded_frames () const;
79         
80         std::string file (std::string f) const;
81         std::string dir (std::string d) const;
82
83         std::string content_path () const;
84         ContentType content_type () const;
85         
86         int target_audio_sample_rate () const;
87         
88         void write_metadata () const;
89         void read_metadata ();
90
91         Size cropped_size (Size) const;
92         boost::optional<int> dcp_length () const;
93         std::string dci_name () const;
94         std::string dcp_name () const;
95
96         /** @return true if our state has changed since we last saved it */
97         bool dirty () const {
98                 return _dirty;
99         }
100
101         int audio_channels () const;
102
103         void set_dci_date_today ();
104
105         void make_kdms (
106                 std::list<boost::shared_ptr<Screen> >,
107                 boost::posix_time::ptime from,
108                 boost::posix_time::ptime until,
109                 std::string directory
110                 ) 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                 DCP_TRIM_START,
127                 DCP_TRIM_END,
128                 REEL_SIZE,
129                 DCP_AB,
130                 CONTENT_AUDIO_STREAM,
131                 EXTERNAL_AUDIO,
132                 USE_CONTENT_AUDIO,
133                 AUDIO_GAIN,
134                 AUDIO_DELAY,
135                 STILL_DURATION,
136                 SUBTITLE_STREAM,
137                 WITH_SUBTITLES,
138                 SUBTITLE_OFFSET,
139                 SUBTITLE_SCALE,
140                 ENCRYPTED,
141                 COLOUR_LUT,
142                 J2K_BANDWIDTH,
143                 DCI_METADATA,
144                 SIZE,
145                 LENGTH,
146                 CONTENT_AUDIO_STREAMS,
147                 SUBTITLE_STREAMS,
148                 FRAMES_PER_SECOND,
149         };
150
151
152         /* GET */
153
154         std::string directory () const {
155                 boost::mutex::scoped_lock lm (_directory_mutex);
156                 return _directory;
157         }
158
159         std::string name () const {
160                 boost::mutex::scoped_lock lm (_state_mutex);
161                 return _name;
162         }
163
164         bool use_dci_name () const {
165                 boost::mutex::scoped_lock lm (_state_mutex);
166                 return _use_dci_name;
167         }
168
169         std::string content () const {
170                 boost::mutex::scoped_lock lm (_state_mutex);
171                 return _content;
172         }
173
174         bool trust_content_header () const {
175                 boost::mutex::scoped_lock lm (_state_mutex);
176                 return _trust_content_header;
177         }
178
179         DCPContentType const * dcp_content_type () const {
180                 boost::mutex::scoped_lock lm (_state_mutex);
181                 return _dcp_content_type;
182         }
183
184         Format const * format () const {
185                 boost::mutex::scoped_lock lm (_state_mutex);
186                 return _format;
187         }
188
189         Crop crop () const {
190                 boost::mutex::scoped_lock lm (_state_mutex);
191                 return _crop;
192         }
193
194         std::vector<Filter const *> filters () const {
195                 boost::mutex::scoped_lock lm (_state_mutex);
196                 return _filters;
197         }
198
199         Scaler const * scaler () const {
200                 boost::mutex::scoped_lock lm (_state_mutex);
201                 return _scaler;
202         }
203
204         SourceFrame dcp_trim_start () const {
205                 boost::mutex::scoped_lock lm (_state_mutex);
206                 return _dcp_trim_start;
207         }
208
209         SourceFrame dcp_trim_end () const {
210                 boost::mutex::scoped_lock lm (_state_mutex);
211                 return _dcp_trim_end;
212         }
213
214         boost::optional<uint64_t> reel_size () const {
215                 boost::mutex::scoped_lock lm (_state_mutex);
216                 return _reel_size;
217         }
218         
219         bool dcp_ab () const {
220                 boost::mutex::scoped_lock lm (_state_mutex);
221                 return _dcp_ab;
222         }
223
224         boost::shared_ptr<AudioStream> content_audio_stream () const {
225                 boost::mutex::scoped_lock lm (_state_mutex);
226                 return _content_audio_stream;
227         }
228
229         std::vector<std::string> external_audio () const {
230                 boost::mutex::scoped_lock lm (_state_mutex);
231                 return _external_audio;
232         }
233
234         bool use_content_audio () const {
235                 boost::mutex::scoped_lock lm (_state_mutex);
236                 return _use_content_audio;
237         }
238         
239         float audio_gain () const {
240                 boost::mutex::scoped_lock lm (_state_mutex);
241                 return _audio_gain;
242         }
243
244         int audio_delay () const {
245                 boost::mutex::scoped_lock lm (_state_mutex);
246                 return _audio_delay;
247         }
248
249         int still_duration () const {
250                 boost::mutex::scoped_lock lm (_state_mutex);
251                 return _still_duration;
252         }
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         bool encrypted () const {
275                 boost::mutex::scoped_lock lm (_state_mutex);
276                 return _encrypted;
277         }
278
279         int colour_lut () const {
280                 boost::mutex::scoped_lock lm (_state_mutex);
281                 return _colour_lut;
282         }
283
284         int j2k_bandwidth () const {
285                 boost::mutex::scoped_lock lm (_state_mutex);
286                 return _j2k_bandwidth;
287         }
288
289         std::string audio_language () const {
290                 boost::mutex::scoped_lock lm (_state_mutex);
291                 return _audio_language;
292         }
293         
294         std::string subtitle_language () const {
295                 boost::mutex::scoped_lock lm (_state_mutex);
296                 return _subtitle_language;
297         }
298         
299         std::string territory () const {
300                 boost::mutex::scoped_lock lm (_state_mutex);
301                 return _territory;
302         }
303         
304         std::string rating () const {
305                 boost::mutex::scoped_lock lm (_state_mutex);
306                 return _rating;
307         }
308         
309         std::string studio () const {
310                 boost::mutex::scoped_lock lm (_state_mutex);
311                 return _studio;
312         }
313         
314         std::string facility () const {
315                 boost::mutex::scoped_lock lm (_state_mutex);
316                 return _facility;
317         }
318         
319         std::string package_type () const {
320                 boost::mutex::scoped_lock lm (_state_mutex);
321                 return _package_type;
322         }
323
324         Size size () const {
325                 boost::mutex::scoped_lock lm (_state_mutex);
326                 return _size;
327         }
328
329         boost::optional<SourceFrame> length () const {
330                 boost::mutex::scoped_lock lm (_state_mutex);
331                 return _length;
332         }
333         
334         std::string content_digest () const {
335                 boost::mutex::scoped_lock lm (_state_mutex);
336                 return _content_digest;
337         }
338         
339         std::vector<boost::shared_ptr<AudioStream> > content_audio_streams () const {
340                 boost::mutex::scoped_lock lm (_state_mutex);
341                 return _content_audio_streams;
342         }
343
344         std::vector<boost::shared_ptr<SubtitleStream> > subtitle_streams () const {
345                 boost::mutex::scoped_lock lm (_state_mutex);
346                 return _subtitle_streams;
347         }
348         
349         float frames_per_second () const {
350                 boost::mutex::scoped_lock lm (_state_mutex);
351                 if (content_type() == STILL) {
352                         return 24;
353                 }
354                 
355                 return _frames_per_second;
356         }
357
358         boost::shared_ptr<AudioStream> audio_stream () const;
359
360         
361         /* SET */
362
363         void set_directory (std::string);
364         void set_name (std::string);
365         void set_use_dci_name (bool);
366         void set_content (std::string);
367         void set_trust_content_header (bool);
368         void set_dcp_content_type (DCPContentType const *);
369         void set_format (Format const *);
370         void set_crop (Crop);
371         void set_left_crop (int);
372         void set_right_crop (int);
373         void set_top_crop (int);
374         void set_bottom_crop (int);
375         void set_filters (std::vector<Filter const *>);
376         void set_scaler (Scaler const *);
377         void set_dcp_trim_start (int);
378         void set_dcp_trim_end (int);
379         void set_reel_size (uint64_t);
380         void unset_reel_size ();
381         void set_dcp_ab (bool);
382         void set_content_audio_stream (boost::shared_ptr<AudioStream>);
383         void set_external_audio (std::vector<std::string>);
384         void set_use_content_audio (bool);
385         void set_audio_gain (float);
386         void set_audio_delay (int);
387         void set_still_duration (int);
388         void set_subtitle_stream (boost::shared_ptr<SubtitleStream>);
389         void set_with_subtitles (bool);
390         void set_subtitle_offset (int);
391         void set_subtitle_scale (float);
392         void set_encrypted (bool);
393         void set_colour_lut (int);
394         void set_j2k_bandwidth (int);
395         void set_audio_language (std::string);
396         void set_subtitle_language (std::string);
397         void set_territory (std::string);
398         void set_rating (std::string);
399         void set_studio (std::string);
400         void set_facility (std::string);
401         void set_package_type (std::string);
402         void set_size (Size);
403         void set_length (SourceFrame);
404         void unset_length ();
405         void set_content_digest (std::string);
406         void set_content_audio_streams (std::vector<boost::shared_ptr<AudioStream> >);
407         void set_subtitle_streams (std::vector<boost::shared_ptr<SubtitleStream> >);
408         void set_frames_per_second (float);
409
410         /** Emitted when some property has changed */
411         mutable boost::signals2::signal<void (Property)> Changed;
412
413         /** Current version number of the state file */
414         static int const state_version;
415
416 private:
417         
418         /** Log to write to */
419         Log* _log;
420
421         /** Any running ExamineContentJob, or 0 */
422         boost::shared_ptr<ExamineContentJob> _examine_content_job;
423
424         /** The date that we should use in a DCI name */
425         boost::gregorian::date _dci_date;
426
427         void signal_changed (Property);
428         void examine_content_finished ();
429
430         /** Complete path to directory containing the film metadata;
431          *  must not be relative.
432          */
433         std::string _directory;
434         /** Mutex for _directory */
435         mutable boost::mutex _directory_mutex;
436         
437         /** Name for DVD-o-matic */
438         std::string _name;
439         /** True if a auto-generated DCI-compliant name should be used for our DCP */
440         bool _use_dci_name;
441         /** File or directory containing content; may be relative to our directory
442          *  or an absolute path.
443          */
444         std::string _content;
445         /** If this is true, we will believe the length specified by the content
446          *  file's header; if false, we will run through the whole content file
447          *  the first time we see it in order to obtain the length.
448          */
449         bool _trust_content_header;
450         /** The type of content that this Film represents (feature, trailer etc.) */
451         DCPContentType const * _dcp_content_type;
452         /** The format to present this Film in (flat, scope, etc.) */
453         Format const * _format;
454         /** The crop to apply to the source */
455         Crop _crop;
456         /** Video filters that should be used when generating DCPs */
457         std::vector<Filter const *> _filters;
458         /** Scaler algorithm to use */
459         Scaler const * _scaler;
460         /** Frames to trim off the start of the DCP */
461         int _dcp_trim_start;
462         /** Frames to trim off the end of the DCP */
463         int _dcp_trim_end;
464         /** Approximate target reel size in bytes; if not set, use a single reel */
465         boost::optional<uint64_t> _reel_size;
466         /** true to create an A/B comparison DCP, where the left half of the image
467             is the video without any filters or post-processing, and the right half
468             has the specified filters and post-processing.
469         */
470         bool _dcp_ab;
471         /** The audio stream to use from our content */
472         boost::shared_ptr<AudioStream> _content_audio_stream;
473         /** List of filenames of external audio files, in channel order
474             (L, R, C, Lfe, Ls, Rs)
475         */
476         std::vector<std::string> _external_audio;
477         /** true to use audio from our content file; false to use external audio */
478         bool _use_content_audio;
479         /** Gain to apply to audio in dB */
480         float _audio_gain;
481         /** Delay to apply to audio (positive moves audio later) in milliseconds */
482         int _audio_delay;
483         /** Duration to make still-sourced films (in seconds) */
484         int _still_duration;
485         boost::shared_ptr<SubtitleStream> _subtitle_stream;
486         /** True if subtitles should be shown for this film */
487         bool _with_subtitles;
488         /** y offset for placing subtitles, in source pixels; +ve is further down
489             the frame, -ve is further up.
490         */
491         int _subtitle_offset;
492         /** scale factor to apply to subtitles */
493         float _subtitle_scale;
494         bool _encrypted;
495
496         /** index of colour LUT to use when converting RGB to XYZ.
497          *  0: sRGB
498          *  1: Rec 709
499          */
500         int _colour_lut;
501         /** bandwidth for J2K files in bits per second */
502         int _j2k_bandwidth;
503         
504         /* DCI naming stuff */
505         std::string _audio_language;
506         std::string _subtitle_language;
507         std::string _territory;
508         std::string _rating;
509         std::string _studio;
510         std::string _facility;
511         std::string _package_type;
512
513         /* Data which are cached to speed things up */
514
515         /** Size, in pixels, of the source (ignoring cropping) */
516         Size _size;
517         /** The length of the source, in video frames (as far as we know) */
518         boost::optional<SourceFrame> _length;
519         /** MD5 digest of our content file */
520         std::string _content_digest;
521         /** The audio streams in our content */
522         std::vector<boost::shared_ptr<AudioStream> > _content_audio_streams;
523         /** A stream to represent possible external audio (will always exist) */
524         boost::shared_ptr<AudioStream> _external_audio_stream;
525         /** the subtitle streams that we can use */
526         std::vector<boost::shared_ptr<SubtitleStream> > _subtitle_streams;
527         /** Frames per second of the source */
528         float _frames_per_second;
529
530         /** true if our state has changed since we last saved it */
531         mutable bool _dirty;
532
533         /** Mutex for all state except _directory */
534         mutable boost::mutex _state_mutex;
535
536         friend class paths_test;
537 };
538
539 #endif