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