Merge master.
[dcpomatic.git] / src / lib / ffmpeg_content.h
1 /*
2     Copyright (C) 2013-2014 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 <boost/lexical_cast.hpp>
25 #include "video_content.h"
26 #include "audio_content.h"
27 #include "subtitle_content.h"
28 #include "audio_mapping.h"
29
30 struct AVFormatContext;
31 struct AVStream;
32
33 class Filter;
34 class ffmpeg_pts_offset_test;
35
36 class FFmpegStream
37 {
38 public:
39         FFmpegStream (std::string n, int i)
40                 : name (n)
41                 , _id (i)
42         {}
43                                 
44         FFmpegStream (cxml::ConstNodePtr);
45
46         void as_xml (xmlpp::Node *) const;
47
48         /** @param c An AVFormatContext.
49          *  @param index A stream index within the AVFormatContext.
50          *  @return true if this FFmpegStream uses the given stream index.
51          */
52         bool uses_index (AVFormatContext const * c, int index) const;
53         AVStream* stream (AVFormatContext const * c) const;
54
55         std::string technical_summary () const {
56                 return "id " + boost::lexical_cast<std::string> (_id);
57         }
58
59         std::string identifier () const {
60                 return boost::lexical_cast<std::string> (_id);
61         }
62
63         std::string name;
64
65         friend bool operator== (FFmpegStream const & a, FFmpegStream const & b);
66         friend bool operator!= (FFmpegStream const & a, FFmpegStream const & b);
67         
68 private:
69         int _id;
70 };
71
72 class FFmpegAudioStream : public FFmpegStream
73 {
74 public:
75         FFmpegAudioStream (std::string n, int i, int f, int c)
76                 : FFmpegStream (n, i)
77                 , frame_rate (f)
78                 , channels (c)
79                 , mapping (c)
80         {
81                 mapping.make_default ();
82         }
83
84         FFmpegAudioStream (cxml::ConstNodePtr, int);
85
86         void as_xml (xmlpp::Node *) const;
87
88         int frame_rate;
89         int channels;
90         AudioMapping mapping;
91         boost::optional<ContentTime> first_audio;
92
93 private:
94         friend class ffmpeg_pts_offset_test;
95
96         /* Constructor for tests */
97         FFmpegAudioStream ()
98                 : FFmpegStream ("", 0)
99                 , frame_rate (0)
100                 , channels (0)
101                 , mapping (1)
102         {}
103 };
104
105 class FFmpegSubtitleStream : public FFmpegStream
106 {
107 public:
108         FFmpegSubtitleStream (std::string n, int i)
109                 : FFmpegStream (n, i)
110         {}
111         
112         FFmpegSubtitleStream (cxml::ConstNodePtr);
113
114         void as_xml (xmlpp::Node *) const;
115 };
116
117 class FFmpegContentProperty : public VideoContentProperty
118 {
119 public:
120         static int const SUBTITLE_STREAMS;
121         static int const SUBTITLE_STREAM;
122         static int const AUDIO_STREAMS;
123         static int const AUDIO_STREAM;
124         static int const FILTERS;
125 };
126
127 class FFmpegContent : public VideoContent, public AudioContent, public SubtitleContent
128 {
129 public:
130         FFmpegContent (boost::shared_ptr<const Film>, boost::filesystem::path);
131         FFmpegContent (boost::shared_ptr<const Film>, cxml::ConstNodePtr, int version, std::list<std::string> &);
132         FFmpegContent (boost::shared_ptr<const Film>, std::vector<boost::shared_ptr<Content> >);
133
134         boost::shared_ptr<FFmpegContent> shared_from_this () {
135                 return boost::dynamic_pointer_cast<FFmpegContent> (Content::shared_from_this ());
136         }
137         
138         void examine (boost::shared_ptr<Job>);
139         std::string summary () const;
140         std::string technical_summary () const;
141         std::string information () const;
142         void as_xml (xmlpp::Node *) const;
143         DCPTime full_length () const;
144
145         std::string identifier () const;
146         
147         /* AudioContent */
148         int audio_channels () const;
149         ContentTime audio_length () const;
150         int 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