blob: 2a21472604c0efb4308c38de15944bc8b233979d [file] [log] [blame]
Andreas Hubera30d4002009-12-08 15:40:06 -08001/*
2 * Copyright (C) 2009 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#include "AMRWBDecoder.h"
18
19#include "pvamrwbdecoder.h"
20
21#include <media/stagefright/MediaBufferGroup.h>
22#include <media/stagefright/MediaDebug.h>
23#include <media/stagefright/MediaDefs.h>
24#include <media/stagefright/MetaData.h>
25
26namespace android {
27
28static const int32_t kNumSamplesPerFrame = 320;
29static const int32_t kSampleRate = 16000;
30
31AMRWBDecoder::AMRWBDecoder(const sp<MediaSource> &source)
32 : mSource(source),
33 mStarted(false),
34 mBufferGroup(NULL),
35 mState(NULL),
36 mDecoderBuf(NULL),
37 mDecoderCookie(NULL),
38 mAnchorTimeUs(0),
39 mNumSamplesOutput(0),
40 mInputBuffer(NULL) {
41}
42
43AMRWBDecoder::~AMRWBDecoder() {
44 if (mStarted) {
45 stop();
46 }
47}
48
49status_t AMRWBDecoder::start(MetaData *params) {
50 CHECK(!mStarted);
51
52 mBufferGroup = new MediaBufferGroup;
53 mBufferGroup->add_buffer(
54 new MediaBuffer(kNumSamplesPerFrame * sizeof(int16_t)));
55
56 int32_t memReq = pvDecoder_AmrWbMemRequirements();
57 mDecoderBuf = malloc(memReq);
58
59 pvDecoder_AmrWb_Init(&mState, mDecoderBuf, &mDecoderCookie);
60
61 mSource->start();
62
63 mAnchorTimeUs = 0;
64 mNumSamplesOutput = 0;
65 mStarted = true;
66
67 return OK;
68}
69
70status_t AMRWBDecoder::stop() {
71 CHECK(mStarted);
72
73 if (mInputBuffer) {
74 mInputBuffer->release();
75 mInputBuffer = NULL;
76 }
77
78 delete mBufferGroup;
79 mBufferGroup = NULL;
80
81 free(mDecoderBuf);
82 mDecoderBuf = NULL;
83
84 mSource->stop();
85
86 mStarted = false;
87
88 return OK;
89}
90
91sp<MetaData> AMRWBDecoder::getFormat() {
92 sp<MetaData> srcFormat = mSource->getFormat();
93
94 int32_t numChannels;
95 int32_t sampleRate;
Andreas Hubera30d4002009-12-08 15:40:06 -080096
97 CHECK(srcFormat->findInt32(kKeyChannelCount, &numChannels));
98 CHECK_EQ(numChannels, 1);
99
100 CHECK(srcFormat->findInt32(kKeySampleRate, &sampleRate));
101 CHECK_EQ(sampleRate, kSampleRate);
102
Andreas Hubera30d4002009-12-08 15:40:06 -0800103 sp<MetaData> meta = new MetaData;
104 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
105 meta->setInt32(kKeyChannelCount, numChannels);
106 meta->setInt32(kKeySampleRate, sampleRate);
Andreas Huber85adf5e2009-12-11 11:27:02 -0800107
108 int64_t durationUs;
109 if (srcFormat->findInt64(kKeyDuration, &durationUs)) {
110 meta->setInt64(kKeyDuration, durationUs);
111 }
Andreas Hubera30d4002009-12-08 15:40:06 -0800112
Andreas Huber71674722010-03-30 13:50:38 -0700113 meta->setCString(kKeyDecoderComponent, "AMRWBDecoder");
114
Andreas Hubera30d4002009-12-08 15:40:06 -0800115 return meta;
116}
117
118static size_t getFrameSize(unsigned FT) {
119 static const size_t kFrameSizeWB[9] = {
120 132, 177, 253, 285, 317, 365, 397, 461, 477
121 };
122
123 size_t frameSize = kFrameSizeWB[FT];
124
125 // Round up bits to bytes and add 1 for the header byte.
126 frameSize = (frameSize + 7) / 8 + 1;
127
128 return frameSize;
129}
130
131status_t AMRWBDecoder::read(
132 MediaBuffer **out, const ReadOptions *options) {
133 status_t err;
134
135 *out = NULL;
136
137 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -0700138 ReadOptions::SeekMode seekMode;
139 if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
Andreas Hubera30d4002009-12-08 15:40:06 -0800140 CHECK(seekTimeUs >= 0);
141
142 mNumSamplesOutput = 0;
143
144 if (mInputBuffer) {
145 mInputBuffer->release();
146 mInputBuffer = NULL;
147 }
148 } else {
149 seekTimeUs = -1;
150 }
151
152 if (mInputBuffer == NULL) {
153 err = mSource->read(&mInputBuffer, options);
154
155 if (err != OK) {
156 return err;
157 }
158
159 int64_t timeUs;
160 if (mInputBuffer->meta_data()->findInt64(kKeyTime, &timeUs)) {
161 mAnchorTimeUs = timeUs;
162 mNumSamplesOutput = 0;
163 } else {
164 // We must have a new timestamp after seeking.
165 CHECK(seekTimeUs < 0);
166 }
167 }
168
169 MediaBuffer *buffer;
170 CHECK_EQ(mBufferGroup->acquire_buffer(&buffer), OK);
171
172 const uint8_t *inputPtr =
173 (const uint8_t *)mInputBuffer->data() + mInputBuffer->range_offset();
174
175 int16 mode = ((inputPtr[0] >> 3) & 0x0f);
176 size_t frameSize = getFrameSize(mode);
Andreas Huber85adf5e2009-12-11 11:27:02 -0800177 CHECK(mInputBuffer->range_length() >= frameSize);
Andreas Hubera30d4002009-12-08 15:40:06 -0800178
179 int16 frameType;
180 RX_State rx_state;
181 mime_unsorting(
182 const_cast<uint8_t *>(&inputPtr[1]),
183 mInputSampleBuffer,
184 &frameType, &mode, 1, &rx_state);
185
186 int16_t *outPtr = (int16_t *)buffer->data();
187
188 int16_t numSamplesOutput;
189 pvDecoder_AmrWb(
190 mode, mInputSampleBuffer,
191 outPtr,
192 &numSamplesOutput,
193 mDecoderBuf, frameType, mDecoderCookie);
194
195 CHECK_EQ(numSamplesOutput, kNumSamplesPerFrame);
196
197 for (int i = 0; i < kNumSamplesPerFrame; ++i) {
198 /* Delete the 2 LSBs (14-bit output) */
199 outPtr[i] &= 0xfffC;
200 }
201
202 buffer->set_range(0, numSamplesOutput * sizeof(int16_t));
203
Andreas Huber85adf5e2009-12-11 11:27:02 -0800204 mInputBuffer->set_range(
205 mInputBuffer->range_offset() + frameSize,
206 mInputBuffer->range_length() - frameSize);
207
208 if (mInputBuffer->range_length() == 0) {
209 mInputBuffer->release();
210 mInputBuffer = NULL;
211 }
Andreas Hubera30d4002009-12-08 15:40:06 -0800212
213 buffer->meta_data()->setInt64(
214 kKeyTime,
215 mAnchorTimeUs
216 + (mNumSamplesOutput * 1000000) / kSampleRate);
217
218 mNumSamplesOutput += kNumSamplesPerFrame;
219
220 *out = buffer;
221
222 return OK;
223}
224
225} // namespace android