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;
48 static void log_close(void)
54 static bool log_init(bool use_logfile
)
57 char logfilename
[MAX_PATH
];
59 rb
->lcd_getstringsize("A", NULL
, &h
);
60 max_line
= LCD_HEIGHT
/ h
;
62 rb
->lcd_clear_display();
67 rb
->create_numbered_filename(logfilename
, "/", "test_codec_log_", ".txt",
68 2 IF_CNFN_NUM_(, NULL
));
69 log_fd
= rb
->open(logfilename
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
76 static void log_text(char *text
, bool advance
)
78 rb
->lcd_puts(0, line
, text
);
82 if (++line
>= max_line
)
85 rb
->fdprintf(log_fd
, "%s\n", text
);
99 static void* audiobuf
;
100 static void* codec_mallocbuf
;
101 static size_t audiosize
;
102 static size_t audiobufsize
;
106 /* Our local implementation of the codec API */
107 static struct codec_api ci
;
109 struct test_track_info
{
110 struct mp3entry id3
; /* TAG metadata */
111 size_t filesize
; /* File total length */
114 static struct test_track_info track
;
115 static bool taginfo_ready
= true;
119 static bool checksum
;
120 static uint32_t crc32
;
122 static volatile unsigned int elapsed
;
123 static volatile bool codec_playing
;
124 static volatile long endtick
;
125 static volatile long rebuffertick
;
126 struct wavinfo_t wavinfo
;
128 static unsigned char wav_header
[44] =
130 'R','I','F','F', // 0 - ChunkID
131 0,0,0,0, // 4 - ChunkSize (filesize-8)
132 'W','A','V','E', // 8 - Format
133 'f','m','t',' ', // 12 - SubChunkID
134 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
135 1,0, // 20 - AudioFormat (1=16-bit)
136 0,0, // 22 - NumChannels
137 0,0,0,0, // 24 - SampleRate in Hz
138 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
139 0,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
140 16,0, // 34 - BitsPerSample
141 'd','a','t','a', // 36 - Subchunk2ID
142 0,0,0,0 // 40 - Subchunk2Size
145 static inline void int2le32(unsigned char* buf
, int32_t x
)
148 buf
[1] = (x
& 0xff00) >> 8;
149 buf
[2] = (x
& 0xff0000) >> 16;
150 buf
[3] = (x
& 0xff000000) >>24;
153 static inline void int2le24(unsigned char* buf
, int32_t x
)
156 buf
[1] = (x
& 0xff00) >> 8;
157 buf
[2] = (x
& 0xff0000) >> 16;
160 static inline void int2le16(unsigned char* buf
, int16_t x
)
163 buf
[1] = (x
& 0xff00) >> 8;
166 static unsigned char *wavbuffer
;
167 static unsigned char *dspbuffer
;
169 void init_wav(char* filename
)
171 wavinfo
.totalsamples
= 0;
173 wavinfo
.fd
= rb
->creat(filename
, 0666);
177 /* Write WAV header - we go back and fill in the details at the end */
178 rb
->write(wavinfo
.fd
, wav_header
, sizeof(wav_header
));
185 int filesize
= rb
->filesize(wavinfo
.fd
);
186 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
187 int bps
= 16; /* TODO */
189 /* We assume 16-bit, Stereo */
191 rb
->lseek(wavinfo
.fd
,0,SEEK_SET
);
193 int2le32(wav_header
+4, filesize
-8); /* ChunkSize */
195 int2le16(wav_header
+22, channels
);
197 int2le32(wav_header
+24, wavinfo
.samplerate
);
199 int2le32(wav_header
+28, wavinfo
.samplerate
* channels
* (bps
/ 8)); /* ByteRate */
201 int2le16(wav_header
+32, channels
* (bps
/ 8));
203 int2le32(wav_header
+40, filesize
- 44); /* Subchunk2Size */
205 rb
->write(wavinfo
.fd
, wav_header
, sizeof(wav_header
));
207 rb
->close(wavinfo
.fd
);
210 /* Returns buffer to malloc array. Only codeclib should need this. */
211 static void* codec_get_buffer(size_t *size
)
213 DEBUGF("codec_get_buffer(%"PRIuPTR
")\n",(uintptr_t)size
);
215 return codec_mallocbuf
;
218 static int process_dsp(const void *ch1
, const void *ch2
, int count
)
220 const char *src
[2] = { ch1
, ch2
};
221 int written_count
= 0;
222 char *dest
= dspbuffer
;
226 int out_count
= rb
->dsp_output_count(ci
.dsp
, count
);
228 int inp_count
= rb
->dsp_input_count(ci
.dsp
, out_count
);
233 if (inp_count
> count
)
236 out_count
= rb
->dsp_process(ci
.dsp
, dest
, src
, inp_count
);
241 written_count
+= out_count
;
242 dest
+= out_count
* 4;
247 return written_count
;
250 static inline int32_t clip_sample(int32_t sample
)
252 if ((int16_t)sample
!= sample
)
253 sample
= 0x7fff ^ (sample
>> 31);
259 static void pcmbuf_insert_null(const void *ch1
, const void *ch2
, int count
)
262 process_dsp(ch1
, ch2
, count
);
264 /* Prevent idle poweroff */
265 rb
->reset_poweroff_timer();
269 * Helper function used when the file is larger then the available memory.
270 * Rebuffers the file by setting the start of the audio buffer to be
271 * new_offset and filling from there.
273 static int fill_buffer(int new_offset
){
274 size_t n
, bytestoread
;
275 long temp
= *rb
->current_tick
;
276 rb
->lseek(fd
,new_offset
,SEEK_SET
);
278 if(new_offset
+ audiobufsize
<= track
.filesize
)
279 bytestoread
= audiobufsize
;
281 bytestoread
= track
.filesize
-new_offset
;
283 n
= rb
->read(fd
, audiobuf
,bytestoread
);
285 if (n
!= bytestoread
)
287 log_text("Read failed.",true);
288 DEBUGF("read fail: got %d bytes, expected %d\n", (int)n
, (int)audiobufsize
);
300 /*keep track of how much time we spent buffering*/
301 rebuffertick
+= *rb
->current_tick
-temp
;
306 /* WAV output or calculate crc32 of output*/
307 static void pcmbuf_insert_wav_checksum(const void *ch1
, const void *ch2
, int count
)
309 const int16_t* data1_16
;
310 const int16_t* data2_16
;
311 const int32_t* data1_32
;
312 const int32_t* data2_32
;
313 unsigned char* p
= wavbuffer
;
314 const int scale
= wavinfo
.sampledepth
- 15;
315 const int dc_bias
= 1 << (scale
- 1);
316 int channels
= (wavinfo
.stereomode
== STEREO_MONO
) ? 1 : 2;
318 /* Prevent idle poweroff */
319 rb
->reset_poweroff_timer();
322 count
= process_dsp(ch1
, ch2
, count
);
323 wavinfo
.totalsamples
+= count
;
326 unsigned char *s
= dspbuffer
, *d
= dspbuffer
;
337 crc32
= rb
->crc_32(dspbuffer
, count
* 2 * channels
, crc32
);
339 rb
->write(wavinfo
.fd
, dspbuffer
, count
* 2 * channels
);
343 if (wavinfo
.sampledepth
<= 16) {
347 switch(wavinfo
.stereomode
)
349 case STEREO_INTERLEAVED
:
351 int2le16(p
,*data1_16
++);
353 int2le16(p
,*data1_16
++);
358 case STEREO_NONINTERLEAVED
:
360 int2le16(p
,*data1_16
++);
362 int2le16(p
,*data2_16
++);
370 int2le16(p
,*data1_16
++);
379 switch(wavinfo
.stereomode
)
381 case STEREO_INTERLEAVED
:
383 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
385 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
390 case STEREO_NONINTERLEAVED
:
392 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
394 int2le16(p
, clip_sample((*data2_32
++ + dc_bias
) >> scale
));
402 int2le16(p
, clip_sample((*data1_32
++ + dc_bias
) >> scale
));
409 wavinfo
.totalsamples
+= count
;
411 crc32
= rb
->crc_32(wavbuffer
, p
- wavbuffer
, crc32
);
413 rb
->write(wavinfo
.fd
, wavbuffer
, p
- wavbuffer
);
417 /* Set song position in WPS (value in ms). */
418 static void set_elapsed(unsigned long value
)
424 /* Read next <size> amount bytes from file buffer to <ptr>.
425 Will return number of bytes read or 0 if end of file. */
426 static size_t read_filebuf(void *ptr
, size_t size
)
428 if (ci
.curpos
> (off_t
)track
.filesize
)
432 size_t realsize
= MIN(track
.filesize
-ci
.curpos
,size
);
434 /* check if we have enough bytes ready*/
435 if(realsize
>(audiobufsize
- (ci
.curpos
-offset
)))
437 /*rebuffer so that we start at ci.curpos*/
438 fill_buffer(ci
.curpos
);
441 rb
->memcpy(ptr
, audiobuf
+ (ci
.curpos
-offset
), realsize
);
442 ci
.curpos
+= realsize
;
448 /* Request pointer to file buffer which can be used to read
449 <realsize> amount of data. <reqsize> tells the buffer system
450 how much data it should try to allocate. If <realsize> is 0,
451 end of file is reached. */
452 static void* request_buffer(size_t *realsize
, size_t reqsize
)
454 *realsize
= MIN(track
.filesize
-ci
.curpos
,reqsize
);
456 /*check if we have enough bytes ready - requested > bufsize-currentbufpos*/
457 if(*realsize
>(audiobufsize
- (ci
.curpos
-offset
)))
459 /*rebuffer so that we start at ci.curpos*/
460 fill_buffer(ci
.curpos
);
463 return (audiobuf
+ (ci
.curpos
-offset
));
466 /* Advance file buffer position by <amount> amount of bytes. */
467 static void advance_buffer(size_t amount
)
473 /* Advance file buffer to a pointer location inside file buffer. */
474 static void advance_buffer_loc(void *ptr
)
476 ci
.curpos
= ptr
- (audiobuf
- offset
);
480 /* Seek file buffer to position <newpos> beginning of file. */
481 static bool seek_buffer(size_t newpos
)
488 /* Codec should call this function when it has done the seeking. */
489 static void seek_complete(void)
494 /* Request file change from file buffer. Returns true is next
495 track is available and changed. If return value is false,
496 codec should exit immediately with PLUGIN_OK status. */
497 static bool request_next_track(void)
499 /* We are only decoding a single track */
504 /* Free the buffer area of the current codec after its loaded */
505 static void discard_codec(void)
511 static void set_offset(size_t value
)
518 /* Configure different codec buffer parameters. */
519 static void configure(int setting
, intptr_t value
)
522 rb
->dsp_configure(ci
.dsp
, setting
, value
);
525 case DSP_SWITCH_FREQUENCY
:
526 case DSP_SET_FREQUENCY
:
527 DEBUGF("samplerate=%d\n",(int)value
);
528 wavinfo
.samplerate
= (int)value
;
531 case DSP_SET_SAMPLE_DEPTH
:
532 DEBUGF("sampledepth = %d\n",(int)value
);
533 wavinfo
.sampledepth
=(int)value
;
536 case DSP_SET_STEREO_MODE
:
537 DEBUGF("Stereo mode = %d\n",(int)value
);
538 wavinfo
.stereomode
=(int)value
;
544 static void init_ci(void)
546 /* --- Our "fake" implementations of the codec API functions. --- */
548 ci
.codec_get_buffer
= codec_get_buffer
;
550 if (wavinfo
.fd
>= 0 || checksum
) {
551 ci
.pcmbuf_insert
= pcmbuf_insert_wav_checksum
;
553 ci
.pcmbuf_insert
= pcmbuf_insert_null
;
556 ci
.set_elapsed
= set_elapsed
;
557 ci
.read_filebuf
= read_filebuf
;
558 ci
.request_buffer
= request_buffer
;
559 ci
.advance_buffer
= advance_buffer
;
560 ci
.advance_buffer_loc
= advance_buffer_loc
;
561 ci
.seek_buffer
= seek_buffer
;
562 ci
.seek_complete
= seek_complete
;
563 ci
.request_next_track
= request_next_track
;
564 ci
.discard_codec
= discard_codec
;
565 ci
.set_offset
= set_offset
;
566 ci
.configure
= configure
;
567 ci
.dsp
= (struct dsp_config
*)rb
->dsp_configure(NULL
, DSP_MYDSP
,
570 /* --- "Core" functions --- */
573 ci
.sleep
= rb
->sleep
;
574 ci
.yield
= rb
->yield
;
576 /* strings and memory */
577 ci
.strcpy
= rb
->strcpy
;
578 ci
.strlen
= rb
->strlen
;
579 ci
.strcmp
= rb
->strcmp
;
580 ci
.strcat
= rb
->strcat
;
581 ci
.memset
= rb
->memset
;
582 ci
.memcpy
= rb
->memcpy
;
583 ci
.memmove
= rb
->memmove
;
584 ci
.memcmp
= rb
->memcmp
;
585 ci
.memchr
= rb
->memchr
;
586 ci
.strcasestr
= rb
->strcasestr
;
587 #if defined(DEBUG) || defined(SIMULATOR)
588 ci
.debugf
= rb
->debugf
;
590 #ifdef ROCKBOX_HAS_LOGF
594 ci
.qsort
= rb
->qsort
;
595 ci
.global_settings
= rb
->global_settings
;
598 ci
.profile_thread
= rb
->profile_thread
;
599 ci
.profstop
= rb
->profstop
;
600 ci
.profile_func_enter
= rb
->profile_func_enter
;
601 ci
.profile_func_exit
= rb
->profile_func_exit
;
605 ci
.cpucache_invalidate
= rb
->cpucache_invalidate
;
606 ci
.cpucache_flush
= rb
->cpucache_flush
;
610 ci
.create_thread
= rb
->create_thread
;
611 ci
.thread_thaw
= rb
->thread_thaw
;
612 ci
.thread_wait
= rb
->thread_wait
;
613 ci
.semaphore_init
= rb
->semaphore_init
;
614 ci
.semaphore_wait
= rb
->semaphore_wait
;
615 ci
.semaphore_release
= rb
->semaphore_release
;
619 ci
.__div0
= rb
->__div0
;
623 static void codec_thread(void)
625 const char* codecname
;
628 codecname
= rb
->get_codec_filename(track
.id3
.codectype
);
630 /* Load the codec and start decoding. */
631 res
= rb
->codec_load_file(codecname
,&ci
);
633 /* Signal to the main thread that we are done */
634 endtick
= *rb
->current_tick
- rebuffertick
;
635 codec_playing
= false;
638 static enum plugin_status
test_track(const char* filename
)
641 enum plugin_status res
= PLUGIN_ERROR
;
645 unsigned long duration
;
650 /* Display filename (excluding any path)*/
651 ch
= rb
->strrchr(filename
, '/');
657 rb
->snprintf(str
,sizeof(str
),"%s",ch
);
660 log_text("Loading...",false);
662 fd
= rb
->open(filename
,O_RDONLY
);
665 log_text("Cannot open file",true);
669 track
.filesize
= rb
->filesize(fd
);
671 /* Clear the id3 struct */
672 rb
->memset(&track
.id3
, 0, sizeof(struct mp3entry
));
674 if (!rb
->get_metadata(&(track
.id3
), fd
, filename
))
676 log_text("Cannot read metadata",true);
680 if (track
.filesize
> audiosize
)
682 audiobufsize
=audiosize
;
686 audiobufsize
=track
.filesize
;
689 n
= rb
->read(fd
, audiobuf
, audiobufsize
);
691 if (n
!= audiobufsize
)
693 log_text("Read failed.",true);
698 /* Initialise the function pointers in the codec API */
701 /* Prepare the codec struct for playing the whole file */
702 ci
.filesize
= track
.filesize
;
704 ci
.taginfo_ready
= &taginfo_ready
;
706 ci
.stop_codec
= false;
711 rb
->dsp_configure(ci
.dsp
, DSP_RESET
, 0);
717 starttick
= *rb
->current_tick
;
719 codec_playing
= true;
721 rb
->codec_thread_do_callback(codec_thread
, NULL
);
723 /* Wait for codec thread to die */
724 while (codec_playing
)
727 rb
->snprintf(str
,sizeof(str
),"%d of %d",elapsed
,(int)track
.id3
.length
);
730 ticks
= endtick
- starttick
;
732 /* Be sure it is done */
733 rb
->codec_thread_do_callback(NULL
, NULL
);
739 rb
->snprintf(str
, sizeof(str
), "CRC32 - %08x", (unsigned)crc32
);
742 else if (wavinfo
.fd
< 0)
744 /* Display benchmark information */
745 rb
->snprintf(str
,sizeof(str
),"Decode time - %d.%02ds",(int)ticks
/100,(int)ticks
%100);
748 duration
= track
.id3
.length
/ 10;
749 rb
->snprintf(str
,sizeof(str
),"File duration - %d.%02ds",(int)duration
/100,(int)duration
%100);
753 speed
= duration
* 10000 / ticks
;
757 rb
->snprintf(str
,sizeof(str
),"%d.%02d%% realtime",(int)speed
/100,(int)speed
%100);
760 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
761 /* show effective clockrate in MHz needed for realtime decoding */
764 speed
= CPUFREQ_MAX
/ speed
;
765 rb
->snprintf(str
,sizeof(str
),"%d.%02dMHz needed for realtime",
766 (int)speed
/100,(int)speed
%100);
785 /* plugin entry point */
786 enum plugin_status
plugin_start(const void* parameter
)
788 int result
, selection
= 0;
789 enum plugin_status res
= PLUGIN_OK
;
791 struct dirent
*entry
;
794 char dirpath
[MAX_PATH
];
795 char filename
[MAX_PATH
];
798 if (parameter
== NULL
)
800 rb
->splash(HZ
*2, "No File");
804 wavbuffer
= rb
->plugin_get_buffer(&buffer_size
);
805 dspbuffer
= wavbuffer
+ buffer_size
/ 2;
807 codec_mallocbuf
= rb
->plugin_get_audio_buffer(&audiosize
);
808 audiobuf
= SKIPBYTES(codec_mallocbuf
, CODEC_SIZE
);
809 audiosize
-= CODEC_SIZE
;
811 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
814 rb
->lcd_clear_display();
823 SPEED_TEST_DIR_WITH_DSP
,
831 menu
, "test_codec", NULL
,
835 "Speed test with DSP",
836 "Speed test folder with DSP",
837 "Write WAV with DSP",
844 rb
->lcd_clear_display();
846 result
= rb
->do_menu(&menu
, &selection
, NULL
, false);
856 if ((checksum
= (result
== CHECKSUM
|| result
== CHECKSUM_DIR
)))
859 if ((use_dsp
= ((result
>= SPEED_TEST_WITH_DSP
)
860 && (result
<= WRITE_WAV_WITH_DSP
)))) {
863 if (result
== SPEED_TEST
) {
866 } else if (result
== SPEED_TEST_DIR
) {
870 /* Only create a log file when we are testing a folder */
871 if (!log_init(true)) {
872 rb
->splash(HZ
*2, "Cannot create logfile");
876 } else if (result
== WRITE_WAV
) {
878 init_wav("/test.wav");
879 if (wavinfo
.fd
< 0) {
880 rb
->splash(HZ
*2, "Cannot create /test.wav");
884 } else if (result
== MENU_ATTACHED_USB
) {
885 res
= PLUGIN_USB_CONNECTED
;
887 } else if (result
< 0) {
893 /* Test all files in the same directory as the file selected by the
896 rb
->strlcpy(dirpath
,parameter
,sizeof(dirpath
));
897 ch
= rb
->strrchr(dirpath
,'/');
900 DEBUGF("Scanning directory \"%s\"\n",dirpath
);
901 dir
= rb
->opendir(dirpath
);
903 entry
= rb
->readdir(dir
);
905 if (!(entry
->attribute
& ATTR_DIRECTORY
)) {
906 rb
->snprintf(filename
,sizeof(filename
),"%s%s",dirpath
,entry
->d_name
);
907 test_track(filename
);
911 /* Read next entry */
912 entry
= rb
->readdir(dir
);
918 /* Just test the file */
919 res
= test_track(parameter
);
921 /* Close WAV file (if there was one) */
922 if (wavinfo
.fd
>= 0) {
924 log_text("Wrote /test.wav",true);
926 while (rb
->button_get(true) != TESTCODEC_EXITBUTTON
);
928 rb
->button_clear_queue();
934 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
935 rb
->cpu_boost(false);