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