summaryrefslogtreecommitdiff
path: root/src/lib/ffmpeg_file_encoder.h
blob: f0b3eac8c1fa1f610f3bdbe6108a3190edba6578 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
    Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#ifndef DCPOMATIC_FFMPEG_FILE_ENCODER_H
#define DCPOMATIC_FFMPEG_FILE_ENCODER_H


#include "audio_mapping.h"
#include "dcpomatic_time.h"
#include "encoder.h"
#include "event_history.h"
#include "log.h"
#include <dcp/key.h>
#include <dcp/warnings.h>
LIBDCP_DISABLE_WARNINGS
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
LIBDCP_ENABLE_WARNINGS


class ExportAudioStream;


enum class ExportFormat
{
	PRORES_4444,
	PRORES_HQ,
	H264_AAC,
	SUBTITLES_DCP
};


class FFmpegFileEncoder
{
public:
	FFmpegFileEncoder (
		dcp::Size video_frame_size,
		int video_frame_rate,
		int audio_frame_rate,
		int channels,
		ExportFormat,
		bool audio_stream_per_channel,
		int x264_crf,
		boost::filesystem::path output
		);

	~FFmpegFileEncoder ();

	void video (std::shared_ptr<PlayerVideo>, dcpomatic::DCPTime);
	void audio (std::shared_ptr<AudioBuffers>);
	void subtitle (PlayerText, dcpomatic::DCPTimePeriod);

	void flush ();

	static AVPixelFormat pixel_format (ExportFormat format);

private:
	void setup_video ();
	void setup_audio ();

	void audio_frame (int size);

	static void buffer_free(void* opaque, uint8_t* data);
	void buffer_free2(uint8_t* data);

	AVCodec const * _video_codec = nullptr;
	AVCodecContext* _video_codec_context = nullptr;
	std::vector<std::shared_ptr<ExportAudioStream>> _audio_streams;
	bool _audio_stream_per_channel;
	AVFormatContext* _format_context = nullptr;
	AVStream* _video_stream = nullptr;
	AVPixelFormat _pixel_format;
	AVSampleFormat _sample_format;
	AVDictionary* _video_options = nullptr;
	std::string _video_codec_name;
	std::string _audio_codec_name;
	int _audio_channels;

	boost::filesystem::path _output;
	dcp::Size _video_frame_size;
	int _video_frame_rate;
	int _audio_frame_rate;

	int64_t _audio_frames = 0;

	std::shared_ptr<AudioBuffers> _pending_audio;

	/** Store of shared_ptr<Image> to keep them alive whilst raw pointers into
	    their data have been passed to FFmpeg.  The second part of the pair is
	    a count of how many copies of the same key must be kept.
	*/
	std::map<uint8_t*, std::pair<std::shared_ptr<const Image>, int>> _pending_images;
	boost::mutex _pending_images_mutex;

	static int _video_stream_index;
	static int _audio_stream_index_base;
};

#endif