294b9aa10ebcb301f220a6850acabd64016b799e
[dcpomatic.git] / src / lib / film_state.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_state.h
21  *  @brief The state of a Film.  This is separate from Film so that
22  *  state can easily be copied and kept around for reference
23  *  by long-running jobs.  This avoids the jobs getting confused
24  *  by the user changing Film settings during their run.
25  */
26
27 #ifndef DVDOMATIC_FILM_STATE_H
28 #define DVDOMATIC_FILM_STATE_H
29
30 #include <sigc++/signal.h>
31 extern "C" {
32 #include <libavcodec/avcodec.h>
33 #include <libswscale/swscale.h>
34 }
35 #include "scaler.h"
36 #include "util.h"
37 #include "trim_action.h"
38 #include "stream.h"
39
40 class Format;
41 class DCPContentType;
42 class Filter;
43
44 /** @class FilmState
45  *  @brief The state of a Film.
46  *
47  *  This is separate from Film so that state can easily be copied and
48  *  kept around for reference by long-running jobs.  This avoids the
49  *  jobs getting confused by the user changing Film settings during
50  *  their run.
51  */
52
53 class FilmState
54 {
55 public:
56         FilmState ()
57                 : _use_dci_name (false)
58                 , _dcp_content_type (0)
59                 , _format (0)
60                 , _scaler (Scaler::from_id ("bicubic"))
61                 , _dcp_frames (0)
62                 , _dcp_trim_action (CUT)
63                 , _dcp_ab (false)
64                 , _audio_stream (-1)
65                 , _audio_gain (0)
66                 , _audio_delay (0)
67                 , _still_duration (10)
68                 , _subtitle_stream (-1)
69                 , _with_subtitles (false)
70                 , _subtitle_offset (0)
71                 , _subtitle_scale (1)
72                 , _length (0)
73                 , _audio_sample_rate (0)
74                 , _has_subtitles (false)
75                 , _frames_per_second (0)
76                 , _dirty (false)
77         {}
78
79         FilmState (FilmState const & o)
80                 : _directory         (o._directory)
81                 , _name              (o._name)
82                 , _use_dci_name      (o._use_dci_name)
83                 , _content           (o._content)
84                 , _dcp_content_type  (o._dcp_content_type)
85                 , _format            (o._format)
86                 , _crop              (o._crop)
87                 , _filters           (o._filters)
88                 , _scaler            (o._scaler)
89                 , _dcp_frames        (o._dcp_frames)
90                 , _dcp_trim_action   (o._dcp_trim_action)
91                 , _dcp_ab            (o._dcp_ab)
92                 , _audio_stream      (o._audio_stream)
93                 , _audio_gain        (o._audio_gain)
94                 , _audio_delay       (o._audio_delay)
95                 , _still_duration    (o._still_duration)
96                 , _subtitle_stream   (o._subtitle_stream)
97                 , _with_subtitles    (o._with_subtitles)
98                 , _subtitle_offset   (o._subtitle_offset)
99                 , _subtitle_scale    (o._subtitle_scale)
100                 , _audio_language    (o._audio_language)
101                 , _subtitle_language (o._subtitle_language)
102                 , _territory         (o._territory)
103                 , _rating            (o._rating)
104                 , _studio            (o._studio)
105                 , _facility          (o._facility)
106                 , _package_type      (o._package_type)
107                 , _thumbs            (o._thumbs)
108                 , _size              (o._size)
109                 , _length            (o._length)
110                 , _audio_sample_rate (o._audio_sample_rate)
111                 , _content_digest    (o._content_digest)
112                 , _has_subtitles     (o._has_subtitles)
113                 , _audio_streams     (o._audio_streams)
114                 , _subtitle_streams  (o._subtitle_streams)
115                 , _frames_per_second (o._frames_per_second)
116                 , _dirty             (o._dirty)
117         {}
118
119         virtual ~FilmState () {}
120
121         std::string file (std::string f) const;
122         std::string dir (std::string d) const;
123
124         std::string content_path () const;
125         ContentType content_type () const;
126         
127         bool content_is_dvd () const;
128
129         std::string thumb_file (int) const;
130         std::string thumb_base (int) const;
131         int thumb_frame (int) const;
132
133         int target_sample_rate () const;
134         
135         void write_metadata () const;
136         void read_metadata ();
137
138         Size cropped_size (Size) const;
139         int dcp_length () const;
140         std::string dci_name () const;
141
142         std::string dcp_name () const;
143
144         boost::shared_ptr<FilmState> state_copy () const;
145
146         bool dirty () const {
147                 return _dirty;
148         }
149
150         int audio_channels () const;
151
152         enum Property {
153                 NONE,
154                 NAME,
155                 USE_DCI_NAME,
156                 CONTENT,
157                 DCP_CONTENT_TYPE,
158                 FORMAT,
159                 CROP,
160                 FILTERS,
161                 SCALER,
162                 DCP_FRAMES,
163                 DCP_TRIM_ACTION,
164                 DCP_AB,
165                 AUDIO_STREAM,
166                 AUDIO_GAIN,
167                 AUDIO_DELAY,
168                 STILL_DURATION,
169                 SUBTITLE_STREAM,
170                 WITH_SUBTITLES,
171                 SUBTITLE_OFFSET,
172                 SUBTITLE_SCALE,
173                 DCI_METADATA,
174                 THUMBS,
175                 SIZE,
176                 LENGTH,
177                 AUDIO_SAMPLE_RATE,
178                 HAS_SUBTITLES,
179                 AUDIO_STREAMS,
180                 SUBTITLE_STREAMS,
181                 FRAMES_PER_SECOND,
182         };
183
184
185         /* GET */
186
187         std::string directory () const {
188                 return _directory;
189         }
190
191         std::string name () const {
192                 return _name;
193         }
194
195         bool use_dci_name () const {
196                 return _use_dci_name;
197         }
198
199         std::string content () const {
200                 return _content;
201         }
202
203         DCPContentType const * dcp_content_type () const {
204                 return _dcp_content_type;
205         }
206
207         Format const * format () const {
208                 return _format;
209         }
210
211         Crop crop () const {
212                 return _crop;
213         }
214
215         std::vector<Filter const *> filters () const {
216                 return _filters;
217         }
218
219         Scaler const * scaler () const {
220                 return _scaler;
221         }
222
223         int dcp_frames () const {
224                 return _dcp_frames;
225         }
226
227         TrimAction dcp_trim_action () const {
228                 return _dcp_trim_action;
229         }
230
231         bool dcp_ab () const {
232                 return _dcp_ab;
233         }
234
235         int audio_stream_index () const {
236                 return _audio_stream;
237         }
238
239         AudioStream audio_stream () const {
240                 assert (_audio_stream < int (_audio_streams.size()));
241                 return _audio_streams[_audio_stream];
242         }
243         
244         float audio_gain () const {
245                 return _audio_gain;
246         }
247
248         int audio_delay () const {
249                 return _audio_delay;
250         }
251
252         int still_duration () const {
253                 return _still_duration;
254         }
255
256         int subtitle_stream_index () const {
257                 return _subtitle_stream;
258         }
259
260         SubtitleStream subtitle_stream () const {
261                 assert (_subtitle_stream < int (_subtitle_streams.size()));
262                 return _subtitle_streams[_subtitle_stream];
263         }
264
265         bool with_subtitles () const {
266                 return _with_subtitles;
267         }
268
269         int subtitle_offset () const {
270                 return _subtitle_offset;
271         }
272
273         float subtitle_scale () const {
274                 return _subtitle_scale;
275         }
276
277         std::string audio_language () const {
278                 return _audio_language;
279         }
280         
281         std::string subtitle_language () const {
282                 return _subtitle_language;
283         }
284         
285         std::string territory () const {
286                 return _territory;
287         }
288         
289         std::string rating () const {
290                 return _rating;
291         }
292         
293         std::string studio () const {
294                 return _studio;
295         }
296         
297         std::string facility () const {
298                 return _facility;
299         }
300         
301         std::string package_type () const {
302                 return _package_type;
303         }
304
305         std::vector<int> thumbs () const {
306                 return _thumbs;
307         }
308         
309         Size size () const {
310                 return _size;
311         }
312
313         int length () const {
314                 return _length;
315         }
316
317         int audio_sample_rate () const {
318                 return _audio_sample_rate;
319         }
320         
321         std::string content_digest () const {
322                 return _content_digest;
323         }
324         
325         bool has_subtitles () const {
326                 return _has_subtitles;
327         }
328
329         std::vector<AudioStream> audio_streams () const {
330                 return _audio_streams;
331         }
332
333         std::vector<SubtitleStream> subtitle_streams () const {
334                 return _subtitle_streams;
335         }
336         
337         float frames_per_second () const {
338                 return _frames_per_second;
339         }
340
341         
342         /* SET */
343
344         void set_directory (std::string);
345         void set_name (std::string);
346         void set_use_dci_name (bool);
347         virtual void set_content (std::string);
348         void set_dcp_content_type (DCPContentType const *);
349         void set_format (Format const *);
350         void set_crop (Crop);
351         void set_left_crop (int);
352         void set_right_crop (int);
353         void set_top_crop (int);
354         void set_bottom_crop (int);
355         void set_filters (std::vector<Filter const *>);
356         void set_scaler (Scaler const *);
357         void set_dcp_frames (int);
358         void set_dcp_trim_action (TrimAction);
359         void set_dcp_ab (bool);
360         void set_audio_stream (int);
361         void set_audio_gain (float);
362         void set_audio_delay (int);
363         void set_still_duration (int);
364         void set_subtitle_stream (int);
365         void set_with_subtitles (bool);
366         void set_subtitle_offset (int);
367         void set_subtitle_scale (float);
368         void set_audio_language (std::string);
369         void set_subtitle_language (std::string);
370         void set_territory (std::string);
371         void set_rating (std::string);
372         void set_studio (std::string);
373         void set_facility (std::string);
374         void set_package_type (std::string);
375         void set_thumbs (std::vector<int>);
376         void set_size (Size);
377         void set_length (int);
378         void set_audio_channels (int);
379         void set_audio_sample_rate (int);
380         void set_content_digest (std::string);
381         void set_has_subtitles (bool);
382         void set_audio_streams (std::vector<AudioStream>);
383         void set_subtitle_streams (std::vector<SubtitleStream>);
384         void set_frames_per_second (float);
385
386         /** Emitted when some property has changed */
387         mutable sigc::signal1<void, Property> Changed;
388         
389 private:        
390
391         std::string thumb_file_for_frame (int) const;
392         std::string thumb_base_for_frame (int) const;
393         void signal_changed (Property);
394         
395         /** Complete path to directory containing the film metadata;
396          *  must not be relative.
397          */
398         std::string _directory;
399         /** Name for DVD-o-matic */
400         std::string _name;
401         /** True if a auto-generated DCI-compliant name should be used for our DCP */
402         bool _use_dci_name;
403         /** File or directory containing content; may be relative to our directory
404          *  or an absolute path.
405          */
406         std::string _content;
407         /** The type of content that this Film represents (feature, trailer etc.) */
408         DCPContentType const * _dcp_content_type;
409         /** The format to present this Film in (flat, scope, etc.) */
410         Format const * _format;
411         /** The crop to apply to the source */
412         Crop _crop;
413         /** Video filters that should be used when generating DCPs */
414         std::vector<Filter const *> _filters;
415         /** Scaler algorithm to use */
416         Scaler const * _scaler;
417         /** Number of frames to put in the DCP, or 0 for all */
418         int _dcp_frames;
419         /** What to do with audio when trimming DCPs */
420         TrimAction _dcp_trim_action;
421         /** true to create an A/B comparison DCP, where the left half of the image
422             is the video without any filters or post-processing, and the right half
423             has the specified filters and post-processing.
424         */
425         bool _dcp_ab;
426         /** An index into our _audio_streams vector for the stream to use for audio, or -1 if there is none */
427         int _audio_stream;
428         /** Gain to apply to audio in dB */
429         float _audio_gain;
430         /** Delay to apply to audio (positive moves audio later) in milliseconds */
431         int _audio_delay;
432         /** Duration to make still-sourced films (in seconds) */
433         int _still_duration;
434         /** An index into our _subtitle_streams vector for the stream to use for subtitles, or -1 if there is none */
435         int _subtitle_stream;
436         /** True if subtitles should be shown for this film */
437         bool _with_subtitles;
438         /** y offset for placing subtitles, in source pixels; +ve is further down
439             the frame, -ve is further up.
440         */
441         int _subtitle_offset;
442         /** scale factor to apply to subtitles */
443         float _subtitle_scale;
444
445         /* DCI naming stuff */
446         std::string _audio_language;
447         std::string _subtitle_language;
448         std::string _territory;
449         std::string _rating;
450         std::string _studio;
451         std::string _facility;
452         std::string _package_type;
453
454         /* Data which are cached to speed things up */
455
456         /** Vector of frame indices for each of our `thumbnails' */
457         std::vector<int> _thumbs;
458         /** Size, in pixels, of the source (ignoring cropping) */
459         Size _size;
460         /** Length of the source in frames */
461         int _length;
462         /** Sample rate of the source audio, in Hz */
463         int _audio_sample_rate;
464         /** MD5 digest of our content file */
465         std::string _content_digest;
466         /** true if the source has subtitles */
467         bool _has_subtitles;
468         /** the audio streams that the source has */
469         std::vector<AudioStream> _audio_streams;
470         /** the subtitle streams that the source has */
471         std::vector<SubtitleStream> _subtitle_streams;
472         /** Frames per second of the source */
473         float _frames_per_second;
474
475         mutable bool _dirty;
476
477         friend class paths_test;
478 };
479
480 #endif