audio_dec.c
41.2 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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
#include "os_config.h"
#include "c_def.h"
#include "debug.h"
#include "oem.h"
#include "regmap.h"
#include "hw_dma.h"
#include "hw_da_pp.h"
#include "hw_timer.h"
#include "mem_reloc.h"
#include "message.h"
#include "interrupt.h"
#include "fifo.h"
#include "audio_com.h"
#include "app_dac.h"
#include "codec.h"
#include "audio_dec.h"
#include "audio_dec_dolby.h"
#include "app_cmd.h"
#include "app_main.h"
#include "app_sdram.h"
#include "spdif_data_parse.h"
#include "optek_hifi2_dapp.h"
#include "xa_type_def.h"
#include "optek_link.h"
#ifdef MP3_ENCODE_ENABLE
#include "app_mp3_encode.h"
#endif
#include "hw_codec.h"
#include "peak_level_meter.h"
#include "xa_error_standards.h"
#include "tools.h"
int xa_sbc_dec_frame (void);
U8 ex_audio_stream_zero;
U8 *pTemp_buf;
int decode_type;
volatile U8 wRequestDecodeFrameExit;
U16 dec_frame_size;
#ifdef SYSTEM_CRASH_CHECK_ENABLE
volatile U8 task_dec_cnt;
#endif
//static U8 wCodecBusy;
U8 wCodecBusy;
U8 *pDecOut;
DECODE_DEVICE *p_decode;
U8 decoding_flag;
peak_level_det peak_det_handle;
peak_level_det peak_det_handle1;
I32 adj_outsamples;
extern U8 req_mode,req_txlen,req_rxlen;
void mix1_processing(int * pmusic, int N);
//U16 app_dec_request_codec_exit( void ) __INTERNAL_RAM_TEXT;
U16 app_dec_request_codec_exit (void)
{
if (wRequestDecodeFrameExit == TRUE)
{
return TRUE;
}
else
{
return FALSE;
}
}
#define AD_PCM_BLOCK_SIZE dec_frame_size
U16 discard_frame_cnt;
U32 max_peak;
U16 noice_cnt;
U8 inout_dma_sync_flag;
#define MAX_NOISE_GAIN 128
void pcm_dec_init(void)
{
app_main_data.audio_src_bit = AUDIO_SRC_16BIT;
app_main_data.playing_stream_sample_rate = 48000;
//app_main_data.playing_stream_sample_rate = 44100;
app_main_data.playing_stream_chans = 2;
app_main_data.playing_stream_sample_bits = 16;
app_main_data.playing_stream_bitrate = app_main_data.playing_stream_sample_rate*app_main_data.playing_stream_chans*app_main_data.playing_stream_sample_bits;
app_audio_clock_freq_setting(app_main_data.playing_stream_sample_rate);
codec_malloc_init ();
pDecOut = (U32 *) codec_malloc(AD_PCM_BLOCK_SIZE*2);
discard_frame_cnt = 3;
max_peak = 0;
noice_cnt = 0;
#ifndef OPTEK_LINK_ENABLE
app_dac_receive_pcm_enable(TRUE);
#endif
inout_dma_sync_flag = FALSE;
}
void pcm_dec_fini(int reason)
{
}
extern volatile U8 master_rev_flag;
U8* wait_decode_data(void);
int pcm_dec_decode_frame(U8 **pout,U16 *plen)
{
U8 *p;
U16 size,i;
#ifdef LC3_ENCODE_ENABLE
int app_lc3_part_encoding_code_text_copy(void);
app_lc3_part_encoding_code_text_copy();
#endif
p = wait_decode_data();
if (discard_frame_cnt)
{
discard_frame_cnt--;
memset(pDecOut,0,AD_PCM_BLOCK_SIZE);
*pout = pDecOut;
*plen = AD_PCM_BLOCK_SIZE;
//DBG_Printf("dma0:%d\r\n",DMA_0_COUNT);
return DECODE_SUCCESS;
}
if (p == NULL)
{
memset(pDecOut,0,AD_PCM_BLOCK_SIZE);
*pout = pDecOut;
*plen = AD_PCM_BLOCK_SIZE;
return DECODE_SUCCESS;
}
#if 0
peak = peak_level_meter_16bit(&peak_det_handle,pDecOut,AD_PCM_BLOCK_SIZE);
if (peak >= 0x5)
{
noice_cnt = 0;
}
else
{
if (noice_cnt < (MAX_NOISE_GAIN+200))
noice_cnt++;
if (noice_cnt > 200)
{
//U32 gain;
AUX_noise_gain = MAX_NOISE_GAIN + 200 - noice_cnt;
optek_vol_16b_process(pDecOut,pDecOut,0x08000000/MAX_NOISE_GAIN*AUX_noise_gain,AD_PCM_BLOCK_SIZE/2);
//memset(pAdcDecPcm,0,AD_PCM_BLOCK_SIZE);
}
}
peak = peak_level_meter_16bit(&peak_det_handle1,pDecOut,AD_PCM_BLOCK_SIZE);
static U16 cnt;
if (peak > max_peak)
max_peak = peak;
if (cnt++ == 2000)
{
cnt = 0;
DBG_Printf("0x%x\r\n",max_peak);
max_peak = 0;
}
#endif
*pout = p;
*plen = AD_PCM_BLOCK_SIZE;
return DECODE_SUCCESS;
}
#ifdef SPDIF_ENABLE
#if 1//
#define SPDIF_PCM_BLOCK dec_frame_size
#else
#define SPDIF_PCM_BLOCK PCM_BLOCK
#endif
#define SPDIF_16_BITS
//#define SPDIF_24_BITS
U32 spdif_frame_rx_cn, spdif_frame_tx_cn;
U8 gStreamInPresent;
U8 spdif_rx_down_sample; /*0x02 down by 2, 0x04 down by 4*/
U8 spdif_type;
U16 spdif_rc_frame_cn;
//U16 spdif_in_cn;
void spdif_down_sample_16bit_by_2 (U32 *pBuf, U16 len) __INTERNAL_RAM_TEXT;
void spdif_down_sample_16bit_by_2 (U32 *pBuf, U16 len)
{
U32 *pBufD;
U16 i;
pBufD = pBuf;
for (i=0; i<(len/4)/2; i++)
{
*pBufD++ = *pBuf;
pBuf += 2;
}
}
void spdif_down_sample_16bit_by_4 (U16 *pBuf, U16 len) __INTERNAL_RAM_TEXT;
void spdif_down_sample_16bit_by_4 (U16 *pBuf, U16 len)
{
U32 *pBufD;
U16 i;
pBufD = pBuf;
for (i=0; i<(len/4)/4; i++)
{
*pBufD++ = *pBuf;
pBuf += 4;
}
}
#if 0
void spdif_stream_type_detection_init(void)
{
//spdif_type = stream_unlock;
}
int spdif_stream_type(U16 *buf, int samples)
{
}
#endif
U8 spdif_rx_enable_1 = 1;
void spdif_rx_enable (void)
{
spdif_rx_enable_1 = 0x1;
}
void spdif_rx_disable(void)
{
spdif_rx_enable_1 = 0x0;
}
//for AAC MCH and Dolby
void app_nav_spdif_stream_reinit (void)
{
U8 *pStreamBuf;
U32 size;
//STREAM_ID s;
long len;
DBG_Printf ("Spdif stream reinit\n\r");
spdif_rx_disable ();
Spdif_rcv_disable();
app_dac_mute_enable();
app_cmd_DecoderExit();
//DMA_Channel3_Disable();
//DMA_Channel1_Disable();
#if 1
pStreamBuf = SPDIF_STREAM_START;
size = SPDIF_STREAM_END - SPDIF_STREAM_START;
#else
pStreamBuf = stream_buffer;
#ifdef STREAM_BUFFER_USED_POINTER
size = STREAM_BUF_MAX_SIZE;
#else
size = sizeof(stream_buffer);
#endif
#endif
//app_sdram_stream_flush ();
AUDIO_FIFO_STREAM_FLUSH();
//fifo_file_stream_flush(&audio_env.stream_fifo)
AUDIO_FIFO_STREAM_OPEN(pStreamBuf, size);
//fifo_init(&audio_env.stream_fifo, pStreamBuf, size, TRUE);
AUDIO_FIFO_STREAM_CREATE(0,STREAM_LENTH_UNKNOWN,0,0);
//fifo_create_file_stream(&audio_env.stream_fifo, 0, STREAM_LENTH_UNKNOWN, 0, 0);
/*s = app_sdram_stream_create_wstream(0,
-1,
0);
DBG_Assert(s != INVALID_STREAM_ID);
s = app_sdram_stream_open_rstream(s);
DBG_Assert(s != INVALID_STREAM_ID);
*/
//app_main_data.playing_stream_status = STREAM_DEC_INIT;
spdif_data_parser_init();
#if 0
fifo_init (&aac_in_fifo, SDRAM_SPDIF_AAC_FIFO_START,
(SDRAM_SPDIF_AAC_FIFO_END-SDRAM_SPDIF_AAC_FIFO_START), TRUE);
fifo_init (&spdif_fifo, SDRAM_SPDIF_FIFO_START, (SDRAM_SPDIF_FIFO_END-SDRAM_SPDIF_FIFO_START), TRUE);
#endif
Spdif_rcv_enable();
spdif_rx_enable ();
}
//void spdif_rx(U8 * buf, I16 len) __INTERNAL_RAM_TEXT;
void spdif_rx(U8 * buf, I16 len)
{
#if 1
//I16 l,rlen;
U16 i,k;
U16 zero_action;
U32 size;
U32 *pDesrBuf = (U32 *) buf;
U32 *pSrcBuf = (U32 *) buf;
U32 *pSync;
U32 sync_header_len;
if (spdif_rx_enable_1 == 0x0)
{
return;
}
//need check stream type
TX_INTERRUPT_SAVE_AREA;
TX_DISABLE;
if (spdif_rc_frame_cn < 2)
{
spdif_rc_frame_cn++;
}
TX_RESTORE;
//spdif_frame_cn++;
#ifdef SPDIF_16_BITS
/* k = 0;
for (i=0;i<len/8;i++)
{
//pSrcBuf[i] = ( (pDesrBuf[k] & 0xFFFF0000) | ((pDesrBuf[k+1] & 0xFFFF0000) >> 16) );
pSrcBuf[i] = ( ((pDesrBuf[k] & 0xFFFF0000) >> 16) | (pDesrBuf[k+1] & 0xFFFF0000) );
k = k + 2;
}
len = len/2;
*/
//DBG_Puts("I\n\r");
#if 1//DOLBY_AC3_DEC
if (spdif_header.data_type == SPDIF_STREAM_UNKNOWN)
{
k = 0;
for (i=0; i<len/8; i++)
{
//pSrcBuf[i] = ( (pDesrBuf[k] & 0xFFFF0000) | ((pDesrBuf[k+1] & 0xFFFF0000) >> 16) );
pSrcBuf[i] = ( ((pDesrBuf[k] & 0xFFFF0000) >> 16) | (pDesrBuf[k+1] & 0xFFFF0000) );
k = k + 2;
}
len = len/2;
//non PCM
spdif_frame_rx_cn = 0;
//HDD_READ_FILE_EVENT_SET;
pSync = spdif_data_parser_open((U32 *) buf, len/4); //Dolby AC3 IN:66us
//DBG_PIN_LOW;
if (pSync != NULL)
{
//sync_header_len = (U32) (pSync-buf);
sync_header_len = ((U32)pSync-(U32)buf);
DBG_Assert (sync_header_len <= len);
//size = app_sdram_stream_write (s, pSync, len-sync_header_len);
//DBG_assert( size == len-sync_header_len );
//codec and i2s setting
switch (spdif_header.data_type)
{
#ifdef DOLBY_AC3_DEC
case SPDIF_STREAM_AC3:
//DBG_PIN_LOW; //5ms,8ms,12ms,16ms,24ms
//IIS_out_chan_switch(2);
//dac_out_24bit_16bit_switch(0);
//DBG_Assert (FALSE);
//wRequestDecodeFrameExit = TRUE; //exit current deccoding
app_main_data.playing_stream_sample_bits = 16; //Dolby/AAC 16 bit
#if 1//def AAC_STREAMS
//size = app_sdram_stream_write (s, pSync, len-sync_header_len);
size = fifo_put_data (&audio_env.stream_fifo, pSync, len-sync_header_len);
DBG_assert( size == len-sync_header_len );
#else
fifo_put_data(&aac_in_fifo, pSync, len-sync_header_len);
#endif
uDecSend (DECODE_SET, DECODE_DD_AC3);
uDecSend (DECODE_INIT, 0);
app_main_data.playing_stream_status = STREAM_CDDA_WAIT_DATA;
DBG_Printf ("SPDIF Dolby AC3\n\r");
break;
#else
spdif_header.data_type = SPDIF_STREAM_UNKNOWN;
#endif //DOLBY_AC3_DEC
case SPDIF_STREAM_AAC:
#ifdef AAC_MCH_DEC
//DBG_Assert (FALSE);
//wRequestDecodeFrameExit = TRUE; //exit current deccoding
app_main_data.playing_stream_sample_bits = 16; //Dolby/AAC 16 bit
#if 1//def AAC_STREAMS
//size = app_sdram_stream_write (s, pSync, len-sync_header_len);
size = fifo_put_data (&audio_env.stream_fifo, pSync, len-sync_header_len);
DBG_assert( size == len-sync_header_len );
#else
fifo_put_data(&aac_in_fifo, pSync, len-sync_header_len);
#endif
//uDecSend (DECODE_SET, DECODE_AAC_MCH);
app_main_data.playing_stream_status = STREAM_CDDA_WAIT_DATA;
DBG_Printf ("SPDIF AAC\n\r");
#else
spdif_header.data_type = SPDIF_STREAM_UNKNOWN;
#endif //AAC_MCH_DEC
break;
case SPDIF_STREAM_PCM:
//IIS_out_chan_switch(0);
//dac_out_24bit_16bit_switch(1);
app_main_data.playing_stream_sample_bits = 16; //Dolby/AAC 16 bit
//DBG_Assert (FALSE);
//codec and i2s setting
app_main_data.playing_stream_status = STREAM_CDDA_WAIT_DATA;
uDecSend (DECODE_SET, DECODE_SPDIF);
break;
default:
DBG_Assert (FALSE);
spdif_header.data_type = SPDIF_STREAM_UNKNOWN;
DBG_Printf ("SPDIF Stream Unknown\n\r");
break;
}
}
asm("nop");
return;
}
else
#endif
{
k = 0;
for (i=0; i<len/8; i++)
{
//pSrcBuf[i] = ( (pDesrBuf[k] & 0xFFFF0000) | ((pDesrBuf[k+1] & 0xFFFF0000) >> 16) );
pSrcBuf[i] = ( ((pDesrBuf[k] & 0xFFFF0000) >> 16) | (pDesrBuf[k+1] & 0xFFFF0000) );
k = k + 2;
}
len = len/2;
if (spdif_header.data_type == SPDIF_STREAM_PCM)
{
if (spdif_data_monitor_by_u16(pDesrBuf, len/4) == 0x01)
{
//not pcm
spdif_rx_enable_1 = 0x0;
//DBG_Assert(FALSE);
ukMsgSend(app_nav_spdif_stream_reinit);
return;
}
if (spdif_rx_down_sample == 2)
{
spdif_down_sample_16bit_by_2 ((U32 *) buf, len);
len = len/2;
}
else if (spdif_rx_down_sample == 4)
{
spdif_down_sample_16bit_by_4 ((U32 *) buf, len);
len = len/4;
}
}
}
#else
//mask user data
for (i=0; i<len/4; i++)
{
pDesrBuf[i] = pDesrBuf[i] & 0xFFFFFF00;
}
#endif
#if 1
gStreamInPresent++;
#if 0
if( ex_audioZeroCheck((U32*)buf, len/4 ) ) {
ex_audio_stream_zero = TRUE;
if (zero_action == 0x02) {
return;
}
else if (zero_action == 0x01) {
size = app_sdram_stream_write (s, buf, len);
DBG_assert( size == len );
}
size = app_sdram_stream_write (s, buf, len);
DBG_assert( size == len );
return;
}
else {
ex_audio_stream_zero = FALSE;
size = app_sdram_stream_write (s, buf, len);
if ( size != len ) {
uMsgSend (UI_SPDIF, UI_SPDIF_STREAM_OVERFLOW, 0);
}
return;
}
#endif
//no need zero stuff
ex_audio_stream_zero = FALSE;
//size = app_sdram_stream_write (s, buf, len);
size = (U32) AUDIO_FIFO_STREAM_PUT_DATA(buf, len);
if ( size != len )
{
DBG_printf("overflow:%d\r\n",len);
uMsgSend (UI_SPDIF, UI_SPDIF_STREAM_OVERFLOW, 0);
}
else
{
if (spdif_rx_down_sample == 2)
{
//96k
spdif_frame_rx_cn += 1; //one frame exit
}
else if (spdif_rx_down_sample == 4)
{
//192k
spdif_frame_rx_cn += 1; //not support now
}
else
{
//48k
spdif_frame_rx_cn += 2; //one frame exit
}
size = AUDIO_FIFO_STREAM_DATA_LEN();
zero_action = app_nav_ex_wstream_callback (size);
}
//DBG_Puts("O\n\r");
return;
#else
U32 l;
l = (I16) app_sdram_stream_write (s, (U8 *) buf, len);
#endif
#endif //DOLBY_AC3_DEC
}
void spdif_dec_init( void )
{
app_main_data.audio_src_bit = AUDIO_SRC_16BIT;
app_main_data.playing_stream_chans = 2;
app_main_data.playing_stream_sample_bits = 16;
app_main_data.playing_stream_bitrate = app_main_data.playing_stream_sample_rate*app_main_data.playing_stream_chans*app_main_data.playing_stream_sample_bits;
#if 1
//DBG_Printf("%s\n\r", __func__);
//codec_malloc_init ();
//pAdcDecPcm = (U32 *) codec_malloc(SPDIF_PCM_BLOCK*2); //holding 24b data
//DBG_Assert (pAdcDecPcm != NULL);
audio_out_mode = AUDIO_OUT_L_R;
da_pp_channel_setting(MI2S_OUTPUT_CHANNEL_DEFAULT);
#endif
//spdif_data_parser_init();
codec_malloc_init ();
#if 0//def OPTEK_DSRC_ENABLE
#if 0
//for convert sample rat from 44.1k to 48k
app_main_data.stream_in_sample_rate = SAMPLE_RATE_44P1K;
app_main_data.stream_out_sample_rate = SAMPLE_RATE_48K;
app_main_data.playing_stream_sample_rate = SAMPLE_RATE_44P1K;
app_audio_clock_freq_setting(SAMPLE_RATE_48K);
#else
//convert sample rat from 48k to 44.1k
app_main_data.stream_in_sample_rate = SAMPLE_RATE_48K;
app_main_data.stream_out_sample_rate = SAMPLE_RATE_44P1K;
//don't convert sample rat from 48k to 44.1k
//app_main_data.stream_in_sample_rate = SAMPLE_RATE_44P1K;
//app_main_data.stream_out_sample_rate = SAMPLE_RATE_44P1K;
app_main_data.playing_stream_sample_rate = SAMPLE_RATE_48K;
app_audio_clock_freq_setting(SAMPLE_RATE_44P1K);
#endif
d2as_need_flag = TRUE;
#endif
pDecOut = (U32 *) codec_malloc(SPDIF_PCM_BLOCK);
}
void spdif_dec_fini(int reason)
{
DBG_Printf("spdif dec fini\n\r");
}
U16 spdif_out_cn;
U16 pre_spdif_in_cn;
int spdif_dec_decode(U8 **pout,U16 *plen )
{
u32 dlen;
U32 eventMask;
I32 diff;
dlen = AUDIO_FIFO_STREAM_DATA_LEN();
if (dlen >= SPDIF_PCM_BLOCK)
{
dlen = AUDIO_FIFO_STREAM_GET_DATA(pDecOut, SPDIF_PCM_BLOCK);
DBG_Assert(dlen == SPDIF_PCM_BLOCK);
/*
spdif_frame_tx_cn++;
if ( (spdif_frame_tx_cn & 0x01) == 0x0)
{
diff = (spdif_frame_rx_cn - spdif_frame_tx_cn);
//diff = (spdif_frame_tx_cn - spdif_frame_rx_cn);
//audio_pll_lock (diff);
}
*/
}
else
{
//app_dac_mute_enable();
memset(pDecOut,0,SPDIF_PCM_BLOCK);
uMsgSend (UI_SPDIF, UI_SPDIF_STREAM_UNDERFLOW, 0);
DBG_Printf ("SPDIF underflow\n\r");
//AUDIO_DECODE_EVENT_GET;
}
*pout = pDecOut;
*plen = SPDIF_PCM_BLOCK;
return DECODE_SUCCESS;
}
#endif //SPDIF_ENABLE
#ifdef BT_HFP_ENABLE
extern FIFO *hfp_upstream_fifo;
#define HFP_PCM_FRAME_SIZE (120*2*3*2)
#define HFP_mSBC_READ_SIZE 60
U8 *pmsbc;
U8 *p48kpcm;
void *hfp_upStreamHandle;
void *hfp_downStreamHandle;
extern U32 bt_clk_adaption_cnt;
void hfp_dec_init(void)
{
void *dsp_buf;
U8 *max_biquad;
int *dsp_coef;
DBG_Printf("hfp dec init\n\r");
app_main_data.audio_src_bit = AUDIO_SRC_16BIT;
app_main_data.playing_stream_chans = 2;
app_main_data.playing_stream_sample_bits = 16;
app_main_data.playing_stream_sample_rate = SAMPLE_RATE_48K;
app_main_data.playing_stream_bitrate = app_main_data.playing_stream_sample_rate*app_main_data.playing_stream_chans*app_main_data.playing_stream_sample_bits;
app_audio_clock_freq_setting(app_main_data.playing_stream_sample_rate);
codec_malloc_init ();
pmsbc = codec_malloc(HFP_mSBC_READ_SIZE);
p48kpcm = codec_malloc(HFP_PCM_FRAME_SIZE);
if (hfp_get_codec() == 2)
{
max_biquad = MSBC_MAX_BIQUAD;
dsp_coef = msbc_dsp_coef;
}
else
{
max_biquad = CVSD_MAX_BIQUAD;
dsp_coef = cvsd_dsp_coef;
}
dsp_buf = codec_malloc(optek_bqriir_alloc(max_biquad));
if (dsp_buf == NULL) {
DBG_Assert(FALSE);
}
hfp_upStreamHandle = optek_bqriir_open(dsp_buf,max_biquad,dsp_coef);
if (hfp_upStreamHandle == NULL) {
DBG_Assert(FALSE);
}
dsp_buf = codec_malloc(optek_bqriir_alloc(max_biquad));
if (dsp_buf == NULL) {
DBG_Assert(FALSE);
}
hfp_downStreamHandle=optek_bqriir_open(dsp_buf,max_biquad,dsp_coef);
if (hfp_downStreamHandle == NULL) {
DBG_Assert(FALSE);
}
hfp_upstream_fifo = codec_malloc((sizeof(FIFO)));
fifo_init(hfp_upstream_fifo,codec_malloc(HFP_mSBC_READ_SIZE*8),HFP_mSBC_READ_SIZE*8,TRUE);
app_dac_receive_pcm_enable(TRUE);
xa_msbc_dec_init();
xa_msbc_enc_init();
bt_clk_adaption_cnt = 3000;
}
void hfp_dec_fini(int reason)
{
hfp_upstream_fifo = NULL;
DBG_Printf("hfp dec fini\n\r");
}
int hfp_dec_decode(U8 **pout,U16 *plen)
{
int i;
short *p1,*p2,data;
U8 *pOut;
u16 outputsize;
U32 dlen;
void bt_clk_adaption_process (void);
if (bt_clk_adaption_cnt)
{
--bt_clk_adaption_cnt;
if (bt_clk_adaption_cnt == 0)
{
bt_clk_adaption_cnt = 108000;
bt_clk_adaption_process();
ld_acl_get_slave_time_sync_init();
}
else if(bt_clk_adaption_cnt == 100000)
{
bt_clk_adaption_cnt = 108000;
bt_clk_adaption_process();
ld_acl_get_slave_time_sync_init();
//optek_link_get_slave_time_sync_init(OPTEK_LINK_H1);
}
}
if (hfp_get_codec() == 2)
{
fifo_put_data_by_dma (&AdcInFifo,DMA_2_DEST);
dlen = fifo_get_fifo_data_len(&AdcInFifo);
if (dlen >= HFP_PCM_FRAME_SIZE)
{
if (dlen > HFP_PCM_FRAME_SIZE+240)
{
//DBG_Assert(FALSE);
fifo_discard_data(&AdcInFifo,(dlen - (HFP_PCM_FRAME_SIZE+120)));
DBG_Printf("HFP mic rec overflow,dlen:%d\n\r",dlen);
}
fifo_get_data (&AdcInFifo, p48kpcm, HFP_PCM_FRAME_SIZE);
}
else
{
if (dlen > 120)
{
fifo_discard_data(&AdcInFifo,(dlen - 120));
}
//DBG_Assert(FALSE);
memset(p48kpcm,0,HFP_PCM_FRAME_SIZE);
DBG_Printf("adc rec buf underflow\n\r");
}
optek_bqdiir_stereo_16b_process (hfp_upStreamHandle,p48kpcm,p48kpcm,HFP_PCM_FRAME_SIZE/2);
/**********48k to 16k,stereo to mono*********/
U16 samples = HFP_PCM_FRAME_SIZE/12;
short *pin = p48kpcm;
for (i=0; i<samples; i++)
{
#ifdef MIC_INPUT_FIX_CHANNEL_L
pin[i] = pin[6*i+1];// left channel
#else
pin[i] = pin[6*i];// right channel
#endif
}
int xa_msbc_enc_main_process(U16 *pInBuf, U32 samples, U8 **pOutBuf, U32 *pOutDataLen);
U8 *pOutBuf = NULL;
U8 size;
xa_msbc_enc_main_process(p48kpcm,samples,&pOutBuf,&size);
fifo_put_data (hfp_upstream_fifo, pOutBuf,size);
dlen = AUDIO_FIFO_STREAM_DATA_LEN();
if (dlen >= HFP_mSBC_READ_SIZE)
{
dlen = AUDIO_FIFO_STREAM_GET_DATA(pmsbc, HFP_mSBC_READ_SIZE);
xa_msbc_dec_frame(pmsbc+2,57,&pOut,&outputsize);
}
else
{
DBG_Printf("hfp buf underflow\r\n");
pOut = NULL;
}
if (pOut != NULL)
{
short *pO = p48kpcm;
short *pI = pOut;
for (i=0;i<120;i++)
{
*pO++ = *pI;
*pO++ = *pI;
*pO++ = *pI;
*pO++ = *pI;
*pO++ = *pI;
*pO++ = *pI++;
}
optek_bqdiir_stereo_16b_process (hfp_downStreamHandle,p48kpcm,p48kpcm,HFP_PCM_FRAME_SIZE/2);
}
else
{
DBG_Printf("msbc dec err\r\n");
memset(p48kpcm,0,HFP_PCM_FRAME_SIZE);
}
//dec_direct_out(p48kpcm,HFP_PCM_FRAME_SIZE);
*pout = p48kpcm;
*plen = HFP_PCM_FRAME_SIZE;
return DECODE_SUCCESS;
}
else
{
short *pIn,*pO,data;
unsigned short hfp_pcm_size = HFP_mSBC_READ_SIZE * 6 * 2;
fifo_put_data_by_dma (&AdcInFifo,DMA_2_DEST);
dlen = fifo_get_fifo_data_len(&AdcInFifo);
if (dlen >= hfp_pcm_size)
{
if (dlen > hfp_pcm_size+240)
{
//DBG_Assert(FALSE);
fifo_discard_data(&AdcInFifo,(dlen - (hfp_pcm_size+120)));
DBG_Printf("HFP mic rec overflow,dlen:%d\n\r",dlen);
}
fifo_get_data (&AdcInFifo, p48kpcm, hfp_pcm_size);
}
else
{
if (dlen > 120)
{
fifo_discard_data(&AdcInFifo,(dlen - 120));
}
//DBG_Assert(FALSE);
memset(p48kpcm,0,hfp_pcm_size);
DBG_Printf("adc rec buf underflow\n\r");
}
optek_bqdiir_stereo_16b_process (hfp_upStreamHandle,p48kpcm,p48kpcm,hfp_pcm_size/2);
/**********48k to 16k,stereo to mono*********/
U16 samples = hfp_pcm_size/24;
short *pin = p48kpcm;
for (i=0; i<samples; i++)
{
#ifdef MIC_INPUT_FIX_CHANNEL_L
pin[i] = pin[12*i+1];// left channel
#else
pin[i] = pin[12*i];// right channel
#endif
}
if (fifo_put_data (hfp_upstream_fifo, p48kpcm,samples<<1) != (samples<<1))
{
DBG_Printf("upstream overflow\r\n");
}
dlen = AUDIO_FIFO_STREAM_DATA_LEN();
if (dlen >= HFP_mSBC_READ_SIZE)
{
dlen = AUDIO_FIFO_STREAM_GET_DATA(pmsbc, HFP_mSBC_READ_SIZE);
}
else
{
DBG_Printf("hfp buf underflow\r\n");
memset(pmsbc,0,HFP_mSBC_READ_SIZE);
//pOut = NULL;
}
pIn = pmsbc;
pO = p48kpcm;
for (i=0;i<HFP_mSBC_READ_SIZE/2;i++)
{
data = *pIn++;
//8k to 48k and mono to stereo
*pO++ = data;
*pO++ = data;
*pO++ = data;
*pO++ = data;
*pO++ = data;
*pO++ = data;
*pO++ = data;
*pO++ = data;
*pO++ = data;
*pO++ = data;
*pO++ = data;
*pO++ = data;
}
optek_bqdiir_stereo_16b_process (hfp_downStreamHandle,p48kpcm,p48kpcm,hfp_pcm_size/2);
*pout = p48kpcm;
*plen = hfp_pcm_size;
return DECODE_SUCCESS;
}
}
#endif
#ifdef SBC_DEC
void sbc_dec_init( void )
{
//asm ("break 1,1");
//app_main_data.playing_stream_sample_rate = 44100;
//app_main_data.playing_stream_bitrate = (256000/8);
//uMsgSend (UI_CODEC, UI_DEC_INIT_FAIL, 0);
//uMsgSend (UI_CODEC, UI_DEC_INIT_SUCCESS, 0);
app_main_data.audio_src_bit = AUDIO_SRC_16BIT;
XA_ERRORCODE xa_sbc_dec_init(void);
xa_sbc_dec_init();
// DBG_Printf("sbc dec init\n\r");
}
void sbc_dec_fini( int reason )
{
DBG_Printf("sbc dec fini\n\r");
}
void sbc_dec_decode( void )
{
xa_sbc_dec_frame();
// Reached the end of bit stream.
if (wRequestDecodeFrameExit != TRUE)
{
//playing to stream end
//uMsgSend (UI_CODEC, UI_DECODE_STREAM_EOF, 0);
}
}
static const DECODE_DEVICE sbc_decode =
{
"sbc decode",
sbc_dec_init,
sbc_dec_fini,
xa_sbc_dec_frame
};
#endif //SBC_DEC
void pcm_downsample_dec_init( void )
{
xa_pcm_downsample_dec_init();
app_main_data.audio_src_bit = AUDIO_SRC_16BIT;
app_main_data.playing_stream_chans = 2;
app_main_data.playing_stream_sample_bits = 16;
app_main_data.playing_stream_bitrate = app_main_data.playing_stream_sample_rate*app_main_data.playing_stream_chans*app_main_data.playing_stream_sample_bits;
DBG_Printf("pcm downsample dec init\n\r");
}
void pcm_downsample_dec_fini( int reason )
{
DBG_Printf("pcm downsample dec fini\n\r");
}
int xa_pcm_downsample_dec_frame (void);
void pcm_downsample_dec_decode( void )
{
//xa_sbc_dec_frame();
xa_pcm_downsample_dec_frame();
// Reached the end of bit stream.
if (wRequestDecodeFrameExit != TRUE)
{
//playing to stream end
//uMsgSend (UI_CODEC, UI_DECODE_STREAM_EOF, 0);
}
}
static const DECODE_DEVICE pcm_downsample_decode =
{
"pcmdownsample decode",
pcm_downsample_dec_init,
pcm_downsample_dec_fini,
xa_pcm_downsample_dec_frame
};
#ifdef BT_HFP_ENABLE
static const DECODE_DEVICE hfp_decode =
{
"hfp decode",
hfp_dec_init,
hfp_dec_fini,
hfp_dec_decode
};
#endif
const DECODE_DEVICE pcm_decode =
{
"pcm decode",
pcm_dec_init,
pcm_dec_fini,
pcm_dec_decode_frame
};
#ifdef MP3_DECODE_ENABLE
void mp3_dec_init( void );
void mp3_dec_fini( int reason );
void mp3_dec_decode( void );
int xa_mp3_dec_frame (U32 **out_buf, U32 *out_len);
static const DECODE_DEVICE mp3_decode =
{
"mp3 decode",
mp3_dec_init,
mp3_dec_fini,
xa_mp3_dec_frame
};
#endif
#ifdef WAV_DEC
void app_wave_dec_init (void);
void app_wave_dec_fini (int reason);
int xa_wave_dec_frame(U8 **pout,U16 *plen);
static const DECODE_DEVICE wave_decode =
{
"wav decode",
app_wave_dec_init,
app_wave_dec_fini,
xa_wave_dec_frame
};
#endif //WAV_DEC
const DECODE_DEVICE spdif_decode =
{
"spdif decode",
spdif_dec_init,
spdif_dec_fini,
spdif_dec_decode
};
#ifdef LC3_DECODE_ENABLE
void lc3_dec_main_process_init(void);
void lc3_dec_fini(int reason);
XA_ERRORCODE xa_lc3_dec_frame (U8 **out_buf, U16 *out_byte_len);
static const DECODE_DEVICE lc3_decode =
{
"lc3 decode",
lc3_dec_main_process_init,
lc3_dec_fini,
xa_lc3_dec_frame
};
#endif
void idle_dec_init(void)
{
app_main_data.audio_src_bit = AUDIO_SRC_16BIT;
}
void idle_dec_fini(int reason)
{
}
U8 pIdleDecOut[512];
int idle_dec_decode_frame(U8 **pout,U16 *plen)
{
memset(pIdleDecOut,0,512);
*pout = pIdleDecOut;
*plen = 512;
return DECODE_SUCCESS;
}
static const DECODE_DEVICE idle_decode =
{
"idle decode",
idle_dec_init,
idle_dec_fini,
idle_dec_decode_frame
};
extern const DECODE_DEVICE usbspeaker_decode;
typedef struct{
DECODE_ID dec_id;
DECODE_DEVICE *pDecode_device;
} DECODE_DEVICE_w_ID;
const DECODE_DEVICE_w_ID prj_decode_list[] =
{
{DECODE_NONE,&idle_decode},
{DECODE_PCM,&pcm_decode},
{DECODE_SPDIF,&spdif_decode},
{DECODE_SBC,&sbc_decode},
{DECODE_MP3,&mp3_decode},
{DECODE_WAV,&wave_decode},
{DECODE_PCM_DOWNSAMPLE,&pcm_downsample_decode},
#ifdef LC3_DECODE_ENABLE
{DECODE_LC3,&lc3_decode},
#endif
#if (defined(USB_SPEAKER) || defined(AOA_USB_AUDIO))
{DECODE_USB_SPEAKER,&usbspeaker_decode},
#endif
#ifdef BT_HFP_ENABLE
{DECODE_HFP,&hfp_decode},
#endif
};
/****************************************************************************************
* get DECODE_DEVICE handle by DECODE_ID *
*****************************************************************************************/
DECODE_DEVICE* get_decode_device_handle (DECODE_ID dec_id)
{
DECODE_DEVICE *decode_device_handle;
int i;
for (i=0;i<sizeof(prj_decode_list)/sizeof(DECODE_DEVICE_w_ID);i++)
{
if (dec_id == prj_decode_list[i].dec_id)
{
break;
}
}
if (i < sizeof(prj_decode_list)/sizeof(DECODE_DEVICE_w_ID))
{
decode_device_handle = prj_decode_list[i].pDecode_device;
}
else
{
decode_device_handle = &idle_decode;
DBG_Printf("err:decode id:%d is not in prj_decode_list!\r\n",dec_id);
}
return decode_device_handle;
}
void app_dec_init(void)
{
wCodecBusy = FALSE;
wRequestDecodeFrameExit = TRUE;
uDecSend(DECODE_SET,DECODE_NONE);
}
void uiDecDecodeFrameExit (void)
{
U8 cnt = 0;
while (1)
{
//force decode frame to exit
wRequestDecodeFrameExit = TRUE;
uDecSendWaitForever(DECODE_EXIT,0);
HDD_READ_FILE_EVENT_SET;
TX_SLEEP_MS(5);
if (++cnt >= 2)
SOFT3_INT_SET;
if (decoding_flag == FALSE)
{
break;
}
}
}
void optek_dapp_init (void);
void optek_dapp_open (void);
U8* wait_decode_event(void);
U8 dma_and_tx_sync_req;
U32 dma_count_in_tx_get_data;
#define DEC_PART1_SAMPLES 80
void taskDec(void *pvParameters)
{
DEC_MSG uMsg;
DECODE_DEVICE *pcur_dec = &idle_decode;
U8 *pOut;
U8 *pOut_up;
U8 *pDacbuf;
U8 *p;
U16 len,i,j;
U8 slice;
int ret;
U16 samples;
U16 part1_samples;
U16 max_out_len;
U16 discard_frame = 0;
U16 out_bytes;
U16 size;
U16 logcnt = 0;
U16 min_count = 0xffff;
DBG_Printf("task dec\n");
codec_malloc_init();
audio_com_init();
SOFT3_INT_ENABLE;
app_dec_init();
optek_dapp_init ();
optek_dapp_open ();
#ifdef PROMPT_TONE_ENABLE
prompt_tone_init();
#endif
peak_level_meter_16bit_open(&peak_det_handle,48*40);
peak_level_meter_16bit_open(&peak_det_handle1,48*20);
idle_dec_init();
#ifdef AUDIO_24_BIT_OUTPUT
app_main_data.audio_out_bit = AUDIO_OUT_32BIT_SBIT_H24;
#else
app_main_data.audio_out_bit = AUDIO_OUT_16BIT;
#endif
app_main_data.playing_stream_chans = 2;
app_main_data.audio_src_bit = AUDIO_SRC_32BIT_SBIT_H24;
p_decode = &idle_decode;
decoding_flag = FALSE;
dma_and_tx_sync_req = 0;
dma_count_in_tx_get_data = 0;
master_rev_flag = 0;
#ifdef OPL_MODE_TWO_WAY
if (TEST_MODE == GAME_HEADPHONE_PT7P5MS_T7R3E || TEST_MODE == BC_SF48K_PT7P5MS)
dec_frame_size = 360*4;
else
dec_frame_size = 240*4;
#elif defined OPL_MODE_SINGLE_WAY
dec_frame_size = 240*4;
#else
dec_frame_size = 120*4;
#endif
adj_outsamples = 0;
//extern FIFO master_rx_fifo;
//extern u8 rx_data_buf[250];
//fifo_init(&master_rx_fifo,rx_data_buf,sizeof(rx_data_buf),TRUE);
part1_samples = DEC_PART1_SAMPLES;//
pDacbuf = NULL;
while(1)
{
#ifdef OPTEK_LINK_ENABLE
if (app_main_data.share_link_role != SL_ROLE_BT)
{
#ifdef OPL_MASTER_ENABLE
if ((req_mode != optek_link_mode)&&(app_main_data.share_link_role == SL_ROLE_MASTER))
{
optek_link_change_mode(req_mode,req_txlen,req_rxlen,OPTEK_LINK_H1);
optek_link_mode = req_mode;
}
#endif
#ifdef LC3_DECODE_ENABLE
if ((optek_link_mode == GAME_HEADPHONE_PT_5MS_LP || optek_link_mode == GAME_HEADPHONE_PT7P5MS_T7R3E)||(app_main_data.share_link_role == SL_ROLE_SLAVE))
{
int app_lc3_decoding_code_text_copy(void);
app_lc3_decoding_code_text_copy();
}
#endif
}
#endif
pDacbuf = wait_decode_event();
/******************receive and deal with command*****************************/
if (xQueueReceive(qDEC, (void *)&uMsg,0) == pdPASS)
{
switch(uMsg.msg)
{
case DECODE_SET:
if (!decoding_flag)
{
p_decode = get_decode_device_handle(uMsg.parm);
if (p_decode == &idle_decode)
decode_type = DECODE_NONE;
else
decode_type = uMsg.parm;
DBG_Printf("codec sel:%s\r\n",p_decode->dec_name);
}
break;
case DECODE_FRAME:
if (!decoding_flag)
{
DBG_Printf("codec start:%s\r\n",p_decode->dec_name);
wRequestDecodeFrameExit = FALSE;
decoding_flag = TRUE;
if (uMsg.parm != NULL)
{
p_decode->init();
csbm_tx_init(app_main_data.playing_stream_sample_rate,decode_type == DECODE_SBC);
}
#ifdef OPL_MODE_SINGLE_WAY
//app_change_mode_req(BC_SF48K_PT5MS,100,6);
if (app_main_data.playing_stream_sample_rate == 48000)
{
app_change_mode_req(BC_SF48K_PT5MS,100,6);
}
else if (app_main_data.playing_stream_sample_rate == 44100)
{
app_change_mode_req(BC_SF44K_PT5MS,108,6);
}
else
{
DBG_Printf("optek link not support sample rate\r\n");
}
#elif defined OPL_MODE_SWF
if (decode_type == DECODE_SBC)
{
/*if (app_main_data.playing_stream_sample_rate == 48000)
{
app_change_mode_req(SUBW_SF48K_PT2P5MS,24,6);
}
else if (app_main_data.playing_stream_sample_rate == 44100)
{
app_change_mode_req(SUBW_SF44K_PT2P5MS,24,6);
}
else
{
DBG_Printf("optek link not support sample rate\r\n");
}*/
}
else
{
if (app_main_data.playing_stream_sample_rate == 48000)
{
app_change_mode_req(SUBW_SF48K_PT2P5MS,24,6);
}
else if (app_main_data.playing_stream_sample_rate == 44100)
{
app_change_mode_req(SUBW_SF44K_PT2P5MS,24,6);
}
else
{
DBG_Printf("optek link not support sample rate\r\n");
}
}
#endif
if (app_main_data.playing_stream_chans == 1)
max_out_len = AUDIO_OUT_BUF_SIZE;
else
max_out_len = AUDIO_OUT_BUF_SIZE*2;
discard_frame = 6;
/*
if (optek_link_mode == GAME_HEADPHONE_PT7P5MS_T7R3E || optek_link_mode == BC_SF48K_PT7P5MS)
dec_frame_size = 360*4;
else
dec_frame_size = 240*4;
*/
part1_samples = DEC_PART1_SAMPLES;
logcnt = 0;
min_count = 0xffff;
}
break;
case DECODE_INIT:
if (!decoding_flag)
{
p_decode->init();
csbm_tx_init(app_main_data.playing_stream_sample_rate,decode_type == DECODE_SBC);
}
break;
case DECODE_EXIT:
if (decoding_flag)
{
DBG_Printf("codec exit:%s\r\n",p_decode->dec_name);
decoding_flag = FALSE;
p_decode->fini(DECODE_USER_EXIT);
app_main_data.playing_stream_chans = 2;
app_main_data.audio_src_bit = AUDIO_SRC_32BIT_SBIT_H24;
logcnt = 0;
min_count = 0xffff;
}
break;
default:
break;
}
}
if (pDacbuf == NULL)
continue;
/******************receive and deal with command end*****************************/
/*****************decode a frame**************************/
slice = 1;
if (decoding_flag && !uiDacMute)
{
pOut_up = NULL;
#if (defined OPL_MASTER_ENABLE && defined OPL_MODE_TWO_WAY)
if((app_main_data.share_link_role == SL_ROLE_MASTER)&&(optek_link_mode == GAME_HEADPHONE_PT_5MS_LP || optek_link_mode == GAME_HEADPHONE_PT7P5MS_T7R3E))
{
//if (master_rev_flag)
{
xa_lc3_dec_frame(&pOut_up,&len);
short *p = pOut_up;
//24k to 48K
for(i=0; i<len/2; i++)
{
p[len - 2*i -1] = p[len/2 - i - 1];
p[len - 2*i -2] = p[len/2 - i - 1];
}
len *= 2;
/*
static U8 cnt;
if (cnt++ == 0)
{
DBG_Printf("rev:%d\r\n",len);
}
*/
extern void *upStreamHandle;
optek_bqdiir_mono_16b_process (upStreamHandle,pOut_up,pOut_up,len/2);
//master_rev_flag = 0xff;
if (fifo_put_data(&AdcInFifo, pOut_up,len) != len)
{
//DBG_Printf("usbdev upstream overflow\r\n");
}
}
}
#endif
ret = p_decode->decode(&pOut,&len);
if (ret == DECODE_SUCCESS)
{
app_main_data.playing_stream_pcm_sample += len/4;
}
else
{
app_main_data.playing_stream_pcm_sample += len/4;
DBG_Printf("codec exit 1:%s\r\n",p_decode->dec_name);
decoding_flag = FALSE;
p_decode->fini(ret);
app_main_data.playing_stream_chans = 2;
app_main_data.audio_src_bit = AUDIO_SRC_32BIT_SBIT_H24;
pDacbuf = NULL;
logcnt = 0;
min_count = 0xffff;
continue;
}
#if (defined OPL_MASTER_ENABLE)
if(app_main_data.share_link_role == SL_ROLE_MASTER)
{
#ifdef LC3_ENCODE_ENABLE
int app_lc3_part_encoding_code_text_copy(void);
app_lc3_part_encoding_code_text_copy();
#endif
//optek_hifi2_24b_to_16b(pDacbuf,pDacbuf,(samples<<1));
if (decode_type == DECODE_PCM)
csbm_put_tx_unencoded_data(pOut,AD_PCM_BLOCK_SIZE);
else
csbm_put_tx_unencoded_data(pOut,len);
#if 0//(SHARE_LINK_MODE != SL_TWO_WAY_AV && SHARE_LINK_MODE != SL_TWO_WAY_VV)
fifo_put_data(&audio_delay_fifo,pDacbuf,samples<<2);
fifo_get_data(&audio_delay_fifo,pDacbuf,samples<<2);
#endif
//optek_hifi2_16b_to_24b(pDacbuf,pDacbuf,(samples<<1));
}
#endif
if (pOut_up != NULL)
{
app_main_data.playing_stream_chans = 1;
len >>= 1;
pOut = pOut_up;
}
while(len > max_out_len)
{
slice <<= 1;
len >>= 1;
}
if (decode_type != DECODE_PCM)
{
app_timer_idle_time_reset();
}
else
{
//need check noise,study later
app_timer_idle_time_reset();
}
}
else
{
//if (app_main_data.share_link_role == SL_ROLE_SLAVE)
// continue;
pOut = pDacbuf;
len = AD_PCM_BLOCK_SIZE*2;
/*********sync***************/
static I32 last_adj = 0;
I32 adj;
U32 hsamples = TRANS_PART1_SAMPLES/2;
adj_outsamples -= last_adj;
last_adj = 0;
adj = adj_outsamples;
if (adj)
{
if (adj > 0)
{
len += (adj<<3);
last_adj = adj;
}
else if (adj < 0)
{
adj = -adj;
if (adj > hsamples)
{
adj = hsamples;
}
len -= (adj<<3);
last_adj = -adj;
}
DBG_Printf("adj:%d,left:%d\r\n",last_adj,adj_outsamples - last_adj);
}
/*********sync end***************/
memset(pDacbuf,0,len);
}
/*****************decode a frame end**************************/
U8 *msrc = pOut;
do
{
if (decoding_flag && !uiDacMute)
{
if(app_main_data.audio_src_bit == AUDIO_SRC_16BIT)
{
int *dst = pDacbuf;
short *src = msrc;
if (app_main_data.playing_stream_chans == 1)
{
samples = len>>1;
for (i=0; i<samples; i++)
{
*dst++ = *src<<16;
*dst++ = *src++<<16;
}
}
else
{
samples = len>>2;
optek_hifi2_16b_to_24b(pDacbuf,msrc,(samples<<1));
/*for (i=0; i<samples; i++)
{
*dst++ = *src++<<16;
*dst++ = *src++<<16;
}*/
}
}
else if(app_main_data.audio_src_bit == AUDIO_SRC_24BIT)
{
U8 *dst = pDacbuf;
U8 *src = msrc;
if (app_main_data.playing_stream_chans == 1)
{
samples = len/3;
for (i=0; i<samples; i++)
{
*dst++ = 0;
*dst++ = *src;
*dst++ = *src;
*dst++ = *src;
*dst++ = 0;
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
}
}
else
{
samples = len/6;
for (i=0; i<samples; i++)
{
*dst++ = 0;
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
*dst++ = 0;
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
}
}
}
else if(app_main_data.audio_src_bit == AUDIO_SRC_32BIT_SBIT_H24)
{
int *dst = pDacbuf;
int *src = msrc;
if (app_main_data.playing_stream_chans == 1)
{
samples = len>>1;
for (i=0; i<samples; i++)
{
*dst++ = *src;
*dst++ = *src++;
}
}
else
{
samples = len>>3;
CFasm_memcpy(pDacbuf,msrc,len);
}
}
}
else
{
samples = len>>3;
}
#ifdef OPTEK_DSP_MX1
mix1_processing(pDacbuf, samples<<1);
#endif
#ifdef OPL_MODE_SWF
optek_dapp_process (pDacbuf, 2,samples<<1);
#else
//optek_dapp_process (pDacbuf, 2,samples<<1);
#endif
if (app_main_data.audio_out_bit == AUDIO_OUT_16BIT)
{
optek_hifi2_24b_to_16b(pDacbuf,pDacbuf,(samples<<1));
out_bytes = samples<<2;
}
else
{
out_bytes = samples<<3;
}
#if 0
if (app_main_data.share_link_role == SL_ROLE_SLAVE)
{
U16 part2_samples;
#if 0
/* prints infomation that the time taken to decode a frame,unit is samples.*/
static U16 cnt;
if (cnt++ == 1000)
{
cnt = 0;
if (app_main_data.audio_out_bit == AUDIO_OUT_16BIT)
{
DBG_Printf("decode frame time:%d samples\r\n",samples - DMA_0_COUNT);
}
else
{
DBG_Printf("decode frame time:%d samples\r\n",samples - (DMA_0_COUNT>>1));
}
}
#endif
if (samples == (dec_frame_size>>2))
{
part2_samples = samples - part1_samples;
}
else
{
DBG_Printf("decode out len err samples:%d\r\n",part2_samples);
part2_samples = (dec_frame_size>>2) - part1_samples;
pDacbuf = NULL;
}
if (app_main_data.audio_out_bit == AUDIO_OUT_16BIT)
size = part2_samples*4;
else
size = part2_samples*8;
if (pDacbuf)
{
memcpy(p,pDacbuf,size);
pDacbuf += size;
}
else
{
memset(p,0,size);
}
}
#endif
#if 0
/* prints infomation that the time taken to decode a frame,unit is samples.*/
U32 dma8_count = DMA_8_COUNT;
U32 dma0_count = DMA_0_COUNT;
if (dma0_count == 0)
{
dma0_count = dma8_count;
}
else
{
dma0_count = dma0_count+DMA_UNDERFLOW_DELAY_COUNT;
}
if (min_count > dma0_count)
min_count = dma0_count;
if (logcnt++ == 1000)
{
logcnt = 0;
if (app_main_data.audio_out_bit == AUDIO_OUT_32BIT_SBIT_H24)
{
min_count >>= 1;
}
DBG_Printf("trans dma count:%d samples\r\n",min_count);
min_count = 0xffff;
}
#endif
set_dac_out_size(out_bytes);
#if (defined OPL_SLAVE_ENBALE)
if ((app_main_data.share_link_role == SL_ROLE_SLAVE)&&(decoding_flag && !uiDacMute))
{
#if 0
while(DMA_0_COUNT);
if (discard_frame)
{
discard_frame--;
//memset(pDacbuf,0,out_bytes+32*4);
if (discard_frame == 0)
{
memset(pDacbuf,0,out_bytes+56*4);
DMA_0_SOURCE = (int *) pDacbuf;//Source address
DMA_0_COUNT = out_bytes/4+56;
}
}
else
{
/*
if (DMA_0_COUNT != 0)
{
DBG_Printf("c1:%d\r\n",DMA_0_COUNT);
}
*/
DMA_0_SOURCE = (int *) pDacbuf;//Source address
DMA_0_COUNT = out_bytes/4;
}
#endif
void optek_link_cal_drift (void);
if (csb_rx_count)
{
--csb_rx_count;
if (csb_rx_count < (50000/5))
{
if (csb_rx_count % (2500/5) == 0)
{
ukMsgSend(optek_link_cal_drift);
ukParmSend(optek_link_get_slave_time_sync_init,OPTEK_LINK_H1);
if (csb_rx_count == 0)
{
csb_rx_count = (100000/5)+(100000/5);
}
}
}
else if(csb_rx_count == (100000/5))
{
csb_rx_count = (100000/5)+(100000/5);
ukMsgSend(optek_link_cal_drift);
ukParmSend(optek_link_get_slave_time_sync_init,OPTEK_LINK_H1);
}
}
if (optek_link_mode == GAME_HEADPHONE_PT_5MS_LP || optek_link_mode == GAME_HEADPHONE_PT7P5MS_T7R3E)
{
#ifdef LC3_ENCODE_ENABLE
int app_lc3_part_encoding_code_text_copy(void);
app_lc3_part_encoding_code_text_copy();
#endif
U8 *p;
p = wait_decode_data();
if (p)
{
csbm_put_tx_unencoded_data(p,AD_PCM_BLOCK_SIZE);
}
}
}
#endif
if (slice > 1)
{
slice--;
msrc += len;
if (app_main_data.share_link_role != SL_ROLE_SLAVE)
pDacbuf = wait_decode_event();
}
else
{
break;
}
}while(1);
}
}
U8* wait_upstream_decode_event(void)
{
xEventGroupWaitBits(event_grop, AUDIO_DECODE_EVENT/*|AUDIO_DECODE_EVENT2*/, pdTRUE, pdTRUE, 100);
return NULL;//(U8*);
}
U8* wait_decode_event(void)
{
#if (defined OPL_SLAVE_ENBALE)
if (app_main_data.share_link_role == SL_ROLE_SLAVE)
{
//while(1)
{
//AUDIO_DECODE_EVENT_GET;
xEventGroupWaitBits(event_grop, AUDIO_DECODE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY);
/*
if (awOut_contrls.awSize == 0)
{
break;
}
*/
}
return (U8*)&awOutStore.awOutSampleStore[awOut_contrls.awIndex][0];
}
else
#endif
{
//while(1)
{
//if (awOut_contrls.awSize == 0)
{
//break;
}
//AUDIO_DECODE_EVENT_GET;
if ((xEventGroupWaitBits(event_grop, AUDIO_DECODE_EVENT, pdTRUE, pdFALSE, 10) & AUDIO_DECODE_EVENT) != AUDIO_DECODE_EVENT)
{
//DBG_Printf("event time out\r\n");
//break;
return NULL;
}
}
//if (app_main_data.share_link_role != SL_ROLE_BT)
// awOut_contrls.awIndex = !awOut_contrls.awIndex;
return (U8*)&awOutStore.awOutSampleStore[awOut_contrls.awIndex][0];
}
}
void set_dac_out_size(U16 size)
{
awOut_contrls.awSize = size>>2;
}