20 #include <com/avpkit/ferry/Logger.h>
21 #include <com/avpkit/core/AudioSamples.h>
22 #include <com/avpkit/core/Global.h>
25 #include <com/avpkit/core/FfmpegIncludes.h>
29 VS_LOG_SETUP(VS_CPP_PACKAGE);
31 namespace com {
namespace avpkit {
namespace core
35 AudioSamples :: AudioSamples()
41 mChannelLayout = IAudioSamples::ChannelLayout::CH_NONE;
45 mRequestedSamples = 0;
49 AudioSamples :: ~AudioSamples()
59 mRequestedSamples = 0;
62 #define VS_AUDIOSAMPLES_BUFFER_PADDING 64
64 AudioSamples :: allocInternalSamples()
68 int32_t bufSize = mRequestedSamples * getSampleSize() +
69 VS_AUDIOSAMPLES_BUFFER_PADDING;
73 throw std::bad_alloc();
74 setBufferType(mSampleFmt, mSamples.value());
83 VS_LOG_TRACE(
"Created AudioSamples(%d bytes)", bufSize);
93 mSamples->getBufferSize() < (capacity + VS_AUDIOSAMPLES_BUFFER_PADDING))
96 VS_LOG_WARN(
"Internal buffer not big enough need to recreate %d",capacity);
99 int32_t sampleSize = getSampleSize();
100 int32_t requiredSamples;
102 requiredSamples = capacity / getSampleSize();
104 requiredSamples = 192000;
105 mRequestedSamples = requiredSamples;
110 AudioSamples :: getRawSamples(uint32_t startingSample)
113 allocInternalSamples();
116 uint32_t startingOffset = startingSample*getSampleSize();
117 uint32_t bufLen = (mNumSamples*getSampleSize())-startingOffset;
118 retval = (
short*)mSamples->getBytes(startingOffset, bufLen);
124 AudioSamples :: make(int32_t numSamples,
131 AudioSamples :: make(int32_t numSamples,
135 AudioSamples *retval=0;
136 if (numSamples > 0 && numChannels > 0)
138 retval = AudioSamples::make();
143 retval->mChannels = numChannels;
144 retval->mSampleFmt = format;
145 retval->mRequestedSamples = numSamples;
152 AudioSamples :: make(
IBuffer* buffer, int32_t channels,
157 if (format == IAudioSamples::FMT_NONE)
165 int samplesRequested = buffer->
getBufferSize()/bytesPerSample;
166 AudioSamples* retval = 0;
169 retval = make(samplesRequested, channels, format);
172 retval->setData(buffer);
174 catch (std::bad_alloc &e)
176 VS_REF_RELEASE(retval);
179 catch (std::exception& e)
181 VS_LOG_DEBUG(
"error: %s", e.what());
182 VS_REF_RELEASE(retval);
192 mSamples.reset(buffer,
true);
193 setBufferType(mSampleFmt, buffer);
221 AudioSamples::getChannelLayout()
223 return mChannelLayout;
236 allocInternalSamples();
237 return mSamples->getBufferSize()-VS_AUDIOSAMPLES_BUFFER_PADDING;
249 int32_t bits = getSampleBitDepth();
253 return bits/8 * getChannels();
259 allocInternalSamples();
260 IBuffer* retval = mSamples.get();
262 throw std::bad_alloc();
269 return getMaxBufferSize() / getSampleSize();
274 int32_t sampleRate, int32_t channels,
Format format,
277 mIsComplete = complete;
281 mChannels = channels;
282 mChannelLayout = (
ChannelLayout)av_get_default_channel_layout(mChannels);
283 mSampleRate = sampleRate;
287 setBufferType(mSampleFmt, mSamples.value());
291 mNumSamples = FFMIN(numSamples,
292 getMaxBufferSize()/(getSampleSize()));
295 short* samps = this->getRawSamples(0);
296 for(int32_t i = 0; i < mNumSamples;i++)
298 int32_t samp = samps[i];
299 VS_LOG_DEBUG(
"i: %d; samp: %d", i, samp);
310 AudioSamples::setComplete(
bool complete, int32_t numSamples, int32_t sampleRate, int32_t channels, ChannelLayout channelLayout, Format format, int64_t pts)
312 setComplete(complete, numSamples, sampleRate, channels, format, pts);
313 mChannelLayout = channelLayout;
343 if (channel < 0 || channel >= mChannels)
344 throw std::invalid_argument(
"cannot setSample for given channel");
345 if (format != FMT_S16)
346 throw std::invalid_argument(
"only support format: FMT_S16");
347 if (sampleIndex >= this->getMaxSamples())
348 throw std::invalid_argument(
"sampleIndex out of bounds");
350 short *rawSamples = this->getRawSamples(0);
352 throw std::runtime_error(
"no samples buffer set in AudioSamples");
354 rawSamples[sampleIndex*mChannels + channel] = (short)sample;
357 catch (std::exception & e)
359 VS_LOG_DEBUG(
"Error: %s", e.what());
371 if (channel < 0 || channel >= mChannels)
372 throw std::invalid_argument(
"cannot getSample for given channel");
373 if (format != FMT_S16)
374 throw std::invalid_argument(
"only support format: FMT_S16");
375 if (sampleIndex >= this->getNumSamples())
376 throw std::invalid_argument(
"sampleIndex out of bounds");
378 short *rawSamples = this->getRawSamples(0);
380 throw std::runtime_error(
"no samples buffer set in AudioSamples");
382 retval = rawSamples[sampleIndex*mChannels + channel];
384 catch(std::exception & e)
386 VS_LOG_DEBUG(
"Error: %s", e.what());
402 buffer->
setType(IBuffer::IBUFFER_DBL64);
406 buffer->
setType(IBuffer::IBUFFER_FLT32);
410 buffer->
setType(IBuffer::IBUFFER_SINT16);
414 buffer->
setType(IBuffer::IBUFFER_SINT32);
418 buffer->
setType(IBuffer::IBUFFER_UINT8);
virtual int32_t getSampleSize()
virtual int32_t getMaxSamples()
virtual int32_t getSampleRate()
Find the sample rate of the samples in this audio buffer.
virtual Format getFormat()
Find the Format of the samples in this buffer.
virtual int32_t getChannels()
Return the number of channels of the samples in this buffer.
virtual com::avpkit::ferry::IBuffer * getData()
Get any underlying raw data available for this object.
virtual int32_t ensureCapacity(int32_t capacityInBytes)
Called by decoder before decoding to ensure sufficient space.
virtual void setComplete(bool complete, int32_t numSamples, int32_t sampleRate, int32_t channels, Format sampleFmt, int64_t pts)
Call this if you modify the samples and are now done.
virtual int64_t getPts()
What is the Presentation Time Stamp of this set of audio samples.
virtual int32_t getMaxBufferSize()
virtual void setPts(int64_t aValue)
Set the Presentation Time Stamp for this set of samples.
virtual int32_t setSample(int32_t sampleIndex, int32_t channel, Format format, int32_t sample)
Sets the sample at the given index and channel to the sample.
virtual int32_t getSample(int32_t sampleIndex, int32_t channel, Format format)
Get the sample at the given sampleIndex and channel, and return it in the asked for format.
virtual int32_t getNumSamples()
Get the number of samples in this video.
virtual int32_t getSampleBitDepth()
Find out the bit-depth of the samples in this buffer.
virtual void setData(com::avpkit::ferry::IBuffer *buffer)
Sets the underlying buffer used by this object.
virtual bool isComplete()
Returns whether or not we think this buffer has been filled with data.
virtual int64_t getNextPts()
What would be the next Presentation Time Stamp after all the samples in this buffer were played?
static const int64_t NO_PTS
A value that means no time stamp is set for a given object.
static int32_t findSampleBitDepth(Format format)
A convenience method that returns the # of bits in a given format.
Format
The format we use to represent audio.
static int64_t samplesToDefaultPts(int64_t samples, int sampleRate)
Converts a number of samples at a given sampleRate into Microseconds.
static IRational * make()
Get a new rational that will be set to 0/0.
Allows Java code to get data from a native buffers, and optionally modify native memory directly.
virtual void setType(Type type)=0
Reset the buffer type to a new type.
static IBuffer * make(RefCounted *requestor, void *bufToWrap, int32_t bufferSize, FreeFunc freeFunc, void *closure)
Allocate a new buffer by wrapping a native buffer.
virtual int32_t getBufferSize()=0
Get the current maximum number of bytes that can be safely placed in this buffer.
This library contains routines used by AVPKit libraries for "ferry"ing Java objects to and from nativ...
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...