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