Pull some emacs mode lines.
[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 #include <boost/thread/mutex.hpp>
30 extern "C" {
31 #include <libavcodec/avcodec.h>
32 #include <libpostproc/postprocess.h>
33 }
34 #include "util.h"
35 #include "decoder.h"
36 #include "video_decoder.h"
37 #include "audio_decoder.h"
38
39 struct AVFilterGraph;
40 struct AVCodecContext;
41 struct AVFilterContext;
42 struct AVFormatContext;
43 struct AVFrame;
44 struct AVBufferContext;
45 struct AVCodec;
46 struct AVStream;
47 class Job;
48 class Options;
49 class Image;
50 class Log;
51 class FFmpegContent;
52 class Film;
53
54 /** @class FFmpegDecoder
55  *  @brief A decoder using FFmpeg to decode content.
56  */
57 class FFmpegDecoder : public VideoDecoder, public AudioDecoder
58 {
59 public:
60         FFmpegDecoder (boost::shared_ptr<const Film>, boost::shared_ptr<const FFmpegContent>, bool video, bool audio);
61         ~FFmpegDecoder ();
62
63         /* Decoder */
64
65         void pass ();
66         void seek (Time);
67         void seek_back ();
68         void seek_forward ();
69         Time position () const;
70         bool done () const;
71
72         /* VideoDecoder */
73
74         float video_frame_rate () const;
75         libdcp::Size video_size () const;
76         ContentVideoFrame video_length () const;
77
78         /* FFmpegDecoder */
79
80         std::vector<boost::shared_ptr<FFmpegSubtitleStream> > subtitle_streams () const {
81                 return _subtitle_streams;
82         }
83         
84         std::vector<boost::shared_ptr<FFmpegAudioStream> > audio_streams () const {
85                 return _audio_streams;
86         }
87
88         boost::shared_ptr<const FFmpegContent> ffmpeg_content () const {
89                 return _ffmpeg_content;
90         }
91
92 private:
93
94         /* No copy construction */
95         FFmpegDecoder (FFmpegDecoder const &);
96         FFmpegDecoder& operator= (FFmpegDecoder const &);
97
98         PixelFormat pixel_format () const;
99         AVSampleFormat audio_sample_format () const;
100         int bytes_per_audio_sample () const;
101         void do_seek (Time, bool, bool);
102
103         void setup_general ();
104         void setup_video ();
105         void setup_audio ();
106         void setup_subtitle ();
107
108         bool decode_video_packet ();
109         void decode_audio_packet ();
110
111         void maybe_add_subtitle ();
112         boost::shared_ptr<AudioBuffers> deinterleave_audio (uint8_t** data, int size);
113
114         std::string stream_name (AVStream* s) const;
115
116         boost::shared_ptr<const FFmpegContent> _ffmpeg_content;
117
118         AVFormatContext* _format_context;
119         int _video_stream;
120         
121         AVFrame* _frame;
122
123         AVCodecContext* _video_codec_context;
124         AVCodec* _video_codec;
125         AVCodecContext* _audio_codec_context;    ///< may be 0 if there is no audio
126         AVCodec* _audio_codec;                   ///< may be 0 if there is no audio
127         AVCodecContext* _subtitle_codec_context; ///< may be 0 if there is no subtitle
128         AVCodec* _subtitle_codec;                ///< may be 0 if there is no subtitle
129
130         AVPacket _packet;
131
132         std::list<boost::shared_ptr<FilterGraph> > _filter_graphs;
133         boost::mutex _filter_graphs_mutex;
134
135         std::vector<boost::shared_ptr<FFmpegSubtitleStream> > _subtitle_streams;
136         std::vector<boost::shared_ptr<FFmpegAudioStream> > _audio_streams;
137
138         bool _decode_video;
139         bool _decode_audio;
140
141         /* It would appear (though not completely verified) that one must have
142            a mutex around calls to avcodec_open* and avcodec_close... and here
143            it is.
144         */
145         static boost::mutex _mutex;
146 };