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