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