app_mp3_encode.c
14.8 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
#include "os_config.h"
#include "c_def.h"
#include "debug.h"
#include "oem.h"
#ifdef MP3_ENCODE_ENABLE
#include "regmap.h"
#include "mem_reloc.h"
#include "message.h"
#include "app_sdram.h"
#include "audio_dec.h"
#include "app_dac.h"
#include "fifo.h"
#include "app_main.h"
#include "app_lcd.h"
#include "app_mp3_encode.h"
#include "optek_src.h"
#ifdef CD_ENABLE
#include "app_cdrom.h"
#endif
#if 1
#include "xa_type_def.h"
#include "xa_error_standards.h"
#include "xa_error_handler.h"
#include "xa_apicmd_standards.h"
#include "xa_memory_standards.h"
#endif
extern U8 __TEXT_START38[];
extern U8 __TEXT_ROM_START38[];
extern U8 __TEXT_START38_END[];
#if 0
FIFO record_fifo;
#else
FIFO record_fifo;
//FIFO record_fifo __attribute__ ((section (".record_fifo_bss")));
#endif
FIFO record_in_fifo;
#define RECORD_BUF_POINTER_USED
#ifdef RECORD_BUF_POINTER_USED
U8 *record_buf = (U8 *)RECORD_FIFO_BUF_START;
#define RECORD_BUF_SIZE (RECORD_FIFO_BUF_END - RECORD_FIFO_BUF_START)
#else
U8 record_buf[RECORD_BUF_SIZE];
//U8 record_buf[RECORD_BUF_SIZE] __attribute__ ((section (".record_fifo_bss")));
#endif
U8 record_buf_overflow;
//U8 record_buf_overflow __attribute__ ((section (".sdram_bss")));
#ifdef AUDIO_OUT_BUF_IS_HALF_OF_MP3_ENC_FRAME_SAMPLE
U8 pcm_in_data_index;
//U8 pcm_in_data_buf[MP3_ENC_FRAME_SAMPLE*4];
U8 *pcm_in_data_buf = NULL;
U32 enc_data_len;
#else
#if 0//for testing
//U8 pcm_in_data_buf[2];
U8 pcm_in_data_buf[1152*4];
#endif
#endif
int xa_mp3_enc_set_config_param(xa_codec_handle_t p_xa_process_api_obj,
UWORD32 *pui_samp_freq,
UWORD32 *pui_num_chan,
UWORD32 *pui_pcm_wd_sz
);
void app_mp3_encode_fifo_init (void)
{
int i;
U8 *pBuf1;
int size = RECORD_FIFO_BUF_END - RECORD_FIFO_BUF_START;
pBuf1 = (U8 *)RECORD_FIFO_BUF_START;
size = RECORD_FIFO_BUF_END - RECORD_FIFO_BUF_START;
fifo_init (&record_fifo, (U8 *)pBuf1, size, TRUE);
#if 0
pBuf1 = (U8 *)RECORD_DATA_BUF_START;
size = RECORD_DATA_BUF_END - RECORD_DATA_BUF_START;
fifo_init (&record_in_fifo, (U8 *)pBuf1, size, TRUE);
#endif
#if 0
int *pBuf = pcm_in_data_buf;
for (i=0; i<(1152*4)/(4*72); i++)
{
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x7fff7fff;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
*pBuf++ = 0x80018001;
}
#endif
}
BOOL app_mp3_copy_init (void)
{
#ifdef MP3_FILE_COPY
U32 len;
DBG_Puts("mp3 copy init enter\n\r");
record_buf_overflow = FALSE;
app_main_data.record_time = 0;
app_main_data.record_timeset = 0;
#ifdef RECORD_BUF_POINTER_USED
//initialize encoding fifo buf
fifo_init (&record_fifo, record_buf, RECORD_BUF_SIZE, TRUE);
#else
//initialize encoding fifo buf
fifo_init (&record_fifo, record_buf, sizeof(record_buf), TRUE);
#endif
#endif
}
/*******************************************************************************
*
* FUNCTION NAME - app_mp3_encode_init
*
* ARGUMENTS - :
*
* RETURN VALUE - Error type
*
*******************************************************************************
*
* DESCRIPTION
* This function initializes the MP3 encoder. It copies the MP3 encoder code
* to SRAM and changes clock to higher frequency.
******************************************************************************/
BOOL app_mp3_encode_init (void)
{
#if 1
//eEncReturnCode eRetCode;
MP3E_RET_CODE eRetCode;
U8 *pucSource, *pucDest;
U32 count;
U32 length;
DBG_Puts("mp3 encode init enter\n\r");
//static MP3E_ENC_PARAMS enc_params = {0, 44100, MP3_ENC_BITRATE, MPG_MD_JOINT_STEREO};
#if 0
/* Copy MP3 encoder data to SRAM. The pucSource address, pucDestination address
* and number of bytes to copy are taken from variables set during
* the linking process.
*/
//CFasm_memcpy (pucDest, pucSource, (U32)&KramDataMp3eSramSize);
length = __TEXT_START38_END - __TEXT_START38;
for (count=0; count < length; count++)
{
__TEXT_START38[count] = __TEXT_ROM_START38[count];
}
#endif
pcm_in_data_index = 0;
enc_data_len = 0;
// pcm_in_data_buf = (U8 *)optek_src_buf;
record_buf_overflow = FALSE;
app_main_data.record_time = 0;
//app_main_data.record_timeset = 0;
#if 1
extern int decode_type;
if (decode_type == DECODE_SBC)
{
#if (BT_SHARE_LINK_ENC_BITRATE == BT_SL_BITRATE_64k)
app_main_data.record_bitrate = 64000;
#elif (BT_SHARE_LINK_ENC_BITRATE == BT_SL_BITRATE_96k)
app_main_data.record_bitrate = 96000;
#else // BT_SL_BITRATE_160k
app_main_data.record_bitrate = 160000;
#endif
}
else
{
app_main_data.record_bitrate = 160000;
}
// app_main_data.playing_stream_sample_rate = 44100;
// app_main_data.record_bitrate = 160000;//128000;//128000;//192000;
#else
app_main_data.playing_stream_sample_rate = 48000;
app_main_data.record_bitrate = 128000;
//app_main_data.playing_stream_sample_rate = 48000;
//app_main_data.record_bitrate = 160000;
//app_main_data.playing_stream_sample_rate = 48000;
//app_main_data.record_bitrate = 192000;
#endif
// initialize the encoder parameters like sampling rate, bit rate etc
//app_mp3_encode_param_set(&enc_params);
xa_mp3_enc_init ();
//uDecSend (MP3ENC_INIT, 0);
DBG_Puts("mp3 encode init exit\n\r");
return TRUE;
#else
return FALSE;
#endif
}
//extern xa_error_info_struct xa_mp3_enc_error_info;
extern const xa_error_info_struct xa_mp3_enc_error_info;
extern xa_codec_handle_t p_xa_process_api_obj;
void app_mp3_encode_param_set (void)
{
/* Error code */
int err_code = XA_NO_ERROR;
xa_error_info_struct *p_proc_err_info = &xa_mp3_enc_error_info;
U32 ui_samp_freq;
U32 ui_num_chan = 2;
U32 ui_pcm_wd_sz = 16;
U32 ui_bit_rate;
#if 1//
ui_samp_freq = app_main_data.playing_stream_sample_rate;
#else
ui_samp_freq = 44100;
#endif
/* stream bit rate */
#if 1
//ui_bit_rate = app_main_data.record_bitrate;
ui_bit_rate = app_main_data.record_bitrate/1000;
#else
//ui_bitrate = 192;
ui_bitrate = 128;
#endif
/* The set config from argc argv */
int (*p_set_config_param)(xa_codec_handle_t p_xa_process_api_obj,
UWORD32 *pui_samp_freq,
UWORD32 *pui_num_chan,
UWORD32 *pui_pcm_wd_sz);
/* The get config from API */
//int (*p_get_config_param)(xa_codec_handle_t p_xa_process_api_obj,
// pUWORD32 pui_bitrate);
p_set_config_param = xa_mp3_enc_set_config_param;
err_code = (*p_set_config_param)(p_xa_process_api_obj,
&ui_samp_freq,
&ui_num_chan,
&ui_pcm_wd_sz);
_XA_HANDLE_ERROR(p_proc_err_info, "", err_code);
//p_get_config_param = xa_mp3_enc_get_config_param;
//p_error_init = xa_mp3_enc_error_handler_init;
//p_proc_err_info = &xa_mp3_enc_error_info;
}
/*******************************************************************************
*
* FUNCTION NAME - xa_mp3_enc_frame
*
* ARGUMENTS - psMessage: pointer to the input buffer
*
* RETURN VALUE - Error type
*
*******************************************************************************
*
* DESCRIPTION
* This function encodes one frame of data that is sent to the encoder module.
* The function assumes that the buffer given is continuous and does not wrap.
* The buffer is also assumed to have 1152 stereo samples. The input is
* assumed to the interleaved and so it is de-interleaved and then encoded.
* This function also increases the system clock to have sufficient MIPS for
* encoding. Once the encoding is over, the system clock is decreased back to
* the previous value.
*
******************************************************************************/
BOOL app_ad_pcm_encode_frame (I16 *ppBuf, I32 *len) __INTERNAL_RAM_TEXT;
//volatile U32 mp3_encodeing_count = 0;
static U16 ticks = 0;
BOOL app_ad_pcm_mp3_encode_frame (I16 *ppBuf, I32 len)
{
#if 1
//U8 uiIndex = 0;
//I32 dwEncLen;
U32 dwEncLen;
U32 w_size, r_size;
//static U8 ticks = 0;
//U8 *Mp3eTempOutBuf;
U8 *mp3enc_outbuf;
//release buf to receive
//sDacOutIsrBuf[wDacOutIndex].wSize = 0;
//DBG_Printf("Encode start %d\n\r");
//Encode one frame
#if 0
//for testing
fifo_put_data(&record_in_fifo, ppBuf, len);
//w_size = fifo_get_fifo_data_wlen(&record_in_fifo);
//if (w_size < 0x100000/2)
//if ( w_size < (RECORD_FIFO_BUF_END-RECORD_FIFO_BUF_START) / 2)
//{
// return;
//}
r_size = fifo_get_fifo_data_len(&record_in_fifo);
if (r_size < (MP3_ENC_FRAME_SAMPLE*4) )
{
//DBG_Printf("rec in buf underflow\n\r");
return;
}
r_size = fifo_get_data (&record_in_fifo, pcm_in_data_buf, (MP3_ENC_FRAME_SAMPLE*4));
//when mp3's encoding(sample rate:44.1kHz, bps:192k) one frame, the dwEncLen'value is 652, 712, 510 or 654 bytes.
//when mp3's encoding(sample rate:48kHz, bps:192k) one frame, the dwEncLen'value is 576, 613, 539 or 576 bytes.
xa_mp3_enc_frame((U16 *)pcm_in_data_buf, MP3_ENC_FRAME_SAMPLE, &mp3enc_outbuf, &dwEncLen);
return;
#endif
#ifdef AUDIO_OUT_BUF_IS_HALF_OF_MP3_ENC_FRAME_SAMPLE
DBG_Assert( (len == (MP3_ENC_FRAME_SAMPLE*2) ) );
pcm_in_data_index++;
#if (SHARE_LINK_ENC_CHANNEL_MODE == SL_MONO)
if (pcm_in_data_index == 1)
{
//CFasm_memcpy((void *)pcm_in_data_buf, (U16 *)ppBuf, (MP3_ENC_FRAME_SAMPLE*2));
optek_hifi2_stereo_16b_to_mono(pcm_in_data_buf,ppBuf,MP3_ENC_FRAME_SAMPLE);
return TRUE;
}
else if (pcm_in_data_index == 2)
{
//CFasm_memcpy((void *)(pcm_in_data_buf+(MP3_ENC_FRAME_SAMPLE*2)), (void *)ppBuf, (MP3_ENC_FRAME_SAMPLE*2));
optek_hifi2_stereo_16b_to_mono(pcm_in_data_buf+MP3_ENC_FRAME_SAMPLE,ppBuf,MP3_ENC_FRAME_SAMPLE);
xa_mp3_enc_frame((U16 *)pcm_in_data_buf, MP3_ENC_FRAME_SAMPLE, &mp3enc_outbuf, &dwEncLen);
pcm_in_data_index = 0;
}
#else
if (pcm_in_data_index == 1)
{
CFasm_memcpy((void *)pcm_in_data_buf, (U16 *)ppBuf, (MP3_ENC_FRAME_SAMPLE*2));
return TRUE;
}
else if (pcm_in_data_index == 2)
{
CFasm_memcpy((void *)(pcm_in_data_buf+(MP3_ENC_FRAME_SAMPLE*2)), (void *)ppBuf, (MP3_ENC_FRAME_SAMPLE*2));
xa_mp3_enc_frame((U16 *)pcm_in_data_buf, MP3_ENC_FRAME_SAMPLE, &mp3enc_outbuf, &dwEncLen);
pcm_in_data_index = 0;
}
#endif
#else
DBG_Assert( (len == (MP3_ENC_FRAME_SAMPLE*4) ) );
xa_mp3_enc_frame((U16 *)ppBuf, MP3_ENC_FRAME_SAMPLE, &mp3enc_outbuf, &dwEncLen);
#endif
app_main_data.playing_stream_pcm_sample += dwEncLen;
//DBG_Printf("len:%d\n\r", dwEncLen);
//DBG_Printf("Encode end\n\r");
//set copyright bit
//mp3enc_outbuf[3] |= 0x08;
{
U32 free_space;
STREAM_ID s;
free_space = fifo_get_free_space (&record_fifo);
//DBG_Printf("free space =0x%x\r\n", free_space);
//put the data to enc fifo
if (free_space >= dwEncLen)
{
//fifo_put_data (&record_fifo, Mp3eTempOutBuf, dwEncLen);
fifo_put_data (&record_fifo, mp3enc_outbuf, dwEncLen);
}
else
{
//encoded buf underflow
//DBG_Assert (FALSE);
//record_buf_overflow = TRUE;
ticks ++;
//if ( (ticks % 1000) == 0)
//if ( (ticks % 10) == 0)
{
DBG_Printf("rec fifo buf overflow\n\r");
}
}
//if (free_space < sizeof (record_buf)/2)
//{
// there are enough encoded data in fifo, write it to media
// DBG_Assert (FALSE);
//}
}
return TRUE;
#endif
}
BOOL app_mp3_cd_encode_frame (I16 *ppBuf, I32 len) __INTERNAL_RAM_TEXT;
BOOL app_mp3_cd_encode_frame (I16 *ppBuf, I32 len)
{
#if 0
U16 uiIndex;
//I32 dwEncLen;
U32 dwEncLen;
//static U8 ticks = 0;
//U8 *Mp3eTempOutBuf;
U8 *mp3enc_outbuf;
//release buf to receive
//sDacOutIsrBuf[wDacOutIndex].wSize = 0;
//DBG_Printf("Encode start %d\n\r");
// Encode one frame
xa_mp3_enc_frame((U16 *)ppBuf, MP3_ENC_FRAME_SAMPLE, &mp3enc_outbuf, &dwEncLen);
MP3_ENC_EVENT_SET;
//DBG_Printf("Encode end\n\r");
//set copyright bit
//mp3enc_outbuf[3] |= 0x08;
*len = dwEncLen;
{
U32 free_space;
STREAM_ID s;
free_space = fifo_get_free_space (&record_fifo);
#ifdef CD_ENABLE
if (app_main_data.media == MEDIA_CD)
{
s = app_sdram_stream_get_wstream();
#if 1
//if ( (s != INVALID_STREAM_ID) && (free_space < FIFO_ENC_DATA_MARGIN) &&
// (app_dac_get_cdservo_mute_state () == FALSE) )
#if 0
if ( (s != INVALID_STREAM_ID) && (free_space < (RECORD_BUF_SIZE/2)) &&
(app_dac_get_cdservo_mute_state () == FALSE) )
#else
if ( (s != INVALID_STREAM_ID) && (free_space < ((2*dwEncLen) + 16)) &&
(app_dac_get_cdservo_mute_state () == FALSE) )
#endif
{
//encoding buf is small
#if 1
DBG_Assert (FALSE);
DBG_Printf ("rec fifo buf over1\n\r");
app_dac_cdservo_disable ();
/*buf is too small, no need to set the point*/
app_sdram_stream_set_wlen (s, app_cdrom_addr.stream_wlen);
record_buf_overflow = TRUE;
//app_lcd_device.display_icon(ICON_SLOW, TRUE);
//app_nav_sleep(1000);
//app_lcd_device.display_icon(ICON_SLOW, FALSE);
#if 0
app_cdrom_addr.tries = 0;
app_cdrom_addr.match = 0;
uMsgSend (UI_CD_RECEIVER, UI_CDDA_EF_ERR, 0);
#endif
#endif
}
#endif
}
#endif //CD_ENABLE
//put the data to enc fifo
if (free_space >= dwEncLen)
{
//fifo_put_data (&record_fifo, Mp3eTempOutBuf, dwEncLen);
fifo_put_data (&record_fifo, mp3enc_outbuf, dwEncLen);
ticks ++;
if ( (ticks % 1000) == 0)
{
DBG_Printf("rec fifo buf overflow\n\r");
}
}
else
{
//encoded buf underflow
record_buf_overflow = TRUE;
//DBG_Assert (FALSE);
//Exit wav recording
//app_main_data.record_status = FALSE;
//app_media_mute_disable();
ticks ++;
if ( (ticks % 1000) == 0)
{
DBG_Printf("rec fifo buf overflow\n\r");
}
}
//if (free_space < sizeof (record_buf)/2)
//{
//there are enough encoded data in fifo, write it to media
//DBG_Assert (FALSE);
//}
}
//return eRetCode;
return TRUE;
#endif
}
#endif //MP3_ENCODE_ENABLE