#ifndef __DVB_SUBTITLES_DECODER2_H
#define __DVB_SUBTITLES_DECODER2_H
#include <map>
#include <list>
#include <set>
#include <stdint.h>

// forward declarations
struct subtitling_segment;
class Region;
class Object;
class Clut;
class cSubtitlesPage;

typedef std::map<int, Region*> RegionMap;
typedef std::map<int, Object*> ObjectMap;
typedef std::map<int, Clut*> ClutMap;
typedef std::list<Region*> RegionList;
typedef std::list<cSubtitlesPage*> PageList;
typedef std::set<Object*> ObjectSet;

class cDecoder
{
public:
  cDecoder();
  virtual ~cDecoder();
  void addPesData(const unsigned char* Data, int Length, bool StartsNewPacket);
  bool PageAvailable();
  cSubtitlesPage* GetPage();
  void restart();

private:

  void reset();
  unsigned int available();


  void processSegment(subtitling_segment* Segment);
  void pageComposition(subtitling_segment* Segment);
  void regionComposition(subtitling_segment* Segment);
  void clutDefinition(subtitling_segment* Segment);
  void objectData(subtitling_segment* Segment);


  virtual void modeChange();
  virtual void acqPoint();
  virtual void normalCase();
  
  bool pageCompleted();
  
  Region* RegionById(int aId);
  Object* ObjectById(int aId);
  Clut*   ClutById(int aId);


  unsigned char* segmentBuffer;
  int readIndex, writeIndex,skip;
  bool pesFound;
  ObjectMap         iObjects;
  ClutMap           iCluts;
  RegionMap         iRegions;
  RegionList        iVisibleRegions;
  PageList          iPages;

  bool              iPageDecoded;
  int               iNumRegions;
  ObjectSet         iAvailableObjects;
  ObjectSet         iRequiredObjects;
  
  int               iDecodedTimeout;
  int64_t           iPagePts;

};


#endif
