diff options
| author | Carl Hetherington <cth@carlh.net> | 2023-04-16 22:07:10 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2023-04-16 22:14:26 +0200 |
| commit | b0520d7a1bffaff1ca7161f5b7672f06b13808a1 (patch) | |
| tree | 8b7777c6fc7fc6f2f36e85c60151031b8860a09b /src | |
| parent | 65d21ed2de3a76dc574c8ad111fc02d8f696fcd6 (diff) | |
Use a shared_ptr<vector> for ArrayData rather than a shared_array.
This is simpler and allows us to remove the hack of allocating some
"maximum" buffer for incoming JPEG2000 data.
It does mean that the buffer is zero-initialized before being written
to, but hopefully that doesn't matter too much.
Diffstat (limited to 'src')
| -rw-r--r-- | src/array_data.cc | 22 | ||||
| -rw-r--r-- | src/array_data.h | 22 | ||||
| -rw-r--r-- | src/j2k_transcode.cc | 21 | ||||
| -rw-r--r-- | src/smpte_subtitle_asset.cc | 7 |
4 files changed, 20 insertions, 52 deletions
diff --git a/src/array_data.cc b/src/array_data.cc index 1234ba22..52a22cf6 100644 --- a/src/array_data.cc +++ b/src/array_data.cc @@ -45,35 +45,25 @@ #include <cstdio> -using boost::shared_array; using namespace dcp; ArrayData::ArrayData () + : _data(std::make_shared<std::vector<uint8_t>>()) { } ArrayData::ArrayData (int size) - : _data (new uint8_t[size]) - , _size (size) + : _data(std::make_shared<std::vector<uint8_t>>(size)) { } ArrayData::ArrayData (uint8_t const * data, int size) - : _data (new uint8_t[size]) - , _size (size) -{ - memcpy (_data.get(), data, size); -} - - -ArrayData::ArrayData (shared_array<uint8_t> data, int size) - : _data (data) - , _size (size) + : _data(std::make_shared<std::vector<uint8_t>>(data, data + size)) { } @@ -81,15 +71,15 @@ ArrayData::ArrayData (shared_array<uint8_t> data, int size) ArrayData::ArrayData (boost::filesystem::path file) { - _size = boost::filesystem::file_size (file); - _data.reset (new uint8_t[_size]); + auto const size = boost::filesystem::file_size (file); + _data = std::make_shared<std::vector<uint8_t>>(size); File f(file, "rb"); if (!f) { throw FileError ("could not open file for reading", file, errno); } - if (f.read(_data.get(), 1, _size) != static_cast<size_t>(_size)) { + if (f.read(_data->data(), 1, size) != static_cast<size_t>(size)) { throw FileError ("could not read from file", file, errno); } } diff --git a/src/array_data.h b/src/array_data.h index da7229bb..7a425f44 100644 --- a/src/array_data.h +++ b/src/array_data.h @@ -58,43 +58,33 @@ public: explicit ArrayData (int size); ArrayData (uint8_t const * data, int size); - /** Create an ArrayData by copying a shared_array<> - * @param data shared_array<> to copy (the shared_array<> is copied, not the data) - * @param size Size of data in bytes - */ - ArrayData (boost::shared_array<uint8_t> data, int size); - /** Create an ArrayData by reading the contents of a file * @param file Filename to read */ - explicit ArrayData (boost::filesystem::path file); - - virtual ~ArrayData () {} + explicit ArrayData(boost::filesystem::path file); uint8_t const * data () const override { - return _data.get(); + return _data->data(); } uint8_t * data () override { - return _data.get(); + return _data->data(); } /** @return size of the data in _data, or whatever was last * passed to a set_size() call */ int size () const override { - return _size; + return _data->size(); } /** Set the size that will be returned from size() */ void set_size (int s) { - _size = s; + _data->resize(s); } private: - boost::shared_array<uint8_t> _data; - /** amount of `valid' data in _data; the array may be larger */ - int _size = 0; + std::shared_ptr<std::vector<uint8_t>> _data; }; diff --git a/src/j2k_transcode.cc b/src/j2k_transcode.cc index 686bc040..664d18a3 100644 --- a/src/j2k_transcode.cc +++ b/src/j2k_transcode.cc @@ -188,23 +188,14 @@ dcp::decompress_j2k (uint8_t const * data, int64_t size, int reduce) class WriteBuffer { public: -/* XXX: is there a better strategy for this? */ -#define MAX_J2K_SIZE (1024 * 1024 * 2) - WriteBuffer () - : _data (shared_array<uint8_t>(new uint8_t[MAX_J2K_SIZE]), MAX_J2K_SIZE) - , _offset (0) - { - _data.set_size (0); - } - OPJ_SIZE_T write (void* buffer, OPJ_SIZE_T nb_bytes) { - DCP_ASSERT ((_offset + nb_bytes) < MAX_J2K_SIZE); - memcpy (_data.data() + _offset, buffer, nb_bytes); - _offset += nb_bytes; - if (_offset > OPJ_SIZE_T(_data.size())) { - _data.set_size (_offset); + auto const new_offset = _offset + nb_bytes; + if (new_offset > OPJ_SIZE_T(_data.size())) { + _data.set_size(new_offset); } + memcpy(_data.data() + _offset, buffer, nb_bytes); + _offset = new_offset; return nb_bytes; } @@ -221,7 +212,7 @@ public: private: ArrayData _data; - OPJ_SIZE_T _offset; + OPJ_SIZE_T _offset = 0; }; diff --git a/src/smpte_subtitle_asset.cc b/src/smpte_subtitle_asset.cc index 4b2ae8cd..6db90b2e 100644 --- a/src/smpte_subtitle_asset.cc +++ b/src/smpte_subtitle_asset.cc @@ -238,9 +238,6 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader> char id[64]; Kumu::bin2UUIDhex (i->ResourceID, ASDCP::UUIDlen, id, sizeof(id)); - shared_array<uint8_t> data (new uint8_t[buffer.Size()]); - memcpy (data.get(), buffer.RoData(), buffer.Size()); - switch (i->Type) { case ASDCP::TimedText::MT_OPENTYPE: { @@ -250,7 +247,7 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader> } if (j != _load_font_nodes.end ()) { - _fonts.push_back (Font ((*j)->id, (*j)->urn, ArrayData (data, buffer.Size ()))); + _fonts.push_back(Font((*j)->id, (*j)->urn, ArrayData(buffer.RoData(), buffer.Size()))); } break; } @@ -262,7 +259,7 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader> } if (j != _subtitles.end()) { - dynamic_pointer_cast<SubtitleImage>(*j)->set_png_image (ArrayData(data, buffer.Size())); + dynamic_pointer_cast<SubtitleImage>(*j)->set_png_image(ArrayData(buffer.RoData(), buffer.Size())); } break; } |
