00001
00002
00003 #ifndef _TS_PACKET_H_
00004 #define _TS_PACKET_H_
00005
00006 #include <cstdlib>
00007 #include "mythcontext.h"
00008 #include "mythtvexp.h"
00009
00010
00011
00012 #define VIDEO_PID(bp) ((bp)+1)
00013 #define AUDIO_PID(bp) ((bp)+4)
00014 #define SYNC_BYTE 0x0047
00015
00023 class MTV_PUBLIC TSHeader
00024 {
00025 public:
00026 TSHeader()
00027 {
00028 _tsdata[0] = SYNC_BYTE;
00029
00030
00031
00032
00033 }
00034 TSHeader(int cc)
00035 {
00036 _tsdata[0] = SYNC_BYTE;
00037 SetContinuityCounter(cc);
00038
00039
00040
00041
00042 }
00043 void InitHeader(const unsigned char* header) {
00044 if (header)
00045 {
00046 _tsdata[0]=header[0];
00047 _tsdata[1]=header[1];
00048 _tsdata[2]=header[2];
00049 _tsdata[3]=header[3];
00050 }
00051 }
00052
00053
00054
00055
00056
00057 bool HasSync() const { return SYNC_BYTE == _tsdata[0]; }
00058
00059
00060 bool TransportError() const { return bool(_tsdata[1]&0x80); }
00061
00062
00063 bool PayloadStart() const { return bool(_tsdata[1]&0x40); }
00064
00065 bool Priority() const { return bool(_tsdata[1]&0x20); }
00066
00067 inline unsigned int PID() const {
00068 return ((_tsdata[1] << 8) + _tsdata[2]) & 0x1fff;
00069 }
00070
00071 unsigned int ScramblingControl() const { return (_tsdata[3] >> 6) & 0x3; }
00072
00073
00074
00075
00076
00077 unsigned int AdaptationFieldControl() const {
00078 return (_tsdata[3] >> 4) & 0x3;
00079 }
00080
00081
00082
00083 unsigned int ContinuityCounter() const { return _tsdata[3] & 0xf; }
00084
00085
00086 bool Scrambled() const { return bool(_tsdata[3]&0x80); }
00087 bool HasAdaptationField() const { return bool(_tsdata[3] & 0x20); }
00088 bool HasPayload() const { return bool(_tsdata[3] & 0x10); }
00089
00090 void SetTransportError(bool err) {
00091 if (err) _tsdata[1] |= 0x80; else _tsdata[1] &= (0xff-(0x80));
00092 }
00093 void SetPayloadStart(bool start) {
00094 if (start) _tsdata[1] |= 0x40; else _tsdata[1] &= (0xff-0x40);
00095 }
00096 void SetPriority(bool priority) {
00097 if (priority) _tsdata[1] |= 0x20; else _tsdata[1] &= (0xff-0x20);
00098 }
00099 void SetPID(unsigned int pid) {
00100 _tsdata[1] = ((pid >> 8) & 0x1F) | (_tsdata[1] & 0xE0);
00101 _tsdata[2] = (pid & 0xFF);
00102 }
00103 void SetScrambled(unsigned int scr) {
00104 _tsdata[3] = (_tsdata[3] & (0xff-(0x3<<6))) | (scr<<6);
00105 }
00106 void SetAdaptationFieldControl(unsigned int afc) {
00107 _tsdata[3] = (_tsdata[3] & 0xcf) | (afc&0x3)<<4;
00108 }
00109 void SetContinuityCounter(unsigned int cc) {
00110 _tsdata[3] = (_tsdata[3] & 0xf0) | (cc & 0xf);
00111 }
00112
00113 const unsigned char* data() const { return _tsdata; }
00114 unsigned char* data() { return _tsdata; }
00115
00116 static const unsigned int kHeaderSize;
00117 static const unsigned char kPayloadOnlyHeader[4];
00118 private:
00119 unsigned char _tsdata[4];
00120 };
00121
00127 class MTV_PUBLIC TSPacket : public TSHeader
00128 {
00129 friend class PESPacket;
00130 public:
00131
00132 TSPacket() : TSHeader() { }
00133
00134 static TSPacket* CreatePayloadOnlyPacket()
00135 {
00136 TSPacket *pkt = new TSPacket();
00137 pkt->InitHeader(kPayloadOnlyHeader);
00138 memset(pkt->_tspayload, 0xFF, kPayloadSize);
00139 pkt->SetStartOfFieldPointer(0);
00140 return pkt;
00141 }
00142
00143 inline TSPacket* CreateClone() const {
00144 TSPacket *pkt = new TSPacket();
00145 memcpy(pkt, this, kSize);
00146 return pkt;
00147 }
00148
00149 void InitPayload(const unsigned char *payload)
00150 {
00151 if (payload)
00152 memcpy(_tspayload, payload, kPayloadSize);
00153 }
00154
00155 void InitPayload(const unsigned char *payload, uint size)
00156 {
00157 if (payload)
00158 memcpy(_tspayload, payload, size);
00159 else
00160 size = 0;
00161
00162 if (size < TSPacket::kPayloadSize)
00163 memset(_tspayload + size, 0xff, TSPacket::kPayloadSize - size);
00164 }
00165
00166
00167
00168 unsigned int AFCOffset() const {
00169 return HasAdaptationField() ? _tspayload[0]+1+4 : 4;
00170 }
00171
00172
00173 unsigned int StartOfFieldPointer() const
00174 { return _tspayload[AFCOffset()-4]; }
00175 void SetStartOfFieldPointer(uint sof)
00176 { _tspayload[AFCOffset()-4] = sof; }
00177
00178 QString toString() const;
00179
00180 static const unsigned int kSize;
00181 static const unsigned int kPayloadSize;
00182 static const unsigned int kDVBEmissionSize;
00183 static const unsigned int kISDBEmissionSize;
00184 static const unsigned int k8VSBEmissionSize;
00185 static const TSPacket *kNullPacket;
00186 private:
00187 unsigned char _tspayload[184];
00188 };
00189
00193 class MTV_PUBLIC TSDVBEmissionPacket : public TSPacket
00194 {
00195 private:
00196 unsigned char _tsfec[16];
00197 };
00198
00202 class MTV_PUBLIC TSISDBEmissionPacket : public TSPacket
00203 {
00204 private:
00205 unsigned char _tsfec[16];
00206 };
00207
00211 class MTV_PUBLIC TS8VSBEmissionPacket : public TSPacket
00212 {
00213 private:
00214 unsigned char _tsfec[20];
00215 };
00216
00217 #endif // _TS_PACKET_H_