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