Merge master.
[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
36 struct AVFilterGraph;
37 struct AVCodecContext;
38 struct AVFilterContext;
39 struct AVFormatContext;
40 struct AVFrame;
41 struct AVBufferContext;
42 struct AVCodec;
43 struct AVStream;
44 class Job;
45 class Options;
46 class Image;
47 class Log;
48
49 /** @class FFmpegDecoder
50  *  @brief A decoder using FFmpeg to decode content.
51  */
52 class FFmpegDecoder : public Decoder
53 {
54 public:
55         FFmpegDecoder (boost::shared_ptr<Film>, boost::shared_ptr<const Options>, Job *);
56         ~FFmpegDecoder ();
57
58         /* Methods to query our input video */
59         float frames_per_second () const;
60         Size native_size () const;
61
62         void set_audio_stream (boost::optional<AudioStream>);
63         void set_subtitle_stream (boost::optional<SubtitleStream>);
64
65 private:
66
67         bool pass ();
68         PixelFormat pixel_format () const;
69         int time_base_numerator () const;
70         int time_base_denominator () const;
71         int sample_aspect_ratio_numerator () const;
72         int sample_aspect_ratio_denominator () const;
73         AVSampleFormat audio_sample_format () const;
74         int bytes_per_audio_sample () const;
75
76         void setup_general ();
77         void setup_video ();
78         void setup_audio ();
79         void setup_subtitle ();
80
81         void maybe_add_subtitle ();
82         boost::shared_ptr<AudioBuffers> deinterleave_audio (uint8_t* data, int size);
83
84         std::string stream_name (AVStream* s) const;
85
86         AVFormatContext* _format_context;
87         int _video_stream;
88         
89         AVFrame* _frame;
90
91         AVCodecContext* _video_codec_context;
92         AVCodec* _video_codec;
93         AVCodecContext* _audio_codec_context;    ///< may be 0 if there is no audio
94         AVCodec* _audio_codec;                   ///< may be 0 if there is no audio
95         AVCodecContext* _subtitle_codec_context; ///< may be 0 if there is no subtitle
96         AVCodec* _subtitle_codec;                ///< may be 0 if there is no subtitle
97
98         AVPacket _packet;
99
100         boost::optional<double> _first_video;
101         boost::optional<double> _first_audio;
102 };