2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/ffmpeg_decoder.h
21 * @brief A decoder using FFmpeg to decode content.
27 #include <boost/shared_ptr.hpp>
28 #include <boost/optional.hpp>
29 #include <boost/thread/mutex.hpp>
31 #include <libavcodec/avcodec.h>
32 #include <libpostproc/postprocess.h>
36 #include "video_decoder.h"
37 #include "audio_decoder.h"
39 #include "ffmpeg_content.h"
42 struct AVCodecContext;
43 struct AVFilterContext;
44 struct AVFormatContext;
46 struct AVBufferContext;
54 /** @class FFmpegDecoder
55 * @brief A decoder using FFmpeg to decode content.
57 class FFmpegDecoder : public VideoDecoder, public AudioDecoder
60 FFmpegDecoder (boost::shared_ptr<const Film>, boost::shared_ptr<const FFmpegContent>, bool video, bool audio, bool subtitles, bool video_sync);
63 float video_frame_rate () const;
64 libdcp::Size native_size () const;
65 ContentVideoFrame video_length () const;
66 int time_base_numerator () const;
67 int time_base_denominator () const;
68 int sample_aspect_ratio_numerator () const;
69 int sample_aspect_ratio_denominator () const;
71 std::vector<FFmpegSubtitleStream> subtitle_streams () const {
72 return _subtitle_streams;
75 std::vector<FFmpegAudioStream> audio_streams () const {
76 return _audio_streams;
85 /* No copy construction */
86 FFmpegDecoder (FFmpegDecoder const &);
87 FFmpegDecoder& operator= (FFmpegDecoder const &);
89 bool do_seek (double p, bool);
90 PixelFormat pixel_format () const;
91 AVSampleFormat audio_sample_format () const;
92 int bytes_per_audio_sample () const;
94 void out_with_sync ();
95 void filter_and_emit_video (AVFrame *);
96 double frame_time () const;
98 void setup_general ();
101 void setup_subtitle ();
103 void decode_audio_packet ();
105 void maybe_add_subtitle ();
106 boost::shared_ptr<AudioBuffers> deinterleave_audio (uint8_t** data, int size);
108 void film_changed (Film::Property);
110 std::string stream_name (AVStream* s) const;
112 boost::shared_ptr<const FFmpegContent> _ffmpeg_content;
114 AVFormatContext* _format_context;
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
128 boost::optional<double> _first_video;
129 boost::optional<double> _first_audio;
131 std::list<boost::shared_ptr<FilterGraph> > _filter_graphs;
132 boost::mutex _filter_graphs_mutex;
134 std::vector<FFmpegSubtitleStream> _subtitle_streams;
135 std::vector<FFmpegAudioStream> _audio_streams;
139 bool _decode_subtitles;
142 /* It would appear (though not completely verified) that one must have
143 a mutex around calls to avcodec_open* and avcodec_close... and here
146 static boost::mutex _mutex;