Don't cache sample format, fix up various things.
[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 <sigc++/sigc++.h>
32 #include "util.h"
33 #include "stream.h"
34
35 class Job;
36 class FilmState;
37 class Options;
38 class Image;
39 class Log;
40 class DelayLine;
41 class TimedSubtitle;
42 class Subtitle;
43
44 /** @class Decoder.
45  *  @brief Parent class for decoders of content.
46  *
47  *  These classes can be instructed run through their content
48  *  (by calling ::go), and they emit signals when video or audio data is ready for something else
49  *  to process.
50  */
51 class Decoder
52 {
53 public:
54         Decoder (boost::shared_ptr<const FilmState>, boost::shared_ptr<const Options>, Job *, Log *, bool, bool);
55         virtual ~Decoder ();
56
57         /* Methods to query our input video */
58
59         /** @return length in video frames */
60         virtual int length_in_frames () const = 0;
61         /** @return video frames per second, or 0 if unknown */
62         virtual float frames_per_second () const = 0;
63         /** @return native size in pixels */
64         virtual Size native_size () const = 0;
65         /** @return number of audio channels */
66         virtual int audio_channels () const = 0;
67         /** @return audio sampling rate in Hz */
68         virtual int audio_sample_rate () const = 0;
69         /** @return format of audio samples */
70         virtual AVSampleFormat audio_sample_format () const = 0;
71         virtual int64_t audio_channel_layout () const = 0;
72         virtual bool has_subtitles () const = 0;
73
74         void process_begin ();
75         bool pass ();
76         void process_end ();
77         void go ();
78
79         /** @return the index of the last video frame to be processed */
80         int last_video_frame () const {
81                 return _video_frame;
82         }
83
84         virtual std::vector<Stream> audio_streams () const {
85                 return std::vector<Stream> ();
86         }
87         
88         virtual std::vector<Stream> subtitle_streams () const {
89                 return std::vector<Stream> ();
90         }
91
92         virtual void set_audio_stream (Stream s) {}
93         virtual void set_subtitle_stream (Stream s) {}
94         
95         /** Emitted when a video frame is ready.
96          *  First parameter is the frame.
97          *  Second parameter is its index within the content.
98          *  Third parameter is either 0 or a subtitle that should be on this frame.
99          */
100         sigc::signal<void, boost::shared_ptr<Image>, int, boost::shared_ptr<Subtitle> > Video;
101
102         /** Emitted when some audio data is ready.
103          *  First parameter is an array of pointers to deinterleaved, floating point sample data for each channel.
104          *  Second parameter is the size of the data in frames (ie samples on each channel).
105          */
106         sigc::signal<void, float**, int> Audio;
107         
108 protected:
109         /** perform a single pass at our content */
110         virtual bool do_pass () = 0;
111         virtual PixelFormat pixel_format () const = 0;
112         virtual int time_base_numerator () const = 0;
113         virtual int time_base_denominator () const = 0;
114         virtual int sample_aspect_ratio_numerator () const = 0;
115         virtual int sample_aspect_ratio_denominator () const = 0;
116         
117         void process_video (AVFrame *);
118         void process_audio (uint8_t *, int);
119         void process_subtitle (boost::shared_ptr<TimedSubtitle>);
120
121         /** our FilmState */
122         boost::shared_ptr<const FilmState> _fs;
123         /** our options */
124         boost::shared_ptr<const Options> _opt;
125         /** associated Job, or 0 */
126         Job* _job;
127         /** log that we can write to */
128         Log* _log;
129
130         /** true to do the bare minimum of work; just run through the content.  Useful for acquiring
131          *  accurate frame counts as quickly as possible.  This generates no video or audio output.
132          */
133         bool _minimal;
134
135         /** ignore_length Ignore the content's claimed length when computing progress */
136         bool _ignore_length;
137
138 private:
139         void setup_video_filters ();
140         void emit_audio (uint8_t* data, int size);
141         int bytes_per_audio_sample () const;
142         
143         /** last video frame to be processed */
144         int _video_frame;
145
146         AVFilterContext* _buffer_src_context;
147         AVFilterContext* _buffer_sink_context;
148
149         bool _have_setup_video_filters;
150         DelayLine* _delay_line;
151         int _delay_in_bytes;
152
153         /* Number of audio frames that we have pushed to the encoder
154            (at the DCP sample rate).
155         */
156         int64_t _audio_frames_processed;
157
158         boost::shared_ptr<TimedSubtitle> _timed_subtitle;
159 };
160
161 #endif