Device detection based on USB PIDs. This is currently linux only and requires libusb...
[Rockbox.git] / apps / plugins / video.c
blob2799e3d5d1b99f2a5b2c2a647d8bc20da2978787
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 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(gBuf.pOSD, 0, LCD_HEIGHT/8-1,
365 LCD_WIDTH, 1, LCD_WIDTH);
366 gPlay.bDirtyOSD = false;
370 rb->lcd_blit(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, timer4_isr);
544 #else
545 rb->timer_register(1, NULL, gFileHdr.video_frametime, 1, timer4_isr);
546 #endif
549 return 0;
552 /* called from default_event_handler_ex() or at end of playback */
553 void Cleanup(void *fd)
555 rb->close(*(int*)fd); /* close the file */
557 if (gPlay.bHasVideo)
558 rb->timer_unregister(); /* stop video ISR, now I can use the display again */
560 if (gPlay.bHasAudio)
561 rb->mp3_play_stop(); /* stop audio ISR */
563 /* Turn on backlight timeout (revert to settings) */
564 backlight_use_settings(rb); /* backlight control in lib/helper.c */
566 /* restore normal contrast */
567 rb->lcd_set_contrast(rb->global_settings->contrast);
570 /* returns >0 if continue, =0 to stop, <0 to abort (USB) */
571 int PlayTick(int fd)
573 int button;
574 static int lastbutton = 0;
575 int avail_audio = -1, avail_video = -1;
576 int retval = 1;
577 int filepos;
579 /* check buffer level */
581 if (gPlay.bHasAudio)
582 avail_audio = Available(gBuf.pReadAudio);
583 if (gPlay.bHasVideo)
584 avail_video = Available(gBuf.pReadVideo);
586 if ((gPlay.bHasAudio && avail_audio < gBuf.low_water)
587 || (gPlay.bHasVideo && avail_video < gBuf.low_water))
589 gPlay.bRefilling = true; /* go to refill mode */
592 if ((!gPlay.bHasAudio || gPlay.bAudioUnderrun)
593 && (!gPlay.bHasVideo || gPlay.bVideoUnderrun)
594 && gBuf.bEOF)
596 if (gFileHdr.resume_pos)
597 { /* we played til the end, clear resume position */
598 gFileHdr.resume_pos = 0;
599 rb->lseek(fd, 0, SEEK_SET); /* save resume position */
600 rb->write(fd, &gFileHdr, sizeof(gFileHdr));
602 Cleanup(&fd);
603 return 0; /* all expired */
606 if (!gPlay.bRefilling || gBuf.bEOF)
607 { /* nothing to do */
608 button = rb->button_get_w_tmo(HZ/10);
610 else
611 { /* refill buffer */
612 int read_now, got_now;
613 int buf_free;
614 long spinup; /* measure the spinup time */
616 /* how much can we reload, don't fill completely, would appear empty */
617 buf_free = gBuf.bufsize - MAX(avail_audio, avail_video) - gBuf.high_water;
618 if (buf_free < 0)
619 buf_free = 0; /* just for safety */
620 buf_free -= buf_free % gBuf.granularity; /* round down to granularity */
622 /* in one piece max. up to buffer end (wrap after that) */
623 read_now = MIN(buf_free, gBuf.pBufEnd - gBuf.pBufFill);
625 /* load piecewise, to stay responsive */
626 read_now = MIN(read_now, gBuf.nReadChunk);
628 if (read_now == buf_free)
629 gPlay.bRefilling = false; /* last piece requested */
631 spinup = *rb->current_tick; /* in case this is interesting below */
633 got_now = rb->read(fd, gBuf.pBufFill, read_now);
634 if (got_now != read_now || read_now == 0)
636 gBuf.bEOF = true;
637 gPlay.bRefilling = false;
640 if (gPlay.bDiskSleep) /* statistics about the spinup time */
642 spinup = *rb->current_tick - spinup;
643 gPlay.bDiskSleep = false;
644 if (spinup > gStats.maxSpinup)
645 gStats.maxSpinup = spinup;
646 if (spinup < gStats.minSpinup)
647 gStats.minSpinup = spinup;
649 /* recalculate the low water mark from real measurements */
650 gBuf.low_water = (gStats.maxSpinup + gBuf.spinup_safety)
651 * gFileHdr.bps_peak / 8 / HZ;
654 if (!gPlay.bRefilling
655 #ifndef HAVE_FLASH_STORAGE
656 && rb->global_settings->disk_spindown < 20 /* condition for test only */
657 #endif
660 rb->ata_sleep(); /* no point in leaving the disk run til timeout */
661 gPlay.bDiskSleep = true;
664 gBuf.pBufFill += got_now;
665 if (gBuf.pBufFill >= gBuf.pBufEnd)
666 gBuf.pBufFill = gBuf.pBufStart; /* wrap */
668 rb->yield(); /* have mercy with the other threads */
669 button = rb->button_get(false);
672 /* check keypresses */
674 if (button != BUTTON_NONE)
676 filepos = rb->lseek(fd, 0, SEEK_CUR);
678 if (gPlay.bHasVideo) /* video position is more accurate */
679 filepos -= Available(gBuf.pReadVideo); /* take video position */
680 else
681 filepos -= Available(gBuf.pReadAudio); /* else audio */
683 switch (button) { /* set exit conditions */
684 case BUTTON_OFF:
685 if (gFileHdr.magic == HEADER_MAGIC /* only if file has header */
686 && !(gFileHdr.flags & FLAG_LOOP)) /* not for stills */
688 gFileHdr.resume_pos = filepos;
689 rb->lseek(fd, 0, SEEK_SET); /* save resume position */
690 rb->write(fd, &gFileHdr, sizeof(gFileHdr));
692 Cleanup(&fd);
693 retval = 0; /* signal "stopped" to caller */
694 break;
695 case VIDEO_STOP_SEEK:
696 #ifdef VIDEO_STOP_SEEK_PRE
697 if (lastbutton != VIDEO_STOP_SEEK_PRE)
698 break;
699 #endif
700 if (gPlay.bSeeking)
702 gPlay.bSeeking = false;
703 gPlay.state = playing;
704 SeekTo(fd, gPlay.nSeekPos);
706 else if (gPlay.state == playing)
708 gPlay.state = paused;
709 if (gPlay.bHasAudio)
710 rb->mp3_play_pause(false); /* pause audio */
711 if (gPlay.bHasVideo)
712 rb->timer_unregister(); /* stop the timer */
714 else if (gPlay.state == paused)
716 gPlay.state = playing;
717 if (gPlay.bHasAudio)
719 if (gPlay.bHasVideo)
720 SyncVideo();
721 rb->mp3_play_pause(true); /* play audio */
723 if (gPlay.bHasVideo)
724 { /* start the video */
725 #if FREQ == 12000000 /* Ondio speed kludge */
726 rb->timer_register(1, NULL,
727 gPlay.nFrameTimeAdjusted, 1, timer4_isr);
728 #else
729 rb->timer_register(1, NULL,
730 gFileHdr.video_frametime, 1, timer4_isr);
731 #endif
734 break;
735 case BUTTON_UP:
736 case BUTTON_UP | BUTTON_REPEAT:
737 if (gPlay.bHasAudio)
738 ChangeVolume(1);
739 break;
740 case BUTTON_DOWN:
741 case BUTTON_DOWN | BUTTON_REPEAT:
742 if (gPlay.bHasAudio)
743 ChangeVolume(-1);
744 break;
745 case BUTTON_LEFT:
746 case BUTTON_LEFT | BUTTON_REPEAT:
747 if (!gPlay.bSeeking) /* prepare seek */
749 gPlay.nSeekPos = filepos;
750 gPlay.bSeeking = true;
751 gPlay.nSeekAcc = 0;
753 else if (gPlay.nSeekAcc > 0) /* other direction, stop sliding */
754 gPlay.nSeekAcc = 0;
755 else
756 gPlay.nSeekAcc--;
757 break;
758 case BUTTON_RIGHT:
759 case BUTTON_RIGHT | BUTTON_REPEAT:
760 if (!gPlay.bSeeking) /* prepare seek */
762 gPlay.nSeekPos = filepos;
763 gPlay.bSeeking = true;
764 gPlay.nSeekAcc = 0;
766 else if (gPlay.nSeekAcc < 0) /* other direction, stop sliding */
767 gPlay.nSeekAcc = 0;
768 else
769 gPlay.nSeekAcc++;
770 break;
771 #ifdef VIDEO_DEBUG
772 case VIDEO_DEBUG: /* debug key */
773 case VIDEO_DEBUG | BUTTON_REPEAT:
774 DrawBuf(); /* show buffer status */
775 gPlay.nTimeOSD = 30;
776 gPlay.bDirtyOSD = true;
777 break;
778 #endif
779 case VIDEO_CONTRAST_DOWN: /* contrast down */
780 case VIDEO_CONTRAST_DOWN | BUTTON_REPEAT:
781 if (gPlay.bHasVideo)
782 ChangeContrast(-1);
783 break;
784 case VIDEO_CONTRAST_UP: /* contrast up */
785 case VIDEO_CONTRAST_UP | BUTTON_REPEAT:
786 if (gPlay.bHasVideo)
787 ChangeContrast(1);
788 break;
789 default:
790 if (rb->default_event_handler_ex(button, Cleanup, &fd)
791 == SYS_USB_CONNECTED)
792 retval = -1; /* signal "aborted" to caller */
793 break;
796 lastbutton = button;
797 } /* if (button != BUTTON_NONE) */
800 /* handle seeking */
802 if (gPlay.bSeeking) /* seeking? */
804 if (gPlay.nSeekAcc < -MAX_ACC)
805 gPlay.nSeekAcc = -MAX_ACC;
806 else if (gPlay.nSeekAcc > MAX_ACC)
807 gPlay.nSeekAcc = MAX_ACC;
809 gPlay.nSeekPos += gPlay.nSeekAcc * gBuf.nSeekChunk;
810 if (gPlay.nSeekPos < 0)
811 gPlay.nSeekPos = 0;
812 if (gPlay.nSeekPos > rb->filesize(fd) - gBuf.granularity)
814 gPlay.nSeekPos = rb->filesize(fd);
815 gPlay.nSeekPos -= gPlay.nSeekPos % gBuf.granularity;
817 DrawPosition(gPlay.nSeekPos, rb->filesize(fd));
821 /* check + recover underruns */
823 if ((gPlay.bAudioUnderrun || gPlay.bVideoUnderrun) && !gBuf.bEOF)
825 gBuf.spinup_safety += HZ/2; /* add extra spinup time for the future */
826 filepos = rb->lseek(fd, 0, SEEK_CUR);
828 if (gPlay.bHasVideo && gPlay.bVideoUnderrun)
830 gStats.nVideoUnderruns++;
831 filepos -= Available(gBuf.pReadVideo); /* take video position */
832 SeekTo(fd, filepos);
834 else if (gPlay.bHasAudio && gPlay.bAudioUnderrun)
836 gStats.nAudioUnderruns++;
837 filepos -= Available(gBuf.pReadAudio); /* else audio */
838 SeekTo(fd, filepos);
842 return retval;
846 int main(char* filename)
848 int file_size;
849 int fd; /* file descriptor handle */
850 int read_now, got_now;
851 int button = 0;
852 int retval;
854 /* try to open the file */
855 fd = rb->open(filename, O_RDWR);
856 if (fd < 0)
857 return PLUGIN_ERROR;
858 file_size = rb->filesize(fd);
860 /* reset pitch value to ensure synchronous playback */
861 rb->sound_set_pitch(1000);
863 /* init statistics */
864 rb->memset(&gStats, 0, sizeof(gStats));
865 gStats.minAudioAvail = gStats.minVideoAvail = INT_MAX;
866 gStats.minSpinup = INT_MAX;
868 /* init playback state */
869 rb->memset(&gPlay, 0, sizeof(gPlay));
871 /* init buffer */
872 rb->memset(&gBuf, 0, sizeof(gBuf));
873 gBuf.pOSD = rb->lcd_framebuffer + LCD_WIDTH*7; /* last screen line */
874 gBuf.pBufStart = rb->plugin_get_audio_buffer((size_t *)&gBuf.bufsize);
875 /*gBuf.bufsize = 1700*1024; // test, like 2MB version!!!! */
876 gBuf.pBufFill = gBuf.pBufStart; /* all empty */
878 /* load file header */
879 read_now = sizeof(gFileHdr);
880 got_now = rb->read(fd, &gFileHdr, read_now);
881 rb->lseek(fd, 0, SEEK_SET); /* rewind to restart sector-aligned */
882 if (got_now != read_now)
884 rb->close(fd);
885 return (PLUGIN_ERROR);
888 /* check header */
889 if (gFileHdr.magic != HEADER_MAGIC)
890 { /* old file, use default info */
891 rb->memset(&gFileHdr, 0, sizeof(gFileHdr));
892 gFileHdr.blocksize = SCREENSIZE;
893 if (file_size < SCREENSIZE * FPS) /* less than a second */
894 gFileHdr.flags |= FLAG_LOOP;
895 gFileHdr.video_format = VIDEOFORMAT_RAW;
896 gFileHdr.video_width = LCD_WIDTH;
897 gFileHdr.video_height = LCD_HEIGHT;
898 gFileHdr.video_frametime = 11059200 / FPS;
899 gFileHdr.bps_peak = gFileHdr.bps_average = LCD_WIDTH * LCD_HEIGHT * FPS;
902 #if FREQ == 12000000 /* Ondio speed kludge, 625 / 576 == 12000000 / 11059200 */
903 gPlay.nFrameTimeAdjusted = (gFileHdr.video_frametime * 625) / 576;
904 #endif
906 /* continue buffer init: align the end, calc low water, read sizes */
907 gBuf.granularity = gFileHdr.blocksize;
908 while (gBuf.granularity % 512) /* common multiple of sector size */
909 gBuf.granularity *= 2;
910 gBuf.bufsize -= gBuf.bufsize % gBuf.granularity; /* round down */
911 gBuf.pBufEnd = gBuf.pBufStart + gBuf.bufsize;
912 gBuf.low_water = SPINUP_INIT * gFileHdr.bps_peak / 8000;
913 gBuf.spinup_safety = SPINUP_SAFETY * HZ / 1000; /* in time ticks */
914 if (gFileHdr.audio_min_associated < 0)
915 gBuf.high_water = 0 - gFileHdr.audio_min_associated;
916 else
917 gBuf.high_water = 1; /* never fill buffer completely, would appear empty */
918 gBuf.nReadChunk = (CHUNK + gBuf.granularity - 1); /* round up */
919 gBuf.nReadChunk -= gBuf.nReadChunk % gBuf.granularity;/* and align */
920 gBuf.nSeekChunk = rb->filesize(fd) / FF_TICKS;
921 gBuf.nSeekChunk += gBuf.granularity - 1; /* round up */
922 gBuf.nSeekChunk -= gBuf.nSeekChunk % gBuf.granularity; /* and align */
924 /* prepare video playback, if contained */
925 if (gFileHdr.video_format == VIDEOFORMAT_RAW)
927 gPlay.bHasVideo = true;
928 /* Turn off backlight timeout */
929 backlight_force_on(rb); /* backlight control in lib/helper.c */
932 /* prepare audio playback, if contained */
933 if (gFileHdr.audio_format == AUDIOFORMAT_MP3_BITSWAPPED)
935 gPlay.bHasAudio = true;
938 /* start playback by seeking to zero or resume position */
939 if (gFileHdr.resume_pos && WantResume(fd)) /* ask the user */
940 SeekTo(fd, gFileHdr.resume_pos);
941 else
942 SeekTo(fd, 0);
944 /* all that's left to do is keep the buffer full */
945 do /* the main loop */
947 retval = PlayTick(fd);
948 } while (retval > 0);
950 if (retval < 0) /* aborted? */
952 return PLUGIN_USB_CONNECTED;
955 #ifndef DEBUG /* for release compilations, only display the stats in case of error */
956 if (gStats.nAudioUnderruns || gStats.nVideoUnderruns)
957 #endif
959 /* display statistics */
960 rb->lcd_clear_display();
961 rb->snprintf(gPrint, sizeof(gPrint), "%d Audio Underruns", gStats.nAudioUnderruns);
962 rb->lcd_puts(0, 0, gPrint);
963 rb->snprintf(gPrint, sizeof(gPrint), "%d Video Underruns", gStats.nVideoUnderruns);
964 rb->lcd_puts(0, 1, gPrint);
965 rb->snprintf(gPrint, sizeof(gPrint), "%d MinAudio bytes", gStats.minAudioAvail);
966 rb->lcd_puts(0, 2, gPrint);
967 rb->snprintf(gPrint, sizeof(gPrint), "%d MinVideo bytes", gStats.minVideoAvail);
968 rb->lcd_puts(0, 3, gPrint);
969 rb->snprintf(gPrint, sizeof(gPrint), "MinSpinup %ld.%02ld", gStats.minSpinup/HZ, gStats.minSpinup%HZ);
970 rb->lcd_puts(0, 4, gPrint);
971 rb->snprintf(gPrint, sizeof(gPrint), "MaxSpinup %ld.%02ld", gStats.maxSpinup/HZ, gStats.maxSpinup%HZ);
972 rb->lcd_puts(0, 5, gPrint);
973 rb->snprintf(gPrint, sizeof(gPrint), "LowWater: %d", gBuf.low_water);
974 rb->lcd_puts(0, 6, gPrint);
975 rb->snprintf(gPrint, sizeof(gPrint), "HighWater: %d", gBuf.high_water);
976 rb->lcd_puts(0, 7, gPrint);
978 rb->lcd_update();
979 button = WaitForButton();
981 return (button == SYS_USB_CONNECTED) ? PLUGIN_USB_CONNECTED : PLUGIN_OK;
985 /***************** Plugin Entry Point *****************/
987 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
989 rb = api; /* copy to global api pointer */
991 if (parameter == NULL)
993 rb->splash(HZ*2, "Play .rvf file!");
994 return PLUGIN_ERROR;
997 /* now go ahead and have fun! */
998 return main((char*) parameter);
1001 #endif /* #ifdef HAVE_LCD_BITMAP */
1002 #endif /* #ifndef SIMULATOR */