4 #define MAXNONUSEFULDATA (10*1024*1024)
5 #define SC_PICTURE 0x00 // "picture header"
6 #define VIDEO_STREAM_S 0xE0
8 cTSRemux::cTSRemux(bool Sync) {
9 memset(m_PROTECTION1, 0x20, PROTECTIONSIZE);
11 m_ResultDelivered = 0;
17 cTSRemux::~cTSRemux(void) {
20 uchar *cTSRemux::Process(const uchar *Data, int &Count, int &Result) {
21 // Remove any previously delivered data from the result buffer:
22 if (m_ResultDelivered) {
23 if (m_ResultDelivered < m_ResultCount)
24 memmove(m_ResultBuffer, m_ResultBuffer + m_ResultDelivered, m_ResultCount
26 m_ResultCount -= m_ResultDelivered;
27 m_ResultDelivered = 0;
32 // Make sure we are looking at a TS packet:
33 while (Count > TS_SIZE) {
34 if (Data[0] == 0x47 && Data[TS_SIZE] == 0x47)
41 esyslog("ERROR: skipped %d byte to sync on TS packet", used);
43 // Convert incoming TS data
44 for (int i = 0; i < Count; i += TS_SIZE) {
45 if (Count - i < TS_SIZE)
49 int pid = get_pid((uint8_t*)(Data + i + 1));
50 if (Data[i + 3] & 0x10) // got payload
51 PutTSPacket(pid, Data + i);
52 /*if (pid == m_VPid) m_VRemux->ConvertTSPacket(Data + i);
53 else if (pid == m_APid1) m_ARemux1->ConvertTSPacket(Data + i);
54 else if (pid == m_APid2 && m_ARemux2) m_ARemux2->ConvertTSPacket(Data + i);
55 else if (pid == m_DPid1 && m_DRemux1) m_DRemux1->ConvertTSPacket(Data + i);
56 else if (pid == m_DPid2 && m_DRemux2) m_DRemux2->ConvertTSPacket(Data + i);*/
58 if (m_ResultCount > (int)sizeof(m_ResultBuffer) / 2)
62 if (memcmp(m_PROTECTION1, m_PROTECTION1+(PROTECTIONSIZE/2), PROTECTIONSIZE/2)) {
63 int ow_from = -1; // counted from _end_ of protection buffer
64 int ow_to = -1; // counted from _end_ of protection buffer
65 for (int pi=0; pi<PROTECTIONSIZE; pi++) {
66 if (ow_from < 0 && m_PROTECTION1[PROTECTIONSIZE-1-pi] != 0x20) {
69 if (ow_to < 0 && m_PROTECTION1[pi] != 0x20) {
70 ow_to = PROTECTIONSIZE-1-pi;
73 fprintf(stderr, "OVERWRITE detected !!! from -%d to -%d\n", ow_from, ow_to);
74 fprintf(stderr, " well, hopefully one signal 11 fewer ;-)\n");
75 memset(m_PROTECTION1, 0x20, PROTECTIONSIZE);
82 // When we don't need to sync, we don't need to sync :-)
84 Result = m_ResultDelivered = m_ResultCount;
85 return Result ? m_ResultBuffer : NULL;
88 // Check if we're getting anywhere here:
90 if (!m_Synced && m_Skipped >= 0) {
91 if (m_Skipped > MAXNONUSEFULDATA) {
92 esyslog("ERROR: no useful data seen within %d byte of video stream", m_Skipped);
95 //cThread::EmergencyExit(true);
101 // Check for frame borders:
103 if (m_ResultCount >= MINVIDEODATA) {
104 for (int i = 0; i < m_ResultCount; i++) {
105 if (m_ResultBuffer[i] == 0 && m_ResultBuffer[i + 1] == 0 && m_ResultBuffer[i + 2] == 1) {
106 switch (m_ResultBuffer[i + 3]) {
107 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
109 uchar pt = NO_PICTURE;
110 int l = ScanVideoPacket(m_ResultBuffer, m_ResultCount, i, pt);
112 return NULL; // no useful data found, wait for more
113 if (pt != NO_PICTURE) {
114 if (pt < I_FRAME || B_FRAME < pt)
115 esyslog("ERROR: unknown picture type '%d'", pt);
116 else if (!m_Synced) {
118 m_ResultDelivered = i; // will drop everything before this position
119 SetBrokenLink(m_ResultBuffer + i, l);
123 m_ResultDelivered = i + l; // will drop everything before and including this packet
130 uchar *p = m_ResultBuffer + m_ResultDelivered;
131 m_ResultDelivered += l;
135 m_ResultDelivered = i + l; // will drop everything before and including this packet
140 case PRIVATE_STREAM1:
141 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
143 int l = GetPacketLength(m_ResultBuffer, m_ResultCount, i);
145 return NULL; // no useful data found, wait for more
147 /* This shouldn't be happening - why does it for some channels? */
148 if ( l > m_ResultCount )
152 uchar *p = m_ResultBuffer + m_ResultDelivered;
153 m_ResultDelivered += l;
157 m_ResultDelivered = i + l; // will drop everything before and including this packet
166 return NULL; // no useful data found, wait for more
169 int cTSRemux::ScanVideoPacket(const uchar *Data, int Count, int Offset, uchar &PictureType) {
170 // Scans the video packet starting at Offset and returns its length.
171 // If the return value is -1 the packet was not completely in the buffer.
173 int Length = GetPacketLength(Data, Count, Offset);
174 if (Length > 0 && Offset + Length <= Count) {
175 int i = Offset + 8; // the minimum length of the video packet header
176 i += Data[i] + 1; // possible additional header bytes
177 for (; i < Offset + Length; i++) {
178 if (Data[i] == 0 && Data[i + 1] == 0 && Data[i + 2] == 1) {
179 switch (Data[i + 3]) {
180 case SC_PICTURE: PictureType = (Data[i + 5] >> 3) & 0x07;
185 PictureType = NO_PICTURE;
191 int cTSRemux::GetPacketLength(const uchar *Data, int Count, int Offset) {
192 // Returns the entire length of the packet starting at offset, or -1 in case of error.
193 return (Offset + 5 < Count) ? (Data[Offset + 4] << 8) + Data[Offset + 5] + 6 : -1;
196 void cTSRemux::SetBrokenLink(uchar *Data, int Length) {
197 if (Length > 9 && Data[0] == 0 && Data[1] == 0 && Data[2] == 1 && (Data[3] & VIDEO_STREAM_S) == VIDEO_STREAM_S) {
198 for (int i = Data[8] + 9; i < Length - 7; i++) { // +9 to skip video packet header
199 if (Data[i] == 0 && Data[i + 1] == 0 && Data[i + 2] == 1 && Data[i + 3] == 0xB8) {
200 if (!(Data[i + 7] & 0x40)) // set flag only if GOP is not closed
205 dsyslog("SetBrokenLink: no GOP header found in video packet");
208 dsyslog("SetBrokenLink: no video packet in frame");