content/media/AudioCompactor.cpp

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "AudioCompactor.h"
     7 #if defined(MOZ_MEMORY)
     8 # include "mozmemory.h"
     9 #endif
    11 namespace mozilla {
    13 static size_t
    14 MallocGoodSize(size_t aSize)
    15 {
    16 # if defined(MOZ_MEMORY)
    17   return malloc_good_size(aSize);
    18 # else
    19   return aSize;
    20 # endif
    21 }
    23 static size_t
    24 TooMuchSlop(size_t aSize, size_t aAllocSize, size_t aMaxSlop)
    25 {
    26   // If the allocated size is less then our target size, then we
    27   // are chunking.  This means it will be completely filled with
    28   // zero slop.
    29   size_t slop = (aAllocSize > aSize) ? (aAllocSize - aSize) : 0;
    30   return slop > aMaxSlop;
    31 }
    33 uint32_t
    34 AudioCompactor::GetChunkSamples(uint32_t aFrames, uint32_t aChannels,
    35                                 size_t aMaxSlop)
    36 {
    37   size_t size = AudioDataSize(aFrames, aChannels);
    38   size_t chunkSize = MallocGoodSize(size);
    40   // Reduce the chunk size until we meet our slop goal or the chunk
    41   // approaches an unreasonably small size.
    42   while (chunkSize > 64 && TooMuchSlop(size, chunkSize, aMaxSlop)) {
    43     chunkSize = MallocGoodSize(chunkSize / 2);
    44   }
    46   // Calculate the number of samples based on expected malloc size
    47   // in order to allow as many frames as possible to be packed.
    48   return chunkSize / sizeof(AudioDataValue);
    49 }
    51 uint32_t
    52 AudioCompactor::NativeCopy::operator()(AudioDataValue *aBuffer,
    53                                        uint32_t aSamples)
    54 {
    55   NS_ASSERTION(aBuffer, "cannot copy to null buffer pointer");
    56   NS_ASSERTION(aSamples, "cannot copy zero values");
    58   size_t bufferBytes = aSamples * sizeof(AudioDataValue);
    59   size_t maxBytes = std::min(bufferBytes, mSourceBytes - mNextByte);
    60   uint32_t frames = maxBytes / BytesPerFrame(mChannels);
    61   size_t bytes = frames * BytesPerFrame(mChannels);
    63   NS_ASSERTION((mNextByte + bytes) <= mSourceBytes,
    64                "tried to copy beyond source buffer");
    65   NS_ASSERTION(bytes <= bufferBytes, "tried to copy beyond destination buffer");
    67   memcpy(aBuffer, mSource + mNextByte, bytes);
    69   mNextByte += bytes;
    70   return frames;
    71 }
    73 } // namespace mozilla

mercurial