#ifndef VDR_EM84XX_DEVICE_H
#define VDR_EM84XX_DEVICE_H

#include <vdr/device.h>

#include "iframe.h"
#include "osd.h"

class cThreadEm84xx;

class cDeviceEm84xx: public cDevice {
public:
   static bool Initialize(bool iOsdHas4mb);
   static void Destruct(void);
   static cDeviceEm84xx* GetInstance(void);

private:
        bool mUsePlayDisc;
public:
        void SetUsePlayDisc(bool iUse) { mUsePlayDisc = iUse; };
        bool GetUsePlayDisc() { return mUsePlayDisc; }; 

private:
   static cDeviceEm84xx* mInstance;
   cOsdProviderEm84xx* mOsdProvider;

cIFrameDecoder mIFrameDecoder;

   cThreadEm84xx *mThread;
   bool           mIsOpen;

   bool      mInputAspectRatio169;
   //bool       mOutputAspectRatio169;
   //int     mOutputHandle43on169;
   //int     mOutputHandle169on43;
   bool      mAspectRatioDataChanged;
   bool      mSpdifForwardEnabled;

   static bool    mOsdHas4mb;

   static int  mControlDvdSpeed;

   ePlayMode    mPlayMode;

   bool     mAc3;
   int      mLastVolume;
   int      mLastSampleRate;
   int      mLastPlayMode;

protected:
   virtual void MakePrimaryDevice(bool On);


#if VDRVERSNUM >= 10338
   virtual uchar *GrabImage(int &Size, bool Jpeg = true, int Quality = -1, int SizeX = -1, int SizeY = -1);
#else
   virtual bool GrabImage(const char *FileName, bool Jpeg = true, int Quality = -1, int SizeX = -1, int SizeY = -1);
#endif
   uchar * GrabOsd(int &Size, bool Jpeg = true, int Quality = -1, int SizeX = -1, int SizeY = -1);

   virtual bool HasDecoder(void) const { return true; }
   virtual bool CanReplay(void) const { return true; }
   virtual bool SetPlayMode(ePlayMode PlayMode);
   enum Tv169Handle { _16x9_ZOOM_ON,
            _16x9_ZOOM_OFF,
            _16x9_NOTHING,
            _16x9_ZOOM_ON_169,
            _16x9_ZOOM_ON_1610,
            };
   enum Tv43Handle { _4x3_LETTERBOX,
           _4x3_PANSCAN,
           };
   
   int Tv169Handle;
        int Tv43Handle;
   
   virtual int PlayVideo(const uchar *Data, int Length);
#if VDRVERSNUM >= 10342
   virtual int PlayAudio(const uchar *Data, int Length, uchar Id);
#else
   virtual int PlayAudio(const uchar *Data, int Length);
#endif

   virtual int64_t GetSTC(void);
   virtual void TrickSpeed(int Speed);
   virtual void Clear(void);
   virtual void Play(void);
   virtual void Freeze(void);
   virtual void Mute(void);
   virtual void StillPicture(const uchar *Data, int Length);
   virtual bool Poll(cPoller &Poller, int TimeoutMs = 0);
   virtual bool Flush(int TimeoutMs = 0);

   virtual void SetVideoFormat(bool VideoFormat16_9);
   virtual eVideoSystem GetVideoSystem(void);

   virtual void SetAudioTrackDevice(eTrackType Type);
   virtual int GetAudioChannelDevice(void);
   virtual void SetAudioChannelDevice(int AudioChannel);
   virtual void SetVolumeDevice(int Volume);
   virtual void SetDigitalAudioDevice(bool On);
   
   void PlaymodeChanged(ePlayMode NewPlayMode);

   void Open(void);
   void Close(void);

   void GetAspectRatioVideoData(const uchar* Data, /*uint32_t*/ int Length);

public:
   static bool SpeedControl(int Speed);

   cDeviceEm84xx(void);
   virtual ~cDeviceEm84xx();

   void EnableOsd(bool Enable);

   
   static void InitEm84(void);
   static void Configure();

   void SwitchAspectRatio(bool Is169);
   void SetOutputAspectRatio(bool Is169);
   void SetOutputHandle43on169(int Handle);
   void SetOutputHandle169on43(int Handle);
   void SetAudioOutput(bool EnableSpdifForward);
   void SetAudioSampleRate(int SampleRate);
   void SetControlDvdSpeed(int ControlDvdSpeed);
   void StopThread();

   cIFrameDecoder* StillFrame() { return &mIFrameDecoder; };
};

#endif // VDR_EM84XX_DEVICE_H
