/*
 * ____ VLDP COPYRIGHT NOTICE ____
 *
 * Copyright (C) 2001 Matt Ownby
 *
 * This file is part of VLDP, a virtual laserdisc player.
 *
 * VLDP 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.
 *
 * VLDP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// should only be used by the vldp private thread!

#ifndef VLDP_INTERNAL_H
#define VLDP_INTERNAL_H

#include "vldp.h" // for the VLDP_BOOL definition and SDL.h

#include <mpeg2.h>

// this is which version of the .dat file format we are using
#define DAT_VERSION 3

// header for the .DAT files that are generated
struct dat_header {
    uint8_t version;     // which version of the DAT file this is
    uint8_t finished;    // whether the parse finished parsing or was interrupted
    uint8_t uses_fields; // whether the stream uses fields or frames
    uint64_t length;     // length of the m2v stream
};

struct precache_entry_s {
    void *ptrBuf;         // buffer that holds precached file
    uint32_t uLength; // length (in bytes) of the buffer
    uint32_t uPos;    // our current position within the stream
};

int idle_handler(void *surface);
void blank_video();
int ivldp_got_new_command();
void ivldp_ack_command();
void ivldp_lock_handler();
void paused_handler();
void play_handler();
void ivldp_set_framerate(Uint8 frame_rate_code);
void vldp_process_sequence_header();
void idle_handler_open();
void idle_handler_precache();
void idle_handler_play();
void ivldp_respond_req_play();
void ivldp_respond_req_pause_or_step();
void ivldp_respond_req_speedchange();
void ivldp_render();
void idle_handler_search(int skip);
VLDP_BOOL ivldp_get_mpeg_frame_offsets(char *mpeg_name);
VLDP_BOOL ivldp_parse_mpeg_frame_offsets(char *datafilename, uint64_t mpeg_size);
void ivldp_update_progress_indicator(SDL_Surface *indicator, double percentage_completed);

VLDP_BOOL io_open(const char *cpszFilename);
VLDP_BOOL io_open_precached(uint32_t uIdx);
unsigned int io_read(void *buf, unsigned int uBytesToRead);
VLDP_BOOL io_seek(uint64_t uPos);
void io_close();
VLDP_BOOL io_is_open();
uint64_t io_length();

void draw_frame(const mpeg2_info_t *info);

///////////////////////////////////////

extern Uint8 s_old_req_cmdORcount; // the last value of the command byte we
                                   // received
extern int s_paused;               // whether the video is to be paused
extern int s_blanked;              // whether the mpeg video is to be blanked
extern int s_frames_to_skip; // how many frames to skip before rendering the
                             // next frame (used for P and B frames seeking)
extern int s_frames_to_skip_with_inc; // how many frames to skip while
                                      // increasing the frame number (for
                                      // multi-speed playback)
extern int s_skip_all; // skip all subsequent frames.  Used to bail out of the
                       // middle of libmpeg2, back to vldp
extern uint32_t s_uSkipAllCount; // how many frames we've skipped when
                                     // s_skip_all is enabled.
extern int s_step_forward;           // if this is set, we step forward 1 frame
extern Uint32 s_timer; // FPS timer used by the blitting code to run at the
                       // right speed
extern Uint32 s_extra_delay_ms; // any extra delay that draw_frame() will
                                // use before drawing a frame (intended for
                                // laserdisc seek delay simulation)
extern Uint32 s_uFramesShownSinceTimer; // how many frames should've been
                                        // rendered (relative to s_timer) before
                                        // we advance
extern int s_overlay_allocated; // whether the SDL overlays have been allocated

// Which frame we've skipped to (0 if we haven't skipped)
// Used in order to maintain the current frame number until the skip actually
// occurs.
extern uint32_t s_uPendingSkipFrame;

extern unsigned int s_skip_per_frame;  // how many frames to skip per frame (for
                                       // playing at 2X for example)
extern unsigned int s_stall_per_frame; // how many frames to stall per frame
                                       // (for playing at 1/2X for example)

#endif
