summaryrefslogtreecommitdiffstats
path: root/plugins/pluginWinAudioDSP/plugin_audio_dsp_utils.cxx
blob: d549336f6653a347f3095dd4c6d95fe19920fd37 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Copyright (C) 2013 Mamadou DIOP
* Copyright (C) 2013 Doubango Telecom <http://www.doubango.org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*/
#include "plugin_audio_dsp_utils.h"
#include "tsk_debug.h"

#include <uuids.h>
#include <Dmo.h>
#include <Mfapi.h>
#include <assert.h>

bool AudioDSPUtils::g_bStarted = false;

HRESULT AudioDSPUtils::Startup()
{
    if(!g_bStarted) {
        HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
        if(SUCCEEDED(hr) || hr == 0x80010106) { // 0x80010106 when called from managed code (e.g. Boghe) - More info: http://support.microsoft.com/kb/824480
            hr = MFStartup(MF_VERSION);
        }
        g_bStarted = SUCCEEDED(hr);
        return hr;
    }
    return S_OK;
}

HRESULT AudioDSPUtils::Shutdown()
{
    return S_OK;
}

HRESULT AudioDSPUtils::CreatePCMAudioType(
    UINT32 sampleRate,        // Samples per second
    UINT32 bitsPerSample,     // Bits per sample
    UINT32 cChannels,         // Number of channels
    IMFMediaType **ppType     // Receives a pointer to the media type.
)
{
    HRESULT hr = S_OK;

    IMFMediaType *pType = NULL;

    // Calculate derived values.
    UINT32 blockAlign = cChannels * (bitsPerSample >> 3);
    UINT32 bytesPerSecond = blockAlign * sampleRate;

    // Create the empty media type.
    CHECK_HR(hr = MFCreateMediaType(&pType));

    // Set attributes on the type.
    CHECK_HR(hr = pType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio));

    CHECK_HR(hr = pType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM));

    CHECK_HR(hr = pType->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, cChannels));

    CHECK_HR(hr = pType->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, sampleRate));

    CHECK_HR(hr = pType->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, blockAlign));

    CHECK_HR(hr = pType->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, bytesPerSecond));

    CHECK_HR(hr = pType->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, bitsPerSample));

    CHECK_HR(hr = pType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE));

    CHECK_HR(hr = pType->SetUINT32(MF_MT_FIXED_SIZE_SAMPLES, TRUE));

    *ppType = pType;
    (*ppType)->AddRef();

bail:
    SafeRelease(&pType);
    return hr;
}

HRESULT AudioDSPUtils::CreateMediaSample(
    DWORD cbData, // Maximum buffer size
    IMFSample **ppSample // Receives the sample
)
{
    HRESULT hr = S_OK;

    if(!ppSample) {
        CHECK_HR(hr = E_POINTER);
    }

    IMFSample *pSample = NULL;
    IMFMediaBuffer *pBuffer = NULL;

    CHECK_HR(hr = MFCreateSample(&pSample));
    CHECK_HR(hr = MFCreateMemoryBuffer(cbData, &pBuffer));
    CHECK_HR(hr = pSample->AddBuffer(pBuffer));

    *ppSample = pSample;
    (*ppSample)->AddRef();

bail:
    SafeRelease(&pSample);
    SafeRelease(&pBuffer);
    return hr;
}

HRESULT AudioDSPUtils::MoInitMediaType(
    UINT32 sampleRate,        // Samples per second
    UINT32 bitsPerSample,     // Bits per sample
    UINT32 cChannels,         // Number of channels
    DMO_MEDIA_TYPE *pType     // The media type to initialize. Must be freed using MoFreeMediaType.
)
{
    HRESULT hr = S_OK;
    WAVEFORMATEX *pWAV = NULL;

    if(!pType) {
        CHECK_HR(hr = E_POINTER);
    }

    pType->majortype = MEDIATYPE_Audio;
    pType->subtype = MEDIASUBTYPE_PCM;
    pType->lSampleSize = 0;
    pType->bFixedSizeSamples = TRUE;
    pType->bTemporalCompression = FALSE;
    pType->formattype = FORMAT_WaveFormatEx;

    CHECK_HR(hr = ::MoInitMediaType(pType, sizeof(WAVEFORMATEX)));

    pWAV = (WAVEFORMATEX*)pType->pbFormat;
    pWAV->wFormatTag = WAVE_FORMAT_PCM;
    pWAV->nChannels = 1;
    pWAV->nSamplesPerSec = sampleRate;
    pWAV->nBlockAlign = cChannels * (bitsPerSample >> 3);
    pWAV->nAvgBytesPerSec = pWAV->nBlockAlign * pWAV->nSamplesPerSec;
    pWAV->wBitsPerSample = bitsPerSample;
    pWAV->cbSize = 0;

bail:
    return hr;
}
OpenPOWER on IntegriCloud