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:
29  PESPacket() = default;
30 
31  public:
32  // does not create it's own data
33  explicit PESPacket(const unsigned char *pesdata)
34  : m_pesData(const_cast<unsigned char*>(pesdata)),
35  m_fullBuffer(const_cast<unsigned char*>(pesdata))
36  {
37  m_badPacket = !VerifyCRC();
38  m_pesDataSize = max(((int)Length())-1 + (PESPacket::HasCRC() ? 4 : 0), 0);
39  }
40 
41  // Deleted functions should be public.
42  //const PESPacket& operator=(const PESPacket& pkt);
43  PESPacket& operator=(const PESPacket& pkt) = delete;
44 
45  // may be modified
46  PESPacket(const PESPacket& pkt)
47  : m_pesData(nullptr),
48  m_psiOffset(pkt.m_psiOffset),
49  m_ccLast(pkt.m_ccLast),
50  m_pesDataSize(pkt.m_pesDataSize),
51  m_allocSize(pkt.m_allocSize),
52  m_badPacket(pkt.m_badPacket)
53  { // clone
54  if (!m_allocSize)
55  m_allocSize = pkt.m_pesDataSize + (pkt.m_pesData - pkt.m_fullBuffer);
56 
57  m_fullBuffer = pes_alloc(m_allocSize);
58  memcpy(m_fullBuffer, pkt.m_fullBuffer, m_allocSize);
59  m_pesData = m_fullBuffer + (pkt.m_pesData - pkt.m_fullBuffer);
60  }
61 
62  // At this point we should have the entire VCT table in buffer
63  // at (buffer - 8), and without the tspacket 4 byte header
64 
65  //if (TableID::TVCT == table_id)
66  //VirtualChannelTable vct;
67 
68  virtual ~PESPacket()
69  {
70  if (IsClone())
71  pes_free(m_fullBuffer);
72 
73  m_fullBuffer = nullptr;
74  m_pesData = nullptr;
75  }
76 
77  bool IsClone() const { return bool(m_allocSize); }
78 
79  // return true if complete or broken
80  bool AddTSPacket(const TSPacket* tspacket, bool &broken);
81 
82  bool IsGood() const { return !m_badPacket; }
83 
84  const TSHeader* tsheader() const
85  { return reinterpret_cast<const TSHeader*>(m_fullBuffer); }
87  { return reinterpret_cast<TSHeader*>(m_fullBuffer); }
88 
89  void GetAsTSPackets(vector<TSPacket> &output, uint cc) const;
90 
91  // m_pesData[-3] == 0, m_pesData[-2] == 0, m_pesData[-1] == 1
92  uint StreamID() const { return m_pesData[0]; }
93  uint Length() const
94  { return (m_pesData[1] & 0x0f) << 8 | m_pesData[2]; }
95  // 2 bits "10"
96  // 2 bits "PES_scrambling_control (0 not scrambled)
98  { return (m_pesData[3] & 0x30) >> 4; }
100  bool HighPriority() const { return (m_pesData[3] & 0x8) >> 3; }
102  bool DataAligned() const { return (m_pesData[3] & 0x4) >> 2; }
107  bool CopyRight() const { return (m_pesData[3] & 0x2) >> 1; }
109  bool OriginalRecording() const { return m_pesData[3] & 0x1; }
110 
112  bool HasPTS() const { return (m_pesData[4] & 0x80) >> 7; }
114  bool HasDTS() const { return (m_pesData[4] & 0x40) >> 6; }
116  bool HasESCR() const { return (m_pesData[4] & 0x20) >> 5; }
118  bool HasESR() const { return (m_pesData[4] & 0x10) >> 4; }
120  bool HasDSM() const { return (m_pesData[4] & 0x8) >> 3; }
122  bool HasACI() const { return (m_pesData[4] & 0x4) >> 2; }
124  virtual bool HasCRC() const { return (m_pesData[4] & 0x2) >> 1; }
126  bool HasExtensionFlags() const { return m_pesData[4] & 0x1; }
127 
129  uint64_t PTS(void) const
130  {
131  int i = 6;
132  return
133  (uint64_t(m_pesData[i+0] & 0x0e) << 29) |
134  (uint64_t(m_pesData[i+1] ) << 22) |
135  (uint64_t(m_pesData[i+2] & 0xfe) << 14) |
136  (uint64_t(m_pesData[i+3] ) << 7) |
137  (uint64_t(m_pesData[i+4] & 0xfe) >> 1);
138  }
140  uint64_t DTS(void) const
141  {
142  int i = 6+(HasPTS()?5:0);
143  return
144  (uint64_t(m_pesData[i+0] & 0x0e) << 29) |
145  (uint64_t(m_pesData[i+1] ) << 22) |
146  (uint64_t(m_pesData[i+2] & 0xfe) << 14) |
147  (uint64_t(m_pesData[i+3] ) << 7) |
148  (uint64_t(m_pesData[i+4] & 0xfe) >> 1);
149  }
150 
151  // 8 bits PES Header Length
152  // variable length -- pes header fields
153  // variable length -- pes data block
154 
155  uint TSSizeInBuffer() const { return m_pesDataSize; }
156  uint PSIOffset() const { return m_psiOffset; }
157 
158  const unsigned char* pesdata() const { return m_pesData; }
159  unsigned char* pesdata() { return m_pesData; }
160 
161  const unsigned char* data() const { return m_fullBuffer; }
162  unsigned char* data() { return m_fullBuffer; }
163 
164  void SetStreamID(uint id) { m_pesData[0] = id; }
165  void SetLength(uint len)
166  {
167  m_pesData[1] = (m_pesData[1] & 0xf0) | ((len>>8) & 0x0f);
168  m_pesData[2] = len & 0xff;
169  }
171  {
172  len += 4 /* for CRC */;
173  len -= 3 /* for data before data last byte of length */;
174  SetLength(len);
175  }
176 
177  void SetPSIOffset(uint offset)
178  {
179  m_psiOffset = offset;
180  m_pesData = m_fullBuffer + m_psiOffset + 1;
181  }
182 
183  uint CRC(void) const
184  {
185  if (!HasCRC() || (Length() < 1))
186  return kTheMagicNoCRCCRC;
187  uint offset = Length() - 1;
188  return ((m_pesData[offset+0]<<24) |
189  (m_pesData[offset+1]<<16) |
190  (m_pesData[offset+2]<<8) |
191  (m_pesData[offset+3]));
192  }
193 
194  void SetCRC(uint crc)
195  {
196  if (Length() < 1)
197  return;
198 
199  uint offset = Length() - 1;
200  m_pesData[offset+0] = (crc & 0xff000000) >> 24;
201  m_pesData[offset+1] = (crc & 0x00ff0000) >> 16;
202  m_pesData[offset+2] = (crc & 0x0000ff00) >> 8;
203  m_pesData[offset+3] = (crc & 0x000000ff);
204  }
205 
206  uint CalcCRC(void) const;
207  bool VerifyCRC(void) const;
208 
209  protected:
210  void Finalize() { SetCRC(CalcCRC()); }
211 
212  unsigned char *m_pesData { nullptr };
213  unsigned char *m_fullBuffer { nullptr };
214 
215  uint m_psiOffset { 0 };
216  uint m_ccLast { 255 };
217  uint m_pesDataSize { 0 };
218  uint m_allocSize { 0 };
219  bool m_badPacket { false };
220 
221  // FIXME re-read the specs and follow all negations to find out the
222  // initial value of the CRC function when its being returned
223  static const uint kTheMagicNoCRCCRC = 0xFFFFFFFF;
224 };
225 
227 {
228  public:
229  uint width(void) const { return (m_data[0] <<4) | (m_data[1]>>4); }
230  uint height(void) const { return ((m_data[1] & 0xf)<<8) | m_data[2]; }
231  uint aspectNum(void) const { return m_data[3] >> 4; }
232  uint fpsNum(void) const { return m_data[3] & 0xf; }
233  float fps(void) const { return kMpeg2Fps[fpsNum()]; }
234  float aspect(bool mpeg1) const;
235 
236  private:
237  SequenceHeader() {;} // only used via reinterpret cast
239 
240  unsigned char m_data[11] {};
241  static const float kMpeg1Aspect[16];
242  static const float kMpeg2Aspect[16];
243  static const float kMpeg2Fps[16];
244 };
245 
246 #endif // _PES_PACKET_H_
Used to access the data of a Transport Stream packet.
Definition: tspacket.h:166
const unsigned char * pesdata() const
Definition: pespacket.h:158
const unsigned char * data() const
Definition: pespacket.h:161
const TSHeader * tsheader() const
Definition: pespacket.h:84
uint Length() const
Definition: pespacket.h:93
unsigned char * m_fullBuffer
Pointer to allocated data.
Definition: pespacket.h:213
void SetLength(uint len)
Definition: pespacket.h:165
uint64_t PTS(void) const
Presentation Time Stamp, present if HasPTS() is true.
Definition: pespacket.h:129
bool HasESR() const
1 bit Elementary Stream Rate field is present
Definition: pespacket.h:118
Definition: cc.h:13
uint m_pesDataSize
Number of data bytes (TS header + PES data)
Definition: pespacket.h:217
uint64_t DTS(void) const
Decode Time Stamp, present if HasDTS() is true.
Definition: pespacket.h:140
bool HasESCR() const
1 bit Elementary Stream Clock Reference field is present
Definition: pespacket.h:116
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:107
virtual bool HasCRC() const
1 bit Cyclic Redundancy Check present
Definition: pespacket.h:124
bool
Definition: pxsup2dast.c:30
void SetStreamID(uint id)
Definition: pespacket.h:164
PESPacket(const PESPacket &pkt)
Definition: pespacket.h:46
virtual ~PESPacket()
Definition: pespacket.h:68
PESPacket(const unsigned char *pesdata)
Definition: pespacket.h:33
bool HasPTS() const
1 bit Presentation Time Stamp field is present
Definition: pespacket.h:112
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:114
static int x4
Definition: mythsocket.cpp:63
bool HighPriority() const
1 bit Indicates if this is a high priority packet
Definition: pespacket.h:100
unsigned char * data()
Definition: pespacket.h:162
void SetPSIOffset(uint offset)
Definition: pespacket.h:177
void SetTotalLength(uint len)
Definition: pespacket.h:170
uint StreamID() const
Definition: pespacket.h:92
#define MTV_PUBLIC
Definition: mythtvexp.h:15
static int x2
Definition: mythsocket.cpp:61
void Finalize()
Definition: pespacket.h:210
unsigned char * m_pesData
Pointer to PES data in full buffer.
Definition: pespacket.h:212
MTV_PUBLIC void pes_free(unsigned char *ptr)
Definition: pespacket.cpp:342
uint ScramblingControl() const
Definition: pespacket.h:97
float fps(void) const
Definition: pespacket.h:233
static uint8_t * SetLength(uint8_t *Data, int Length)
Definition: dvbci.cpp:117
unsigned int uint
Definition: compat.h:140
bool OriginalRecording() const
1 bit Original Recording
Definition: pespacket.h:109
unsigned char * pesdata()
Definition: pespacket.h:159
bool HasDSM() const
1 bit DSM field present (should always be false for broadcasts)
Definition: pespacket.h:120
TSHeader * tsheader()
Definition: pespacket.h:86
uint TSSizeInBuffer() const
Definition: pespacket.h:155
uint CRC(void) const
Definition: pespacket.h:183
bool IsClone() const
Definition: pespacket.h:77
bool IsGood() const
Definition: pespacket.h:82
void SetCRC(uint crc)
Definition: pespacket.h:194
bool DataAligned() const
1 bit Data alignment indicator (must be 0 for video)
Definition: pespacket.h:102
uint PSIOffset() const
Definition: pespacket.h:156
uint width(void) const
Definition: pespacket.h:229
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:231
uint height(void) const
Definition: pespacket.h:230
bool HasACI() const
1 bit Additional Copy Info field is present
Definition: pespacket.h:122
bool HasExtensionFlags() const
1 bit Extension flags are present
Definition: pespacket.h:126
#define output
uint fpsNum(void) const
Definition: pespacket.h:232