Supporters update.
[dcpomatic.git] / src / lib / image_store.cc
1 /*
2     Copyright (C) 2017-2018 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 "image_store.h"
24 extern "C" {
25 #include <libavutil/buffer.h>
26 }
27
28
29 using std::shared_ptr;
30
31
32 void
33 ImageStore::buffer_free(void* opaque, uint8_t* data)
34 {
35         reinterpret_cast<ImageStore*>(opaque)->buffer_free2(data);
36 }
37
38
39 void
40 ImageStore::buffer_free2(uint8_t* data)
41 {
42         boost::mutex::scoped_lock lm(_mutex);
43         auto iter = _images.find(data);
44         if (iter != _images.end()) {
45                 iter->second.second--;
46                 if (iter->second.second == 0) {
47                         _images.erase(data);
48                 }
49         }
50 }
51
52
53 AVBufferRef*
54 ImageStore::create_buffer(shared_ptr<const Image> image, int component)
55 {
56         boost::mutex::scoped_lock lm(_mutex);
57         auto key = image->data()[component];
58         auto iter = _images.find(key);
59         if (iter != _images.end()) {
60                 iter->second.second++;
61         } else {
62                 _images[key] = { image, 1 };
63         }
64
65         return av_buffer_create(image->data()[component], image->stride()[component] * image->size().height, &buffer_free, this, 0);
66 }