/*
 * 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: device.h,v 1.30 2006/04/25 17:19:34 akool Exp $
 * Release: $Name:  $
 *
 */

#ifndef DEVICE_H
#define DEVICE_H

#include <vdr/plugin.h>
#include <vdr/device.h>

#include "redremote.h"

#undef  DBG // PTS/DTS debugging
#define PVRPES_ALWAYS (s.mpeg_encoder == PVRPES) // 0

// TS:
#define TRANSPORT_ERROR     0x80
#define PAYLOAD_START	    0x40 // PUSI: Payload unit start indicator
#define TRANSPORT_PRORITY   0x20 
#define PID_MASK_HI	    0x1F
#define CONTINUITY_MASK	    0x0F 
#define SCRAMBLING1 	    0x80 
#define SCRAMBLING2 	    0x40 
#define ADAPTION_FIELD	    0x20
#define PAYLOAD		    0x10
#define PCR_FLAG       	    0x10
#define OPCR_FLAG      	    0x08

// PES: 
#define GROUP_START_CODE    0xB8
#define SYSTEM_START_CODE_S 0xB9
#define PACK_START          0xBA
#define SYS_START           0xBB
#define PROG_STREAM_MAP     0xBC
#define PRIVATE_STREAM1     0xBD
#define PADDING_STREAM      0xBE
#define PRIVATE_STREAM2     0xBF
#define AUDIO_STREAM_S      0xC0
#define AUDIO_STREAM_E      0xDF
#define VIDEO_STREAM_S      0xE0
#define VIDEO_STREAM_E      0xEF
#define ECM_STREAM          0xF0
#define EMM_STREAM          0xF1
#define DSM_CC_STREAM       0xF2
#define ISO13522_STREAM     0xF3
#define PROG_STREAM_DIR     0xFF

#define PTS_DTS_FLAGS       0xC0
#define PTS_ONLY            0x80
#define PTS_DTS             0xC0
#define ESCR_FLAG           0x20

#define MAX_PTS (0x0000000200000000ULL)

class cEncoder;

class cAnalogtvDevice : public cDevice {
private:
  int source;
  int ca;
  cEncoder *encoder;
  int pids[3];
  int PidsSeen[0x2000];
  int fd_pvr;
  long long unsigned int bytestotal;
  int skipped;
  int dupf; 
  int af, vf, pf, sf;
  int TSerrors;
  int CCErrors;
  int LastCC;
  bool dropAudio;
  int  droppedAudio; 
  bool bufferFilled; 
  uchar buf[TS_SIZE];
  cTSBuffer *tsBuffer;
  unsigned short int pid;
  uint64_t pts[3], last_pts[3];
  uint64_t dts[3], last_dts[3];
  uint64_t firstVideoPts;
  char status[3];
  uint8_t LastPts[3][5];
  uint8_t pcr[6], opcr[6];
  bool has_vi, has_ai;
#ifdef DBG
  FILE *log;
#endif
  cRedRemote *redremote;
protected:
  virtual bool SetPid(cPidHandle *Handle, int Type, bool On);
  virtual bool saveRead(int bytes);
  virtual bool TSalign(void);
  virtual bool OpenDvr(void);
  virtual void CloseDvr(void);
  virtual uint64_t transPtsDts(uint8_t *pts);
  virtual void AVsync(uchar *b, int type);
  virtual bool get_ainfo(uchar *b, int l);
  virtual bool get_vinfo(uchar *b, int l);
  virtual bool getPES(uchar *b, int l);
  virtual unsigned short int getPid(void);
  virtual bool validPid(unsigned short int pid);
  virtual bool getTS(void);
  virtual double get_idle(void);
  virtual void formatpts(int64_t pts, char *out);
  virtual void DisplayStatus(void);
  virtual bool GetTSPacket(uchar *&Data);
  virtual bool checkContinuity(void);
#if VDRVERSNUM < 10300
  cSIProcessor *sip;
#else
  cSchedules   *sip; 
#endif
public:
  cAnalogtvDevice(void);
  virtual ~cAnalogtvDevice();
  int getOffset(char *ID);
  virtual bool ProvidesSource(int Source) const;
#if VDRVERSNUM >= 10300
  virtual bool ProvidesTransponder(const cChannel *Channel) const;
#endif
  virtual bool ProvidesChannel(const cChannel *Channel, int Priority = -1, bool *NeedsSetChannel = NULL) const;
#if VDRVERSNUM >= 10300
  virtual int  ProvidesCa(const cChannel *Channel) const;
#else
  virtual int  ProvidesCa(int Ca) const;
#endif
  virtual bool SetChannelDevice(const cChannel *Channel, bool LiveView);
  virtual void SetVideoFormat(bool VideoFormat16_9);
  virtual bool GrabImageFile(const char *FileName, bool Jpeg = true, int Quality = -1, int SizeX = -1, int SizeY = -1);
  bool setup(int freq, const char *name, int amixer);
  void StopEncoder();
};

#endif // DEVICE_H
