Fix merge; other tweaks.
[dcpomatic.git] / src / lib / decoder.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/decoder.h
21  *  @brief Parent class for decoders of content.
22  */
23
24 #ifndef DVDOMATIC_DECODER_H
25 #define DVDOMATIC_DECODER_H
26
27 #include <vector>
28 #include <string>
29 #include <stdint.h>
30 #include <boost/shared_ptr.hpp>
31 #include <boost/signals2.hpp>
32 #include "util.h"
33 #include "stream.h"
34
35 class Job;
36 class Options;
37 class Image;
38 class Log;
39 class DelayLine;
40 class TimedSubtitle;
41 class Subtitle;
42 class Film;
43 class FilterGraph;
44
45 /** @class Decoder.
46  *  @brief Parent class for decoders of content.
47  *
48  *  These classes can be instructed run through their content
49  *  (by calling ::go), and they emit signals when video or audio data is ready for something else
50  *  to process.
51  */
52 class Decoder
53 {
54 public:
55         Decoder (boost::shared_ptr<Film>, boost::shared_ptr<const Options>, Job *, bool);
56         virtual ~Decoder ();
57
58         /* Methods to query our input video */
59
60         /** @return video frames per second, or 0 if unknown */
61         virtual float frames_per_second () const = 0;
62         /** @return native size in pixels */
63         virtual Size native_size () const = 0;
64         /** @return number of audio channels */
65         virtual int audio_channels () const = 0;
66         /** @return audio sampling rate in Hz */
67         virtual int audio_sample_rate () const = 0;
68         /** @return format of audio samples */
69         virtual AVSampleFormat audio_sample_format () const = 0;
70         virtual int64_t audio_channel_layout () const = 0;
71         virtual bool has_subtitles () const = 0;
72
73         virtual int time_base_numerator () const = 0;
74         virtual int time_base_denominator () const = 0;
75         virtual int sample_aspect_ratio_numerator () const = 0;
76         virtual int sample_aspect_ratio_denominator () const = 0;
77         
78         void process_begin ();
79         virtual bool pass () = 0;
80         void process_end ();
81         void go ();
82
83         /** @return the number of video frames we got from the source in the last run */
84         SourceFrame video_frames_in () const {
85                 return _video_frames_in;
86         }
87
88         virtual std::vector<AudioStream> audio_streams () const {
89                 return std::vector<AudioStream> ();
90         }
91         
92         virtual std::vector<SubtitleStream> subtitle_streams () const {
93                 return std::vector<SubtitleStream> ();
94         }
95
96         /** Emitted when a video frame is ready.
97          *  First parameter is the frame within the source.
98          *  Second parameter is its index within the content.
99          *  Third parameter is either 0 or a subtitle that should be on this frame.
100          */
101         boost::signals2::signal<void (boost::shared_ptr<Image>, SourceFrame, boost::shared_ptr<Subtitle>)> Video;
102
103         /** Emitted when some audio data is ready */
104         boost::signals2::signal<void (boost::shared_ptr<AudioBuffers>)> Audio;
105
106 protected:
107         
108         virtual PixelFormat pixel_format () const = 0;
109         
110         void process_video (AVFrame *);
111         void process_audio (uint8_t *, int);
112         void process_subtitle (boost::shared_ptr<TimedSubtitle>);
113         void repeat_last_video ();
114
115         int bytes_per_audio_sample () const;
116         
117         /** our Film */
118         boost::shared_ptr<Film> _film;
119         /** our options */
120         boost::shared_ptr<const Options> _opt;
121         /** associated Job, or 0 */
122         Job* _job;
123
124         /** true to do the bare minimum of work; just run through the content.  Useful for acquiring
125          *  accurate frame counts as quickly as possible.  This generates no video or audio output.
126          */
127         bool _minimal;
128
129 private:
130         void emit_video (boost::shared_ptr<Image>, boost::shared_ptr<Subtitle>);
131         void emit_audio (boost::shared_ptr<AudioBuffers>);
132
133         int64_t video_frames_to_audio_frames (SourceFrame v) const;
134         
135         SourceFrame _video_frames_in;
136         SourceFrame _video_frames_out;
137         int64_t _audio_frames_in;
138         int64_t _audio_frames_out;
139         
140         std::list<boost::shared_ptr<FilterGraph> > _filter_graphs;
141
142         DelayLine* _delay_line;
143         int _delay_in_frames;
144
145         boost::shared_ptr<TimedSubtitle> _timed_subtitle;
146
147         boost::shared_ptr<Image> _last_image;
148         boost::shared_ptr<Subtitle> _last_subtitle;
149 };
150
151 #endif