Non-working FFmpeg context cache.
[dcpomatic.git] / src / lib / ffmpeg.h
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #ifndef DCPOMATIC_FFMPEG_H
22 #define DCPOMATIC_FFMPEG_H
23
24 #include "file_group.h"
25 #include "ffmpeg_subtitle_period.h"
26 #include "encrypted_ecinema_kdm.h"
27 extern "C" {
28 #include <libavcodec/avcodec.h>
29 }
30 #include <boost/shared_ptr.hpp>
31 #include <boost/thread/mutex.hpp>
32
33 struct AVFormatContext;
34 struct AVFrame;
35 struct AVIOContext;
36
37 class FFmpegContent;
38 class FFmpegAudioStream;
39 class Log;
40
41 class FFmpeg
42 {
43 public:
44         explicit FFmpeg (boost::shared_ptr<const FFmpegContent>);
45         virtual ~FFmpeg ();
46
47         boost::shared_ptr<const FFmpegContent> ffmpeg_content () const {
48                 return _ffmpeg_content;
49         }
50
51         int avio_read (uint8_t *, int);
52         int64_t avio_seek (int64_t, int);
53
54 protected:
55         AVCodecContext* video_codec_context () const;
56         AVCodecContext* subtitle_codec_context () const;
57         dcpomatic::ContentTime pts_offset (
58                 std::vector<boost::shared_ptr<FFmpegAudioStream> > audio_streams, boost::optional<dcpomatic::ContentTime> first_video, double video_frame_rate
59                 ) const;
60
61         static FFmpegSubtitlePeriod subtitle_period (AVSubtitle const & sub);
62
63         boost::shared_ptr<const FFmpegContent> _ffmpeg_content;
64
65         int _avio_buffer_size;
66         FileGroup _file_group;
67
68         AVFormatContext* _format_context;
69         AVPacket _packet;
70         AVFrame* _frame;
71
72         /** Index of video stream within AVFormatContext */
73         boost::optional<int> _video_stream;
74
75         /* It would appear (though not completely verified) that one must have
76            a mutex around calls to avcodec_open* and avcodec_close... and here
77            it is.
78         */
79         static boost::mutex _mutex;
80
81 private:
82         void setup_general ();
83         void setup_decoders ();
84
85         static void ffmpeg_log_callback (void* ptr, int level, const char* fmt, va_list vl);
86         static boost::weak_ptr<Log> _ffmpeg_log;
87
88         struct Cache
89         {
90                 Cache (std::vector<boost::filesystem::path> paths_, boost::optional<EncryptedECinemaKDM> kdm_, AVFormatContext* format_context_)
91                         : paths(paths_)
92                         , kdm(kdm_)
93                         , format_context(format_context_)
94                 {}
95                 
96                 std::vector<boost::filesystem::path> paths;
97                 boost::optional<EncryptedECinemaKDM> kdm;
98                 AVFormatContext* format_context;
99         };
100         
101         static std::list<Cache> _cache;
102 };
103
104 #endif