1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_NDEBUG 1
18#include <utils/Log.h>
19#include "AudioMixer.h"
20#include "VideoEditorResampler.h"
21
22namespace android {
23
24struct VideoEditorResampler : public AudioBufferProvider {
25
26    public:
27
28        virtual status_t getNextBuffer(Buffer* buffer);
29        virtual void releaseBuffer(Buffer* buffer);
30
31    enum { //Sampling freq
32     kFreq8000Hz = 8000,
33     kFreq11025Hz = 11025,
34     kFreq12000Hz = 12000,
35     kFreq16000Hz = 16000,
36     kFreq22050Hz = 22050,
37     kFreq240000Hz = 24000,
38     kFreq32000Hz = 32000,
39     kFreq44100 = 44100,
40     kFreq48000 = 48000,
41    };
42
43    AudioResampler *mResampler;
44    int16_t* mInput;
45    int nbChannels;
46    int nbSamples;
47    M4OSA_Int32 outSamplingRate;
48    M4OSA_Int32 inSamplingRate;
49
50    int16_t *mTmpInBuffer;
51};
52
53#define MAX_SAMPLEDURATION_FOR_CONVERTION 40 //ms
54
55status_t VideoEditorResampler::getNextBuffer(AudioBufferProvider::Buffer *pBuffer) {
56
57    uint32_t dataSize = pBuffer->frameCount * this->nbChannels * sizeof(int16_t);
58    mTmpInBuffer = (int16_t*)malloc(dataSize);
59    memcpy(mTmpInBuffer, this->mInput, dataSize);
60    pBuffer->raw = (void*)mTmpInBuffer;
61
62    return OK;
63}
64
65void VideoEditorResampler::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) {
66
67    if(pBuffer->raw != NULL) {
68        free(pBuffer->raw);
69        pBuffer->raw = NULL;
70        mTmpInBuffer = NULL;
71    }
72    pBuffer->frameCount = 0;
73}
74
75extern "C" {
76
77M4OSA_Context  LVAudioResamplerCreate(M4OSA_Int32 bitDepth, M4OSA_Int32 inChannelCount,
78                                     M4OSA_Int32 sampleRate, M4OSA_Int32 quality) {
79
80    VideoEditorResampler *context = new VideoEditorResampler();
81    context->mResampler = AudioResampler::create(
82        bitDepth, inChannelCount, sampleRate, AudioResampler::DEFAULT);
83    if (context->mResampler == NULL) {
84        return NULL;
85    }
86    context->mResampler->setSampleRate(android::VideoEditorResampler::kFreq32000Hz);
87    context->mResampler->setVolume(0x1000, 0x1000);
88    context->nbChannels = inChannelCount;
89    context->outSamplingRate = sampleRate;
90    context->mInput = NULL;
91    context->mTmpInBuffer = NULL;
92
93    return ((M4OSA_Context )context);
94}
95
96
97void LVAudiosetSampleRate(M4OSA_Context resamplerContext, M4OSA_Int32 inSampleRate) {
98
99    VideoEditorResampler *context =
100      (VideoEditorResampler *)resamplerContext;
101    context->mResampler->setSampleRate(inSampleRate);
102    /*
103     * nbSamples is calculated for 40ms worth of data;hence sample rate
104     * is used to calculate the nbSamples
105     */
106    context->inSamplingRate = inSampleRate;
107    // Allocate buffer for maximum allowed number of samples.
108    context->mInput = (int16_t*)malloc( (inSampleRate * MAX_SAMPLEDURATION_FOR_CONVERTION *
109                                   context->nbChannels * sizeof(int16_t)) / 1000);
110}
111
112void LVAudiosetVolume(M4OSA_Context resamplerContext, M4OSA_Int16 left, M4OSA_Int16 right) {
113
114    VideoEditorResampler *context =
115       (VideoEditorResampler *)resamplerContext;
116    context->mResampler->setVolume(left,right);
117}
118
119void LVDestroy(M4OSA_Context resamplerContext) {
120
121    VideoEditorResampler *context =
122       (VideoEditorResampler *)resamplerContext;
123
124    if (context->mTmpInBuffer != NULL) {
125        free(context->mTmpInBuffer);
126        context->mTmpInBuffer = NULL;
127    }
128
129    if (context->mInput != NULL) {
130        free(context->mInput);
131        context->mInput = NULL;
132    }
133
134    if (context->mResampler != NULL) {
135        delete context->mResampler;
136        context->mResampler = NULL;
137    }
138
139    if (context != NULL) {
140        delete context;
141        context = NULL;
142    }
143}
144
145void LVAudioresample_LowQuality(M4OSA_Int16* out, M4OSA_Int16* input,
146                                     M4OSA_Int32 outFrameCount, M4OSA_Context resamplerContext) {
147
148    VideoEditorResampler *context =
149      (VideoEditorResampler *)resamplerContext;
150    int32_t *pTmpBuffer = NULL;
151
152    context->nbSamples = (context->inSamplingRate * outFrameCount) / context->outSamplingRate;
153    memcpy(context->mInput,input,(context->nbSamples * context->nbChannels * sizeof(int16_t)));
154
155    /*
156     SRC module always gives stereo output, hence 2 for stereo audio
157    */
158    pTmpBuffer = (int32_t*)malloc(outFrameCount * 2 * sizeof(int32_t));
159    memset(pTmpBuffer, 0x00, outFrameCount * 2 * sizeof(int32_t));
160
161    context->mResampler->resample((int32_t *)pTmpBuffer,
162       (size_t)outFrameCount, (VideoEditorResampler *)resamplerContext);
163    // Convert back to 16 bits
164    AudioMixer::ditherAndClamp((int32_t*)out, pTmpBuffer, outFrameCount);
165    free(pTmpBuffer);
166    pTmpBuffer = NULL;
167}
168
169}
170
171} //namespace android
172