Merge.
[dcpomatic.git] / src / lib / ffmpeg_content.h
1 /*
2     Copyright (C) 2013 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 #ifndef DCPOMATIC_FFMPEG_CONTENT_H
21 #define DCPOMATIC_FFMPEG_CONTENT_H
22
23 #include <boost/enable_shared_from_this.hpp>
24 #include "video_content.h"
25 #include "audio_content.h"
26 #include "subtitle_content.h"
27 #include "audio_mapping.h"
28
29 struct AVFormatContext;
30 struct AVStream;
31
32 class Filter;
33 class ffmpeg_pts_offset_test;
34
35 class FFmpegStream
36 {
37 public:
38         FFmpegStream (std::string n, int i)
39                 : name (n)
40                 , _id (i)
41         {}
42                                 
43         FFmpegStream (boost::shared_ptr<const cxml::Node>);
44
45         void as_xml (xmlpp::Node *) const;
46
47         /** @param c An AVFormatContext.
48          *  @param index A stream index within the AVFormatContext.
49          *  @return true if this FFmpegStream uses the given stream index.
50          */
51         bool uses_index (AVFormatContext const * c, int index) const;
52         AVStream* stream (AVFormatContext const * c) const;
53
54         std::string technical_summary () const {
55                 return "id " + boost::lexical_cast<std::string> (_id);
56         }
57
58         std::string identifier () const {
59                 return boost::lexical_cast<std::string> (_id);
60         }
61
62         std::string name;
63
64         friend bool operator== (FFmpegStream const & a, FFmpegStream const & b);
65         friend bool operator!= (FFmpegStream const & a, FFmpegStream const & b);
66         
67 private:
68         int _id;
69 };
70
71 class FFmpegAudioStream : public FFmpegStream
72 {
73 public:
74         FFmpegAudioStream (std::string n, int i, int f, int c)
75                 : FFmpegStream (n, i)
76                 , frame_rate (f)
77                 , channels (c)
78                 , mapping (c)
79         {
80                 mapping.make_default ();
81         }
82
83         FFmpegAudioStream (boost::shared_ptr<const cxml::Node>, int);
84
85         void as_xml (xmlpp::Node *) const;
86
87         int frame_rate;
88         int channels;
89         AudioMapping mapping;
90         boost::optional<ContentTime> first_audio;
91
92 private:
93         friend class ffmpeg_pts_offset_test;
94
95         /* Constructor for tests */
96         FFmpegAudioStream ()
97                 : FFmpegStream ("", 0)
98                 , frame_rate (0)
99                 , channels (0)
100                 , mapping (1)
101         {}
102 };
103
104 class FFmpegSubtitleStream : public FFmpegStream
105 {
106 public:
107         FFmpegSubtitleStream (std::string n, int i)
108                 : FFmpegStream (n, i)
109         {}
110         
111         FFmpegSubtitleStream (boost::shared_ptr<const cxml::Node>);
112
113         void as_xml (xmlpp::Node *) const;
114 };
115
116 class FFmpegContentProperty : public VideoContentProperty
117 {
118 public:
119         static int const SUBTITLE_STREAMS;
120         static int const SUBTITLE_STREAM;
121         static int const AUDIO_STREAMS;
122         static int const AUDIO_STREAM;
123         static int const FILTERS;
124 };
125
126 class FFmpegContent : public VideoContent, public AudioContent, public SubtitleContent
127 {
128 public:
129         FFmpegContent (boost::shared_ptr<const Film>, boost::filesystem::path);
130         FFmpegContent (boost::shared_ptr<const Film>, boost::shared_ptr<const cxml::Node>, int version, std::list<std::string> &);
131         FFmpegContent (boost::shared_ptr<const Film>, std::vector<boost::shared_ptr<Content> >);
132
133         boost::shared_ptr<FFmpegContent> shared_from_this () {
134                 return boost::dynamic_pointer_cast<FFmpegContent> (Content::shared_from_this ());
135         }
136         
137         void examine (boost::shared_ptr<Job>);
138         std::string summary () const;
139         std::string technical_summary () const;
140         std::string information () const;
141         void as_xml (xmlpp::Node *) const;
142         DCPTime full_length () const;
143
144         std::string identifier () const;
145         
146         /* AudioContent */
147         int audio_channels () const;
148         ContentTime audio_length () const;
149         int content_audio_frame_rate () const;
150         int output_audio_frame_rate () const;
151         AudioMapping audio_mapping () const;
152         void set_audio_mapping (AudioMapping);
153         boost::filesystem::path audio_analysis_path () const;
154
155         void set_filters (std::vector<Filter const *> const &);
156         
157         std::vector<boost::shared_ptr<FFmpegSubtitleStream> > subtitle_streams () const {
158                 boost::mutex::scoped_lock lm (_mutex);
159                 return _subtitle_streams;
160         }
161
162         boost::shared_ptr<FFmpegSubtitleStream> subtitle_stream () const {
163                 boost::mutex::scoped_lock lm (_mutex);
164                 return _subtitle_stream;
165         }
166
167         std::vector<boost::shared_ptr<FFmpegAudioStream> > audio_streams () const {
168                 boost::mutex::scoped_lock lm (_mutex);
169                 return _audio_streams;
170         }
171         
172         boost::shared_ptr<FFmpegAudioStream> audio_stream () const {
173                 boost::mutex::scoped_lock lm (_mutex);
174                 return _audio_stream;
175         }
176
177         std::vector<Filter const *> filters () const {
178                 boost::mutex::scoped_lock lm (_mutex);
179                 return _filters;
180         }
181
182         void set_subtitle_stream (boost::shared_ptr<FFmpegSubtitleStream>);
183         void set_audio_stream (boost::shared_ptr<FFmpegAudioStream>);
184
185         boost::optional<ContentTime> first_video () const {
186                 boost::mutex::scoped_lock lm (_mutex);
187                 return _first_video;
188         }
189
190 private:
191         friend class ffmpeg_pts_offset_test;
192         
193         std::vector<boost::shared_ptr<FFmpegSubtitleStream> > _subtitle_streams;
194         boost::shared_ptr<FFmpegSubtitleStream> _subtitle_stream;
195         std::vector<boost::shared_ptr<FFmpegAudioStream> > _audio_streams;
196         boost::shared_ptr<FFmpegAudioStream> _audio_stream;
197         boost::optional<ContentTime> _first_video;
198         /** Video filters that should be used when generating DCPs */
199         std::vector<Filter const *> _filters;
200 };
201
202 #endif