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 defined(HAVE_TOUCHSCREEN)
36 #define TESTCODEC_EXITBUTTON BUTTON_TOPLEFT
38 #define TESTCODEC_EXITBUTTON BUTTON_SELECT
41 /* Log functions copied from test_disk.c */
43 static int max_line
= 0;
44 static int log_fd
= -1;
45 static char logfilename
[MAX_PATH
];
47 static bool log_init(bool use_logfile
)
51 rb
->lcd_getstringsize("A", NULL
, &h
);
52 max_line
= LCD_HEIGHT
/ h
;
54 rb
->lcd_clear_display();
58 rb
->create_numbered_filename(logfilename
, "/", "test_codec_log_", ".txt",
59 2 IF_CNFN_NUM_(, NULL
));
60 log_fd
= rb
->open(logfilename
, O_RDWR
|O_CREAT
|O_TRUNC
);
67 static void log_text(char *text
, bool advance
)
69 rb
->lcd_puts(0, line
, text
);
73 if (++line
>= max_line
)
76 rb
->fdprintf(log_fd
, "%s\n", text
);
80 static void log_close(void)
96 static void* audiobuf
;
97 static void* codec_mallocbuf
;
98 static size_t audiosize
;
99 static char str
[MAX_PATH
];
101 /* Our local implementation of the codec API */
102 static struct codec_api ci
;
104 struct test_track_info
{
105 struct mp3entry id3
; /* TAG metadata */
106 size_t filesize
; /* File total length */
109 static struct test_track_info track
;
110 static bool taginfo_ready
= true;
114 static bool checksum
;
115 static uint32_t crc32
;
117 static volatile unsigned int elapsed
;
118 static volatile bool codec_playing
;
119 static volatile long endtick
;
120 struct wavinfo_t wavinfo
;
122 static unsigned char wav_header
[44] =
124 'R','I','F','F', // 0 - ChunkID
125 0,0,0,0, // 4 - ChunkSize (filesize-8)
126 'W','A','V','E', // 8 - Format
127 'f','m','t',' ', // 12 - SubChunkID
128 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
129 1,0, // 20 - AudioFormat (1=16-bit)
130 0,0, // 22 - NumChannels
131 0,0,0,0, // 24 - SampleRate in Hz
132 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
133 0,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
134 16,0, // 34 - BitsPerSample
135 'd','a','t','a', // 36 - Subchunk2ID
136 0,0,0,0 // 40 - Subchunk2Size
139 static inline void int2le32(unsigned char* buf
, int32_t x
)
142 buf
[1] = (x
& 0xff00) >> 8;
143 buf
[2] = (x
& 0xff0000) >> 16;
144 buf
[3] = (x
& 0xff000000) >>24;
147 static inline void int2le24(unsigned char* buf
, int32_t x
)
150 buf
[1] = (x
& 0xff00) >> 8;
151 buf
[2] = (x
& 0xff0000) >> 16;
154 static inline void int2le16(unsigned char* buf
, int16_t x
)
157 buf
[1] = (x
& 0xff00) >> 8;
160 /* 32KB should be enough */
161 static unsigned char wavbuffer
[32*1024];
162 static unsigned char dspbuffer
[32*1024];
164 void init_wav(char* filename
)
166 wavinfo
.totalsamples
= 0;
168 wavinfo
.fd
= rb
->creat(filename
);
172 /* Write WAV header - we go back and fill in the details at the end */
173 rb
->write(wavinfo
.fd
, wav_header
, sizeof(wav_header
));
180 int filesize
= rb
->filesize(wavinfo
.fd
);
181 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
182 int bps
= 16; /* TODO */
184 /* We assume 16-bit, Stereo */
186 rb
->lseek(wavinfo
.fd
,0,SEEK_SET
);
188 int2le32(wav_header
+4, filesize
-8); /* ChunkSize */
190 int2le16(wav_header
+22, channels
);
192 int2le32(wav_header
+24, wavinfo
.samplerate
);
194 int2le32(wav_header
+28, wavinfo
.samplerate
* channels
* (bps
/ 8)); /* ByteRate */
196 int2le16(wav_header
+32, channels
* (bps
/ 8));
198 int2le32(wav_header
+40, filesize
- 44); /* Subchunk2Size */
200 rb
->write(wavinfo
.fd
, wav_header
, sizeof(wav_header
));
202 rb
->close(wavinfo
.fd
);
205 /* Returns buffer to malloc array. Only codeclib should need this. */
206 static void* codec_get_buffer(size_t *size
)
208 DEBUGF("codec_get_buffer(%d)\n",(int)size
);
210 return codec_mallocbuf
;
213 static int process_dsp(const void *ch1
, const void *ch2
, int count
)
215 const char *src
[2] = { ch1
, ch2
};
216 int written_count
= 0;
217 char *dest
= dspbuffer
;
221 int out_count
= rb
->dsp_output_count(ci
.dsp
, count
);
223 int inp_count
= rb
->dsp_input_count(ci
.dsp
, out_count
);
228 if (inp_count
> count
)
231 out_count
= rb
->dsp_process(ci
.dsp
, dest
, src
, inp_count
);
236 written_count
+= out_count
;
237 dest
+= out_count
* 4;
242 return written_count
;
245 static inline int32_t clip_sample(int32_t sample
)
247 if ((int16_t)sample
!= sample
)
248 sample
= 0x7fff ^ (sample
>> 31);
254 static void pcmbuf_insert_null(const void *ch1
, const void *ch2
, int count
)
257 process_dsp(ch1
, ch2
, count
);
259 /* Prevent idle poweroff */
260 rb
->reset_poweroff_timer();
263 static void pcmbuf_insert_checksum(const void *ch1
, const void *ch2
, int count
)
265 const int16_t* data1_16
;
266 const int16_t* data2_16
;
267 const int32_t* data1_32
;
268 const int32_t* data2_32
;
269 const int scale
= wavinfo
.sampledepth
- 15;
270 const int dc_bias
= 1 << (scale
- 1);
271 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
273 /* Prevent idle poweroff */
274 rb
->reset_poweroff_timer();
277 count
= process_dsp(ch1
, ch2
, count
);
278 wavinfo
.totalsamples
+= count
;
281 unsigned char *s
= dspbuffer
, *d
= dspbuffer
;
291 crc32
= rb
->crc_32(dspbuffer
, count
* 2 * channels
, crc32
);
295 if (wavinfo
.sampledepth
<= 16) {
299 switch(wavinfo
.stereomode
)
301 case STEREO_INTERLEAVED
:
303 crc32
= rb
->crc_32(data1_16
, 4, crc32
);
308 case STEREO_NONINTERLEAVED
:
310 crc32
= rb
->crc_32(data1_16
++, 2, crc32
);
311 crc32
= rb
->crc_32(data2_16
++, 2, crc32
);
317 crc32
= rb
->crc_32(data1_16
++, 2, crc32
);
327 switch(wavinfo
.stereomode
)
329 case STEREO_INTERLEAVED
:
331 int16_t s
= clip_sample((*data1_32
++ + dc_bias
) >> scale
);
332 crc32
= rb
->crc_32(&s
, 2, crc32
);
333 s
= clip_sample((*data1_32
++ + dc_bias
) >> scale
);
334 crc32
= rb
->crc_32(&s
, 2, crc32
);
338 case STEREO_NONINTERLEAVED
:
340 int16_t s
= clip_sample((*data1_32
++ + dc_bias
) >> scale
);
341 crc32
= rb
->crc_32(&s
, 2, crc32
);
342 s
= clip_sample((*data2_32
++ + dc_bias
) >> scale
);
343 crc32
= rb
->crc_32(&s
, 2, crc32
);
350 int16_t s
= clip_sample((*data1_32
++ + dc_bias
) >> scale
);
351 crc32
= rb
->crc_32(&s
, 2, crc32
);
360 static void pcmbuf_insert_wav(const void *ch1
, const void *ch2
, int count
)
362 const int16_t* data1_16
;
363 const int16_t* data2_16
;
364 const int32_t* data1_32
;
365 const int32_t* data2_32
;
366 unsigned char* p
= wavbuffer
;
367 const int scale
= wavinfo
.sampledepth
- 15;
368 const int dc_bias
= 1 << (scale
- 1);
369 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
371 /* Prevent idle poweroff */
372 rb
->reset_poweroff_timer();
375 count
= process_dsp(ch1
, ch2
, count
);
376 wavinfo
.totalsamples
+= count
;
379 unsigned char *s
= dspbuffer
, *d
= dspbuffer
;
389 rb
->write(wavinfo
.fd
, dspbuffer
, count
* 2 * channels
);
393 if (wavinfo
.sampledepth
<= 16) {
397 switch(wavinfo
.stereomode
)
399 case STEREO_INTERLEAVED
:
401 int2le16(p
,*data1_16
++);
403 int2le16(p
,*data1_16
++);
408 case STEREO_NONINTERLEAVED
:
410 int2le16(p
,*data1_16
++);
412 int2le16(p
,*data2_16
++);
420 int2le16(p
,*data1_16
++);
429 switch(wavinfo
.stereomode
)
431 case STEREO_INTERLEAVED
:
433 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
435 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
440 case STEREO_NONINTERLEAVED
:
442 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
444 int2le16(p
, clip_sample((*data2_32
++ + dc_bias
) >> scale
));
452 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
459 wavinfo
.totalsamples
+= count
;
460 rb
->write(wavinfo
.fd
, wavbuffer
, p
- wavbuffer
);
464 /* Set song position in WPS (value in ms). */
465 static void set_elapsed(unsigned long value
)
471 /* Read next <size> amount bytes from file buffer to <ptr>.
472 Will return number of bytes read or 0 if end of file. */
473 static size_t read_filebuf(void *ptr
, size_t size
)
475 if (ci
.curpos
> (off_t
)track
.filesize
)
479 /* TODO: Don't read beyond end of buffer */
480 rb
->memcpy(ptr
, audiobuf
+ ci
.curpos
, size
);
487 /* Request pointer to file buffer which can be used to read
488 <realsize> amount of data. <reqsize> tells the buffer system
489 how much data it should try to allocate. If <realsize> is 0,
490 end of file is reached. */
491 static void* request_buffer(size_t *realsize
, size_t reqsize
)
493 *realsize
= MIN(track
.filesize
-ci
.curpos
,reqsize
);
495 return (audiobuf
+ ci
.curpos
);
499 /* Advance file buffer position by <amount> amount of bytes. */
500 static void advance_buffer(size_t amount
)
506 /* Advance file buffer to a pointer location inside file buffer. */
507 static void advance_buffer_loc(void *ptr
)
509 ci
.curpos
= ptr
- audiobuf
;
513 /* Seek file buffer to position <newpos> beginning of file. */
514 static bool seek_buffer(size_t newpos
)
521 /* Codec should call this function when it has done the seeking. */
522 static void seek_complete(void)
527 /* Request file change from file buffer. Returns true is next
528 track is available and changed. If return value is false,
529 codec should exit immediately with PLUGIN_OK status. */
530 static bool request_next_track(void)
532 /* We are only decoding a single track */
537 /* Free the buffer area of the current codec after its loaded */
538 static void discard_codec(void)
544 static void set_offset(size_t value
)
551 /* Configure different codec buffer parameters. */
552 static void configure(int setting
, intptr_t value
)
555 rb
->dsp_configure(ci
.dsp
, setting
, value
);
558 case DSP_SWITCH_FREQUENCY
:
559 case DSP_SET_FREQUENCY
:
560 DEBUGF("samplerate=%d\n",(int)value
);
561 wavinfo
.samplerate
= (int)value
;
564 case DSP_SET_SAMPLE_DEPTH
:
565 DEBUGF("sampledepth = %d\n",(int)value
);
566 wavinfo
.sampledepth
=(int)value
;
569 case DSP_SET_STEREO_MODE
:
570 DEBUGF("Stereo mode = %d\n",(int)value
);
571 wavinfo
.stereomode
=(int)value
;
577 static void init_ci(void)
579 /* --- Our "fake" implementations of the codec API functions. --- */
581 ci
.codec_get_buffer
= codec_get_buffer
;
583 if (wavinfo
.fd
>= 0) {
584 ci
.pcmbuf_insert
= pcmbuf_insert_wav
;
585 } else if (checksum
){
586 ci
.pcmbuf_insert
= pcmbuf_insert_checksum
;
588 ci
.pcmbuf_insert
= pcmbuf_insert_null
;
591 ci
.set_elapsed
= set_elapsed
;
592 ci
.read_filebuf
= read_filebuf
;
593 ci
.request_buffer
= request_buffer
;
594 ci
.advance_buffer
= advance_buffer
;
595 ci
.advance_buffer_loc
= advance_buffer_loc
;
596 ci
.seek_buffer
= seek_buffer
;
597 ci
.seek_complete
= seek_complete
;
598 ci
.request_next_track
= request_next_track
;
599 ci
.discard_codec
= discard_codec
;
600 ci
.set_offset
= set_offset
;
601 ci
.configure
= configure
;
602 ci
.dsp
= (struct dsp_config
*)rb
->dsp_configure(NULL
, DSP_MYDSP
,
605 /* --- "Core" functions --- */
608 ci
.sleep
= rb
->sleep
;
609 ci
.yield
= rb
->yield
;
611 /* strings and memory */
612 ci
.strcpy
= rb
->strcpy
;
613 ci
.strlen
= rb
->strlen
;
614 ci
.strcmp
= rb
->strcmp
;
615 ci
.strcat
= rb
->strcat
;
616 ci
.memset
= rb
->memset
;
617 ci
.memcpy
= rb
->memcpy
;
618 ci
.memmove
= rb
->memmove
;
619 ci
.memcmp
= rb
->memcmp
;
620 ci
.memchr
= rb
->memchr
;
621 ci
.strcasestr
= rb
->strcasestr
;
622 #if defined(DEBUG) || defined(SIMULATOR)
623 ci
.debugf
= rb
->debugf
;
625 #ifdef ROCKBOX_HAS_LOGF
629 ci
.qsort
= rb
->qsort
;
630 ci
.global_settings
= rb
->global_settings
;
633 ci
.profile_thread
= rb
->profile_thread
;
634 ci
.profstop
= rb
->profstop
;
635 ci
.profile_func_enter
= rb
->profile_func_enter
;
636 ci
.profile_func_exit
= rb
->profile_func_exit
;
640 ci
.cpucache_invalidate
= rb
->cpucache_invalidate
;
641 ci
.cpucache_flush
= rb
->cpucache_flush
;
645 ci
.create_thread
= rb
->create_thread
;
646 ci
.thread_thaw
= rb
->thread_thaw
;
647 ci
.thread_wait
= rb
->thread_wait
;
648 ci
.semaphore_init
= rb
->semaphore_init
;
649 ci
.semaphore_wait
= rb
->semaphore_wait
;
650 ci
.semaphore_release
= rb
->semaphore_release
;
654 ci
.__div0
= rb
->__div0
;
658 static void codec_thread(void)
660 const char* codecname
;
663 codecname
= rb
->get_codec_filename(track
.id3
.codectype
);
665 /* Load the codec and start decoding. */
666 res
= rb
->codec_load_file(codecname
,&ci
);
668 /* Signal to the main thread that we are done */
669 endtick
= *rb
->current_tick
;
670 codec_playing
= false;
673 static enum plugin_status
test_track(const char* filename
)
677 enum plugin_status res
= PLUGIN_ERROR
;
681 unsigned long duration
;
684 /* Display filename (excluding any path)*/
685 ch
= rb
->strrchr(filename
, '/');
691 rb
->snprintf(str
,sizeof(str
),"%s",ch
);
694 log_text("Loading...",false);
696 fd
= rb
->open(filename
,O_RDONLY
);
699 log_text("Cannot open file",true);
703 track
.filesize
= rb
->filesize(fd
);
705 /* Clear the id3 struct */
706 rb
->memset(&track
.id3
, 0, sizeof(struct mp3entry
));
708 if (!rb
->get_metadata(&(track
.id3
), fd
, filename
))
710 log_text("Cannot read metadata",true);
714 if (track
.filesize
> audiosize
)
716 log_text("File too large",true);
720 n
= rb
->read(fd
, audiobuf
, track
.filesize
);
722 if (n
!= track
.filesize
)
724 log_text("Read failed.",true);
728 /* Initialise the function pointers in the codec API */
731 /* Prepare the codec struct for playing the whole file */
732 ci
.filesize
= track
.filesize
;
734 ci
.taginfo_ready
= &taginfo_ready
;
736 ci
.stop_codec
= false;
741 rb
->dsp_configure(ci
.dsp
, DSP_RESET
, 0);
746 starttick
= *rb
->current_tick
;
748 codec_playing
= true;
750 rb
->codec_thread_do_callback(codec_thread
, NULL
);
752 /* Wait for codec thread to die */
753 while (codec_playing
)
756 rb
->snprintf(str
,sizeof(str
),"%d of %d",elapsed
,(int)track
.id3
.length
);
759 ticks
= endtick
- starttick
;
761 /* Be sure it is done */
762 rb
->codec_thread_do_callback(NULL
, NULL
);
768 rb
->snprintf(str
, sizeof(str
), "CRC32 - %x", (unsigned)crc32
);
771 else if (wavinfo
.fd
< 0)
773 /* Display benchmark information */
774 rb
->snprintf(str
,sizeof(str
),"Decode time - %d.%02ds",(int)ticks
/100,(int)ticks
%100);
777 duration
= track
.id3
.length
/ 10;
778 rb
->snprintf(str
,sizeof(str
),"File duration - %d.%02ds",(int)duration
/100,(int)duration
%100);
782 speed
= duration
* 10000 / ticks
;
786 rb
->snprintf(str
,sizeof(str
),"%d.%02d%% realtime",(int)speed
/100,(int)speed
%100);
790 /* show effective clockrate in MHz needed for realtime decoding */
793 speed
= CPUFREQ_MAX
/ speed
;
794 rb
->snprintf(str
,sizeof(str
),"%d.%02dMHz needed for realtime",
795 (int)speed
/100,(int)speed
%100);
814 /* plugin entry point */
815 enum plugin_status
plugin_start(const void* parameter
)
817 int result
, selection
= 0;
818 enum plugin_status res
= PLUGIN_OK
;
820 struct dirent
*entry
;
823 char dirpath
[MAX_PATH
];
824 char filename
[MAX_PATH
];
826 if (parameter
== NULL
)
828 rb
->splash(HZ
*2, "No File");
832 codec_mallocbuf
= rb
->plugin_get_audio_buffer(&audiosize
);
833 audiobuf
= SKIPBYTES(codec_mallocbuf
, CODEC_SIZE
);
834 audiosize
-= CODEC_SIZE
;
836 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
839 rb
->lcd_clear_display();
848 SPEED_TEST_DIR_WITH_DSP
,
856 menu
, "test_codec", NULL
,
860 "Speed test with DSP",
861 "Speed test folder with DSP",
862 "Write WAV with DSP",
869 rb
->lcd_clear_display();
871 result
= rb
->do_menu(&menu
, &selection
, NULL
, false);
881 if ((checksum
= (result
== CHECKSUM
|| result
== CHECKSUM_DIR
)))
884 if ((use_dsp
= ((result
>= SPEED_TEST_WITH_DSP
)
885 && (result
<= WRITE_WAV_WITH_DSP
)))) {
888 if (result
== SPEED_TEST
) {
891 } else if (result
== SPEED_TEST_DIR
) {
895 /* Only create a log file when we are testing a folder */
896 if (!log_init(true)) {
897 rb
->splash(HZ
*2, "Cannot create logfile");
901 } else if (result
== WRITE_WAV
) {
903 init_wav("/test.wav");
904 if (wavinfo
.fd
< 0) {
905 rb
->splash(HZ
*2, "Cannot create /test.wav");
909 } else if (result
== MENU_ATTACHED_USB
) {
910 res
= PLUGIN_USB_CONNECTED
;
912 } else if (result
< 0) {
918 /* Test all files in the same directory as the file selected by the
921 rb
->strlcpy(dirpath
,parameter
,sizeof(dirpath
));
922 ch
= rb
->strrchr(dirpath
,'/');
925 DEBUGF("Scanning directory \"%s\"\n",dirpath
);
926 dir
= rb
->opendir(dirpath
);
928 entry
= rb
->readdir(dir
);
930 if (!(entry
->attribute
& ATTR_DIRECTORY
)) {
931 rb
->snprintf(filename
,sizeof(filename
),"%s%s",dirpath
,entry
->d_name
);
932 test_track(filename
);
936 /* Read next entry */
937 entry
= rb
->readdir(dir
);
943 /* Just test the file */
944 res
= test_track(parameter
);
946 /* Close WAV file (if there was one) */
947 if (wavinfo
.fd
>= 0) {
949 log_text("Wrote /test.wav",true);
951 while (rb
->button_get(true) != TESTCODEC_EXITBUTTON
);
958 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
959 rb
->cpu_boost(false);