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