summaryrefslogtreecommitdiff
path: root/src/lib/cuda_j2k_frame_encoder.h
blob: 38d5c9ef834bb7d31cfd612ad22ac450b66fd622 (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
/*
    Copyright (C) 2022 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_CUDA_J2K_FRAME_ENCODER
#define DCPOMATIC_CUDA_J2K_FRAME_ENCODER


#include "j2k_frame_encoder.h"
#include "types.h"
#include <dcp/types.h>
#include <nvjpeg2k.h>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <map>
#include <queue>
#include <thread>
#include <vector>


class CUDAJ2KFrameEncoder : public J2KFrameEncoder
{
public:
	CUDAJ2KFrameEncoder();

	boost::optional<dcp::ArrayData> encode(DCPVideo const &) override;
	void flush() override;
	void log_thread_start() override;

private:
	void encode_queue();

	class Input
	{
	public:
		Input(DCPVideo const& vf);
		Input(Input const& other) = delete;
		Input(Input&& other);
		~Input();

		Input& operator=(Input const& other) = delete;

		nvjpeg2kImage_t const* const device_image() const {
			return &_device_image;
		}

		int index() const {
			return _index;
		}

		Eyes eyes() const {
			return _eyes;
		}

		dcp::Size size() const {
			return _size;
		}

		Resolution resolution() const {
			return _resolution;
		}

	private:
		std::vector<int16_t> xyz_x;
		std::vector<int16_t> xyz_y;
		std::vector<int16_t> xyz_z;
		uint8_t* _pixel_data_h[3];
		uint8_t* _pixel_data_d[3];
		size_t _pitch_in_bytes_h[3];
		size_t _pitch_in_bytes_d[3];
		nvjpeg2kImage_t _device_image;
		int _index;
		Eyes _eyes;
		dcp::Size _size;
		Resolution _resolution;
	};

	boost::optional<dcp::Size> _size;
	boost::optional<Resolution> _resolution;

	static void cuda_thread();

	static std::vector<std::thread> _cuda_threads;
	static std::queue<Input> _input;
	static boost::condition _input_condition;
	static boost::mutex _input_mutex;

	static std::map<std::pair<int, Eyes>, dcp::ArrayData> _output;
	static boost::condition _output_condition;
	static boost::mutex _output_mutex;
};


#endif