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