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