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