Remove film player, DVD ripping, alignment, screen configs; never finished and not...
[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
68         void make_dcp (bool);
69
70         /** @return Logger.
71          *  It is safe to call this from any thread.
72          */
73         Log* log () const {
74                 return _log;
75         }
76
77         int encoded_frames () const;
78         
79         std::string file (std::string f) const;
80         std::string dir (std::string d) const;
81
82         std::string content_path () const;
83         ContentType content_type () const;
84         
85         std::string thumb_file (int) const;
86         std::string thumb_base (int) const;
87         SourceFrame thumb_frame (int) const;
88
89         int target_audio_sample_rate () const;
90         
91         void write_metadata () const;
92         void read_metadata ();
93
94         Size cropped_size (Size) const;
95         boost::optional<SourceFrame> dcp_length () const;
96         std::string dci_name () const;
97         std::string dcp_name () const;
98
99         bool dirty () const {
100                 return _dirty;
101         }
102
103         int audio_channels () const;
104
105         void set_dci_date_today ();
106
107         enum Property {
108                 NONE,
109                 NAME,
110                 USE_DCI_NAME,
111                 CONTENT,
112                 DCP_CONTENT_TYPE,
113                 FORMAT,
114                 CROP,
115                 FILTERS,
116                 SCALER,
117                 DCP_TRIM_START,
118                 DCP_TRIM_END,
119                 DCP_AB,
120                 AUDIO_STREAM,
121                 AUDIO_GAIN,
122                 AUDIO_DELAY,
123                 STILL_DURATION,
124                 SUBTITLE_STREAM,
125                 WITH_SUBTITLES,
126                 SUBTITLE_OFFSET,
127                 SUBTITLE_SCALE,
128                 DCI_METADATA,
129                 THUMBS,
130                 SIZE,
131                 LENGTH,
132                 AUDIO_SAMPLE_RATE,
133                 HAS_SUBTITLES,
134                 AUDIO_STREAMS,
135                 SUBTITLE_STREAMS,
136                 FRAMES_PER_SECOND,
137         };
138
139
140         /* GET */
141
142         std::string directory () const {
143                 boost::mutex::scoped_lock lm (_directory_mutex);
144                 return _directory;
145         }
146
147         std::string name () const {
148                 boost::mutex::scoped_lock lm (_state_mutex);
149                 return _name;
150         }
151
152         bool use_dci_name () const {
153                 boost::mutex::scoped_lock lm (_state_mutex);
154                 return _use_dci_name;
155         }
156
157         std::string content () const {
158                 boost::mutex::scoped_lock lm (_state_mutex);
159                 return _content;
160         }
161
162         DCPContentType const * dcp_content_type () const {
163                 boost::mutex::scoped_lock lm (_state_mutex);
164                 return _dcp_content_type;
165         }
166
167         Format const * format () const {
168                 boost::mutex::scoped_lock lm (_state_mutex);
169                 return _format;
170         }
171
172         Crop crop () const {
173                 boost::mutex::scoped_lock lm (_state_mutex);
174                 return _crop;
175         }
176
177         std::vector<Filter const *> filters () const {
178                 boost::mutex::scoped_lock lm (_state_mutex);
179                 return _filters;
180         }
181
182         Scaler const * scaler () const {
183                 boost::mutex::scoped_lock lm (_state_mutex);
184                 return _scaler;
185         }
186
187         SourceFrame dcp_trim_start () const {
188                 boost::mutex::scoped_lock lm (_state_mutex);
189                 return _dcp_trim_start;
190         }
191
192         SourceFrame dcp_trim_end () const {
193                 boost::mutex::scoped_lock lm (_state_mutex);
194                 return _dcp_trim_end;
195         }
196         
197         bool dcp_ab () const {
198                 boost::mutex::scoped_lock lm (_state_mutex);
199                 return _dcp_ab;
200         }
201
202         int audio_stream_index () const {
203                 boost::mutex::scoped_lock lm (_state_mutex);
204                 return _audio_stream;
205         }
206
207         AudioStream audio_stream () const {
208                 boost::mutex::scoped_lock lm (_state_mutex);
209                 assert (_audio_stream < int (_audio_streams.size()));
210                 return _audio_streams[_audio_stream];
211         }
212         
213         float audio_gain () const {
214                 boost::mutex::scoped_lock lm (_state_mutex);
215                 return _audio_gain;
216         }
217
218         int audio_delay () const {
219                 boost::mutex::scoped_lock lm (_state_mutex);
220                 return _audio_delay;
221         }
222
223         int still_duration () const {
224                 boost::mutex::scoped_lock lm (_state_mutex);
225                 return _still_duration;
226         }
227
228         int subtitle_stream_index () const {
229                 boost::mutex::scoped_lock lm (_state_mutex);
230                 return _subtitle_stream;
231         }
232
233         SubtitleStream subtitle_stream () const {
234                 boost::mutex::scoped_lock lm (_state_mutex);
235                 assert (_subtitle_stream < int (_subtitle_streams.size()));
236                 return _subtitle_streams[_subtitle_stream];
237         }
238
239         bool with_subtitles () const {
240                 boost::mutex::scoped_lock lm (_state_mutex);
241                 return _with_subtitles;
242         }
243
244         int subtitle_offset () const {
245                 boost::mutex::scoped_lock lm (_state_mutex);
246                 return _subtitle_offset;
247         }
248
249         float subtitle_scale () const {
250                 boost::mutex::scoped_lock lm (_state_mutex);
251                 return _subtitle_scale;
252         }
253
254         std::string audio_language () const {
255                 boost::mutex::scoped_lock lm (_state_mutex);
256                 return _audio_language;
257         }
258         
259         std::string subtitle_language () const {
260                 boost::mutex::scoped_lock lm (_state_mutex);
261                 return _subtitle_language;
262         }
263         
264         std::string territory () const {
265                 boost::mutex::scoped_lock lm (_state_mutex);
266                 return _territory;
267         }
268         
269         std::string rating () const {
270                 boost::mutex::scoped_lock lm (_state_mutex);
271                 return _rating;
272         }
273         
274         std::string studio () const {
275                 boost::mutex::scoped_lock lm (_state_mutex);
276                 return _studio;
277         }
278         
279         std::string facility () const {
280                 boost::mutex::scoped_lock lm (_state_mutex);
281                 return _facility;
282         }
283         
284         std::string package_type () const {
285                 boost::mutex::scoped_lock lm (_state_mutex);
286                 return _package_type;
287         }
288
289         std::vector<SourceFrame> thumbs () const {
290                 boost::mutex::scoped_lock lm (_state_mutex);
291                 return _thumbs;
292         }
293         
294         Size size () const {
295                 boost::mutex::scoped_lock lm (_state_mutex);
296                 return _size;
297         }
298
299         boost::optional<SourceFrame> length () const {
300                 boost::mutex::scoped_lock lm (_state_mutex);
301                 return _length;
302         }
303         
304         int audio_sample_rate () const {
305                 boost::mutex::scoped_lock lm (_state_mutex);
306                 return _audio_sample_rate;
307         }
308         
309         std::string content_digest () const {
310                 boost::mutex::scoped_lock lm (_state_mutex);
311                 return _content_digest;
312         }
313         
314         bool has_subtitles () const {
315                 boost::mutex::scoped_lock lm (_state_mutex);
316                 return _has_subtitles;
317         }
318
319         std::vector<AudioStream> audio_streams () const {
320                 boost::mutex::scoped_lock lm (_state_mutex);
321                 return _audio_streams;
322         }
323
324         std::vector<SubtitleStream> subtitle_streams () const {
325                 boost::mutex::scoped_lock lm (_state_mutex);
326                 return _subtitle_streams;
327         }
328         
329         float frames_per_second () const {
330                 boost::mutex::scoped_lock lm (_state_mutex);
331                 return _frames_per_second;
332         }
333
334
335         /* SET */
336
337         void set_directory (std::string);
338         void set_name (std::string);
339         void set_use_dci_name (bool);
340         void set_content (std::string);
341         void set_dcp_content_type (DCPContentType const *);
342         void set_format (Format const *);
343         void set_crop (Crop);
344         void set_left_crop (int);
345         void set_right_crop (int);
346         void set_top_crop (int);
347         void set_bottom_crop (int);
348         void set_filters (std::vector<Filter const *>);
349         void set_scaler (Scaler const *);
350         void set_dcp_trim_start (int);
351         void set_dcp_trim_end (int);
352         void set_dcp_ab (bool);
353         void set_audio_stream (int);
354         void set_audio_gain (float);
355         void set_audio_delay (int);
356         void set_still_duration (int);
357         void set_subtitle_stream (int);
358         void set_with_subtitles (bool);
359         void set_subtitle_offset (int);
360         void set_subtitle_scale (float);
361         void set_audio_language (std::string);
362         void set_subtitle_language (std::string);
363         void set_territory (std::string);
364         void set_rating (std::string);
365         void set_studio (std::string);
366         void set_facility (std::string);
367         void set_package_type (std::string);
368         void set_thumbs (std::vector<SourceFrame>);
369         void set_size (Size);
370         void set_length (SourceFrame);
371         void unset_length ();
372         void set_audio_sample_rate (int);
373         void set_content_digest (std::string);
374         void set_has_subtitles (bool);
375         void set_audio_streams (std::vector<AudioStream>);
376         void set_subtitle_streams (std::vector<SubtitleStream>);
377         void set_frames_per_second (float);
378
379         /** Emitted when some property has changed */
380         mutable boost::signals2::signal<void (Property)> Changed;
381         
382 private:
383         
384         /** Log to write to */
385         Log* _log;
386
387         /** Any running ExamineContentJob, or 0 */
388         boost::shared_ptr<ExamineContentJob> _examine_content_job;
389
390         /** The date that we should use in a DCI name */
391         boost::gregorian::date _dci_date;
392
393         std::string thumb_file_for_frame (SourceFrame) const;
394         std::string thumb_base_for_frame (SourceFrame) const;
395         void signal_changed (Property);
396         void examine_content_finished ();
397
398         /** Complete path to directory containing the film metadata;
399          *  must not be relative.
400          */
401         std::string _directory;
402         /** Mutex for _directory */
403         mutable boost::mutex _directory_mutex;
404         
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         /** Frames to trim off the start of the source */
424         SourceFrame _dcp_trim_start;
425         /** Frames to trim off the end of the source */
426         SourceFrame _dcp_trim_end;
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<SourceFrame> _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<SourceFrame> _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         /** Mutex for all state except _directory */
484         mutable boost::mutex _state_mutex;
485
486         friend class paths_test;
487 };
488
489 #endif