Remove trim action settings.
[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         int 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<int> 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_FRAMES,
121                 DCP_AB,
122                 AUDIO_STREAM,
123                 AUDIO_GAIN,
124                 AUDIO_DELAY,
125                 STILL_DURATION,
126                 SUBTITLE_STREAM,
127                 WITH_SUBTITLES,
128                 SUBTITLE_OFFSET,
129                 SUBTITLE_SCALE,
130                 DCI_METADATA,
131                 THUMBS,
132                 SIZE,
133                 LENGTH,
134                 AUDIO_SAMPLE_RATE,
135                 HAS_SUBTITLES,
136                 AUDIO_STREAMS,
137                 SUBTITLE_STREAMS,
138                 FRAMES_PER_SECOND,
139         };
140
141
142         /* GET */
143
144         std::string directory () const {
145                 boost::mutex::scoped_lock lm (_directory_mutex);
146                 return _directory;
147         }
148
149         std::string name () const {
150                 boost::mutex::scoped_lock lm (_state_mutex);
151                 return _name;
152         }
153
154         bool use_dci_name () const {
155                 boost::mutex::scoped_lock lm (_state_mutex);
156                 return _use_dci_name;
157         }
158
159         std::string content () const {
160                 boost::mutex::scoped_lock lm (_state_mutex);
161                 return _content;
162         }
163
164         DCPContentType const * dcp_content_type () const {
165                 boost::mutex::scoped_lock lm (_state_mutex);
166                 return _dcp_content_type;
167         }
168
169         Format const * format () const {
170                 boost::mutex::scoped_lock lm (_state_mutex);
171                 return _format;
172         }
173
174         Crop crop () const {
175                 boost::mutex::scoped_lock lm (_state_mutex);
176                 return _crop;
177         }
178
179         std::vector<Filter const *> filters () const {
180                 boost::mutex::scoped_lock lm (_state_mutex);
181                 return _filters;
182         }
183
184         Scaler const * scaler () const {
185                 boost::mutex::scoped_lock lm (_state_mutex);
186                 return _scaler;
187         }
188
189         boost::optional<int> dcp_frames () const {
190                 boost::mutex::scoped_lock lm (_state_mutex);
191                 return _dcp_frames;
192         }
193
194         bool dcp_ab () const {
195                 boost::mutex::scoped_lock lm (_state_mutex);
196                 return _dcp_ab;
197         }
198
199         int audio_stream_index () const {
200                 boost::mutex::scoped_lock lm (_state_mutex);
201                 return _audio_stream;
202         }
203
204         AudioStream audio_stream () const {
205                 boost::mutex::scoped_lock lm (_state_mutex);
206                 assert (_audio_stream < int (_audio_streams.size()));
207                 return _audio_streams[_audio_stream];
208         }
209         
210         float audio_gain () const {
211                 boost::mutex::scoped_lock lm (_state_mutex);
212                 return _audio_gain;
213         }
214
215         int audio_delay () const {
216                 boost::mutex::scoped_lock lm (_state_mutex);
217                 return _audio_delay;
218         }
219
220         int still_duration () const {
221                 boost::mutex::scoped_lock lm (_state_mutex);
222                 return _still_duration;
223         }
224
225         int subtitle_stream_index () const {
226                 boost::mutex::scoped_lock lm (_state_mutex);
227                 return _subtitle_stream;
228         }
229
230         SubtitleStream subtitle_stream () const {
231                 boost::mutex::scoped_lock lm (_state_mutex);
232                 assert (_subtitle_stream < int (_subtitle_streams.size()));
233                 return _subtitle_streams[_subtitle_stream];
234         }
235
236         bool with_subtitles () const {
237                 boost::mutex::scoped_lock lm (_state_mutex);
238                 return _with_subtitles;
239         }
240
241         int subtitle_offset () const {
242                 boost::mutex::scoped_lock lm (_state_mutex);
243                 return _subtitle_offset;
244         }
245
246         float subtitle_scale () const {
247                 boost::mutex::scoped_lock lm (_state_mutex);
248                 return _subtitle_scale;
249         }
250
251         std::string audio_language () const {
252                 boost::mutex::scoped_lock lm (_state_mutex);
253                 return _audio_language;
254         }
255         
256         std::string subtitle_language () const {
257                 boost::mutex::scoped_lock lm (_state_mutex);
258                 return _subtitle_language;
259         }
260         
261         std::string territory () const {
262                 boost::mutex::scoped_lock lm (_state_mutex);
263                 return _territory;
264         }
265         
266         std::string rating () const {
267                 boost::mutex::scoped_lock lm (_state_mutex);
268                 return _rating;
269         }
270         
271         std::string studio () const {
272                 boost::mutex::scoped_lock lm (_state_mutex);
273                 return _studio;
274         }
275         
276         std::string facility () const {
277                 boost::mutex::scoped_lock lm (_state_mutex);
278                 return _facility;
279         }
280         
281         std::string package_type () const {
282                 boost::mutex::scoped_lock lm (_state_mutex);
283                 return _package_type;
284         }
285
286         std::vector<int> thumbs () const {
287                 boost::mutex::scoped_lock lm (_state_mutex);
288                 return _thumbs;
289         }
290         
291         Size size () const {
292                 boost::mutex::scoped_lock lm (_state_mutex);
293                 return _size;
294         }
295
296         boost::optional<int> length () const {
297                 boost::mutex::scoped_lock lm (_state_mutex);
298                 return _length;
299         }
300         
301         int audio_sample_rate () const {
302                 boost::mutex::scoped_lock lm (_state_mutex);
303                 return _audio_sample_rate;
304         }
305         
306         std::string content_digest () const {
307                 boost::mutex::scoped_lock lm (_state_mutex);
308                 return _content_digest;
309         }
310         
311         bool has_subtitles () const {
312                 boost::mutex::scoped_lock lm (_state_mutex);
313                 return _has_subtitles;
314         }
315
316         std::vector<AudioStream> audio_streams () const {
317                 boost::mutex::scoped_lock lm (_state_mutex);
318                 return _audio_streams;
319         }
320
321         std::vector<SubtitleStream> subtitle_streams () const {
322                 boost::mutex::scoped_lock lm (_state_mutex);
323                 return _subtitle_streams;
324         }
325         
326         float frames_per_second () const {
327                 boost::mutex::scoped_lock lm (_state_mutex);
328                 return _frames_per_second;
329         }
330
331
332         /* SET */
333
334         void set_directory (std::string);
335         void set_name (std::string);
336         void set_use_dci_name (bool);
337         virtual void set_content (std::string);
338         void set_dcp_content_type (DCPContentType const *);
339         void set_format (Format const *);
340         void set_crop (Crop);
341         void set_left_crop (int);
342         void set_right_crop (int);
343         void set_top_crop (int);
344         void set_bottom_crop (int);
345         void set_filters (std::vector<Filter const *>);
346         void set_scaler (Scaler const *);
347         void set_dcp_frames (int);
348         void unset_dcp_frames ();
349         void set_dcp_ab (bool);
350         void set_audio_stream (int);
351         void set_audio_gain (float);
352         void set_audio_delay (int);
353         void set_still_duration (int);
354         void set_subtitle_stream (int);
355         void set_with_subtitles (bool);
356         void set_subtitle_offset (int);
357         void set_subtitle_scale (float);
358         void set_audio_language (std::string);
359         void set_subtitle_language (std::string);
360         void set_territory (std::string);
361         void set_rating (std::string);
362         void set_studio (std::string);
363         void set_facility (std::string);
364         void set_package_type (std::string);
365         void set_thumbs (std::vector<int>);
366         void set_size (Size);
367         void set_length (int);
368         void unset_length ();
369         void set_audio_sample_rate (int);
370         void set_content_digest (std::string);
371         void set_has_subtitles (bool);
372         void set_audio_streams (std::vector<AudioStream>);
373         void set_subtitle_streams (std::vector<SubtitleStream>);
374         void set_frames_per_second (float);
375
376         /** Emitted when some property has changed */
377         mutable boost::signals2::signal<void (Property)> Changed;
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         boost::gregorian::date _dci_date;
388
389         std::string thumb_file_for_frame (int) const;
390         std::string thumb_base_for_frame (int) const;
391         void signal_changed (Property);
392         void examine_content_finished ();
393         
394         /** Complete path to directory containing the film metadata;
395          *  must not be relative.
396          */
397         std::string _directory;
398         /** Mutex for _directory */
399         mutable boost::mutex _directory_mutex;
400         
401         /** Name for DVD-o-matic */
402         std::string _name;
403         /** True if a auto-generated DCI-compliant name should be used for our DCP */
404         bool _use_dci_name;
405         /** File or directory containing content; may be relative to our directory
406          *  or an absolute path.
407          */
408         std::string _content;
409         /** The type of content that this Film represents (feature, trailer etc.) */
410         DCPContentType const * _dcp_content_type;
411         /** The format to present this Film in (flat, scope, etc.) */
412         Format const * _format;
413         /** The crop to apply to the source */
414         Crop _crop;
415         /** Video filters that should be used when generating DCPs */
416         std::vector<Filter const *> _filters;
417         /** Scaler algorithm to use */
418         Scaler const * _scaler;
419         /** Maximum number of frames to put in the DCP, if applicable */
420         boost::optional<int> _dcp_frames;
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         /** An index into our _audio_streams vector for the stream to use for audio, or -1 if there is none */
427         int _audio_stream;
428         /** Gain to apply to audio in dB */
429         float _audio_gain;
430         /** Delay to apply to audio (positive moves audio later) in milliseconds */
431         int _audio_delay;
432         /** Duration to make still-sourced films (in seconds) */
433         int _still_duration;
434         /** An index into our _subtitle_streams vector for the stream to use for subtitles, or -1 if there is none */
435         int _subtitle_stream;
436         /** True if subtitles should be shown for this film */
437         bool _with_subtitles;
438         /** y offset for placing subtitles, in source pixels; +ve is further down
439             the frame, -ve is further up.
440         */
441         int _subtitle_offset;
442         /** scale factor to apply to subtitles */
443         float _subtitle_scale;
444
445         /* DCI naming stuff */
446         std::string _audio_language;
447         std::string _subtitle_language;
448         std::string _territory;
449         std::string _rating;
450         std::string _studio;
451         std::string _facility;
452         std::string _package_type;
453
454         /* Data which are cached to speed things up */
455
456         /** Vector of frame indices for each of our `thumbnails' */
457         std::vector<int> _thumbs;
458         /** Size, in pixels, of the source (ignoring cropping) */
459         Size _size;
460         /** Actual length of the source (in video frames) from examining it */
461         boost::optional<int> _length;
462         /** Sample rate of the source audio, in Hz */
463         int _audio_sample_rate;
464         /** MD5 digest of our content file */
465         std::string _content_digest;
466         /** true if the source has subtitles */
467         bool _has_subtitles;
468         /** the audio streams that the source has */
469         std::vector<AudioStream> _audio_streams;
470         /** the subtitle streams that the source has */
471         std::vector<SubtitleStream> _subtitle_streams;
472         /** Frames per second of the source */
473         float _frames_per_second;
474
475         mutable bool _dirty;
476
477         /** Mutex for all state except _directory */
478         mutable boost::mutex _state_mutex;
479
480         friend class paths_test;
481 };
482
483 #endif