Add effective clockrate for realtime decoding to the displayed results of test_codec...
[kugel-rb.git] / apps / plugins / video.c
blobf1f6533f4f62f80754c7d871b11bcdf5a1a5c6cb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Plugin for video playback
11 * Reads raw image data + audio data from a file
12 * !!!!!!!!!! Code Police free zone !!!!!!!!!!
14 * Copyright (C) 2003-2004 J�g Hohensohn aka [IDC]Dragon
16 * All files in this archive are subject to the GNU General Public License.
17 * See the file COPYING in the source tree root for full license agreement.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
25 /****************** imports ******************/
27 #include "plugin.h"
28 #include "sh7034.h"
29 #include "system.h"
30 #include "helper.h"
32 #ifndef SIMULATOR /* not for simulator by now */
33 #ifdef HAVE_LCD_BITMAP /* and definitely not for the Player, haha */
35 PLUGIN_HEADER
37 /* variable button definitions */
38 #if CONFIG_KEYPAD == RECORDER_PAD
39 #define VIDEO_STOP_SEEK BUTTON_PLAY
40 #define VIDEO_RESUME BUTTON_PLAY
41 #define VIDEO_DEBUG BUTTON_F1
42 #define VIDEO_CONTRAST_DOWN BUTTON_F2
43 #define VIDEO_CONTRAST_UP BUTTON_F3
45 #elif CONFIG_KEYPAD == ONDIO_PAD
46 #define VIDEO_STOP_SEEK_PRE BUTTON_MENU
47 #define VIDEO_STOP_SEEK (BUTTON_MENU | BUTTON_REL)
48 #define VIDEO_RESUME BUTTON_RIGHT
49 #define VIDEO_CONTRAST_DOWN (BUTTON_MENU | BUTTON_DOWN)
50 #define VIDEO_CONTRAST_UP (BUTTON_MENU | BUTTON_UP)
52 #endif
53 /****************** constants ******************/
55 #define SCREENSIZE (LCD_WIDTH*LCD_HEIGHT/8) /* in bytes */
56 #define FPS 68 /* default fps for headerless (old video-only) file */
57 #define MAX_ACC 20 /* maximum FF/FR speedup */
58 #define FF_TICKS 3000; /* experimentally found nice */
60 /* trigger levels, we need about 80 kB/sec */
61 #define SPINUP_INIT 5000 /* from what level on to refill, in milliseconds */
62 #define SPINUP_SAFETY 700 /* how much on top of the measured spinup time */
63 #define CHUNK (1024*32) /* read size */
66 /****************** prototypes ******************/
67 void timer4_isr(void); /* IMIA4 ISR */
68 int check_button(void); /* determine next relative frame */
71 /****************** data types ******************/
73 /* plugins don't introduce headers, so structs are repeated from rvf_format.h */
75 #define HEADER_MAGIC 0x52564668 /* "RVFh" at file start */
76 #define AUDIO_MAGIC 0x41756446 /* "AudF" for each audio block */
77 #define FILEVERSION 100 /* 1.00 */
79 /* format type definitions */
80 #define VIDEOFORMAT_NO_VIDEO 0
81 #define VIDEOFORMAT_RAW 1
82 #define AUDIOFORMAT_NO_AUDIO 0
83 #define AUDIOFORMAT_MP3 1
84 #define AUDIOFORMAT_MP3_BITSWAPPED 2
86 /* bit flags */
87 #define FLAG_LOOP 0x00000001 /* loop the playback, e.g. for stills */
89 typedef struct /* contains whatever might be useful to the player */
91 /* general info (16 entries = 64 byte) */
92 unsigned long magic; /* HEADER_MAGIC */
93 unsigned long version; /* file version */
94 unsigned long flags; /* combination of FLAG_xx */
95 unsigned long blocksize; /* how many bytes per block (=video frame) */
96 unsigned long bps_average; /* bits per second of the whole stream */
97 unsigned long bps_peak; /* max. of above (audio may be VBR) */
98 unsigned long resume_pos; /* file position to resume to */
99 unsigned long reserved[9]; /* reserved, should be zero */
101 /* video info (16 entries = 64 byte) */
102 unsigned long video_format; /* one of VIDEOFORMAT_xxx */
103 unsigned long video_1st_frame; /* byte position of first video frame */
104 unsigned long video_duration; /* total length of video part, in ms */
105 unsigned long video_payload_size; /* total amount of video data, in bytes */
106 unsigned long video_bitrate; /* derived from resolution and frame time, in bps */
107 unsigned long video_frametime; /* frame interval in 11.0592 MHz clocks */
108 long video_preroll; /* video is how much ahead, in 11.0592 MHz clocks */
109 unsigned long video_width; /* in pixels */
110 unsigned long video_height; /* in pixels */
111 unsigned long video_reserved[7]; /* reserved, should be zero */
113 /* audio info (16 entries = 64 byte) */
114 unsigned long audio_format; /* one of AUDIOFORMAT_xxx */
115 unsigned long audio_1st_frame; /* byte position of first video frame */
116 unsigned long audio_duration; /* total length of audio part, in ms */
117 unsigned long audio_payload_size; /* total amount of audio data, in bytes */
118 unsigned long audio_avg_bitrate; /* average audio bitrate, in bits per second */
119 unsigned long audio_peak_bitrate; /* maximum bitrate */
120 unsigned long audio_headersize; /* offset to payload in audio frames */
121 long audio_min_associated; /* minimum offset to video frame, in bytes */
122 long audio_max_associated; /* maximum offset to video frame, in bytes */
123 unsigned long audio_reserved[7]; /* reserved, should be zero */
125 /* more to come... ? */
127 /* Note: padding up to 'blocksize' with zero following this header */
128 } tFileHeader;
130 typedef struct /* the little header for all audio blocks */
132 unsigned long magic; /* AUDIO_MAGIC indicates an audio block */
133 unsigned char previous_block; /* previous how many blocks backwards */
134 unsigned char next_block; /* next how many blocks forward */
135 short associated_video; /* offset to block with corresponding video */
136 unsigned short frame_start; /* offset to first frame starting in this block */
137 unsigned short frame_end; /* offset to behind last frame ending in this block */
138 } tAudioFrameHeader;
142 /****************** globals ******************/
144 static const struct plugin_api* rb; /* here is a global api struct pointer */
145 static char gPrint[32]; /* a global printf buffer, saves stack */
148 /* playstate */
149 static struct
151 enum
153 paused,
154 playing,
155 } state;
156 bool bAudioUnderrun;
157 bool bVideoUnderrun;
158 bool bHasAudio;
159 bool bHasVideo;
160 int nTimeOSD; /* OSD should stay for this many frames */
161 bool bDirtyOSD; /* OSD needs redraw */
162 bool bRefilling; /* set if refilling buffer */
163 bool bSeeking;
164 int nSeekAcc; /* accelleration value for seek */
165 int nSeekPos; /* current file position for seek */
166 bool bDiskSleep; /* disk is suspended */
167 #if FREQ == 12000000 /* Ondio speed kludge */
168 int nFrameTimeAdjusted;
169 #endif
170 } gPlay;
172 /* buffer information */
173 static struct
175 ssize_t bufsize;
176 int granularity; /* common multiple of block and sector size */
177 unsigned char* pBufStart; /* start of ring buffer */
178 unsigned char* pBufEnd; /* end of ring buffer */
179 unsigned char* pOSD; /* OSD memory (112 bytes for 112*8 pixels) */
181 int vidcount; /* how many video blocks are known in a row */
182 unsigned char* pBufFill; /* write pointer for disk, owned by main task */
183 unsigned char* pReadVideo; /* video readout, maintained by timer ISR */
184 unsigned char* pReadAudio; /* audio readout, maintained by demand ISR */
185 bool bEOF; /* flag for end of file */
186 int low_water; /* reload threshold */
187 int high_water; /* end of reload threshold */
188 int spinup_safety; /* safety margin when recalculating low_water */
189 int nReadChunk; /* how much data for normal buffer fill */
190 int nSeekChunk; /* how much data while seeking */
191 } gBuf;
193 /* statistics */
194 static struct
196 int minAudioAvail;
197 int minVideoAvail;
198 int nAudioUnderruns;
199 int nVideoUnderruns;
200 long minSpinup;
201 long maxSpinup;
202 } gStats;
204 tFileHeader gFileHdr; /* file header */
206 /****************** implementation ******************/
208 /* tool function: return how much playable audio/video is left */
209 int Available(unsigned char* pSnapshot)
211 if (pSnapshot <= gBuf.pBufFill)
212 return gBuf.pBufFill - pSnapshot;
213 else
214 return gBuf.bufsize - (pSnapshot - gBuf.pBufFill);
217 /* debug function to draw buffer indicators */
218 void DrawBuf(void)
220 int fill, video, audio;
222 rb->memset(gBuf.pOSD, 0x10, LCD_WIDTH); /* draw line */
223 gBuf.pOSD[0] = gBuf.pOSD[LCD_WIDTH-1] = 0xFE; /* ends */
225 /* calculate new tick positions */
226 fill = 1 + ((gBuf.pBufFill - gBuf.pBufStart) * (LCD_WIDTH-2)) / gBuf.bufsize;
227 video = 1 + ((gBuf.pReadVideo - gBuf.pBufStart) * (LCD_WIDTH-2)) / gBuf.bufsize;
228 audio = 1 + ((gBuf.pReadAudio - gBuf.pBufStart) * (LCD_WIDTH-2)) / gBuf.bufsize;
230 gBuf.pOSD[fill] |= 0x20; /* below the line, two pixels */
231 gBuf.pOSD[video] |= 0x08; /* one above */
232 gBuf.pOSD[audio] |= 0x04; /* two above */
234 if (gPlay.state == paused) /* we have to draw ourselves */
235 rb->lcd_update_rect(0, LCD_HEIGHT-8, LCD_WIDTH, 8);
236 else
237 gPlay.bDirtyOSD = true; /* redraw it with next timer IRQ */
241 /* helper function to draw a position indicator */
242 void DrawPosition(int pos, int total)
244 int w,h;
245 int sec; /* estimated seconds */
248 /* print the estimated position */
249 sec = pos / (gFileHdr.bps_average/8);
250 if (sec < 100*60) /* fits into mm:ss format */
251 rb->snprintf(gPrint, sizeof(gPrint), "%02d:%02dm", sec/60, sec%60);
252 else /* a very long clip, hh:mm format */
253 rb->snprintf(gPrint, sizeof(gPrint), "%02d:%02dh", sec/3600, (sec/60)%60);
254 rb->lcd_puts(0, 7, gPrint);
256 /* draw a slider over the rest of the line */
257 rb->lcd_getstringsize(gPrint, &w, &h);
258 w++;
259 rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN],w, LCD_HEIGHT-7, LCD_WIDTH-w,
260 7, total, 0, pos, HORIZONTAL);
262 if (gPlay.state == paused) /* we have to draw ourselves */
263 rb->lcd_update_rect(0, LCD_HEIGHT-8, LCD_WIDTH, 8);
264 else /* let the display time do it */
266 gPlay.nTimeOSD = 70;
267 gPlay.bDirtyOSD = true; /* redraw it with next timer IRQ */
272 /* helper function to change the volume by a certain amount, +/- */
273 void ChangeVolume(int delta)
275 int minvol = rb->sound_min(SOUND_VOLUME);
276 int maxvol = rb->sound_max(SOUND_VOLUME);
277 int vol = rb->global_settings->volume + delta;
279 if (vol > maxvol) vol = maxvol;
280 else if (vol < minvol) vol = minvol;
281 if (vol != rb->global_settings->volume)
283 rb->sound_set(SOUND_VOLUME, vol);
284 rb->global_settings->volume = vol;
285 rb->snprintf(gPrint, sizeof(gPrint), "Vol: %d dB", vol);
286 rb->lcd_puts(0, 7, gPrint);
287 if (gPlay.state == paused) /* we have to draw ourselves */
288 rb->lcd_update_rect(0, LCD_HEIGHT-8, LCD_WIDTH, 8);
289 else /* let the display time do it */
291 gPlay.nTimeOSD = 50; /* display it for 50 frames */
292 gPlay.bDirtyOSD = true; /* let the refresh copy it to LCD */
298 /* helper function to change the LCD contrast by a certain amount, +/- */
299 void ChangeContrast(int delta)
301 static int mycontrast = -1; /* the "permanent" value while running */
302 int contrast; /* updated value */
304 if (mycontrast == -1)
305 mycontrast = rb->global_settings->contrast;
307 contrast = mycontrast + delta;
308 if (contrast > 63) contrast = 63;
309 else if (contrast < 5) contrast = 5;
310 if (contrast != mycontrast)
312 rb->lcd_set_contrast(contrast);
313 mycontrast = contrast;
314 rb->snprintf(gPrint, sizeof(gPrint), "Contrast: %d", contrast);
315 rb->lcd_puts(0, 7, gPrint);
316 if (gPlay.state == paused) /* we have to draw ourselves */
317 rb->lcd_update_rect(0, LCD_HEIGHT-8, LCD_WIDTH, 8);
318 else /* let the display time do it */
320 gPlay.nTimeOSD = 50; /* display it for 50 frames */
321 gPlay.bDirtyOSD = true; /* let the refresh copy it to LCD */
327 /* sync the video to the current audio */
328 void SyncVideo(void)
330 tAudioFrameHeader* pAudioBuf;
332 pAudioBuf = (tAudioFrameHeader*)(gBuf.pReadAudio);
333 if (pAudioBuf->magic == AUDIO_MAGIC)
335 gBuf.vidcount = 0; /* nothing known */
336 /* sync the video position */
337 gBuf.pReadVideo = gBuf.pReadAudio +
338 (long)pAudioBuf->associated_video * (long)gFileHdr.blocksize;
340 /* handle possible wrap */
341 if (gBuf.pReadVideo >= gBuf.pBufEnd)
342 gBuf.pReadVideo -= gBuf.bufsize;
343 else if (gBuf.pReadVideo < gBuf.pBufStart)
344 gBuf.pReadVideo += gBuf.bufsize;
349 /* timer interrupt handler to display a frame */
350 void timer4_isr(void)
352 int available;
353 tAudioFrameHeader* pAudioBuf;
354 int height; /* height to display */
356 /* reduce height if we have OSD on */
357 height = gFileHdr.video_height/8;
358 if (gPlay.nTimeOSD > 0)
360 gPlay.nTimeOSD--;
361 height = MIN(LCD_HEIGHT/8-1, height); /* reserve bottom line */
362 if (gPlay.bDirtyOSD)
363 { /* OSD to bottom line */
364 rb->lcd_blit_mono(gBuf.pOSD, 0, LCD_HEIGHT/8-1,
365 LCD_WIDTH, 1, LCD_WIDTH);
366 gPlay.bDirtyOSD = false;
370 rb->lcd_blit_mono(gBuf.pReadVideo, 0, 0,
371 gFileHdr.video_width, height, gFileHdr.video_width);
373 available = Available(gBuf.pReadVideo);
375 /* loop to skip audio frame(s) */
376 while(1)
378 /* just for the statistics */
379 if (!gBuf.bEOF && available < gStats.minVideoAvail)
380 gStats.minVideoAvail = available;
382 if (available <= (int)gFileHdr.blocksize)
383 { /* no data for next frame */
385 if (gBuf.bEOF && (gFileHdr.flags & FLAG_LOOP))
386 { /* loop now, assuming the looped clip fits in memory */
387 gBuf.pReadVideo = gBuf.pBufStart + gFileHdr.video_1st_frame;
388 /* FixMe: pReadVideo is incremented below */
390 else
392 gPlay.bVideoUnderrun = true;
393 rb->timer_unregister(); /* disable ourselves */
394 return; /* no data available */
397 else /* normal advance for next time */
399 gBuf.pReadVideo += gFileHdr.blocksize;
400 if (gBuf.pReadVideo >= gBuf.pBufEnd)
401 gBuf.pReadVideo -= gBuf.bufsize; /* wraparound */
402 available -= gFileHdr.blocksize;
405 if (!gPlay.bHasAudio)
406 break; /* no need to skip any audio */
408 if (gBuf.vidcount)
410 /* we know the next is a video frame */
411 gBuf.vidcount--;
412 break; /* exit the loop */
415 pAudioBuf = (tAudioFrameHeader*)(gBuf.pReadVideo);
416 if (pAudioBuf->magic == AUDIO_MAGIC)
417 { /* we ran into audio, can happen after seek */
418 gBuf.vidcount = pAudioBuf->next_block;
419 if (gBuf.vidcount)
420 gBuf.vidcount--; /* minus the audio block */
422 } /* while */
426 /* ISR function to get more mp3 data */
427 void GetMoreMp3(unsigned char** start, size_t* size)
429 int available;
430 int advance;
432 tAudioFrameHeader* pAudioBuf = (tAudioFrameHeader*)(gBuf.pReadAudio);
434 advance = pAudioBuf->next_block * gFileHdr.blocksize;
436 available = Available(gBuf.pReadAudio);
438 /* just for the statistics */
439 if (!gBuf.bEOF && available < gStats.minAudioAvail)
440 gStats.minAudioAvail = available;
442 if (available < advance + (int)gFileHdr.blocksize || advance == 0)
444 gPlay.bAudioUnderrun = true;
445 return; /* no data available */
448 gBuf.pReadAudio += advance;
449 if (gBuf.pReadAudio >= gBuf.pBufEnd)
450 gBuf.pReadAudio -= gBuf.bufsize; /* wraparound */
452 *start = gBuf.pReadAudio + gFileHdr.audio_headersize;
453 *size = gFileHdr.blocksize - gFileHdr.audio_headersize;
457 int WaitForButton(void)
459 int button;
463 button = rb->button_get(true);
464 rb->default_event_handler(button);
465 } while ((button & BUTTON_REL) && button != SYS_USB_CONNECTED);
467 return button;
471 bool WantResume(int fd)
473 int button;
475 rb->lcd_puts(0, 0, "Resume to this");
476 rb->lcd_puts(0, 1, "last position?");
477 rb->lcd_puts(0, 2, "PLAY = yes");
478 rb->lcd_puts(0, 3, "Any Other = no");
479 rb->lcd_puts(0, 4, " (plays from start)");
480 DrawPosition(gFileHdr.resume_pos, rb->filesize(fd));
481 rb->lcd_update();
483 button = WaitForButton();
484 return (button == VIDEO_RESUME);
488 int SeekTo(int fd, int nPos)
490 int read_now, got_now;
492 if (gPlay.bHasAudio)
493 rb->mp3_play_stop(); /* stop audio ISR */
494 if (gPlay.bHasVideo)
495 rb->timer_unregister(); /* stop the timer */
497 rb->lseek(fd, nPos, SEEK_SET);
499 gBuf.pBufFill = gBuf.pBufStart; /* all empty */
500 gBuf.pReadVideo = gBuf.pReadAudio = gBuf.pBufStart;
502 read_now = gBuf.low_water - 1; /* less than low water, so loading will continue */
503 read_now -= read_now % gBuf.granularity; /* round down to granularity */
504 got_now = rb->read(fd, gBuf.pBufFill, read_now);
505 gBuf.bEOF = (read_now != got_now);
506 gBuf.pBufFill += got_now;
508 if (nPos == 0)
509 { /* we seeked to the start */
510 if (gPlay.bHasVideo)
511 gBuf.pReadVideo += gFileHdr.video_1st_frame;
513 if (gPlay.bHasAudio)
514 gBuf.pReadAudio += gFileHdr.audio_1st_frame;
516 else
517 { /* we have to search for the positions */
518 if (gPlay.bHasAudio) /* prepare audio playback, if contained */
520 /* search for audio frame */
521 while (((tAudioFrameHeader*)(gBuf.pReadAudio))->magic != AUDIO_MAGIC)
522 gBuf.pReadAudio += gFileHdr.blocksize;
524 if (gPlay.bHasVideo)
525 SyncVideo(); /* pick the right video for that */
529 /* synchronous start */
530 gPlay.state = playing;
531 if (gPlay.bHasAudio)
533 gPlay.bAudioUnderrun = false;
534 rb->mp3_play_data(gBuf.pReadAudio + gFileHdr.audio_headersize,
535 gFileHdr.blocksize - gFileHdr.audio_headersize, GetMoreMp3);
536 rb->mp3_play_pause(true); /* kickoff audio */
538 if (gPlay.bHasVideo)
540 gPlay.bVideoUnderrun = false;
541 /* start display interrupt */
542 #if FREQ == 12000000 /* Ondio speed kludge */
543 rb->timer_register(1, NULL, gPlay.nFrameTimeAdjusted, 1,
544 timer4_isr IF_COP(, CPU));
545 #else
546 rb->timer_register(1, NULL, gFileHdr.video_frametime, 1,
547 timer4_isr IF_COP(, CPU));
548 #endif
551 return 0;
554 /* called from default_event_handler_ex() or at end of playback */
555 void Cleanup(void *fd)
557 rb->close(*(int*)fd); /* close the file */
559 if (gPlay.bHasVideo)
560 rb->timer_unregister(); /* stop video ISR, now I can use the display again */
562 if (gPlay.bHasAudio)
563 rb->mp3_play_stop(); /* stop audio ISR */
565 /* Turn on backlight timeout (revert to settings) */
566 backlight_use_settings(rb); /* backlight control in lib/helper.c */
568 /* restore normal contrast */
569 rb->lcd_set_contrast(rb->global_settings->contrast);
572 /* returns >0 if continue, =0 to stop, <0 to abort (USB) */
573 int PlayTick(int fd)
575 int button;
576 static int lastbutton = 0;
577 int avail_audio = -1, avail_video = -1;
578 int retval = 1;
579 int filepos;
581 /* check buffer level */
583 if (gPlay.bHasAudio)
584 avail_audio = Available(gBuf.pReadAudio);
585 if (gPlay.bHasVideo)
586 avail_video = Available(gBuf.pReadVideo);
588 if ((gPlay.bHasAudio && avail_audio < gBuf.low_water)
589 || (gPlay.bHasVideo && avail_video < gBuf.low_water))
591 gPlay.bRefilling = true; /* go to refill mode */
594 if ((!gPlay.bHasAudio || gPlay.bAudioUnderrun)
595 && (!gPlay.bHasVideo || gPlay.bVideoUnderrun)
596 && gBuf.bEOF)
598 if (gFileHdr.resume_pos)
599 { /* we played til the end, clear resume position */
600 gFileHdr.resume_pos = 0;
601 rb->lseek(fd, 0, SEEK_SET); /* save resume position */
602 rb->write(fd, &gFileHdr, sizeof(gFileHdr));
604 Cleanup(&fd);
605 return 0; /* all expired */
608 if (!gPlay.bRefilling || gBuf.bEOF)
609 { /* nothing to do */
610 button = rb->button_get_w_tmo(HZ/10);
612 else
613 { /* refill buffer */
614 int read_now, got_now;
615 int buf_free;
616 long spinup; /* measure the spinup time */
618 /* how much can we reload, don't fill completely, would appear empty */
619 buf_free = gBuf.bufsize - MAX(avail_audio, avail_video) - gBuf.high_water;
620 if (buf_free < 0)
621 buf_free = 0; /* just for safety */
622 buf_free -= buf_free % gBuf.granularity; /* round down to granularity */
624 /* in one piece max. up to buffer end (wrap after that) */
625 read_now = MIN(buf_free, gBuf.pBufEnd - gBuf.pBufFill);
627 /* load piecewise, to stay responsive */
628 read_now = MIN(read_now, gBuf.nReadChunk);
630 if (read_now == buf_free)
631 gPlay.bRefilling = false; /* last piece requested */
633 spinup = *rb->current_tick; /* in case this is interesting below */
635 got_now = rb->read(fd, gBuf.pBufFill, read_now);
636 if (got_now != read_now || read_now == 0)
638 gBuf.bEOF = true;
639 gPlay.bRefilling = false;
642 if (gPlay.bDiskSleep) /* statistics about the spinup time */
644 spinup = *rb->current_tick - spinup;
645 gPlay.bDiskSleep = false;
646 if (spinup > gStats.maxSpinup)
647 gStats.maxSpinup = spinup;
648 if (spinup < gStats.minSpinup)
649 gStats.minSpinup = spinup;
651 /* recalculate the low water mark from real measurements */
652 gBuf.low_water = (gStats.maxSpinup + gBuf.spinup_safety)
653 * gFileHdr.bps_peak / 8 / HZ;
656 if (!gPlay.bRefilling
657 #ifndef HAVE_FLASH_STORAGE
658 && rb->global_settings->disk_spindown < 20 /* condition for test only */
659 #endif
662 rb->ata_sleep(); /* no point in leaving the disk run til timeout */
663 gPlay.bDiskSleep = true;
666 gBuf.pBufFill += got_now;
667 if (gBuf.pBufFill >= gBuf.pBufEnd)
668 gBuf.pBufFill = gBuf.pBufStart; /* wrap */
670 rb->yield(); /* have mercy with the other threads */
671 button = rb->button_get(false);
674 /* check keypresses */
676 if (button != BUTTON_NONE)
678 filepos = rb->lseek(fd, 0, SEEK_CUR);
680 if (gPlay.bHasVideo) /* video position is more accurate */
681 filepos -= Available(gBuf.pReadVideo); /* take video position */
682 else
683 filepos -= Available(gBuf.pReadAudio); /* else audio */
685 switch (button) { /* set exit conditions */
686 case BUTTON_OFF:
687 if (gFileHdr.magic == HEADER_MAGIC /* only if file has header */
688 && !(gFileHdr.flags & FLAG_LOOP)) /* not for stills */
690 gFileHdr.resume_pos = filepos;
691 rb->lseek(fd, 0, SEEK_SET); /* save resume position */
692 rb->write(fd, &gFileHdr, sizeof(gFileHdr));
694 Cleanup(&fd);
695 retval = 0; /* signal "stopped" to caller */
696 break;
697 case VIDEO_STOP_SEEK:
698 #ifdef VIDEO_STOP_SEEK_PRE
699 if (lastbutton != VIDEO_STOP_SEEK_PRE)
700 break;
701 #endif
702 if (gPlay.bSeeking)
704 gPlay.bSeeking = false;
705 gPlay.state = playing;
706 SeekTo(fd, gPlay.nSeekPos);
708 else if (gPlay.state == playing)
710 gPlay.state = paused;
711 if (gPlay.bHasAudio)
712 rb->mp3_play_pause(false); /* pause audio */
713 if (gPlay.bHasVideo)
714 rb->timer_unregister(); /* stop the timer */
716 else if (gPlay.state == paused)
718 gPlay.state = playing;
719 if (gPlay.bHasAudio)
721 if (gPlay.bHasVideo)
722 SyncVideo();
723 rb->mp3_play_pause(true); /* play audio */
725 if (gPlay.bHasVideo)
726 { /* start the video */
727 #if FREQ == 12000000 /* Ondio speed kludge */
728 rb->timer_register(1, NULL,
729 gPlay.nFrameTimeAdjusted, 1, timer4_isr);
730 #else
731 rb->timer_register(1, NULL,
732 gFileHdr.video_frametime, 1, timer4_isr);
733 #endif
736 break;
737 case BUTTON_UP:
738 case BUTTON_UP | BUTTON_REPEAT:
739 if (gPlay.bHasAudio)
740 ChangeVolume(1);
741 break;
742 case BUTTON_DOWN:
743 case BUTTON_DOWN | BUTTON_REPEAT:
744 if (gPlay.bHasAudio)
745 ChangeVolume(-1);
746 break;
747 case BUTTON_LEFT:
748 case BUTTON_LEFT | BUTTON_REPEAT:
749 if (!gPlay.bSeeking) /* prepare seek */
751 gPlay.nSeekPos = filepos;
752 gPlay.bSeeking = true;
753 gPlay.nSeekAcc = 0;
755 else if (gPlay.nSeekAcc > 0) /* other direction, stop sliding */
756 gPlay.nSeekAcc = 0;
757 else
758 gPlay.nSeekAcc--;
759 break;
760 case BUTTON_RIGHT:
761 case BUTTON_RIGHT | BUTTON_REPEAT:
762 if (!gPlay.bSeeking) /* prepare seek */
764 gPlay.nSeekPos = filepos;
765 gPlay.bSeeking = true;
766 gPlay.nSeekAcc = 0;
768 else if (gPlay.nSeekAcc < 0) /* other direction, stop sliding */
769 gPlay.nSeekAcc = 0;
770 else
771 gPlay.nSeekAcc++;
772 break;
773 #ifdef VIDEO_DEBUG
774 case VIDEO_DEBUG: /* debug key */
775 case VIDEO_DEBUG | BUTTON_REPEAT:
776 DrawBuf(); /* show buffer status */
777 gPlay.nTimeOSD = 30;
778 gPlay.bDirtyOSD = true;
779 break;
780 #endif
781 case VIDEO_CONTRAST_DOWN: /* contrast down */
782 case VIDEO_CONTRAST_DOWN | BUTTON_REPEAT:
783 if (gPlay.bHasVideo)
784 ChangeContrast(-1);
785 break;
786 case VIDEO_CONTRAST_UP: /* contrast up */
787 case VIDEO_CONTRAST_UP | BUTTON_REPEAT:
788 if (gPlay.bHasVideo)
789 ChangeContrast(1);
790 break;
791 default:
792 if (rb->default_event_handler_ex(button, Cleanup, &fd)
793 == SYS_USB_CONNECTED)
794 retval = -1; /* signal "aborted" to caller */
795 break;
798 lastbutton = button;
799 } /* if (button != BUTTON_NONE) */
802 /* handle seeking */
804 if (gPlay.bSeeking) /* seeking? */
806 if (gPlay.nSeekAcc < -MAX_ACC)
807 gPlay.nSeekAcc = -MAX_ACC;
808 else if (gPlay.nSeekAcc > MAX_ACC)
809 gPlay.nSeekAcc = MAX_ACC;
811 gPlay.nSeekPos += gPlay.nSeekAcc * gBuf.nSeekChunk;
812 if (gPlay.nSeekPos < 0)
813 gPlay.nSeekPos = 0;
814 if (gPlay.nSeekPos > rb->filesize(fd) - gBuf.granularity)
816 gPlay.nSeekPos = rb->filesize(fd);
817 gPlay.nSeekPos -= gPlay.nSeekPos % gBuf.granularity;
819 DrawPosition(gPlay.nSeekPos, rb->filesize(fd));
823 /* check + recover underruns */
825 if ((gPlay.bAudioUnderrun || gPlay.bVideoUnderrun) && !gBuf.bEOF)
827 gBuf.spinup_safety += HZ/2; /* add extra spinup time for the future */
828 filepos = rb->lseek(fd, 0, SEEK_CUR);
830 if (gPlay.bHasVideo && gPlay.bVideoUnderrun)
832 gStats.nVideoUnderruns++;
833 filepos -= Available(gBuf.pReadVideo); /* take video position */
834 SeekTo(fd, filepos);
836 else if (gPlay.bHasAudio && gPlay.bAudioUnderrun)
838 gStats.nAudioUnderruns++;
839 filepos -= Available(gBuf.pReadAudio); /* else audio */
840 SeekTo(fd, filepos);
844 return retval;
848 int main(char* filename)
850 int file_size;
851 int fd; /* file descriptor handle */
852 int read_now, got_now;
853 int button = 0;
854 int retval;
856 /* try to open the file */
857 fd = rb->open(filename, O_RDWR);
858 if (fd < 0)
859 return PLUGIN_ERROR;
860 file_size = rb->filesize(fd);
862 /* reset pitch value to ensure synchronous playback */
863 rb->sound_set_pitch(1000);
865 /* init statistics */
866 rb->memset(&gStats, 0, sizeof(gStats));
867 gStats.minAudioAvail = gStats.minVideoAvail = INT_MAX;
868 gStats.minSpinup = INT_MAX;
870 /* init playback state */
871 rb->memset(&gPlay, 0, sizeof(gPlay));
873 /* init buffer */
874 rb->memset(&gBuf, 0, sizeof(gBuf));
875 gBuf.pOSD = rb->lcd_framebuffer + LCD_WIDTH*7; /* last screen line */
876 gBuf.pBufStart = rb->plugin_get_audio_buffer((size_t *)&gBuf.bufsize);
877 /*gBuf.bufsize = 1700*1024; // test, like 2MB version!!!! */
878 gBuf.pBufFill = gBuf.pBufStart; /* all empty */
880 /* load file header */
881 read_now = sizeof(gFileHdr);
882 got_now = rb->read(fd, &gFileHdr, read_now);
883 rb->lseek(fd, 0, SEEK_SET); /* rewind to restart sector-aligned */
884 if (got_now != read_now)
886 rb->close(fd);
887 return (PLUGIN_ERROR);
890 /* check header */
891 if (gFileHdr.magic != HEADER_MAGIC)
892 { /* old file, use default info */
893 rb->memset(&gFileHdr, 0, sizeof(gFileHdr));
894 gFileHdr.blocksize = SCREENSIZE;
895 if (file_size < SCREENSIZE * FPS) /* less than a second */
896 gFileHdr.flags |= FLAG_LOOP;
897 gFileHdr.video_format = VIDEOFORMAT_RAW;
898 gFileHdr.video_width = LCD_WIDTH;
899 gFileHdr.video_height = LCD_HEIGHT;
900 gFileHdr.video_frametime = 11059200 / FPS;
901 gFileHdr.bps_peak = gFileHdr.bps_average = LCD_WIDTH * LCD_HEIGHT * FPS;
904 #if FREQ == 12000000 /* Ondio speed kludge, 625 / 576 == 12000000 / 11059200 */
905 gPlay.nFrameTimeAdjusted = (gFileHdr.video_frametime * 625) / 576;
906 #endif
908 /* continue buffer init: align the end, calc low water, read sizes */
909 gBuf.granularity = gFileHdr.blocksize;
910 while (gBuf.granularity % 512) /* common multiple of sector size */
911 gBuf.granularity *= 2;
912 gBuf.bufsize -= gBuf.bufsize % gBuf.granularity; /* round down */
913 gBuf.pBufEnd = gBuf.pBufStart + gBuf.bufsize;
914 gBuf.low_water = SPINUP_INIT * gFileHdr.bps_peak / 8000;
915 gBuf.spinup_safety = SPINUP_SAFETY * HZ / 1000; /* in time ticks */
916 if (gFileHdr.audio_min_associated < 0)
917 gBuf.high_water = 0 - gFileHdr.audio_min_associated;
918 else
919 gBuf.high_water = 1; /* never fill buffer completely, would appear empty */
920 gBuf.nReadChunk = (CHUNK + gBuf.granularity - 1); /* round up */
921 gBuf.nReadChunk -= gBuf.nReadChunk % gBuf.granularity;/* and align */
922 gBuf.nSeekChunk = rb->filesize(fd) / FF_TICKS;
923 gBuf.nSeekChunk += gBuf.granularity - 1; /* round up */
924 gBuf.nSeekChunk -= gBuf.nSeekChunk % gBuf.granularity; /* and align */
926 /* prepare video playback, if contained */
927 if (gFileHdr.video_format == VIDEOFORMAT_RAW)
929 gPlay.bHasVideo = true;
930 /* Turn off backlight timeout */
931 backlight_force_on(rb); /* backlight control in lib/helper.c */
934 /* prepare audio playback, if contained */
935 if (gFileHdr.audio_format == AUDIOFORMAT_MP3_BITSWAPPED)
937 gPlay.bHasAudio = true;
940 /* start playback by seeking to zero or resume position */
941 if (gFileHdr.resume_pos && WantResume(fd)) /* ask the user */
942 SeekTo(fd, gFileHdr.resume_pos);
943 else
944 SeekTo(fd, 0);
946 /* all that's left to do is keep the buffer full */
947 do /* the main loop */
949 retval = PlayTick(fd);
950 } while (retval > 0);
952 if (retval < 0) /* aborted? */
954 return PLUGIN_USB_CONNECTED;
957 #ifndef DEBUG /* for release compilations, only display the stats in case of error */
958 if (gStats.nAudioUnderruns || gStats.nVideoUnderruns)
959 #endif
961 /* display statistics */
962 rb->lcd_clear_display();
963 rb->snprintf(gPrint, sizeof(gPrint), "%d Audio Underruns", gStats.nAudioUnderruns);
964 rb->lcd_puts(0, 0, gPrint);
965 rb->snprintf(gPrint, sizeof(gPrint), "%d Video Underruns", gStats.nVideoUnderruns);
966 rb->lcd_puts(0, 1, gPrint);
967 rb->snprintf(gPrint, sizeof(gPrint), "%d MinAudio bytes", gStats.minAudioAvail);
968 rb->lcd_puts(0, 2, gPrint);
969 rb->snprintf(gPrint, sizeof(gPrint), "%d MinVideo bytes", gStats.minVideoAvail);
970 rb->lcd_puts(0, 3, gPrint);
971 rb->snprintf(gPrint, sizeof(gPrint), "MinSpinup %ld.%02ld", gStats.minSpinup/HZ, gStats.minSpinup%HZ);
972 rb->lcd_puts(0, 4, gPrint);
973 rb->snprintf(gPrint, sizeof(gPrint), "MaxSpinup %ld.%02ld", gStats.maxSpinup/HZ, gStats.maxSpinup%HZ);
974 rb->lcd_puts(0, 5, gPrint);
975 rb->snprintf(gPrint, sizeof(gPrint), "LowWater: %d", gBuf.low_water);
976 rb->lcd_puts(0, 6, gPrint);
977 rb->snprintf(gPrint, sizeof(gPrint), "HighWater: %d", gBuf.high_water);
978 rb->lcd_puts(0, 7, gPrint);
980 rb->lcd_update();
981 button = WaitForButton();
983 return (button == SYS_USB_CONNECTED) ? PLUGIN_USB_CONNECTED : PLUGIN_OK;
987 /***************** Plugin Entry Point *****************/
989 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
991 rb = api; /* copy to global api pointer */
993 if (parameter == NULL)
995 rb->splash(HZ*2, "Play .rvf file!");
996 return PLUGIN_ERROR;
999 /* now go ahead and have fun! */
1000 return main((char*) parameter);
1003 #endif /* #ifdef HAVE_LCD_BITMAP */
1004 #endif /* #ifndef SIMULATOR */