1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 /* All swcodec targets have BUTTON_SELECT apart from the H10 and M3 */
27 #if CONFIG_KEYPAD == IRIVER_H10_PAD
28 #define TESTCODEC_EXITBUTTON BUTTON_RIGHT
29 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
30 #define TESTCODEC_EXITBUTTON BUTTON_RC_PLAY
31 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
32 #define TESTCODEC_EXITBUTTON BUTTON_PLAY
33 #elif CONFIG_KEYPAD == COWON_D2_PAD || CONFIG_KEYPAD == ONDAVX747_PAD
34 #define TESTCODEC_EXITBUTTON BUTTON_POWER
35 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
36 #define TESTCODEC_EXITBUTTON BUTTON_REC
37 #elif defined(HAVE_TOUCHSCREEN)
38 #define TESTCODEC_EXITBUTTON BUTTON_TOPLEFT
40 #define TESTCODEC_EXITBUTTON BUTTON_SELECT
43 /* Log functions copied from test_disk.c */
45 static int max_line
= 0;
46 static int log_fd
= -1;
47 static char logfilename
[MAX_PATH
];
49 static bool log_init(bool use_logfile
)
53 rb
->lcd_getstringsize("A", NULL
, &h
);
54 max_line
= LCD_HEIGHT
/ h
;
56 rb
->lcd_clear_display();
60 rb
->create_numbered_filename(logfilename
, "/", "test_codec_log_", ".txt",
61 2 IF_CNFN_NUM_(, NULL
));
62 log_fd
= rb
->open(logfilename
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
69 static void log_text(char *text
, bool advance
)
71 rb
->lcd_puts(0, line
, text
);
75 if (++line
>= max_line
)
78 rb
->fdprintf(log_fd
, "%s\n", text
);
82 static void log_close(void)
98 static void* audiobuf
;
99 static void* codec_mallocbuf
;
100 static size_t audiosize
;
101 static char str
[MAX_PATH
];
103 /* Our local implementation of the codec API */
104 static struct codec_api ci
;
106 struct test_track_info
{
107 struct mp3entry id3
; /* TAG metadata */
108 size_t filesize
; /* File total length */
111 static struct test_track_info track
;
112 static bool taginfo_ready
= true;
116 static bool checksum
;
117 static uint32_t crc32
;
119 static volatile unsigned int elapsed
;
120 static volatile bool codec_playing
;
121 static volatile long endtick
;
122 struct wavinfo_t wavinfo
;
124 static unsigned char wav_header
[44] =
126 'R','I','F','F', // 0 - ChunkID
127 0,0,0,0, // 4 - ChunkSize (filesize-8)
128 'W','A','V','E', // 8 - Format
129 'f','m','t',' ', // 12 - SubChunkID
130 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
131 1,0, // 20 - AudioFormat (1=16-bit)
132 0,0, // 22 - NumChannels
133 0,0,0,0, // 24 - SampleRate in Hz
134 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
135 0,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
136 16,0, // 34 - BitsPerSample
137 'd','a','t','a', // 36 - Subchunk2ID
138 0,0,0,0 // 40 - Subchunk2Size
141 static inline void int2le32(unsigned char* buf
, int32_t x
)
144 buf
[1] = (x
& 0xff00) >> 8;
145 buf
[2] = (x
& 0xff0000) >> 16;
146 buf
[3] = (x
& 0xff000000) >>24;
149 static inline void int2le24(unsigned char* buf
, int32_t x
)
152 buf
[1] = (x
& 0xff00) >> 8;
153 buf
[2] = (x
& 0xff0000) >> 16;
156 static inline void int2le16(unsigned char* buf
, int16_t x
)
159 buf
[1] = (x
& 0xff00) >> 8;
162 /* 32KB should be enough */
163 static unsigned char wavbuffer
[32*1024];
164 static unsigned char dspbuffer
[32*1024];
166 void init_wav(char* filename
)
168 wavinfo
.totalsamples
= 0;
170 wavinfo
.fd
= rb
->creat(filename
, 0666);
174 /* Write WAV header - we go back and fill in the details at the end */
175 rb
->write(wavinfo
.fd
, wav_header
, sizeof(wav_header
));
182 int filesize
= rb
->filesize(wavinfo
.fd
);
183 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
184 int bps
= 16; /* TODO */
186 /* We assume 16-bit, Stereo */
188 rb
->lseek(wavinfo
.fd
,0,SEEK_SET
);
190 int2le32(wav_header
+4, filesize
-8); /* ChunkSize */
192 int2le16(wav_header
+22, channels
);
194 int2le32(wav_header
+24, wavinfo
.samplerate
);
196 int2le32(wav_header
+28, wavinfo
.samplerate
* channels
* (bps
/ 8)); /* ByteRate */
198 int2le16(wav_header
+32, channels
* (bps
/ 8));
200 int2le32(wav_header
+40, filesize
- 44); /* Subchunk2Size */
202 rb
->write(wavinfo
.fd
, wav_header
, sizeof(wav_header
));
204 rb
->close(wavinfo
.fd
);
207 /* Returns buffer to malloc array. Only codeclib should need this. */
208 static void* codec_get_buffer(size_t *size
)
210 DEBUGF("codec_get_buffer(%d)\n",(int)size
);
212 return codec_mallocbuf
;
215 static int process_dsp(const void *ch1
, const void *ch2
, int count
)
217 const char *src
[2] = { ch1
, ch2
};
218 int written_count
= 0;
219 char *dest
= dspbuffer
;
223 int out_count
= rb
->dsp_output_count(ci
.dsp
, count
);
225 int inp_count
= rb
->dsp_input_count(ci
.dsp
, out_count
);
230 if (inp_count
> count
)
233 out_count
= rb
->dsp_process(ci
.dsp
, dest
, src
, inp_count
);
238 written_count
+= out_count
;
239 dest
+= out_count
* 4;
244 return written_count
;
247 static inline int32_t clip_sample(int32_t sample
)
249 if ((int16_t)sample
!= sample
)
250 sample
= 0x7fff ^ (sample
>> 31);
256 static void pcmbuf_insert_null(const void *ch1
, const void *ch2
, int count
)
259 process_dsp(ch1
, ch2
, count
);
261 /* Prevent idle poweroff */
262 rb
->reset_poweroff_timer();
265 /* WAV output or calculate crc32 of output*/
266 static void pcmbuf_insert_wav_checksum(const void *ch1
, const void *ch2
, int count
)
268 const int16_t* data1_16
;
269 const int16_t* data2_16
;
270 const int32_t* data1_32
;
271 const int32_t* data2_32
;
272 unsigned char* p
= wavbuffer
;
273 const int scale
= wavinfo
.sampledepth
- 15;
274 const int dc_bias
= 1 << (scale
- 1);
275 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
277 /* Prevent idle poweroff */
278 rb
->reset_poweroff_timer();
281 count
= process_dsp(ch1
, ch2
, count
);
282 wavinfo
.totalsamples
+= count
;
285 unsigned char *s
= dspbuffer
, *d
= dspbuffer
;
296 crc32
= rb
->crc_32(dspbuffer
, count
* 2 * channels
, crc32
);
298 rb
->write(wavinfo
.fd
, dspbuffer
, count
* 2 * channels
);
302 if (wavinfo
.sampledepth
<= 16) {
306 switch(wavinfo
.stereomode
)
308 case STEREO_INTERLEAVED
:
310 int2le16(p
,*data1_16
++);
312 int2le16(p
,*data1_16
++);
317 case STEREO_NONINTERLEAVED
:
319 int2le16(p
,*data1_16
++);
321 int2le16(p
,*data2_16
++);
329 int2le16(p
,*data1_16
++);
338 switch(wavinfo
.stereomode
)
340 case STEREO_INTERLEAVED
:
342 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
344 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
349 case STEREO_NONINTERLEAVED
:
351 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
353 int2le16(p
, clip_sample((*data2_32
++ + dc_bias
) >> scale
));
361 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
368 wavinfo
.totalsamples
+= count
;
370 crc32
= rb
->crc_32(wavbuffer
, p
- wavbuffer
, crc32
);
372 rb
->write(wavinfo
.fd
, wavbuffer
, p
- wavbuffer
);
376 /* Set song position in WPS (value in ms). */
377 static void set_elapsed(unsigned long value
)
383 /* Read next <size> amount bytes from file buffer to <ptr>.
384 Will return number of bytes read or 0 if end of file. */
385 static size_t read_filebuf(void *ptr
, size_t size
)
387 if (ci
.curpos
> (off_t
)track
.filesize
)
391 /* TODO: Don't read beyond end of buffer */
392 rb
->memcpy(ptr
, audiobuf
+ ci
.curpos
, size
);
399 /* Request pointer to file buffer which can be used to read
400 <realsize> amount of data. <reqsize> tells the buffer system
401 how much data it should try to allocate. If <realsize> is 0,
402 end of file is reached. */
403 static void* request_buffer(size_t *realsize
, size_t reqsize
)
405 *realsize
= MIN(track
.filesize
-ci
.curpos
,reqsize
);
407 return (audiobuf
+ ci
.curpos
);
411 /* Advance file buffer position by <amount> amount of bytes. */
412 static void advance_buffer(size_t amount
)
418 /* Advance file buffer to a pointer location inside file buffer. */
419 static void advance_buffer_loc(void *ptr
)
421 ci
.curpos
= ptr
- audiobuf
;
425 /* Seek file buffer to position <newpos> beginning of file. */
426 static bool seek_buffer(size_t newpos
)
433 /* Codec should call this function when it has done the seeking. */
434 static void seek_complete(void)
439 /* Request file change from file buffer. Returns true is next
440 track is available and changed. If return value is false,
441 codec should exit immediately with PLUGIN_OK status. */
442 static bool request_next_track(void)
444 /* We are only decoding a single track */
449 /* Free the buffer area of the current codec after its loaded */
450 static void discard_codec(void)
456 static void set_offset(size_t value
)
463 /* Configure different codec buffer parameters. */
464 static void configure(int setting
, intptr_t value
)
467 rb
->dsp_configure(ci
.dsp
, setting
, value
);
470 case DSP_SWITCH_FREQUENCY
:
471 case DSP_SET_FREQUENCY
:
472 DEBUGF("samplerate=%d\n",(int)value
);
473 wavinfo
.samplerate
= (int)value
;
476 case DSP_SET_SAMPLE_DEPTH
:
477 DEBUGF("sampledepth = %d\n",(int)value
);
478 wavinfo
.sampledepth
=(int)value
;
481 case DSP_SET_STEREO_MODE
:
482 DEBUGF("Stereo mode = %d\n",(int)value
);
483 wavinfo
.stereomode
=(int)value
;
489 static void init_ci(void)
491 /* --- Our "fake" implementations of the codec API functions. --- */
493 ci
.codec_get_buffer
= codec_get_buffer
;
495 if (wavinfo
.fd
>= 0 || checksum
) {
496 ci
.pcmbuf_insert
= pcmbuf_insert_wav_checksum
;
498 ci
.pcmbuf_insert
= pcmbuf_insert_null
;
501 ci
.set_elapsed
= set_elapsed
;
502 ci
.read_filebuf
= read_filebuf
;
503 ci
.request_buffer
= request_buffer
;
504 ci
.advance_buffer
= advance_buffer
;
505 ci
.advance_buffer_loc
= advance_buffer_loc
;
506 ci
.seek_buffer
= seek_buffer
;
507 ci
.seek_complete
= seek_complete
;
508 ci
.request_next_track
= request_next_track
;
509 ci
.discard_codec
= discard_codec
;
510 ci
.set_offset
= set_offset
;
511 ci
.configure
= configure
;
512 ci
.dsp
= (struct dsp_config
*)rb
->dsp_configure(NULL
, DSP_MYDSP
,
515 /* --- "Core" functions --- */
518 ci
.sleep
= rb
->sleep
;
519 ci
.yield
= rb
->yield
;
521 /* strings and memory */
522 ci
.strcpy
= rb
->strcpy
;
523 ci
.strlen
= rb
->strlen
;
524 ci
.strcmp
= rb
->strcmp
;
525 ci
.strcat
= rb
->strcat
;
526 ci
.memset
= rb
->memset
;
527 ci
.memcpy
= rb
->memcpy
;
528 ci
.memmove
= rb
->memmove
;
529 ci
.memcmp
= rb
->memcmp
;
530 ci
.memchr
= rb
->memchr
;
531 ci
.strcasestr
= rb
->strcasestr
;
532 #if defined(DEBUG) || defined(SIMULATOR)
533 ci
.debugf
= rb
->debugf
;
535 #ifdef ROCKBOX_HAS_LOGF
539 ci
.qsort
= rb
->qsort
;
540 ci
.global_settings
= rb
->global_settings
;
543 ci
.profile_thread
= rb
->profile_thread
;
544 ci
.profstop
= rb
->profstop
;
545 ci
.profile_func_enter
= rb
->profile_func_enter
;
546 ci
.profile_func_exit
= rb
->profile_func_exit
;
550 ci
.cpucache_invalidate
= rb
->cpucache_invalidate
;
551 ci
.cpucache_flush
= rb
->cpucache_flush
;
555 ci
.create_thread
= rb
->create_thread
;
556 ci
.thread_thaw
= rb
->thread_thaw
;
557 ci
.thread_wait
= rb
->thread_wait
;
558 ci
.semaphore_init
= rb
->semaphore_init
;
559 ci
.semaphore_wait
= rb
->semaphore_wait
;
560 ci
.semaphore_release
= rb
->semaphore_release
;
564 ci
.__div0
= rb
->__div0
;
568 static void codec_thread(void)
570 const char* codecname
;
573 codecname
= rb
->get_codec_filename(track
.id3
.codectype
);
575 /* Load the codec and start decoding. */
576 res
= rb
->codec_load_file(codecname
,&ci
);
578 /* Signal to the main thread that we are done */
579 endtick
= *rb
->current_tick
;
580 codec_playing
= false;
583 static enum plugin_status
test_track(const char* filename
)
587 enum plugin_status res
= PLUGIN_ERROR
;
591 unsigned long duration
;
594 /* Display filename (excluding any path)*/
595 ch
= rb
->strrchr(filename
, '/');
601 rb
->snprintf(str
,sizeof(str
),"%s",ch
);
604 log_text("Loading...",false);
606 fd
= rb
->open(filename
,O_RDONLY
);
609 log_text("Cannot open file",true);
613 track
.filesize
= rb
->filesize(fd
);
615 /* Clear the id3 struct */
616 rb
->memset(&track
.id3
, 0, sizeof(struct mp3entry
));
618 if (!rb
->get_metadata(&(track
.id3
), fd
, filename
))
620 log_text("Cannot read metadata",true);
624 if (track
.filesize
> audiosize
)
626 log_text("File too large",true);
630 n
= rb
->read(fd
, audiobuf
, track
.filesize
);
632 if (n
!= track
.filesize
)
634 log_text("Read failed.",true);
638 /* Initialise the function pointers in the codec API */
641 /* Prepare the codec struct for playing the whole file */
642 ci
.filesize
= track
.filesize
;
644 ci
.taginfo_ready
= &taginfo_ready
;
646 ci
.stop_codec
= false;
651 rb
->dsp_configure(ci
.dsp
, DSP_RESET
, 0);
656 starttick
= *rb
->current_tick
;
658 codec_playing
= true;
660 rb
->codec_thread_do_callback(codec_thread
, NULL
);
662 /* Wait for codec thread to die */
663 while (codec_playing
)
666 rb
->snprintf(str
,sizeof(str
),"%d of %d",elapsed
,(int)track
.id3
.length
);
669 ticks
= endtick
- starttick
;
671 /* Be sure it is done */
672 rb
->codec_thread_do_callback(NULL
, NULL
);
678 rb
->snprintf(str
, sizeof(str
), "CRC32 - %x", (unsigned)crc32
);
681 else if (wavinfo
.fd
< 0)
683 /* Display benchmark information */
684 rb
->snprintf(str
,sizeof(str
),"Decode time - %d.%02ds",(int)ticks
/100,(int)ticks
%100);
687 duration
= track
.id3
.length
/ 10;
688 rb
->snprintf(str
,sizeof(str
),"File duration - %d.%02ds",(int)duration
/100,(int)duration
%100);
692 speed
= duration
* 10000 / ticks
;
696 rb
->snprintf(str
,sizeof(str
),"%d.%02d%% realtime",(int)speed
/100,(int)speed
%100);
700 /* show effective clockrate in MHz needed for realtime decoding */
703 speed
= CPUFREQ_MAX
/ speed
;
704 rb
->snprintf(str
,sizeof(str
),"%d.%02dMHz needed for realtime",
705 (int)speed
/100,(int)speed
%100);
724 /* plugin entry point */
725 enum plugin_status
plugin_start(const void* parameter
)
727 int result
, selection
= 0;
728 enum plugin_status res
= PLUGIN_OK
;
730 struct dirent
*entry
;
733 char dirpath
[MAX_PATH
];
734 char filename
[MAX_PATH
];
736 if (parameter
== NULL
)
738 rb
->splash(HZ
*2, "No File");
742 codec_mallocbuf
= rb
->plugin_get_audio_buffer(&audiosize
);
743 audiobuf
= SKIPBYTES(codec_mallocbuf
, CODEC_SIZE
);
744 audiosize
-= CODEC_SIZE
;
746 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
749 rb
->lcd_clear_display();
758 SPEED_TEST_DIR_WITH_DSP
,
766 menu
, "test_codec", NULL
,
770 "Speed test with DSP",
771 "Speed test folder with DSP",
772 "Write WAV with DSP",
779 rb
->lcd_clear_display();
781 result
= rb
->do_menu(&menu
, &selection
, NULL
, false);
791 if ((checksum
= (result
== CHECKSUM
|| result
== CHECKSUM_DIR
)))
794 if ((use_dsp
= ((result
>= SPEED_TEST_WITH_DSP
)
795 && (result
<= WRITE_WAV_WITH_DSP
)))) {
798 if (result
== SPEED_TEST
) {
801 } else if (result
== SPEED_TEST_DIR
) {
805 /* Only create a log file when we are testing a folder */
806 if (!log_init(true)) {
807 rb
->splash(HZ
*2, "Cannot create logfile");
811 } else if (result
== WRITE_WAV
) {
813 init_wav("/test.wav");
814 if (wavinfo
.fd
< 0) {
815 rb
->splash(HZ
*2, "Cannot create /test.wav");
819 } else if (result
== MENU_ATTACHED_USB
) {
820 res
= PLUGIN_USB_CONNECTED
;
822 } else if (result
< 0) {
828 /* Test all files in the same directory as the file selected by the
831 rb
->strlcpy(dirpath
,parameter
,sizeof(dirpath
));
832 ch
= rb
->strrchr(dirpath
,'/');
835 DEBUGF("Scanning directory \"%s\"\n",dirpath
);
836 dir
= rb
->opendir(dirpath
);
838 entry
= rb
->readdir(dir
);
840 if (!(entry
->attribute
& ATTR_DIRECTORY
)) {
841 rb
->snprintf(filename
,sizeof(filename
),"%s%s",dirpath
,entry
->d_name
);
842 test_track(filename
);
846 /* Read next entry */
847 entry
= rb
->readdir(dir
);
853 /* Just test the file */
854 res
= test_track(parameter
);
856 /* Close WAV file (if there was one) */
857 if (wavinfo
.fd
>= 0) {
859 log_text("Wrote /test.wav",true);
861 while (rb
->button_get(true) != TESTCODEC_EXITBUTTON
);
868 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
869 rb
->cpu_boost(false);