MythTV  master
pespacket.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 // Copyright (c) 2003-2004, Daniel Thor Kristjansson
3 #ifndef _PES_PACKET_H_
4 #define _PES_PACKET_H_
5 
6 /*
7  max length of PSI table = 1024 bytes
8  max length of private_section = 4096 bytes
9 */
10 
11 #include <vector>
12 using namespace std;
13 
14 #include "tspacket.h"
15 #include "mythlogging.h"
16 
17 MTV_PUBLIC unsigned char *pes_alloc(uint size);
18 MTV_PUBLIC void pes_free(unsigned char *ptr);
19 
26 {
27  protected:
30  : _pesdata(nullptr), _fullbuffer(nullptr), _psiOffset(0),
31  _ccLast(255), _pesdataSize(0), _allocSize(0), _badPacket(false)
32  { ; }
33 
34  public:
35  // does not create it's own data
36  explicit PESPacket(const unsigned char *pesdata)
37  : _pesdata(const_cast<unsigned char*>(pesdata)),
38  _fullbuffer(const_cast<unsigned char*>(pesdata)),
39  _psiOffset(0), _ccLast(255), _allocSize(0)
40  {
41  _badPacket = !VerifyCRC();
42  _pesdataSize = max(((int)Length())-1 + (PESPacket::HasCRC() ? 4 : 0), 0);
43  }
44 
45  private:
46  //const PESPacket& operator=(const PESPacket& pkt);
47  PESPacket& operator=(const PESPacket& pkt);
48 
49  public:
50  // may be modified
51  PESPacket(const PESPacket& pkt)
52  : _pesdata(nullptr),
53  _psiOffset(pkt._psiOffset),
54  _ccLast(pkt._ccLast),
55  _pesdataSize(pkt._pesdataSize),
56  _allocSize(pkt._allocSize),
57  _badPacket(pkt._badPacket)
58  { // clone
59  if (!_allocSize)
60  _allocSize = pkt._pesdataSize + (pkt._pesdata - pkt._fullbuffer);
61 
62  _fullbuffer = pes_alloc(_allocSize);
63  memcpy(_fullbuffer, pkt._fullbuffer, _allocSize);
64  _pesdata = _fullbuffer + (pkt._pesdata - pkt._fullbuffer);
65  }
66 
67  // At this point we should have the entire VCT table in buffer
68  // at (buffer - 8), and without the tspacket 4 byte header
69 
70  //if (TableID::TVCT == table_id)
71  //VirtualChannelTable vct;
72 
73  virtual ~PESPacket()
74  {
75  if (IsClone())
76  pes_free(_fullbuffer);
77 
78  _fullbuffer = nullptr;
79  _pesdata = nullptr;
80  }
81 
82  bool IsClone() const { return bool(_allocSize); }
83 
84  // return true if complete or broken
85  bool AddTSPacket(const TSPacket* tspacket, bool &broken);
86 
87  bool IsGood() const { return !_badPacket; }
88 
89  const TSHeader* tsheader() const
90  { return reinterpret_cast<const TSHeader*>(_fullbuffer); }
92  { return reinterpret_cast<TSHeader*>(_fullbuffer); }
93 
94  void GetAsTSPackets(vector<TSPacket> &output, uint cc) const;
95 
96  // _pesdata[-3] == 0, _pesdata[-2] == 0, _pesdata[-1] == 1
97  uint StreamID() const { return _pesdata[0]; }
98  uint Length() const
99  { return (_pesdata[1] & 0x0f) << 8 | _pesdata[2]; }
100  // 2 bits "10"
101  // 2 bits "PES_scrambling_control (0 not scrambled)
103  { return (_pesdata[3] & 0x30) >> 4; }
105  bool HighPriority() const { return (_pesdata[3] & 0x8) >> 3; }
107  bool DataAligned() const { return (_pesdata[3] & 0x4) >> 2; }
112  bool CopyRight() const { return (_pesdata[3] & 0x2) >> 1; }
114  bool OriginalRecording() const { return _pesdata[3] & 0x1; }
115 
117  bool HasPTS() const { return (_pesdata[4] & 0x80) >> 7; }
119  bool HasDTS() const { return (_pesdata[4] & 0x40) >> 6; }
121  bool HasESCR() const { return (_pesdata[4] & 0x20) >> 5; }
123  bool HasESR() const { return (_pesdata[4] & 0x10) >> 4; }
125  bool HasDSM() const { return (_pesdata[4] & 0x8) >> 3; }
127  bool HasACI() const { return (_pesdata[4] & 0x4) >> 2; }
129  virtual bool HasCRC() const { return (_pesdata[4] & 0x2) >> 1; }
131  bool HasExtensionFlags() const { return _pesdata[4] & 0x1; }
132 
134  uint64_t PTS(void) const
135  {
136  int i = 6;
137  return
138  (uint64_t(_pesdata[i+0] & 0x0e) << 29) |
139  (uint64_t(_pesdata[i+1] ) << 22) |
140  (uint64_t(_pesdata[i+2] & 0xfe) << 14) |
141  (uint64_t(_pesdata[i+3] ) << 7) |
142  (uint64_t(_pesdata[i+4] & 0xfe) >> 1);
143  }
145  uint64_t DTS(void) const
146  {
147  int i = 6+(HasPTS()?5:0);
148  return
149  (uint64_t(_pesdata[i+0] & 0x0e) << 29) |
150  (uint64_t(_pesdata[i+1] ) << 22) |
151  (uint64_t(_pesdata[i+2] & 0xfe) << 14) |
152  (uint64_t(_pesdata[i+3] ) << 7) |
153  (uint64_t(_pesdata[i+4] & 0xfe) >> 1);
154  }
155 
156  // 8 bits PES Header Length
157  // variable length -- pes header fields
158  // variable length -- pes data block
159 
160  uint TSSizeInBuffer() const { return _pesdataSize; }
161  uint PSIOffset() const { return _psiOffset; }
162 
163  const unsigned char* pesdata() const { return _pesdata; }
164  unsigned char* pesdata() { return _pesdata; }
165 
166  const unsigned char* data() const { return _fullbuffer; }
167  unsigned char* data() { return _fullbuffer; }
168 
169  void SetStreamID(uint id) { _pesdata[0] = id; }
170  void SetLength(uint len)
171  {
172  _pesdata[1] = (_pesdata[1] & 0xf0) | ((len>>8) & 0x0f);
173  _pesdata[2] = len & 0xff;
174  }
176  {
177  len += 4 /* for CRC */;
178  len -= 3 /* for data before data last byte of length */;
179  SetLength(len);
180  }
181 
182  void SetPSIOffset(uint offset)
183  {
184  _psiOffset = offset;
185  _pesdata = _fullbuffer + _psiOffset + 1;
186  }
187 
188  uint CRC(void) const
189  {
190  if (!HasCRC() || (Length() < 1))
191  return kTheMagicNoCRCCRC;
192  uint offset = Length() - 1;
193  return ((_pesdata[offset+0]<<24) |
194  (_pesdata[offset+1]<<16) |
195  (_pesdata[offset+2]<<8) |
196  (_pesdata[offset+3]));
197  }
198 
199  void SetCRC(uint crc)
200  {
201  if (Length() < 1)
202  return;
203 
204  uint offset = Length() - 1;
205  _pesdata[offset+0] = (crc & 0xff000000) >> 24;
206  _pesdata[offset+1] = (crc & 0x00ff0000) >> 16;
207  _pesdata[offset+2] = (crc & 0x0000ff00) >> 8;
208  _pesdata[offset+3] = (crc & 0x000000ff);
209  }
210 
211  uint CalcCRC(void) const;
212  bool VerifyCRC(void) const;
213 
214  protected:
215  void Finalize() { SetCRC(CalcCRC()); }
216 
217  unsigned char *_pesdata;
218  unsigned char *_fullbuffer;
219 
224  bool _badPacket;
225 
226  // FIXME re-read the specs and follow all negations to find out the
227  // initial value of the CRC function when its being returned
228  static const uint kTheMagicNoCRCCRC = 0xFFFFFFFF;
229 };
230 
232 {
233  public:
234  uint width(void) const { return (data[0] <<4) | (data[1]>>4); }
235  uint height(void) const { return ((data[1] & 0xf)<<8) | data[2]; }
236  uint aspectNum(void) const { return data[3] >> 4; }
237  uint fpsNum(void) const { return data[3] & 0xf; }
238  float fps(void) const { return mpeg2_fps[fpsNum()]; }
239  float aspect(bool mpeg1) const;
240 
241  private:
242  SequenceHeader() {;} // only used via reinterpret cast
244 
245  unsigned char data[11];
246  static const float mpeg1_aspect[16];
247  static const float mpeg2_aspect[16];
248  static const float mpeg2_fps[16];
249 };
250 
251 #endif // _PES_PACKET_H_
uint _ccLast
Continuity counter of last inserted TS Packet.
Definition: pespacket.h:221
Used to access the data of a Transport Stream packet.
Definition: tspacket.h:166
const unsigned char * pesdata() const
Definition: pespacket.h:163
const unsigned char * data() const
Definition: pespacket.h:166
const TSHeader * tsheader() const
Definition: pespacket.h:89
uint Length() const
Definition: pespacket.h:98
void SetLength(uint len)
Definition: pespacket.h:170
uint64_t PTS(void) const
Presentation Time Stamp, present if HasPTS() is true.
Definition: pespacket.h:134
bool HasESR() const
1 bit Elementary Stream Rate field is present
Definition: pespacket.h:123
Definition: cc.h:13
uint64_t DTS(void) const
Decode Time Stamp, present if HasDTS() is true.
Definition: pespacket.h:145
bool HasESCR() const
1 bit Elementary Stream Clock Reference field is present
Definition: pespacket.h:121
unsigned char * _fullbuffer
Pointer to allocated data.
Definition: pespacket.h:218
MTV_PUBLIC unsigned char * pes_alloc(uint size)
Definition: pespacket.cpp:330
bool CopyRight() const
1 bit If true packet may contain copy righted material and is known to have once contained materiale ...
Definition: pespacket.h:112
unsigned char * _pesdata
Pointer to PES data in full buffer.
Definition: pespacket.h:217
virtual bool HasCRC() const
1 bit Cyclic Redundancy Check present
Definition: pespacket.h:129
bool
Definition: pxsup2dast.c:30
void SetStreamID(uint id)
Definition: pespacket.h:169
PESPacket(const PESPacket &pkt)
Definition: pespacket.h:51
virtual ~PESPacket()
Definition: pespacket.h:73
PESPacket(const unsigned char *pesdata)
Definition: pespacket.h:36
unsigned int uint
Definition: compat.h:140
bool HasPTS() const
1 bit Presentation Time Stamp field is present
Definition: pespacket.h:117
Used to access header of a TSPacket.
Definition: tspacket.h:24
bool HasDTS() const
1 bit Decoding Time Stamp field is present
Definition: pespacket.h:119
static int x4
Definition: mythsocket.cpp:63
bool HighPriority() const
1 bit Indicates if this is a high priority packet
Definition: pespacket.h:105
unsigned char * data()
Definition: pespacket.h:167
void SetPSIOffset(uint offset)
Definition: pespacket.h:182
void SetTotalLength(uint len)
Definition: pespacket.h:175
uint _psiOffset
AFCOffset + StartOfFieldPointer.
Definition: pespacket.h:220
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
uint _pesdataSize
Number of data bytes (TS header + PES data)
Definition: pespacket.h:222
uint StreamID() const
Definition: pespacket.h:97
#define MTV_PUBLIC
Definition: mythtvexp.h:15
static int x2
Definition: mythsocket.cpp:61
void Finalize()
Definition: pespacket.h:215
PESPacket()
noop constructor, only for use by derived classes
Definition: pespacket.h:29
uint _allocSize
Total number of bytes we allocated.
Definition: pespacket.h:223
MTV_PUBLIC void pes_free(unsigned char *ptr)
Definition: pespacket.cpp:342
uint ScramblingControl() const
Definition: pespacket.h:102
float fps(void) const
Definition: pespacket.h:238
static uint8_t * SetLength(uint8_t *Data, int Length)
Definition: dvbci.cpp:117
bool OriginalRecording() const
1 bit Original Recording
Definition: pespacket.h:114
bool _badPacket
true if a CRC is not good yet
Definition: pespacket.h:224
unsigned char * pesdata()
Definition: pespacket.h:164
bool HasDSM() const
1 bit DSM field present (should always be false for broadcasts)
Definition: pespacket.h:125
TSHeader * tsheader()
Definition: pespacket.h:91
uint TSSizeInBuffer() const
Definition: pespacket.h:160
uint CRC(void) const
Definition: pespacket.h:188
bool IsClone() const
Definition: pespacket.h:82
bool IsGood() const
Definition: pespacket.h:87
void SetCRC(uint crc)
Definition: pespacket.h:199
bool DataAligned() const
1 bit Data alignment indicator (must be 0 for video)
Definition: pespacket.h:107
uint PSIOffset() const
Definition: pespacket.h:161
uint width(void) const
Definition: pespacket.h:234
Allows us to transform TS packets to PES packets, which are used to hold multimedia streams and very ...
Definition: pespacket.h:25
uint aspectNum(void) const
Definition: pespacket.h:236
uint height(void) const
Definition: pespacket.h:235
bool HasACI() const
1 bit Additional Copy Info field is present
Definition: pespacket.h:127
bool HasExtensionFlags() const
1 bit Extension flags are present
Definition: pespacket.h:131
#define output
uint fpsNum(void) const
Definition: pespacket.h:237