2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <unique_ptr.h>
19 #include <interf_enc.h>
20 #include <FBaseColHashMap.h>
21 #include <FBaseInteger.h>
22 #include <FMediaTypes.h>
23 #include <FMediaAudioTypes.h>
24 #include <FBaseSysLog.h>
25 #include "FMedia_AmrEncoder.h"
27 using namespace Tizen::Base;
28 using namespace Tizen::Io;
29 using namespace Tizen::Base::Collection;
32 namespace Tizen { namespace Media
36 _AmrEncoder_CreateInstance(void)
38 return new (std::nothrow) _AmrEncoder();
41 _AmrEncoder::_AmrEncoder(void)
48 _AmrEncoder::~_AmrEncoder(void)
52 Encoder_Interface_exit(__pEnc);
58 _AmrEncoder::Construct(const Tizen::Base::Collection::HashMap* pOption)
62 Integer* pValue = null;
66 SysTryReturnResult(NID_MEDIA, __pEnc == null, E_INVALID_STATE, "already constructed");
70 // The initialization values are given in the Hashmap
71 std::unique_ptr<IMapEnumerator> pMapEnum(pOption->GetMapEnumeratorN());
72 if (pMapEnum.get() != null)
74 while (pMapEnum.get()->MoveNext() == E_SUCCESS)
76 pKey = static_cast<Integer*>(pMapEnum.get()->GetKey());
77 pValue = static_cast<Integer*>(pMapEnum.get()->GetValue());
79 if (pKey != null && pValue != null)
82 value = pValue->ToInt();
86 case MEDIA_PROPERTY_AUDIO_CHANNEL_TYPE:
87 SysTryCatch(NID_MEDIA, value == AUDIO_CHANNEL_TYPE_MONO || value == AUDIO_CHANNEL_TYPE_STEREO,
88 r = E_OUT_OF_RANGE, E_OUT_OF_RANGE,
89 "[%s] Audio Channel Type is out of range :%d", GetErrorMessage(E_OUT_OF_RANGE), value);
92 case MEDIA_PROPERTY_AUDIO_SAMPLE_RATE:
93 SysTryCatch(NID_MEDIA, value == 8000, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE,
94 "[%s] Audio sampleRate is out of range :%d", GetErrorMessage(E_OUT_OF_RANGE), value);
97 case MEDIA_PROPERTY_AUDIO_BIT_RATE:
98 SysTryCatch(NID_MEDIA, (value > 0), r = E_OUT_OF_RANGE, E_OUT_OF_RANGE,
99 "[%s] Audio bitrate is out of range :%d", GetErrorMessage(E_OUT_OF_RANGE), value);
100 __encBitrate = GetBitrateMode(value);
101 SysTryCatch(NID_MEDIA, __encBitrate >= 0, r = E_OUT_OF_RANGE, E_OUT_OF_RANGE,
102 "[%s] Audio bitrate is out of range %d %d", GetErrorMessage(E_OUT_OF_RANGE), value, __encBitrate);
113 //intialising using opencore amr
114 __pEnc = Encoder_Interface_init(0);
115 SysTryCatch(NID_MEDIA, __pEnc != null, r = E_SYSTEM, E_SYSTEM,
116 "[%s] Encoder init failed", GetErrorMessage(E_SYSTEM));
124 Encoder_Interface_exit(__pEnc);
131 _AmrEncoder::Encode(const byte* srcBuf, int& srcBufLength, byte*& dstBuf, int& dstBufLength)
133 result r = E_SUCCESS;
135 byte outBuf[DECODED_FRAME_SIZE];
136 memset(outBuf, 0, sizeof(outBuf));
137 SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
139 //Partial frame encoding is supported by the open core amr_nb library . So no need of checking sourcebuf length.
140 SysTryCatch(NID_MEDIA, srcBuf != null && dstBuf != null
141 && srcBufLength > 0 && dstBufLength > 0,
142 r = E_INVALID_ARG, E_INVALID_ARG,
143 "[E_INVALID_ARG] Invalid argument is used. 0x%x %d 0x%x %d",
144 srcBuf, srcBufLength, dstBuf, dstBufLength);
146 // Encoding the Audio packet using the amr open core
147 // Accomodating for destinationBuf less than encoded frame size.
148 if (dstBufLength >= DECODED_FRAME_SIZE )
150 res = Encoder_Interface_Encode(__pEnc, __encBitrate, (const short*)srcBuf, dstBuf, 0);
154 res = Encoder_Interface_Encode(__pEnc, __encBitrate, (const short*)srcBuf,(byte*) outBuf, 0);
157 SysTryCatch(NID_MEDIA, res > 0, r = E_SYSTEM, E_SYSTEM,
158 "[%s] Amr Encoding Failed:%d", GetErrorMessage(E_SYSTEM), res);
159 SysTryCatch(NID_MEDIA, res <= dstBufLength, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
160 "[%s] Amr Encoding Failed %d %d", GetErrorMessage(E_OUT_OF_MEMORY), res, dstBufLength);
162 if (dstBufLength < DECODED_FRAME_SIZE )
164 memcpy(dstBuf, outBuf, res);
166 srcBufLength = DECODED_FRAME_SIZE;
175 _AmrEncoder::Reset(void)
177 result r = E_SUCCESS;
179 SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
184 _AmrEncoder::GetBitrateMode(int bitrate)
186 // make the correspondance between bitrate and mode
187 static struct _AmrBitRate
203 for (unsigned int i = 0; i < sizeof(map)/sizeof(map[0]); i++)
205 if (map[i].key == bitrate)