1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 /*
19 ------------------------------------------------------------------------------
20
21 PacketVideo Corp.
22 MP3 Decoder Library
23
24 Filename: pvmp3_decoder.cpp
25
26 Date: 09/25/2007
27
28 ------------------------------------------------------------------------------
29 REVISION HISTORY
30
31
32 Description:
33
34 ------------------------------------------------------------------------------
35
36
37 ------------------------------------------------------------------------------
38 FUNCTION DESCRIPTION
39
40 Entry point to the mp3 library
41
42 ------------------------------------------------------------------------------
43 REQUIREMENTS
44
45
46 ------------------------------------------------------------------------------
47 REFERENCES
48
49 ------------------------------------------------------------------------------
50 PSEUDO-CODE
51
52 ------------------------------------------------------------------------------
53 */
54
55
56 /*----------------------------------------------------------------------------
57 ; INCLUDES
58 ----------------------------------------------------------------------------*/
59
60 #include "pvmp3_decoder.h"
61 #include "oscl_error_codes.h"
62 #include "oscl_exception.h"
63 #include "pvmp3_framedecoder.h"
64 #include "pvmp3_seek_synch.h"
65
66
67 /*----------------------------------------------------------------------------
68 ; MACROS
69 ; Define module specific macros here
70 ----------------------------------------------------------------------------*/
71
72
73 /*----------------------------------------------------------------------------
74 ; DEFINES
75 ; Include all pre-processor statements here. Include conditional
76 ; compile variables also.
77 ----------------------------------------------------------------------------*/
78
79 /*----------------------------------------------------------------------------
80 ; LOCAL FUNCTION DEFINITIONS
81 ; Function Prototype declaration
82 ----------------------------------------------------------------------------*/
83
84 /*----------------------------------------------------------------------------
85 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
86 ; Variable declaration - defined here and used outside this module
87 ----------------------------------------------------------------------------*/
88
89 /*----------------------------------------------------------------------------
90 ; EXTERNAL FUNCTION REFERENCES
91 ; Declare functions defined elsewhere and referenced in this module
92 ----------------------------------------------------------------------------*/
93
94 /*----------------------------------------------------------------------------
95 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
96 ; Declare variables used in this module but defined elsewhere
97 ----------------------------------------------------------------------------*/
98
99 /*----------------------------------------------------------------------------
100 ; FUNCTION CODE
101 ----------------------------------------------------------------------------*/
102
103
104 #define KMP3_MAX_STREAMING_BUFFER_SIZE BUFSIZE
105
106 #define KCAI_CODEC_INIT_FAILURE -1
107
108 // Use default DLL entry point
109 #include "oscl_dll.h"
OSCL_DLL_ENTRY_POINT_DEFAULT()110 OSCL_DLL_ENTRY_POINT_DEFAULT()
111
112
113 OSCL_EXPORT_REF CPvMP3_Decoder *CPvMP3_Decoder::NewL()
114 {
115 CPvMP3_Decoder *dec = new CPvMP3_Decoder;
116
117 if (dec == NULL)
118 {
119 OSCL_LEAVE(OsclErrNoMemory);
120 }
121 else
122 {
123 dec->ConstructL();
124 }
125
126 return dec;
127 }
128
129
ConstructL()130 OSCL_EXPORT_REF void CPvMP3_Decoder::ConstructL()
131 {
132 // Initialize member variables
133 pMem = NULL;
134 iInputBuf = NULL;
135 iOutputBuf = NULL;
136 }
137
138
139 /*
140 -----------------------------------------------------------------------------
141
142 CPvMP3_Decoder
143
144 ~CPvMP3_Decoder
145
146 Empty decoder destructor.
147
148 Parameters: none
149
150 Return Values: none
151
152 -----------------------------------------------------------------------------
153 */
~CPvMP3_Decoder()154 OSCL_EXPORT_REF CPvMP3_Decoder::~CPvMP3_Decoder()
155 {
156 if (pMem != NULL)
157 {
158 OSCL_ARRAY_DELETE(pMem);
159 pMem = NULL;
160 }
161
162 if (iAllocateInputBuffer && iInputBuf)
163 {
164 OSCL_ARRAY_DELETE(iInputBuf);
165 iInputBuf = NULL;
166 }
167
168 if (iAllocateOutputBuffer && iOutputBuf)
169 {
170 OSCL_ARRAY_DELETE(iOutputBuf);
171 iOutputBuf = NULL;
172 }
173 }
174
175
176 /*
177 -----------------------------------------------------------------------------
178
179 CPvMP3_Decoder
180
181 StartL
182
183 Start decoder object. Initialize codec status.
184
185 Parameters: none
186
187 Return Values: status
188
189 -----------------------------------------------------------------------------
190 */
191
StartL(tPVMP3DecoderExternal * pExt,bool aAllocateInputBuffer,bool aAllocateOutputBuffer,bool crcEnabler,e_equalization equalizType)192 OSCL_EXPORT_REF int32 CPvMP3_Decoder::StartL(tPVMP3DecoderExternal * pExt,
193 bool aAllocateInputBuffer,
194 bool aAllocateOutputBuffer,
195 bool crcEnabler,
196 e_equalization equalizType)
197 {
198
199 iAllocateInputBuffer = aAllocateInputBuffer;
200 iAllocateOutputBuffer = aAllocateOutputBuffer;
201
202 if (iAllocateInputBuffer)
203 {
204 iInputBuf = OSCL_ARRAY_NEW(uint8, KMP3_MAX_STREAMING_BUFFER_SIZE);
205 if (iInputBuf == NULL)
206 {
207 return KCAI_CODEC_INIT_FAILURE;
208 }
209 // pExt->inputBufferMaxLength = KMP3_MAX_STREAMING_BUFFER_SIZE;
210 pExt->inputBufferMaxLength = 512;
211 }
212 else
213 {
214 iInputBuf = NULL;
215 pExt->inputBufferMaxLength = 0;
216 }
217 pExt->pInputBuffer = iInputBuf;
218
219 if (iAllocateOutputBuffer)
220 {
221 iOutputBuf = OSCL_ARRAY_NEW(int16, (KMP3_MAX_OUTPUT_SIZE >> 1));
222
223 if (iOutputBuf == NULL)
224 {
225 return KCAI_CODEC_INIT_FAILURE;
226 }
227
228 pExt->outputFrameSize = (KMP3_MAX_OUTPUT_SIZE >> 1);
229 }
230 else
231 {
232 iOutputBuf = NULL;
233 pExt->outputFrameSize = 0;
234
235 }
236 pExt->pOutputBuffer = iOutputBuf;
237
238 pExt->crcEnabled = crcEnabler;
239 pExt->equalizerType = equalizType; /* Dynamically enable equalizing type */
240
241 int32 memreq = pvmp3_decoderMemRequirements();
242
243 pMem = OSCL_ARRAY_NEW(uint8 , memreq);
244
245 if (pMem == 0)
246 {
247 return KCAI_CODEC_INIT_FAILURE;
248 }
249
250 pvmp3_InitDecoder(pExt, pMem);
251
252
253 return SUCCESS;
254 }
255
256
257 /*
258 -----------------------------------------------------------------------------
259
260 CPvMP3_Decoder
261
262 ExecuteL
263
264 Execute decoder object. Read one encoded mp3 frame from the input
265 stream, decode it and write the decoded frame to output stream.
266
267 Parameters:
268 tPVMP3DecoderExternal * pExt, pointer to decoder external state variables
269
270 Return Values: status
271
272
273 -----------------------------------------------------------------------------
274 */
275
ExecuteL(tPVMP3DecoderExternal * pExt)276 OSCL_EXPORT_REF int32 CPvMP3_Decoder::ExecuteL(tPVMP3DecoderExternal * pExt)
277 {
278 ERROR_CODE errorCode = NO_DECODING_ERROR;
279 int32 status;
280
281 errorCode = pvmp3_framedecoder(pExt, pMem);
282
283
284 switch (errorCode)
285 {
286 case NO_DECODING_ERROR:
287
288 status = MP3DEC_SUCCESS;
289 break;
290
291 case NO_ENOUGH_MAIN_DATA_ERROR:
292
293 status = MP3DEC_INCOMPLETE_FRAME;
294 break;
295
296 case OUTPUT_BUFFER_TOO_SMALL:
297
298 status = MP3DEC_OUTPUT_BUFFER_TOO_SMALL;
299 break;
300
301 case UNSUPPORTED_LAYER:
302 case UNSUPPORTED_FREE_BITRATE:
303 case CHANNEL_CONFIG_ERROR:
304 case SYNTHESIS_WINDOW_ERROR:
305 case SIDE_INFO_ERROR:
306 case HUFFMAN_TABLE_ERROR:
307 case SYNCH_LOST_ERROR:
308 default:
309
310 status = MP3DEC_INVALID_FRAME;
311 break;
312
313 }
314
315
316 return status;
317 }
318
319 /*
320 -----------------------------------------------------------------------------
321
322 CPvMP3_Decoder
323
324 StopL
325
326 Stop decoder object. Flush out last frame
327
328 Parameters:
329 tPVMP3DecoderExternal * pExt, pointer to decoder external state variables
330
331
332 Return Values: none
333
334 -----------------------------------------------------------------------------
335 */
StopL()336 OSCL_EXPORT_REF void CPvMP3_Decoder::StopL()
337 {
338 pvmp3_resetDecoder(pMem);
339 }
340
341 /*
342 -----------------------------------------------------------------------------
343
344 CPvMP3_Decoder
345
346 ResetDecoderL
347
348 Stop decoder object. Reset decoder.
349
350 Parameters:
351 tPVMP3DecoderExternal * pExt, pointer to decoder external state variables
352
353
354 Return Values: status
355
356 -----------------------------------------------------------------------------
357 */
ResetDecoderL()358 OSCL_EXPORT_REF void CPvMP3_Decoder::ResetDecoderL()
359 {
360
361 pvmp3_resetDecoder(pMem);
362 }
363
364
365 /*
366 -----------------------------------------------------------------------------
367
368 CPvMP3_Decoder
369
370 TerminateDecoderL
371
372 Stop decoder object. close decoder.
373
374 Parameters: none
375
376 Return Values: none
377
378 -----------------------------------------------------------------------------
379 */
TerminateDecoderL()380 OSCL_EXPORT_REF void CPvMP3_Decoder::TerminateDecoderL()
381 {
382 if (pMem != NULL)
383 {
384 OSCL_ARRAY_DELETE(pMem);
385 pMem = NULL;
386 }
387
388 if (iAllocateInputBuffer && iInputBuf)
389 {
390 OSCL_ARRAY_DELETE(iInputBuf);
391 iInputBuf = NULL;
392 }
393
394 if (iAllocateOutputBuffer && iOutputBuf)
395 {
396 OSCL_ARRAY_DELETE(iOutputBuf);
397 iOutputBuf = NULL;
398 }
399 }
400
401
402
403 /*
404 -----------------------------------------------------------------------------
405
406 CPvMP3_Decoder
407
408 SeekMp3Synchronization
409
410 Utility to seek for the mp3 frames boundaries.
411
412 Parameters:
413 tPVMP3DecoderExternal * pExt, pointer to decoder external state variables
414
415 Return Values: status
416
417 -----------------------------------------------------------------------------
418 */
SeekMp3Synchronization(tPVMP3DecoderExternal * pExt)419 OSCL_EXPORT_REF int32 CPvMP3_Decoder::SeekMp3Synchronization(
420 tPVMP3DecoderExternal * pExt)
421 {
422 ERROR_CODE errorCode = NO_DECODING_ERROR;
423
424 errorCode = pvmp3_frame_synch(pExt, pMem);
425
426 if (errorCode != NO_DECODING_ERROR)
427 {
428 return 1;
429 }
430 else
431 {
432 return 0;
433 }
434 }
435
436
437
438
439