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