summaryrefslogtreecommitdiff
path: root/command/ts2pkt.cpp
blob: 3fbfa758d212b413a8818b4d3877d6e208425660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * ts2pkt.cpp: A program for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <stdlib.h>
#include <string.h>

extern "C"
{
#include "debug.h"
}

#include "ts2pkt.h"

cMarkAdTS2Pkt::cMarkAdTS2Pkt(const char *QueueName, int QueueSize)
{
    queue=new cMarkAdPaketQueue(QueueName,QueueSize);
    Reset();
}

cMarkAdTS2Pkt::~cMarkAdTS2Pkt()
{
    if (queue) delete queue;
}

void cMarkAdTS2Pkt::Clear()
{
    Reset();
}

bool cMarkAdTS2Pkt::Reset(int ErrIndex)
{
    sync=false;
    switch (ErrIndex)
    {
    case MA_ERR_TSSIZE:
        dsyslog("inbuf not 188 bytes");
        break;
    case MA_ERR_NOSYNC:
        dsyslog("found no sync");
        break;
    case MA_ERR_SEQ:
        dsyslog("sequence error");
        break;
    case MA_ERR_AFC:
        dsyslog("wrong AFC value");
        break;
    case MA_ERR_TOBIG:
        dsyslog("buflen > 188 bytes");
        break;
    case MA_ERR_NEG:
        dsyslog("buflen negative");
        break;
    }
    counter=-1;
    if (queue) queue->Clear();
    return false;
}

bool cMarkAdTS2Pkt::InjectVideoPES(uchar *PESData, int PESSize)
{
    if ((!PESData) || (!PESSize)) return false;

    struct PESHDR *peshdr=(struct PESHDR *) PESData;

    // first check some simple things
    if ((peshdr->Sync1!=0) && (peshdr->Sync2!=0) && (peshdr->Sync3!=1)) return false;
    if ((peshdr->StreamID & 0xF0)!=0xE0) return false;

    int Length=(peshdr->LenH<<8)+peshdr->LenL;
    if (Length) Length+=sizeof(PESHDR);
    if (Length!=PESSize) return false;

    struct PESHDROPT *peshdropt=(struct PESHDROPT *) &PESData[sizeof(struct PESHDR)];

    uchar *buf;
    int buflen;

    if (peshdropt->MarkerBits==0x2)
    {
        // we have an optional PES header
        int bpos=sizeof(struct PESHDR)+sizeof(struct PESHDROPT)+
                 peshdropt->Length;
        buf=&PESData[bpos];
        buflen=PESSize-bpos;
    }
    else
    {
        int bpos=sizeof(struct PESHDR);
        buf=&PESData[bpos];
        buflen=PESSize-bpos;
    }
    queue->Inject(buf,buflen);
    return true;
}

bool cMarkAdTS2Pkt::Process(MarkAdPid Pid, uchar *TSData, int TSSize, MarkAdPacket *Pkt)
{
    if ((!Pkt) || (!queue)) return false;

    bool ret=true;

    if (TSData)
    {
        if (TSSize!=TS_SIZE)
        {
            return Reset(MA_ERR_TSSIZE);  // we need a full packet
        }

        // check TS packet sync
        if (TSData[0]!=0x47)
        {
            return Reset(MA_ERR_NOSYNC); // no sync
        }

        struct TSHDR *tshdr = (struct TSHDR *) TSData;

        int pid = (tshdr->PidH << 8) | tshdr->PidL;
        if (Pid.Num!=pid)
        {
            return true; // not for us, but this is ok
        }

        if ((counter!=-1) && (((counter+1) & 0xF)!=tshdr->Counter))
        {
            if (counter==(int) tshdr->Counter)
            {
                return true; // duplicate paket -> just ignore
            }
            // sequence error
            ret=Reset(MA_ERR_SEQ);
            if (!tshdr->PayloadStart) return ret;
        }
        counter=tshdr->Counter;

        if (tshdr->PayloadStart)
        {
            sync=true;
        }
        if (!sync)
        {
            return false; // not synced
        }

        if ((tshdr->AFC<=0) || (tshdr->AFC>3))
        {
            return Reset(MA_ERR_AFC);
        }

        // we just ignore the infos in the adaption field (e.g. OPCR/PCR)
        if ((tshdr->AFC!=1) && (tshdr->AFC!=3))
        {
            return true;
        }

        int buflen=TS_SIZE+1;
        uchar *buf=NULL;

        if (tshdr->AFC==1)
        {
            // payload only
            buflen=TS_SIZE-sizeof(struct TSHDR);
            buf=&TSData[sizeof(struct TSHDR)];
        }

        if (tshdr->AFC==3)
        {
            // adaption field + payload
            struct TSADAPT *tsadapt = (struct TSADAPT *) &TSData[4];
            int alen=tsadapt->Len+1;
            buflen=TS_SIZE-(sizeof(struct TSHDR)+alen);
            buf=&TSData[sizeof(struct TSHDR)+alen];
        }

        if (buflen>TS_SIZE)
        {
            // size to large
            return Reset(MA_ERR_TOBIG);
        }
        if (buflen<0)
        {
            // error in size
            return Reset(MA_ERR_NEG);
        }
        if (buflen==0)
        {
            // no data?
            return false;
        }

        if (tshdr->PayloadStart)
        {
            if ((buf[0]!=0) && (buf[1]!=0))
            {
                sync=false;
                if (buflen<7) return false;
                // add a pseudo padding stream
                buf[0]=0;
                buf[1]=0;
                buf[2]=1;
                buf[3]=0xbe;
                buf[4]=buflen-6;
                buf[5]=0;
            }
        }
        queue->Put(buf,buflen);
    }
    if (!ret) return ret;
    if (Pid.Type==MARKAD_PIDTYPE_VIDEO_H264)
    {
        Pkt->Data=queue->GetPacket(&Pkt->Length,MA_PACKET_H264);
    }
    else
    {
        Pkt->Data=queue->GetPacket(&Pkt->Length,MA_PACKET_PKT);
    }
    return ret;
}