Basic support for selection of audio / subtitle streams.
[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 <sigc++/signal.h>
34 extern "C" {
35 #include <libavcodec/avcodec.h>
36 }
37 #include "dcp_content_type.h"
38 #include "film_state.h"
39
40 class Format;
41 class Job;
42 class Filter;
43 class Log;
44 class ExamineContentJob;
45
46 /** @class Film
47  *  @brief A representation of a video with sound.
48  *
49  *  A representation of a piece of video (with sound), including naming,
50  *  the source content file, and how it should be presented in a DCP.
51  */
52 class Film
53 {
54 public:
55         Film (std::string d, bool must_exist = true);
56         Film (Film const &);
57         ~Film ();
58
59         void write_metadata () const;
60
61         /** @return complete path to directory containing the film metadata */
62         std::string directory () const {
63                 return _state.directory;
64         }
65
66         std::string content () const;
67         ContentType content_type () const;
68
69         /** @return name for DVD-o-matic */
70         std::string name () const {
71                 return _state.name;
72         }
73
74         /** @return name to give the DCP */
75         std::string dcp_name () const {
76                 return _state.dcp_name ();
77         }
78
79         /** @return true to use a DCI-spec name for the DCP */
80         bool use_dci_name () const {
81                 return _state.use_dci_name;
82         }
83
84         /** @return number of pixels to crop from the sides of the original picture */
85         Crop crop () const {
86                 return _state.crop;
87         }
88
89         /** @return the format to present this film in (flat, scope, etc.) */
90         Format const * format () const {
91                 return _state.format;
92         }
93
94         /** @return video filters that should be used when generating DCPs */
95         std::vector<Filter const *> filters () const {
96                 return _state.filters;
97         }
98
99         /** @return scaler algorithm to use */
100         Scaler const * scaler () const {
101                 return _state.scaler;
102         }
103
104         /** @return number of frames to put in the DCP, or 0 for all */
105         int dcp_frames () const {
106                 return _state.dcp_frames;
107         }
108
109         /** @return what to do with the end of an encode when trimming */
110         TrimAction dcp_trim_action () const {
111                 return _state.dcp_trim_action;
112         }
113
114         /** @return true to create an A/B comparison DCP, where the left half of the image
115          *  is the video without any filters or post-processing, and the right half
116          *  has the specified filters and post-processing.
117          */
118         bool dcp_ab () const {
119                 return _state.dcp_ab;
120         }
121
122         /** @return gain that should be applied to the audio when making a DCP
123             (in dB).
124         */
125         float audio_gain () const {
126                 return _state.audio_gain;
127         }
128
129         /** @return delay to apply to audio (positive moves audio later) in milliseconds */
130         int audio_delay () const {
131                 return _state.audio_delay;
132         }
133
134         /** @return duration to make still-sourced films (in seconds) */
135         int still_duration () const {
136                 return _state.still_duration;
137         }
138
139         /** @return true to encode DCP with subtitles, if they are available */
140         bool with_subtitles () const {
141                 return _state.with_subtitles;
142         }
143
144         /** @return offset to move subtitles by, in source pixels; +ve moves
145             them down the image, -ve moves them up.
146         */
147         int subtitle_offset () const {
148                 return _state.subtitle_offset;
149         }
150
151         /** @return scaling factor to apply to subtitle images */
152         float subtitle_scale () const {
153                 return _state.subtitle_scale;
154         }
155         
156         void set_filters (std::vector<Filter const *> const &);
157
158         void set_scaler (Scaler const *);
159
160         /** @return the type of content that this Film represents (feature, trailer etc.) */
161         DCPContentType const * dcp_content_type () {
162                 return _state.dcp_content_type;
163         }
164
165         std::vector<Stream> audio_streams () const {
166                 return _state.audio_streams;
167         }
168
169         int audio_stream () const {
170                 return _state.audio_stream;
171         }
172
173         std::vector<Stream> subtitle_streams () const {
174                 return _state.subtitle_streams;
175         }
176
177         int subtitle_stream () const {
178                 return _state.subtitle_stream;
179         }
180         
181         void set_dcp_frames (int);
182         void set_dcp_trim_action (TrimAction);
183         void set_dcp_ab (bool);
184         
185         void set_name (std::string);
186         void set_use_dci_name (bool);
187         void set_content (std::string);
188         void set_top_crop (int);
189         void set_bottom_crop (int);
190         void set_left_crop (int);
191         void set_right_crop (int);
192         void set_format (Format const *);
193         void set_dcp_content_type (DCPContentType const *);
194         void set_audio_gain (float);
195         void set_audio_delay (int);
196         void set_still_duration (int);
197         void set_with_subtitles (bool);
198         void set_subtitle_offset (int);
199         void set_subtitle_scale (float);
200         void set_audio_language (std::string);
201         void set_subtitle_language (std::string);
202         void set_territory (std::string);
203         void set_rating (std::string);
204         void set_studio (std::string);
205         void set_facility (std::string);
206         void set_package_type (std::string);
207
208         /** @return size, in pixels, of the source (ignoring cropping) */
209         Size size () const {
210                 return _state.size;
211         }
212
213         /** @return length, in video frames */
214         int length () const {
215                 return _state.length;
216         }
217
218         /** @return nnumber of video frames per second */
219         float frames_per_second () const {
220                 return _state.frames_per_second;
221         }
222
223         /** @return number of audio channels */
224         int audio_channels () const {
225                 return _state.audio_channels;
226         }
227
228         /** @return audio sample rate, in Hz */
229         int audio_sample_rate () const {
230                 return _state.audio_sample_rate;
231         }
232
233         /** @return format of the audio samples */
234         AVSampleFormat audio_sample_format () const {
235                 return _state.audio_sample_format;
236         }
237
238         bool has_subtitles () const {
239                 return _state.has_subtitles;
240         }
241
242         std::string audio_language () const {
243                 return _state.audio_language;
244         }
245
246         std::string subtitle_language () const {
247                 return _state.subtitle_language;
248         }
249         
250         std::string territory () const {
251                 return _state.territory;
252         }
253
254         std::string rating () const {
255                 return _state.rating;
256         }
257
258         std::string studio () const {
259                 return _state.studio;
260         }
261
262         std::string facility () const {
263                 return _state.facility;
264         }
265
266         std::string package_type () const {
267                 return _state.package_type;
268         }
269
270         std::string j2k_dir () const;
271
272         std::vector<std::string> audio_files () const;
273
274         void update_thumbs_pre_gui ();
275         void update_thumbs_post_gui ();
276         int num_thumbs () const;
277         int thumb_frame (int) const;
278         std::string thumb_file (int) const;
279         std::pair<Position, std::string> thumb_subtitle (int) const;
280
281         void copy_from_dvd_post_gui ();
282         void examine_content ();
283         void examine_content_post_gui ();
284         void send_dcp_to_tms ();
285         void copy_from_dvd ();
286
287         /** @return true if our metadata has been modified since it was last saved */
288         bool dirty () const {
289                 return _dirty;
290         }
291
292         void make_dcp (bool, int freq = 0);
293
294         enum Property {
295                 NONE,
296                 NAME,
297                 CONTENT,
298                 DCP_CONTENT_TYPE,
299                 FORMAT,
300                 CROP,
301                 FILTERS,
302                 SCALER,
303                 DCP_FRAMES,
304                 DCP_TRIM_ACTION,
305                 DCP_AB,
306                 AUDIO_STREAM,
307                 AUDIO_GAIN,
308                 AUDIO_DELAY,
309                 THUMBS,
310                 SIZE,
311                 LENGTH,
312                 FRAMES_PER_SECOND,
313                 AUDIO_CHANNELS,
314                 AUDIO_SAMPLE_RATE,
315                 STILL_DURATION,
316                 SUBTITLE_STREAM,
317                 WITH_SUBTITLES,
318                 SUBTITLE_OFFSET,
319                 SUBTITLE_SCALE,
320                 USE_DCI_NAME,
321                 DCI_METADATA
322         };
323
324         boost::shared_ptr<FilmState> state_copy () const;
325
326         /** @return Logger.
327          *  It is safe to call this from any thread.
328          */
329         Log* log () const {
330                 return _log;
331         }
332
333         int encoded_frames () const;
334
335         /** Emitted when some metadata property has changed */
336         mutable sigc::signal1<void, Property> Changed;
337         
338 private:
339         void read_metadata ();
340         std::string metadata_file () const;
341         void update_dimensions ();
342         void signal_changed (Property);
343
344         /** The majority of our state.  Kept in a separate object
345          *  so that it can easily be copied for passing onto long-running
346          *  jobs (which then have an unchanging set of parameters).
347          */
348         FilmState _state;
349
350         /** true if our metadata has changed since it was last written to disk */
351         mutable bool _dirty;
352
353         /** Log to write to */
354         Log* _log;
355
356         /** Any running ExamineContentJob, or 0 */
357         boost::shared_ptr<ExamineContentJob> _examine_content_job;
358 };
359
360 #endif