Various more hacks; basically trying to remove Regions as an unnecessary complexity.
[dcpomatic.git] / src / lib / ffmpeg_decoder.h
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 /** @file  src/ffmpeg_decoder.h
23  *  @brief A decoder using FFmpeg to decode content.
24  */
25
26 #include <vector>
27 #include <string>
28 #include <stdint.h>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/optional.hpp>
31 #include <boost/thread/mutex.hpp>
32 extern "C" {
33 #include <libavcodec/avcodec.h>
34 #include <libpostproc/postprocess.h>
35 }
36 #include "util.h"
37 #include "decoder.h"
38 #include "video_decoder.h"
39 #include "audio_decoder.h"
40 #include "film.h"
41 #include "ffmpeg_content.h"
42
43 struct AVFilterGraph;
44 struct AVCodecContext;
45 struct AVFilterContext;
46 struct AVFormatContext;
47 struct AVFrame;
48 struct AVBufferContext;
49 struct AVCodec;
50 struct AVStream;
51 class Job;
52 class Options;
53 class Image;
54 class Log;
55
56 /** @class FFmpegDecoder
57  *  @brief A decoder using FFmpeg to decode content.
58  */
59 class FFmpegDecoder : public VideoDecoder, public AudioDecoder
60 {
61 public:
62         FFmpegDecoder (boost::shared_ptr<const Film>, boost::shared_ptr<const FFmpegContent>, bool video, bool audio, bool subtitles);
63         ~FFmpegDecoder ();
64
65         float video_frame_rate () const;
66         libdcp::Size native_size () const;
67         ContentVideoFrame video_length () const;
68         int time_base_numerator () const;
69         int time_base_denominator () const;
70         int sample_aspect_ratio_numerator () const;
71         int sample_aspect_ratio_denominator () const;
72
73         std::vector<boost::shared_ptr<FFmpegSubtitleStream> > subtitle_streams () const {
74                 return _subtitle_streams;
75         }
76         
77         std::vector<boost::shared_ptr<FFmpegAudioStream> > audio_streams () const {
78                 return _audio_streams;
79         }
80
81         bool seek (double);
82         bool seek_forward ();
83         bool seek_back ();
84         bool pass ();
85
86 private:
87
88         /* No copy construction */
89         FFmpegDecoder (FFmpegDecoder const &);
90         FFmpegDecoder& operator= (FFmpegDecoder const &);
91
92         PixelFormat pixel_format () const;
93         AVSampleFormat audio_sample_format () const;
94         int bytes_per_audio_sample () const;
95         bool do_seek (double, bool, bool);
96
97         void setup_general ();
98         void setup_video ();
99         void setup_audio ();
100         void setup_subtitle ();
101
102         bool decode_video_packet ();
103         void decode_audio_packet ();
104
105         void maybe_add_subtitle ();
106         boost::shared_ptr<AudioBuffers> deinterleave_audio (uint8_t** data, int size);
107
108         void film_changed (Film::Property);
109
110         std::string stream_name (AVStream* s) const;
111
112         boost::shared_ptr<const FFmpegContent> _ffmpeg_content;
113
114         AVFormatContext* _format_context;
115         int _video_stream;
116         
117         AVFrame* _frame;
118
119         AVCodecContext* _video_codec_context;
120         AVCodec* _video_codec;
121         AVCodecContext* _audio_codec_context;    ///< may be 0 if there is no audio
122         AVCodec* _audio_codec;                   ///< may be 0 if there is no audio
123         AVCodecContext* _subtitle_codec_context; ///< may be 0 if there is no subtitle
124         AVCodec* _subtitle_codec;                ///< may be 0 if there is no subtitle
125
126         AVPacket _packet;
127
128         std::list<boost::shared_ptr<FilterGraph> > _filter_graphs;
129         boost::mutex _filter_graphs_mutex;
130
131         std::vector<boost::shared_ptr<FFmpegSubtitleStream> > _subtitle_streams;
132         std::vector<boost::shared_ptr<FFmpegAudioStream> > _audio_streams;
133
134         bool _decode_video;
135         bool _decode_audio;
136         bool _decode_subtitles;
137
138         /* It would appear (though not completely verified) that one must have
139            a mutex around calls to avcodec_open* and avcodec_close... and here
140            it is.
141         */
142         static boost::mutex _mutex;
143 };