Partial fix to sync according to pts.
[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         virtual ~FilmState () {}
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 target_sample_rate () const;
94         
95         void write_metadata () const;
96         void read_metadata ();
97
98         Size cropped_size (Size) const;
99         int dcp_length () const;
100         std::string dci_name () const;
101
102         std::string dcp_name () const;
103
104         boost::shared_ptr<FilmState> state_copy () const;
105
106         bool dirty () const {
107                 return _dirty;
108         }
109
110         int audio_channels () const;
111
112         enum Property {
113                 NONE,
114                 NAME,
115                 USE_DCI_NAME,
116                 CONTENT,
117                 DCP_CONTENT_TYPE,
118                 FORMAT,
119                 CROP,
120                 FILTERS,
121                 SCALER,
122                 DCP_FRAMES,
123                 DCP_TRIM_ACTION,
124                 DCP_AB,
125                 AUDIO_STREAM,
126                 AUDIO_GAIN,
127                 AUDIO_DELAY,
128                 STILL_DURATION,
129                 SUBTITLE_STREAM,
130                 WITH_SUBTITLES,
131                 SUBTITLE_OFFSET,
132                 SUBTITLE_SCALE,
133                 DCI_METADATA,
134                 THUMBS,
135                 SIZE,
136                 LENGTH,
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_index () const {
196                 return _audio_stream;
197         }
198
199         AudioStream audio_stream () const {
200                 assert (_audio_stream < int (_audio_streams.size()));
201                 return _audio_streams[_audio_stream];
202         }
203         
204         float audio_gain () const {
205                 return _audio_gain;
206         }
207
208         int audio_delay () const {
209                 return _audio_delay;
210         }
211
212         int still_duration () const {
213                 return _still_duration;
214         }
215
216         int subtitle_stream_index () const {
217                 return _subtitle_stream;
218         }
219
220         SubtitleStream subtitle_stream () const {
221                 assert (_subtitle_stream < int (_subtitle_streams.size()));
222                 return _subtitle_streams[_subtitle_stream];
223         }
224
225         bool with_subtitles () const {
226                 return _with_subtitles;
227         }
228
229         int subtitle_offset () const {
230                 return _subtitle_offset;
231         }
232
233         float subtitle_scale () const {
234                 return _subtitle_scale;
235         }
236
237         std::string audio_language () const {
238                 return _audio_language;
239         }
240         
241         std::string subtitle_language () const {
242                 return _subtitle_language;
243         }
244         
245         std::string territory () const {
246                 return _territory;
247         }
248         
249         std::string rating () const {
250                 return _rating;
251         }
252         
253         std::string studio () const {
254                 return _studio;
255         }
256         
257         std::string facility () const {
258                 return _facility;
259         }
260         
261         std::string package_type () const {
262                 return _package_type;
263         }
264
265         std::vector<int> thumbs () const {
266                 return _thumbs;
267         }
268         
269         Size size () const {
270                 return _size;
271         }
272
273         int length () const {
274                 return _length;
275         }
276
277         int audio_sample_rate () const {
278                 return _audio_sample_rate;
279         }
280         
281         std::string content_digest () const {
282                 return _content_digest;
283         }
284         
285         bool has_subtitles () const {
286                 return _has_subtitles;
287         }
288
289         std::vector<AudioStream> audio_streams () const {
290                 return _audio_streams;
291         }
292
293         std::vector<SubtitleStream> subtitle_streams () const {
294                 return _subtitle_streams;
295         }
296         
297         float frames_per_second () const {
298                 return _frames_per_second;
299         }
300
301         
302         /* SET */
303
304         void set_directory (std::string);
305         void set_name (std::string);
306         void set_use_dci_name (bool);
307         virtual void set_content (std::string);
308         void set_dcp_content_type (DCPContentType const *);
309         void set_format (Format const *);
310         void set_crop (Crop);
311         void set_left_crop (int);
312         void set_right_crop (int);
313         void set_top_crop (int);
314         void set_bottom_crop (int);
315         void set_filters (std::vector<Filter const *>);
316         void set_scaler (Scaler const *);
317         void set_dcp_frames (int);
318         void set_dcp_trim_action (TrimAction);
319         void set_dcp_ab (bool);
320         void set_audio_stream (int);
321         void set_audio_gain (float);
322         void set_audio_delay (int);
323         void set_still_duration (int);
324         void set_subtitle_stream (int);
325         void set_with_subtitles (bool);
326         void set_subtitle_offset (int);
327         void set_subtitle_scale (float);
328         void set_audio_language (std::string);
329         void set_subtitle_language (std::string);
330         void set_territory (std::string);
331         void set_rating (std::string);
332         void set_studio (std::string);
333         void set_facility (std::string);
334         void set_package_type (std::string);
335         void set_thumbs (std::vector<int>);
336         void set_size (Size);
337         void set_length (int);
338         void set_audio_channels (int);
339         void set_audio_sample_rate (int);
340         void set_content_digest (std::string);
341         void set_has_subtitles (bool);
342         void set_audio_streams (std::vector<AudioStream>);
343         void set_subtitle_streams (std::vector<SubtitleStream>);
344         void set_frames_per_second (float);
345
346         /** Emitted when some property has changed */
347         mutable sigc::signal1<void, Property> Changed;
348         
349 private:        
350
351         std::string thumb_file_for_frame (int) const;
352         std::string thumb_base_for_frame (int) const;
353         void signal_changed (Property);
354         
355         /** Complete path to directory containing the film metadata;
356          *  must not be relative.
357          */
358         std::string _directory;
359         /** Name for DVD-o-matic */
360         std::string _name;
361         /** True if a auto-generated DCI-compliant name should be used for our DCP */
362         bool _use_dci_name;
363         /** File or directory containing content; may be relative to our directory
364          *  or an absolute path.
365          */
366         std::string _content;
367         /** The type of content that this Film represents (feature, trailer etc.) */
368         DCPContentType const * _dcp_content_type;
369         /** The format to present this Film in (flat, scope, etc.) */
370         Format const * _format;
371         /** The crop to apply to the source */
372         Crop _crop;
373         /** Video filters that should be used when generating DCPs */
374         std::vector<Filter const *> _filters;
375         /** Scaler algorithm to use */
376         Scaler const * _scaler;
377         /** Number of frames to put in the DCP, or 0 for all */
378         int _dcp_frames;
379         /** What to do with audio when trimming DCPs */
380         TrimAction _dcp_trim_action;
381         /** true to create an A/B comparison DCP, where the left half of the image
382             is the video without any filters or post-processing, and the right half
383             has the specified filters and post-processing.
384         */
385         bool _dcp_ab;
386         /** An index into our _audio_streams vector for the stream to use for audio, or -1 if there is none */
387         int _audio_stream;
388         /** Gain to apply to audio in dB */
389         float _audio_gain;
390         /** Delay to apply to audio (positive moves audio later) in milliseconds */
391         int _audio_delay;
392         /** Duration to make still-sourced films (in seconds) */
393         int _still_duration;
394         /** An index into our _subtitle_streams vector for the stream to use for subtitles, or -1 if there is none */
395         int _subtitle_stream;
396         /** True if subtitles should be shown for this film */
397         bool _with_subtitles;
398         /** y offset for placing subtitles, in source pixels; +ve is further down
399             the frame, -ve is further up.
400         */
401         int _subtitle_offset;
402         /** scale factor to apply to subtitles */
403         float _subtitle_scale;
404
405         /* DCI naming stuff */
406         std::string _audio_language;
407         std::string _subtitle_language;
408         std::string _territory;
409         std::string _rating;
410         std::string _studio;
411         std::string _facility;
412         std::string _package_type;
413
414         /* Data which are cached to speed things up */
415
416         /** Vector of frame indices for each of our `thumbnails' */
417         std::vector<int> _thumbs;
418         /** Size, in pixels, of the source (ignoring cropping) */
419         Size _size;
420         /** Length of the source in frames */
421         int _length;
422         /** Sample rate of the source audio, in Hz */
423         int _audio_sample_rate;
424         /** MD5 digest of our content file */
425         std::string _content_digest;
426         /** true if the source has subtitles */
427         bool _has_subtitles;
428         /** the audio streams that the source has */
429         std::vector<AudioStream> _audio_streams;
430         /** the subtitle streams that the source has */
431         std::vector<SubtitleStream> _subtitle_streams;
432         /** Frames per second of the source */
433         float _frames_per_second;
434
435         mutable bool _dirty;
436
437         friend class paths_test;
438 };
439
440 #endif