Try to fix range UI a little.
[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 extern "C" {
35 #include <libavcodec/avcodec.h>
36 }
37 #include "dcp_content_type.h"
38 #include "util.h"
39 #include "stream.h"
40 #include "trim_action.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         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_FRAMES,
119                 DCP_TRIM_ACTION,
120                 DCP_AB,
121                 AUDIO_STREAM,
122                 AUDIO_GAIN,
123                 AUDIO_DELAY,
124                 STILL_DURATION,
125                 SUBTITLE_STREAM,
126                 WITH_SUBTITLES,
127                 SUBTITLE_OFFSET,
128                 SUBTITLE_SCALE,
129                 DCI_METADATA,
130                 THUMBS,
131                 SIZE,
132                 LENGTH,
133                 AUDIO_SAMPLE_RATE,
134                 HAS_SUBTITLES,
135                 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 (_state_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         boost::optional<int> dcp_frames () const {
189                 boost::mutex::scoped_lock lm (_state_mutex);
190                 return _dcp_frames;
191         }
192
193         TrimAction dcp_trim_action () const {
194                 boost::mutex::scoped_lock lm (_state_mutex);
195                 return _dcp_trim_action;
196         }
197
198         bool dcp_ab () const {
199                 boost::mutex::scoped_lock lm (_state_mutex);
200                 return _dcp_ab;
201         }
202
203         int audio_stream_index () const {
204                 boost::mutex::scoped_lock lm (_state_mutex);
205                 return _audio_stream;
206         }
207
208         AudioStream audio_stream () const {
209                 boost::mutex::scoped_lock lm (_state_mutex);
210                 assert (_audio_stream < int (_audio_streams.size()));
211                 return _audio_streams[_audio_stream];
212         }
213         
214         float audio_gain () const {
215                 boost::mutex::scoped_lock lm (_state_mutex);
216                 return _audio_gain;
217         }
218
219         int audio_delay () const {
220                 boost::mutex::scoped_lock lm (_state_mutex);
221                 return _audio_delay;
222         }
223
224         int still_duration () const {
225                 boost::mutex::scoped_lock lm (_state_mutex);
226                 return _still_duration;
227         }
228
229         int subtitle_stream_index () const {
230                 boost::mutex::scoped_lock lm (_state_mutex);
231                 return _subtitle_stream;
232         }
233
234         SubtitleStream subtitle_stream () const {
235                 boost::mutex::scoped_lock lm (_state_mutex);
236                 assert (_subtitle_stream < int (_subtitle_streams.size()));
237                 return _subtitle_streams[_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<int> 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<int> length () const {
301                 boost::mutex::scoped_lock lm (_state_mutex);
302                 return _length;
303         }
304         
305         int audio_sample_rate () const {
306                 boost::mutex::scoped_lock lm (_state_mutex);
307                 return _audio_sample_rate;
308         }
309         
310         std::string content_digest () const {
311                 boost::mutex::scoped_lock lm (_state_mutex);
312                 return _content_digest;
313         }
314         
315         bool has_subtitles () const {
316                 boost::mutex::scoped_lock lm (_state_mutex);
317                 return _has_subtitles;
318         }
319
320         std::vector<AudioStream> audio_streams () const {
321                 boost::mutex::scoped_lock lm (_state_mutex);
322                 return _audio_streams;
323         }
324
325         std::vector<SubtitleStream> subtitle_streams () const {
326                 boost::mutex::scoped_lock lm (_state_mutex);
327                 return _subtitle_streams;
328         }
329         
330         float frames_per_second () const {
331                 boost::mutex::scoped_lock lm (_state_mutex);
332                 return _frames_per_second;
333         }
334
335
336         /* SET */
337
338         void set_directory (std::string);
339         void set_name (std::string);
340         void set_use_dci_name (bool);
341         virtual void set_content (std::string);
342         void set_dcp_content_type (DCPContentType const *);
343         void set_format (Format const *);
344         void set_crop (Crop);
345         void set_left_crop (int);
346         void set_right_crop (int);
347         void set_top_crop (int);
348         void set_bottom_crop (int);
349         void set_filters (std::vector<Filter const *>);
350         void set_scaler (Scaler const *);
351         void set_dcp_frames (int);
352         void unset_dcp_frames ();
353         void set_dcp_trim_action (TrimAction);
354         void set_dcp_ab (bool);
355         void set_audio_stream (int);
356         void set_audio_gain (float);
357         void set_audio_delay (int);
358         void set_still_duration (int);
359         void set_subtitle_stream (int);
360         void set_with_subtitles (bool);
361         void set_subtitle_offset (int);
362         void set_subtitle_scale (float);
363         void set_audio_language (std::string);
364         void set_subtitle_language (std::string);
365         void set_territory (std::string);
366         void set_rating (std::string);
367         void set_studio (std::string);
368         void set_facility (std::string);
369         void set_package_type (std::string);
370         void set_thumbs (std::vector<int>);
371         void set_size (Size);
372         void set_length (int);
373         void unset_length ();
374         void set_audio_sample_rate (int);
375         void set_content_digest (std::string);
376         void set_has_subtitles (bool);
377         void set_audio_streams (std::vector<AudioStream>);
378         void set_subtitle_streams (std::vector<SubtitleStream>);
379         void set_frames_per_second (float);
380
381         /** Emitted when some property has changed */
382         mutable boost::signals2::signal<void (Property)> Changed;
383         
384 private:
385         
386         /** Log to write to */
387         Log* _log;
388
389         /** Any running ExamineContentJob, or 0 */
390         boost::shared_ptr<ExamineContentJob> _examine_content_job;
391
392         std::string thumb_file_for_frame (int) const;
393         std::string thumb_file_for_frame_locked (int) const;
394         std::string thumb_base_for_frame (int) const;
395         std::string thumb_base_for_frame_locked (int) const;
396         void signal_changed (Property);
397         std::string file_locked (std::string) const;
398         std::string dir_locked (std::string d) const;
399         void examine_content_finished ();
400         
401         /** Complete path to directory containing the film metadata;
402          *  must not be relative.
403          */
404         std::string _directory;
405         /** Name for DVD-o-matic */
406         std::string _name;
407         /** True if a auto-generated DCI-compliant name should be used for our DCP */
408         bool _use_dci_name;
409         /** File or directory containing content; may be relative to our directory
410          *  or an absolute path.
411          */
412         std::string _content;
413         /** The type of content that this Film represents (feature, trailer etc.) */
414         DCPContentType const * _dcp_content_type;
415         /** The format to present this Film in (flat, scope, etc.) */
416         Format const * _format;
417         /** The crop to apply to the source */
418         Crop _crop;
419         /** Video filters that should be used when generating DCPs */
420         std::vector<Filter const *> _filters;
421         /** Scaler algorithm to use */
422         Scaler const * _scaler;
423         /** Maximum number of frames to put in the DCP, if applicable */
424         boost::optional<int> _dcp_frames;
425         /** What to do with audio when trimming DCPs */
426         TrimAction _dcp_trim_action;
427         /** true to create an A/B comparison DCP, where the left half of the image
428             is the video without any filters or post-processing, and the right half
429             has the specified filters and post-processing.
430         */
431         bool _dcp_ab;
432         /** An index into our _audio_streams vector for the stream to use for audio, or -1 if there is none */
433         int _audio_stream;
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         /** An index into our _subtitle_streams vector for the stream to use for subtitles, or -1 if there is none */
441         int _subtitle_stream;
442         /** True if subtitles should be shown for this film */
443         bool _with_subtitles;
444         /** y offset for placing subtitles, in source pixels; +ve is further down
445             the frame, -ve is further up.
446         */
447         int _subtitle_offset;
448         /** scale factor to apply to subtitles */
449         float _subtitle_scale;
450
451         /* DCI naming stuff */
452         std::string _audio_language;
453         std::string _subtitle_language;
454         std::string _territory;
455         std::string _rating;
456         std::string _studio;
457         std::string _facility;
458         std::string _package_type;
459
460         /* Data which are cached to speed things up */
461
462         /** Vector of frame indices for each of our `thumbnails' */
463         std::vector<int> _thumbs;
464         /** Size, in pixels, of the source (ignoring cropping) */
465         Size _size;
466         /** Actual length of the source (in video frames) from examining it */
467         boost::optional<int> _length;
468         /** Sample rate of the source audio, in Hz */
469         int _audio_sample_rate;
470         /** MD5 digest of our content file */
471         std::string _content_digest;
472         /** true if the source has subtitles */
473         bool _has_subtitles;
474         /** the audio streams that the source has */
475         std::vector<AudioStream> _audio_streams;
476         /** the subtitle streams that the source has */
477         std::vector<SubtitleStream> _subtitle_streams;
478         /** Frames per second of the source */
479         float _frames_per_second;
480
481         mutable bool _dirty;
482
483         mutable boost::mutex _state_mutex;
484
485         friend class paths_test;
486 };
487
488 #endif