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