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 || CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
35 #define TESTCODEC_EXITBUTTON BUTTON_POWER
36 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
37 #define TESTCODEC_EXITBUTTON BUTTON_REC
38 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
39 #define TESTCODEC_EXITBUTTON (BUTTON_REC | BUTTON_PLAY)
40 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
41 #define TESTCODEC_EXITBUTTON (BUTTON_REC | BUTTON_REPEAT)
42 #elif defined(HAVE_TOUCHSCREEN)
43 #define TESTCODEC_EXITBUTTON BUTTON_TOPLEFT
45 #define TESTCODEC_EXITBUTTON BUTTON_SELECT
48 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
49 static unsigned int boost
=1;
51 static const struct opt_items boost_settings
[2] = {
58 /* Log functions copied from test_disk.c */
60 static int max_line
= 0;
61 static int log_fd
= -1;
63 static void log_close(void)
69 static bool log_init(bool use_logfile
)
72 char logfilename
[MAX_PATH
];
74 rb
->lcd_getstringsize("A", NULL
, &h
);
75 max_line
= LCD_HEIGHT
/ h
;
77 rb
->lcd_clear_display();
82 rb
->create_numbered_filename(logfilename
, "/", "test_codec_log_", ".txt",
83 2 IF_CNFN_NUM_(, NULL
));
84 log_fd
= rb
->open(logfilename
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
91 static void log_text(char *text
, bool advance
)
93 rb
->lcd_puts(0, line
, text
);
97 if (++line
>= max_line
)
100 rb
->fdprintf(log_fd
, "%s\n", text
);
114 static void* audiobuf
;
115 static void* codec_mallocbuf
;
116 static size_t audiosize
;
117 static size_t audiobufsize
;
121 /* Our local implementation of the codec API */
122 static struct codec_api ci
;
124 struct test_track_info
{
125 struct mp3entry id3
; /* TAG metadata */
126 size_t filesize
; /* File total length */
129 static struct test_track_info track
;
133 static bool checksum
;
134 static uint32_t crc32
;
136 static volatile unsigned int elapsed
;
137 static volatile bool codec_playing
;
138 static volatile long endtick
;
139 static volatile long rebuffertick
;
140 struct wavinfo_t wavinfo
;
142 static unsigned char wav_header
[44] =
144 'R','I','F','F', // 0 - ChunkID
145 0,0,0,0, // 4 - ChunkSize (filesize-8)
146 'W','A','V','E', // 8 - Format
147 'f','m','t',' ', // 12 - SubChunkID
148 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
149 1,0, // 20 - AudioFormat (1=16-bit)
150 0,0, // 22 - NumChannels
151 0,0,0,0, // 24 - SampleRate in Hz
152 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
153 0,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
154 16,0, // 34 - BitsPerSample
155 'd','a','t','a', // 36 - Subchunk2ID
156 0,0,0,0 // 40 - Subchunk2Size
159 static inline void int2le32(unsigned char* buf
, int32_t x
)
162 buf
[1] = (x
& 0xff00) >> 8;
163 buf
[2] = (x
& 0xff0000) >> 16;
164 buf
[3] = (x
& 0xff000000) >>24;
167 static inline void int2le24(unsigned char* buf
, int32_t x
)
170 buf
[1] = (x
& 0xff00) >> 8;
171 buf
[2] = (x
& 0xff0000) >> 16;
174 static inline void int2le16(unsigned char* buf
, int16_t x
)
177 buf
[1] = (x
& 0xff00) >> 8;
180 static unsigned char *wavbuffer
;
181 static unsigned char *dspbuffer
;
183 void init_wav(char* filename
)
185 wavinfo
.totalsamples
= 0;
187 wavinfo
.fd
= rb
->creat(filename
, 0666);
191 /* Write WAV header - we go back and fill in the details at the end */
192 rb
->write(wavinfo
.fd
, wav_header
, sizeof(wav_header
));
199 int filesize
= rb
->filesize(wavinfo
.fd
);
200 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
201 int bps
= 16; /* TODO */
203 /* We assume 16-bit, Stereo */
205 rb
->lseek(wavinfo
.fd
,0,SEEK_SET
);
207 int2le32(wav_header
+4, filesize
-8); /* ChunkSize */
209 int2le16(wav_header
+22, channels
);
211 int2le32(wav_header
+24, wavinfo
.samplerate
);
213 int2le32(wav_header
+28, wavinfo
.samplerate
* channels
* (bps
/ 8)); /* ByteRate */
215 int2le16(wav_header
+32, channels
* (bps
/ 8));
217 int2le32(wav_header
+40, filesize
- 44); /* Subchunk2Size */
219 rb
->write(wavinfo
.fd
, wav_header
, sizeof(wav_header
));
221 rb
->close(wavinfo
.fd
);
224 /* Returns buffer to malloc array. Only codeclib should need this. */
225 static void* codec_get_buffer(size_t *size
)
227 DEBUGF("codec_get_buffer(%"PRIuPTR
")\n",(uintptr_t)size
);
229 return codec_mallocbuf
;
232 static int process_dsp(const void *ch1
, const void *ch2
, int count
)
234 const char *src
[2] = { ch1
, ch2
};
235 int written_count
= 0;
236 char *dest
= dspbuffer
;
240 int out_count
= rb
->dsp_output_count(ci
.dsp
, count
);
242 int inp_count
= rb
->dsp_input_count(ci
.dsp
, out_count
);
247 if (inp_count
> count
)
250 out_count
= rb
->dsp_process(ci
.dsp
, dest
, src
, inp_count
);
255 written_count
+= out_count
;
256 dest
+= out_count
* 4;
261 return written_count
;
264 static inline int32_t clip_sample(int32_t sample
)
266 if ((int16_t)sample
!= sample
)
267 sample
= 0x7fff ^ (sample
>> 31);
273 static void pcmbuf_insert_null(const void *ch1
, const void *ch2
, int count
)
276 process_dsp(ch1
, ch2
, count
);
278 /* Prevent idle poweroff */
279 rb
->reset_poweroff_timer();
283 * Helper function used when the file is larger then the available memory.
284 * Rebuffers the file by setting the start of the audio buffer to be
285 * new_offset and filling from there.
287 static int fill_buffer(int new_offset
){
288 size_t n
, bytestoread
;
289 long temp
= *rb
->current_tick
;
290 rb
->lseek(fd
,new_offset
,SEEK_SET
);
292 if(new_offset
+ audiobufsize
<= track
.filesize
)
293 bytestoread
= audiobufsize
;
295 bytestoread
= track
.filesize
-new_offset
;
297 n
= rb
->read(fd
, audiobuf
,bytestoread
);
299 if (n
!= bytestoread
)
301 log_text("Read failed.",true);
302 DEBUGF("read fail: got %d bytes, expected %d\n", (int)n
, (int)audiobufsize
);
314 /*keep track of how much time we spent buffering*/
315 rebuffertick
+= *rb
->current_tick
-temp
;
320 /* WAV output or calculate crc32 of output*/
321 static void pcmbuf_insert_wav_checksum(const void *ch1
, const void *ch2
, int count
)
323 const int16_t* data1_16
;
324 const int16_t* data2_16
;
325 const int32_t* data1_32
;
326 const int32_t* data2_32
;
327 unsigned char* p
= wavbuffer
;
328 const int scale
= wavinfo
.sampledepth
- 15;
329 const int dc_bias
= 1 << (scale
- 1);
330 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
332 /* Prevent idle poweroff */
333 rb
->reset_poweroff_timer();
336 count
= process_dsp(ch1
, ch2
, count
);
337 wavinfo
.totalsamples
+= count
;
340 unsigned char *s
= dspbuffer
, *d
= dspbuffer
;
351 crc32
= rb
->crc_32(dspbuffer
, count
* 2 * channels
, crc32
);
353 rb
->write(wavinfo
.fd
, dspbuffer
, count
* 2 * channels
);
357 if (wavinfo
.sampledepth
<= 16) {
361 switch(wavinfo
.stereomode
)
363 case STEREO_INTERLEAVED
:
365 int2le16(p
,*data1_16
++);
367 int2le16(p
,*data1_16
++);
372 case STEREO_NONINTERLEAVED
:
374 int2le16(p
,*data1_16
++);
376 int2le16(p
,*data2_16
++);
384 int2le16(p
,*data1_16
++);
393 switch(wavinfo
.stereomode
)
395 case STEREO_INTERLEAVED
:
397 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
399 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
404 case STEREO_NONINTERLEAVED
:
406 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
408 int2le16(p
, clip_sample((*data2_32
++ + dc_bias
) >> scale
));
416 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
423 wavinfo
.totalsamples
+= count
;
425 crc32
= rb
->crc_32(wavbuffer
, p
- wavbuffer
, crc32
);
427 rb
->write(wavinfo
.fd
, wavbuffer
, p
- wavbuffer
);
431 /* Set song position in WPS (value in ms). */
432 static void set_elapsed(unsigned long value
)
435 ci
.id3
->elapsed
= value
;
439 /* Read next <size> amount bytes from file buffer to <ptr>.
440 Will return number of bytes read or 0 if end of file. */
441 static size_t read_filebuf(void *ptr
, size_t size
)
443 if (ci
.curpos
> (off_t
)track
.filesize
)
447 size_t realsize
= MIN(track
.filesize
-ci
.curpos
,size
);
449 /* check if we have enough bytes ready*/
450 if(realsize
>(audiobufsize
- (ci
.curpos
-offset
)))
452 /*rebuffer so that we start at ci.curpos*/
453 fill_buffer(ci
.curpos
);
456 rb
->memcpy(ptr
, audiobuf
+ (ci
.curpos
-offset
), realsize
);
457 ci
.curpos
+= realsize
;
463 /* Request pointer to file buffer which can be used to read
464 <realsize> amount of data. <reqsize> tells the buffer system
465 how much data it should try to allocate. If <realsize> is 0,
466 end of file is reached. */
467 static void* request_buffer(size_t *realsize
, size_t reqsize
)
469 *realsize
= MIN(track
.filesize
-ci
.curpos
,reqsize
);
471 /*check if we have enough bytes ready - requested > bufsize-currentbufpos*/
472 if(*realsize
>(audiobufsize
- (ci
.curpos
-offset
)))
474 /*rebuffer so that we start at ci.curpos*/
475 fill_buffer(ci
.curpos
);
478 return (audiobuf
+ (ci
.curpos
-offset
));
481 /* Advance file buffer position by <amount> amount of bytes. */
482 static void advance_buffer(size_t amount
)
485 ci
.id3
->offset
= ci
.curpos
;
489 /* Seek file buffer to position <newpos> beginning of file. */
490 static bool seek_buffer(size_t newpos
)
497 /* Codec should call this function when it has done the seeking. */
498 static void seek_complete(void)
503 /* Codec calls this to know what it should do next. */
504 static enum codec_command_action
get_command(intptr_t *param
)
507 return CODEC_ACTION_NULL
; /* just continue processing */
511 static void set_offset(size_t value
)
513 ci
.id3
->offset
= value
;
517 /* Configure different codec buffer parameters. */
518 static void configure(int setting
, intptr_t value
)
521 rb
->dsp_configure(ci
.dsp
, setting
, value
);
524 case DSP_SWITCH_FREQUENCY
:
525 case DSP_SET_FREQUENCY
:
526 DEBUGF("samplerate=%d\n",(int)value
);
527 wavinfo
.samplerate
= (int)value
;
530 case DSP_SET_SAMPLE_DEPTH
:
531 DEBUGF("sampledepth = %d\n",(int)value
);
532 wavinfo
.sampledepth
=(int)value
;
535 case DSP_SET_STEREO_MODE
:
536 DEBUGF("Stereo mode = %d\n",(int)value
);
537 wavinfo
.stereomode
=(int)value
;
543 static void init_ci(void)
545 /* --- Our "fake" implementations of the codec API functions. --- */
547 ci
.dsp
= (struct dsp_config
*)rb
->dsp_configure(NULL
, DSP_MYDSP
,
550 ci
.codec_get_buffer
= codec_get_buffer
;
552 if (wavinfo
.fd
>= 0 || checksum
) {
553 ci
.pcmbuf_insert
= pcmbuf_insert_wav_checksum
;
555 ci
.pcmbuf_insert
= pcmbuf_insert_null
;
558 ci
.set_elapsed
= set_elapsed
;
559 ci
.read_filebuf
= read_filebuf
;
560 ci
.request_buffer
= request_buffer
;
561 ci
.advance_buffer
= advance_buffer
;
562 ci
.seek_buffer
= seek_buffer
;
563 ci
.seek_complete
= seek_complete
;
564 ci
.set_offset
= set_offset
;
565 ci
.configure
= configure
;
566 ci
.get_command
= get_command
;
568 /* --- "Core" functions --- */
571 ci
.sleep
= rb
->sleep
;
572 ci
.yield
= rb
->yield
;
574 /* strings and memory */
575 ci
.strcpy
= rb
->strcpy
;
576 ci
.strlen
= rb
->strlen
;
577 ci
.strcmp
= rb
->strcmp
;
578 ci
.strcat
= rb
->strcat
;
579 ci
.memset
= rb
->memset
;
580 ci
.memcpy
= rb
->memcpy
;
581 ci
.memmove
= rb
->memmove
;
582 ci
.memcmp
= rb
->memcmp
;
583 ci
.memchr
= rb
->memchr
;
584 ci
.strcasestr
= rb
->strcasestr
;
585 #if defined(DEBUG) || defined(SIMULATOR)
586 ci
.debugf
= rb
->debugf
;
588 #ifdef ROCKBOX_HAS_LOGF
592 ci
.qsort
= rb
->qsort
;
593 ci
.global_settings
= rb
->global_settings
;
596 ci
.profile_thread
= rb
->profile_thread
;
597 ci
.profstop
= rb
->profstop
;
598 ci
.profile_func_enter
= rb
->profile_func_enter
;
599 ci
.profile_func_exit
= rb
->profile_func_exit
;
602 ci
.cpucache_invalidate
= rb
->cpucache_invalidate
;
603 ci
.cpucache_flush
= rb
->cpucache_flush
;
606 ci
.create_thread
= rb
->create_thread
;
607 ci
.thread_thaw
= rb
->thread_thaw
;
608 ci
.thread_wait
= rb
->thread_wait
;
609 ci
.semaphore_init
= rb
->semaphore_init
;
610 ci
.semaphore_wait
= rb
->semaphore_wait
;
611 ci
.semaphore_release
= rb
->semaphore_release
;
614 #if defined(CPU_ARM) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
615 ci
.__div0
= rb
->__div0
;
619 static void codec_thread(void)
621 const char* codecname
;
624 codecname
= rb
->get_codec_filename(track
.id3
.codectype
);
627 res
= rb
->codec_load_file(codecname
, &ci
);
631 /* Decode the file */
632 res
= rb
->codec_run_proc();
638 /* Signal to the main thread that we are done */
639 endtick
= *rb
->current_tick
- rebuffertick
;
640 codec_playing
= false;
643 static enum plugin_status
test_track(const char* filename
)
646 enum plugin_status res
= PLUGIN_ERROR
;
650 unsigned long duration
;
655 /* Display filename (excluding any path)*/
656 ch
= rb
->strrchr(filename
, '/');
662 rb
->snprintf(str
,sizeof(str
),"%s",ch
);
665 log_text("Loading...",false);
667 fd
= rb
->open(filename
,O_RDONLY
);
670 log_text("Cannot open file",true);
674 track
.filesize
= rb
->filesize(fd
);
676 /* Clear the id3 struct */
677 rb
->memset(&track
.id3
, 0, sizeof(struct mp3entry
));
679 if (!rb
->get_metadata(&(track
.id3
), fd
, filename
))
681 log_text("Cannot read metadata",true);
685 if (track
.filesize
> audiosize
)
687 audiobufsize
=audiosize
;
691 audiobufsize
=track
.filesize
;
694 n
= rb
->read(fd
, audiobuf
, audiobufsize
);
696 if (n
!= audiobufsize
)
698 log_text("Read failed.",true);
703 /* Initialise the function pointers in the codec API */
706 /* Prepare the codec struct for playing the whole file */
707 ci
.filesize
= track
.filesize
;
712 rb
->dsp_configure(ci
.dsp
, DSP_RESET
, 0);
718 starttick
= *rb
->current_tick
;
720 codec_playing
= true;
722 rb
->codec_thread_do_callback(codec_thread
, NULL
);
724 /* Wait for codec thread to die */
725 while (codec_playing
)
728 rb
->snprintf(str
,sizeof(str
),"%d of %d",elapsed
,(int)track
.id3
.length
);
731 ticks
= endtick
- starttick
;
733 /* Be sure it is done */
734 rb
->codec_thread_do_callback(NULL
, NULL
);
740 rb
->snprintf(str
, sizeof(str
), "CRC32 - %08x", (unsigned)crc32
);
743 else if (wavinfo
.fd
< 0)
745 /* Display benchmark information */
746 rb
->snprintf(str
,sizeof(str
),"Decode time - %d.%02ds",(int)ticks
/100,(int)ticks
%100);
749 duration
= track
.id3
.length
/ 10;
750 rb
->snprintf(str
,sizeof(str
),"File duration - %d.%02ds",(int)duration
/100,(int)duration
%100);
754 speed
= duration
* 10000 / ticks
;
758 rb
->snprintf(str
,sizeof(str
),"%d.%02d%% realtime",(int)speed
/100,(int)speed
%100);
761 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
762 /* show effective clockrate in MHz needed for realtime decoding */
765 int freq
= CPUFREQ_MAX
;
767 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
769 freq
= CPUFREQ_NORMAL
;
772 speed
= freq
/ speed
;
773 rb
->snprintf(str
,sizeof(str
),"%d.%02dMHz needed for realtime",
774 (int)speed
/100,(int)speed
%100);
793 /* plugin entry point */
794 enum plugin_status
plugin_start(const void* parameter
)
796 int result
, selection
= 0;
797 enum plugin_status res
= PLUGIN_OK
;
799 struct dirent
*entry
;
802 char dirpath
[MAX_PATH
];
803 char filename
[MAX_PATH
];
806 if (parameter
== NULL
)
808 rb
->splash(HZ
*2, "No File");
812 wavbuffer
= rb
->plugin_get_buffer(&buffer_size
);
813 dspbuffer
= wavbuffer
+ buffer_size
/ 2;
815 codec_mallocbuf
= rb
->plugin_get_audio_buffer(&audiosize
);
816 /* Align codec_mallocbuf to pointer size, tlsf wants that */
817 codec_mallocbuf
= (void*)(((intptr_t)codec_mallocbuf
+
818 sizeof(intptr_t)-1) & ~(sizeof(intptr_t)-1));
819 audiobuf
= SKIPBYTES(codec_mallocbuf
, CODEC_SIZE
);
820 audiosize
-= CODEC_SIZE
;
822 rb
->lcd_clear_display();
829 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
834 SPEED_TEST_DIR_WITH_DSP
,
842 menu
, "test_codec", NULL
,
845 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
849 "Speed test with DSP",
850 "Speed test folder with DSP",
851 "Write WAV with DSP",
859 rb
->lcd_clear_display();
861 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
865 result
= rb
->do_menu(&menu
, &selection
, NULL
, false);
866 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
870 rb
->set_option("Boosting", &boost
, INT
,
871 boost_settings
, 2, NULL
);
886 if ((checksum
= (result
== CHECKSUM
|| result
== CHECKSUM_DIR
)))
887 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
893 if ((use_dsp
= ((result
>= SPEED_TEST_WITH_DSP
)
894 && (result
<= WRITE_WAV_WITH_DSP
)))) {
897 if (result
== SPEED_TEST
) {
900 } else if (result
== SPEED_TEST_DIR
) {
904 /* Only create a log file when we are testing a folder */
905 if (!log_init(true)) {
906 rb
->splash(HZ
*2, "Cannot create logfile");
910 } else if (result
== WRITE_WAV
) {
912 init_wav("/test.wav");
913 if (wavinfo
.fd
< 0) {
914 rb
->splash(HZ
*2, "Cannot create /test.wav");
918 } else if (result
== MENU_ATTACHED_USB
) {
919 res
= PLUGIN_USB_CONNECTED
;
921 } else if (result
< 0) {
927 /* Test all files in the same directory as the file selected by the
930 rb
->strlcpy(dirpath
,parameter
,sizeof(dirpath
));
931 ch
= rb
->strrchr(dirpath
,'/');
934 DEBUGF("Scanning directory \"%s\"\n",dirpath
);
935 dir
= rb
->opendir(dirpath
);
937 entry
= rb
->readdir(dir
);
939 struct dirinfo info
= rb
->dir_get_info(dir
, entry
);
940 if (!(info
.attribute
& ATTR_DIRECTORY
)) {
941 rb
->snprintf(filename
,sizeof(filename
),"%s%s",dirpath
,entry
->d_name
);
942 test_track(filename
);
946 /* Read next entry */
947 entry
= rb
->readdir(dir
);
953 /* Just test the file */
954 res
= test_track(parameter
);
956 /* Close WAV file (if there was one) */
957 if (wavinfo
.fd
>= 0) {
959 log_text("Wrote /test.wav",true);
961 while (rb
->button_get(true) != TESTCODEC_EXITBUTTON
);
964 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
966 rb
->cpu_boost(false);
969 rb
->button_clear_queue();