diff options
| author | Carl Hetherington <cth@carlh.net> | 2020-10-31 22:44:28 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2020-11-02 00:12:56 +0100 |
| commit | 9e613ae8a3cd7994194d2d709f6ff9b88feac70b (patch) | |
| tree | 40611aafec40f836ca23ef9b99093d526f9d43c1 | |
| parent | da15bff6e278d351301c9c50a4c96737ae4b5545 (diff) | |
Add Data class and change API to a raw pointer.
| -rw-r--r-- | benchmark/j2k_transcode.cc | 2 | ||||
| -rw-r--r-- | src/array_data.cc | 28 | ||||
| -rw-r--r-- | src/array_data.h | 16 | ||||
| -rw-r--r-- | src/data.cc | 74 | ||||
| -rw-r--r-- | src/data.h | 65 | ||||
| -rw-r--r-- | src/j2k.cc | 4 | ||||
| -rw-r--r-- | src/smpte_subtitle_asset.cc | 5 | ||||
| -rw-r--r-- | src/util.cc | 2 | ||||
| -rw-r--r-- | src/wscript | 2 | ||||
| -rw-r--r-- | test/dcp_font_test.cc | 4 | ||||
| -rw-r--r-- | test/frame_info_hash_test.cc | 2 | ||||
| -rw-r--r-- | test/make_digest_test.cc | 2 | ||||
| -rw-r--r-- | test/test.cc | 2 | ||||
| -rw-r--r-- | test/verify_test.cc | 10 | ||||
| -rw-r--r-- | tools/dcpdumpsub.cc | 2 |
15 files changed, 168 insertions, 52 deletions
diff --git a/benchmark/j2k_transcode.cc b/benchmark/j2k_transcode.cc index 7bdf357d..cd1bd52a 100644 --- a/benchmark/j2k_transcode.cc +++ b/benchmark/j2k_transcode.cc @@ -110,6 +110,6 @@ main (int argc, char* argv[]) cout << "Compress: " << count / compress.get() << " fps.\n"; FILE* f = fopen ("check.j2c", "wb"); - fwrite (recomp.data().get(), 1, recomp.size(), f); + fwrite (recomp.data(), 1, recomp.size(), f); fclose (f); } diff --git a/src/array_data.cc b/src/array_data.cc index 9d8473b8..a6f73868 100644 --- a/src/array_data.cc +++ b/src/array_data.cc @@ -85,31 +85,3 @@ ArrayData::ArrayData (boost::filesystem::path file) fclose (f); } - -void -ArrayData::write (boost::filesystem::path file) const -{ - FILE* f = fopen_boost (file, "wb"); - if (!f) { - throw FileError ("could not write to file", file, errno); - } - size_t const r = fwrite (_data.get(), 1, _size, f); - if (r != size_t (_size)) { - fclose (f); - throw FileError ("could not write to file", file, errno); - } - fclose (f); -} - -void -ArrayData::write_via_temp (boost::filesystem::path temp, boost::filesystem::path final) const -{ - write (temp); - boost::filesystem::rename (temp, final); -} - -bool -dcp::operator== (ArrayData const & a, ArrayData const & b) -{ - return (a.size() == b.size() && memcmp (a.data().get(), b.data().get(), a.size()) == 0); -} diff --git a/src/array_data.h b/src/array_data.h index 06ccec27..09cfd09d 100644 --- a/src/array_data.h +++ b/src/array_data.h @@ -34,13 +34,16 @@ #ifndef LIBDCP_ARRAY_DATA_H #define LIBDCP_ARRAY_DATA_H + +#include "data.h" #include <boost/shared_array.hpp> #include <boost/filesystem.hpp> #include <stdint.h> namespace dcp { -class ArrayData + +class ArrayData : public Data { public: ArrayData (); @@ -51,11 +54,12 @@ public: virtual ~ArrayData () {} - void write (boost::filesystem::path file) const; - void write_via_temp (boost::filesystem::path temp, boost::filesystem::path final) const; + uint8_t const * data () const { + return _data.get(); + } - boost::shared_array<uint8_t> data () const { - return _data; + uint8_t * data () { + return _data.get(); } int size () const { @@ -72,8 +76,6 @@ private: int _size; }; -bool operator==(ArrayData const & a, ArrayData const & b); - } #endif diff --git a/src/data.cc b/src/data.cc new file mode 100644 index 00000000..ab6ae698 --- /dev/null +++ b/src/data.cc @@ -0,0 +1,74 @@ +/* + Copyright (C) 2015-2020 Carl Hetherington <cth@carlh.net> + + This file is part of libdcp. + + libdcp 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. + + libdcp 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 libdcp. If not, see <http://www.gnu.org/licenses/>. + + In addition, as a special exception, the copyright holders give + permission to link the code of portions of this program with the + OpenSSL library under certain conditions as described in each + individual source file, and distribute linked combinations + including the two. + + You must obey the GNU General Public License in all respects + for all of the code used other than OpenSSL. If you modify + file(s) with this exception, you may extend this exception to your + version of the file(s), but you are not obligated to do so. If you + do not wish to do so, delete this exception statement from your + version. If you delete this exception statement from all source + files in the program, then also delete it here. +*/ + + +#include "data.h" +#include "util.h" +#include "exceptions.h" +#include <cstdio> +#include <cerrno> + + +using namespace dcp; + + +void +Data::write (boost::filesystem::path file) const +{ + FILE* f = fopen_boost (file, "wb"); + if (!f) { + throw FileError ("could not write to file", file, errno); + } + size_t const r = fwrite (data(), 1, size(), f); + if (r != size_t(size())) { + fclose (f); + throw FileError ("could not write to file", file, errno); + } + fclose (f); +} + + +void +Data::write_via_temp (boost::filesystem::path temp, boost::filesystem::path final) const +{ + write (temp); + boost::filesystem::rename (temp, final); +} + + +bool +dcp::operator== (Data const & a, Data const & b) +{ + return (a.size() == b.size() && memcmp(a.data(), b.data(), a.size()) == 0); +} + diff --git a/src/data.h b/src/data.h new file mode 100644 index 00000000..dfc49fe0 --- /dev/null +++ b/src/data.h @@ -0,0 +1,65 @@ +/* + Copyright (C) 2015-2020 Carl Hetherington <cth@carlh.net> + + This file is part of libdcp. + + libdcp 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. + + libdcp 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 libdcp. If not, see <http://www.gnu.org/licenses/>. + + In addition, as a special exception, the copyright holders give + permission to link the code of portions of this program with the + OpenSSL library under certain conditions as described in each + individual source file, and distribute linked combinations + including the two. + + You must obey the GNU General Public License in all respects + for all of the code used other than OpenSSL. If you modify + file(s) with this exception, you may extend this exception to your + version of the file(s), but you are not obligated to do so. If you + do not wish to do so, delete this exception statement from your + version. If you delete this exception statement from all source + files in the program, then also delete it here. +*/ + + +#ifndef LIBDCP_DATA_H +#define LIBDCP_DATA_H + + +#include <boost/filesystem.hpp> +#include <stdint.h> + + +namespace dcp { + + +class Data +{ +public: + virtual ~Data () {} + + void write (boost::filesystem::path file) const; + void write_via_temp (boost::filesystem::path temp, boost::filesystem::path final) const; + + virtual uint8_t const * data () const = 0; + virtual uint8_t * data () = 0; + virtual int size () const = 0; +}; + + +bool operator==(Data const & a, Data const & b); + + +} + +#endif @@ -52,7 +52,7 @@ using namespace dcp; shared_ptr<dcp::OpenJPEGImage> dcp::decompress_j2k (ArrayData data, int reduce) { - return dcp::decompress_j2k (data.data().get(), data.size(), reduce); + return dcp::decompress_j2k (data.data(), data.size(), reduce); } #ifdef LIBDCP_OPENJPEG2 @@ -229,7 +229,7 @@ public: OPJ_SIZE_T write (void* buffer, OPJ_SIZE_T nb_bytes) { DCP_ASSERT ((_offset + nb_bytes) < MAX_J2K_SIZE); - memcpy (_data.data().get() + _offset, buffer, nb_bytes); + memcpy (_data.data() + _offset, buffer, nb_bytes); _offset += nb_bytes; if (_offset > OPJ_SIZE_T (_data.size())) { _data.set_size (_offset); diff --git a/src/smpte_subtitle_asset.cc b/src/smpte_subtitle_asset.cc index c9545052..224ae8ba 100644 --- a/src/smpte_subtitle_asset.cc +++ b/src/smpte_subtitle_asset.cc @@ -418,7 +418,8 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const } if (j != _fonts.end ()) { ASDCP::TimedText::FrameBuffer buffer; - buffer.SetData (j->data.data().get(), j->data.size()); + ArrayData data_copy(j->data); + buffer.SetData (data_copy.data(), data_copy.size()); buffer.Size (j->data.size()); r = writer.WriteAncillaryResource (buffer, enc.context(), enc.hmac()); if (ASDCP_FAILURE (r)) { @@ -433,7 +434,7 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const shared_ptr<SubtitleImage> si = dynamic_pointer_cast<SubtitleImage>(i); if (si) { ASDCP::TimedText::FrameBuffer buffer; - buffer.SetData (si->png_image().data().get(), si->png_image().size()); + buffer.SetData (si->png_image().data(), si->png_image().size()); buffer.Size (si->png_image().size()); r = writer.WriteAncillaryResource (buffer, enc.context(), enc.hmac()); if (ASDCP_FAILURE(r)) { diff --git a/src/util.cc b/src/util.cc index b62def07..56b44889 100644 --- a/src/util.cc +++ b/src/util.cc @@ -102,7 +102,7 @@ dcp::make_digest (ArrayData data) { SHA_CTX sha; SHA1_Init (&sha); - SHA1_Update (&sha, data.data().get(), data.size()); + SHA1_Update (&sha, data.data(), data.size()); byte_t byte_buffer[SHA_DIGEST_LENGTH]; SHA1_Final (byte_buffer, &sha); char digest[64]; diff --git a/src/wscript b/src/wscript index 85d4a6cb..1222dbde 100644 --- a/src/wscript +++ b/src/wscript @@ -47,6 +47,7 @@ def build(bld): colour_conversion.cc combine.cc cpl.cc + data.cc dcp.cc dcp_time.cc decrypted_kdm.cc @@ -129,6 +130,7 @@ def build(bld): compose.hpp cpl.h crypto_context.h + data.h dcp.h dcp_assert.h dcp_time.h diff --git a/test/dcp_font_test.cc b/test/dcp_font_test.cc index ab7a707d..6a3941f9 100644 --- a/test/dcp_font_test.cc +++ b/test/dcp_font_test.cc @@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE (interop_dcp_font_test) fread (ref.get(), 1, size, f); fclose (f); - BOOST_CHECK_EQUAL (memcmp (subs2->_fonts.front().data.data().get(), ref.get(), size), 0); + BOOST_CHECK_EQUAL (memcmp (subs2->_fonts.front().data.data(), ref.get(), size), 0); } /** Create a DCP with SMPTE subtitles and check that the font is written and read back correctly */ @@ -121,5 +121,5 @@ BOOST_AUTO_TEST_CASE (smpte_dcp_font_test) fclose (f); BOOST_REQUIRE (subs2->_fonts.front().data.data()); - BOOST_CHECK_EQUAL (memcmp (subs2->_fonts.front().data.data().get(), ref.get(), size), 0); + BOOST_CHECK_EQUAL (memcmp (subs2->_fonts.front().data.data(), ref.get(), size), 0); } diff --git a/test/frame_info_hash_test.cc b/test/frame_info_hash_test.cc index 3070ecd6..2d08669e 100644 --- a/test/frame_info_hash_test.cc +++ b/test/frame_info_hash_test.cc @@ -52,7 +52,7 @@ check (unsigned int* seed, shared_ptr<dcp::PictureAssetWriter> writer, string ha dcp::ArrayData data = dcp::compress_j2k (xyz, 100000000, 24, false, false); - dcp::FrameInfo info = writer->write (data.data().get(), data.size()); + dcp::FrameInfo info = writer->write (data.data(), data.size()); BOOST_CHECK_EQUAL (info.hash, hash); } diff --git a/test/make_digest_test.cc b/test/make_digest_test.cc index 11c410d0..7fefd809 100644 --- a/test/make_digest_test.cc +++ b/test/make_digest_test.cc @@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE (make_digest_test) srand (1); int const N = 256 * 1024 * 1024; dcp::ArrayData data (N); - uint8_t* p = data.data().get(); + uint8_t* p = data.data(); for (int i = 0; i < N; ++i) { *p++ = rand() & 0xff; } diff --git a/test/test.cc b/test/test.cc index 245dd91c..bace2326 100644 --- a/test/test.cc +++ b/test/test.cc @@ -429,7 +429,7 @@ black_picture_asset (boost::filesystem::path dir, int frames) boost::filesystem::create_directories (dir); shared_ptr<dcp::PictureAssetWriter> writer = asset->start_write (dir / "pic.mxf", true); for (int i = 0; i < frames; ++i) { - writer->write (frame.data().get(), frame.size()); + writer->write (frame.data(), frame.size()); } writer->finalize (); diff --git a/test/verify_test.cc b/test/verify_test.cc index 3e2a665f..9b9111d2 100644 --- a/test/verify_test.cc +++ b/test/verify_test.cc @@ -515,7 +515,7 @@ dcp_from_frame (dcp::ArrayData const& frame, boost::filesystem::path dir) boost::filesystem::create_directories (dir); shared_ptr<dcp::PictureAssetWriter> writer = asset->start_write (dir / "pic.mxf", true); for (int i = 0; i < 24; ++i) { - writer->write (frame.data().get(), frame.size()); + writer->write (frame.data(), frame.size()); } writer->finalize (); @@ -542,8 +542,8 @@ BOOST_AUTO_TEST_CASE (verify_test15) /* Place it in a bigger block with some zero padding at the end */ dcp::ArrayData oversized_frame(too_big); - memcpy (oversized_frame.data().get(), frame.data().get(), frame.size()); - memset (oversized_frame.data().get() + frame.size(), 0, too_big - frame.size()); + memcpy (oversized_frame.data(), frame.data(), frame.size()); + memset (oversized_frame.data() + frame.size(), 0, too_big - frame.size()); boost::filesystem::path const dir("build/test/verify_test15"); boost::filesystem::remove_all (dir); @@ -569,8 +569,8 @@ BOOST_AUTO_TEST_CASE (verify_test16) /* Place it in a bigger block with some zero padding at the end */ dcp::ArrayData oversized_frame(nearly_too_big); - memcpy (oversized_frame.data().get(), frame.data().get(), frame.size()); - memset (oversized_frame.data().get() + frame.size(), 0, nearly_too_big - frame.size()); + memcpy (oversized_frame.data(), frame.data(), frame.size()); + memset (oversized_frame.data() + frame.size(), 0, nearly_too_big - frame.size()); boost::filesystem::path const dir("build/test/verify_test16"); boost::filesystem::remove_all (dir); diff --git a/tools/dcpdumpsub.cc b/tools/dcpdumpsub.cc index 026b1a4d..5d6668af 100644 --- a/tools/dcpdumpsub.cc +++ b/tools/dcpdumpsub.cc @@ -137,7 +137,7 @@ main (int argc, char* argv[]) cerr << "Could not open font file " << i->first << ".ttf for writing"; exit (EXIT_FAILURE); } - fwrite (i->second.data().get(), 1, i->second.size(), f); + fwrite (i->second.data(), 1, i->second.size(), f); fclose (f); } } |
