GUI and metadata for external 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_SOURCE_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_SAMPLE_RATE,
138                 HAS_SUBTITLES,
139                 AUDIO_STREAMS,
140                 SUBTITLE_STREAMS,
141                 FRAMES_PER_SECOND,
142         };
143
144
145         /* GET */
146
147         std::string directory () const {
148                 boost::mutex::scoped_lock lm (_directory_mutex);
149                 return _directory;
150         }
151
152         std::string name () const {
153                 boost::mutex::scoped_lock lm (_state_mutex);
154                 return _name;
155         }
156
157         bool use_dci_name () const {
158                 boost::mutex::scoped_lock lm (_state_mutex);
159                 return _use_dci_name;
160         }
161
162         std::string content () const {
163                 boost::mutex::scoped_lock lm (_state_mutex);
164                 return _content;
165         }
166
167         DCPContentType const * dcp_content_type () const {
168                 boost::mutex::scoped_lock lm (_state_mutex);
169                 return _dcp_content_type;
170         }
171
172         Format const * format () const {
173                 boost::mutex::scoped_lock lm (_state_mutex);
174                 return _format;
175         }
176
177         Crop crop () const {
178                 boost::mutex::scoped_lock lm (_state_mutex);
179                 return _crop;
180         }
181
182         std::vector<Filter const *> filters () const {
183                 boost::mutex::scoped_lock lm (_state_mutex);
184                 return _filters;
185         }
186
187         Scaler const * scaler () const {
188                 boost::mutex::scoped_lock lm (_state_mutex);
189                 return _scaler;
190         }
191
192         SourceFrame dcp_trim_start () const {
193                 boost::mutex::scoped_lock lm (_state_mutex);
194                 return _dcp_trim_start;
195         }
196
197         SourceFrame dcp_trim_end () const {
198                 boost::mutex::scoped_lock lm (_state_mutex);
199                 return _dcp_trim_end;
200         }
201         
202         bool dcp_ab () const {
203                 boost::mutex::scoped_lock lm (_state_mutex);
204                 return _dcp_ab;
205         }
206
207         bool use_source_audio () const {
208                 boost::mutex::scoped_lock lm (_state_mutex);
209                 return _use_source_audio;
210         }
211
212         int audio_stream_index () const {
213                 boost::mutex::scoped_lock lm (_state_mutex);
214                 return _audio_stream;
215         }
216
217         AudioStream audio_stream () const {
218                 boost::mutex::scoped_lock lm (_state_mutex);
219                 assert (_audio_stream < int (_audio_streams.size()));
220                 return _audio_streams[_audio_stream];
221         }
222
223         std::vector<std::string> external_audio () const {
224                 boost::mutex::scoped_lock lm (_state_mutex);
225                 return _external_audio;
226         }
227         
228         float audio_gain () const {
229                 boost::mutex::scoped_lock lm (_state_mutex);
230                 return _audio_gain;
231         }
232
233         int audio_delay () const {
234                 boost::mutex::scoped_lock lm (_state_mutex);
235                 return _audio_delay;
236         }
237
238         int still_duration () const {
239                 boost::mutex::scoped_lock lm (_state_mutex);
240                 return _still_duration;
241         }
242
243         int subtitle_stream_index () const {
244                 boost::mutex::scoped_lock lm (_state_mutex);
245                 return _subtitle_stream;
246         }
247
248         SubtitleStream subtitle_stream () const {
249                 boost::mutex::scoped_lock lm (_state_mutex);
250                 assert (_subtitle_stream < int (_subtitle_streams.size()));
251                 return _subtitle_streams[_subtitle_stream];
252         }
253
254         bool with_subtitles () const {
255                 boost::mutex::scoped_lock lm (_state_mutex);
256                 return _with_subtitles;
257         }
258
259         int subtitle_offset () const {
260                 boost::mutex::scoped_lock lm (_state_mutex);
261                 return _subtitle_offset;
262         }
263
264         float subtitle_scale () const {
265                 boost::mutex::scoped_lock lm (_state_mutex);
266                 return _subtitle_scale;
267         }
268
269         std::string audio_language () const {
270                 boost::mutex::scoped_lock lm (_state_mutex);
271                 return _audio_language;
272         }
273         
274         std::string subtitle_language () const {
275                 boost::mutex::scoped_lock lm (_state_mutex);
276                 return _subtitle_language;
277         }
278         
279         std::string territory () const {
280                 boost::mutex::scoped_lock lm (_state_mutex);
281                 return _territory;
282         }
283         
284         std::string rating () const {
285                 boost::mutex::scoped_lock lm (_state_mutex);
286                 return _rating;
287         }
288         
289         std::string studio () const {
290                 boost::mutex::scoped_lock lm (_state_mutex);
291                 return _studio;
292         }
293         
294         std::string facility () const {
295                 boost::mutex::scoped_lock lm (_state_mutex);
296                 return _facility;
297         }
298         
299         std::string package_type () const {
300                 boost::mutex::scoped_lock lm (_state_mutex);
301                 return _package_type;
302         }
303
304         std::vector<SourceFrame> thumbs () const {
305                 boost::mutex::scoped_lock lm (_state_mutex);
306                 return _thumbs;
307         }
308         
309         Size size () const {
310                 boost::mutex::scoped_lock lm (_state_mutex);
311                 return _size;
312         }
313
314         boost::optional<SourceFrame> length () const {
315                 boost::mutex::scoped_lock lm (_state_mutex);
316                 return _length;
317         }
318         
319         int audio_sample_rate () const {
320                 boost::mutex::scoped_lock lm (_state_mutex);
321                 return _audio_sample_rate;
322         }
323         
324         std::string content_digest () const {
325                 boost::mutex::scoped_lock lm (_state_mutex);
326                 return _content_digest;
327         }
328         
329         bool has_subtitles () const {
330                 boost::mutex::scoped_lock lm (_state_mutex);
331                 return _has_subtitles;
332         }
333
334         std::vector<AudioStream> audio_streams () const {
335                 boost::mutex::scoped_lock lm (_state_mutex);
336                 return _audio_streams;
337         }
338
339         std::vector<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                 return _frames_per_second;
347         }
348
349
350         /* SET */
351
352         void set_directory (std::string);
353         void set_name (std::string);
354         void set_use_dci_name (bool);
355         virtual void set_content (std::string);
356         void set_dcp_content_type (DCPContentType const *);
357         void set_format (Format const *);
358         void set_crop (Crop);
359         void set_left_crop (int);
360         void set_right_crop (int);
361         void set_top_crop (int);
362         void set_bottom_crop (int);
363         void set_filters (std::vector<Filter const *>);
364         void set_scaler (Scaler const *);
365         void set_dcp_trim_start (int);
366         void set_dcp_trim_end (int);
367         void set_dcp_ab (bool);
368         void set_use_source_audio (bool);
369         void set_audio_stream (int);
370         void set_external_audio (std::vector<std::string>);
371         void set_audio_gain (float);
372         void set_audio_delay (int);
373         void set_still_duration (int);
374         void set_subtitle_stream (int);
375         void set_with_subtitles (bool);
376         void set_subtitle_offset (int);
377         void set_subtitle_scale (float);
378         void set_audio_language (std::string);
379         void set_subtitle_language (std::string);
380         void set_territory (std::string);
381         void set_rating (std::string);
382         void set_studio (std::string);
383         void set_facility (std::string);
384         void set_package_type (std::string);
385         void set_thumbs (std::vector<SourceFrame>);
386         void set_size (Size);
387         void set_length (SourceFrame);
388         void unset_length ();
389         void set_audio_sample_rate (int);
390         void set_content_digest (std::string);
391         void set_has_subtitles (bool);
392         void set_audio_streams (std::vector<AudioStream>);
393         void set_subtitle_streams (std::vector<SubtitleStream>);
394         void set_frames_per_second (float);
395
396         /** Emitted when some property has changed */
397         mutable boost::signals2::signal<void (Property)> Changed;
398         
399 private:
400         
401         /** Log to write to */
402         Log* _log;
403
404         /** Any running ExamineContentJob, or 0 */
405         boost::shared_ptr<ExamineContentJob> _examine_content_job;
406
407         /** The date that we should use in a DCI name */
408         boost::gregorian::date _dci_date;
409
410         std::string thumb_file_for_frame (SourceFrame) const;
411         std::string thumb_base_for_frame (SourceFrame) const;
412         void signal_changed (Property);
413         void examine_content_finished ();
414
415         /** Complete path to directory containing the film metadata;
416          *  must not be relative.
417          */
418         std::string _directory;
419         /** Mutex for _directory */
420         mutable boost::mutex _directory_mutex;
421         
422         /** Name for DVD-o-matic */
423         std::string _name;
424         /** True if a auto-generated DCI-compliant name should be used for our DCP */
425         bool _use_dci_name;
426         /** File or directory containing content; may be relative to our directory
427          *  or an absolute path.
428          */
429         std::string _content;
430         /** The type of content that this Film represents (feature, trailer etc.) */
431         DCPContentType const * _dcp_content_type;
432         /** The format to present this Film in (flat, scope, etc.) */
433         Format const * _format;
434         /** The crop to apply to the source */
435         Crop _crop;
436         /** Video filters that should be used when generating DCPs */
437         std::vector<Filter const *> _filters;
438         /** Scaler algorithm to use */
439         Scaler const * _scaler;
440         /** Frames to trim off the start of the source */
441         SourceFrame _dcp_trim_start;
442         /** Frames to trim off the end of the source */
443         SourceFrame _dcp_trim_end;
444         /** true to create an A/B comparison DCP, where the left half of the image
445             is the video without any filters or post-processing, and the right half
446             has the specified filters and post-processing.
447         */
448         bool _dcp_ab;
449         bool _use_source_audio;
450         /** An index into our _audio_streams vector for the stream to use for audio, or -1 if there is none */
451         int _audio_stream;
452         std::vector<std::string> _external_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         /** An index into our _subtitle_streams vector for the stream to use for subtitles, or -1 if there is none */
460         int _subtitle_stream;
461         /** True if subtitles should be shown for this film */
462         bool _with_subtitles;
463         /** y offset for placing subtitles, in source pixels; +ve is further down
464             the frame, -ve is further up.
465         */
466         int _subtitle_offset;
467         /** scale factor to apply to subtitles */
468         float _subtitle_scale;
469
470         /* DCI naming stuff */
471         std::string _audio_language;
472         std::string _subtitle_language;
473         std::string _territory;
474         std::string _rating;
475         std::string _studio;
476         std::string _facility;
477         std::string _package_type;
478
479         /* Data which are cached to speed things up */
480
481         /** Vector of frame indices for each of our `thumbnails' */
482         std::vector<SourceFrame> _thumbs;
483         /** Size, in pixels, of the source (ignoring cropping) */
484         Size _size;
485         /** Actual length of the source (in video frames) from examining it */
486         boost::optional<SourceFrame> _length;
487         /** Sample rate of the source audio, in Hz */
488         int _audio_sample_rate;
489         /** MD5 digest of our content file */
490         std::string _content_digest;
491         /** true if the source has subtitles */
492         bool _has_subtitles;
493         /** the audio streams that the source has */
494         std::vector<AudioStream> _audio_streams;
495         /** the subtitle streams that the source has */
496         std::vector<SubtitleStream> _subtitle_streams;
497         /** Frames per second of the source */
498         float _frames_per_second;
499
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