codec.c
8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include "os_config.h"
#include "c_def.h"
#include "debug.h"
#include "oem.h"
#include "mem_reloc.h"
#include "message.h"
#include "codec.h"
#include "audio_dec.h"
#include "fifo.h"
#include "audio_com.h"
#include "audio_dec.h"
#include "app_sdram.h"
#include "app_main.h"
extern U8 CODEC_MEMORY_START[];
//#undef TIME_SCALE_CODE
//#define POINTER_USED
#ifdef POINTER_USED
U8 *codec_mem = (U8 *)CODEC_MEMORY_START;
#else
U8 codec_mem[MAX_CODEC_MEM_SIZE] __attribute__ ((section (".codec_memory_bss")));
#endif
U32 g_w_codec_mem_used;
U8 wNoMoreFrames;
int xa_fread(void *buffer, int size, int count, void *fp) __INTERNAL_RAM_TEXT;
/*****get current position************/
long xa_ftell(void *ptr)
{
return AUDIO_FIFO_STREAM_FIFO_RLEN();
}
/*****file had end?************/
int xa_feof(void *stream)
{
return (AUDIO_FIFO_STREAM_FIFO_RLEN()>=AUDIO_FIFO_STREAM_STREAM_LEN());
}
/*****get file len************/
long xa_flength(void *ptr)
{
return AUDIO_FIFO_STREAM_STREAM_LEN();
}
int xa_fseek( void *stream, long offset, int origin )
{
U32 pos,curr_pos,len,new_pos,wlen;
TX_INTERRUPT_SAVE_AREA;
int ret;
DBG_Printf("%s\n\r", __func__);
if (AUDIO_FIFO_STREAM_IS_VAILD() == FALSE)
{
DBG_Printf("XA Read Fatal Err!!!\n\r");
wRequestDecodeFrameExit = TRUE; /*Quit from codec*/
return -1;
}
curr_pos = AUDIO_FIFO_STREAM_FIFO_RLEN();
len = AUDIO_FIFO_STREAM_STREAM_LEN();
wlen = AUDIO_FIFO_STREAM_FIFO_WLEN();
switch (origin)
{
case 0: //SEEK_SET
//DBG_Assert (pos >= offset); //the seek function can only seek forward
pos = offset;
break;
case 1: //SEEK_CURR
pos = curr_pos + offset;
break;
case 2: //SEEK_END
pos = len - offset;
break;
default: //ERR
break;
}
if (pos > len)
{
DBG_Printf ("curr pos=%d offset=%d org=%d err\n\r", curr_pos, offset,origin);
DBG_Printf ("pos=%d > len=%d err\n\r", pos,len);
//if to jump next track, the set will be dead.
//app_dac_mute_enable();
//uMsgSend (UI_CD_INTERFACE, UI_DECODE_SKIP_F, 0);
return -1;
//return 0;
}
if (pos == curr_pos)
{
return 0;
}
else if (pos < curr_pos)
{
DBG_Printf ("Seek backward:0x%x\n\r",curr_pos - pos);
}
else
{
DBG_Printf ("Seek forward:0x%x\n\r",pos - curr_pos);
}
ret = AUDIO_FIFO_STREAM_SEEK(pos);
if (ret == 0)
{
return 0;
}
else
{
if (pos > (wlen + 0x20000) || pos < curr_pos)
//if (pos < curr_pos)
{
//no cd data coming
//no read file from usb
//app_nav_lock_stream_mutex ();//force the hdd to idle state
//app_nav_unlock_stream_mutex ();
app_media_mute_enable();
STREAM_MUTEX_LOCK;
AUDIO_FIFO_STREAM_FLUSH ();
new_pos = (pos / 0x2000) * 0x2000;
wlen = new_pos;
//only for usb
AUDIO_FIFO_STREAM_CREATE(new_pos,len,app_main_data.playing_index,audio_env.stream_fifo.stream_margin);
app_media_mute_disable ();
STREAM_MUTEX_UNLOCK;
uHddMsgSend (HDD_READ_FILE,new_pos,0);
//DBG_Assert(FALSE);
}
}
U16 to = 100;
U32 lwlen = 0;
while (pos > wlen)
{
//xEventGroupWaitBits(event_grop,AUDIO_DECODE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY);
if (wRequestDecodeFrameExit == TRUE)
{
DBG_Printf ("xa_fread return\n\r");
return 0;
}
if (lwlen == wlen)
{
if (--to == 0)
{
wRequestDecodeFrameExit = TRUE; /*Quit from codec*/
DBG_Printf("read file timeout,seek fail!\r\n");
return 0;
}
}
else
{
AUDIO_FIFO_STREAM_SEEK(wlen); //feed buf
lwlen = wlen;
to = 100;
}
//HDD_READ_FILE_EVENT_GET;
TX_SLEEP_MS(1);
wlen = AUDIO_FIFO_STREAM_FIFO_WLEN();
}
ret = AUDIO_FIFO_STREAM_SEEK(pos);
DBG_Assert(ret == 0);
return 0;
}
int xa_fread(void *buffer, int size, int count, void *fp)
{
U32 bytes;
U32 len;
U32 to = 100;
bytes = size*count;
if (bytes == 0)
{
return 0;
}
while(1)
{
len = AUDIO_FIFO_STREAM_GET_DATA((U8 *)buffer, bytes);
if (len > 0)
{
return len/size;
}
else
{
/************steam underflow****************/
DBG_Printf ("Buf underflow\n\r");
#ifdef BT_HCI_ENABLE
if (app_main_data.media == MEDIA_BT_HCI)
{
//void dec_buf_underflow (void);
//dec_buf_underflow();
uMsgSend (UI_CODEC, UI_STREAM_UNDERFLOW, 0);
//xEventGroupWaitBits(event_grop,AUDIO_DECODE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY);
//if (wRequestDecodeFrameExit == TRUE)
{
return 0;
}
}
else
#endif
{
if (--to == 0)
{
DBG_Printf("fs read data timeout!\r\n");
return 0;
}
if ( (AUDIO_FIFO_STREAM_STREAM_LEN() != STREAM_LENTH_UNKNOWN) &&
(AUDIO_FIFO_STREAM_STREAM_LEN() == AUDIO_FIFO_STREAM_FIFO_WLEN()) )
{
return 0;
}
//clear event first
//HDD_READ_FILE_EVENT_CLR;
//HDD_READ_FILE_EVENT_GET;
//DBG_Printf ("data rec\n\r");
//tx_event_flags_set(&event_grop, ~HDD_READ_FILE_EVENT, TX_AND);
//tx_event_flags_get(&event_grop, HDD_READ_FILE_EVENT, TX_OR_CLEAR, &eventMask, TX_WAIT_FOREVER);
if (wRequestDecodeFrameExit || !AUDIO_FIFO_STREAM_IS_VAILD())
{
DBG_Printf ("xa_fread return\n\r");
return 0;
}
TX_SLEEP_MS(1);
}
}
}
}
#if 1//(defined AAC_MCH_DEC || defined DOLBY_AC3_DEC)
void app_nav_spdif_stream_reinit (void);
extern U8 *spdif_parser_buf;
U16 spdif_parsed_data_size;
U16 spdif_parsed_data_index;
int _xa_spdif_fread(void *buffer, int size, int count, void *fp) __INTERNAL_RAM_TEXT;
int _xa_spdif_fread(void *buffer, int size, int count, void *fp)
{
DBG_Assert(FALSE);
#if (defined AAC_MCH_DEC || defined DOLBY_AC3_DEC)
STREAM_ID s;
U32 len;
U32 bytes;
U32 data_burst_size;
U16 remain;
static U8 count_num = 0;
/*s = app_sdram_stream_get_rstream();
if (s == INVALID_STREAM_ID)
{
DBG_Printf("XA Spdif Read Fatal Err!!!\n\r");
wRequestDecodeFrameExit = TRUE;
}*/
bytes = count * size;
if (bytes == 0)
{
return 0;
}
if (spdif_parsed_data_size != 0 && spdif_parsed_data_index < spdif_parsed_data_size)
{
remain = spdif_parsed_data_size - spdif_parsed_data_index;
if (remain < bytes)
{
bytes = remain;
}
CFasm_memcpy (buffer, &spdif_parser_buf[spdif_parsed_data_index], bytes);
spdif_parsed_data_index += bytes;
return bytes;
}
data_burst_size = spdif_get_data_burst_size();
if (data_burst_size == 0)
{
return 0;
}
while (1)
{
//len = app_sdram_stream_read (s, (unsigned char *)spdif_parser_buf, data_burst_size);
len = AUDIO_FIFO_STREAM_GET_DATA( (unsigned char *)spdif_parser_buf, data_burst_size);
//if (len == STREAM_UNDERFLOW)
if (len == 0)
{
#if 1
//added the code below for solving the issue of low bit rate, for example 224k or 448k.
count_num++;
//if Dolby bit rate is 224, the count_num's 2, in begining of playing
//if Dolby bit rate is 448, the count_num's 1, in begining of playing
DBG_Printf ("spdif r count_num:%d\n\r", count_num);
if (count_num >= 100)
{
count_num = 0;
DBG_Printf ("Spdif rx underflow\n\r");
#if 1
extern U8 analog_vol_adjust;
//analog_vol_adjust = 1;
//void spdif_underflow_mute(void);
//spdif_underflow_mute();
//DBG_Printf("spdif rcv underflow\n\r");
//app_dac_mute_enable();
uMsgSend (UI_SPDIF, UI_SPDIF_STREAM_UNDERFLOW, 0);
//analog_vol_adjust=true;
//spdif_data_line_off=true;
//ukMsgSend(app_nav_spdif_stream_reinit);
#endif
}
#else
DBG_Printf ("Spdif rx underflow\n\r");
uMsgSend (UI_SPDIF, UI_SPDIF_STREAM_UNDERFLOW, 0);
#endif
AUDIO_DECODE_EVENT_CLR;
AUDIO_DECODE_EVENT_GET;
//if (wRequestDecodeFrameExit || app_sdram_stream_get_rstream() == INVALID_STREAM_ID)
if (wRequestDecodeFrameExit )
{
DBG_Printf ("xa_spdif_fread return\n\r");
return 0;
}
}
else
{
count_num = 0;
//len = spdif_data_burst_parser (spdif_parser_buf, buffer, data_burst_size);
len = spdif_data_burst_parser (spdif_parser_buf, data_burst_size);
if (len == 0)
{
//sync lost
ukMsgSend(app_nav_spdif_stream_reinit);
return 0;
}
spdif_parsed_data_size = len;
spdif_parsed_data_index = 0;
if (spdif_parsed_data_size < bytes)
{
bytes = spdif_parsed_data_size;
}
CFasm_memcpy (buffer, spdif_parser_buf, bytes);
spdif_parsed_data_index += bytes;
return bytes;
}
}
#else
return 0;
#endif
}
#endif //(defined AAC_MCH_DEC || defined DOLBY_AC3_DEC)
void xa_fwrite(void *buffer,int size, int length, void *pointer)
{
}
void codec_malloc_init (void)
{
g_w_codec_mem_used = 0;
}
U8 *codec_malloc ( U32 size)
{
unsigned char *pbMem;
//assert ( (g_w_codec_mem_used + size) <= MAX_CODEC_MEM_SIZE);
pbMem = &codec_mem[g_w_codec_mem_used];
g_w_codec_mem_used += ((size + 7) >> 3) << 3; //aligned with 8 bytes
DBG_Printf("mem used:0x%x\n", g_w_codec_mem_used);
if ( g_w_codec_mem_used >= MAX_CODEC_MEM_SIZE )
{
DBG_Assert (FALSE);
DBG_Printf("malloc err:%d\n\r", size);
pbMem = NULL;
}
return pbMem;
}
U32 codec_get_mem_used(void)
{
return g_w_codec_mem_used;
}
void codec_set_mem_used(U32 val)
{
g_w_codec_mem_used = val;
}