Rename use_source_audio to use_content_audio.
[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
48 /** @class Film
49  *  @brief A representation of a video with sound.
50  *
51  *  A representation of a piece of video (with sound), including naming,
52  *  the source content file, and how it should be presented in a DCP.
53  */
54 class Film : public boost::enable_shared_from_this<Film>
55 {
56 public:
57         Film (std::string d, bool must_exist = true);
58         Film (Film const &);
59         ~Film ();
60
61         std::string j2k_dir () const;
62         std::vector<std::string> audio_files () const;
63         std::pair<Position, std::string> thumb_subtitle (int) const;
64
65         void examine_content ();
66         void send_dcp_to_tms ();
67         void copy_from_dvd ();
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         bool content_is_dvd () const;
87
88         std::string thumb_file (int) const;
89         std::string thumb_base (int) const;
90         SourceFrame thumb_frame (int) const;
91
92         int target_audio_sample_rate () const;
93         
94         void write_metadata () const;
95         void read_metadata ();
96
97         Size cropped_size (Size) const;
98         boost::optional<SourceFrame> dcp_length () const;
99         std::string dci_name () const;
100         std::string dcp_name () const;
101
102         bool dirty () const {
103                 return _dirty;
104         }
105
106         int audio_channels () const;
107
108         void set_dci_date_today ();
109
110         enum Property {
111                 NONE,
112                 NAME,
113                 USE_DCI_NAME,
114                 CONTENT,
115                 DCP_CONTENT_TYPE,
116                 FORMAT,
117                 CROP,
118                 FILTERS,
119                 SCALER,
120                 DCP_TRIM_START,
121                 DCP_TRIM_END,
122                 DCP_AB,
123                 USE_CONTENT_AUDIO,
124                 AUDIO_STREAM,
125                 EXTERNAL_AUDIO,
126                 AUDIO_GAIN,
127                 AUDIO_DELAY,
128                 STILL_DURATION,
129                 SUBTITLE_STREAM,
130                 WITH_SUBTITLES,
131                 SUBTITLE_OFFSET,
132                 SUBTITLE_SCALE,
133                 DCI_METADATA,
134                 THUMBS,
135                 SIZE,
136                 LENGTH,
137                 AUDIO_STREAMS,
138                 SUBTITLE_STREAMS,
139                 FRAMES_PER_SECOND,
140         };
141
142
143         /* GET */
144
145         std::string directory () const {
146                 boost::mutex::scoped_lock lm (_directory_mutex);
147                 return _directory;
148         }
149
150         std::string name () const {
151                 boost::mutex::scoped_lock lm (_state_mutex);
152                 return _name;
153         }
154
155         bool use_dci_name () const {
156                 boost::mutex::scoped_lock lm (_state_mutex);
157                 return _use_dci_name;
158         }
159
160         std::string content () const {
161                 boost::mutex::scoped_lock lm (_state_mutex);
162                 return _content;
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         bool use_content_audio () const {
206                 boost::mutex::scoped_lock lm (_state_mutex);
207                 return _use_content_audio;
208         }
209
210         boost::optional<AudioStream> audio_stream () const {
211                 boost::mutex::scoped_lock lm (_state_mutex);
212                 return _audio_stream;
213         }
214
215         std::vector<std::string> external_audio () const {
216                 boost::mutex::scoped_lock lm (_state_mutex);
217                 return _external_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::optional<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         std::string audio_language () const {
256                 boost::mutex::scoped_lock lm (_state_mutex);
257                 return _audio_language;
258         }
259         
260         std::string subtitle_language () const {
261                 boost::mutex::scoped_lock lm (_state_mutex);
262                 return _subtitle_language;
263         }
264         
265         std::string territory () const {
266                 boost::mutex::scoped_lock lm (_state_mutex);
267                 return _territory;
268         }
269         
270         std::string rating () const {
271                 boost::mutex::scoped_lock lm (_state_mutex);
272                 return _rating;
273         }
274         
275         std::string studio () const {
276                 boost::mutex::scoped_lock lm (_state_mutex);
277                 return _studio;
278         }
279         
280         std::string facility () const {
281                 boost::mutex::scoped_lock lm (_state_mutex);
282                 return _facility;
283         }
284         
285         std::string package_type () const {
286                 boost::mutex::scoped_lock lm (_state_mutex);
287                 return _package_type;
288         }
289
290         std::vector<SourceFrame> thumbs () const {
291                 boost::mutex::scoped_lock lm (_state_mutex);
292                 return _thumbs;
293         }
294         
295         Size size () const {
296                 boost::mutex::scoped_lock lm (_state_mutex);
297                 return _size;
298         }
299
300         boost::optional<SourceFrame> length () const {
301                 boost::mutex::scoped_lock lm (_state_mutex);
302                 return _length;
303         }
304         
305         std::string content_digest () const {
306                 boost::mutex::scoped_lock lm (_state_mutex);
307                 return _content_digest;
308         }
309         
310         std::vector<AudioStream> audio_streams () const {
311                 boost::mutex::scoped_lock lm (_state_mutex);
312                 return _audio_streams;
313         }
314
315         std::vector<SubtitleStream> subtitle_streams () const {
316                 boost::mutex::scoped_lock lm (_state_mutex);
317                 return _subtitle_streams;
318         }
319         
320         float frames_per_second () const {
321                 boost::mutex::scoped_lock lm (_state_mutex);
322                 return _frames_per_second;
323         }
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         virtual 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_use_content_audio (bool);
345         void set_audio_stream (boost::optional<AudioStream>);
346         void set_external_audio (std::vector<std::string>);
347         void set_audio_gain (float);
348         void set_audio_delay (int);
349         void set_still_duration (int);
350         void set_subtitle_stream (boost::optional<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_audio_streams (std::vector<AudioStream>);
367         void set_subtitle_streams (std::vector<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 private:
374         
375         /** Log to write to */
376         Log* _log;
377
378         /** Any running ExamineContentJob, or 0 */
379         boost::shared_ptr<ExamineContentJob> _examine_content_job;
380
381         /** The date that we should use in a DCI name */
382         boost::gregorian::date _dci_date;
383
384         std::string thumb_file_for_frame (SourceFrame) const;
385         std::string thumb_base_for_frame (SourceFrame) const;
386         void signal_changed (Property);
387         void examine_content_finished ();
388
389         /** Complete path to directory containing the film metadata;
390          *  must not be relative.
391          */
392         std::string _directory;
393         /** Mutex for _directory */
394         mutable boost::mutex _directory_mutex;
395         
396         /** Name for DVD-o-matic */
397         std::string _name;
398         /** True if a auto-generated DCI-compliant name should be used for our DCP */
399         bool _use_dci_name;
400         /** File or directory containing content; may be relative to our directory
401          *  or an absolute path.
402          */
403         std::string _content;
404         /** The type of content that this Film represents (feature, trailer etc.) */
405         DCPContentType const * _dcp_content_type;
406         /** The format to present this Film in (flat, scope, etc.) */
407         Format const * _format;
408         /** The crop to apply to the source */
409         Crop _crop;
410         /** Video filters that should be used when generating DCPs */
411         std::vector<Filter const *> _filters;
412         /** Scaler algorithm to use */
413         Scaler const * _scaler;
414         /** Frames to trim off the start of the source */
415         SourceFrame _dcp_trim_start;
416         /** Frames to trim off the end of the source */
417         SourceFrame _dcp_trim_end;
418         /** true to create an A/B comparison DCP, where the left half of the image
419             is the video without any filters or post-processing, and the right half
420             has the specified filters and post-processing.
421         */
422         bool _dcp_ab;
423         /** true to use the audio from the content file, false to use external audio */
424         bool _use_content_audio;
425         boost::optional<AudioStream> _audio_stream;
426         std::vector<std::string> _external_audio;
427         /** Gain to apply to audio in dB */
428         float _audio_gain;
429         /** Delay to apply to audio (positive moves audio later) in milliseconds */
430         int _audio_delay;
431         /** Duration to make still-sourced films (in seconds) */
432         int _still_duration;
433         boost::optional<SubtitleStream> _subtitle_stream;
434         /** True if subtitles should be shown for this film */
435         bool _with_subtitles;
436         /** y offset for placing subtitles, in source pixels; +ve is further down
437             the frame, -ve is further up.
438         */
439         int _subtitle_offset;
440         /** scale factor to apply to subtitles */
441         float _subtitle_scale;
442
443         /* DCI naming stuff */
444         std::string _audio_language;
445         std::string _subtitle_language;
446         std::string _territory;
447         std::string _rating;
448         std::string _studio;
449         std::string _facility;
450         std::string _package_type;
451
452         /* Data which are cached to speed things up */
453
454         /** Vector of frame indices for each of our `thumbnails' */
455         std::vector<SourceFrame> _thumbs;
456         /** Size, in pixels, of the source (ignoring cropping) */
457         Size _size;
458         /** Actual length of the source (in video frames) from examining it */
459         boost::optional<SourceFrame> _length;
460         /** MD5 digest of our content file */
461         std::string _content_digest;
462         /** the audio streams that the source has */
463         std::vector<AudioStream> _audio_streams;
464         /** the subtitle streams that the source has */
465         std::vector<SubtitleStream> _subtitle_streams;
466         /** Frames per second of the source */
467         float _frames_per_second;
468
469         mutable bool _dirty;
470
471         /** Mutex for all state except _directory */
472         mutable boost::mutex _state_mutex;
473
474         friend class paths_test;
475 };
476
477 #endif