summaryrefslogtreecommitdiff
path: root/audio/tests/HLK/ThirdPartyApoTests/audioconnectionutil.h
blob: 771d03de8db25d4237c0ba707b2a28f3e42b697f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once 

#define IS_2_POW_N(X)           (((X)&(X-1)) == 0)

enum class aligned_size_t : size_t {};

void * AllocateAligned(size_t cb, aligned_size_t AlignmentMask)
{
    ASSERT(0 != cb);
    //ASSERT(IS_2_POW_N(AlignmentMask + 1));
    
    void *res = _aligned_malloc(cb, static_cast<size_t>(AlignmentMask) + 1);

    return res;
}

void DeallocateAligned(void * p, aligned_size_t /* AlignmentMask */)
{
    if (NULL != p)
    {
        _aligned_free(p);
    }
}

UINT32 CalculateAlignmentMask(APO_CONNECTION_DESCRIPTOR * /* pDescriptor */, UINT32 u32BytesPerFrame)
{
    const UINT32 u32AudioAlignment = sizeof(PVOID);
    UINT32 u32Alignment = u32AudioAlignment - 1;

    // Calculate the new alignment requirement for this buffer.
    if (u32BytesPerFrame > u32AudioAlignment)
    {
        // Use the frame size directly if it is a power of two.
        if (IS_2_POW_N(u32BytesPerFrame))
        {
            u32Alignment = u32BytesPerFrame - 1;
        }
        // The new alignment size should be greater than frame 
        // size and should be a power of 2.
        else
        {
            u32Alignment = u32AudioAlignment;
            while (u32BytesPerFrame / u32Alignment)
            {
                u32Alignment *= 2;
            }
            u32Alignment = u32Alignment - 1;
        }
    }

    // Use default alignment if the frame size is less than the default 
    // alignment.
    return u32Alignment; 
} // CalculateAlignmentMask

HRESULT
CreateConnection(APO_CONNECTION_DESCRIPTOR* pDescriptor, APO_CONNECTION_PROPERTY* pProperty)
{
    RETURN_HR_IF_NULL(E_POINTER, pDescriptor);
    RETURN_HR_IF_NULL(E_POINTER, pDescriptor->pFormat);
    RETURN_HR_IF_NULL(E_POINTER, pProperty);

    UINT32 u32BytesPerFrame;
    unsigned byteAlignmentMask;
    BYTE*  pAllocatedBuffer;
    UNCOMPRESSEDAUDIOFORMAT Format;

    RETURN_IF_FAILED(pDescriptor->pFormat->GetUncompressedAudioFormat(&Format));

    u32BytesPerFrame = 
        Format.dwSamplesPerFrame * Format.dwBytesPerSampleContainer;

    // Allocate buffer internally.
    if (NULL == pDescriptor->pBuffer)
    {
        byteAlignmentMask = CalculateAlignmentMask(pDescriptor, u32BytesPerFrame);

        pAllocatedBuffer = (BYTE *)AllocateAligned(pDescriptor->u32MaxFrameCount * u32BytesPerFrame, static_cast<aligned_size_t>(byteAlignmentMask));
        RETURN_HR_IF_NULL(E_OUTOFMEMORY, pAllocatedBuffer);

        pDescriptor->pBuffer = reinterpret_cast<UINT_PTR>(pAllocatedBuffer);
        pDescriptor->Type = APO_CONNECTION_BUFFER_TYPE_ALLOCATED;
    }
    // Use the given buffer.
    else
    {
        pDescriptor->Type = APO_CONNECTION_BUFFER_TYPE_EXTERNAL;
    }
    
    pProperty->pBuffer = pDescriptor->pBuffer;
    pProperty->u32ValidFrameCount = 0;
    pProperty->u32Signature = APO_CONNECTION_PROPERTY_SIGNATURE;

    return S_OK;
} // CreateConnection

HRESULT
DestroyConnection(APO_CONNECTION_DESCRIPTOR *pDescriptor)
{
    HRESULT hResult;
    UINT32 u32BytesPerFrame;
    size_t byteAlignmentMask;
    BYTE*  pAllocatedBuffer;
    UNCOMPRESSEDAUDIOFORMAT Format;

    hResult = pDescriptor->pFormat->GetUncompressedAudioFormat(&Format);
    //IF_FAILED_JUMP(hResult, Exit);

    if (APO_CONNECTION_BUFFER_TYPE_ALLOCATED == pDescriptor->Type)
    {
        u32BytesPerFrame = 
            Format.dwSamplesPerFrame * Format.dwBytesPerSampleContainer;

        byteAlignmentMask = CalculateAlignmentMask(pDescriptor, u32BytesPerFrame);

        if (pDescriptor->pBuffer)
        {
            pAllocatedBuffer = reinterpret_cast<BYTE*>(pDescriptor->pBuffer);
        	RETURN_HR_IF_NULL(E_OUTOFMEMORY, pAllocatedBuffer);

            DeallocateAligned((void *) pAllocatedBuffer, static_cast<aligned_size_t>(byteAlignmentMask));
        }
    }

    pDescriptor->pFormat->Release();
    pDescriptor->pFormat = NULL;

    return hResult;
} // DestroyConnection