From d15887f68fd900ec42b1566692d2cd8d21dcde80 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Sun, 9 Oct 2005 09:30:24 +0000 Subject: [PATCH] Merged the new memory_ptr_c and memory_c classes into a single class for a refcounted memory block. Removed the unsigned char * members of packet_t that were only holding copies of the memory_c members. The data is now stored directly in packet_t.data and packet_t.data_adds. --- src/common/common_memory.h | 100 ++++++++++++++++++++--------- src/common/compression.cpp | 120 ++++++++++++----------------------- src/common/compression.h | 26 ++++---- src/common/mpeg4_common.cpp | 4 +- src/common/smart_pointers.h | 67 ------------------- src/extract/xtr_textsubs.cpp | 27 +++----- src/extract/xtr_vobsub.cpp | 38 +++++------ src/extract/xtr_vobsub.h | 3 +- src/input/r_matroska.cpp | 60 ++++++++---------- src/input/r_real.cpp | 103 +++++++++++++----------------- src/input/r_real.h | 27 ++++---- src/input/r_wavpack.cpp | 5 +- src/merge/cluster_helper.cpp | 18 +++--- src/merge/packet.h | 23 ++++--- src/merge/pr_generic.cpp | 41 ++++-------- src/output/p_aac.cpp | 4 +- src/output/p_ac3.cpp | 2 +- src/output/p_dts.cpp | 2 +- src/output/p_flac.cpp | 5 +- src/output/p_mp3.cpp | 2 +- src/output/p_passthrough.cpp | 4 +- src/output/p_pcm.cpp | 14 ++-- src/output/p_textsubs.cpp | 10 +-- src/output/p_video.cpp | 36 +++++------ src/output/p_vobbtn.cpp | 4 +- src/output/p_vorbis.cpp | 4 +- src/output/p_wavpack.cpp | 2 +- 27 files changed, 315 insertions(+), 436 deletions(-) diff --git a/src/common/common_memory.h b/src/common/common_memory.h index effd73f5a..889e63daf 100644 --- a/src/common/common_memory.h +++ b/src/common/common_memory.h @@ -23,57 +23,95 @@ #include "common.h" #include "smart_pointers.h" +class MTX_DLL_API memory_c; +typedef counted_ptr memory_cptr; +typedef std::vector memories_c; + class MTX_DLL_API memory_c { public: - unsigned char *data; - uint32_t size; - bool is_free; + typedef unsigned char X; -public: - memory_c(unsigned char *ndata, uint32_t nsize, bool nis_free): - data(ndata), size(nsize), is_free(nis_free) { - if (data == NULL) - die("memory_c::memory_c: data = %p, size = %u\n", data, size); - } - - memory_c(const memory_c &src) { - die("memory_c::memory_c(const memory_c &) called\n"); + explicit memory_c(X *p = NULL, int s = 0, bool f = false): // allocate a new counter + its_counter(NULL) { + if (p) + its_counter = new counter(p, s, f); } ~memory_c() { release(); } - int lock() { - is_free = false; - return 0; + memory_c(const memory_c &r) throw() { + acquire(r.its_counter); } - unsigned char *grab() { - if (is_free) { - is_free = false; - return data; + memory_c &operator=(const memory_c &r) { + if (this != &r) { + release(); + acquire(r.its_counter); } - return (unsigned char *)safememdup(data, size); + return *this; } - int release() { - if (is_free) { - safefree(data); - data = NULL; - is_free = false; - } - return 0; + X *get() const throw() { + return its_counter ? its_counter->ptr : NULL; + } + + int get_size() const throw() { + return its_counter ? its_counter->size : 0; + } + + bool unique() const throw() { + return (its_counter ? its_counter->count == 1 : true); } memory_c *clone() const { - return new memory_c((unsigned char *)safememdup(data, size), size, true); + return new memory_c((unsigned char *)safememdup(get(), get_size()), + get_size(), true); + } + + void grab() { + if (!its_counter || its_counter->is_free) + return; + + its_counter->ptr = (unsigned char *)safememdup(get(), get_size()); + its_counter->is_free = true; + } + + void lock() { + if (its_counter) + its_counter->is_free = false; + } + +private: + struct counter { + counter(X *p = NULL, int s = 0, bool f = false, unsigned c = 1): + ptr(p), size(s), is_free(f), count(c) {} + + X *ptr; + int size; + bool is_free; + unsigned count; + } *its_counter; + + void acquire(counter *c) throw() { // increment the count + its_counter = c; + if (c) + ++c->count; + } + + void release() { // decrement the count, delete if it is 0 + if (its_counter) { + if (--its_counter->count == 0) { + if (its_counter->is_free) + free(its_counter->ptr); + delete its_counter; + } + its_counter = 0; + } } }; -typedef counted_ptr memory_cptr; -typedef std::vector memories_c; - struct buffer_t { unsigned char *m_buffer; int m_size; diff --git a/src/common/compression.cpp b/src/common/compression.cpp index d7388c6c6..26692cc5e 100644 --- a/src/common/compression.cpp +++ b/src/common/compression.cpp @@ -45,25 +45,22 @@ lzo_compressor_c::~lzo_compressor_c() { safefree(wrkmem); } -unsigned char * -lzo_compressor_c::decompress(unsigned char *buffer, - int &size) { +void +lzo_compressor_c::decompress(memory_cptr &buffer) { die("lzo_compressor_c::decompress() not implemented\n"); - - return NULL; } -unsigned char * -lzo_compressor_c::compress(unsigned char *buffer, - int &size) { +void +lzo_compressor_c::compress(memory_cptr &buffer) { unsigned char *dst; - int result, dstsize; + int result, dstsize, size; + size = buffer->get_size(); dst = (unsigned char *)safemalloc(size * 2); lzo_uint lzo_dstsize = size * 2; - if ((result = lzo1x_999_compress(buffer, size, dst, &lzo_dstsize, - wrkmem)) != LZO_E_OK) + if ((result = lzo1x_999_compress(buffer->get(), buffer->get_size(), + dst, &lzo_dstsize, wrkmem)) != LZO_E_OK) mxerror("LZO compression failed. Result: %d\n", result); dstsize = lzo_dstsize; @@ -75,9 +72,7 @@ lzo_compressor_c::compress(unsigned char *buffer, items++; dst = (unsigned char *)saferealloc(dst, dstsize); - size = dstsize; - - return dst; + buffer = memory_cptr(new memory_c(dst, dstsize, true)); } #endif // HAVE_LZO1X_H @@ -90,9 +85,8 @@ zlib_compressor_c::zlib_compressor_c(): zlib_compressor_c::~zlib_compressor_c() { } -unsigned char * -zlib_compressor_c::decompress(unsigned char *buffer, - int &size) { +void +zlib_compressor_c::decompress(memory_cptr &buffer) { int result, dstsize, n; unsigned char *dst; z_stream d_stream; @@ -104,8 +98,8 @@ zlib_compressor_c::decompress(unsigned char *buffer, if (result != Z_OK) mxerror("inflateInit() failed. Result: %d\n", result); - d_stream.next_in = (Bytef *)buffer; - d_stream.avail_in = size; + d_stream.next_in = (Bytef *)buffer->get(); + d_stream.avail_in = buffer->get_size(); n = 0; dst = NULL; do { @@ -123,17 +117,14 @@ zlib_compressor_c::decompress(unsigned char *buffer, inflateEnd(&d_stream); mxverb(3, "zlib_compressor_c: Decompression from %d to %d, %d%%\n", - size, dstsize, dstsize * 100 / size); + buffer->get_size(), dstsize, dstsize * 100 / buffer->get_size()); dst = (unsigned char *)saferealloc(dst, dstsize); - size = dstsize; - - return dst; + buffer = memory_cptr(new memory_c(dst, dstsize, true)); } -unsigned char * -zlib_compressor_c::compress(unsigned char *buffer, - int &size) { +void +zlib_compressor_c::compress(memory_cptr &buffer) { int result, dstsize, n; unsigned char *dst; z_stream c_stream; @@ -145,8 +136,8 @@ zlib_compressor_c::compress(unsigned char *buffer, if (result != Z_OK) mxerror("deflateInit() failed. Result: %d\n", result); - c_stream.next_in = (Bytef *)buffer; - c_stream.avail_in = size; + c_stream.next_in = (Bytef *)buffer->get(); + c_stream.avail_in = buffer->get_size(); n = 0; dst = NULL; do { @@ -162,12 +153,10 @@ zlib_compressor_c::compress(unsigned char *buffer, deflateEnd(&c_stream); mxverb(3, "zlib_compressor_c: Compression from %d to %d, %d%%\n", - size, dstsize, dstsize * 100 / size); + buffer->get_size(), dstsize, dstsize * 100 / buffer->get_size()); dst = (unsigned char *)saferealloc(dst, dstsize); - size = dstsize; - - return dst; + buffer = memory_cptr(new memory_c(dst, dstsize, true)); } #endif // HAVE_ZLIB_H @@ -180,9 +169,8 @@ bzlib_compressor_c::bzlib_compressor_c(): bzlib_compressor_c::~bzlib_compressor_c() { } -unsigned char * -bzlib_compressor_c::decompress(unsigned char *buffer, - int &size) { +void +bzlib_compressor_c::decompress(memory_cptr &buffer) { int result; bz_stream d_stream; @@ -196,17 +184,15 @@ bzlib_compressor_c::decompress(unsigned char *buffer, if (result != BZ_OK) mxerror("BZ2_bzCompressInit() failed. Result: %d\n", result); BZ2_bzDecompressEnd(&d_stream); - - return NULL; } -unsigned char * -bzlib_compressor_c::compress(unsigned char *buffer, - int &size) { +void +bzlib_compressor_c::compress(memory_cptr &buffer) { unsigned char *dst; - int result, dstsize; + int result, dstsize, size; bz_stream c_stream; + size = buffer->get_size(); dst = (unsigned char *)safemalloc(size * 2); c_stream.bzalloc = NULL; @@ -217,7 +203,7 @@ bzlib_compressor_c::compress(unsigned char *buffer, if (result != BZ_OK) mxerror("BZ2_bzCompressInit() failed. Result: %d\n", result); - c_stream.next_in = (char *)buffer; + c_stream.next_in = (char *)buffer->get(); c_stream.next_out = (char *)dst; c_stream.avail_in = size; c_stream.avail_out = 2 * size; @@ -225,6 +211,8 @@ bzlib_compressor_c::compress(unsigned char *buffer, if (result != BZ_RUN_OK) mxerror("bzip2 compression failed. Result: %d\n", result); + BZ2_bzCompressEnd(&c_stream); + dstsize = 2 * size - c_stream.avail_out; mxverb(3, "bzlib_compressor_c: Compression from %d to %d, %d%%\n", @@ -235,11 +223,7 @@ bzlib_compressor_c::compress(unsigned char *buffer, items++; dst = (unsigned char *)saferealloc(dst, dstsize); - size = dstsize; - - BZ2_bzCompressEnd(&c_stream); - - return dst; + buffer = memory_cptr(new memory_c(dst, dstsize, true)); } #endif // HAVE_BZLIB_H @@ -304,9 +288,9 @@ content_decoder_c::~content_decoder_c() { } #define EBMLBIN_TO_COUNTEDMEM(bin) \ - counted_mem_ptr((unsigned char *)safememdup(bin->GetBuffer(), \ - bin->GetSize()), \ - bin->GetSize()) + memory_cptr(new memory_c((unsigned char *)safememdup(bin->GetBuffer(), \ + bin->GetSize()), \ + bin->GetSize(), true)) bool content_decoder_c::initialize(KaxTrackEntry &ktentry) { @@ -458,37 +442,15 @@ content_decoder_c::initialize(KaxTrackEntry &ktentry) { return ok; } -bool -content_decoder_c::reverse(unsigned char *&data, - uint32_t &size, +void +content_decoder_c::reverse(memory_cptr &memory, content_encoding_scope_e scope) { - int new_size; - unsigned char *new_data, *old_data; - bool modified; vector::const_iterator ce; - if (!ok) - return false; + if (!is_ok() || encodings.empty()) + return; - if (0 == encodings.size()) - return false; - - new_data = data; - new_size = size; - modified = false; - foreach(ce, encodings) { - if (0 == (ce->scope & scope)) - continue; - - old_data = new_data; - new_data = ce->compressor->decompress(old_data, new_size); - if (modified) - safefree(old_data); - modified = true; - } - - data = new_data; - size = new_size; - - return modified; + foreach(ce, encodings) + if (0 != (ce->scope & scope)) + ce->compressor->decompress(memory); } diff --git a/src/common/compression.h b/src/common/compression.h index 2aca7ad45..d7b9536db 100644 --- a/src/common/compression.h +++ b/src/common/compression.h @@ -23,6 +23,7 @@ #include #include "common.h" +#include "common_memory.h" #include "smart_pointers.h" using namespace libmatroska; @@ -58,12 +59,10 @@ public: return method; }; - virtual unsigned char *decompress(unsigned char *buffer, int &size) { - return (unsigned char *)safememdup(buffer, size); + virtual void decompress(memory_cptr &buffer) { }; - virtual unsigned char *compress(unsigned char *buffer, int &size) { - return (unsigned char *)safememdup(buffer, size); + virtual void compress(memory_cptr &buffer) { }; static compressor_ptr create(compression_method_e method); @@ -81,8 +80,8 @@ public: lzo_compressor_c(); virtual ~lzo_compressor_c(); - virtual unsigned char *decompress(unsigned char *buffer, int &size); - virtual unsigned char *compress(unsigned char *buffer, int &size); + virtual void decompress(memory_cptr &buffer); + virtual void compress(memory_cptr &buffer); }; #endif // HAVE_LZO1X_H @@ -94,8 +93,8 @@ public: zlib_compressor_c(); virtual ~zlib_compressor_c(); - virtual unsigned char *decompress(unsigned char *buffer, int &size); - virtual unsigned char *compress(unsigned char *buffer, int &size); + virtual void decompress(memory_cptr &buffer); + virtual void compress(memory_cptr &buffer); }; #endif // HAVE_ZLIB_H @@ -107,8 +106,8 @@ public: bzlib_compressor_c(); virtual ~bzlib_compressor_c(); - virtual unsigned char *decompress(unsigned char *buffer, int &size); - virtual unsigned char *compress(unsigned char *buffer, int &size); + virtual void decompress(memory_cptr &buffer); + virtual void compress(memory_cptr &buffer); }; #endif // HAVE_BZLIB_H @@ -120,9 +119,9 @@ enum content_encoding_scope_e { struct kax_content_encoding_t { uint32_t order, type, scope; uint32_t comp_algo; - counted_mem_ptr comp_settings; + memory_cptr comp_settings; uint32_t enc_algo, sig_algo, sig_hash_algo; - counted_mem_ptr enc_keyid, sig_keyid, signature; + memory_cptr enc_keyid, sig_keyid, signature; counted_ptr compressor; @@ -140,8 +139,7 @@ public: ~content_decoder_c(); bool initialize(KaxTrackEntry &ktentry); - bool reverse(unsigned char *&data, uint32_t &size, - content_encoding_scope_e scope); + void reverse(memory_cptr &data, content_encoding_scope_e scope); bool is_ok() { return ok; } diff --git a/src/common/mpeg4_common.cpp b/src/common/mpeg4_common.cpp index e32fedda8..3b7d7939d 100644 --- a/src/common/mpeg4_common.cpp +++ b/src/common/mpeg4_common.cpp @@ -364,14 +364,14 @@ mpeg4_p2_parse_config_data(const unsigned char *buffer, if (-1 == vos_offset) { mem = new memory_c((unsigned char *)safemalloc(size + 5), size + 5, true); - dst = mem->data; + dst = mem->get(); put_uint32_be(dst, MPEGVIDEO_VOS_START_CODE); dst[4] = 0xf5; memcpy(dst + 5, buffer, size); } else { mem = new memory_c((unsigned char *)safemalloc(size), size, true); - dst = mem->data; + dst = mem->get(); put_uint32_be(dst, MPEGVIDEO_VOS_START_CODE); if (3 >= buffer[vos_offset + 4]) dst[4] = 0xf5; diff --git a/src/common/smart_pointers.h b/src/common/smart_pointers.h index 4ccafe0b3..56ee34dc8 100644 --- a/src/common/smart_pointers.h +++ b/src/common/smart_pointers.h @@ -78,73 +78,6 @@ private: // ----------------------------------------------------------------- -class counted_mem_ptr { -public: - typedef unsigned char X; - - explicit counted_mem_ptr(X *p = NULL, int s = 0): // allocate a new counter - its_counter(NULL) { - if (p) - its_counter = new counter(p, s); - } - - ~counted_mem_ptr() { - release(); - } - - counted_mem_ptr(const counted_mem_ptr &r) throw() { - acquire(r.its_counter); - } - - counted_mem_ptr &operator=(const counted_mem_ptr &r) { - if (this != &r) { - release(); - acquire(r.its_counter); - } - return *this; - } - - X *get() const throw() { - return its_counter ? its_counter->ptr : NULL; - } - - int get_size() const throw() { - return its_counter ? its_counter->size : 0; - } - - bool unique() const throw() { - return (its_counter ? its_counter->count == 1 : true); - } - -private: - struct counter { - counter(X *p = NULL, int s = 0, unsigned c = 1): - ptr(p), size(s), count(c) {} - - X *ptr; - int size; - unsigned count; - } *its_counter; - - void acquire(counter *c) throw() { // increment the count - its_counter = c; - if (c) - ++c->count; - } - - void release() { // decrement the count, delete if it is 0 - if (its_counter) { - if (--its_counter->count == 0) { - free(its_counter->ptr); - delete its_counter; - } - its_counter = 0; - } - } -}; - -// ----------------------------------------------------------------- - template class autofree_ptr { protected: X *ptr; diff --git a/src/extract/xtr_textsubs.cpp b/src/extract/xtr_textsubs.cpp index 6ffd552f1..0dec85974 100644 --- a/src/extract/xtr_textsubs.cpp +++ b/src/extract/xtr_textsubs.cpp @@ -298,8 +298,6 @@ xtr_usf_c::create_file(xtr_base_c *_master, KaxTrackEntry &track) { KaxCodecPrivate *priv; KaxTrackLanguage *language; - unsigned char *new_priv; - uint32_t private_size; priv = FINDFIRST(&track, KaxCodecPrivate); if (NULL == priv) @@ -310,16 +308,10 @@ xtr_usf_c::create_file(xtr_base_c *_master, mxerror("Tracks with unsupported content encoding schemes (compression " "or encryption) cannot be extracted.\n"); - private_size = priv->GetSize(); - - new_priv = const_cast(priv->GetBuffer()); - if (!content_decoder.reverse(new_priv, private_size, - CONTENT_ENCODING_SCOPE_CODECPRIVATE)) { - m_codec_private.append((const char *)new_priv, private_size); - } else { - m_codec_private.append((const char *)new_priv, private_size); - safefree(new_priv); - } + memory_cptr new_priv(new memory_c(priv->GetBuffer(), priv->GetSize(), + false)); + content_decoder.reverse(new_priv, CONTENT_ENCODING_SCOPE_CODECPRIVATE); + m_codec_private.append((const char *)new_priv->get(), new_priv->get_size()); language = FINDFIRST(&track, KaxTrackLanguage); if (NULL == language) @@ -387,13 +379,10 @@ xtr_usf_c::handle_block(KaxBlock &block, DataBuffer &data_buffer = block.GetBuffer(0); usf_entry_t entry("", timecode, timecode + duration); - uint32_t size = data_buffer.Size(); - unsigned char *data = data_buffer.Buffer(); - bool free_mem = content_decoder.reverse(data, size, - CONTENT_ENCODING_SCOPE_BLOCK); - entry.m_text.append((const char *)data, size); - if (free_mem) - safefree(data); + memory_cptr data(new memory_c(data_buffer.Buffer(), data_buffer.Size(), + false)); + content_decoder.reverse(data, CONTENT_ENCODING_SCOPE_BLOCK); + entry.m_text.append((const char *)data->get(), data->get_size()); m_entries.push_back(entry); } diff --git a/src/extract/xtr_vobsub.cpp b/src/extract/xtr_vobsub.cpp index 125db4f57..b7a263ada 100644 --- a/src/extract/xtr_vobsub.cpp +++ b/src/extract/xtr_vobsub.cpp @@ -50,7 +50,7 @@ xtr_vobsub_c::xtr_vobsub_c(const string &_codec_id, int64_t _tid, track_spec_t &tspec): xtr_base_c(_codec_id, _tid, tspec), - private_size(0), stream_id(0x20) { + stream_id(0x20) { int pos; @@ -64,7 +64,6 @@ void xtr_vobsub_c::create_file(xtr_base_c *_master, KaxTrackEntry &track) { KaxCodecPrivate *priv; - unsigned char *new_priv; priv = FINDFIRST(&track, KaxCodecPrivate); if (NULL == priv) @@ -75,14 +74,10 @@ xtr_vobsub_c::create_file(xtr_base_c *_master, mxerror("Tracks with unsupported content encoding schemes (compression " "or encryption) cannot be extracted.\n"); - private_size = priv->GetSize(); - - new_priv = const_cast(priv->GetBuffer()); - if (!content_decoder.reverse(new_priv, private_size, - CONTENT_ENCODING_SCOPE_CODECPRIVATE)) - private_data.set(safememdup(priv->GetBuffer(), private_size)); - else - private_data.set(new_priv); + private_data = memory_cptr(new memory_c(priv->GetBuffer(), priv->GetSize(), + false)); + content_decoder.reverse(private_data, CONTENT_ENCODING_SCOPE_CODECPRIVATE); + private_data->grab(); master = _master; language = kt_get_language(track); @@ -105,8 +100,9 @@ xtr_vobsub_c::create_file(xtr_base_c *_master, "This was requested for the tracks %lld and %lld.\n", tid, master->tid); - if ((private_size != vmaster->private_size) || - memcmp(priv->GetBuffer(), vmaster->private_data.get(), private_size)) + if ((private_data->get_size() != vmaster->private_data->get_size()) || + memcmp(priv->GetBuffer(), vmaster->private_data->get(), + private_data->get_size())) mxerror("Two VobSub tracks can only be extracted into the same file " "if their CodecPrivate data matches. This is not the case " "for the tracks %lld and %lld.\n", tid, master->tid); @@ -138,15 +134,15 @@ xtr_vobsub_c::handle_block(KaxBlock &block, vmaster = static_cast(master); for (i = 0; i < block.NumberFrames(); i++) { - autofree_ptr af_data; - unsigned char *data; uint32_t size, padding, first; DataBuffer &data_buffer = block.GetBuffer(i); + memory_cptr data_m(new memory_c(data_buffer.Buffer(), data_buffer.Size(), + false)); + unsigned char *data; - size = data_buffer.Size(); - data = data_buffer.Buffer(); - if (content_decoder.reverse(data, size, CONTENT_ENCODING_SCOPE_BLOCK)) - af_data.set(data); + content_decoder.reverse(data_m, CONTENT_ENCODING_SCOPE_BLOCK); + data = data_m->get(); + size = data_m->get_size(); positions.push_back(vmaster->out->getFilePointer()); timecodes.push_back(timecode); @@ -255,10 +251,10 @@ xtr_vobsub_c::finish_file() { mm_file_io_c idx(base_name, MODE_CREATE); mxinfo("Writing the VobSub index file '%s'.\n", base_name.c_str()); - if ((25 > private_size) || strncasecmp((char *)private_data.get(), - header_line, 25)) + if ((25 > private_data->get_size()) || + strncasecmp((char *)private_data->get(), header_line, 25)) idx.printf(header_line); - idx.write(private_data.get(), private_size); + idx.write(private_data->get(), private_data->get_size()); write_idx(idx, 0); for (slave = 0; slave < slaves.size(); slave++) diff --git a/src/extract/xtr_vobsub.h b/src/extract/xtr_vobsub.h index 704e639b3..79c4764cb 100644 --- a/src/extract/xtr_vobsub.h +++ b/src/extract/xtr_vobsub.h @@ -25,8 +25,7 @@ class xtr_vobsub_c: public xtr_base_c { public: vector positions, timecodes; vector slaves; - autofree_ptr private_data; - uint32_t private_size; + memory_cptr private_data; string base_name, language; int stream_id; diff --git a/src/input/r_matroska.cpp b/src/input/r_matroska.cpp index ddf43a4ff..10204abf7 100644 --- a/src/input/r_matroska.cpp +++ b/src/input/r_matroska.cpp @@ -245,14 +245,13 @@ kax_reader_c::verify_tracks() { t->ok = 0; if (t->private_data != NULL) { - c = (unsigned char *)t->private_data; - length = t->private_size; - if (t->content_decoder.reverse(c, length, - CONTENT_ENCODING_SCOPE_CODECPRIVATE)) { - safefree(t->private_data); - t->private_data = c; - t->private_size = length; - } + memory_cptr private_data(new memory_c((unsigned char *)t->private_data, + t->private_size, true)); + t->content_decoder.reverse(private_data, + CONTENT_ENCODING_SCOPE_CODECPRIVATE); + private_data->lock(); + t->private_data = private_data->get(); + t->private_size = private_data->get_size(); } switch (t->type) { @@ -276,8 +275,8 @@ kax_reader_c::verify_tracks() { u = get_uint32_le(&bih->bi_width); if (t->v_width != u) { if (verbose) - mxwarn(PFX "(MS compatibility " - "mode, track %llu) Matrosa says video width is %llu, but " + mxwarn(PFX "(MS compatibility mode, track %llu) Matrosa says " + "video width is %llu, but " "the BITMAPINFOHEADER says %u.\n", t->tnum, t->v_width, u); if (t->v_width == 0) @@ -1913,36 +1912,30 @@ kax_reader_c::read(generic_packetizer_c *, block_fref += (int64_t)last_timecode; for (i = 0; i < (int)block->NumberFrames(); i++) { - unsigned char *re_buffer; - uint32_t re_size; - bool re_modified; + DataBuffer &data_buffer = block->GetBuffer(i); + memory_cptr data(new memory_c(data_buffer.Buffer(), + data_buffer.Size(), false)); + block_track->content_decoder. + reverse(data, CONTENT_ENCODING_SCOPE_BLOCK); - DataBuffer &data = block->GetBuffer(i); - re_buffer = data.Buffer(); - re_size = data.Size(); - re_modified = block_track->content_decoder. - reverse(re_buffer, re_size, CONTENT_ENCODING_SCOPE_BLOCK); if ((block_track->type == 's') && (block_track->sub_type == 't')) { - if ((re_size > 2) || - ((re_size > 0) && (*re_buffer != ' ') && - (*re_buffer != 0) && !iscr(*re_buffer))) { + if ((data->get_size() > 2) || + ((data->get_size() > 0) && (*data->get() != ' ') && + (*data->get() != 0) && !iscr(*data->get()))) { char *lines; - lines = (char *)safemalloc(re_size + 1); - lines[re_size] = 0; - memcpy(lines, re_buffer, re_size); + lines = (char *)safemalloc(data->get_size() + 1); + lines[data->get_size()] = 0; + memcpy(lines, data->get(), data->get_size()); PTZR(block_track->ptzr)-> process(new packet_t(new memory_c((unsigned char *)lines, 0, true), last_timecode, block_duration, block_bref, block_fref)); - if (re_modified) - safefree(re_buffer); } } else { - memory_c *mem = new memory_c(re_buffer, re_size, re_modified); - packet_cptr packet(new packet_t(mem, last_timecode + i * + packet_cptr packet(new packet_t(data, last_timecode + i * frame_duration, block_duration, block_bref, block_fref)); @@ -1955,10 +1948,13 @@ kax_reader_c::read(generic_packetizer_c *, uint64(*static_cast (&GetChild(*blockmore))); blockadd_data = &GetChild(*blockmore); - memory_c *blockadded = - new memory_c(blockadd_data->GetBuffer(), - blockadd_data->GetSize(), false); - packet->memory_adds.push_back(memory_cptr(blockadded)); + memory_cptr + blockadded(new memory_c(blockadd_data->GetBuffer(), + blockadd_data->GetSize(), + false)); + block_track->content_decoder. + reverse(blockadded, CONTENT_ENCODING_SCOPE_BLOCK); + packet->data_adds.push_back(blockadded); } PTZR(block_track->ptzr)->process(packet); diff --git a/src/input/r_real.cpp b/src/input/r_real.cpp index 9fa91421d..6a1535eb2 100644 --- a/src/input/r_real.cpp +++ b/src/input/r_real.cpp @@ -170,19 +170,13 @@ real_reader_c::real_reader_c(track_info_c &_ti) } real_reader_c::~real_reader_c() { - real_demuxer_t *demuxer; - int i, j; + int i; for (i = 0; i < demuxers.size(); i++) { - demuxer = demuxers[i]; - if (demuxer->segments != NULL) { - for (j = 0; j < demuxer->segments->size(); j++) - safefree((*demuxer->segments)[j].data); - delete demuxer->segments; - } + real_demuxer_cptr &demuxer = demuxers[i]; + safefree(demuxer->private_data); safefree(demuxer->extra_data); - safefree(demuxer); } demuxers.clear(); ti.private_data = NULL; @@ -193,7 +187,6 @@ void real_reader_c::parse_headers() { uint32_t ts_size, ndx, i; unsigned char *ts_data; - real_demuxer_t *dmx; rmff_track_t *track; if (rmff_read_headers(file) == RMFF_ERR_OK) { @@ -216,8 +209,7 @@ real_reader_c::parse_headers() { ts_data = track->mdpr_header.type_specific_data; ts_size = get_uint32_be(&track->mdpr_header.type_specific_size); - dmx = (real_demuxer_t *)safemalloc(sizeof(real_demuxer_t)); - memset(dmx, 0, sizeof(real_demuxer_t)); + real_demuxer_cptr dmx(new real_demuxer_t); dmx->track = track; dmx->ptzr = -1; @@ -298,8 +290,7 @@ real_reader_c::parse_headers() { dmx->private_size = ts_size; demuxers.push_back(dmx); - } else - free(dmx); + } } } } @@ -308,11 +299,11 @@ real_reader_c::parse_headers() { void real_reader_c::create_packetizer(int64_t tid) { int i; - real_demuxer_t *dmx; + real_demuxer_cptr dmx; rmff_track_t *track; dmx = find_demuxer(tid); - if (dmx == NULL) + if (dmx.get() == NULL) return; if (dmx->ptzr == -1) { @@ -428,7 +419,6 @@ real_reader_c::create_packetizer(int64_t tid) { dmx->private_data, dmx->private_size, ti); dmx->ptzr = add_packetizer(ptzr); - dmx->segments = new vector; mxinfo(FMT_TID "Using the RealAudio output module (FourCC: %s).\n", ti.fname.c_str(), (int64_t)track->id, dmx->fourcc); @@ -445,7 +435,7 @@ real_reader_c::create_packetizers() { create_packetizer(demuxers[i]->track->id); } -real_demuxer_t * +real_demuxer_cptr real_reader_c::find_demuxer(int id) { int i; @@ -453,19 +443,18 @@ real_reader_c::find_demuxer(int id) { if (demuxers[i]->track->id == id) return demuxers[i]; - return NULL; + return real_demuxer_cptr(NULL); } file_status_e real_reader_c::finish() { int i; int64_t dur; - real_demuxer_t *dmx; + real_demuxer_cptr dmx; for (i = 0; i < demuxers.size(); i++) { dmx = demuxers[i]; - if ((dmx->track->type == RMFF_TRACK_TYPE_AUDIO) && - (dmx->segments != NULL)) { + if (dmx->track->type == RMFF_TRACK_TYPE_AUDIO) { dur = dmx->last_timecode / dmx->num_packets; deliver_audio_frames(dmx, dur); } @@ -483,7 +472,7 @@ real_reader_c::read(generic_packetizer_c *, bool) { int size; unsigned char *chunk; - real_demuxer_t *dmx; + real_demuxer_cptr dmx; int64_t timecode; rmff_frame_t *frame; @@ -512,7 +501,7 @@ real_reader_c::read(generic_packetizer_c *, timecode = (int64_t)frame->timecode * 1000000ll; dmx = find_demuxer(frame->id); - if (dmx == NULL) { + if (dmx.get() == NULL) { rmff_release_frame(frame); return FILE_STATUS_MOREDATA; } @@ -539,79 +528,75 @@ real_reader_c::read(generic_packetizer_c *, } void -real_reader_c::queue_one_audio_frame(real_demuxer_t *dmx, +real_reader_c::queue_one_audio_frame(real_demuxer_cptr dmx, memory_c &mem, uint64_t timecode, uint32_t flags) { - rv_segment_t segment; + rv_segment_cptr segment(new rv_segment_t); - segment.data = mem.data; - segment.size = mem.size; - mem.lock(); - segment.flags = flags; - dmx->segments->push_back(segment); + segment->data = memory_cptr(new memory_c(mem)); + segment->flags = flags; + dmx->segments.push_back(segment); dmx->last_timecode = timecode; mxverb(2, "enqueueing one for %u/'%s' length %u timecode %llu flags " - "0x%08x\n", dmx->track->id, ti.fname.c_str(), mem.size, timecode, - flags); + "0x%08x\n", dmx->track->id, ti.fname.c_str(), mem.get_size(), + timecode, flags); } void -real_reader_c::queue_audio_frames(real_demuxer_t *dmx, +real_reader_c::queue_audio_frames(real_demuxer_cptr dmx, memory_c &mem, uint64_t timecode, uint32_t flags) { // Enqueue the packets if no packets are in the queue or if the current // packet's timecode is the same as the timecode of those before. - if ((dmx->segments->size() == 0) || - (dmx->last_timecode == timecode)) { + if (dmx->segments.empty() || (dmx->last_timecode == timecode)) { queue_one_audio_frame(dmx, mem, timecode, flags); return; } // This timecode is different. So let's push the packets out. deliver_audio_frames(dmx, (timecode - dmx->last_timecode) / - dmx->segments->size()); + dmx->segments.size()); // Enqueue this packet. queue_one_audio_frame(dmx, mem, timecode, flags); } void -real_reader_c::deliver_audio_frames(real_demuxer_t *dmx, +real_reader_c::deliver_audio_frames(real_demuxer_cptr dmx, uint64_t duration) { uint32_t i; - rv_segment_t segment; + rv_segment_cptr segment; - if (dmx->segments->size() == 0) + if (dmx->segments.empty()) return; - for (i = 0; i < dmx->segments->size(); i++) { - segment = (*dmx->segments)[i]; - mxverb(2, "delivering audio for %u/'%s' length %llu timecode %llu flags " + for (i = 0; i < dmx->segments.size(); i++) { + segment = dmx->segments[i]; + mxverb(2, "delivering audio for %u/'%s' length %d timecode %llu flags " "0x%08x duration %llu\n", dmx->track->id, ti.fname.c_str(), - segment.size, dmx->last_timecode, (uint32_t)segment.flags, - duration); - PTZR(dmx->ptzr)->process(new packet_t(new memory_c(segment.data, - segment.size, true), + segment->data->get_size(), dmx->last_timecode, + (uint32_t)segment->flags, duration); + PTZR(dmx->ptzr)->process(new packet_t(segment->data, dmx->last_timecode, duration, - (segment.flags & 2) == 2 ? -1 : + (segment->flags & 2) == 2 ? -1 : dmx->ref_timecode)); - if ((segment.flags & 2) == 2) + if ((segment->flags & 2) == 2) dmx->ref_timecode = dmx->last_timecode; } - dmx->num_packets += dmx->segments->size(); - dmx->segments->clear(); + dmx->num_packets += dmx->segments.size(); + dmx->segments.clear(); } void -real_reader_c::deliver_aac_frames(real_demuxer_t *dmx, +real_reader_c::deliver_aac_frames(real_demuxer_cptr dmx, memory_c &mem) { uint32_t num_sub_packets, data_idx, i, sub_length, len_check; uint32_t length; unsigned char *chunk; - chunk = mem.data; - length = mem.size; + chunk = mem.get(); + length = mem.get_size(); if (length < 2) { mxwarn(PFX "Short AAC audio packet for track ID %u of " "'%s' (length: %u < 2)\n", dmx->track->id, ti.fname.c_str(), @@ -655,7 +640,7 @@ real_reader_c::get_progress() { void real_reader_c::identify() { int i; - real_demuxer_t *demuxer; + real_demuxer_cptr demuxer; mxinfo("File '%s': container: RealMedia\n", ti.fname.c_str()); for (i = 0; i < demuxers.size(); i++) { @@ -671,7 +656,7 @@ real_reader_c::identify() { } void -real_reader_c::assemble_video_packet(real_demuxer_t *dmx, +real_reader_c::assemble_video_packet(real_demuxer_cptr dmx, rmff_frame_t *frame) { int result; rmff_frame_t *assembled; @@ -748,7 +733,7 @@ real_reader_c::get_rv_dimensions(unsigned char *buf, } void -real_reader_c::set_dimensions(real_demuxer_t *dmx, +real_reader_c::set_dimensions(real_demuxer_cptr dmx, unsigned char *buffer, int size) { uint32_t width, height, disp_width, disp_height; @@ -815,7 +800,7 @@ real_reader_c::set_dimensions(real_demuxer_t *dmx, void real_reader_c::get_information_from_data() { int i; - real_demuxer_t *dmx; + real_demuxer_cptr dmx; bool information_found; rmff_frame_t *frame; int64_t old_pos; @@ -834,7 +819,7 @@ real_reader_c::get_information_from_data() { frame = rmff_read_next_frame(file, NULL); dmx = find_demuxer(frame->id); - if (dmx == NULL) { + if (dmx.get() == NULL) { rmff_release_frame(frame); continue; } diff --git a/src/input/r_real.h b/src/input/r_real.h index fe3e0da68..a0f36d0ce 100644 --- a/src/input/r_real.h +++ b/src/input/r_real.h @@ -29,10 +29,12 @@ #include "pr_generic.h" typedef struct { - unsigned char *data; - uint64_t size, flags; + memory_cptr data; + uint64_t flags; } rv_segment_t; +typedef counted_ptr rv_segment_cptr; + typedef struct { int ptzr; rmff_track_t *track; @@ -54,13 +56,15 @@ typedef struct { int num_packets; int64_t last_timecode, ref_timecode; - vector *segments; + vector segments; } real_demuxer_t; +typedef counted_ptr real_demuxer_cptr; + class real_reader_c: public generic_reader_c { private: rmff_file_t *file; - vector demuxers; + vector > demuxers; int64_t file_size; bool done; @@ -79,20 +83,21 @@ public: protected: virtual void parse_headers(); - virtual real_demuxer_t *find_demuxer(int id); - virtual void assemble_video_packet(real_demuxer_t *dmx, rmff_frame_t *frame); + virtual real_demuxer_cptr find_demuxer(int id); + virtual void assemble_video_packet(real_demuxer_cptr dmx, + rmff_frame_t *frame); virtual file_status_e finish(); virtual bool get_rv_dimensions(unsigned char *buf, int size, uint32_t &width, uint32_t &height); - virtual void set_dimensions(real_demuxer_t *dmx, unsigned char *buffer, + virtual void set_dimensions(real_demuxer_cptr dmx, unsigned char *buffer, int size); virtual void get_information_from_data(); - virtual void deliver_aac_frames(real_demuxer_t *dmx, memory_c &mem); - virtual void queue_audio_frames(real_demuxer_t *dmx, memory_c &mem, + virtual void deliver_aac_frames(real_demuxer_cptr dmx, memory_c &mem); + virtual void queue_audio_frames(real_demuxer_cptr dmx, memory_c &mem, uint64_t timecode, uint32_t flags); - virtual void queue_one_audio_frame(real_demuxer_t *dmx, memory_c &mem, + virtual void queue_one_audio_frame(real_demuxer_cptr dmx, memory_c &mem, uint64_t timecode, uint32_t flags); - virtual void deliver_audio_frames(real_demuxer_t *dmx, uint64_t duration); + virtual void deliver_audio_frames(real_demuxer_cptr dmx, uint64_t duration); }; #endif // __R_REAL_H diff --git a/src/input/r_wavpack.cpp b/src/input/r_wavpack.cpp index 7ca5f6cf5..b6b51c59d 100644 --- a/src/input/r_wavpack.cpp +++ b/src/input/r_wavpack.cpp @@ -235,9 +235,8 @@ wavpack_reader_c::read(generic_packetizer_c *ptzr, databuffer += block_size; } - packet->memory_adds.push_back(memory_cptr(new memory_c(chunk_correc, - data_size, - true))); + packet->data_adds.push_back(memory_cptr(new memory_c(chunk_correc, + data_size, true))); } } diff --git a/src/merge/cluster_helper.cpp b/src/merge/cluster_helper.cpp index 0d97fdb93..305c3568a 100644 --- a/src/merge/cluster_helper.cpp +++ b/src/merge/cluster_helper.cpp @@ -132,7 +132,7 @@ cluster_helper_c::add_packet(packet_cptr packet) { // Add sizes for all frames. for (i = 0; i < c->packets.size(); i++) { packet_cptr &p = c->packets[i]; - additional_size += p->length; + additional_size += p->data->get_size(); if (p->bref == -1) additional_size += 10; else if (p->fref == -1) @@ -192,7 +192,7 @@ cluster_helper_c::add_packet(packet_cptr packet) { c = clusters.back(); c->packets.push_back(packet); - cluster_content_size += packet->length; + cluster_content_size += packet->data->get_size(); walk_clusters(); @@ -398,7 +398,7 @@ cluster_helper_c::render_cluster(ch_contents_t *clstr) { (cluster)->set_min_timecode(pack->assigned_timecode - timecode_offset); max_cl_timecode = pack->assigned_timecode; - data_buffer = new DataBuffer((binary *)pack->data, pack->length); + data_buffer = new DataBuffer((binary *)pack->data->get(), pack->data->get_size()); KaxTrackEntry &track_entry = static_cast(*source->get_track_entry()); @@ -505,8 +505,8 @@ cluster_helper_c::render_cluster(ch_contents_t *clstr) { *static_cast(&GetChild(block_more)) = k + 1; GetChild(block_more). - SetBuffer((binary *)pack->data_adds[k], pack->data_adds_lengths[k]); - pack->data_adds[k] = NULL; + CopyBuffer((binary *)pack->data_adds[k]->get(), + pack->data_adds[k]->get_size()); } } @@ -558,11 +558,9 @@ cluster_helper_c::render_cluster(ch_contents_t *clstr) { } else last_cluster_tc = 0; - for (i = 0; i < clstr->packets.size(); i++) { - pack = clstr->packets[i]; - safefree(pack->data); - pack->data = NULL; - } + for (i = 0; i < clstr->packets.size(); i++) + clstr->packets[i]->data = memory_cptr(new memory_c()); + for (i = 0; i < render_groups.size(); i++) delete render_groups[i]; diff --git a/src/merge/packet.h b/src/merge/packet.h index d8fbd439e..e68d18665 100644 --- a/src/merge/packet.h +++ b/src/merge/packet.h @@ -37,24 +37,21 @@ class generic_packetizer_c; struct packet_t { static int64_t packet_number_counter; + memory_cptr data; + vector data_adds; + KaxBlockGroup *group; KaxBlock *block; KaxCluster *cluster; - unsigned char *data; - int length, ref_priority, time_factor; + int ref_priority, time_factor; int64_t timecode, bref, fref, duration, packet_num, assigned_timecode; int64_t timecode_before_factory; int64_t unmodified_assigned_timecode, unmodified_duration; bool duration_mandatory, superseeded, gap_following, factory_applied; generic_packetizer_c *source; - vector data_adds; - vector data_adds_lengths; - - memory_cptr memory; - memories_c memory_adds; packet_t(): - group(NULL), block(NULL), cluster(NULL), data(NULL), length(0), + group(NULL), block(NULL), cluster(NULL), ref_priority(0), time_factor(1), timecode(0), bref(0), fref(0), duration(0), packet_num(packet_number_counter++), @@ -68,7 +65,8 @@ struct packet_t { int64_t n_duration = -1, int64_t n_bref = -1, int64_t n_fref = -1): - group(NULL), block(NULL), cluster(NULL), data(NULL), length(0), + data(n_memory), + group(NULL), block(NULL), cluster(NULL), ref_priority(0), time_factor(1), timecode(n_timecode), bref(n_bref), fref(n_fref), duration(n_duration), @@ -76,14 +74,15 @@ struct packet_t { assigned_timecode(0), timecode_before_factory(0), unmodified_assigned_timecode(0), unmodified_duration(0), duration_mandatory(false), superseeded(false), gap_following(false), - factory_applied(false), source(NULL), memory(n_memory) { } + factory_applied(false), source(NULL) { } packet_t(memory_c *n_memory, int64_t n_timecode = -1, int64_t n_duration = -1, int64_t n_bref = -1, int64_t n_fref = -1): - group(NULL), block(NULL), cluster(NULL), data(NULL), length(0), + data(memory_cptr(n_memory)), + group(NULL), block(NULL), cluster(NULL), ref_priority(0), time_factor(1), timecode(n_timecode), bref(n_bref), fref(n_fref), duration(n_duration), @@ -91,7 +90,7 @@ struct packet_t { assigned_timecode(0), timecode_before_factory(0), unmodified_assigned_timecode(0), unmodified_duration(0), duration_mandatory(false), superseeded(false), gap_following(false), - factory_applied(false), source(NULL), memory(memory_cptr(n_memory)) { } + factory_applied(false), source(NULL) { } ~packet_t(); }; diff --git a/src/merge/pr_generic.cpp b/src/merge/pr_generic.cpp index ac4ee43a2..ea4381ead 100644 --- a/src/merge/pr_generic.cpp +++ b/src/merge/pr_generic.cpp @@ -39,11 +39,6 @@ using namespace std; int64_t packet_t::packet_number_counter = 0; packet_t::~packet_t() { - vector::iterator i; - - safefree(data); - foreach(i, data_adds) - safefree(*i); } // --------------------------------------------------------------------- @@ -793,41 +788,29 @@ generic_packetizer_c::fix_headers() { void generic_packetizer_c::add_packet(packet_cptr pack) { - int length, add_length, i; + int length, i; if (reader->ptzr_first_packet == NULL) reader->ptzr_first_packet = this; // strip elements to be removed if ((htrack_max_add_block_ids != -1) && - (htrack_max_add_block_ids < pack->memory_adds.size())) - pack->memory_adds.resize(htrack_max_add_block_ids); + (htrack_max_add_block_ids < pack->data_adds.size())) + pack->data_adds.resize(htrack_max_add_block_ids); - pack->data_adds.resize(pack->memory_adds.size()); - pack->data_adds_lengths.resize(pack->memory_adds.size()); - - length = pack->memory->size; + length = pack->data->get_size(); if (NULL != compressor.get()) { - pack->data = compressor->compress(pack->memory->data, length); - pack->memory->release(); - for (i = 0; i < pack->memory_adds.size(); i++) { - add_length = pack->memory_adds[i]->size; - pack->data_adds[i] = compressor->compress(pack->memory_adds[i]->data, - add_length); - pack->data_adds_lengths[i] = add_length; - pack->memory_adds[i]->release(); - } + compressor->compress(pack->data); + for (i = 0; i < pack->data_adds.size(); i++) + compressor->compress(pack->data_adds[i]); } else { - pack->data = pack->memory->grab(); - for (i = 0; i < pack->memory_adds.size(); i++) { - pack->data_adds[i] = pack->memory_adds[i]->grab(); - pack->data_adds_lengths[i] = pack->memory_adds[i]->size; - } + pack->data->grab(); + for (i = 0; i < pack->data_adds.size(); i++) + pack->data_adds[i]->grab(); } - pack->length = length; pack->source = this; - enqueued_bytes += pack->length; + enqueued_bytes += pack->data->get_size(); if ((0 > pack->bref) && (0 <= pack->fref)) { int64_t tmp = pack->bref; @@ -946,7 +929,7 @@ generic_packetizer_c::get_packet() { packet_cptr pack = packet_queue.front(); packet_queue.pop_front(); - enqueued_bytes -= pack->length; + enqueued_bytes -= pack->data->get_size(); return pack; } diff --git a/src/output/p_aac.cpp b/src/output/p_aac.cpp index d4df3e1e0..0ca8e6714 100644 --- a/src/output/p_aac.cpp +++ b/src/output/p_aac.cpp @@ -190,7 +190,7 @@ aac_packetizer_c::process(packet_cptr packet) { return FILE_STATUS_MOREDATA; } while (needs_positive_displacement(duration)) { - add_packet(new packet_t(packet->memory->clone(), my_timecode + + add_packet(new packet_t(packet->data->clone(), my_timecode + ti.async.displacement, duration)); displace(duration); } @@ -206,7 +206,7 @@ aac_packetizer_c::process(packet_cptr packet) { return FILE_STATUS_MOREDATA; } - byte_buffer.add(packet->memory->data, packet->memory->size); + byte_buffer.add(packet->data->get(), packet->data->get_size()); while ((aac_packet = get_aac_packet(&header, &aacheader)) != NULL) { if (timecode == -1) my_timecode = (int64_t)(1024 * 1000000000.0 * packetno / diff --git a/src/output/p_ac3.cpp b/src/output/p_ac3.cpp index ed98692a9..07535b867 100644 --- a/src/output/p_ac3.cpp +++ b/src/output/p_ac3.cpp @@ -151,7 +151,7 @@ ac3_packetizer_c::process(packet_cptr packet) { debug_enter("ac3_packetizer_c::process"); - add_to_buffer(packet->memory->data, packet->memory->size); + add_to_buffer(packet->data->get(), packet->data->get_size()); while ((ac3_packet = get_ac3_packet(&header, &ac3header)) != NULL) { if (packet->timecode == -1) my_timecode = (int64_t)(1000000000.0 * packetno * 1536 / diff --git a/src/output/p_dts.cpp b/src/output/p_dts.cpp index 050ec95f8..977071d3d 100644 --- a/src/output/p_dts.cpp +++ b/src/output/p_dts.cpp @@ -176,7 +176,7 @@ dts_packetizer_c::process(packet_cptr packet) { debug_enter("dts_packetizer_c::process"); - add_to_buffer(packet->memory->data, packet->memory->size); + add_to_buffer(packet->data->get(), packet->data->get_size()); while ((dts_packet = get_dts_packet(dtsheader)) != NULL) { int64_t packet_len_in_ns = (int64_t)get_dts_packet_length_in_nanoseconds(&dtsheader); diff --git a/src/output/p_flac.cpp b/src/output/p_flac.cpp index 2915e7991..b6ada4f18 100644 --- a/src/output/p_flac.cpp +++ b/src/output/p_flac.cpp @@ -90,8 +90,9 @@ int flac_packetizer_c::process(packet_cptr packet) { debug_enter("flac_packetizer_c::process"); - packet->duration = flac_get_num_samples(packet->memory->data, - packet->memory->size, stream_info); + packet->duration = + flac_get_num_samples(packet->data->get(), packet->data->get_size(), + stream_info); if (packet->duration == -1) { mxwarn(_(FMT_TID "Packet number %lld contained an invalid FLAC header " "and is being skipped.\n"), ti.fname.c_str(), (int64_t)ti.id, diff --git a/src/output/p_mp3.cpp b/src/output/p_mp3.cpp index 5f4611278..7e0ee9a41 100644 --- a/src/output/p_mp3.cpp +++ b/src/output/p_mp3.cpp @@ -193,7 +193,7 @@ mp3_packetizer_c::process(packet_cptr packet) { debug_enter("mp3_packetizer_c::process"); - byte_buffer.add(packet->memory->data, packet->memory->size); + byte_buffer.add(packet->data->get(), packet->data->get_size()); while ((mp3_packet = get_mp3_packet(&mp3header)) != NULL) { if (packet->timecode == -1) my_timecode = (int64_t)(1000000000.0 * packetno * spf / samples_per_sec); diff --git a/src/output/p_passthrough.cpp b/src/output/p_passthrough.cpp index 7ae140038..6ebb04d46 100644 --- a/src/output/p_passthrough.cpp +++ b/src/output/p_passthrough.cpp @@ -47,7 +47,7 @@ passthrough_packetizer_c::process(packet_cptr packet) { timecode = packet->timecode; packets_processed++; - bytes_processed += packet->memory->size; + bytes_processed += packet->data->get_size(); if ((packet->duration > 0) && needs_negative_displacement(packet->duration)) { displace(-packet->duration); @@ -62,7 +62,7 @@ passthrough_packetizer_c::process(packet_cptr packet) { while ((packet->duration > 0) && needs_positive_displacement(packet->duration)) { packet_t *new_packet = - new packet_t(packet->memory->clone(), + new packet_t(packet->data->clone(), (int64_t)((timecode + ti.async.displacement) * ti.async.linear), packet->duration, packet->bref, packet->fref); diff --git a/src/output/p_pcm.cpp b/src/output/p_pcm.cpp index f2eb854fd..91e0d04a0 100644 --- a/src/output/p_pcm.cpp +++ b/src/output/p_pcm.cpp @@ -73,6 +73,7 @@ pcm_packetizer_c::set_headers() { int pcm_packetizer_c::process(packet_cptr packet) { unsigned char *new_buf; + int new_size; debug_enter("pcm_packetizer_c::process"); @@ -93,18 +94,19 @@ pcm_packetizer_c::process(packet_cptr packet) { initial_displacement = 0; } + new_size = packet->data->get_size(); if (skip_bytes) { - if (skip_bytes > packet->memory->size) { - skip_bytes -= packet->memory->size; + if (skip_bytes > packet->data->get_size()) { + skip_bytes -= packet->data->get_size(); return FILE_STATUS_MOREDATA; } - packet->memory->size -= skip_bytes; - new_buf = &packet->memory->data[skip_bytes]; + new_size -= skip_bytes; + new_buf = packet->data->get() + skip_bytes; skip_bytes = 0; } else - new_buf = packet->memory->data; + new_buf = packet->data->get(); - buffer.add(new_buf, packet->memory->size); + buffer.add(new_buf, new_size); while (buffer.get_size() >= packet_size) { add_packet(new packet_t(new memory_c(buffer.get_buffer(), packet_size, diff --git a/src/output/p_textsubs.cpp b/src/output/p_textsubs.cpp index 94010c38e..17725caa1 100644 --- a/src/output/p_textsubs.cpp +++ b/src/output/p_textsubs.cpp @@ -88,7 +88,7 @@ textsubs_packetizer_c::process(packet_cptr packet) { } // Count the number of lines. - idx1 = (char *)packet->memory->data; + idx1 = (char *)packet->data->get(); subs = NULL; num_newlines = 0; while (*idx1 != 0) { @@ -96,11 +96,11 @@ textsubs_packetizer_c::process(packet_cptr packet) { num_newlines++; idx1++; } - subs = (char *)safemalloc(strlen((char *)packet->memory->data) + + subs = (char *)safemalloc(strlen((char *)packet->data->get()) + num_newlines * 2 + 1); // Unify the new lines into DOS style newlines. - idx1 = (char *)packet->memory->data; + idx1 = (char *)packet->data->get(); idx2 = subs; while (*idx1 != 0) { if (*idx1 == '\n') { @@ -128,13 +128,13 @@ textsubs_packetizer_c::process(packet_cptr packet) { utf8_subs = to_utf8(cc_utf8, subs); safefree(subs); - packet->memory = + packet->data = memory_cptr(new memory_c((unsigned char *)utf8_subs.c_str(), utf8_subs.length(), false)); add_packet(packet); } else { - packet->memory = + packet->data = memory_cptr(new memory_c((unsigned char *)subs, strlen(subs), true)); add_packet(packet); } diff --git a/src/output/p_video.cpp b/src/output/p_video.cpp index f9a48a765..e23663138 100644 --- a/src/output/p_video.cpp +++ b/src/output/p_video.cpp @@ -231,13 +231,13 @@ mpeg1_2_video_packetizer_c::process(packet_cptr packet) { int new_bytes, state; if (fps < 0.0) - extract_fps(packet->memory->data, packet->memory->size); + extract_fps(packet->data->get(), packet->data->get_size()); if (!aspect_ratio_extracted) - extract_aspect_ratio(packet->memory->data, packet->memory->size); + extract_aspect_ratio(packet->data->get(), packet->data->get_size()); if (framed) { - if (0 != packet->memory->size) + if (0 != packet->data->get_size()) return video_packetizer_c::process(packet); else return FILE_STATUS_MOREDATA; @@ -248,9 +248,9 @@ mpeg1_2_video_packetizer_c::process(packet_cptr packet) { (state == MPV_PARSER_STATE_ERROR)) return FILE_STATUS_DONE; - memory_cptr old_memory = packet->memory; - data_ptr = old_memory->data; - new_bytes = old_memory->size; + memory_cptr old_memory = packet->data; + data_ptr = old_memory->get(); + new_bytes = old_memory->get_size(); do { int bytes_to_add; @@ -383,9 +383,9 @@ mpeg4_p2_video_packetizer_c(generic_reader_c *_reader, int mpeg4_p2_video_packetizer_c::process(packet_cptr packet) { if (!size_extracted) - extract_size(packet->memory->data, packet->memory->size); + extract_size(packet->data->get(), packet->data->get_size()); if (!aspect_ratio_extracted) - extract_aspect_ratio(packet->memory->data, packet->memory->size); + extract_aspect_ratio(packet->data->get(), packet->data->get_size()); if (input_is_native == output_is_native) return video_packetizer_c::process(packet); @@ -404,11 +404,12 @@ mpeg4_p2_video_packetizer_c::process_non_native(packet_cptr packet) { if (NULL == ti.private_data) { memory_c *config_data; - config_data = mpeg4_p2_parse_config_data(packet->memory->data, - packet->memory->size); + config_data = mpeg4_p2_parse_config_data(packet->data->get(), + packet->data->get_size()); if (NULL != config_data) { - ti.private_data = config_data->grab(); - ti.private_size = config_data->size; + ti.private_data = (unsigned char *)safememdup(config_data->get(), + config_data->get_size()); + ti.private_size = config_data->get_size(); delete config_data; fix_codec_string(); set_codec_private(ti.private_data, ti.private_size); @@ -420,7 +421,7 @@ mpeg4_p2_video_packetizer_c::process_non_native(packet_cptr packet) { "native mode.\n"); } - mpeg4_p2_find_frame_types(packet->memory->data, packet->memory->size, + mpeg4_p2_find_frame_types(packet->data->get(), packet->data->get_size(), frames); // Add a timecode and a duration if they've been given. @@ -449,13 +450,8 @@ mpeg4_p2_video_packetizer_c::process_non_native(packet_cptr packet) { if (-1 == packet->duration) available_durations.push_back((int64_t)(1000000000.0 / fps)); - // Copy the data. If there's only one frame in this packet then - // we might save a memcpy. - if ((1 == frames.size()) && (frame->size == packet->memory->size)) - frame->data = packet->memory->grab(); - else - frame->data = (unsigned char *) - safememdup(&packet->memory->data[frame->pos], frame->size); + frame->data = (unsigned char *) + safememdup(packet->data->get() + frame->pos, frame->size); queued_frames.push_back(*frame); } diff --git a/src/output/p_vobbtn.cpp b/src/output/p_vobbtn.cpp index 367ce9255..b33e4e47f 100644 --- a/src/output/p_vobbtn.cpp +++ b/src/output/p_vobbtn.cpp @@ -63,8 +63,8 @@ int vobbtn_packetizer_c::process(packet_cptr packet) { int32_t vobu_start, vobu_end; - vobu_start = get_uint32_be(packet->memory->data + 0x0d); - vobu_end = get_uint32_be(packet->memory->data + 0x11); + vobu_start = get_uint32_be(packet->data->get() + 0x0d); + vobu_end = get_uint32_be(packet->data->get() + 0x11); packet->duration = (int64_t)(100000.0 * (float)(vobu_end - vobu_start) / 9); if (packet->timecode == -1) { diff --git a/src/output/p_vorbis.cpp b/src/output/p_vorbis.cpp index 388854bde..6c91d84ec 100644 --- a/src/output/p_vorbis.cpp +++ b/src/output/p_vorbis.cpp @@ -175,8 +175,8 @@ vorbis_packetizer_c::process(packet_cptr packet) { // Update the number of samples we have processed so that we can // calculate the timecode on the next call. - op.packet = packet->memory->data; - op.bytes = packet->memory->size; + op.packet = packet->data->get(); + op.bytes = packet->data->get_size(); this_bs = vorbis_packet_blocksize(&vi, &op); samples_here = (this_bs + last_bs) / 4; last_bs = this_bs; diff --git a/src/output/p_wavpack.cpp b/src/output/p_wavpack.cpp index b2e5a9bd6..26604da06 100644 --- a/src/output/p_wavpack.cpp +++ b/src/output/p_wavpack.cpp @@ -57,7 +57,7 @@ wavpack_packetizer_c::set_headers() { int wavpack_packetizer_c::process(packet_cptr packet) { debug_enter("wavpack_packetizer_c::process"); - int64_t samples = get_uint32_le(packet->memory->data); + int64_t samples = get_uint32_le(packet->data->get()); if (-1 == packet->duration) packet->duration = irnd(samples * 1000000000 / sample_rate);