Bump version
[dcpomatic.git] / src / lib / ffmpeg_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/ffmpeg_decoder.h
21  *  @brief A decoder using FFmpeg to decode content.
22  */
23
24 #include <vector>
25 #include <string>
26 #include <stdint.h>
27 #include <boost/shared_ptr.hpp>
28 #include <boost/optional.hpp>
29 #include <boost/thread/mutex.hpp>
30 extern "C" {
31 #include <libavcodec/avcodec.h>
32 #include <libpostproc/postprocess.h>
33 }
34 #include "util.h"
35 #include "decoder.h"
36 #include "video_decoder.h"
37 #include "audio_decoder.h"
38 #include "film.h"
39
40 struct AVFilterGraph;
41 struct AVCodecContext;
42 struct AVFilterContext;
43 struct AVFormatContext;
44 struct AVFrame;
45 struct AVBufferContext;
46 struct AVCodec;
47 struct AVStream;
48 class Job;
49 class Options;
50 class Image;
51 class Log;
52
53 class FFmpegAudioStream : public AudioStream
54 {
55 public:
56         FFmpegAudioStream (std::string n, int i, int s, int64_t c)
57                 : AudioStream (s, c)
58                 , _name (n)
59                 , _id (i)
60         {}
61
62         std::string to_string () const;
63
64         std::string name () const {
65                 return _name;
66         }
67
68         int id () const {
69                 return _id;
70         }
71
72         static boost::shared_ptr<FFmpegAudioStream> create (std::string t, boost::optional<int> v);
73
74 private:
75         friend class stream_test;
76         
77         FFmpegAudioStream (std::string t, boost::optional<int> v);
78         
79         std::string _name;
80         int _id;
81 };
82
83 /** @class FFmpegDecoder
84  *  @brief A decoder using FFmpeg to decode content.
85  */
86 class FFmpegDecoder : public VideoDecoder, public AudioDecoder
87 {
88 public:
89         FFmpegDecoder (boost::shared_ptr<Film>, DecodeOptions);
90         ~FFmpegDecoder ();
91
92         float frames_per_second () const;
93         libdcp::Size native_size () const;
94         SourceFrame length () const;
95         int time_base_numerator () const;
96         int time_base_denominator () const;
97         int sample_aspect_ratio_numerator () const;
98         int sample_aspect_ratio_denominator () const;
99
100         void set_audio_stream (boost::shared_ptr<AudioStream>);
101         void set_subtitle_stream (boost::shared_ptr<SubtitleStream>);
102
103         bool seek (double);
104         bool seek_to_last ();
105         void seek_forward ();
106         void seek_back ();
107
108 private:
109
110         bool pass ();
111         bool do_seek (double p, bool, bool);
112         PixelFormat pixel_format () const;
113         AVSampleFormat audio_sample_format () const;
114         int bytes_per_audio_sample () const;
115
116         void filter_and_emit_video ();
117
118         void setup_general ();
119         void setup_video ();
120         void setup_audio ();
121         void setup_subtitle ();
122
123         void decode_audio_packet ();
124
125         void maybe_add_subtitle ();
126         boost::shared_ptr<AudioBuffers> deinterleave_audio (uint8_t** data, int size);
127
128         void film_changed (Film::Property);
129
130         std::string stream_name (AVStream* s) const;
131
132         AVFormatContext* _format_context;
133         int _video_stream;
134         
135         AVFrame* _frame;
136
137         AVCodecContext* _video_codec_context;
138         AVCodec* _video_codec;
139         AVCodecContext* _audio_codec_context;    ///< may be 0 if there is no audio
140         AVCodec* _audio_codec;                   ///< may be 0 if there is no audio
141         AVCodecContext* _subtitle_codec_context; ///< may be 0 if there is no subtitle
142         AVCodec* _subtitle_codec;                ///< may be 0 if there is no subtitle
143
144         AVPacket _packet;
145
146         std::list<boost::shared_ptr<FilterGraph> > _filter_graphs;
147         boost::mutex _filter_graphs_mutex;
148 };