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