summaryrefslogtreecommitdiff
path: root/audio_codec/libraac/raac_decode.c (plain)
blob: 99528f1413cc0abeb53f22d77f7bd67155ac1af3
1#include <stdio.h>
2#include <limits.h>
3//#include <asm/cache.h>
4
5#ifndef __MW__
6#include <inttypes.h>
7#else
8#include <core/types.h>
9#endif
10#include "raac_decode.h"
11#include "aac_decode.h"
12#include "include/rm_parse.h"
13#include "include/ra_depack.h"
14#include "include/ra_decode.h"
15
16#include "include/rm_memory_default.h"
17#include "aacdec.h"
18
19#include "../../amadec/adec-armdec-mgt.h"
20
21#include <android/log.h>
22
23#define LOG_TAG "RaacDecoder"
24#define raac_print(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
25
26#define DefaultReadSize 32*1024
27#define DefaultOutBufSize 370*1024
28
29typedef struct {
30 rm_parser* pParser;
31 ra_depack* pDepack;
32 ra_decode* pDecode;
33 ra_format_info* pRaInfo;
34 rm_packet* pPacket;
35 ra_block* pBlock;
36 rm_stream_header* pHdr;
37 UINT32 usStreamNum;
38} rm_info_t;
39
40#define AUDIO_EXTRA_DATA_SIZE (2048*2)
41
42struct audio_info {
43 int valid;
44 int sample_rate;
45 int channels;
46 int bitrate;
47 int codec_id;
48 int block_align;
49 int extradata_size;
50 char extradata[AUDIO_EXTRA_DATA_SIZE];
51};
52
53struct frame_info {
54 int len;
55 unsigned long offset;/*steam start to here*/
56 unsigned long buffered_len;/*data buffer in dsp,pcm datalen*/
57 int reversed[1];/*for cache aligned 32 bytes*/
58};
59
60cook_IObuf cook_input;
61cook_IObuf cook_output;
62
63struct frame_info cur_frame;
64static unsigned aac_timestamplen = 0;
65static unsigned aac_timestamp = 0;
66static raac_decoder_info_t raac_dec_info ;
67static rm_info_t raac_info ;
68static char file_header[AUDIO_EXTRA_DATA_SIZE];
69#pragma align_to(64,file_header)
70static char *cur_read_ptr = file_header;
71
72void set_timestamp(unsigned val)
73{
74 aac_timestamp = val;
75}
76
77void set_timestamp_len(unsigned val)
78{
79 aac_timestamplen = val;
80}
81
82ra_decode*
83ra_decode_create2(void* pUserError,
84 rm_error_func_ptr fpError,
85 void* pUserMem,
86 rm_malloc_func_ptr fpMalloc,
87 rm_free_func_ptr fpFree)
88{
89 ra_decode* pRet = HXNULL;
90
91 if (fpMalloc && fpFree) {
92 pRet = (ra_decode*) fpMalloc(pUserMem, sizeof(ra_decode));
93
94 if (pRet) {
95 /* Zero out the struct */
96 memset((void*) pRet, 0, sizeof(ra_decode));
97 /* Assign the error function */
98 pRet->fpError = fpError;
99 pRet->pUserError = pUserError;
100 /* Assign the memory functions */
101 pRet->fpMalloc = fpMalloc;
102 pRet->fpFree = fpFree;
103 pRet->pUserMem = pUserMem;
104 }
105 }
106
107 return pRet;
108}
109
110ra_decode*
111ra_decode_create(void* pUserError,
112 rm_error_func_ptr fpError)
113{
114 return ra_decode_create2(pUserError,
115 fpError,
116 HXNULL,
117 rm_memory_default_malloc,
118 rm_memory_default_free);
119}
120
121/* ra_decode_destroy()
122 * Deletes the decoder backend and frontend instances. */
123void
124ra_decode_destroy(ra_decode* pFrontEnd)
125{
126 rm_free_func_ptr fpFree;
127 void* pUserMem;
128
129 if (pFrontEnd && pFrontEnd->fpFree) {
130 /* Save a pointer to fpFree and pUserMem */
131 fpFree = pFrontEnd->fpFree;
132 pUserMem = pFrontEnd->pUserMem;
133
134 if (pFrontEnd->pDecode && pFrontEnd->fpClose) {
135 /* Free the decoder instance and backend */
136 pFrontEnd->fpClose(pFrontEnd->pDecode,
137 pUserMem,
138 fpFree);
139 }
140
141 /* Free the ra_decode struct memory */
142 fpFree(pUserMem, pFrontEnd);
143 }
144}
145
146/* ra_decode_init()
147 * Selects decoder backend with fourCC code.
148 * Calls decoder backend init function with init params.
149 * Returns zero on success, negative result indicates failure. */
150HX_RESULT
151ra_decode_init(ra_decode* pFrontEnd,
152 UINT32 ulFourCC,
153 void* pInitParams,
154 UINT32 ulInitParamsSize,
155 ra_format_info* pStreamInfo)
156{
157 HX_RESULT retVal = HXR_OK;
158
159 /* Assign the backend function pointers */
160 if (ulFourCC == 0x72616163 || /* 'raac' */
161 ulFourCC == 0x72616370) { /* 'racp' */
162 pFrontEnd->fpInit = aac_decode_init;
163 pFrontEnd->fpReset = aac_decode_reset;
164 pFrontEnd->fpConceal = aac_decode_conceal;
165 pFrontEnd->fpDecode = aac_decode_decode;
166 pFrontEnd->fpGetMaxSize = aac_decode_getmaxsize;
167 pFrontEnd->fpGetChannels = aac_decode_getchannels;
168 pFrontEnd->fpGetChannelMask = aac_decode_getchannelmask;
169 pFrontEnd->fpGetSampleRate = aac_decode_getrate;
170 pFrontEnd->fpMaxSamp = aac_decode_getdelay;
171 pFrontEnd->fpClose = aac_decode_close;
172 } else {
173 /* error - codec not supported */
174 raac_print(" cook decode: not supported fourcc\n");
175 retVal = HXR_DEC_NOT_FOUND;
176 }
177
178 if (retVal == HXR_OK && pFrontEnd && pFrontEnd->fpInit && pStreamInfo) {
179 retVal = pFrontEnd->fpInit(pInitParams, ulInitParamsSize, pStreamInfo,
180 &pFrontEnd->pDecode, pFrontEnd->pUserMem,
181 pFrontEnd->fpMalloc, pFrontEnd->fpFree);
182 }
183
184 return retVal;
185}
186
187/* ra_decode_reset()
188 * Calls decoder backend reset function.
189 * Depending on which codec is in use, *pNumSamplesOut samples may
190 * be flushed. After reset, the decoder returns to its initial state.
191 * Returns zero on success, negative result indicates failure. */
192HX_RESULT
193ra_decode_reset(ra_decode* pFrontEnd,
194 UINT16* pSamplesOut,
195 UINT32 ulNumSamplesAvail,
196 UINT32* pNumSamplesOut)
197{
198 HX_RESULT retVal = HXR_FAIL;
199
200 if (pFrontEnd && pFrontEnd->fpReset) {
201 retVal = pFrontEnd->fpReset(pFrontEnd->pDecode, pSamplesOut,
202 ulNumSamplesAvail, pNumSamplesOut);
203 }
204
205 return retVal;
206}
207
208/* ra_decode_conceal()
209 * Calls decoder backend conceal function.
210 * On successive calls to ra_decode_decode(), the decoder will attempt
211 * to conceal ulNumSamples. No input data should be sent while concealed
212 * frames are being produced. Once the decoder has exhausted the concealed
213 * samples, it can proceed normally with decoding valid input data.
214 * Returns zero on success, negative result indicates failure. */
215HX_RESULT
216ra_decode_conceal(ra_decode* pFrontEnd,
217 UINT32 ulNumSamples)
218{
219 HX_RESULT retVal = HXR_FAIL;
220
221 if (pFrontEnd && pFrontEnd->fpConceal) {
222 retVal = pFrontEnd->fpConceal(pFrontEnd->pDecode, ulNumSamples);
223 }
224
225 return retVal;
226}
227
228/* ra_decode_decode()
229 * Calls decoder backend decode function.
230 * pData : input data (compressed frame).
231 * ulNumBytes : input data size in bytes.
232 * pNumBytesConsumed : amount of input data consumed by decoder.
233 * pSamplesOut : output data (uncompressed frame).
234 * ulNumSamplesAvail : size of output buffer.
235 * pNumSamplesOut : amount of ouput data produced by decoder.
236 * ulFlags : control flags for decoder.
237 * Returns zero on success, negative result indicates failure. */
238HX_RESULT
239ra_decode_decode(ra_decode* pFrontEnd,
240 UINT8* pData,
241 UINT32 ulNumBytes,
242 UINT32* pNumBytesConsumed,
243 UINT16* pSamplesOut,
244 UINT32 ulNumSamplesAvail,
245 UINT32* pNumSamplesOut,
246 UINT32 ulFlags,
247 UINT32 ulTimeStamp)
248{
249 HX_RESULT retVal = HXR_FAIL;
250
251 if (pFrontEnd && pFrontEnd->fpDecode) {
252 retVal = pFrontEnd->fpDecode(pFrontEnd->pDecode, pData, ulNumBytes,
253 pNumBytesConsumed, pSamplesOut,
254 ulNumSamplesAvail, pNumSamplesOut, ulFlags, ulTimeStamp);
255 }
256
257 return retVal;
258}
259
260
261/**************** Accessor Functions *******************/
262/* ra_decode_getmaxsize()
263 * pNumSamples receives the maximum number of samples produced
264 * by the decoder in response to a call to ra_decode_decode().
265 * Returns zero on success, negative result indicates failure. */
266HX_RESULT
267ra_decode_getmaxsize(ra_decode* pFrontEnd,
268 UINT32* pNumSamples)
269{
270 HX_RESULT retVal = HXR_FAIL;
271
272 if (pFrontEnd && pFrontEnd->fpGetMaxSize) {
273 retVal = pFrontEnd->fpGetMaxSize(pFrontEnd->pDecode, pNumSamples);
274 }
275
276 return retVal;
277}
278
279/* ra_decode_getchannels()
280 * pNumChannels receives the number of audio channels in the bitstream.
281 * Returns zero on success, negative result indicates failure. */
282HX_RESULT
283ra_decode_getchannels(ra_decode* pFrontEnd,
284 UINT32* pNumChannels)
285{
286 HX_RESULT retVal = HXR_FAIL;
287
288 if (pFrontEnd && pFrontEnd->fpGetChannels) {
289 retVal = pFrontEnd->fpGetChannels(pFrontEnd->pDecode, pNumChannels);
290 }
291
292 return retVal;
293}
294
295/* ra_decode_getchannelmask()
296 * pChannelMask receives the 32-bit mapping of the audio output channels.
297 * Returns zero on success, negative result indicates failure. */
298HX_RESULT
299ra_decode_getchannelmask(ra_decode* pFrontEnd,
300 UINT32* pChannelMask)
301{
302 HX_RESULT retVal = HXR_FAIL;
303
304 if (pFrontEnd && pFrontEnd->fpGetChannelMask) {
305 retVal = pFrontEnd->fpGetChannelMask(pFrontEnd->pDecode, pChannelMask);
306 }
307
308 return retVal;
309}
310
311/* ra_decode_getrate()
312 * pSampleRate receives the sampling rate of the output samples.
313 * Returns zero on success, negative result indicates failure. */
314HX_RESULT
315ra_decode_getrate(ra_decode* pFrontEnd,
316 UINT32* pSampleRate)
317{
318 HX_RESULT retVal = HXR_FAIL;
319
320 if (pFrontEnd && pFrontEnd->fpGetSampleRate) {
321 retVal = pFrontEnd->fpGetSampleRate(pFrontEnd->pDecode, pSampleRate);
322 }
323
324 return retVal;
325}
326
327/* ra_decode_getdelay()
328 * pNumSamples receives the number of invalid output samples
329 * produced by the decoder at startup.
330 * If non-zero, it is up to the user to discard these samples.
331 * Returns zero on success, negative result indicates failure. */
332HX_RESULT
333ra_decode_getdelay(ra_decode* pFrontEnd,
334 UINT32* pNumSamples)
335{
336 HX_RESULT retVal = HXR_FAIL;
337
338 if (pFrontEnd && pFrontEnd->fpMaxSamp) {
339 retVal = pFrontEnd->fpMaxSamp(pFrontEnd->pDecode, pNumSamples);
340 }
341
342 return retVal;
343}
344
345
346void rm_error(void* pError, HX_RESULT result, const char* pszMsg)
347{
348 //dsp_mailbox_send(1,M1B_IRQ7_DECODE_FATAL_ERR, 1, NULL, 0);
349 //trans_err_code(DECODE_FATAL_ERR);
350 raac_print("rm_error pError=0x%08x result=0x%08x msg=%s\n", pError, result, pszMsg);
351 //while(1);
352}
353//static int raac_decode_frame(unsigned char *buf, int maxlen, struct frame_fmt *fmt)
354int audio_dec_decode(audio_decoder_operations_t *adec_ops, char *outbuf, int *outlen, char *inbuf, int inlen)
355{
356 HX_RESULT retVal = HXR_OK;
357 UINT32 ulBytesConsumed = 0;
358 UINT32 ulTotalConsumed = 0;
359 UINT32 ulBytesLeft = 0;
360 UINT32 ulNumSamplesOut = 0;
361 UINT32 ulMaxSamples = 0;
362 UINT32 out_data_len = 0;
363
364 if (inlen > 0) {
365 int len = inlen - cook_input.buf_len;
366 int read_len;
367 read_len = (inlen > cook_input.buf_max) ? cook_input.buf_max : inlen;
368 if (len > 0) {
369 memcpy(cook_input.buf + cook_input.buf_len,
370 inbuf + cook_input.buf_len, read_len - cook_input.buf_len);
371 cook_input.buf_len += read_len - cook_input.buf_len;
372 }
373 }
374 //libcook_print("audio_dec_decoder cook_input.buf_len = %d\n", cook_input.buf_len);
375 while (cook_output.buf_len <= 0) {
376 retVal = rm_parser_get_packet(raac_info.pParser, &raac_info.pPacket);
377 if (retVal == HXR_OK) {
378 //if (ra_info.pPacket->usStream == ra_info.usStreamNum)
379 {
380 retVal = ra_depack_add_packet(raac_info.pDepack, raac_info.pPacket);
381
382 }
383 rm_parser_destroy_packet(raac_info.pParser, &raac_info.pPacket);
384 }
385 if (retVal != HXR_OK) {
386 raac_print("cook_decode_frame£º add packet failed\n");
387 break;
388 }
389 }
390
391 *outlen = 0;
392 if (cook_output.buf_len > 0) {
393 memcpy(outbuf, cook_output.buf, cook_output.buf_len);
394 (*outlen) = cook_output.buf_len;
395 cook_output.buf_len = 0;
396 }
397
398 int ret = 0;
399 //ret = ra_dec_info.decoded_size + cook_input.cousume;
400 ret = cook_input.cousume;
401 //libcook_print("ret decoder = %d\n", ret);
402 cook_input.cousume = 0;
403 adec_ops->pts = cur_frame.offset;
404 cur_frame.offset = 0;
405 return ret;
406 //return raac_dec_info.decoded_size;
407}
408
409
410UINT32 rm_io_read(void* pUserRead, BYTE* pBuf, UINT32 ulBytesToRead)
411{
412 memcpy(pBuf, cur_read_ptr, ulBytesToRead);
413 cur_read_ptr = cur_read_ptr + ulBytesToRead;
414 if ((unsigned)(cur_read_ptr - file_header) > AUDIO_EXTRA_DATA_SIZE) {
415 //trans_err_code(DECODE_INIT_ERR);
416 raac_print("warning :: raac.read byte exceed the the buffer then sent,%d \n", (unsigned)(cur_read_ptr - file_header));
417 while (1) {
418 ;
419 }
420 }
421 return ulBytesToRead;
422}
423void rm_io_seek(void* pUserRead, UINT32 ulOffset, UINT32 ulOrigin)
424{
425 if (ulOrigin == HX_SEEK_ORIGIN_CUR) {
426 cur_read_ptr += ulOffset;
427 } else if (ulOrigin == HX_SEEK_ORIGIN_SET) {
428 cur_read_ptr = (char *)pUserRead + ulOffset;
429 } else if (ulOrigin == HX_SEEK_ORIGIN_END) {
430 cur_read_ptr = sizeof(file_header) + (char *)pUserRead - ulOffset;
431 }
432 if ((unsigned)(cur_read_ptr - file_header) > AUDIO_EXTRA_DATA_SIZE) {
433 //trans_err_code(DECODE_INIT_ERR);
434 raac_print("warning :: raac.seek buffer pos exceed the the buffer then sent,%d \n", (unsigned)(cur_read_ptr - file_header));
435 while (1) {
436 ;
437 }
438 }
439
440}
441unsigned rm_ab_read(void* pUserRead, BYTE* pBuf, UINT32 ulBytesToRead)
442{
443 int ret = 0;
444#if 0
445 if (pBuf && ulBytesToRead) {
446 ret = read_buffer(pBuf, ulBytesToRead);
447 }
448 return ret;
449#endif
450 if (pBuf && ulBytesToRead) {
451 ret = (ulBytesToRead > cook_input.buf_len) ? cook_input.buf_len : ulBytesToRead;
452 memcpy(pBuf, cook_input.buf, ret);
453 if (ret < cook_input.buf_len) {
454 memcpy(cook_input.buf, (cook_input.buf + ret), cook_input.buf_len - ret);
455 }
456 cook_input.buf_len -= ret;
457 }
458 cook_input.cousume += ret;
459 cook_input.all_consume += ret;
460 return ret;
461}
462void rm_ab_seek(void* pUserRead, UINT32 ulOffset, UINT32 ulOrigin)
463{
464 int i;
465#if 0
466 if (ulOrigin == HX_SEEK_ORIGIN_CUR) {
467 for (i = 0; i < ulOffset; i++) {
468 read_byte();
469 }
470 }
471#endif
472 if (ulOrigin == HX_SEEK_ORIGIN_CUR) {
473 if (ulOffset <= cook_input.buf_len) {
474 //memcpy(cook_input.buf, cook_input.buf + ulOffset, cook_input.buf_len - ulOffset);
475 cook_input.buf_len -= ulOffset;
476 cook_input.cousume += ulOffset;
477 //cook_input.all_consume += ulOffset;
478 memcpy(cook_input.buf, cook_input.buf + ulOffset, cook_input.buf_len);
479 } else {
480 raac_print("rm_ab_seek failed\n");
481 }
482 }
483
484}
485HX_RESULT _raac_block_available(void* pAvail, UINT32 ulSubStream, ra_block* pBlock)
486{
487 int len = 0, wlen;
488 int offset = 0;
489 HX_RESULT retVal = HXR_OK;
490 UINT32 ulBytesConsumed = 0;
491 UINT32 ulTotalConsumed = 0;
492 UINT32 ulBytesLeft = 0;
493 UINT32 ulNumSamplesOut = 0;
494 UINT32 ulMaxSamples = 0;
495 UINT32 out_data_len = 0;
496 UINT32 delay_pts = 0;
497 //BYTE* buf = raac_dec_info.input_buf;//passed from the audio dec thread
498 rm_info_t* pInfo = (rm_info_t*) pAvail;
499 if (pAvail && pBlock && pBlock->pData && pBlock->ulDataLen) {
500 ulBytesLeft = pBlock->ulDataLen;
501 while (retVal == HXR_OK && ulBytesLeft) {
502 retVal = ra_decode_decode(raac_dec_info.pDecode,
503 pBlock->pData + pBlock->ulDataLen - ulBytesLeft,
504 ulBytesLeft,
505 &ulBytesConsumed,
506 (UINT16*)raac_dec_info.pOutBuf,
507 raac_dec_info.ulOutBufSize / 2,
508 &ulNumSamplesOut,
509 pBlock->ulDataFlags,
510 pBlock->ulTimestamp * 90);
511
512 if (retVal == HXR_OK) {
513 if (ulBytesConsumed) {
514 ulBytesLeft -= ulBytesConsumed;
515 }
516 if (ulNumSamplesOut) {
517 len = ulNumSamplesOut * 2;
518 }
519 } else if (retVal == HXR_NO_DATA) {
520 raac_print("raac decode not enough data.\n");
521 return 0;
522 } else {
523 raac_print("raac decode error.\n");
524 return 0;
525 }
526 //#if 0
527 //raac_print("ulTimestamp=%ld\n", pBlock->ulTimestamp);
528 cur_frame.offset = pBlock->ulTimestamp * 90 + 1;
529 cur_frame.buffered_len = 0;
530 cur_frame.len = 0;
531 delay_pts += ulNumSamplesOut;//(ulNumSamplesOut/raac_info.pRaInfo->usNumChannels)*90 /(raac_info.pRaInfo->ulSampleRate / 1000);
532 //refresh_swap_register1(cur_frame.offset+delay_pts, len);
533 //#endif
534 memcpy(cook_output.buf + cook_output.buf_len, raac_dec_info.pOutBuf, len);
535 cook_output.buf_len += len;
536 len = 0;
537 }
538 delay_pts = (delay_pts / raac_info.pRaInfo->usNumChannels) * 90 / (raac_info.pRaInfo->ulSampleRate / 1000);
539 cur_frame.offset += delay_pts;
540 raac_dec_info.decoded_size = out_data_len;
541 }
542 return retVal;
543}
544//static int raac_decode_init(struct frame_fmt * fmt)
545int audio_dec_init(audio_decoder_operations_t *adec_ops)
546{
547 //raac_print("\n\n[%s]BuildDate--%s BuildTime--%s", __FUNCTION__, __DATE__, __TIME__);
548 raac_print("enter into %s:%d\n", __FUNCTION__, __LINE__);
549 HX_RESULT retVal = HXR_OK;
550 unsigned ulNumStreams = 0;
551 rm_parser* pParser = HXNULL;
552 rm_stream_header *pHdr = HXNULL;
553 ra_depack *pRADpack = HXNULL;
554 ra_format_info *pRAInfo = HXNULL;
555 int i;
556#if 0
557 struct audio_info *real_data;
558 real_data = (struct audio_info *)fmt->private_data;
559 if (!real_data || !real_data->extradata) {
560 raac_print("[raac decode],got extra data failed\n");
561 return -1;
562 }
563#endif
564 struct audio_info real_data;
565
566 real_data.bitrate = adec_ops->bps;
567 real_data.channels = adec_ops->channels;
568 real_data.extradata_size = adec_ops->extradata_size;
569 real_data.sample_rate = adec_ops->samplerate;
570
571 adec_ops->nInBufSize = DefaultReadSize;
572 adec_ops->nOutBufSize = DefaultOutBufSize;
573 raac_print("%s:%d\n", __FUNCTION__, __LINE__);
574 memset(real_data.extradata, 0, AUDIO_EXTRA_DATA_SIZE);
575 raac_print("%d,%d\n", real_data.extradata_size, adec_ops->extradata_size);
576 for (i = 0; i < real_data.extradata_size; i++) {
577 real_data.extradata[i] = adec_ops->extradata[i];
578 }
579
580 raac_print("raac audioinfo four data [0x%x], [0x%x],[0x%x],[0x%x],[0x%x],[0x%x],[0x%x],[0x%x],\n", real_data.extradata[0], \
581 real_data.extradata[1], real_data.extradata[2], real_data.extradata[3], \
582 real_data.extradata[4], real_data.extradata[5], real_data.extradata[6], real_data.extradata[7]);
583
584 memcpy(file_header, real_data.extradata, AUDIO_EXTRA_DATA_SIZE);
585
586 if (cook_input.buf == NULL) {
587 cook_input.buf = (BYTE *)malloc(DefaultReadSize * sizeof(BYTE));
588 if (cook_input.buf != NULL) {
589 memset(cook_input.buf, 0, DefaultReadSize);
590 cook_input.buf_len = 0;
591 cook_input.buf_max = DefaultReadSize;
592 cook_input.cousume = 0;
593 cook_input.all_consume = 0;
594 } else {
595 raac_print("inbuf malloc failed\n");
596 return -1;
597 }
598 }
599 if (cook_output.buf == NULL) {
600 cook_output.buf = (BYTE *)malloc(DefaultOutBufSize * sizeof(BYTE));
601 if (cook_output.buf != NULL) {
602 memset(cook_output.buf, 0, DefaultOutBufSize);
603 cook_output.buf_len = 0;
604 cook_output.buf_max = DefaultOutBufSize;
605 cook_output.cousume = 0;
606 } else {
607 raac_print("outbuf malloc failed\n");
608 return -1;
609 }
610 }
611
612 if (cook_input.buf == NULL || cook_output.buf == NULL) {
613 raac_print("malloc buf failed\n");
614 return -1;
615
616 }
617 //dsp_cache_wback((unsigned)file_header,AUDIO_EXTRA_DATA_SIZE);
618 cur_frame.offset = 0;
619
620 unsigned ulCodec4CC = 0;
621 //int i;
622 /*clear up the decoder structure */
623 memset(&raac_dec_info, 0, sizeof(raac_decoder_info_t));
624 memset(&raac_info, 0, sizeof(rm_info_t));
625 cur_read_ptr = file_header;
626
627 /* Create the parser struct */
628 pParser = rm_parser_create(NULL, rm_error);
629 if (!pParser) {
630 raac_print("[raac decode],create parser failed\n");
631 return -1;
632 }
633 /* Set the stream into the parser */
634#if 0
635 for (i = 0; i < 1; i++) {
636 raac_print("%d header data [0x%x],[0x%x],[0x%x],[0x%x],[0x%x],[0x%x],[0x%x],[0x%x]\n\t", i, \
637 file_header[i * 8 + 0], file_header[i * 8 + 1], file_header[i * 8 + 2], file_header[i * 8 + 3], \
638 file_header[i * 8 + 4], file_header[i * 8 + 5], file_header[i * 8 + 6], file_header[i * 8 + 7]);
639 }
640#endif /* 0 */
641 retVal = rm_parser_init_io(pParser, file_header, rm_io_read, rm_io_seek);
642 if (retVal != HXR_OK) {
643 raac_print("[raac decode], parser init IO failed,errid %d\n", retVal);
644 rm_parser_destroy(&pParser);
645 return -1;
646 }
647 /* Read all the headers at the beginning of the .rm file */
648 retVal = rm_parser_read_headers(pParser);
649 if (retVal != HXR_OK) {
650 raac_print("[raac decode], parser read header failed,errid %d\n", retVal);
651 rm_parser_destroy(&pParser);
652 return -1;
653 }
654 raac_print("raac: rm_parser_read_headers finished \n");
655 /* Get the number of streams */
656 ulNumStreams = rm_parser_get_num_streams(pParser);
657 if (ulNumStreams == 0) {
658 raac_print("[raac decode], no stream found\n");
659 rm_parser_destroy(&pParser);
660 return -1;
661 }
662 for (i = 0; i < ulNumStreams && retVal == HXR_OK; i++) {
663 retVal = rm_parser_get_stream_header(pParser, i, &pHdr);
664 if (retVal == HXR_OK) {
665 if (rm_stream_is_realaudio(pHdr)) {
666 /* Create the RealAudio depacketizer */
667 pRADpack = ra_depack_create((void*)pParser, _raac_block_available, NULL, rm_error);
668 if (!pRADpack) {
669 raac_print("[raac decode], create depack failed\n");
670 rm_parser_destroy_stream_header(pParser, &pHdr);
671 rm_parser_destroy(&pParser);
672 return -1;
673 }
674 /* Initialize the RA depacketizer with the stream header */
675 retVal = ra_depack_init(pRADpack, pHdr);
676 if (retVal != HXR_OK) {
677 raac_print("[raac decode],init depack failed,errid %d\n", retVal);
678 ra_depack_destroy(&pRADpack);
679 rm_parser_destroy_stream_header(pParser, &pHdr);
680 rm_parser_destroy(&pParser);
681 return -1;
682 }
683 /*
684 * Get the codec 4CC of substream 0. We
685 * arbitrarily choose substream 0 here.
686 */
687 ulCodec4CC = ra_depack_get_codec_4cc(pRADpack, 0);
688 if (ulCodec4CC == 0x636F6F6B) { /* cook */
689 retVal = ra_depack_get_codec_init_info(pRADpack, 0, &pRAInfo);
690 raac_info.pRaInfo = pRAInfo;
691 } else if ((ulCodec4CC == 0x72616163) || (ulCodec4CC == 0x72616370))
692 /* raac racp */
693 {
694 retVal = ra_depack_get_codec_init_info(pRADpack, 0, &pRAInfo);
695 raac_info.pRaInfo = pRAInfo;
696 }
697 raac_info.pDepack = pRADpack;
698 }
699 rm_parser_destroy_stream_header(pParser, &pHdr);
700 }
701 raac_print("raac rm_parser_get_stream_header finished\n");
702 }
703 /* Set the stream into the parser */
704 retVal = rm_parser_init_io(pParser, 0, rm_ab_read, rm_ab_seek);
705 if (retVal != HXR_OK) {
706 if (pRADpack) {
707 ra_depack_destroy(&pRADpack);
708 raac_info.pDepack = NULL;
709 }
710 if (pParser) {
711 rm_parser_destroy(&pParser);
712 }
713 raac_print("[raac decode],rm_parser_init_io failed,errid %d\n", retVal);
714 return -1;
715 }
716 raac_info.pParser = pParser;
717 rm_parser_set_stream(&pParser, 0);
718 rm_parser_file_seek(pParser, 0);
719 raac_dec_info.pDecode = ra_decode_create(HXNULL, rm_error);
720 if (retVal != HXR_OK) {
721 if (pRADpack) {
722 ra_depack_destroy(&pRADpack);
723 raac_info.pDepack = NULL;
724 }
725 if (pParser) {
726 rm_parser_destroy(&pParser);
727 }
728 raac_print("[raac decode],ra_decode_create failed,errid %d\n", retVal);
729 return -1;
730 }
731 raac_dec_info.ulStatus = RADEC_PLAY;
732 raac_dec_info.ulTotalSample = 0;
733 raac_dec_info.ulTotalSamplePlayed = 0;
734 UINT32 ulBufSize = 2048/*max sample*/ * 2/*max channel*/ * sizeof(UINT16) * SBR_MUL;
735 raac_dec_info.ulOutBufSize = ulBufSize;
736 raac_dec_info.pOutBuf = (BYTE*) malloc(raac_dec_info.ulOutBufSize);
737 if (raac_dec_info.pOutBuf == NULL) {
738 if (pRADpack) {
739 ra_depack_destroy(&pRADpack);
740 raac_info.pDepack = NULL;
741 }
742 if (pParser) {
743 rm_parser_destroy(&pParser);
744 raac_info.pParser = NULL;
745 }
746 raac_print("[raac decode],dsp malloc failed,request %s bytes\n", raac_dec_info.ulOutBufSize);
747 return -1;
748 }
749 retVal = ra_decode_init(raac_dec_info.pDecode, ulCodec4CC, HXNULL, 0, raac_info.pRaInfo);
750 if (retVal != HXR_OK) {
751 if (pRADpack) {
752 ra_depack_destroy(&pRADpack);
753 raac_info.pDepack = NULL;
754 }
755 if (pParser) {
756 rm_parser_destroy(&pParser);
757 raac_info.pParser = NULL;
758 }
759 raac_print("[raac decode],ra_decode_init failed,errid %d\n", retVal);
760 return -1;
761 }
762 //fmt->valid=CHANNEL_VALID | SAMPLE_RATE_VALID | DATA_WIDTH_VALID;
763 //fmt->channel_num = raac_info.pRaInfo->usNumChannels;
764 //fmt->data_width = 16;
765#if 0//def AAC_ENABLE_SBR
766 //fmt->sample_rate = raac_info.pRaInfo->ulActualRate;
767#else
768 //fmt->sample_rate = raac_info.pRaInfo->ulSampleRate;
769 aac_decode *pdec = (aac_decode *)raac_dec_info.pDecode->pDecode;
770 if (pdec /*&& pdec->ulSampleRateCore*/) {
771 //fmt->sample_rate = pdec->ulSampleRateCore;
772 raac_print("actual sr %d, sr %d,sbr %d,core sr %d,dec aac sr %d \n", raac_info.pRaInfo->ulActualRate, \
773 raac_info.pRaInfo->ulSampleRate, pdec->bSBR, \
774 pdec->ulSampleRateCore, pdec->ulSampleRateOut);
775 }
776#endif
777
778 return 0;
779}
780
781void ra_depack_cleanup(void)
782{
783 /* Destroy the codec init info */
784 if (raac_info.pRaInfo) {
785 ra_depack_destroy_codec_init_info(raac_info.pDepack, &raac_info.pRaInfo);
786 }
787 /* Destroy the depacketizer */
788 if (raac_info.pDepack) {
789 ra_depack_destroy(&raac_info.pDepack);
790 }
791 /* If we have a packet, destroy it */
792 if (raac_info.pPacket) {
793 rm_parser_destroy_packet(raac_info.pParser, &raac_info.pPacket);
794 }
795 if (raac_info.pParser) {
796 rm_parser_destroy(&raac_info.pParser);
797 }
798 memset(&raac_info, 0, sizeof(raac_info));
799}
800
801
802//static int raac_decode_release(void)
803int audio_dec_release(audio_decoder_operations_t *adec_ops)
804{
805 if (cook_input.buf != NULL) {
806 free(cook_input.buf);
807 cook_input.buf = NULL;
808 }
809 if (cook_output.buf != NULL) {
810 free(cook_output.buf);
811 cook_output.buf = NULL;
812 }
813
814 ra_decode_destroy(raac_dec_info.pDecode);
815 raac_dec_info.pDecode = HXNULL;
816 if (raac_dec_info.pOutBuf) {
817 free(raac_dec_info.pOutBuf);
818 raac_dec_info.pOutBuf = HXNULL;
819 }
820 raac_dec_info.ulStatus = RADEC_IDLE;
821 ra_depack_cleanup();
822 raac_print(" raac decoder release\n");
823
824 return 0;
825}
826
827int audio_dec_getinfo(audio_decoder_operations_t *adec_ops, void *pAudioInfo)
828{
829 aac_decode *pdec = (aac_decode *)raac_dec_info.pDecode->pDecode;
830 if (pdec) {
831 ((AudioInfo *)pAudioInfo)->channels = pdec->ulNumChannels;
832 ((AudioInfo *)pAudioInfo)->samplerate = pdec->ulSampleRateOut;
833 }
834 return 0;
835}
836
837
838#if 0
839static struct codec_type raac_codec = {
840 .name = "raac",
841 .init = raac_decode_init,
842 .release = raac_decode_release,
843 .decode_frame = raac_decode_frame,
844};
845
846
847void __used raac_codec_init(void)
848{
849 raac_print("register raac lib \n");
850 register_codec(&raac_codec);
851}
852
853CODEC_INIT(raac_codec_init);
854#endif
855