VideoEditorResampler.cpp revision 1526a3385d36d6e1953c932410548bf9631a0eb3
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
49};
50
51
52status_t VideoEditorResampler::getNextBuffer(AudioBufferProvider::Buffer *pBuffer) {
53
54    pBuffer->raw = (void*)(this->mInput);
55    return OK;
56}
57
58void VideoEditorResampler::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) {
59
60    if(pBuffer->raw != NULL) {
61        pBuffer->raw = NULL;
62    }
63    pBuffer->frameCount = 0;
64}
65
66extern "C" {
67
68M4OSA_Int32 LVAudioResamplerCreate(M4OSA_Int32 bitDepth, M4OSA_Int32 inChannelCount,
69                                     M4OSA_Int32 sampleRate, M4OSA_Int32 quality) {
70
71    VideoEditorResampler *context = new VideoEditorResampler();
72    context->mResampler = AudioResampler::create(
73        bitDepth, inChannelCount, sampleRate, AudioResampler::DEFAULT);
74    if (context->mResampler == NULL) {
75        return NO_MEMORY;
76    }
77    context->mResampler->setSampleRate(32000);
78    context->mResampler->setVolume(0x1000, 0x1000);
79    context->nbChannels = inChannelCount;
80
81    return ((M4OSA_Int32)context);
82}
83
84
85void LVAudiosetSampleRate(M4OSA_Int32 resamplerContext, M4OSA_Int32 inSampleRate) {
86
87    VideoEditorResampler *context =
88      (VideoEditorResampler *)resamplerContext;
89    context->mResampler->setSampleRate(inSampleRate);
90    /*
91     * nbSamples is calculated for 40ms worth of data;hence sample rate
92     * is used to calculate the nbSamples
93     */
94    context->nbSamples = inSampleRate / 25;
95    context->mInput = (int16_t*)malloc(context->nbSamples *
96                                   context->nbChannels * sizeof(int16_t));
97}
98
99void LVAudiosetVolume(M4OSA_Int32 resamplerContext, M4OSA_Int16 left, M4OSA_Int16 right) {
100
101    VideoEditorResampler *context =
102       (VideoEditorResampler *)resamplerContext;
103    context->mResampler->setVolume(left,right);
104}
105
106
107void LVAudioresample_LowQuality(M4OSA_Int16* out, M4OSA_Int16* input,
108                                     M4OSA_Int32 outFrameCount, M4OSA_Int32 resamplerContext) {
109
110    VideoEditorResampler *context =
111      (VideoEditorResampler *)resamplerContext;
112    int32_t *pTmpBuffer = NULL;
113    memcpy(context->mInput,input,(context->nbSamples * context->nbChannels * sizeof(int16_t)));
114    /*
115     SRC module always gives stereo output, hence 2 for stereo audio
116    */
117    pTmpBuffer = (int32_t*)malloc(outFrameCount * 2 * sizeof(int32_t));
118    memset(pTmpBuffer, 0x00, outFrameCount * 2 * sizeof(int32_t));
119
120    context->mResampler->resample((int32_t *)pTmpBuffer,
121       (size_t)outFrameCount, (VideoEditorResampler *)resamplerContext);
122    // Convert back to 16 bits
123    AudioMixer::ditherAndClamp((int32_t*)out, pTmpBuffer, outFrameCount);
124    free(pTmpBuffer);
125    pTmpBuffer = NULL;
126}
127
128}
129
130} //namespace android
131