Various work on better seeking (and seeking of audio).
[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 #include "subtitle_decoder.h"
39 #include "ffmpeg.h"
40
41 class Film;
42 class FilterGraph;
43 class ffmpeg_pts_offset_test;
44
45 /** @class FFmpegDecoder
46  *  @brief A decoder using FFmpeg to decode content.
47  */
48 class FFmpegDecoder : public VideoDecoder, public AudioDecoder, public SubtitleDecoder, public FFmpeg
49 {
50 public:
51         FFmpegDecoder (boost::shared_ptr<const Film>, boost::shared_ptr<const FFmpegContent>, bool video, bool audio);
52         ~FFmpegDecoder ();
53
54         void pass ();
55         void seek (Time time, bool);
56         bool done () const;
57
58 private:
59         friend class ::ffmpeg_pts_offset_test;
60
61         static double compute_pts_offset (double, double, float);
62
63         void flush ();
64
65         void setup_subtitle ();
66
67         AVSampleFormat audio_sample_format () const;
68         int bytes_per_audio_sample () const;
69
70         bool decode_video_packet ();
71         void decode_audio_packet ();
72         void decode_subtitle_packet ();
73
74         void maybe_add_subtitle ();
75         boost::shared_ptr<AudioBuffers> deinterleave_audio (uint8_t** data, int size);
76
77         bool seek_overrun_finished (Time) const;
78         bool seek_final_finished (int, int) const;
79         int minimal_run (boost::function<bool (int)>);
80         void seek_and_flush (int64_t);
81
82         AVCodecContext* _subtitle_codec_context; ///< may be 0 if there is no subtitle
83         AVCodec* _subtitle_codec;                ///< may be 0 if there is no subtitle
84         
85         std::list<boost::shared_ptr<FilterGraph> > _filter_graphs;
86         boost::mutex _filter_graphs_mutex;
87
88         bool _decode_video;
89         bool _decode_audio;
90
91         double _video_pts_offset;
92         double _audio_pts_offset;
93         bool _just_sought;
94 };