]> git.vomp.tv Git - vompserver.git/blob - remux/tsremux.c
Cut marks support
[vompserver.git] / remux / tsremux.c
1 #include "tsremux.h"
2
3 // from VDR's remux.c
4 #define MAXNONUSEFULDATA (10*1024*1024)
5 #define SC_PICTURE 0x00  // "picture header"
6 #define VIDEO_STREAM_S   0xE0
7
8 cTSRemux::cTSRemux(bool Sync) {
9         memset(m_PROTECTION1, 0x20, PROTECTIONSIZE);
10         m_ResultCount = 0;
11         m_ResultDelivered = 0;
12         m_Synced = false;
13         m_Skipped = 0;
14         m_Sync = Sync;
15 }
16
17 cTSRemux::~cTSRemux(void) {
18 }
19
20 uchar *cTSRemux::Process(const uchar *Data, int &Count, int &Result)
21 {
22   // Remove any previously delivered data from the result buffer:
23   if (m_ResultDelivered)
24   {
25     if (m_ResultDelivered < m_ResultCount)
26       memmove(m_ResultBuffer, m_ResultBuffer + m_ResultDelivered, m_ResultCount - m_ResultDelivered);
27
28     m_ResultCount -= m_ResultDelivered;
29     m_ResultDelivered = 0;
30   }
31
32   int used = 0;
33
34         // Make sure we are looking at a TS packet:
35   while (Count > TS_SIZE)
36   {
37     if (Data[0] == 0x47 && Data[TS_SIZE] == 0x47) break;
38     Data++;
39     Count--;
40     used++;
41   }
42
43   if (used) esyslog("ERROR: skipped %d byte to sync on TS packet", used);
44
45   // Convert incoming TS data
46   for (int i = 0; i < Count; i += TS_SIZE)
47   {
48     if (Count - i < TS_SIZE) break;
49     if (Data[i] != 0x47) break;
50
51     int pid = get_pid((uint8_t*)(Data + i + 1));
52     if (Data[i + 3] & 0x10)
53     {
54       // got payload
55       PutTSPacket(pid, Data + i);
56     }
57     /*if      (pid == m_VPid)               m_VRemux->ConvertTSPacket(Data + i);
58          else if (pid == m_APid1)              m_ARemux1->ConvertTSPacket(Data + i);
59          else if (pid == m_APid2 && m_ARemux2) m_ARemux2->ConvertTSPacket(Data + i);
60          else if (pid == m_DPid1 && m_DRemux1) m_DRemux1->ConvertTSPacket(Data + i);
61          else if (pid == m_DPid2 && m_DRemux2) m_DRemux2->ConvertTSPacket(Data + i);*/
62     used += TS_SIZE;
63     if (m_ResultCount > (int)sizeof(m_ResultBuffer) / 2)
64       break;
65
66 #if 0
67     if (memcmp(m_PROTECTION1, m_PROTECTION1+(PROTECTIONSIZE/2), PROTECTIONSIZE/2)) {
68       int ow_from = -1; // counted from _end_ of protection buffer
69       int ow_to = -1; // counted from _end_ of protection buffer
70       for (int pi=0; pi<PROTECTIONSIZE; pi++) {
71         if (ow_from < 0 && m_PROTECTION1[PROTECTIONSIZE-1-pi] != 0x20) {
72           ow_from = pi;
73         }
74         if (ow_to < 0 && m_PROTECTION1[pi] != 0x20) {
75           ow_to = PROTECTIONSIZE-1-pi;
76         }
77       }
78       fprintf(stderr, "OVERWRITE detected !!! from -%d to -%d\n", ow_from, ow_to);
79       fprintf(stderr, " well, hopefully one signal 11 fewer ;-)\n");
80       memset(m_PROTECTION1, 0x20, PROTECTIONSIZE);
81     }
82 #endif
83
84   }
85   Count = used;
86
87   // When we don't need to sync, we don't need to sync :-)
88   if (!m_Sync)
89   {
90     Result = m_ResultDelivered = m_ResultCount;
91     return Result ? m_ResultBuffer : NULL;
92   }
93
94   // Check if we're getting anywhere here:
95
96   if (!m_Synced && m_Skipped >= 0) {
97      if (m_Skipped > MAXNONUSEFULDATA) {
98         esyslog("ERROR: no useful data seen within %d byte of video stream", m_Skipped);
99         m_Skipped = -1;
100         //if (exitOnFailure)
101            //cThread::EmergencyExit(true);
102         }
103      else
104         m_Skipped += Count;
105      }
106
107   // Check for frame borders:
108
109   if (m_ResultCount >= MINVIDEODATA) {
110      for (int i = 0; i < m_ResultCount; i++) {
111          if (m_ResultBuffer[i] == 0 && m_ResultBuffer[i + 1] == 0 && m_ResultBuffer[i + 2] == 1) {
112             switch (m_ResultBuffer[i + 3]) {
113               case VIDEO_STREAM_S ... VIDEO_STREAM_E:
114                    {
115                      uchar pt = NO_PICTURE;
116                      int l = ScanVideoPacket(m_ResultBuffer, m_ResultCount, i, pt);
117                      if (l < 0)
118                         return NULL; // no useful data found, wait for more
119                      if (pt != NO_PICTURE) {
120                         if (pt < I_FRAME || B_FRAME < pt)
121                            esyslog("ERROR: unknown picture type '%d'", pt);
122                         else if (!m_Synced) {
123                            if (pt == I_FRAME) {
124                               m_ResultDelivered = i; // will drop everything before this position
125                               SetBrokenLink(m_ResultBuffer + i, l);
126                               m_Synced = true;
127                               }
128                            else {
129                               m_ResultDelivered = i + l; // will drop everything before and including this packet
130                               return NULL;
131                               }
132                            }
133                         }
134                      if (m_Synced) {
135                         Result = l;
136                         uchar *p = m_ResultBuffer + m_ResultDelivered;
137                         m_ResultDelivered += l;
138                         return p;
139                         }
140                      else {
141                         m_ResultDelivered = i + l; // will drop everything before and including this packet
142                         return NULL;
143                         }
144                    }
145                    break;
146               case PRIVATE_STREAM1:
147               case AUDIO_STREAM_S ... AUDIO_STREAM_E:
148                    {
149                      int l = GetPacketLength(m_ResultBuffer, m_ResultCount, i);
150                      if (l < 0)
151                         return NULL; // no useful data found, wait for more
152
153                      /* This shouldn't be happening - why does it for some channels? */
154                      if ( l > m_ResultCount )
155                          return NULL;
156                      if (m_Synced) {
157                         Result = l;
158                         uchar *p = m_ResultBuffer + m_ResultDelivered;
159                         m_ResultDelivered += l;
160                         return p;
161                         }
162                      else {
163                         m_ResultDelivered = i + l; // will drop everything before and including this packet
164                         return NULL;
165                         }
166                    }
167                    break;
168               }
169             }
170          }
171      }
172   return NULL; // no useful data found, wait for more
173 }
174
175 int cTSRemux::ScanVideoPacket(const uchar *Data, int Count, int Offset, uchar &PictureType) {
176   // Scans the video packet starting at Offset and returns its length.
177   // If the return value is -1 the packet was not completely in the buffer.
178
179   int Length = GetPacketLength(Data, Count, Offset);
180   if (Length > 0 && Offset + Length <= Count) {
181      int i = Offset + 8; // the minimum length of the video packet header
182      i += Data[i] + 1;   // possible additional header bytes
183      for (; i < Offset + Length; i++) {
184          if (Data[i] == 0 && Data[i + 1] == 0 && Data[i + 2] == 1) {
185             switch (Data[i + 3]) {
186               case SC_PICTURE: PictureType = (Data[i + 5] >> 3) & 0x07;
187                                return Length;
188               }
189             }
190          }
191      PictureType = NO_PICTURE;
192      return Length;
193      }
194   return -1;
195 }
196
197 int cTSRemux::GetPacketLength(const uchar *Data, int Count, int Offset) {
198   // Returns the entire length of the packet starting at offset, or -1 in case of error.
199   return (Offset + 5 < Count) ? (Data[Offset + 4] << 8) + Data[Offset + 5] + 6 : -1;
200 }
201
202 void cTSRemux::SetBrokenLink(uchar *Data, int Length) {
203   if (Length > 9 && Data[0] == 0 && Data[1] == 0 && Data[2] == 1 && (Data[3] & VIDEO_STREAM_S) == VIDEO_STREAM_S) {
204      for (int i = Data[8] + 9; i < Length - 7; i++) { // +9 to skip video packet header
205          if (Data[i] == 0 && Data[i + 1] == 0 && Data[i + 2] == 1 && Data[i + 3] == 0xB8) {
206             if (!(Data[i + 7] & 0x40)) // set flag only if GOP is not closed
207                Data[i + 7] |= 0x20;
208             return;
209             }
210          }
211      dsyslog("SetBrokenLink: no GOP header found in video packet");
212      }
213   else
214      dsyslog("SetBrokenLink: no video packet in frame");
215 }