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