summaryrefslogtreecommitdiff
path: root/genindex/pes.h
blob: 8fe7481210ef9461ce98c84c90b26f7e6b89ce7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * pes.h:
 *
 * See the main source file 'genindex.c' for copyright information and
 * how to reach the author.
 *
 *-------------------------------------------------------------------------------
 *  Revision History
 *  $Log: pes.h,v $
 *  Revision 1.2  2016/09/01 15:37:42  richard
 *  Extensively updated to V0.2 for EN 300 743 compliant subtitles
 *  added   -b  and  -n flags
 *
 *-------------------------------------------------------------------------------
 *
 */

#ifndef __PES_H
#define __PES_H

#include "thread.h"
#include "tools.h"
#include "fifo.h"

#define PES_MIN_SIZE 4    // min. number of bytes to identify a packet
#define PES_HDR_SIZE 6    // length of PES header
#define PES_EXT_SIZE 3    // length of PES extension

#define NUM_RULESETS 4    // number of rule sets
#define NUM_RULES    256  // how many rules in every set

class cFrame;
class cRingBufferFrame;
//RF
class cDvbSubtitleAssembler; // for legacy PES recordings

class cPES : public cMutex {
protected:
  enum eRule { prPass, prSkip, prAct1, prAct2, prAct3, prAct4, prAct5, prAct6, prAct7, prAct8 };
private:
  eRule rules[NUM_RULESETS][NUM_RULES], *currRules, currRule, defaultRule;
  int currNum;
  // Statistics
  unsigned int seen[256];
  int skipped, zeros;
  int64_t totalBytes, totalSkipped, totalZeros;
  //
  enum eMode { pmNewSync, pmFastSync, pmSync, pmGetHeader, pmHeaderOk, pmPayload,
               pmRingGet, pmRingDrop, pmDataPut, pmDataReady, pmOutput };
  eMode mode, nextMode;
  uchar hbuff[PES_HDR_SIZE+PES_EXT_SIZE+256];
  uchar type;
  int have, need, old;
  bool unsavedHeader, outputHeader, redirect;
  //
  cFrame *frame;
  const uchar *outData;
  int outCount;
  int saveddata;
  //
  bool ValidRuleset(const int num);
  void Skip(uchar *data, int count=1);
  int Return(int used, int len);
  int HeaderSize(uchar *head, int len);
  int PacketSize(uchar *head, int len);
//RF
  cDvbSubtitleAssembler *dvbSubtitleAssembler;
  int dvbsub_probe(uchar *p, int size, int64_t pts);
  uchar pktbuf[KILOBYTE(64)];
  fifo_t fifo;
  int64_t ptsnow[256];
  int64_t ptsfirst[256];
  int64_t ptslast[256];
  int64_t totsubsize;
  int subspkts;
  int errpkts;

protected:
  bool SOP;        // true if we process the start of packet
  int headerSize;  // size of the header including additional header data
  uchar *header;   // the actual header
  int mpegType;    // gives type of packet 1=mpeg1 2=mpeg2
  int payloadSize; // number of data bytes in the packet
  //
  cRingBufferFrame *rb;
  //
  // Rule Management
  void UseRuleset(int num);
  int CurrentRuleset(void);
  void SetDefaultRule(eRule ru, const int num=0);
  void SetRule(uchar type, eRule ru, const int num=0);
  void SetRuleR(uchar ltype, uchar htype, eRule ru, const int num=0);
  void Reset(void);
  // Misc
  unsigned int Seen(uchar type) const;
  void ClearSeen(void);
  void Statistics(bool);
  void ModifyPaketSize(int mod);
  // Data Processing
  int Process(const uchar *data, int len);
  void Redirect(eRule ru);
  void Clear(void);
  virtual int Output(const uchar *data, int len) { return len; }
  virtual int Action1(uchar type, uchar *data, int len) { return len; }
  virtual int Action2(uchar type, uchar *data, int len) { return len; }
  virtual int Action3(uchar type, uchar *data, int len) { return len; }
  virtual int Action4(uchar type, uchar *data, int len) { return len; }
  virtual int Action5(uchar type, uchar *data, int len) { return len; }
  virtual int Action6(uchar type, uchar *data, int len) { return len; }
  virtual int Action7(uchar type, uchar *data, int len) { return len; }
  virtual int Action8(uchar type, uchar *data, int len) { return len; }
  virtual void Skipped(uchar *data, int len) {}
public:

  cPES(eRule ru=prPass);
  virtual ~cPES();
  };

inline bool PesHasPts(const uchar *p)
{
  return (p[7] & 0x80) && p[8] >= 5;
}

inline bool PesHasDts(const uchar *p)
{
  return (p[7] & 0x40) && p[8] >= 10;
}

inline void PesSetDtsbit(uchar *p)
{
  p[7] |= 0x40;
}

inline void PesSetPtsbit(uchar *p)
{
  p[7] |= 0x80;
}

inline int64_t PesGetPts(const uchar *p)
{
  return ((((int64_t)p[ 9]) & 0x0E) << 29) |
         (( (int64_t)p[10])         << 22) |
         ((((int64_t)p[11]) & 0xFE) << 14) |
         (( (int64_t)p[12])         <<  7) |
         ((((int64_t)p[13]) & 0xFE) >>  1);
}

inline int64_t PesGetDts(const uchar *p)
{
  return ((((int64_t)p[14]) & 0x0E) << 29) |
         (( (int64_t)p[15])         << 22) |
         ((((int64_t)p[16]) & 0xFE) << 14) |
         (( (int64_t)p[17])         <<  7) |
         ((((int64_t)p[18]) & 0xFE) >>  1);
}


#define MAX33BIT  0x00000001FFFFFFFFLL // max. possible value with 33 bit
#define ROUNDUP(N, S) ((N + (S>>1)) / S)

//ffmpeg 
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))

#endif

// --------- $Id: pes.h,v 1.2 2016/09/01 15:37:42 richard Exp $ ---------- END