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