/*
 * Analogue TV Player plugin for VDR
 *
 * Copyright (C) 2003 .. 2006 Andreas Kool <akool@gmx.de>
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 *
 * Version: $Id: player-analogtv.h,v 1.75 2005/01/09 18:16:09 akool Exp $
 * Release: $Name:  $
 *
 */

#ifndef __PLAYER_ANALOG_H
#define __PLAYER_ANALOG_H

#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <asm/types.h>
#include "videodev25.h" // from "rte/mp1e/common"
#include "videodev.h"   // from "rte/mp1e/common"

#ifdef __ANALOGTV_C
#define _EXTERN
#else
#define _EXTERN extern
#endif

#undef DEBUG

#define	VIDEO		  1
#define	AUDIO		  2

#define BUFFSIZE       4096 * 32
#define NUM_BUFS         16
#define MAX_DEV_NAME     64
#define FREQDELTA        50
#define PRIORITY        -15 // -10

#define USLEEP        40000 // 0.04 sec (use 1 for a really fast CPU ...)
#define PVRSLEEP     500000

#define MAXSOURCES       16

#define MP1E              0 // exec(Encoder) >FIFO> runEncoder() -> write_buffer() ][ read_buffer() -> VDR
#define FFMPEGTS          1 // exec(Encoder) >FIFO> ][ read(FIFO) -> VDR
#define FFMPEGPES         2 // exec(Encoder) >FIFO> runEncoder() -> write_buffer() ][ read_buffer() -> VDR
#define FAME              3 // exec(Encoder) >FIFO> runEncoder() -> write_buffer() ][ read_buffer() -> VDR
#define TEST              4
#define LAVREC            5 // exec(Encoder) >FIFO> runEncoder() -> write_buffer() ][ read_buffer() -> VDR
#define MENCODER          6 // exec(Encoder) >FIFO> runEncoder() -> write_buffer() ][ read_buffer() -> VDR
#define PVRTS             7 // read(video_device) -> VDR
#define PVRPES            8 // readPVR() -> write_buffer() ][ read_buffer() -> VDR
#define NVRECTS           9 // exec(Encoder) >FIFO> ][ read(FIFO) -> VDR
#define NVRECPES         10 // exec(Encoder) >FIFO> runEncoder() -> write_buffer() ][ read_buffer() -> VDR

#define MP1EOPTS          "-e 1" // 1:completely remove skipped frames


#define STDOUT            1
#define SYSLOG            2
#define ASFILE            3

#define TOUCH(x) x = x

#define IOCTL(fd, cmd, data)                         \
({ int __result; do __result = ioctl(fd, cmd, data); \
   while (__result == -1L && errno == EINTR); __result; })

#define ENTRIES(x) sizeof(x) / sizeof(char *)

#define isPVR   ((s.mpeg_encoder == PVRTS) || (s.mpeg_encoder == PVRPES))
#define isPVRx  ((sx.mpeg_encoder == PVRTS) || (sx.mpeg_encoder == PVRPES))
#define USERDEF           0
#define FASTEST           1
#define MEDIUM            2
#define BEST              3
#define SMALL             4
#define PVR               5
#define AUTO              6

typedef unsigned char   UC;
typedef unsigned int    UI;

typedef struct {

  ///> Picture Properties:
  int  brightness;
  int  contrast;
  int  colour;
  int  hue;
  char finetune[2048];

  ///> EPG:
  int  epg_source;      // file, pipe, hoerzu2vdr
  char epg_external[MAX_DEV_NAME]; // external programm to start
  char epg_provider[5]; // 9001, 0dc7, 0d92, FF
  int  epg_autoupdate;  // (hours)
  int  epg_language;    // none, eng, ger, fra

  ///> MPEG Encoder:
  int  mpeg_encoder;    // mp1e, ffmpeg, fame, test, lavrec, mencoder, PVR, nvrec

  int  video_bit_rate;
  int  audio_bit_rate;
  int  image_size;      // 352x288, 352x576, 480x576, 544x576, 704x576, 720x576
  int  clipping;
  int  gop;             // IBBPBBPBBPBBP, I, IPPPPP

  int  filter;          // YUV 4:2:0 fastest ...
  int  motion[2];
  int  deinterlace;

  int  profile;         // User defined, fastest, medium, best ...
  int  mpeg;            // MPEG1 MPEG2
  int  test;

  int  encode; 		// VIDEO, AUDIO

  ///> Audiodevice:
  char audio_device[MAX_DEV_NAME];
  char mixer_device[MAX_DEV_NAME];
  int  mixer_line;      // OSS Audio-Eingang (Line, CD, Video ...)
  int  mixer_volume;
  int  audio_mode;      // Mono/Stereo
  int  sample_rate;     // sampling rate
  int  analysis;

  ///> Misc:
  int  debug;
  int  logger;
  int  report;
  int  HideMainMenu;    // guess what ;-)
  int  EncoderAlwaysUp;
  int  link; 		// select() / read()

  int  memcpy_method;

  ///> PVR:
  int  framespergop;
  int  bframes;
  int  gop_closure;
  int  dnr_mode;
  int  dnr_spatial;
  int  dnr_temporal;
  int  dnr_type;
  int  bitrate_mode;
  int  aspect;
  int  pulldown;

  int  syncHack;
  int  winingHack;
  int  avHack;
  int  propHack;
  int  stutteringHack;
  int  remux;
  int  usePVR350;

  ///> Sky digibox stuff:
  int  enabledigibox;
  char redremote_device[FILENAME_MAX+1];
  int  backupHack;
  int  backupInterval;
  int  fastdigibox_change;
} SETUP;

typedef struct {
  int    freq;
  char  *channelname;
} CHANNELNAMES;


_EXTERN SETUP s, sx, sc, sy;

_EXTERN char video_device[MAX_DEV_NAME];

_EXTERN FILE *lf;

_EXTERN int  v4l;          // V4L or V4L2
_EXTERN int  lv;           // Linux-Version
_EXTERN int  ReaderLoopRunning;
_EXTERN int  dropPES;
_EXTERN	int  SetChannelDeviceDone;
_EXTERN int  EncoderNeeded; // true. if currently watching/recording a analogue channel
_EXTERN int  EncoderRunning;
_EXTERN int  Restart;

_EXTERN int  vdr_ready;    // vdr accepts TS packets
_EXTERN int  pid;

_EXTERN int  videosource;     // 0 = Television, 1 = Composite1, 2 = S-Video
_EXTERN int  lastvideosource;
_EXTERN int  videodev;
_EXTERN int  lastvideodev;
_EXTERN int  videonorm;       // 0 = PAL, 1 = NTSC, 2 = SECAM
_EXTERN int  lastvideonorm;
_EXTERN int  frequency;       // currently tuned frequency
_EXTERN int  freqOffset;
_EXTERN int  xfreqOffset;
_EXTERN char ID[64];
_EXTERN char extraOpts[64];

_EXTERN int  Apid;
_EXTERN int  Vpid;
_EXTERN int  Ppid;
_EXTERN int  Tpid;

_EXTERN int  wide;

_EXTERN int  analogModule;
_EXTERN char CPU[128];
_EXTERN double CPUspeed;
_EXTERN char capdev[128];
_EXTERN char vdrver[16];
_EXTERN char ac3ver[16];

_EXTERN char FIFO[MAX_DEV_NAME];

_EXTERN char sources[MAXSOURCES][32];

_EXTERN int  recordings;
_EXTERN int  replays;

_EXTERN time_t epg_update;
_EXTERN int  nxtvepg_killed;

_EXTERN const char *confdir;

_EXTERN void *(* fast_memcpy)(void *to, const void *from, size_t len);

#if defined(__cplusplus)
extern "C" {
#endif

void d(int level, const char *fmt, ...);
void write_buffer(UC *data, int len, void *p);
int read_buffer(UC *data, int len);
void flush_buffer(void);
int v4l_version(void);
int get_inputs(void);
int audio_free(void);
int runEncoder(int Apid, int Vpid);
int readPVR(int Apid, int Vpid);
int tune(int frequency, int freqOffset);
int setNorm(int norm);
int getPictureProperties(void);
int setPictureProperties(int brightness, int contrast, int colour, int hue);
void Assign(int mode);
int PVRencoderSettingsChanged(void);
int EncoderSettingsChanged(void);

#if defined(__cplusplus)
}
#endif

#endif // __PLAYER_ANALOG_H
