summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/ltable.h
blob: aa289148719c27feeda964cbc9be0c98e96e8ffc (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
/*
** $Id$
** Lua tables (hash)
** See Copyright Notice in lua.h
*/

#ifndef ltable_h
#define ltable_h

#include "lobject.h"


#define gnode(t,i)	(&(t)->node[i])
#define gkey(n)		(&(n)->i_key.nk)
#define gval(n)		(&(n)->i_val)
#define gnext(n)	((n)->i_key.nk.next)

#define key2tval(n)	(&(n)->i_key.tvk)


LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key);
LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash);
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize);
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
LUAI_FUNC int luaH_getn (Table *t);


#if defined(LUA_DEBUG)
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
LUAI_FUNC int luaH_isdummy (Node *n);
#endif


#endif
194' href='#n194'>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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
/***************************************************************************
*             __________               __   ___.
*   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
*   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
*   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
*   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
*                     \/            \/     \/    \/            \/
* $Id$
*
* Plugin for video playback
* Reads raw image data + audio data from a file
* !!!!!!!!!! Code Police free zone !!!!!!!!!!
*
* Copyright (C) 2003-2004 J�g Hohensohn aka [IDC]Dragon
*
* 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.
*
****************************************************************************/


/****************** imports ******************/

#include "plugin.h"
#include "sh7034.h"
#include "system.h"
#include "lib/helper.h"

#ifdef HAVE_LCD_BITMAP /* and definitely not for the Player, haha */



/* variable button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#define VIDEO_STOP_SEEK BUTTON_PLAY
#define VIDEO_RESUME BUTTON_PLAY
#define VIDEO_DEBUG BUTTON_F1
#define VIDEO_CONTRAST_DOWN BUTTON_F2
#define VIDEO_CONTRAST_UP BUTTON_F3

#elif CONFIG_KEYPAD == ONDIO_PAD
#define VIDEO_STOP_SEEK_PRE BUTTON_MENU
#define VIDEO_STOP_SEEK (BUTTON_MENU | BUTTON_REL)
#define VIDEO_RESUME BUTTON_RIGHT
#define VIDEO_CONTRAST_DOWN (BUTTON_MENU | BUTTON_DOWN)
#define VIDEO_CONTRAST_UP (BUTTON_MENU | BUTTON_UP)

#endif
/****************** constants ******************/

#define SCREENSIZE (LCD_WIDTH*LCD_HEIGHT/8) /* in bytes */
#define FPS 68 /* default fps for headerless (old video-only) file */
#define MAX_ACC 20 /* maximum FF/FR speedup */
#define FF_TICKS 3000; /* experimentally found nice */

/* trigger levels, we need about 80 kB/sec */
#define SPINUP_INIT 5000 /* from what level on to refill, in milliseconds */
#define SPINUP_SAFETY 700 /* how much on top of the measured spinup time */
#define CHUNK (1024*32) /* read size */


/****************** prototypes ******************/
void timer4_isr(void); /* IMIA4 ISR */
int check_button(void); /* determine next relative frame */


/****************** data types ******************/

/* plugins don't introduce headers, so structs are repeated from rvf_format.h */

#define HEADER_MAGIC 0x52564668 /* "RVFh" at file start  */
#define AUDIO_MAGIC  0x41756446 /* "AudF" for each audio block */
#define FILEVERSION  100 /* 1.00 */

/* format type definitions */
#define VIDEOFORMAT_NO_VIDEO       0
#define VIDEOFORMAT_RAW            1
#define AUDIOFORMAT_NO_AUDIO       0
#define AUDIOFORMAT_MP3            1
#define AUDIOFORMAT_MP3_BITSWAPPED 2

/* bit flags */
#define FLAG_LOOP 0x00000001 /* loop the playback, e.g. for stills */

typedef struct /* contains whatever might be useful to the player */
{
    /* general info (16 entries = 64 byte) */
    unsigned long magic; /* HEADER_MAGIC */
    unsigned long version; /* file version */
    unsigned long flags; /* combination of FLAG_xx */
    unsigned long blocksize; /* how many bytes per block (=video frame) */
    unsigned long bps_average; /* bits per second of the whole stream */
    unsigned long bps_peak; /* max. of above (audio may be VBR) */
    unsigned long resume_pos; /* file position to resume to */
    unsigned long reserved[9]; /* reserved, should be zero */

    /* video info (16 entries = 64 byte) */
    unsigned long video_format; /* one of VIDEOFORMAT_xxx */
    unsigned long video_1st_frame; /* byte position of first video frame */
    unsigned long video_duration; /* total length of video part, in ms */
    unsigned long video_payload_size; /* total amount of video data, in bytes */
    unsigned long video_bitrate; /* derived from resolution and frame time, in bps */
    unsigned long video_frametime; /* frame interval in 11.0592 MHz clocks */
    long video_preroll; /* video is how much ahead, in 11.0592 MHz clocks */
    unsigned long video_width; /* in pixels */
    unsigned long video_height; /* in pixels */
    unsigned long video_reserved[7]; /* reserved, should be zero */

    /* audio info (16 entries = 64 byte) */
    unsigned long audio_format; /* one of AUDIOFORMAT_xxx */
    unsigned long audio_1st_frame; /* byte position of first video frame */
    unsigned long audio_duration; /* total length of audio part, in ms */
    unsigned long audio_payload_size; /* total amount of audio data, in bytes */
    unsigned long audio_avg_bitrate; /* average audio bitrate, in bits per second */
    unsigned long audio_peak_bitrate; /* maximum bitrate */
    unsigned long audio_headersize; /* offset to payload in audio frames */
    long audio_min_associated; /* minimum offset to video frame, in bytes */
    long audio_max_associated; /* maximum offset to video frame, in bytes */
    unsigned long audio_reserved[7]; /* reserved, should be zero */

    /* more to come... ? */

    /* Note: padding up to 'blocksize' with zero following this header */
} tFileHeader;

typedef struct /* the little header for all audio blocks */
{
    unsigned long magic; /* AUDIO_MAGIC indicates an audio block */
    unsigned char previous_block; /* previous how many blocks backwards */
    unsigned char next_block; /* next how many blocks forward */
    short associated_video; /* offset to block with corresponding video */
    unsigned short frame_start; /* offset to first frame starting in this block */
    unsigned short frame_end; /* offset to behind last frame ending in this block */
} tAudioFrameHeader;



/****************** globals ******************/

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
{
    ssize_t bufsize;
    int granularity; /* common multiple of block and sector size */
    unsigned char* pBufStart; /* start of ring buffer */
    unsigned char* pBufEnd; /* end of ring buffer */
    int osd_ypos;
    int osd_height;

    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 ypos, fill, video, audio;

    rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
    rb->lcd_fillrect(0, gBuf.osd_ypos, LCD_WIDTH, gBuf.osd_height);
    rb->lcd_set_drawmode(DRMODE_SOLID);
    
    ypos = gBuf.osd_ypos + gBuf.osd_height/2 - 3; /* center vertically */

    rb->lcd_hline(1, LCD_WIDTH-2, ypos + 3);
    rb->lcd_vline(0, ypos, ypos + 6);
    rb->lcd_vline(LCD_WIDTH-1, ypos, ypos + 6);

    /* 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;

    rb->lcd_drawpixel(fill, ypos + 4);
    rb->lcd_drawpixel(video, ypos + 2);
    rb->lcd_drawpixel(audio, ypos + 1);

    if (gPlay.state == paused) /* we have to draw ourselves */
        rb->lcd_update_rect(0, gBuf.osd_ypos, LCD_WIDTH, gBuf.osd_height);
    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 */
    int ypos;

    rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
    rb->lcd_fillrect(0, gBuf.osd_ypos, LCD_WIDTH, gBuf.osd_height);
    rb->lcd_set_drawmode(DRMODE_SOLID);

    /* 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_getstringsize(gPrint, &w, &h);
    w++;
    ypos = gBuf.osd_ypos + (gBuf.osd_height - h) / 2;
    rb->lcd_putsxy(0, ypos, gPrint);

    /* draw a slider over the rest of the line */
    rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN], w, ypos, LCD_WIDTH-w,
                             h, total, 0, pos, HORIZONTAL);

    if (gPlay.state == paused) /* we have to draw ourselves */
        rb->lcd_update_rect(0, gBuf.osd_ypos, LCD_WIDTH, gBuf.osd_height);
    else /* let the display time do it */
    {
        gPlay.nTimeOSD = FPS;
        gPlay.bDirtyOSD = true; /* redraw it with next timer IRQ */
    }
}

/* Put text on OSD and activate it for 1 second */
void osd_show_text(void)
{
    int h, ypos;

    rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
    rb->lcd_fillrect(0, gBuf.osd_ypos, LCD_WIDTH, gBuf.osd_height);
    rb->lcd_set_drawmode(DRMODE_SOLID);

    rb->lcd_getstringsize(gPrint, NULL, &h);
    ypos = gBuf.osd_ypos + (gBuf.osd_height - h) / 2;
    rb->lcd_putsxy(0, ypos, gPrint);

    if (gPlay.state == paused) /* we have to draw ourselves */
        rb->lcd_update_rect(0, gBuf.osd_ypos, LCD_WIDTH, gBuf.osd_height);
    else /* let the display time do it */
    {
        gPlay.nTimeOSD = FPS; /* display it for 1 sec */
        gPlay.bDirtyOSD = true; /* let the refresh copy it to LCD */
    }
}

/* helper function to change the volume by a certain amount, +/- */
void ChangeVolume(int delta)
{
    int minvol = rb->sound_min(SOUND_VOLUME);
    int maxvol = rb->sound_max(SOUND_VOLUME);
    int vol = rb->global_settings->volume + delta;

    if (vol > maxvol) vol = maxvol;
    else if (vol < minvol) vol = minvol;
    if (vol != rb->global_settings->volume)
    {
        rb->sound_set(SOUND_VOLUME, vol);
        rb->global_settings->volume = vol;
        
        rb->snprintf(gPrint, sizeof(gPrint), "Vol: %d dB", vol);
        osd_show_text();
    }
}


/* 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);
        osd_show_text();
    }
}


/* 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;
    if (gPlay.nTimeOSD > 0)
    {
        gPlay.nTimeOSD--;
        height = MIN(gBuf.osd_ypos, height);
        if (gPlay.bDirtyOSD)
        {
            rb->lcd_update_rect(0, gBuf.osd_ypos, LCD_WIDTH, gBuf.osd_height);
            gPlay.bDirtyOSD = false;
        }
    }

    rb->lcd_blit_mono(gBuf.pReadVideo, 0, 0,
        gFileHdr.video_width, height/8, 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->timer_unregister(); /* 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, size_t* 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->timer_unregister(); /* 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->timer_register(1, NULL, gPlay.nFrameTimeAdjusted,
                           timer4_isr IF_COP(, CPU));
#else
        rb->timer_register(1, NULL, gFileHdr.video_frametime,
                           timer4_isr IF_COP(, CPU));
#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->timer_unregister(); /* stop video ISR, now I can use the display again */

    if (gPlay.bHasAudio)
        rb->mp3_play_stop(); /* stop audio ISR */

    /* Turn on backlight timeout (revert to settings) */
    backlight_use_settings(); /* backlight control in lib/helper.c */

    /* 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)