std::shared_ptr
[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 "warnings.h"
27 DCPOMATIC_DISABLE_WARNINGS
28 extern "C" {
29 #include <libavcodec/avcodec.h>
30 }
31 DCPOMATIC_ENABLE_WARNINGS
32 #include <boost/thread/mutex.hpp>
33
34 struct AVFormatContext;
35 struct AVFrame;
36 struct AVIOContext;
37
38 class FFmpegContent;
39 class FFmpegAudioStream;
40 class Log;
41
42 class FFmpeg
43 {
44 public:
45         explicit FFmpeg (std::shared_ptr<const FFmpegContent>);
46         virtual ~FFmpeg ();
47
48         std::shared_ptr<const FFmpegContent> ffmpeg_content () const {
49                 return _ffmpeg_content;
50         }
51
52         int avio_read (uint8_t *, int);
53         int64_t avio_seek (int64_t, int);
54
55 protected:
56         AVCodecContext* video_codec_context () const;
57         AVCodecContext* subtitle_codec_context () const;
58         dcpomatic::ContentTime pts_offset (
59                 std::vector<std::shared_ptr<FFmpegAudioStream> > audio_streams, boost::optional<dcpomatic::ContentTime> first_video, double video_frame_rate
60                 ) const;
61
62         static FFmpegSubtitlePeriod subtitle_period (AVSubtitle const & sub);
63
64         std::shared_ptr<const FFmpegContent> _ffmpeg_content;
65
66         uint8_t* _avio_buffer;
67         int _avio_buffer_size;
68         AVIOContext* _avio_context;
69         FileGroup _file_group;
70
71         AVFormatContext* _format_context;
72         AVPacket _packet;
73         AVFrame* _frame;
74
75         /** Index of video stream within AVFormatContext */
76         boost::optional<int> _video_stream;
77
78         /* It would appear (though not completely verified) that one must have
79            a mutex around calls to avcodec_open* and avcodec_close... and here
80            it is.
81         */
82         static boost::mutex _mutex;
83
84 private:
85         void setup_general ();
86         void setup_decoders ();
87
88         static void ffmpeg_log_callback (void* ptr, int level, const char* fmt, va_list vl);
89         static std::weak_ptr<Log> _ffmpeg_log;
90 };
91
92 #endif