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 == COWOND2_PAD || CONFIG_KEYPAD == ONDAVX747_PAD
34 #define TESTCODEC_EXITBUTTON BUTTON_POWER
36 #define TESTCODEC_EXITBUTTON BUTTON_SELECT
39 /* Log functions copied from test_disk.c */
41 static int max_line
= 0;
42 static int log_fd
= -1;
43 static char logfilename
[MAX_PATH
];
45 static bool log_init(bool use_logfile
)
49 rb
->lcd_getstringsize("A", NULL
, &h
);
50 max_line
= LCD_HEIGHT
/ h
;
52 rb
->lcd_clear_display();
56 rb
->create_numbered_filename(logfilename
, "/", "test_codec_log_", ".txt",
57 2 IF_CNFN_NUM_(, NULL
));
58 log_fd
= rb
->open(logfilename
, O_RDWR
|O_CREAT
|O_TRUNC
);
65 static void log_text(char *text
, bool advance
)
67 rb
->lcd_puts(0, line
, text
);
71 if (++line
>= max_line
)
74 rb
->fdprintf(log_fd
, "%s\n", text
);
78 static void log_close(void)
94 static void* audiobuf
;
95 static void* codec_mallocbuf
;
96 static size_t audiosize
;
97 static char str
[MAX_PATH
];
99 /* Our local implementation of the codec API */
100 static struct codec_api ci
;
102 struct test_track_info
{
103 struct mp3entry id3
; /* TAG metadata */
104 size_t filesize
; /* File total length */
107 static struct test_track_info track
;
108 static bool taginfo_ready
= true;
112 static volatile unsigned int elapsed
;
113 static volatile bool codec_playing
;
114 static volatile long endtick
;
115 struct wavinfo_t wavinfo
;
117 static unsigned char wav_header
[44] =
119 'R','I','F','F', // 0 - ChunkID
120 0,0,0,0, // 4 - ChunkSize (filesize-8)
121 'W','A','V','E', // 8 - Format
122 'f','m','t',' ', // 12 - SubChunkID
123 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
124 1,0, // 20 - AudioFormat (1=16-bit)
125 0,0, // 22 - NumChannels
126 0,0,0,0, // 24 - SampleRate in Hz
127 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
128 0,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
129 16,0, // 34 - BitsPerSample
130 'd','a','t','a', // 36 - Subchunk2ID
131 0,0,0,0 // 40 - Subchunk2Size
134 static inline void int2le32(unsigned char* buf
, int32_t x
)
137 buf
[1] = (x
& 0xff00) >> 8;
138 buf
[2] = (x
& 0xff0000) >> 16;
139 buf
[3] = (x
& 0xff000000) >>24;
142 static inline void int2le24(unsigned char* buf
, int32_t x
)
145 buf
[1] = (x
& 0xff00) >> 8;
146 buf
[2] = (x
& 0xff0000) >> 16;
149 static inline void int2le16(unsigned char* buf
, int16_t x
)
152 buf
[1] = (x
& 0xff00) >> 8;
155 /* 32KB should be enough */
156 static unsigned char wavbuffer
[32*1024];
157 static unsigned char dspbuffer
[32*1024];
159 void init_wav(char* filename
)
161 wavinfo
.totalsamples
= 0;
163 wavinfo
.fd
= rb
->creat(filename
);
167 /* Write WAV header - we go back and fill in the details at the end */
168 rb
->write(wavinfo
.fd
, wav_header
, sizeof(wav_header
));
173 void close_wav(void) {
174 int filesize
= rb
->filesize(wavinfo
.fd
);
175 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
176 int bps
= 16; /* TODO */
178 /* We assume 16-bit, Stereo */
180 rb
->lseek(wavinfo
.fd
,0,SEEK_SET
);
182 int2le32(wav_header
+4, filesize
-8); /* ChunkSize */
184 int2le16(wav_header
+22, channels
);
186 int2le32(wav_header
+24, wavinfo
.samplerate
);
188 int2le32(wav_header
+28, wavinfo
.samplerate
* channels
* (bps
/ 8)); /* ByteRate */
190 int2le16(wav_header
+32, channels
* (bps
/ 8));
192 int2le32(wav_header
+40, filesize
- 44); /* Subchunk2Size */
194 rb
->write(wavinfo
.fd
, wav_header
, sizeof(wav_header
));
196 rb
->close(wavinfo
.fd
);
199 /* Returns buffer to malloc array. Only codeclib should need this. */
200 static void* codec_get_buffer(size_t *size
)
202 DEBUGF("codec_get_buffer(%d)\n",(int)size
);
204 return codec_mallocbuf
;
207 static int process_dsp(const void *ch1
, const void *ch2
, int count
)
209 const char *src
[2] = { ch1
, ch2
};
210 int written_count
= 0;
211 char *dest
= dspbuffer
;
215 int out_count
= rb
->dsp_output_count(ci
.dsp
, count
);
217 int inp_count
= rb
->dsp_input_count(ci
.dsp
, out_count
);
222 if (inp_count
> count
)
225 out_count
= rb
->dsp_process(ci
.dsp
, dest
, src
, inp_count
);
230 written_count
+= out_count
;
231 dest
+= out_count
* 4;
236 return written_count
;
240 static bool pcmbuf_insert_null(const void *ch1
, const void *ch2
, int count
)
242 if (use_dsp
) process_dsp(ch1
, ch2
, count
);
244 /* Prevent idle poweroff */
245 rb
->reset_poweroff_timer();
250 static inline int32_t clip_sample(int32_t sample
)
252 if ((int16_t)sample
!= sample
)
253 sample
= 0x7fff ^ (sample
>> 31);
260 static bool pcmbuf_insert_wav(const void *ch1
, const void *ch2
, int count
)
262 const int16_t* data1_16
;
263 const int16_t* data2_16
;
264 const int32_t* data1_32
;
265 const int32_t* data2_32
;
266 unsigned char* p
= wavbuffer
;
267 const int scale
= wavinfo
.sampledepth
- 15;
268 const int dc_bias
= 1 << (scale
- 1);
269 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
271 /* Prevent idle poweroff */
272 rb
->reset_poweroff_timer();
275 count
= process_dsp(ch1
, ch2
, count
);
276 wavinfo
.totalsamples
+= count
;
279 unsigned char *s
= dspbuffer
, *d
= dspbuffer
;
289 rb
->write(wavinfo
.fd
, dspbuffer
, count
* 2 * channels
);
292 if (wavinfo
.sampledepth
<= 16) {
296 switch(wavinfo
.stereomode
)
298 case STEREO_INTERLEAVED
:
300 int2le16(p
,*data1_16
++);
302 int2le16(p
,*data1_16
++);
307 case STEREO_NONINTERLEAVED
:
309 int2le16(p
,*data1_16
++);
311 int2le16(p
,*data2_16
++);
319 int2le16(p
,*data1_16
++);
328 switch(wavinfo
.stereomode
)
330 case STEREO_INTERLEAVED
:
332 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
334 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
339 case STEREO_NONINTERLEAVED
:
341 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
343 int2le16(p
, clip_sample((*data2_32
++ + dc_bias
) >> scale
));
351 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
358 wavinfo
.totalsamples
+= count
;
359 rb
->write(wavinfo
.fd
, wavbuffer
, p
- wavbuffer
);
365 /* Set song position in WPS (value in ms). */
366 static void set_elapsed(unsigned int value
)
372 /* Read next <size> amount bytes from file buffer to <ptr>.
373 Will return number of bytes read or 0 if end of file. */
374 static size_t read_filebuf(void *ptr
, size_t size
)
376 if (ci
.curpos
> (off_t
)track
.filesize
)
380 /* TODO: Don't read beyond end of buffer */
381 rb
->memcpy(ptr
, audiobuf
+ ci
.curpos
, size
);
388 /* Request pointer to file buffer which can be used to read
389 <realsize> amount of data. <reqsize> tells the buffer system
390 how much data it should try to allocate. If <realsize> is 0,
391 end of file is reached. */
392 static void* request_buffer(size_t *realsize
, size_t reqsize
)
394 *realsize
= MIN(track
.filesize
-ci
.curpos
,reqsize
);
396 return (audiobuf
+ ci
.curpos
);
400 /* Advance file buffer position by <amount> amount of bytes. */
401 static void advance_buffer(size_t amount
)
407 /* Advance file buffer to a pointer location inside file buffer. */
408 static void advance_buffer_loc(void *ptr
)
410 ci
.curpos
= ptr
- audiobuf
;
414 /* Seek file buffer to position <newpos> beginning of file. */
415 static bool seek_buffer(size_t newpos
)
422 /* Codec should call this function when it has done the seeking. */
423 static void seek_complete(void)
428 /* Request file change from file buffer. Returns true is next
429 track is available and changed. If return value is false,
430 codec should exit immediately with PLUGIN_OK status. */
431 static bool request_next_track(void)
433 /* We are only decoding a single track */
438 /* Free the buffer area of the current codec after its loaded */
439 static void discard_codec(void)
445 static void set_offset(size_t value
)
452 /* Configure different codec buffer parameters. */
453 static void configure(int setting
, intptr_t value
)
456 rb
->dsp_configure(ci
.dsp
, setting
, value
);
459 case DSP_SWITCH_FREQUENCY
:
460 case DSP_SET_FREQUENCY
:
461 DEBUGF("samplerate=%d\n",(int)value
);
462 wavinfo
.samplerate
= (int)value
;
465 case DSP_SET_SAMPLE_DEPTH
:
466 DEBUGF("sampledepth = %d\n",(int)value
);
467 wavinfo
.sampledepth
=(int)value
;
470 case DSP_SET_STEREO_MODE
:
471 DEBUGF("Stereo mode = %d\n",(int)value
);
472 wavinfo
.stereomode
=(int)value
;
478 static void init_ci(void)
480 /* --- Our "fake" implementations of the codec API functions. --- */
482 ci
.codec_get_buffer
= codec_get_buffer
;
484 if (wavinfo
.fd
>= 0) {
485 ci
.pcmbuf_insert
= pcmbuf_insert_wav
;
487 ci
.pcmbuf_insert
= pcmbuf_insert_null
;
489 ci
.set_elapsed
= set_elapsed
;
490 ci
.read_filebuf
= read_filebuf
;
491 ci
.request_buffer
= request_buffer
;
492 ci
.advance_buffer
= advance_buffer
;
493 ci
.advance_buffer_loc
= advance_buffer_loc
;
494 ci
.seek_buffer
= seek_buffer
;
495 ci
.seek_complete
= seek_complete
;
496 ci
.request_next_track
= request_next_track
;
497 ci
.discard_codec
= discard_codec
;
498 ci
.set_offset
= set_offset
;
499 ci
.configure
= configure
;
500 ci
.dsp
= (struct dsp_config
*)rb
->dsp_configure(NULL
, DSP_MYDSP
,
503 /* --- "Core" functions --- */
506 ci
.sleep
= rb
->sleep
;
507 ci
.yield
= rb
->yield
;
509 /* strings and memory */
510 ci
.strcpy
= rb
->strcpy
;
511 ci
.strlen
= rb
->strlen
;
512 ci
.strcmp
= rb
->strcmp
;
513 ci
.strcat
= rb
->strcat
;
514 ci
.memset
= rb
->memset
;
515 ci
.memcpy
= rb
->memcpy
;
516 ci
.memmove
= rb
->memmove
;
517 ci
.memcmp
= rb
->memcmp
;
518 ci
.memchr
= rb
->memchr
;
519 ci
.strcasestr
= rb
->strcasestr
;
520 #if defined(DEBUG) || defined(SIMULATOR)
521 ci
.debugf
= rb
->debugf
;
523 #ifdef ROCKBOX_HAS_LOGF
527 ci
.qsort
= rb
->qsort
;
528 ci
.global_settings
= rb
->global_settings
;
531 ci
.profile_thread
= rb
->profile_thread
;
532 ci
.profstop
= rb
->profstop
;
533 ci
.profile_func_enter
= rb
->profile_func_enter
;
534 ci
.profile_func_exit
= rb
->profile_func_exit
;
538 ci
.cpucache_invalidate
= rb
->cpucache_invalidate
;
539 ci
.cpucache_flush
= rb
->cpucache_flush
;
543 ci
.create_thread
= rb
->create_thread
;
544 ci
.thread_thaw
= rb
->thread_thaw
;
545 ci
.thread_wait
= rb
->thread_wait
;
546 ci
.semaphore_init
= rb
->semaphore_init
;
547 ci
.semaphore_wait
= rb
->semaphore_wait
;
548 ci
.semaphore_release
= rb
->semaphore_release
;
552 ci
.__div0
= rb
->__div0
;
556 static void codec_thread(void)
558 const char* codecname
;
561 codecname
= rb
->get_codec_filename(track
.id3
.codectype
);
563 /* Load the codec and start decoding. */
564 res
= rb
->codec_load_file(codecname
,&ci
);
566 /* Signal to the main thread that we are done */
567 endtick
= *rb
->current_tick
;
568 codec_playing
= false;
571 static enum plugin_status
test_track(const char* filename
)
575 enum plugin_status res
= PLUGIN_ERROR
;
579 unsigned long duration
;
582 /* Display filename (excluding any path)*/
583 ch
= rb
->strrchr(filename
, '/');
589 rb
->snprintf(str
,sizeof(str
),"%s",ch
);
592 log_text("Loading...",false);
594 fd
= rb
->open(filename
,O_RDONLY
);
597 log_text("Cannot open file",true);
601 track
.filesize
= rb
->filesize(fd
);
603 /* Clear the id3 struct */
604 rb
->memset(&track
.id3
, 0, sizeof(struct mp3entry
));
606 if (!rb
->get_metadata(&(track
.id3
), fd
, filename
))
608 log_text("Cannot read metadata",true);
612 if (track
.filesize
> audiosize
)
614 log_text("File too large",true);
618 n
= rb
->read(fd
, audiobuf
, track
.filesize
);
620 if (n
!= track
.filesize
)
622 log_text("Read failed.",true);
626 /* Initialise the function pointers in the codec API */
629 /* Prepare the codec struct for playing the whole file */
630 ci
.filesize
= track
.filesize
;
632 ci
.taginfo_ready
= &taginfo_ready
;
634 ci
.stop_codec
= false;
639 rb
->dsp_configure(ci
.dsp
, DSP_RESET
, 0);
641 starttick
= *rb
->current_tick
;
643 codec_playing
= true;
645 rb
->codec_thread_do_callback(codec_thread
, NULL
);
647 /* Wait for codec thread to die */
648 while (codec_playing
)
651 rb
->snprintf(str
,sizeof(str
),"%d of %d",elapsed
,(int)track
.id3
.length
);
654 ticks
= endtick
- starttick
;
656 /* Be sure it is done */
657 rb
->codec_thread_do_callback(NULL
, NULL
);
663 /* Display benchmark information */
664 rb
->snprintf(str
,sizeof(str
),"Decode time - %d.%02ds",(int)ticks
/100,(int)ticks
%100);
667 duration
= track
.id3
.length
/ 10;
668 rb
->snprintf(str
,sizeof(str
),"File duration - %d.%02ds",(int)duration
/100,(int)duration
%100);
672 speed
= duration
* 10000 / ticks
;
676 rb
->snprintf(str
,sizeof(str
),"%d.%02d%% realtime",(int)speed
/100,(int)speed
%100);
680 /* show effective clockrate in MHz needed for realtime decoding */
683 speed
= CPUFREQ_MAX
/ speed
;
684 rb
->snprintf(str
,sizeof(str
),"%d.%02dMHz needed for realtime",
685 (int)speed
/100,(int)speed
%100);
704 /* plugin entry point */
705 enum plugin_status
plugin_start(const void* parameter
)
707 int result
, selection
= 0;
708 enum plugin_status res
= PLUGIN_OK
;
710 struct dirent
*entry
;
713 char dirpath
[MAX_PATH
];
714 char filename
[MAX_PATH
];
716 if (parameter
== NULL
)
718 rb
->splash(HZ
*2, "No File");
722 codec_mallocbuf
= rb
->plugin_get_audio_buffer(&audiosize
);
723 audiobuf
= SKIPBYTES(codec_mallocbuf
, CODEC_SIZE
);
724 audiosize
-= CODEC_SIZE
;
726 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
729 rb
->lcd_clear_display();
733 menu
, "test_codec", NULL
,
738 "Speed test folder w/DSP",
744 rb
->lcd_clear_display();
746 result
=rb
->do_menu(&menu
,&selection
, NULL
, false);
756 if ((use_dsp
= ((result
>= 3) && (result
<=5)))) {
762 } else if (result
==1) {
766 /* Only create a log file when we are testing a folder */
767 if (!log_init(true)) {
768 rb
->splash(HZ
*2, "Cannot create logfile");
772 } else if (result
==2) {
774 init_wav("/test.wav");
775 if (wavinfo
.fd
< 0) {
776 rb
->splash(HZ
*2, "Cannot create /test.wav");
780 } else if (result
== MENU_ATTACHED_USB
) {
781 res
= PLUGIN_USB_CONNECTED
;
783 } else if (result
< 0) {
789 /* Test all files in the same directory as the file selected by the
792 rb
->strlcpy(dirpath
,parameter
,sizeof(dirpath
));
793 ch
= rb
->strrchr(dirpath
,'/');
796 DEBUGF("Scanning directory \"%s\"\n",dirpath
);
797 dir
= rb
->opendir(dirpath
);
799 entry
= rb
->readdir(dir
);
801 if (!(entry
->attribute
& ATTR_DIRECTORY
)) {
802 rb
->snprintf(filename
,sizeof(filename
),"%s%s",dirpath
,entry
->d_name
);
803 test_track(filename
);
807 /* Read next entry */
808 entry
= rb
->readdir(dir
);
814 /* Just test the file */
815 res
= test_track(parameter
);
817 /* Close WAV file (if there was one) */
818 if (wavinfo
.fd
>= 0) {
820 log_text("Wrote /test.wav",true);
823 while (rb
->button_get(true) != TESTCODEC_EXITBUTTON
);
830 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
831 rb
->cpu_boost(false);