Merge master.
[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         std::pair<Position, std::string> thumb_subtitle (int) 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         std::string thumb_file (int) const;
87         std::string thumb_base (int) const;
88         SourceFrame thumb_frame (int) const;
89
90         int target_audio_sample_rate () const;
91         
92         void write_metadata () const;
93         void read_metadata ();
94
95         Size cropped_size (Size) const;
96         boost::optional<SourceFrame> dcp_length () const;
97         std::string dci_name () const;
98         std::string dcp_name () const;
99
100         bool dirty () const {
101                 return _dirty;
102         }
103
104         int audio_channels () const;
105
106         void set_dci_date_today ();
107
108         enum Property {
109                 NONE,
110                 NAME,
111                 USE_DCI_NAME,
112                 CONTENT,
113                 DCP_CONTENT_TYPE,
114                 FORMAT,
115                 CROP,
116                 FILTERS,
117                 SCALER,
118                 DCP_TRIM_START,
119                 DCP_TRIM_END,
120                 DCP_AB,
121                 CONTENT_AUDIO_STREAM,
122                 EXTERNAL_AUDIO,
123                 USE_CONTENT_AUDIO,
124                 AUDIO_GAIN,
125                 AUDIO_DELAY,
126                 STILL_DURATION,
127                 SUBTITLE_STREAM,
128                 WITH_SUBTITLES,
129                 SUBTITLE_OFFSET,
130                 SUBTITLE_SCALE,
131                 DCI_METADATA,
132                 THUMBS,
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         DCPContentType const * dcp_content_type () const {
164                 boost::mutex::scoped_lock lm (_state_mutex);
165                 return _dcp_content_type;
166         }
167
168         Format const * format () const {
169                 boost::mutex::scoped_lock lm (_state_mutex);
170                 return _format;
171         }
172
173         Crop crop () const {
174                 boost::mutex::scoped_lock lm (_state_mutex);
175                 return _crop;
176         }
177
178         std::vector<Filter const *> filters () const {
179                 boost::mutex::scoped_lock lm (_state_mutex);
180                 return _filters;
181         }
182
183         Scaler const * scaler () const {
184                 boost::mutex::scoped_lock lm (_state_mutex);
185                 return _scaler;
186         }
187
188         SourceFrame dcp_trim_start () const {
189                 boost::mutex::scoped_lock lm (_state_mutex);
190                 return _dcp_trim_start;
191         }
192
193         SourceFrame dcp_trim_end () const {
194                 boost::mutex::scoped_lock lm (_state_mutex);
195                 return _dcp_trim_end;
196         }
197         
198         bool dcp_ab () const {
199                 boost::mutex::scoped_lock lm (_state_mutex);
200                 return _dcp_ab;
201         }
202
203         boost::shared_ptr<AudioStream> content_audio_stream () const {
204                 boost::mutex::scoped_lock lm (_state_mutex);
205                 return _content_audio_stream;
206         }
207
208         std::vector<std::string> external_audio () const {
209                 boost::mutex::scoped_lock lm (_state_mutex);
210                 return _external_audio;
211         }
212
213         bool use_content_audio () const {
214                 boost::mutex::scoped_lock lm (_state_mutex);
215                 return _use_content_audio;
216         }
217         
218         float audio_gain () const {
219                 boost::mutex::scoped_lock lm (_state_mutex);
220                 return _audio_gain;
221         }
222
223         int audio_delay () const {
224                 boost::mutex::scoped_lock lm (_state_mutex);
225                 return _audio_delay;
226         }
227
228         int still_duration () const {
229                 boost::mutex::scoped_lock lm (_state_mutex);
230                 return _still_duration;
231         }
232
233         boost::shared_ptr<SubtitleStream> subtitle_stream () const {
234                 boost::mutex::scoped_lock lm (_state_mutex);
235                 return _subtitle_stream;
236         }
237
238         bool with_subtitles () const {
239                 boost::mutex::scoped_lock lm (_state_mutex);
240                 return _with_subtitles;
241         }
242
243         int subtitle_offset () const {
244                 boost::mutex::scoped_lock lm (_state_mutex);
245                 return _subtitle_offset;
246         }
247
248         float subtitle_scale () const {
249                 boost::mutex::scoped_lock lm (_state_mutex);
250                 return _subtitle_scale;
251         }
252
253         std::string audio_language () const {
254                 boost::mutex::scoped_lock lm (_state_mutex);
255                 return _audio_language;
256         }
257         
258         std::string subtitle_language () const {
259                 boost::mutex::scoped_lock lm (_state_mutex);
260                 return _subtitle_language;
261         }
262         
263         std::string territory () const {
264                 boost::mutex::scoped_lock lm (_state_mutex);
265                 return _territory;
266         }
267         
268         std::string rating () const {
269                 boost::mutex::scoped_lock lm (_state_mutex);
270                 return _rating;
271         }
272         
273         std::string studio () const {
274                 boost::mutex::scoped_lock lm (_state_mutex);
275                 return _studio;
276         }
277         
278         std::string facility () const {
279                 boost::mutex::scoped_lock lm (_state_mutex);
280                 return _facility;
281         }
282         
283         std::string package_type () const {
284                 boost::mutex::scoped_lock lm (_state_mutex);
285                 return _package_type;
286         }
287
288         std::vector<SourceFrame> thumbs () const {
289                 boost::mutex::scoped_lock lm (_state_mutex);
290                 return _thumbs;
291         }
292         
293         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 frames_per_second () const {
319                 boost::mutex::scoped_lock lm (_state_mutex);
320                 return _frames_per_second;
321         }
322
323         boost::shared_ptr<AudioStream> audio_stream () const;
324
325         
326         /* SET */
327
328         void set_directory (std::string);
329         void set_name (std::string);
330         void set_use_dci_name (bool);
331         void set_content (std::string);
332         void set_dcp_content_type (DCPContentType const *);
333         void set_format (Format const *);
334         void set_crop (Crop);
335         void set_left_crop (int);
336         void set_right_crop (int);
337         void set_top_crop (int);
338         void set_bottom_crop (int);
339         void set_filters (std::vector<Filter const *>);
340         void set_scaler (Scaler const *);
341         void set_dcp_trim_start (int);
342         void set_dcp_trim_end (int);
343         void set_dcp_ab (bool);
344         void set_content_audio_stream (boost::shared_ptr<AudioStream>);
345         void set_external_audio (std::vector<std::string>);
346         void set_use_content_audio (bool);
347         void set_audio_gain (float);
348         void set_audio_delay (int);
349         void set_still_duration (int);
350         void set_subtitle_stream (boost::shared_ptr<SubtitleStream>);
351         void set_with_subtitles (bool);
352         void set_subtitle_offset (int);
353         void set_subtitle_scale (float);
354         void set_audio_language (std::string);
355         void set_subtitle_language (std::string);
356         void set_territory (std::string);
357         void set_rating (std::string);
358         void set_studio (std::string);
359         void set_facility (std::string);
360         void set_package_type (std::string);
361         void set_thumbs (std::vector<SourceFrame>);
362         void set_size (Size);
363         void set_length (SourceFrame);
364         void unset_length ();
365         void set_content_digest (std::string);
366         void set_content_audio_streams (std::vector<boost::shared_ptr<AudioStream> >);
367         void set_subtitle_streams (std::vector<boost::shared_ptr<SubtitleStream> >);
368         void set_frames_per_second (float);
369
370         /** Emitted when some property has changed */
371         mutable boost::signals2::signal<void (Property)> Changed;
372
373         /** Current version number of the state file */
374         static int const state_version;
375
376 private:
377         
378         /** Log to write to */
379         Log* _log;
380
381         /** Any running ExamineContentJob, or 0 */
382         boost::shared_ptr<ExamineContentJob> _examine_content_job;
383
384         /** The date that we should use in a DCI name */
385         boost::gregorian::date _dci_date;
386
387         std::string thumb_file_for_frame (SourceFrame) const;
388         std::string thumb_base_for_frame (SourceFrame) const;
389         void signal_changed (Property);
390         void examine_content_finished ();
391
392         /** Complete path to directory containing the film metadata;
393          *  must not be relative.
394          */
395         std::string _directory;
396         /** Mutex for _directory */
397         mutable boost::mutex _directory_mutex;
398         
399         /** Name for DVD-o-matic */
400         std::string _name;
401         /** True if a auto-generated DCI-compliant name should be used for our DCP */
402         bool _use_dci_name;
403         /** File or directory containing content; may be relative to our directory
404          *  or an absolute path.
405          */
406         std::string _content;
407         /** The type of content that this Film represents (feature, trailer etc.) */
408         DCPContentType const * _dcp_content_type;
409         /** The format to present this Film in (flat, scope, etc.) */
410         Format const * _format;
411         /** The crop to apply to the source */
412         Crop _crop;
413         /** Video filters that should be used when generating DCPs */
414         std::vector<Filter const *> _filters;
415         /** Scaler algorithm to use */
416         Scaler const * _scaler;
417         /** Frames to trim off the start of the source */
418         SourceFrame _dcp_trim_start;
419         /** Frames to trim off the end of the source */
420         SourceFrame _dcp_trim_end;
421         /** true to create an A/B comparison DCP, where the left half of the image
422             is the video without any filters or post-processing, and the right half
423             has the specified filters and post-processing.
424         */
425         bool _dcp_ab;
426         /** The audio stream to use from our content */
427         boost::shared_ptr<AudioStream> _content_audio_stream;
428         /** List of filenames of external audio files, in channel order
429             (L, R, C, Lfe, Ls, Rs)
430         */
431         std::vector<std::string> _external_audio;
432         /** true to use audio from our content file; false to use external audio */
433         bool _use_content_audio;
434         /** Gain to apply to audio in dB */
435         float _audio_gain;
436         /** Delay to apply to audio (positive moves audio later) in milliseconds */
437         int _audio_delay;
438         /** Duration to make still-sourced films (in seconds) */
439         int _still_duration;
440         boost::shared_ptr<SubtitleStream> _subtitle_stream;
441         /** True if subtitles should be shown for this film */
442         bool _with_subtitles;
443         /** y offset for placing subtitles, in source pixels; +ve is further down
444             the frame, -ve is further up.
445         */
446         int _subtitle_offset;
447         /** scale factor to apply to subtitles */
448         float _subtitle_scale;
449
450         /* DCI naming stuff */
451         std::string _audio_language;
452         std::string _subtitle_language;
453         std::string _territory;
454         std::string _rating;
455         std::string _studio;
456         std::string _facility;
457         std::string _package_type;
458
459         /* Data which are cached to speed things up */
460
461         /** Vector of frame indices for each of our `thumbnails' */
462         std::vector<SourceFrame> _thumbs;
463         /** Size, in pixels, of the source (ignoring cropping) */
464         Size _size;
465         /** Actual length of the source (in video frames) from examining it */
466         boost::optional<SourceFrame> _length;
467         /** MD5 digest of our content file */
468         std::string _content_digest;
469         /** The audio streams in our content */
470         std::vector<boost::shared_ptr<AudioStream> > _content_audio_streams;
471         /** A stream to represent possible external audio (will always exist) */
472         boost::shared_ptr<AudioStream> _external_audio_stream;
473         /** the subtitle streams that we can use */
474         std::vector<boost::shared_ptr<SubtitleStream> > _subtitle_streams;
475         /** Frames per second of the source */
476         float _frames_per_second;
477
478         /** true if our state has changed since we last saved it */
479         mutable bool _dirty;
480
481         /** Mutex for all state except _directory */
482         mutable boost::mutex _state_mutex;
483
484         friend class paths_test;
485 };
486
487 #endif