Re-work FilmState / Film relationship a bit; Film now inherits from FilmState and...
[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_channels (0)
74                 , _audio_sample_rate (0)
75                 , _audio_sample_format (AV_SAMPLE_FMT_NONE)
76                 , _has_subtitles (false)
77                 , _frames_per_second (0)
78                 , _dirty (false)
79         {}
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 bytes_per_sample () const;
94         int target_sample_rate () const;
95         
96         void write_metadata () const;
97         void read_metadata ();
98
99         Size cropped_size (Size) const;
100         int dcp_length () const;
101         std::string dci_name () const;
102
103         std::string dcp_name () const;
104
105         boost::shared_ptr<FilmState> state_copy () const;
106
107         bool dirty () const {
108                 return _dirty;
109         }
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_CHANNELS,
137                 AUDIO_SAMPLE_RATE,
138                 HAS_SUBTITLES,
139                 AUDIO_STREAMS,
140                 SUBTITLE_STREAMS,
141                 FRAMES_PER_SECOND,
142         };
143
144
145         /* GET */
146
147         std::string directory () const {
148                 return _directory;
149         }
150
151         std::string name () const {
152                 return _name;
153         }
154
155         bool use_dci_name () const {
156                 return _use_dci_name;
157         }
158
159         std::string content () const {
160                 return _content;
161         }
162
163         DCPContentType const * dcp_content_type () const {
164                 return _dcp_content_type;
165         }
166
167         Format const * format () const {
168                 return _format;
169         }
170
171         Crop crop () const {
172                 return _crop;
173         }
174
175         std::vector<Filter const *> filters () const {
176                 return _filters;
177         }
178
179         Scaler const * scaler () const {
180                 return _scaler;
181         }
182
183         int dcp_frames () const {
184                 return _dcp_frames;
185         }
186
187         TrimAction dcp_trim_action () const {
188                 return _dcp_trim_action;
189         }
190
191         bool dcp_ab () const {
192                 return _dcp_ab;
193         }
194
195         int audio_stream () const {
196                 return _audio_stream;
197         }
198
199         float audio_gain () const {
200                 return _audio_gain;
201         }
202
203         int audio_delay () const {
204                 return _audio_delay;
205         }
206
207         int still_duration () const {
208                 return _still_duration;
209         }
210
211         int subtitle_stream () const {
212                 return _subtitle_stream;
213         }
214
215         bool with_subtitles () const {
216                 return _with_subtitles;
217         }
218
219         int subtitle_offset () const {
220                 return _subtitle_offset;
221         }
222
223         float subtitle_scale () const {
224                 return _subtitle_scale;
225         }
226
227         std::string audio_language () const {
228                 return _audio_language;
229         }
230         
231         std::string subtitle_language () const {
232                 return _subtitle_language;
233         }
234         
235         std::string territory () const {
236                 return _territory;
237         }
238         
239         std::string rating () const {
240                 return _rating;
241         }
242         
243         std::string studio () const {
244                 return _studio;
245         }
246         
247         std::string facility () const {
248                 return _facility;
249         }
250         
251         std::string package_type () const {
252                 return _package_type;
253         }
254
255         std::vector<int> thumbs () const {
256                 return _thumbs;
257         }
258         
259         Size size () const {
260                 return _size;
261         }
262
263         int length () const {
264                 return _length;
265         }
266
267         int audio_channels () const {
268                 return _audio_channels;
269         }
270         
271         int audio_sample_rate () const {
272                 return _audio_sample_rate;
273         }
274         
275         AVSampleFormat audio_sample_format () const {
276                 return _audio_sample_format;
277         }
278         
279         std::string content_digest () const {
280                 return _content_digest;
281         }
282         
283         bool has_subtitles () const {
284                 return _has_subtitles;
285         }
286
287         std::vector<Stream> audio_streams () const {
288                 return _audio_streams;
289         }
290
291         std::vector<Stream> subtitle_streams () const {
292                 return _subtitle_streams;
293         }
294         
295         float frames_per_second () const {
296                 return _frames_per_second;
297         }
298
299         
300         /* SET */
301
302         void set_directory (std::string);
303         void set_name (std::string);
304         void set_use_dci_name (bool);
305         void set_content (std::string);
306         void set_dcp_content_type (DCPContentType const *);
307         void set_format (Format const *);
308         void set_crop (Crop);
309         void set_left_crop (int);
310         void set_right_crop (int);
311         void set_top_crop (int);
312         void set_bottom_crop (int);
313         void set_filters (std::vector<Filter const *>);
314         void set_scaler (Scaler const *);
315         void set_dcp_frames (int);
316         void set_dcp_trim_action (TrimAction);
317         void set_dcp_ab (bool);
318         void set_audio_stream (int);
319         void set_audio_gain (float);
320         void set_audio_delay (int);
321         void set_still_duration (int);
322         void set_subtitle_stream (int);
323         void set_with_subtitles (bool);
324         void set_subtitle_offset (int);
325         void set_subtitle_scale (float);
326         void set_audio_language (std::string);
327         void set_subtitle_language (std::string);
328         void set_territory (std::string);
329         void set_rating (std::string);
330         void set_studio (std::string);
331         void set_facility (std::string);
332         void set_package_type (std::string);
333         void set_thumbs (std::vector<int>);
334         void set_size (Size);
335         void set_length (int);
336         void set_audio_channels (int);
337         void set_audio_sample_rate (int);
338         void set_audio_sample_format (AVSampleFormat);
339         void set_content_digest (std::string);
340         void set_has_subtitles (bool);
341         void set_audio_streams (std::vector<Stream>);
342         void set_subtitle_streams (std::vector<Stream>);
343         void set_frames_per_second (float);
344
345         /** Emitted when some property has changed */
346         mutable sigc::signal1<void, Property> Changed;
347         
348 private:        
349
350         std::string thumb_file_for_frame (int) const;
351         std::string thumb_base_for_frame (int) const;
352         void signal_changed (Property);
353         
354         /** Complete path to directory containing the film metadata;
355          *  must not be relative.
356          */
357         std::string _directory;
358         /** Name for DVD-o-matic */
359         std::string _name;
360         /** True if a auto-generated DCI-compliant name should be used for our DCP */
361         bool _use_dci_name;
362         /** File or directory containing content; may be relative to our directory
363          *  or an absolute path.
364          */
365         std::string _content;
366         /** The type of content that this Film represents (feature, trailer etc.) */
367         DCPContentType const * _dcp_content_type;
368         /** The format to present this Film in (flat, scope, etc.) */
369         Format const * _format;
370         /** The crop to apply to the source */
371         Crop _crop;
372         /** Video filters that should be used when generating DCPs */
373         std::vector<Filter const *> _filters;
374         /** Scaler algorithm to use */
375         Scaler const * _scaler;
376         /** Number of frames to put in the DCP, or 0 for all */
377         int _dcp_frames;
378         /** What to do with audio when trimming DCPs */
379         TrimAction _dcp_trim_action;
380         /** true to create an A/B comparison DCP, where the left half of the image
381             is the video without any filters or post-processing, and the right half
382             has the specified filters and post-processing.
383         */
384         bool _dcp_ab;
385         /** The decoder's stream ID to use for audio, or -1 if there is none */
386         int _audio_stream;
387         /** Gain to apply to audio in dB */
388         float _audio_gain;
389         /** Delay to apply to audio (positive moves audio later) in milliseconds */
390         int _audio_delay;
391         /** Duration to make still-sourced films (in seconds) */
392         int _still_duration;
393         /** The decoder's stream ID to use for subtitles, or -1 if there are none */
394         int _subtitle_stream;
395         /** True if subtitles should be shown for this film */
396         bool _with_subtitles;
397         /** y offset for placing subtitles, in source pixels; +ve is further down
398             the frame, -ve is further up.
399         */
400         int _subtitle_offset;
401         /** scale factor to apply to subtitles */
402         float _subtitle_scale;
403
404         /* DCI naming stuff */
405         std::string _audio_language;
406         std::string _subtitle_language;
407         std::string _territory;
408         std::string _rating;
409         std::string _studio;
410         std::string _facility;
411         std::string _package_type;
412
413         /* Data which are cached to speed things up */
414
415         /** Vector of frame indices for each of our `thumbnails' */
416         std::vector<int> _thumbs;
417         /** Size, in pixels, of the source (ignoring cropping) */
418         Size _size;
419         /** Length of the source in frames */
420         int _length;
421         /** Number of audio channels */
422         int _audio_channels;
423         /** Sample rate of the source audio, in Hz */
424         int _audio_sample_rate;
425         /** Format of the audio samples */
426         AVSampleFormat _audio_sample_format;
427         /** MD5 digest of our content file */
428         std::string _content_digest;
429         /** true if the source has subtitles */
430         bool _has_subtitles;
431         /** the audio streams that the source has */
432         std::vector<Stream> _audio_streams;
433         /** the subtitle streams that the source has */
434         std::vector<Stream> _subtitle_streams;
435         /** Frames per second of the source */
436         float _frames_per_second;
437
438         mutable bool _dirty;
439
440         friend class paths_test;
441 };
442
443 #endif