Another small clean-up.
[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                 , _legacy_id (false)
42         {}
43                                 
44         FFmpegStream (boost::shared_ptr<const cxml::Node>, int);
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 name;
56         int id;
57         
58 private:
59         /** If this is true, id is in fact the index */
60         bool _legacy_id;
61 };
62
63 class FFmpegAudioStream : public FFmpegStream
64 {
65 public:
66         FFmpegAudioStream (std::string n, int i, int f, int c)
67                 : FFmpegStream (n, i)
68                 , frame_rate (f)
69                 , channels (c)
70                 , mapping (c)
71         {
72                 mapping.make_default ();
73         }
74
75         FFmpegAudioStream (boost::shared_ptr<const cxml::Node>, int);
76
77         void as_xml (xmlpp::Node *) const;
78
79         int frame_rate;
80         int channels;
81         AudioMapping mapping;
82         boost::optional<double> first_audio;
83
84 private:
85         friend class ffmpeg_pts_offset_test;
86
87         /* Constructor for tests */
88         FFmpegAudioStream ()
89                 : FFmpegStream ("", 0)
90                 , frame_rate (0)
91                 , channels (0)
92                 , mapping (1)
93         {}
94 };
95
96 extern bool operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b);
97 extern bool operator!= (FFmpegAudioStream const & a, FFmpegAudioStream const & b);
98
99 class FFmpegSubtitleStream : public FFmpegStream
100 {
101 public:
102         FFmpegSubtitleStream (std::string n, int i)
103                 : FFmpegStream (n, i)
104         {}
105         
106         FFmpegSubtitleStream (boost::shared_ptr<const cxml::Node>, int);
107
108         void as_xml (xmlpp::Node *) const;
109 };
110
111 extern bool operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b);
112 extern bool operator!= (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b);
113
114 class FFmpegContentProperty : public VideoContentProperty
115 {
116 public:
117         static int const SUBTITLE_STREAMS;
118         static int const SUBTITLE_STREAM;
119         static int const AUDIO_STREAMS;
120         static int const AUDIO_STREAM;
121         static int const FILTERS;
122 };
123
124 class FFmpegContent : public VideoContent, public AudioContent, public SubtitleContent
125 {
126 public:
127         FFmpegContent (boost::shared_ptr<const Film>, boost::filesystem::path);
128         FFmpegContent (boost::shared_ptr<const Film>, boost::shared_ptr<const cxml::Node>, int version);
129         FFmpegContent (boost::shared_ptr<const Film>, std::vector<boost::shared_ptr<Content> >);
130
131         boost::shared_ptr<FFmpegContent> shared_from_this () {
132                 return boost::dynamic_pointer_cast<FFmpegContent> (Content::shared_from_this ());
133         }
134         
135         void examine (boost::shared_ptr<Job>);
136         std::string summary () const;
137         std::string technical_summary () const;
138         std::string information () const;
139         void as_xml (xmlpp::Node *) const;
140         Time full_length () const;
141
142         std::string identifier () const;
143         
144         /* AudioContent */
145         int audio_channels () const;
146         AudioContent::Frame audio_length () const;
147         int content_audio_frame_rate () const;
148         int output_audio_frame_rate () const;
149         AudioMapping audio_mapping () const;
150         void set_audio_mapping (AudioMapping);
151
152         void set_filters (std::vector<Filter const *> const &);
153         
154         std::vector<boost::shared_ptr<FFmpegSubtitleStream> > subtitle_streams () const {
155                 boost::mutex::scoped_lock lm (_mutex);
156                 return _subtitle_streams;
157         }
158
159         boost::shared_ptr<FFmpegSubtitleStream> subtitle_stream () const {
160                 boost::mutex::scoped_lock lm (_mutex);
161                 return _subtitle_stream;
162         }
163
164         std::vector<boost::shared_ptr<FFmpegAudioStream> > audio_streams () const {
165                 boost::mutex::scoped_lock lm (_mutex);
166                 return _audio_streams;
167         }
168         
169         boost::shared_ptr<FFmpegAudioStream> audio_stream () const {
170                 boost::mutex::scoped_lock lm (_mutex);
171                 return _audio_stream;
172         }
173
174         std::vector<Filter const *> filters () const {
175                 boost::mutex::scoped_lock lm (_mutex);
176                 return _filters;
177         }
178
179         void set_subtitle_stream (boost::shared_ptr<FFmpegSubtitleStream>);
180         void set_audio_stream (boost::shared_ptr<FFmpegAudioStream>);
181
182         boost::optional<double> first_video () const {
183                 boost::mutex::scoped_lock lm (_mutex);
184                 return _first_video;
185         }
186
187 private:
188         friend class ffmpeg_pts_offset_test;
189         
190         std::vector<boost::shared_ptr<FFmpegSubtitleStream> > _subtitle_streams;
191         boost::shared_ptr<FFmpegSubtitleStream> _subtitle_stream;
192         std::vector<boost::shared_ptr<FFmpegAudioStream> > _audio_streams;
193         boost::shared_ptr<FFmpegAudioStream> _audio_stream;
194         boost::optional<double> _first_video;
195         /** Video filters that should be used when generating DCPs */
196         std::vector<Filter const *> _filters;
197 };
198
199 #endif