summaryrefslogtreecommitdiff
path: root/apps/plugins/lib/simple_viewer.c
blob: 06cc9c1a71cf7fa45761075eb108a145d8002b54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/***************************************************************************
*             __________               __   ___.
*   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
*   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
*   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
*   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
*                     \/            \/     \/    \/            \/
* $Id$
*
* Copyright (C) 2010 Teruaki Kawashima
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/

#include "config.h"
#include "plugin.h"
#include "pluginlib_actions.h"
#include "simple_viewer.h"
#include <ctype.h>


struct view_info {
#ifdef HAVE_LCD_BITMAP
    struct font* pf;
    struct viewport scrollbar_vp; /* viewport for scrollbar */
#endif
    struct viewport vp;
    const char *title;
    const char *text;   /* displayed text */
    int display_lines;  /* number of lines can be displayed */
    int line_count;     /* number of lines */
    int line;           /* current first line */
    int start;          /* possition of first line in text  */
};

static bool isbrchr(const unsigned char *str, int len)
{
    const unsigned char *p = "!,-.:;? 、。!,.:;?―";
    if (isspace(*str))
        return true;

    while(*p)
    {
        int n = rb->utf8seek(p, 1);
        if (len == n && !rb->strncmp(p, str, len))
            return true;
        p += n;
    }
    return false;
}

static const char* get_next_line(const char *text, struct view_info *info)
{
    const char *ptr = text;
    const char *space = NULL;
    int total, n, w;
    total = 0;
    while(*ptr)
    {
#ifdef HAVE_LCD_CHARCELLS
        n = rb->utf8seek(ptr, 1);
        w = 1;
#else
        unsigned short ch;
        n = ((long)rb->utf8decode(ptr, &ch) - (long)ptr);
        if (rb->is_diacritic(ch, NULL))
            w = 0;
        else
            w = rb->font_get_width(info->pf, ch);
#endif
        if (isbrchr(ptr, n))
            space = ptr+(isspace(*ptr) || total + w <= info->vp.width? n: 0);
        if (*ptr == '\n')
        {
            ptr += n;
            break;
        }
        if (total + w > info->vp.width)
            break;
        ptr += n;
        total += w;
    }
    return *ptr && space? space: ptr;
}

static void calc_line_count(struct view_info *info)
{
    const char *ptr = info->text;
    int i = 0;
#ifdef HAVE_LCD_BITMAP
    bool scrollbar = false;
#endif

    while (*ptr)
    {
        ptr = get_next_line(ptr, info);
        i++;
#ifdef HAVE_LCD_BITMAP
        if (!scrollbar && i > info->display_lines)
        {
            ptr = info->text;
            i = 0;
            info->scrollbar_vp = info->vp;
            info->scrollbar_vp.width = rb->global_settings->scrollbar_width;
            info->vp.width -= info->scrollbar_vp.width;
            if (rb->global_settings->scrollbar != SCROLLBAR_RIGHT)
                info->vp.x = info->scrollbar_vp.width;
            else
                info->scrollbar_vp.x = info->vp.width;
            scrollbar = true;
        }
#endif
    }
    info->line_count = i;
}

static void calc_first_line(struct view_info *info, int line)
{
    const char *ptr = info->text;
    int i = 0;

    if (line > info->line_count - info->display_lines)
        line = info->line_count - info->display_lines;
    if (line < 0)
        line = 0;

    if (info->line <= line)
    {
        ptr += info->start;
        i = info->line;
    }
    while (*ptr && i < line)
    {
        ptr = get_next_line(ptr, info);
        i++;
    }
    info->start = ptr - info->text;
    info->line = i;
}

static int init_view(struct view_info *info,
                     const char *title, const char *text)
{
    rb->viewport_set_defaults(&info->vp, SCREEN_MAIN);
#ifdef HAVE_LCD_BITMAP
    info->pf = rb->font_get(FONT_UI);
    info->display_lines = info->vp.height / info->pf->height;
#else
    info->display_lines = info->vp.height;
#endif

    info->title = title;
    info->text = text;
    info->line_count = 0;
    info->line = 0;
    info->start = 0;

#ifdef HAVE_LCD_BITMAP
    /* no title for small screens. */
    if (info->display_lines < 4)
    {
        info->title = NULL;
    }
    else
    {
        info->display_lines--;
        info->vp.y += info->pf->height;
        info->vp.height -= info->pf->height;
    }
#endif

    calc_line_count(info);
    return 0;
}

static void draw_text(struct view_info *info)
{
#ifdef HAVE_LCD_BITMAP
#define OUTPUT_SIZE LCD_WIDTH+1
#else
#define OUTPUT_SIZE LCD_WIDTH*3+1
#endif
    static char output[OUTPUT_SIZE];
    const char *text, *ptr;
    int max_show, line;
    struct screen* display = rb->screens[SCREEN_MAIN];

    /* clear screen */
    display->clear_display();

#ifdef HAVE_LCD_BITMAP
    /* display title. */
    if(info->title)
    {
        display->set_viewport(NULL);
        display->puts(0, 0, info->title);
    }
#endif

    max_show = MIN(info->line_count - info->line, info->display_lines);
    text = info->text + info->start;

    display->set_viewport(&info->vp);
    for (line = 0; line < max_show; line++)
    {
        int len;
        ptr = get_next_line(text, info);
        len = ptr-text;
        while(len > 0 && isspace(text[len-1]))
            len--;
        rb->memcpy(output, text, len);
        output[len] = 0;
        display->puts(0, line, output);
        text = ptr;
    }
#ifdef HAVE_LCD_BITMAP
    if (info->line_count > info->display_lines)
    {
        display->set_viewport(&info->scrollbar_vp);
        rb->gui_scrollbar_draw(display, (info->scrollbar_vp.width? 0: 1), 0,
                info->scrollbar_vp.width - 1, info->scrollbar_vp.height,
                info->line_count, info->line, info->line + max_show,
                VERTICAL);
    }
#endif

    display->set_viewport(NULL);
    display->update();
}

static void scroll_up(struct view_info *info, int n)
{
    if (info->line <= 0)
        return;

    calc_first_line(info, info->line-n);
    draw_text(info);
}

static void scroll_down(struct view_info *info, int n)
{
    if (info->line + info->display_lines >= info->line_count)
        return;

    calc_first_line(info, info->line+n);
    draw_text(info);
}

static void scroll_to_top(struct view_info *info)
{
    if (info->line <= 0)
        return;

    calc_first_line(info, 0);
    draw_text(info);
}

static void scroll_to_bottom(struct view_info *info)
{
    if (info->line + info->display_lines >= info->line_count)
        return;

    calc_first_line(info, info->line_count - info->display_lines);
    draw_text(info);
}

int view_text(const char *title, const char *text)
{
    struct view_info info;
    const struct button_mapping *view_contexts[] = {
        pla_main_ctx,
    };
    int button;

    init_view(&info, title, text);
    draw_text(&info);

    /* wait for keypress */
    while(1)
    {
        button = pluginlib_getaction(TIMEOUT_BLOCK, view_contexts,
                                     ARRAYLEN(view_contexts));
        switch (button)
        {
        case PLA_UP:
        case PLA_UP_REPEAT:
#ifdef HAVE_SCROLLWHEEL
        case PLA_SCROLL_BACK:
        case PLA_SCROLL_BACK_REPEAT:
#endif
            scroll_up(&info, 1);
            break;
        case PLA_DOWN:
        case PLA_DOWN_REPEAT:
#ifdef HAVE_SCROLLWHEEL
        case PLA_SCROLL_FWD:
        case PLA_SCROLL_FWD_REPEAT:
#endif
            scroll_down(&info, 1);
            break;
        case PLA_LEFT:
            scroll_up(&info, info.display_lines);
            break;
        case PLA_RIGHT:
            scroll_down(&info, info.display_lines);
            break;
        case PLA_LEFT_REPEAT:
            scroll_to_top(&info);
            break;
        case PLA_RIGHT_REPEAT:
            scroll_to_bottom(&info);
            break;
        case PLA_EXIT:
        case PLA_CANCEL:
            return PLUGIN_OK;
        default:
            if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
                return PLUGIN_USB_CONNECTED;
            break;
        }
   }

    return PLUGIN_OK;
}
set to first frame starting in this block unsigned short frame_end; // offset to behind last frame ending in this block } tAudioFrameHeader; /****************** globals ******************/ static struct plugin_api* rb; /* here is a global api struct pointer */ static char gPrint[32]; /* a global printf buffer, saves stack */ // playstate static struct { enum { paused, playing, } state; bool bAudioUnderrun; bool bVideoUnderrun; bool bHasAudio; bool bHasVideo; int nTimeOSD; // OSD should stay for this many frames bool bDirtyOSD; // OSD needs redraw bool bRefilling; // set if refilling buffer bool bSeeking; int nSeekAcc; // accelleration value for seek int nSeekPos; // current file position for seek bool bDiskSleep; // disk is suspended #if FREQ == 12000000 /* Ondio speed kludge */ int nFrameTimeAdjusted; #endif } gPlay; // buffer information static struct { int bufsize; int granularity; // common multiple of block and sector size unsigned char* pBufStart; // start of ring buffer unsigned char* pBufEnd; // end of ring buffer unsigned char* pOSD; // OSD memory (112 bytes for 112*8 pixels) int vidcount; // how many video blocks are known in a row unsigned char* pBufFill; // write pointer for disk, owned by main task unsigned char* pReadVideo; // video readout, maintained by timer ISR unsigned char* pReadAudio; // audio readout, maintained by demand ISR bool bEOF; // flag for end of file int low_water; // reload threshold int high_water; // end of reload threshold int spinup_safety; // safety margin when recalculating low_water int nReadChunk; // how much data for normal buffer fill int nSeekChunk; // how much data while seeking } gBuf; // statistics static struct { int minAudioAvail; int minVideoAvail; int nAudioUnderruns; int nVideoUnderruns; long minSpinup; long maxSpinup; } gStats; tFileHeader gFileHdr; // file header /****************** implementation ******************/ // tool function: return how much playable audio/video is left int Available(unsigned char* pSnapshot) { if (pSnapshot <= gBuf.pBufFill) return gBuf.pBufFill - pSnapshot; else return gBuf.bufsize - (pSnapshot - gBuf.pBufFill); } // debug function to draw buffer indicators void DrawBuf(void) { int fill, video, audio; rb->memset(gBuf.pOSD, 0x10, LCD_WIDTH); // draw line gBuf.pOSD[0] = gBuf.pOSD[LCD_WIDTH-1] = 0xFE; // ends // calculate new tick positions fill = 1 + ((gBuf.pBufFill - gBuf.pBufStart) * (LCD_WIDTH-2)) / gBuf.bufsize; video = 1 + ((gBuf.pReadVideo - gBuf.pBufStart) * (LCD_WIDTH-2)) / gBuf.bufsize; audio = 1 + ((gBuf.pReadAudio - gBuf.pBufStart) * (LCD_WIDTH-2)) / gBuf.bufsize; gBuf.pOSD[fill] |= 0x20; // below the line, two pixels gBuf.pOSD[video] |= 0x08; // one above gBuf.pOSD[audio] |= 0x04; // two above if (gPlay.state == paused) // we have to draw ourselves rb->lcd_update_rect(0, LCD_HEIGHT-8, LCD_WIDTH, 8); else gPlay.bDirtyOSD = true; // redraw it with next timer IRQ } // helper function to draw a position indicator void DrawPosition(int pos, int total) { int w,h; int sec; // estimated seconds /* print the estimated position */ sec = pos / (gFileHdr.bps_average/8); if (sec < 100*60) /* fits into mm:ss format */ rb->snprintf(gPrint, sizeof(gPrint), "%02d:%02dm", sec/60, sec%60); else /* a very long clip, hh:mm format */ rb->snprintf(gPrint, sizeof(gPrint), "%02d:%02dh", sec/3600, (sec/60)%60); rb->lcd_puts(0, 7, gPrint); /* draw a slider over the rest of the line */ rb->lcd_getstringsize(gPrint, &w, &h); w++; rb->scrollbar(w, LCD_HEIGHT-7, LCD_WIDTH-w, 7, total, 0, pos, HORIZONTAL); if (gPlay.state == paused) // we have to draw ourselves rb->lcd_update_rect(0, LCD_HEIGHT-8, LCD_WIDTH, 8); else // let the display time do it { gPlay.nTimeOSD = 70; gPlay.bDirtyOSD = true; // redraw it with next timer IRQ } } // helper function to change the volume by a certain amount, +/- void ChangeVolume(int delta) { int vol = rb->global_settings->volume + delta; if (vol > 100) vol = 100; else if (vol < 0) vol = 0; if (vol != rb->global_settings->volume) { rb->sound_set(SOUND_VOLUME, vol); rb->global_settings->volume = vol; rb->snprintf(gPrint, sizeof(gPrint), "Vol: %d", vol); rb->lcd_puts(0, 7, gPrint); if (gPlay.state == paused) // we have to draw ourselves rb->lcd_update_rect(0, LCD_HEIGHT-8, LCD_WIDTH, 8); else // let the display time do it { gPlay.nTimeOSD = 50; // display it for 50 frames gPlay.bDirtyOSD = true; // let the refresh copy it to LCD } } } // helper function to change the LCD contrast by a certain amount, +/- void ChangeContrast(int delta) { static int mycontrast = -1; /* the "permanent" value while running */ int contrast; /* updated value */ if (mycontrast == -1) mycontrast = rb->global_settings->contrast; contrast = mycontrast + delta; if (contrast > 63) contrast = 63; else if (contrast < 5) contrast = 5; if (contrast != mycontrast) { rb->lcd_set_contrast(contrast); mycontrast = contrast; rb->snprintf(gPrint, sizeof(gPrint), "Contrast: %d", contrast); rb->lcd_puts(0, 7, gPrint); if (gPlay.state == paused) // we have to draw ourselves rb->lcd_update_rect(0, LCD_HEIGHT-8, LCD_WIDTH, 8); else // let the display time do it { gPlay.nTimeOSD = 50; // display it for 50 frames gPlay.bDirtyOSD = true; // let the refresh copy it to LCD } } } // sync the video to the current audio void SyncVideo(void) { tAudioFrameHeader* pAudioBuf; pAudioBuf = (tAudioFrameHeader*)(gBuf.pReadAudio); if (pAudioBuf->magic == AUDIO_MAGIC) { gBuf.vidcount = 0; // nothing known // sync the video position gBuf.pReadVideo = gBuf.pReadAudio + (long)pAudioBuf->associated_video * (long)gFileHdr.blocksize; // handle possible wrap if (gBuf.pReadVideo >= gBuf.pBufEnd) gBuf.pReadVideo -= gBuf.bufsize; else if (gBuf.pReadVideo < gBuf.pBufStart) gBuf.pReadVideo += gBuf.bufsize; } } // timer interrupt handler to display a frame void timer4_isr(void) { int available; tAudioFrameHeader* pAudioBuf; int height; // height to display // reduce height if we have OSD on height = gFileHdr.video_height/8; if (gPlay.nTimeOSD > 0) { gPlay.nTimeOSD--; height = MIN(LCD_HEIGHT/8-1, height); // reserve bottom line if (gPlay.bDirtyOSD) { // OSD to bottom line rb->lcd_blit(gBuf.pOSD, 0, LCD_HEIGHT/8-1, LCD_WIDTH, 1, LCD_WIDTH); gPlay.bDirtyOSD = false; } } rb->lcd_blit(gBuf.pReadVideo, 0, 0, gFileHdr.video_width, height, gFileHdr.video_width); available = Available(gBuf.pReadVideo); // loop to skip audio frame(s) while(1) { // just for the statistics if (!gBuf.bEOF && available < gStats.minVideoAvail) gStats.minVideoAvail = available; if (available <= (int)gFileHdr.blocksize) { // no data for next frame if (gBuf.bEOF && (gFileHdr.flags & FLAG_LOOP)) { // loop now, assuming the looped clip fits in memory gBuf.pReadVideo = gBuf.pBufStart + gFileHdr.video_1st_frame; // FixMe: pReadVideo is incremented below } else { gPlay.bVideoUnderrun = true; rb->plugin_unregister_timer(); // disable ourselves return; // no data available } } else // normal advance for next time { gBuf.pReadVideo += gFileHdr.blocksize; if (gBuf.pReadVideo >= gBuf.pBufEnd) gBuf.pReadVideo -= gBuf.bufsize; // wraparound available -= gFileHdr.blocksize; } if (!gPlay.bHasAudio) break; // no need to skip any audio if (gBuf.vidcount) { // we know the next is a video frame gBuf.vidcount--; break; // exit the loop } pAudioBuf = (tAudioFrameHeader*)(gBuf.pReadVideo); if (pAudioBuf->magic == AUDIO_MAGIC) { // we ran into audio, can happen after seek gBuf.vidcount = pAudioBuf->next_block; if (gBuf.vidcount) gBuf.vidcount--; // minus the audio block } } // while } // ISR function to get more mp3 data void GetMoreMp3(unsigned char** start, int* size) { int available; int advance; tAudioFrameHeader* pAudioBuf = (tAudioFrameHeader*)(gBuf.pReadAudio); advance = pAudioBuf->next_block * gFileHdr.blocksize; available = Available(gBuf.pReadAudio); // just for the statistics if (!gBuf.bEOF && available < gStats.minAudioAvail) gStats.minAudioAvail = available; if (available < advance + (int)gFileHdr.blocksize || advance == 0) { gPlay.bAudioUnderrun = true; return; // no data available } gBuf.pReadAudio += advance; if (gBuf.pReadAudio >= gBuf.pBufEnd) gBuf.pReadAudio -= gBuf.bufsize; // wraparound *start = gBuf.pReadAudio + gFileHdr.audio_headersize; *size = gFileHdr.blocksize - gFileHdr.audio_headersize; } int WaitForButton(void) { int button; do { button = rb->button_get(true); rb->default_event_handler(button); } while ((button & BUTTON_REL) && button != SYS_USB_CONNECTED); return button; } bool WantResume(int fd) { int button; rb->lcd_puts(0, 0, "Resume to this"); rb->lcd_puts(0, 1, "last position?"); rb->lcd_puts(0, 2, "PLAY = yes"); rb->lcd_puts(0, 3, "Any Other = no"); rb->lcd_puts(0, 4, " (plays from start)"); DrawPosition(gFileHdr.resume_pos, rb->filesize(fd)); rb->lcd_update(); button = WaitForButton(); return (button == VIDEO_RESUME); } int SeekTo(int fd, int nPos) { int read_now, got_now; if (gPlay.bHasAudio) rb->mp3_play_stop(); // stop audio ISR if (gPlay.bHasVideo) rb->plugin_unregister_timer(); // stop the timer rb->lseek(fd, nPos, SEEK_SET); gBuf.pBufFill = gBuf.pBufStart; // all empty gBuf.pReadVideo = gBuf.pReadAudio = gBuf.pBufStart; read_now = gBuf.low_water - 1; // less than low water, so loading will continue read_now -= read_now % gBuf.granularity; // round down to granularity got_now = rb->read(fd, gBuf.pBufFill, read_now); gBuf.bEOF = (read_now != got_now); gBuf.pBufFill += got_now; if (nPos == 0) { // we seeked to the start if (gPlay.bHasVideo) gBuf.pReadVideo += gFileHdr.video_1st_frame; if (gPlay.bHasAudio) gBuf.pReadAudio += gFileHdr.audio_1st_frame; } else { // we have to search for the positions if (gPlay.bHasAudio) // prepare audio playback, if contained { // search for audio frame while (((tAudioFrameHeader*)(gBuf.pReadAudio))->magic != AUDIO_MAGIC) gBuf.pReadAudio += gFileHdr.blocksize; if (gPlay.bHasVideo) SyncVideo(); // pick the right video for that } } // synchronous start gPlay.state = playing; if (gPlay.bHasAudio) { gPlay.bAudioUnderrun = false; rb->mp3_play_data(gBuf.pReadAudio + gFileHdr.audio_headersize, gFileHdr.blocksize - gFileHdr.audio_headersize, GetMoreMp3); rb->mp3_play_pause(true); // kickoff audio } if (gPlay.bHasVideo) { gPlay.bVideoUnderrun = false; // start display interrupt #if FREQ == 12000000 /* Ondio speed kludge */ rb->plugin_register_timer(gPlay.nFrameTimeAdjusted, 1, timer4_isr); #else rb->plugin_register_timer(gFileHdr.video_frametime, 1, timer4_isr); #endif } return 0; } // called from default_event_handler_ex() or at end of playback void Cleanup(void *fd) { rb->close(*(int*)fd); // close the file if (gPlay.bHasVideo) rb->plugin_unregister_timer(); // stop video ISR, now I can use the display again if (gPlay.bHasAudio) rb->mp3_play_stop(); // stop audio ISR // restore normal backlight setting rb->backlight_set_timeout(rb->global_settings->backlight_timeout); // restore normal contrast rb->lcd_set_contrast(rb->global_settings->contrast); } // returns >0 if continue, =0 to stop, <0 to abort (USB) int PlayTick(int fd) { int button; static int lastbutton = 0; int avail_audio = -1, avail_video = -1; int retval = 1; int filepos; // check buffer level if (gPlay.bHasAudio) avail_audio = Available(gBuf.pReadAudio); if (gPlay.bHasVideo) avail_video = Available(gBuf.pReadVideo); if ((gPlay.bHasAudio && avail_audio < gBuf.low_water) || (gPlay.bHasVideo && avail_video < gBuf.low_water)) { gPlay.bRefilling = true; /* go to refill mode */ } if ((!gPlay.bHasAudio || gPlay.bAudioUnderrun) && (!gPlay.bHasVideo || gPlay.bVideoUnderrun) && gBuf.bEOF) { if (gFileHdr.resume_pos) { // we played til the end, clear resume position gFileHdr.resume_pos = 0; rb->lseek(fd, 0, SEEK_SET); // save resume position rb->write(fd, &gFileHdr, sizeof(gFileHdr)); } Cleanup(&fd); return 0; // all expired } if (!gPlay.bRefilling || gBuf.bEOF) { // nothing to do button = rb->button_get_w_tmo(HZ/10); } else { // refill buffer int read_now, got_now; int buf_free; long spinup; // measure the spinup time // how much can we reload, don't fill completely, would appear empty buf_free = gBuf.bufsize - MAX(avail_audio, avail_video) - gBuf.high_water; if (buf_free < 0) buf_free = 0; // just for safety buf_free -= buf_free % gBuf.granularity; // round down to granularity // in one piece max. up to buffer end (wrap after that) read_now = MIN(buf_free, gBuf.pBufEnd - gBuf.pBufFill); // load piecewise, to stay responsive read_now = MIN(read_now, gBuf.nReadChunk); if (read_now == buf_free) gPlay.bRefilling = false; // last piece requested spinup = *rb->current_tick; // in case this is interesting below got_now = rb->read(fd, gBuf.pBufFill, read_now); if (got_now != read_now || read_now == 0) { gBuf.bEOF = true; gPlay.bRefilling = false; } if (gPlay.bDiskSleep) // statistics about the spinup time { spinup = *rb->current_tick - spinup; gPlay.bDiskSleep = false; if (spinup > gStats.maxSpinup) gStats.maxSpinup = spinup; if (spinup < gStats.minSpinup) gStats.minSpinup = spinup; // recalculate the low water mark from real measurements gBuf.low_water = (gStats.maxSpinup + gBuf.spinup_safety) * gFileHdr.bps_peak / 8 / HZ; } if (!gPlay.bRefilling && rb->global_settings->disk_spindown < 20) // condition for test only { rb->ata_sleep(); // no point in leaving the disk run til timeout gPlay.bDiskSleep = true; } gBuf.pBufFill += got_now; if (gBuf.pBufFill >= gBuf.pBufEnd) gBuf.pBufFill = gBuf.pBufStart; // wrap rb->yield(); // have mercy with the other threads button = rb->button_get(false); } // check keypresses if (button != BUTTON_NONE) { filepos = rb->lseek(fd, 0, SEEK_CUR); if (gPlay.bHasVideo) // video position is more accurate filepos -= Available(gBuf.pReadVideo); // take video position else filepos -= Available(gBuf.pReadAudio); // else audio switch (button) { // set exit conditions case BUTTON_OFF: if (gFileHdr.magic == HEADER_MAGIC // only if file has header && !(gFileHdr.flags & FLAG_LOOP)) // not for stills { gFileHdr.resume_pos = filepos; rb->lseek(fd, 0, SEEK_SET); // save resume position rb->write(fd, &gFileHdr, sizeof(gFileHdr)); } Cleanup(&fd); retval = 0; // signal "stopped" to caller break; case VIDEO_STOP_SEEK: #ifdef VIDEO_STOP_SEEK_PRE if (lastbutton != VIDEO_STOP_SEEK_PRE) break; #endif if (gPlay.bSeeking) { gPlay.bSeeking = false; gPlay.state = playing; SeekTo(fd, gPlay.nSeekPos); } else if (gPlay.state == playing) { gPlay.state = paused; if (gPlay.bHasAudio) rb->mp3_play_pause(false); // pause audio if (gPlay.bHasVideo) rb->plugin_unregister_timer(); // stop the timer } else if (gPlay.state == paused) { gPlay.state = playing; if (gPlay.bHasAudio) { if (gPlay.bHasVideo) SyncVideo(); rb->mp3_play_pause(true); // play audio } if (gPlay.bHasVideo) { // start the video #if FREQ == 12000000 /* Ondio speed kludge */ rb->plugin_register_timer( gPlay.nFrameTimeAdjusted, 1, timer4_isr); #else rb->plugin_register_timer( gFileHdr.video_frametime, 1, timer4_isr); #endif } } break; case BUTTON_UP: case BUTTON_UP | BUTTON_REPEAT: if (gPlay.bHasAudio) ChangeVolume(1); break; case BUTTON_DOWN: case BUTTON_DOWN | BUTTON_REPEAT: if (gPlay.bHasAudio) ChangeVolume(-1); break; case BUTTON_LEFT: case BUTTON_LEFT | BUTTON_REPEAT: if (!gPlay.bSeeking) // prepare seek { gPlay.nSeekPos = filepos; gPlay.bSeeking = true; gPlay.nSeekAcc = 0; } else if (gPlay.nSeekAcc > 0) // other direction, stop sliding gPlay.nSeekAcc = 0; else gPlay.nSeekAcc--; break; case BUTTON_RIGHT: case BUTTON_RIGHT | BUTTON_REPEAT: if (!gPlay.bSeeking) // prepare seek { gPlay.nSeekPos = filepos; gPlay.bSeeking = true; gPlay.nSeekAcc = 0; } else if (gPlay.nSeekAcc < 0) // other direction, stop sliding gPlay.nSeekAcc = 0; else gPlay.nSeekAcc++; break; #ifdef VIDEO_DEBUG case VIDEO_DEBUG: // debug key case VIDEO_DEBUG | BUTTON_REPEAT: DrawBuf(); // show buffer status gPlay.nTimeOSD = 30; gPlay.bDirtyOSD = true; break; #endif case VIDEO_CONTRAST_DOWN: // contrast down case VIDEO_CONTRAST_DOWN | BUTTON_REPEAT: if (gPlay.bHasVideo) ChangeContrast(-1); break; case VIDEO_CONTRAST_UP: // contrast up case VIDEO_CONTRAST_UP | BUTTON_REPEAT: if (gPlay.bHasVideo) ChangeContrast(1); break; default: if (rb->default_event_handler_ex(button, Cleanup, &fd) == SYS_USB_CONNECTED) retval = -1; // signal "aborted" to caller break; } lastbutton = button; } /* if (button != BUTTON_NONE) */ // handle seeking if (gPlay.bSeeking) // seeking? { if (gPlay.nSeekAcc < -MAX_ACC) gPlay.nSeekAcc = -MAX_ACC; else if (gPlay.nSeekAcc > MAX_ACC) gPlay.nSeekAcc = MAX_ACC; gPlay.nSeekPos += gPlay.nSeekAcc * gBuf.nSeekChunk; if (gPlay.nSeekPos < 0) gPlay.nSeekPos = 0; if (gPlay.nSeekPos > rb->filesize(fd) - gBuf.granularity) { gPlay.nSeekPos = rb->filesize(fd); gPlay.nSeekPos -= gPlay.nSeekPos % gBuf.granularity; } DrawPosition(gPlay.nSeekPos, rb->filesize(fd)); } // check + recover underruns if ((gPlay.bAudioUnderrun || gPlay.bVideoUnderrun) && !gBuf.bEOF) { gBuf.spinup_safety += HZ/2; // add extra spinup time for the future filepos = rb->lseek(fd, 0, SEEK_CUR); if (gPlay.bHasVideo && gPlay.bVideoUnderrun) { gStats.nVideoUnderruns++; filepos -= Available(gBuf.pReadVideo); // take video position SeekTo(fd, filepos); } else if (gPlay.bHasAudio && gPlay.bAudioUnderrun) { gStats.nAudioUnderruns++; filepos -= Available(gBuf.pReadAudio); // else audio SeekTo(fd, filepos); } } return retval; } int main(char* filename) { int file_size; int fd; /* file descriptor handle */ int read_now, got_now; int button = 0; int retval; // try to open the file fd = rb->open(filename, O_RDWR); if (fd < 0) return PLUGIN_ERROR; file_size = rb->filesize(fd); // reset pitch value to ensure synchronous playback rb->sound_set_pitch(1000); // init statistics rb->memset(&gStats, 0, sizeof(gStats)); gStats.minAudioAvail = gStats.minVideoAvail = INT_MAX; gStats.minSpinup = INT_MAX; // init playback state rb->memset(&gPlay, 0, sizeof(gPlay)); // init buffer rb->memset(&gBuf, 0, sizeof(gBuf)); gBuf.pOSD = rb->lcd_framebuffer + LCD_WIDTH*7; // last screen line gBuf.pBufStart = rb->plugin_get_audio_buffer(&gBuf.bufsize); //gBuf.bufsize = 1700*1024; // test, like 2MB version!!!! gBuf.pBufFill = gBuf.pBufStart; // all empty // load file header read_now = sizeof(gFileHdr); got_now = rb->read(fd, &gFileHdr, read_now); rb->lseek(fd, 0, SEEK_SET); // rewind to restart sector-aligned if (got_now != read_now) { rb->close(fd); return (PLUGIN_ERROR); } // check header if (gFileHdr.magic != HEADER_MAGIC) { // old file, use default info rb->memset(&gFileHdr, 0, sizeof(gFileHdr)); gFileHdr.blocksize = SCREENSIZE; if (file_size < SCREENSIZE * FPS) // less than a second gFileHdr.flags |= FLAG_LOOP; gFileHdr.video_format = VIDEOFORMAT_RAW; gFileHdr.video_width = LCD_WIDTH; gFileHdr.video_height = LCD_HEIGHT; gFileHdr.video_frametime = 11059200 / FPS; gFileHdr.bps_peak = gFileHdr.bps_average = LCD_WIDTH * LCD_HEIGHT * FPS; } #if FREQ == 12000000 /* Ondio speed kludge, 625 / 576 == 12000000 / 11059200 */ gPlay.nFrameTimeAdjusted = (gFileHdr.video_frametime * 625) / 576; #endif // continue buffer init: align the end, calc low water, read sizes gBuf.granularity = gFileHdr.blocksize; while (gBuf.granularity % 512) // common multiple of sector size gBuf.granularity *= 2; gBuf.bufsize -= gBuf.bufsize % gBuf.granularity; // round down gBuf.pBufEnd = gBuf.pBufStart + gBuf.bufsize; gBuf.low_water = SPINUP_INIT * gFileHdr.bps_peak / 8000; gBuf.spinup_safety = SPINUP_SAFETY * HZ / 1000; // in time ticks if (gFileHdr.audio_min_associated < 0) gBuf.high_water = 0 - gFileHdr.audio_min_associated; else gBuf.high_water = 1; // never fill buffer completely, would appear empty gBuf.nReadChunk = (CHUNK + gBuf.granularity - 1); // round up gBuf.nReadChunk -= gBuf.nReadChunk % gBuf.granularity;// and align gBuf.nSeekChunk = rb->filesize(fd) / FF_TICKS; gBuf.nSeekChunk += gBuf.granularity - 1; // round up gBuf.nSeekChunk -= gBuf.nSeekChunk % gBuf.granularity; // and align // prepare video playback, if contained if (gFileHdr.video_format == VIDEOFORMAT_RAW) { gPlay.bHasVideo = true; if (rb->global_settings->backlight_timeout > 0) rb->backlight_set_timeout(1); // keep the light on } // prepare audio playback, if contained if (gFileHdr.audio_format == AUDIOFORMAT_MP3_BITSWAPPED) { gPlay.bHasAudio = true; } // start playback by seeking to zero or resume position if (gFileHdr.resume_pos && WantResume(fd)) // ask the user SeekTo(fd, gFileHdr.resume_pos); else SeekTo(fd, 0); // all that's left to do is keep the buffer full do // the main loop { retval = PlayTick(fd); } while (retval > 0); if (retval < 0) // aborted? { return PLUGIN_USB_CONNECTED; } #ifndef DEBUG // for release compilations, only display the stats in case of error if (gStats.nAudioUnderruns || gStats.nVideoUnderruns) #endif { // display statistics rb->lcd_clear_display(); rb->snprintf(gPrint, sizeof(gPrint), "%d Audio Underruns", gStats.nAudioUnderruns); rb->lcd_puts(0, 0, gPrint); rb->snprintf(gPrint, sizeof(gPrint), "%d Video Underruns", gStats.nVideoUnderruns); rb->lcd_puts(0, 1, gPrint); rb->snprintf(gPrint, sizeof(gPrint), "%d MinAudio bytes", gStats.minAudioAvail); rb->lcd_puts(0, 2, gPrint); rb->snprintf(gPrint, sizeof(gPrint), "%d MinVideo bytes", gStats.minVideoAvail); rb->lcd_puts(0, 3, gPrint); rb->snprintf(gPrint, sizeof(gPrint), "MinSpinup %d.%02d", gStats.minSpinup/HZ, gStats.minSpinup%HZ); rb->lcd_puts(0, 4, gPrint); rb->snprintf(gPrint, sizeof(gPrint), "MaxSpinup %d.%02d", gStats.maxSpinup/HZ, gStats.maxSpinup%HZ); rb->lcd_puts(0, 5, gPrint); rb->snprintf(gPrint, sizeof(gPrint), "LowWater: %d", gBuf.low_water); rb->lcd_puts(0, 6, gPrint); rb->snprintf(gPrint, sizeof(gPrint), "HighWater: %d", gBuf.high_water); rb->lcd_puts(0, 7, gPrint); rb->lcd_update(); button = WaitForButton(); } return (button == SYS_USB_CONNECTED) ? PLUGIN_USB_CONNECTED : PLUGIN_OK; } /***************** Plugin Entry Point *****************/ enum plugin_status plugin_start(struct plugin_api* api, void* parameter) { /* this macro should be called as the first thing you do in the plugin. it test that the api version and model the plugin was compiled for matches the machine it is running on */ TEST_PLUGIN_API(api); rb = api; // copy to global api pointer if (parameter == NULL) { rb->splash(HZ*2, true, "Play .rvf file!"); return PLUGIN_ERROR; } // now go ahead and have fun! return main((char*) parameter); } #endif // #ifdef HAVE_LCD_BITMAP #endif // #ifndef SIMULATOR