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