8d89c7df7e2e06963ecd650881529b49395be2ee
[dcpomatic.git] / src / lib / cuda.h
1 /*
2     Copyright (C) 2022 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
22 #include "image.h"
23 #include <dcp/array_data.h>
24 extern "C" {
25 #include <libavutil/pixfmt.h>
26 }
27 #include <boost/thread/condition.hpp>
28 #include <boost/thread/mutex.hpp>
29 #include <memory>
30 #include <queue>
31 #include <thread>
32
33
34 class CUDA
35 {
36 public:
37         CUDA();
38
39         CUDA(CUDA &) = delete;
40         CUDA(CUDA &&) = delete;
41
42         std::shared_ptr<Image> decode(std::shared_ptr<const dcp::Data> j2k_data, int reduce, AVPixelFormat pixel_format, Image::Alignment alignment);
43
44         static CUDA* instance();
45
46 private:
47         void decode_thread();
48
49         typedef uint64_t ID;
50
51         class DecodeQueueItem
52         {
53         public:
54                 ID id;
55                 std::shared_ptr<const dcp::Data> data;
56                 int reduce;
57                 AVPixelFormat pixel_format;
58                 Image::Alignment alignment;
59         };
60
61         std::queue<DecodeQueueItem> _decode_queue;
62         std::map<ID, std::shared_ptr<Image>> _decode_output;
63         boost::condition _decode_queue_empty_condition;
64         boost::condition _decode_complete_condition;
65         boost::mutex _decode_mutex;
66         std::thread _decode_thread;
67         ID _next_decode_id = 0;
68
69         static CUDA* _instance;
70 };
71