2 Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #ifndef DCPOMATIC_FFMPEG_H
23 #define DCPOMATIC_FFMPEG_H
26 #include "file_group.h"
27 #include "ffmpeg_subtitle_period.h"
28 #include <dcp/warnings.h>
29 LIBDCP_DISABLE_WARNINGS
31 #include <libavcodec/avcodec.h>
33 LIBDCP_ENABLE_WARNINGS
34 #include <boost/thread/mutex.hpp>
37 struct AVFormatContext;
43 class FFmpegAudioStream;
50 explicit FFmpeg (std::shared_ptr<const FFmpegContent>);
53 std::shared_ptr<const FFmpegContent> ffmpeg_content () const {
54 return _ffmpeg_content;
57 int avio_read (uint8_t *, int);
58 int64_t avio_seek (int64_t, int);
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
67 static FFmpegSubtitlePeriod subtitle_period (AVPacket const* packet, AVStream const* stream, AVSubtitle const & sub);
69 std::shared_ptr<const FFmpegContent> _ffmpeg_content;
71 uint8_t* _avio_buffer = nullptr;
72 int _avio_buffer_size = 4096;
73 AVIOContext* _avio_context = nullptr;
74 FileGroup _file_group;
76 AVFormatContext* _format_context = nullptr;
77 std::vector<AVCodecContext*> _codec_context;
79 /** AVFrame used for decoding video */
80 AVFrame* _video_frame = nullptr;
81 /** Index of video stream within AVFormatContext */
82 boost::optional<int> _video_stream;
84 AVFrame* audio_frame (std::shared_ptr<const FFmpegAudioStream> stream);
86 /* It would appear (though not completely verified) that one must have
87 a mutex around calls to avcodec_open* and avcodec_close... and here
90 static boost::mutex _mutex;
93 void setup_general ();
94 void setup_decoders ();
96 /** AVFrames used for decoding audio streams; accessed with audio_frame() */
97 std::map<std::shared_ptr<const FFmpegAudioStream>, AVFrame*> _audio_frame;