Try to clean up stream handling wrt audio channel counts.
[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 extern "C" {
29 #include <libavcodec/avcodec.h>
30 #include <libpostproc/postprocess.h>
31 }
32 #include "util.h"
33 #include "decoder.h"
34
35 struct AVFilterGraph;
36 struct AVCodecContext;
37 struct AVFilterContext;
38 struct AVFormatContext;
39 struct AVFrame;
40 struct AVBufferContext;
41 struct AVCodec;
42 struct AVStream;
43 class Job;
44 class FilmState;
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<const FilmState>, boost::shared_ptr<const Options>, Job *, Log *, bool, bool);
56         ~FFmpegDecoder ();
57
58         /* Methods to query our input video */
59         int length_in_frames () const;
60         int decoding_frames () const;
61         float frames_per_second () const;
62         Size native_size () const;
63         int audio_channels () const;
64         int audio_sample_rate () const;
65         AVSampleFormat audio_sample_format () const;
66         int64_t audio_channel_layout () const;
67         bool has_subtitles () const;
68         int bytes_per_audio_sample () const;
69
70         std::vector<AudioStream> audio_streams () const;
71         std::vector<SubtitleStream> subtitle_streams () const;
72
73 private:
74
75         bool do_pass ();
76         PixelFormat pixel_format () const;
77         int time_base_numerator () const;
78         int time_base_denominator () const;
79         int sample_aspect_ratio_numerator () const;
80         int sample_aspect_ratio_denominator () const;
81
82         void setup_general ();
83         void setup_video ();
84         void setup_audio ();
85         void setup_subtitle ();
86
87         void maybe_add_subtitle ();
88
89         std::string stream_name (AVStream* s) const;
90
91         AVFormatContext* _format_context;
92         int _video_stream;
93         int _audio_stream; ///< may be < 0 if there is no audio
94         int _subtitle_stream; ///< may be < 0 if there is no subtitle
95         AVFrame* _frame;
96
97         std::vector<AudioStream> _audio_streams;
98         std::vector<SubtitleStream> _subtitle_streams;
99         
100         AVCodecContext* _video_codec_context;
101         AVCodec* _video_codec;
102         AVCodecContext* _audio_codec_context;    ///< may be 0 if there is no audio
103         AVCodec* _audio_codec;                   ///< may be 0 if there is no audio
104         AVCodecContext* _subtitle_codec_context; ///< may be 0 if there is no subtitle
105         AVCodec* _subtitle_codec;                ///< may be 0 if there is no subtitle
106
107         AVPacket _packet;
108 };