Skip to content

Commit

Permalink
Merge pull request #1791 from ashvarma/release_1_23
Browse files Browse the repository at this point in the history
Release 1 23
  • Loading branch information
ashvarma authored Jan 24, 2017
2 parents 4804ed9 + 8df2b66 commit 09d9f19
Show file tree
Hide file tree
Showing 1,128 changed files with 11,928 additions and 5,918 deletions.
6 changes: 3 additions & 3 deletions Frameworks/Accelerate/vImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@ vImage_Error vImageBuffer_Init(
const uint32_t allocSize = maxPitch * height;

// Note: We can't use alignedMalloc here since the client is responsible for freeing the buffer and can use free or aligned_free
buffer->data = malloc(allocSize);
buffer->data = IwMalloc(allocSize);

if (buffer->data != nullptr) {
// SIMD load/store instructions operate best on 16byte aligned memory and in blocks of 16bytes
Expand All @@ -1738,15 +1738,15 @@ vImage_Error vImageBuffer_Init(
} else {
// Try again without additional padding
buffer->rowBytes = alignedPitch;
buffer->data = malloc(buffer->rowBytes * height);
buffer->data = IwMalloc(buffer->rowBytes * height);

if (buffer->data == nullptr) {
returnCode = kvImageMemoryAllocationError;
}
}
} else {
buffer->rowBytes = alignedPitch;
buffer->data = malloc(buffer->rowBytes * height);
buffer->data = IwMalloc(buffer->rowBytes * height);

if (buffer->data == nullptr) {
returnCode = kvImageMemoryAllocationError;
Expand Down
4 changes: 2 additions & 2 deletions Frameworks/Accelerate/vImage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ CGImageRef vImageCreateCGImageFromBuffer(vImage_Buffer* buffer,

if (packedWidthInBytes < buffer->rowBytes) {
// packing needed
packedBuffer = malloc(buffer->height * packedWidthInBytes);
packedBuffer = IwMalloc(buffer->height * packedWidthInBytes);

if (packedBuffer != nil) {
packedBufferAllocated = true;
Expand Down Expand Up @@ -215,7 +215,7 @@ CGImageRef vImageCreateCGImageFromBuffer(vImage_Buffer* buffer,
CGContextRelease(ctx);

if (packedBufferAllocated == true) {
free(packedBuffer);
IwFree(packedBuffer);
}
}

Expand Down
230 changes: 217 additions & 13 deletions Frameworks/AudioToolbox/AudioConverter.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//******************************************************************************
//
// Copyright (c) 2016 Intel Corporation. All rights reserved.
// Copyright (c) 2016 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
Expand All @@ -16,28 +17,140 @@

#import <AudioToolbox/AudioConverter.h>
#import <StubReturn.h>
#import <NSBundleInternal.h>
#import <NSLogging.h>
#import "AssertARCEnabled.h"

static const wchar_t* TAG = L"AudioConverter";

#import <AudioToolbox/AudioConverterInternal.h>


@implementation AudioConverter
- (instancetype)initWithSourceFormat:(const AudioStreamBasicDescription*)srcFormat
destinationFormat:(const AudioStreamBasicDescription*)destFormat {
if (self = [super init]) {
ComPtr<IUnknown> spTransformUnk;

RETURN_NIL_IF_FAILED(
CoCreateInstance(CLSID_AudioResamplerMediaObject, nullptr, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&spTransformUnk));

RETURN_NIL_IF_FAILED(spTransformUnk.As(&_transform));

_sizeChangeMultiplier =
(destFormat->mSampleRate * destFormat->mBytesPerFrame) / (srcFormat->mSampleRate * srcFormat->mBytesPerFrame);
}

return self;
}

- (ComPtr<IMFTransform>)getTransform {
return _transform;
}

- (float)getSizeChangeMultiplier {
return _sizeChangeMultiplier;
}
@end

/**
@Status Stub
@Status Caveat: AudioConverter supported only for conversions through AudioConverterConvertBuffer.
Returns kAudioConverterErr_UnspecifiedError on failure and noErr (0) on success. Other return types not supported.
@Notes
*/
OSStatus AudioConverterDispose(AudioConverterRef inAudioConverter) {
UNIMPLEMENTED();
return StubReturn();
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(MFShutdown(), @"MFShutdown Failed");
CFBridgingRelease(inAudioConverter);
return noErr;
}

OSStatus _setMFProperties(const AudioStreamBasicDescription* format, IMFMediaType** mediaType) {
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(MFCreateMediaType(mediaType), @"MFCreateMediaType failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG((*mediaType)->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio), @"Setting the major type failed");

if (format->mFormatFlags & kAudioFormatFlagIsFloat) {
RETURN_AUDIOERR_IF_FAILED_WITH_MSG((*mediaType)->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_Float), @"Setting the subtype failed");
} else {
RETURN_AUDIOERR_IF_FAILED_WITH_MSG((*mediaType)->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM), @"Setting the subtype failed");
}

RETURN_AUDIOERR_IF_FAILED_WITH_MSG((*mediaType)->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, format->mChannelsPerFrame),
@"Setting the number of audio channels failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG((*mediaType)->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, format->mSampleRate),
@"Setting the number of audio samples per second failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG((*mediaType)->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, format->mBytesPerFrame),
@"Setting the block alignment failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG((*mediaType)
->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, format->mSampleRate * format->mBytesPerFrame),
@"Setting the average number of bytes per second failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG((*mediaType)->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, format->mBitsPerChannel),
@"Setting the number of bits per audio sample failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG((*mediaType)->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE),
@"Setting the audio sample independent flag failed");

return noErr;
}

/**
@Status Stub
@Status Caveat: AudioConverter supported only for conversions through AudioConverterConvertBuffer.
Only Linear PCM formats are supported with 1 Frame per Packet and equal source & destination Channels per Frame.
Returns kAudioConverterErr_UnspecifiedError on failure and noErr (0) on success. Other return types not supported.
*/
OSStatus AudioConverterNew(const AudioStreamBasicDescription* inSourceFormat,
const AudioStreamBasicDescription* inDestinationFormat,
DWORD* outAudioConverter) {
// TODO: outAudioConverter ought to be made AudioConverterRef _Nullable * when this is unstubbed
UNIMPLEMENTED();
if (outAudioConverter) {
*outAudioConverter = 1234;
AudioConverterRef _Nullable* outAudioConverter) {
if (inSourceFormat == nullptr || inDestinationFormat == nullptr) {
return kAudioConverterErr_UnspecifiedError;
}
return StubReturn();

if (inSourceFormat->mFormatID != kAudioFormatLinearPCM || inDestinationFormat->mFormatID != kAudioFormatLinearPCM) {
UNIMPLEMENTED();
return StubReturn();
}

if (inSourceFormat->mChannelsPerFrame != inDestinationFormat->mChannelsPerFrame) {
UNIMPLEMENTED();
return StubReturn();
}

if (inSourceFormat->mFramesPerPacket != 1 || inSourceFormat->mFramesPerPacket != inDestinationFormat->mFramesPerPacket) {
UNIMPLEMENTED();
return StubReturn();
}

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET), @"MFStartup Failed");

AudioConverter* outConverter = [[AudioConverter alloc] initWithSourceFormat:inSourceFormat destinationFormat:inDestinationFormat];

ComPtr<IMFTransform> mediaTransform = [outConverter getTransform];

ComPtr<IMFMediaType> sourceMediaType;
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(_setMFProperties(inSourceFormat, &sourceMediaType), @"Creating source mediatype Failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaTransform->SetInputType(0, sourceMediaType.Get(), 0), @"MFTransform InputStream Failed");

ComPtr<IMFMediaType> destinationMediaType;
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(_setMFProperties(inDestinationFormat, &destinationMediaType),
@"Creating destination mediatype Failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaTransform->SetOutputType(0, destinationMediaType.Get(), 0), @"MFTransform OutputStream Failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaTransform->ProcessMessage(MFT_MESSAGE_COMMAND_FLUSH, NULL), @"MFTransform Flush Failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaTransform->ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, NULL),
@"MFTransform Begin Streaming Failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaTransform->ProcessMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, NULL),
@"MFTransform Start Streaming Failed");

*outAudioConverter = (AudioConverterRef)CFBridgingRetain(outConverter);
return noErr;
}

/**
Expand Down Expand Up @@ -99,13 +212,104 @@ OSStatus AudioConverterSetProperty(AudioConverterRef inAudioConverter,
}

/**
@Status Stub
@Status Caveat: Returns kAudioConverterErr_UnspecifiedError, kAudioConverterErr_InvalidInputSize or
kAudioConverterErr_InvalidOutputSize on failure and noErr (0) on success. Other return types not supported.
@Notes
*/
OSStatus AudioConverterConvertBuffer(
AudioConverterRef inAudioConverter, UInt32 inInputDataSize, const void* inInputData, UInt32* ioOutputDataSize, void* outOutputData) {
UNIMPLEMENTED();
return StubReturn();
if (inAudioConverter == nullptr) {
return kAudioConverterErr_UnspecifiedError;
}

if (inInputData == nullptr || inInputDataSize == 0) {
return kAudioConverterErr_InvalidInputSize;
}

float multiplier = [(__bridge AudioConverter*)inAudioConverter getSizeChangeMultiplier];
if (outOutputData == nullptr || ioOutputDataSize == 0 || *ioOutputDataSize < ceil(float(inInputDataSize) * multiplier)) {
return kAudioConverterErr_InvalidOutputSize;
}

ComPtr<IMFMediaBuffer> mediaBufferIn = nullptr;
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(MFCreateMemoryBuffer(inInputDataSize, &mediaBufferIn), @"Temp buffer creation failed");

BYTE* byteBufferIn = nullptr;
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaBufferIn->Lock(&byteBufferIn, nullptr, nullptr), @"Buffer Lock failed");

memcpy(byteBufferIn, inInputData, inInputDataSize);
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaBufferIn->Unlock(), @"Buffer Unlock failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaBufferIn->SetCurrentLength(inInputDataSize), @"Set Length failed");

ComPtr<IMFSample> sampleIn = nullptr;
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(MFCreateSample(&sampleIn), @"MFSample creation failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(sampleIn->AddBuffer(mediaBufferIn.Get()), @"Buffer Addition failed");

ComPtr<IMFTransform> mediaTransform = [(__bridge AudioConverter*)inAudioConverter getTransform];

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaTransform->ProcessInput(0, sampleIn.Get(), 0), @"Input processing failed");

// Create the output sample
ComPtr<IMFSample> sampleOut = NULL;
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(MFCreateSample(&sampleOut), @"Output sample creation failed");

// Create a buffer for the output sample
ComPtr<IMFMediaBuffer> bufferOut = NULL;
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(MFCreateMemoryBuffer(*ioOutputDataSize, &bufferOut), @"Output buffer creation failed");

// Add the output buffer
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(sampleOut->AddBuffer(bufferOut.Get()), @"Adding output buffer failed");

// Starting the counter for number of bytes written to "outOutputData"
*ioOutputDataSize = 0;
bool drainFlag = false;
MFT_OUTPUT_DATA_BUFFER dataBuffer;
dataBuffer.pSample = sampleOut.Get();
dataBuffer.dwStreamID = 0;
dataBuffer.dwStatus = 0;
dataBuffer.pEvents = NULL;
DWORD outputStatus;

for (;;) {
HRESULT status = mediaTransform->ProcessOutput(0, 1, &dataBuffer, &outputStatus);

// State 0: All of the Input data has been processed. There may still be some Output data yet to be processed.
if (status == MF_E_TRANSFORM_NEED_MORE_INPUT && drainFlag == false) {
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaTransform->ProcessMessage(MFT_MESSAGE_NOTIFY_END_OF_STREAM, NULL),
@"End Stream notification failed");

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaTransform->ProcessMessage(MFT_MESSAGE_COMMAND_DRAIN, NULL),
@"Drain notification failed");

drainFlag = true;
continue;
}

// State 1: The remaining Output data has been processed.
if (status == MF_E_TRANSFORM_NEED_MORE_INPUT && drainFlag == true) {
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaTransform->ProcessMessage(MFT_MESSAGE_NOTIFY_END_STREAMING, NULL),
@"Nearing-End Stream notification failed");
break;
}

ComPtr<IMFMediaBuffer> mediaBufferOut;
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(sampleOut->ConvertToContiguousBuffer(&mediaBufferOut), @"Buffer conversion failed");

DWORD outputBytes = 0;
mediaBufferOut->GetCurrentLength(&outputBytes);

BYTE* outputByteBuffer = nullptr;
RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaBufferOut->Lock(&outputByteBuffer, nullptr, nullptr), @"Output Buffer Lock failed");

memcpy(static_cast<BYTE*>(outOutputData) + *ioOutputDataSize, outputByteBuffer, outputBytes);
*ioOutputDataSize += outputBytes;

RETURN_AUDIOERR_IF_FAILED_WITH_MSG(mediaBufferOut->Unlock(), @"Output Buffer Unlock failed");
}

return noErr;
}

/**
Expand Down
Loading

0 comments on commit 09d9f19

Please sign in to comment.