Tizen 2.0 Release
[framework/osp/media.git] / src / FMedia_VideoDecoderImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 #include <stdio.h>
19 #include <FBaseColArrayListT.h>
20 #include <FBaseColHashMap.h>
21 #include <FBaseColHashMapT.h>
22 #include <FMediaTypes.h>
23 #include <FBaseSysLog.h>
24 #include "FMedia_IVideoDecoder.h"
25 #include "FMedia_VideoDecoderImpl.h"
26 #include "FMedia_CodecFactory.h"
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30
31 namespace Tizen { namespace Media
32 {
33
34 _VideoDecoderImpl::_VideoDecoderImpl(void)
35 {
36         __pDec = null;
37 }
38
39 _VideoDecoderImpl::~_VideoDecoderImpl(void)
40 {
41         if (__pDec != null)
42         {
43                 delete __pDec;
44         }
45 }
46
47 result
48 _VideoDecoderImpl::Construct(CodecType type, const Tizen::Base::Collection::HashMap* pOption)
49 {
50         _IVideoDecoder* pVideoDecoder = null;
51         _CodecFactory* pCodecFactory = null;
52         result r = E_SUCCESS;
53
54         SysTryReturnResult(NID_MEDIA, !__pDec, E_INVALID_STATE, "already constructed");
55         pCodecFactory = _CodecFactory::GetInstance();
56         SysTryCatch(NID_MEDIA, null != pCodecFactory, r = E_SYSTEM, E_SYSTEM,
57                            "Construct Failed ");
58         r = pCodecFactory->CreateCodecInstance(type, pVideoDecoder);
59         SysTryCatch(NID_MEDIA, pVideoDecoder != null, , r, "Construct Failed ");
60         __pDec = pVideoDecoder;
61         r = __pDec->Construct(pOption);
62         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "Construct Failed ");
63         return r;
64
65 CATCH:
66         if (__pDec != null)
67         {
68                 delete __pDec;
69                 __pDec = null;
70         }
71         return r;
72 }
73
74 result
75 _VideoDecoderImpl::Probe(const Tizen::Base::ByteBuffer& srcBuf, int& width, int& height, MediaPixelFormat& pixelFormat)
76 {
77         result r = E_SUCCESS;
78         const byte* pSrcByteBuf = null;
79         int srcByteBufSize = 0;
80
81         SysTryReturnResult(NID_MEDIA, __pDec, E_INVALID_STATE, "not constructed");
82         pSrcByteBuf = srcBuf.GetPointer() + srcBuf.GetPosition();
83         SysTryCatch(NID_MEDIA, null != pSrcByteBuf, r = E_INVALID_ARG, E_INVALID_ARG,
84                            "[E_INVALID_ARG] Probe Failed ");
85         srcByteBufSize = srcBuf.GetRemaining();
86
87         SysTryCatch(NID_MEDIA, srcByteBufSize > 0, r = E_INVALID_ARG, E_INVALID_ARG,
88                            "[E_INVALID_ARG] Probe Failed ");
89         r = __pDec->Probe(pSrcByteBuf, srcByteBufSize, width, height, pixelFormat);
90         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "Probe Failed ");
91         return r;
92
93 CATCH:
94         return r;
95 }
96
97 result
98 _VideoDecoderImpl::Probe(const byte* pSrcBuf, int srcBugLength, int& width, int& height, MediaPixelFormat& pixelFormat)
99 {
100         result r = E_SUCCESS;
101
102         SysTryReturnResult(NID_MEDIA, __pDec, E_INVALID_STATE, "not constructed");
103         r = __pDec->Probe(pSrcBuf, srcBugLength, width, height, pixelFormat);
104         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "Probe Failed ");
105         return r;
106
107 CATCH:
108         return r;
109 }
110
111 result
112 _VideoDecoderImpl::Decode(Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& dstBuf, bool& gotFrame)
113 {
114         result r = E_INVALID_STATE;
115         const byte* pSrcByte = null;
116         byte* pDstByte = null;
117         int srcByteSize = 0;
118         int dstByteSize = 0;
119         int offset = 0;
120
121         SysTryReturnResult(NID_MEDIA, __pDec, E_INVALID_STATE, "not constructed");
122         pSrcByte = srcBuf.GetPointer() + srcBuf.GetPosition();
123         pDstByte = (byte*) dstBuf.GetPointer() + dstBuf.GetPosition();
124         SysTryCatch(NID_MEDIA, (null != pDstByte && null != pSrcByte), r = E_INVALID_ARG, E_INVALID_ARG,
125                            "[E_INVALID_ARG] Decode Failed ");
126         srcByteSize = srcBuf.GetRemaining();
127         dstByteSize = dstBuf.GetRemaining();
128         SysTryCatch(NID_MEDIA, (srcByteSize > 0 && dstByteSize > 0), r = E_INVALID_ARG, E_INVALID_ARG,
129                            "[E_INVALID_ARG] Decode Failed ");
130
131         r = __pDec->Decode(pSrcByte, srcByteSize, pDstByte, dstByteSize, gotFrame);
132         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
133                            "Decode Failed ");
134
135         offset = srcBuf.GetPosition();
136         if (offset + srcByteSize > srcBuf.GetLimit())
137         {
138                 srcBuf.SetPosition(srcBuf.GetLimit());
139         }
140         else
141         {
142                 srcBuf.SetPosition(offset + srcByteSize);
143         }
144
145         offset = dstBuf.GetPosition();
146         if (offset + dstByteSize > dstBuf.GetLimit())
147         {
148                 dstBuf.SetPosition(dstBuf.GetLimit());
149         }
150         else
151         {
152                 dstBuf.SetPosition(offset + dstByteSize);
153         }
154
155         return r;
156
157 CATCH:
158         return r;
159 }
160
161 result
162 _VideoDecoderImpl::Decode(const byte* pSrcBuf, int& srcBufLength, byte* pDstBuf, int& dstBufLength, bool& gotFrame)
163 {
164         result r = E_SUCCESS;
165
166         SysTryReturnResult(NID_MEDIA, __pDec, E_INVALID_STATE, "not constructed");
167         r = __pDec->Decode(pSrcBuf, srcBufLength, pDstBuf, dstBufLength, gotFrame);
168         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Decode Failed", GetErrorMessage(r));
169         return r;
170
171 CATCH:
172         return r;
173 }
174
175 result
176 _VideoDecoderImpl::Reset(void)
177 {
178         result r = E_SUCCESS;
179
180         SysTryReturnResult(NID_MEDIA, __pDec, E_INVALID_STATE, "not constructed");
181
182         r = __pDec->Reset();
183         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Reset Failed", GetErrorMessage(r));
184         return r;
185
186 CATCH:
187         return r;
188 }
189
190 result
191 _VideoDecoderImpl::GetValue(MediaPropertyType key, int& value) const
192 {
193         result r = E_SUCCESS;
194
195         SysTryReturnResult(NID_MEDIA, __pDec, E_INVALID_STATE, "not constructed");
196         r = __pDec->GetValue(key, value);
197         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "Probe Failed ");
198         return r;
199
200 CATCH:
201         return r;
202 }
203
204 result
205 _VideoDecoderImpl::GetValue(MediaPropertyType key, float& value) const
206 {
207         result r = E_SUCCESS;
208
209         SysTryReturnResult(NID_MEDIA, __pDec, E_INVALID_STATE, "not constructed");
210         r = __pDec->GetValue(key, value);
211         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] GetValue Failed", GetErrorMessage(r));
212         return r;
213
214 CATCH:
215         return r;
216 }
217
218 Tizen::Base::Collection::IListT<MediaPropertyType>*
219 _VideoDecoderImpl::GetSupportedPropertyListN(void) const
220 {
221         SysTryReturn(NID_MEDIA, __pDec, null, E_INVALID_STATE, "[E_INVALID_STATE] not constructed");
222
223         return __pDec->GetSupportedPropertyListN();
224 }
225
226 bool
227 _VideoDecoderImpl::IsPropertySupported(MediaPropertyType key) const
228 {
229         SysTryReturn(NID_MEDIA, __pDec, false, E_INVALID_STATE, "[E_INVALID_STATE] not constructed");
230         return __pDec->IsPropertySupported(key);
231 }
232
233 Tizen::Base::Collection::IListT<CodecType>*
234 _VideoDecoderImpl::GetSupportedCodecListN(void)
235 {
236         _CodecFactory* pFactory = null;
237         Tizen::Base::Collection::IListT<CodecType>* pList = null;
238
239         pFactory = _CodecFactory::GetInstance();
240         SysTryReturn(NID_MEDIA, pFactory != null, null, E_OUT_OF_MEMORY, "[%s] Propagated.",
241                                 GetErrorMessage(GetLastResult()));
242         pList = pFactory->GetSupportedVideoDecoderListN();
243         SysTryReturn(NID_MEDIA, pList != null, null, E_OUT_OF_MEMORY, "[%s] Propagated.",
244                                 GetErrorMessage(GetLastResult()));
245         SetLastResult(E_SUCCESS);
246         return pList;
247 }
248
249 }} // Tizen::Media