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